Release 1:3.18-1
[nbd.git] / nbdsrv.c
blob1ac4d35360f4339c2eecbcce881ab8750b1a8ad5
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"
20 #ifdef HAVE_SYS_MOUNT_H
21 #include <sys/mount.h>
22 #endif
24 #define LINELEN 256 /**< Size of static buffer used to read the
25 authorization file (yuck) */
27 #include <cliserv.h>
29 bool address_matches(const char* mask, const struct sockaddr* addr, GError** err) {
30 struct addrinfo *res, *aitmp, hints;
31 char *masksep;
32 char privmask[strlen(mask)+1];
33 int masklen;
34 int addrlen = addr->sa_family == AF_INET ? 4 : 16;
36 strcpy(privmask, mask);
38 memset(&hints, 0, sizeof(hints));
39 hints.ai_family = AF_UNSPEC;
40 hints.ai_flags = AI_NUMERICHOST;
42 if((masksep = strchr(privmask, '/'))) {
43 *masksep = '\0';
44 masklen = strtol(++masksep, NULL, 10);
45 } else {
46 masklen = addrlen * 8;
49 int e;
50 if((e = getaddrinfo(privmask, NULL, &hints, &res))) {
51 g_set_error(err, NBDS_ERR, NBDS_ERR_GAI, "could not parse netmask line: %s", gai_strerror(e));
52 return false;
54 aitmp = res;
55 while(res) {
56 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
57 const uint8_t* byte_s;
58 uint8_t* byte_t;
59 uint8_t mask = 0;
60 int len_left = masklen;
61 if(res->ai_family != addr->sa_family) {
62 msg(LOG_DEBUG, "client address does not match %d/%d: address family mismatch (IPv4 vs IPv6?)",
63 (int)res->ai_family, (int)addr->sa_family);
64 goto next;
66 switch(addr->sa_family) {
67 case AF_INET:
68 byte_s = (const uint8_t*)(&(((struct sockaddr_in*)addr)->sin_addr));
69 byte_t = (uint8_t*)(&(((struct sockaddr_in*)(res->ai_addr))->sin_addr));
70 break;
71 case AF_INET6:
72 byte_s = (const uint8_t*)(&(((struct sockaddr_in6*)addr)->sin6_addr));
73 byte_t = (uint8_t*)(&(((struct sockaddr_in6*)(res->ai_addr))->sin6_addr));
74 break;
76 while(len_left >= 8) {
77 if(*byte_s != *byte_t) {
78 goto next;
80 byte_s++; byte_t++;
81 len_left -= 8;
83 if(len_left) {
84 mask = getmaskbyte(len_left);
85 if((*byte_s & mask) != (*byte_t & mask)) {
86 goto next;
89 freeaddrinfo(aitmp);
90 return true;
91 next:
92 res = res->ai_next;
94 freeaddrinfo(aitmp);
95 return false;
98 uint8_t getmaskbyte(int masklen) {
99 if(masklen >= 8) {
100 return 0xFF;
102 uint8_t retval = 0;
103 for(int i = 7; i + masklen > 7; i--) {
104 retval |= 1 << i;
107 return retval;
110 int authorized_client(CLIENT *opts) {
111 FILE *f ;
112 char line[LINELEN];
114 if (opts->server->authname == NULL) {
115 msg(LOG_INFO, "No authorization file, granting access.");
116 return 1;
119 if ((f=fopen(opts->server->authname,"r"))==NULL) {
120 msg(LOG_INFO, "Can't open authorization file %s (%s).",
121 opts->server->authname, strerror(errno));
122 return 1 ;
125 while (fgets(line,LINELEN,f)!=NULL) {
126 char* pos;
127 /* Drop comments */
128 if((pos = strchr(line, '#'))) {
129 *pos = '\0';
131 /* Skip whitespace */
132 pos = line;
133 while((*pos) && isspace(*pos)) {
134 pos++;
136 /* Skip content-free lines */
137 if(!(*pos)) {
138 continue;
140 if(address_matches(line, (struct sockaddr*)&opts->clientaddr, NULL)) {
141 fclose(f);
142 return 1;
145 fclose(f);
146 return 0;
150 * duplicate server
151 * @param s the old server we want to duplicate
152 * @return new duplicated server
154 SERVER* dup_serve(const SERVER *const s) {
155 SERVER *serve = NULL;
157 serve=g_new0(SERVER, 1);
158 if(serve == NULL)
159 return NULL;
161 if(s->exportname)
162 serve->exportname = g_strdup(s->exportname);
164 serve->expected_size = s->expected_size;
166 if(s->listenaddr)
167 serve->listenaddr = g_strdup(s->listenaddr);
169 if(s->authname)
170 serve->authname = g_strdup(s->authname);
172 serve->flags = s->flags;
173 serve->virtstyle = s->virtstyle;
174 serve->cidrlen = s->cidrlen;
176 if(s->prerun)
177 serve->prerun = g_strdup(s->prerun);
179 if(s->postrun)
180 serve->postrun = g_strdup(s->postrun);
182 if(s->transactionlog)
183 serve->transactionlog = g_strdup(s->transactionlog);
185 if(s->servename)
186 serve->servename = g_strdup(s->servename);
188 if(s->cowdir)
189 serve->cowdir = g_strdup(s->cowdir);
191 serve->max_connections = s->max_connections;
193 return serve;
196 uint64_t size_autodetect(int fhandle) {
197 off_t es;
198 u64 bytes __attribute__((unused));
199 struct stat stat_buf;
200 int error;
202 #ifdef HAVE_SYS_MOUNT_H
203 #ifdef HAVE_SYS_IOCTL_H
204 #ifdef BLKGETSIZE64
205 DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
206 if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) {
207 return bytes;
209 #endif /* BLKGETSIZE64 */
210 #endif /* HAVE_SYS_IOCTL_H */
211 #endif /* HAVE_SYS_MOUNT_H */
213 DEBUG("looking for fhandle size with fstat\n");
214 stat_buf.st_size = 0;
215 error = fstat(fhandle, &stat_buf);
216 if (!error) {
217 /* always believe stat if a regular file as it might really
218 * be zero length */
219 if (S_ISREG(stat_buf.st_mode) || (stat_buf.st_size > 0))
220 return (uint64_t)stat_buf.st_size;
221 } else {
222 DEBUG("fstat failed: %s", strerror(errno));
225 DEBUG("looking for fhandle size with lseek SEEK_END\n");
226 es = lseek(fhandle, (off_t)0, SEEK_END);
227 if (es > ((off_t)0)) {
228 return (uint64_t)es;
229 } else {
230 DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
233 DEBUG("Could not find size of exported block device: %s", strerror(errno));
234 return UINT64_MAX;
237 int exptrim(struct nbd_request* req, CLIENT* client) {
238 /* Caller did range checking */
239 assert(!(client->server->flags & F_READONLY));
240 assert(req->from + req->len <= client->exportsize);
241 /* For copy-on-write, we should trim on the diff file. Not yet
242 * implemented. */
243 if(client->server->flags & F_COPYONWRITE) {
244 DEBUG("TRIM not supported yet on copy-on-write exports");
245 return 0;
247 if (client->server->flags & F_TREEFILES) {
248 /* start address of first block to be trimmed */
249 off_t min = ( ( req->from + TREEPAGESIZE - 1 ) / TREEPAGESIZE) * TREEPAGESIZE;
250 /* start address of first block NOT to be trimmed */
251 off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE;
252 while (min<max) {
253 delete_treefile(client->exportname,client->exportsize,min);
254 min+=TREEPAGESIZE;
256 DEBUG("Performed TRIM request on TREE structure from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
257 return 0;
259 FILE_INFO cur = g_array_index(client->export, FILE_INFO, 0);
260 FILE_INFO next;
261 int i = 1;
262 do {
263 if(i<client->export->len) {
264 next = g_array_index(client->export, FILE_INFO, i);
265 } else {
266 next.fhandle = -1;
267 next.startoff = client->exportsize;
269 if(cur.startoff <= req->from && next.startoff - cur.startoff >= req->len) {
270 off_t reqoff = req->from - cur.startoff;
271 off_t curlen = next.startoff - reqoff;
272 off_t reqlen = curlen - reqoff > req->len ? req->len : curlen - reqoff;
273 punch_hole(cur.fhandle, reqoff, reqlen);
275 cur = next;
276 i++;
277 } while(i < client->export->len && cur.startoff < (req->from + req->len));
278 DEBUG("Performed TRIM request from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
279 return 0;