Don't fail out prematurely
[nbd.git] / nbdsrv.c
blobc44bb20bb4db2c2198b6ad657aa39646c57618df
1 #include "config.h"
2 #include "nbd-debug.h"
4 #include <nbdsrv.h>
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>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <treefiles.h>
19 #include "backend.h"
21 #define LINELEN 256 /**< Size of static buffer used to read the
22 authorization file (yuck) */
24 #include <cliserv.h>
26 bool address_matches(const char* mask, const struct sockaddr* addr, GError** err) {
27 struct addrinfo *res, *aitmp, hints;
28 char *masksep;
29 char privmask[strlen(mask)+1];
30 int masklen;
31 int addrlen = addr->sa_family == AF_INET ? 4 : 16;
33 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
35 strcpy(privmask, mask);
37 memset(&hints, 0, sizeof(hints));
38 hints.ai_family = AF_UNSPEC;
39 hints.ai_flags = AI_NUMERICHOST;
41 if((masksep = strchr(privmask, '/'))) {
42 *masksep = '\0';
43 masklen = strtol(++masksep, NULL, 10);
44 } else {
45 masklen = addrlen * 8;
48 int e;
49 if((e = getaddrinfo(privmask, NULL, &hints, &res))) {
50 g_set_error(err, NBDS_ERR, NBDS_ERR_GAI, "could not parse netmask line: %s", gai_strerror(e));
51 return false;
53 aitmp = res;
54 while(res) {
55 const uint8_t* byte_s;
56 uint8_t* byte_t;
57 uint8_t mask = 0;
58 int len_left = masklen;
59 if(res->ai_family != addr->sa_family) {
60 goto next;
62 switch(addr->sa_family) {
63 case AF_INET:
64 byte_s = (const uint8_t*)(&(((struct sockaddr_in*)addr)->sin_addr));
65 byte_t = (uint8_t*)(&(((struct sockaddr_in*)(res->ai_addr))->sin_addr));
66 break;
67 case AF_INET6:
68 byte_s = (const uint8_t*)(&(((struct sockaddr_in6*)addr)->sin6_addr));
69 byte_t = (uint8_t*)(&(((struct sockaddr_in6*)(res->ai_addr))->sin6_addr));
70 break;
72 while(len_left >= 8) {
73 if(*byte_s != *byte_t) {
74 goto next;
76 byte_s++; byte_t++;
77 len_left -= 8;
79 if(len_left) {
80 mask = getmaskbyte(len_left);
81 if((*byte_s & mask) != (*byte_t & mask)) {
82 goto next;
85 freeaddrinfo(aitmp);
86 return true;
87 next:
88 res = res->ai_next;
90 freeaddrinfo(aitmp);
91 return false;
94 uint8_t getmaskbyte(int masklen) {
95 if(masklen >= 8) {
96 return 0xFF;
98 uint8_t retval = 0;
99 for(int i = 7; i + masklen > 7; i--) {
100 retval |= 1 << i;
103 return retval;
106 int authorized_client(CLIENT *opts) {
107 FILE *f ;
108 char line[LINELEN];
110 if (opts->server->authname == NULL) {
111 msg(LOG_INFO, "No authorization file, granting access.");
112 return 1;
115 if ((f=fopen(opts->server->authname,"r"))==NULL) {
116 msg(LOG_INFO, "Can't open authorization file %s (%s).",
117 opts->server->authname, strerror(errno));
118 return 1 ;
121 while (fgets(line,LINELEN,f)!=NULL) {
122 char* pos;
123 /* Drop comments */
124 if((pos = strchr(line, '#'))) {
125 *pos = '\0';
127 /* Skip whitespace */
128 pos = line;
129 while((*pos) && isspace(*pos)) {
130 pos++;
132 /* Skip content-free lines */
133 if(!(*pos)) {
134 continue;
136 if(address_matches(line, (struct sockaddr*)&opts->clientaddr, NULL)) {
137 fclose(f);
138 return 1;
141 fclose(f);
142 return 0;
146 * duplicate server
147 * @param s the old server we want to duplicate
148 * @return new duplicated server
150 SERVER* dup_serve(const SERVER *const s) {
151 SERVER *serve = NULL;
153 serve=g_new0(SERVER, 1);
154 if(serve == NULL)
155 return NULL;
157 if(s->exportname)
158 serve->exportname = g_strdup(s->exportname);
160 serve->expected_size = s->expected_size;
162 if(s->listenaddr)
163 serve->listenaddr = g_strdup(s->listenaddr);
165 if(s->authname)
166 serve->authname = g_strdup(s->authname);
168 serve->flags = s->flags;
169 serve->virtstyle = s->virtstyle;
170 serve->cidrlen = s->cidrlen;
172 if(s->prerun)
173 serve->prerun = g_strdup(s->prerun);
175 if(s->postrun)
176 serve->postrun = g_strdup(s->postrun);
178 if(s->transactionlog)
179 serve->transactionlog = g_strdup(s->transactionlog);
181 if(s->servename)
182 serve->servename = g_strdup(s->servename);
184 if(s->cowdir)
185 serve->cowdir = g_strdup(s->cowdir);
187 serve->max_connections = s->max_connections;
189 return serve;
192 uint64_t size_autodetect(int fhandle) {
193 off_t es;
194 u64 bytes __attribute__((unused));
195 struct stat stat_buf;
196 int error;
198 #ifdef HAVE_SYS_MOUNT_H
199 #ifdef HAVE_SYS_IOCTL_H
200 #ifdef BLKGETSIZE64
201 DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
202 if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) {
203 return bytes;
205 #endif /* BLKGETSIZE64 */
206 #endif /* HAVE_SYS_IOCTL_H */
207 #endif /* HAVE_SYS_MOUNT_H */
209 DEBUG("looking for fhandle size with fstat\n");
210 stat_buf.st_size = 0;
211 error = fstat(fhandle, &stat_buf);
212 if (!error) {
213 /* always believe stat if a regular file as it might really
214 * be zero length */
215 if (S_ISREG(stat_buf.st_mode) || (stat_buf.st_size > 0))
216 return (uint64_t)stat_buf.st_size;
217 } else {
218 DEBUG("fstat failed: %s", strerror(errno));
221 DEBUG("looking for fhandle size with lseek SEEK_END\n");
222 es = lseek(fhandle, (off_t)0, SEEK_END);
223 if (es > ((off_t)0)) {
224 return (uint64_t)es;
225 } else {
226 DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
229 DEBUG("Could not find size of exported block device: %s", strerror(errno));
230 return UINT64_MAX;
233 int exptrim(struct nbd_request* req, CLIENT* client) {
234 /* Don't trim when we're read only */
235 if(client->server->flags & F_READONLY) {
236 errno = EINVAL;
237 return -1;
239 /* Don't trim beyond the size of the device, please */
240 if(req->from + req->len > client->exportsize) {
241 errno = EINVAL;
242 return -1;
244 /* For copy-on-write, we should trim on the diff file. Not yet
245 * implemented. */
246 if(client->server->flags & F_COPYONWRITE) {
247 DEBUG("TRIM not supported yet on copy-on-write exports");
248 return 0;
250 if (client->server->flags & F_TREEFILES) {
251 /* start address of first block to be trimmed */
252 off_t min = ( ( req->from + TREEPAGESIZE - 1 ) / TREEPAGESIZE) * TREEPAGESIZE;
253 /* start address of first block NOT to be trimmed */
254 off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE;
255 while (min<max) {
256 delete_treefile(client->exportname,client->exportsize,min);
257 min+=TREEPAGESIZE;
259 DEBUG("Performed TRIM request on TREE structure from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
260 return 0;
262 FILE_INFO cur = g_array_index(client->export, FILE_INFO, 0);
263 FILE_INFO next;
264 int i = 1;
265 do {
266 if(i<client->export->len) {
267 next = g_array_index(client->export, FILE_INFO, i);
268 } else {
269 next.fhandle = -1;
270 next.startoff = client->exportsize;
272 if(cur.startoff <= req->from && next.startoff - cur.startoff >= req->len) {
273 off_t reqoff = req->from - cur.startoff;
274 off_t curlen = next.startoff - reqoff;
275 off_t reqlen = curlen - reqoff > req->len ? req->len : curlen - reqoff;
276 punch_hole(cur.fhandle, reqoff, reqlen);
278 cur = next;
279 i++;
280 } while(i < client->export->len && cur.startoff < (req->from + req->len));
281 DEBUG("Performed TRIM request from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
282 return 0;
285 void myseek(int handle,off_t a) {
286 if (lseek(handle, a, SEEK_SET) < 0) {
287 err("Can not seek locally!\n");