client: fix size offset in parsing of response
[nbd.git] / nbdsrv.c
blob7d351cf8f19595ba55f885aa89ccd930a526023d
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 assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
38 strcpy(privmask, mask);
40 memset(&hints, 0, sizeof(hints));
41 hints.ai_family = AF_UNSPEC;
42 hints.ai_flags = AI_NUMERICHOST;
44 if((masksep = strchr(privmask, '/'))) {
45 *masksep = '\0';
46 masklen = strtol(++masksep, NULL, 10);
47 } else {
48 masklen = addrlen * 8;
51 int e;
52 if((e = getaddrinfo(privmask, NULL, &hints, &res))) {
53 g_set_error(err, NBDS_ERR, NBDS_ERR_GAI, "could not parse netmask line: %s", gai_strerror(e));
54 return false;
56 aitmp = res;
57 while(res) {
58 const uint8_t* byte_s;
59 uint8_t* byte_t;
60 uint8_t mask = 0;
61 int len_left = masklen;
62 if(res->ai_family != addr->sa_family) {
63 msg(LOG_DEBUG, "client address does not match %d/%d: address family mismatch (IPv4 vs IPv6?)",
64 (int)res->ai_family, (int)addr->sa_family);
65 goto next;
67 switch(addr->sa_family) {
68 case AF_INET:
69 byte_s = (const uint8_t*)(&(((struct sockaddr_in*)addr)->sin_addr));
70 byte_t = (uint8_t*)(&(((struct sockaddr_in*)(res->ai_addr))->sin_addr));
71 break;
72 case AF_INET6:
73 byte_s = (const uint8_t*)(&(((struct sockaddr_in6*)addr)->sin6_addr));
74 byte_t = (uint8_t*)(&(((struct sockaddr_in6*)(res->ai_addr))->sin6_addr));
75 break;
77 while(len_left >= 8) {
78 if(*byte_s != *byte_t) {
79 goto next;
81 byte_s++; byte_t++;
82 len_left -= 8;
84 if(len_left) {
85 mask = getmaskbyte(len_left);
86 if((*byte_s & mask) != (*byte_t & mask)) {
87 goto next;
90 freeaddrinfo(aitmp);
91 return true;
92 next:
93 res = res->ai_next;
95 freeaddrinfo(aitmp);
96 return false;
99 uint8_t getmaskbyte(int masklen) {
100 if(masklen >= 8) {
101 return 0xFF;
103 uint8_t retval = 0;
104 for(int i = 7; i + masklen > 7; i--) {
105 retval |= 1 << i;
108 return retval;
111 int authorized_client(CLIENT *opts) {
112 FILE *f ;
113 char line[LINELEN];
115 if (opts->server->authname == NULL) {
116 msg(LOG_INFO, "No authorization file, granting access.");
117 return 1;
120 if ((f=fopen(opts->server->authname,"r"))==NULL) {
121 msg(LOG_INFO, "Can't open authorization file %s (%s).",
122 opts->server->authname, strerror(errno));
123 return 1 ;
126 while (fgets(line,LINELEN,f)!=NULL) {
127 char* pos;
128 /* Drop comments */
129 if((pos = strchr(line, '#'))) {
130 *pos = '\0';
132 /* Skip whitespace */
133 pos = line;
134 while((*pos) && isspace(*pos)) {
135 pos++;
137 /* Skip content-free lines */
138 if(!(*pos)) {
139 continue;
141 if(address_matches(line, (struct sockaddr*)&opts->clientaddr, NULL)) {
142 fclose(f);
143 return 1;
146 fclose(f);
147 return 0;
151 * duplicate server
152 * @param s the old server we want to duplicate
153 * @return new duplicated server
155 SERVER* dup_serve(const SERVER *const s) {
156 SERVER *serve = NULL;
158 serve=g_new0(SERVER, 1);
159 if(serve == NULL)
160 return NULL;
162 if(s->exportname)
163 serve->exportname = g_strdup(s->exportname);
165 serve->expected_size = s->expected_size;
167 if(s->listenaddr)
168 serve->listenaddr = g_strdup(s->listenaddr);
170 if(s->authname)
171 serve->authname = g_strdup(s->authname);
173 serve->flags = s->flags;
174 serve->virtstyle = s->virtstyle;
175 serve->cidrlen = s->cidrlen;
177 if(s->prerun)
178 serve->prerun = g_strdup(s->prerun);
180 if(s->postrun)
181 serve->postrun = g_strdup(s->postrun);
183 if(s->transactionlog)
184 serve->transactionlog = g_strdup(s->transactionlog);
186 if(s->servename)
187 serve->servename = g_strdup(s->servename);
189 if(s->cowdir)
190 serve->cowdir = g_strdup(s->cowdir);
192 serve->max_connections = s->max_connections;
194 return serve;
197 uint64_t size_autodetect(int fhandle) {
198 off_t es;
199 u64 bytes __attribute__((unused));
200 struct stat stat_buf;
201 int error;
203 #ifdef HAVE_SYS_MOUNT_H
204 #ifdef HAVE_SYS_IOCTL_H
205 #ifdef BLKGETSIZE64
206 DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
207 if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) {
208 return bytes;
210 #endif /* BLKGETSIZE64 */
211 #endif /* HAVE_SYS_IOCTL_H */
212 #endif /* HAVE_SYS_MOUNT_H */
214 DEBUG("looking for fhandle size with fstat\n");
215 stat_buf.st_size = 0;
216 error = fstat(fhandle, &stat_buf);
217 if (!error) {
218 /* always believe stat if a regular file as it might really
219 * be zero length */
220 if (S_ISREG(stat_buf.st_mode) || (stat_buf.st_size > 0))
221 return (uint64_t)stat_buf.st_size;
222 } else {
223 DEBUG("fstat failed: %s", strerror(errno));
226 DEBUG("looking for fhandle size with lseek SEEK_END\n");
227 es = lseek(fhandle, (off_t)0, SEEK_END);
228 if (es > ((off_t)0)) {
229 return (uint64_t)es;
230 } else {
231 DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
234 DEBUG("Could not find size of exported block device: %s", strerror(errno));
235 return UINT64_MAX;
238 int exptrim(struct nbd_request* req, CLIENT* client) {
239 /* Don't trim when we're read only */
240 if(client->server->flags & F_READONLY) {
241 errno = EINVAL;
242 return -1;
244 /* Don't trim beyond the size of the device, please */
245 if(req->from + req->len > client->exportsize) {
246 errno = EINVAL;
247 return -1;
249 /* For copy-on-write, we should trim on the diff file. Not yet
250 * implemented. */
251 if(client->server->flags & F_COPYONWRITE) {
252 DEBUG("TRIM not supported yet on copy-on-write exports");
253 return 0;
255 if (client->server->flags & F_TREEFILES) {
256 /* start address of first block to be trimmed */
257 off_t min = ( ( req->from + TREEPAGESIZE - 1 ) / TREEPAGESIZE) * TREEPAGESIZE;
258 /* start address of first block NOT to be trimmed */
259 off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE;
260 while (min<max) {
261 delete_treefile(client->exportname,client->exportsize,min);
262 min+=TREEPAGESIZE;
264 DEBUG("Performed TRIM request on TREE structure from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
265 return 0;
267 FILE_INFO cur = g_array_index(client->export, FILE_INFO, 0);
268 FILE_INFO next;
269 int i = 1;
270 do {
271 if(i<client->export->len) {
272 next = g_array_index(client->export, FILE_INFO, i);
273 } else {
274 next.fhandle = -1;
275 next.startoff = client->exportsize;
277 if(cur.startoff <= req->from && next.startoff - cur.startoff >= req->len) {
278 off_t reqoff = req->from - cur.startoff;
279 off_t curlen = next.startoff - reqoff;
280 off_t reqlen = curlen - reqoff > req->len ? req->len : curlen - reqoff;
281 punch_hole(cur.fhandle, reqoff, reqlen);
283 cur = next;
284 i++;
285 } while(i < client->export->len && cur.startoff < (req->from + req->len));
286 DEBUG("Performed TRIM request from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
287 return 0;