forked from NetworkBlockDevice/nbd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbdsrv.c
308 lines (270 loc) · 7.55 KB
/
nbdsrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "config.h"
#include "nbd-debug.h"
#include <nbdsrv.h>
#include <assert.h>
#include <ctype.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <treefiles.h>
#include "backend.h"
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#define LINELEN 256 /**< Size of static buffer used to read the
authorization file (yuck) */
#include <cliserv.h>
bool address_matches(const char* mask, const struct sockaddr* addr, GError** err) {
struct addrinfo *res, *aitmp, hints;
char *masksep;
char privmask[strlen(mask)+1];
int masklen;
int addrlen = addr->sa_family == AF_INET ? 4 : 16;
strcpy(privmask, mask);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_flags = AI_NUMERICHOST;
if((masksep = strchr(privmask, '/'))) {
*masksep = '\0';
masklen = strtol(++masksep, NULL, 10);
} else {
masklen = addrlen * 8;
}
int e;
if((e = getaddrinfo(privmask, NULL, &hints, &res))) {
g_set_error(err, NBDS_ERR, NBDS_ERR_GAI, "could not parse netmask line: %s", gai_strerror(e));
return false;
}
aitmp = res;
while(res) {
assert(addr->sa_family == AF_INET || addr->sa_family == AF_INET6);
const uint8_t* byte_s;
uint8_t* byte_t;
uint8_t mask = 0;
int len_left = masklen;
if(res->ai_family != addr->sa_family) {
msg(LOG_DEBUG, "client address does not match %d/%d: address family mismatch (IPv4 vs IPv6?)",
(int)res->ai_family, (int)addr->sa_family);
goto next;
}
switch(addr->sa_family) {
case AF_INET:
byte_s = (const uint8_t*)(&(((struct sockaddr_in*)addr)->sin_addr));
byte_t = (uint8_t*)(&(((struct sockaddr_in*)(res->ai_addr))->sin_addr));
break;
case AF_INET6:
byte_s = (const uint8_t*)(&(((struct sockaddr_in6*)addr)->sin6_addr));
byte_t = (uint8_t*)(&(((struct sockaddr_in6*)(res->ai_addr))->sin6_addr));
break;
}
while(len_left >= 8) {
if(*byte_s != *byte_t) {
goto next;
}
byte_s++; byte_t++;
len_left -= 8;
}
if(len_left) {
mask = getmaskbyte(len_left);
if((*byte_s & mask) != (*byte_t & mask)) {
goto next;
}
}
freeaddrinfo(aitmp);
return true;
next:
res = res->ai_next;
}
freeaddrinfo(aitmp);
return false;
}
uint8_t getmaskbyte(int masklen) {
if(masklen >= 8) {
return 0xFF;
}
uint8_t retval = 0;
for(int i = 7; i + masklen > 7; i--) {
retval |= 1 << i;
}
return retval;
}
int authorized_client(CLIENT *opts) {
FILE *f ;
char line[LINELEN];
if (opts->server->authname == NULL) {
msg(LOG_INFO, "No authorization file, granting access.");
return 1;
}
if ((f=fopen(opts->server->authname,"r"))==NULL) {
msg(LOG_INFO, "Can't open authorization file %s (%s).",
opts->server->authname, strerror(errno));
return 1 ;
}
while (fgets(line,LINELEN,f)!=NULL) {
char* pos;
char* endpos;
/* Drop comments */
if((pos = strchr(line, '#'))) {
*pos = '\0';
}
/* Skip whitespace */
pos = line;
while((*pos) && isspace(*pos)) {
pos++;
}
/* Skip content-free lines */
if(!(*pos)) {
continue;
}
/* Trim trailing whitespace */
endpos = pos;
while ((*endpos) && !isspace(*endpos))
endpos++;
*endpos = '\0';
if(address_matches(pos, (struct sockaddr*)&opts->clientaddr, NULL)) {
fclose(f);
return 1;
}
}
fclose(f);
return 0;
}
/**
* duplicate server
* @param s the old server we want to duplicate
* @return new duplicated server
**/
SERVER* dup_serve(const SERVER *const s) {
SERVER *serve = NULL;
serve=g_new0(SERVER, 1);
if(serve == NULL)
return NULL;
if(s->exportname)
serve->exportname = g_strdup(s->exportname);
serve->expected_size = s->expected_size;
if(s->listenaddr)
serve->listenaddr = g_strdup(s->listenaddr);
if(s->authname)
serve->authname = g_strdup(s->authname);
serve->flags = s->flags;
serve->virtstyle = s->virtstyle;
serve->cidrlen = s->cidrlen;
if(s->prerun)
serve->prerun = g_strdup(s->prerun);
if(s->postrun)
serve->postrun = g_strdup(s->postrun);
if(s->transactionlog)
serve->transactionlog = g_strdup(s->transactionlog);
if(s->servename)
serve->servename = g_strdup(s->servename);
if(s->cowdir)
serve->cowdir = g_strdup(s->cowdir);
serve->max_connections = s->max_connections;
return serve;
}
uint64_t size_autodetect(int fhandle) {
off_t es;
u64 bytes __attribute__((unused));
struct stat stat_buf;
int error;
#ifdef HAVE_SYS_MOUNT_H
#ifdef HAVE_SYS_IOCTL_H
#ifdef BLKGETSIZE64
DEBUG("looking for export size with ioctl BLKGETSIZE64\n");
if (!ioctl(fhandle, BLKGETSIZE64, &bytes) && bytes) {
return bytes;
}
#endif /* BLKGETSIZE64 */
#endif /* HAVE_SYS_IOCTL_H */
#endif /* HAVE_SYS_MOUNT_H */
DEBUG("looking for fhandle size with fstat\n");
stat_buf.st_size = 0;
error = fstat(fhandle, &stat_buf);
if (!error) {
/* always believe stat if a regular file as it might really
* be zero length */
if (S_ISREG(stat_buf.st_mode) || (stat_buf.st_size > 0))
return (uint64_t)stat_buf.st_size;
} else {
DEBUG("fstat failed: %s", strerror(errno));
}
DEBUG("looking for fhandle size with lseek SEEK_END\n");
es = lseek(fhandle, (off_t)0, SEEK_END);
if (es > ((off_t)0)) {
return (uint64_t)es;
} else {
DEBUG("lseek failed: %d", errno==EBADF?1:(errno==ESPIPE?2:(errno==EINVAL?3:4)));
}
DEBUG("Could not find size of exported block device: %s", strerror(errno));
return UINT64_MAX;
}
int exptrim(struct nbd_request* req, CLIENT* client) {
/* Caller did range checking */
assert(!(client->server->flags & F_READONLY));
assert(req->from + req->len <= client->exportsize);
/* For copy-on-write, we should trim on the diff file. Not yet
* implemented. */
if(client->server->flags & F_COPYONWRITE) {
DEBUG("TRIM not supported yet on copy-on-write exports");
return 0;
}
if (client->server->flags & F_TREEFILES) {
/* start address of first block to be trimmed */
off_t min = ( ( req->from + TREEPAGESIZE - 1 ) / TREEPAGESIZE) * TREEPAGESIZE;
/* start address of first block NOT to be trimmed */
off_t max = ( ( req->from + req->len ) / TREEPAGESIZE) * TREEPAGESIZE;
while (min<max) {
delete_treefile(client->exportname,client->exportsize,min);
min+=TREEPAGESIZE;
}
DEBUG("Performed TRIM request on TREE structure from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
return 0;
}
FILE_INFO cur = g_array_index(client->export, FILE_INFO, 0);
FILE_INFO next;
int i = 1;
do {
if(i<client->export->len) {
next = g_array_index(client->export, FILE_INFO, i);
} else {
next.fhandle = -1;
next.startoff = client->exportsize;
}
if(cur.startoff <= req->from && next.startoff - cur.startoff >= req->len) {
off_t reqoff = req->from - cur.startoff;
off_t curlen = next.startoff - reqoff;
off_t reqlen = curlen - reqoff > req->len ? req->len : curlen - reqoff;
punch_hole(cur.fhandle, reqoff, reqlen);
}
cur = next;
i++;
} while(i < client->export->len && cur.startoff < (req->from + req->len));
DEBUG("Performed TRIM request from %llu to %llu", (unsigned long long) req->from, (unsigned long long) req->len);
return 0;
}
pthread_mutex_t cntmutex = PTHREAD_MUTEX_INITIALIZER;
SERVER* serve_inc_ref(SERVER *s) {
pthread_mutex_lock(&cntmutex);
s->refcnt++;
pthread_mutex_unlock(&cntmutex);
return s;
}
SERVER* serve_dec_ref(SERVER *s) {
pthread_mutex_lock(&cntmutex);
if(--(s->refcnt) == 0) {
g_free(s);
s = NULL;
}
pthread_mutex_unlock(&cntmutex);
return s;
}