Network Block Device  @PACKAGE_VERSION@
nbdsrv.c
Go to the documentation of this file.
1 #include "config.h"
2 #include "nbd-debug.h"
3 
4 #include <nbdsrv.h>
5 
6 #include <assert.h>
7 #include <ctype.h>
8 #include <netdb.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <syslog.h>
13 #include <unistd.h>
14 
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 
19 #define LINELEN 256 /**< Size of static buffer used to read the
20  authorization file (yuck) */
21 
22 #include <cliserv.h>
23 
24 bool address_matches(const char* mask, const void* addr, int af, GError** err) {
25  struct addrinfo *res, *aitmp, hints;
26  char *masksep;
27  char privmask[strlen(mask)+1];
28  int masklen;
29  int addrlen = af == AF_INET ? 4 : 16;
30 
31  assert(af == AF_INET || af == AF_INET6);
32 
33  strcpy(privmask, mask);
34 
35  memset(&hints, 0, sizeof(hints));
36  hints.ai_family = AF_UNSPEC;
37  hints.ai_flags = AI_NUMERICHOST;
38 
39  if((masksep = strchr(privmask, '/'))) {
40  *masksep = '\0';
41  masklen = strtol(++masksep, NULL, 10);
42  } else {
43  masklen = addrlen * 8;
44  }
45 
46  int e;
47  if((e = getaddrinfo(privmask, NULL, &hints, &res))) {
48  g_set_error(err, NBDS_ERR, NBDS_ERR_GAI, "could not parse netmask line: %s", gai_strerror(e));
49  return false;
50  }
51  aitmp = res;
52  while(res) {
53  const uint8_t* byte_s = addr;
54  uint8_t* byte_t;
55  uint8_t mask = 0;
56  int len_left = masklen;
57  if(res->ai_family != af) {
58  goto next;
59  }
60  switch(af) {
61  case AF_INET:
62  byte_t = (uint8_t*)(&(((struct sockaddr_in*)(res->ai_addr))->sin_addr));
63  break;
64  case AF_INET6:
65  byte_t = (uint8_t*)(&(((struct sockaddr_in6*)(res->ai_addr))->sin6_addr));
66  break;
67  }
68  while(len_left >= 8) {
69  if(*byte_s != *byte_t) {
70  goto next;
71  }
72  byte_s++; byte_t++;
73  len_left -= 8;
74  }
75  if(len_left) {
76  mask = getmaskbyte(len_left);
77  if((*byte_s & mask) != (*byte_t & mask)) {
78  goto next;
79  }
80  }
81  freeaddrinfo(aitmp);
82  return true;
83  next:
84  res = res->ai_next;
85  }
86  freeaddrinfo(aitmp);
87  return false;
88 }
89 
90 uint8_t getmaskbyte(int masklen) {
91  if(masklen >= 8) {
92  return 0xFF;
93  }
94  uint8_t retval = 0;
95  for(int i = 7; i + masklen > 7; i--) {
96  retval |= 1 << i;
97  }
98 
99  return retval;
100 }
101 
102 int authorized_client(CLIENT *opts) {
103  FILE *f ;
104  char line[LINELEN];
105  char *tmp;
106  struct in_addr addr;
107  struct in_addr client;
108  struct in_addr cltemp;
109  int len;
110 
111  if ((f=fopen(opts->server->authname,"r"))==NULL) {
112  msg(LOG_INFO, "Can't open authorization file %s (%s).",
113  opts->server->authname, strerror(errno));
114  return 1 ;
115  }
116 
117  while (fgets(line,LINELEN,f)!=NULL) {
118  char* pos;
119  /* Drop comments */
120  if((pos = strchr(line, '#'))) {
121  *pos = '\0';
122  }
123  /* Skip whitespace */
124  pos = line;
125  while((*pos) && isspace(*pos)) {
126  pos++;
127  }
128  /* Skip content-free lines */
129  if(!(*pos)) {
130  continue;
131  }
132  struct sockaddr* sa = (struct sockaddr*)&opts->clientaddr;
133  if(address_matches(line, sa->sa_data, sa->sa_family, NULL)) {
134  fclose(f);
135  return 1;
136  }
137  }
138  fclose(f);
139  return 0;
140 }
141 
142 /**
143  * duplicate server
144  * @param s the old server we want to duplicate
145  * @return new duplicated server
146  **/
147 SERVER* dup_serve(const SERVER *const s) {
148  SERVER *serve = NULL;
149 
150  serve=g_new0(SERVER, 1);
151  if(serve == NULL)
152  return NULL;
153 
154  if(s->exportname)
155  serve->exportname = g_strdup(s->exportname);
156 
157  serve->expected_size = s->expected_size;
158 
159  if(s->listenaddr)
160  serve->listenaddr = g_strdup(s->listenaddr);
161 
162  serve->port = s->port;
163 
164  if(s->authname)
165  serve->authname = strdup(s->authname);
166 
167  serve->flags = s->flags;
168  serve->socket = s->socket;
169  serve->socket_family = s->socket_family;
170  serve->virtstyle = s->virtstyle;
171  serve->cidrlen = s->cidrlen;
172 
173  if(s->prerun)
174  serve->prerun = g_strdup(s->prerun);
175 
176  if(s->postrun)
177  serve->postrun = g_strdup(s->postrun);
178 
179  if(s->transactionlog)
180  serve->transactionlog = g_strdup(s->transactionlog);
181 
182  if(s->servename)
183  serve->servename = g_strdup(s->servename);
184 
185  serve->max_connections = s->max_connections;
186 
187  return serve;
188 }
189 
190 int append_serve(const SERVER *const s, GArray *const a) {
191  SERVER *ns = NULL;
192  struct addrinfo hints;
193  struct addrinfo *ai = NULL;
194  struct addrinfo *rp = NULL;
195  char host[NI_MAXHOST];
196  gchar *port = NULL;
197  int e;
198  int ret;
199 
200  assert(s != NULL);
201  if(a == NULL) {
202  return -1;
203  }
204 
205  port = g_strdup_printf("%d", s->port);
206 
207  memset(&hints,'\0',sizeof(hints));
208  hints.ai_family = AF_UNSPEC;
209  hints.ai_socktype = SOCK_STREAM;
210  hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE;
211  hints.ai_protocol = IPPROTO_TCP;
212 
213  e = getaddrinfo(s->listenaddr, port, &hints, &ai);
214 
215  if (port)
216  g_free(port);
217 
218  if(e == 0) {
219  for (rp = ai; rp != NULL; rp = rp->ai_next) {
220  e = getnameinfo(rp->ai_addr, rp->ai_addrlen, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
221 
222  if (e != 0) { // error
223  fprintf(stderr, "getnameinfo: %s\n", gai_strerror(e));
224  continue;
225  }
226 
227  // duplicate server and set listenaddr to resolved IP address
228  ns = dup_serve (s);
229  if (ns) {
230  ns->listenaddr = g_strdup(host);
231  ns->socket_family = rp->ai_family;
232  g_array_append_val(a, *ns);
233  free(ns);
234  ns = NULL;
235  }
236  }
237 
238  ret = 0;
239  } else {
240  fprintf(stderr, "getaddrinfo failed on listen host/address: %s (%s)\n", s->listenaddr ? s->listenaddr : "any", gai_strerror(e));
241  ret = -1;
242  }
243 
244  if (ai)
245  freeaddrinfo(ai);
246 
247  return ret;
248 }
249 
250 uint64_t size_autodetect(int fhandle) {
251  off_t es;
252  u64 bytes __attribute__((unused));
253  struct stat stat_buf;
254  int error;
255 
256 #ifdef HAVE_SYS_MOUNT_H
257 #ifdef HAVE_SYS_IOCTL_H
258 #ifdef BLKGETSIZE64
259  DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
260  if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) {
261  return bytes;
262  }
263 #endif /* BLKGETSIZE64 */
264 #endif /* HAVE_SYS_IOCTL_H */
265 #endif /* HAVE_SYS_MOUNT_H */
266 
267  DEBUG("looking for fhandle size with fstat\n");
268  stat_buf.st_size = 0;
269  error = fstat(fhandle, &stat_buf);
270  if (!error) {
271  /* always believe stat if a regular file as it might really
272  * be zero length */
273  if (S_ISREG(stat_buf.st_mode) || (stat_buf.st_size > 0))
274  return (uint64_t)stat_buf.st_size;
275  } else {
276  DEBUG("fstat failed: %s", strerror(errno));
277  }
278 
279  DEBUG("looking for fhandle size with lseek SEEK_END\n");
280  es = lseek(fhandle, (off_t)0, SEEK_END);
281  if (es > ((off_t)0)) {
282  return (uint64_t)es;
283  } else {
284  DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
285  }
286 
287  DEBUG("Could not find size of exported block device: %s", strerror(errno));
288  return UINT64_MAX;
289 }
290 
gchar * servename
name of the export as selected by nbd-client
Definition: nbdsrv.h:46
Variables associated with a server.
Definition: nbdsrv.h:29
uint8_t getmaskbyte(int masklen)
Gets a byte to allow for address masking.
Definition: nbdsrv.c:91
SERVER * server
The server this client is getting data from.
Definition: nbdsrv.h:63
gchar * postrun
command that will be ran after the client disconnects
Definition: nbdsrv.h:44
#define msg(prio,...)
Logging macros.
Definition: nbdsrv.h:116
struct sockaddr_storage clientaddr
peer, in binary format, network byte order
Definition: nbdsrv.h:57
int flags
flags associated with this exported file
Definition: nbdsrv.h:36
gchar * exportname
(unprocessed) filename of the file we&#39;re exporting
Definition: nbdsrv.h:30
unsigned int port
port we&#39;re exporting this file at
Definition: nbdsrv.h:34
gchar * transactionlog
filename for transaction log
Definition: nbdsrv.h:48
gchar * listenaddr
The IP address we&#39;re listening on.
Definition: nbdsrv.h:33
int socket
The socket of this server.
Definition: nbdsrv.h:37
Variables associated with a client connection.
Definition: nbdsrv.h:54
struct nbd_reply __attribute__
Failed to get address info.
Definition: nbdsrv.h:100
SERVER * dup_serve(const SERVER *const s)
duplicate server
Definition: nbdsrv.c:148
uint64_t size_autodetect(int fhandle)
Detect the size of a file.
Definition: nbdsrv.c:251
void err(const char *s) G_GNUC_NORETURN
Definition: cliserv.h:120
int max_connections
maximum number of opened connections
Definition: nbdsrv.h:47
VIRT_STYLE virtstyle
The style of virtualization, if any.
Definition: nbdsrv.h:39
int append_serve(const SERVER *const s, GArray *const a)
append new server to array
Definition: nbdsrv.c:191
int socket_family
family of the socket
Definition: nbdsrv.h:38
uint64_t expected_size
size of the exported file as it was told to us through configuration
Definition: nbdsrv.h:31
gchar * prerun
command to be ran after connecting a client, but before starting to serve
Definition: nbdsrv.h:42
bool address_matches(const char *mask, const void *addr, int af, GError **err)
Check whether a given address matches a given netmask.
Definition: nbdsrv.c:25
#define DEBUG(...)
Definition: nbd-debug.h:8
char * authname
filename of the authorization file
Definition: nbdsrv.h:35
#define LINELEN
Size of static buffer used to read the authorization file (yuck)
Definition: nbdsrv.c:19
#define NBDS_ERR
Error domain common for all NBD server errors.
Definition: nbdsrv.h:80
uint8_t cidrlen
The length of the mask when we use CIDR-style virtualization.
Definition: nbdsrv.h:40
__be32 len
Definition: nbd.h:41
int authorized_client(CLIENT *opts)
Check whether a client is allowed to connect.
Definition: nbdsrv.c:103