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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
/*-
* Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/endian.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/disk.h>
#include <sys/bio.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <signal.h>
#include <err.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <syslog.h>
#include <stdarg.h>
#include <geom/gate/g_gate.h>
#include "ggate.h"
#define G_GATED_EXPORT_FILE "/etc/gg.exports"
#define G_GATED_DEBUG(...) \
if (g_gate_verbose) { \
printf(__VA_ARGS__); \
printf("\n"); \
}
static const char *exports = G_GATED_EXPORT_FILE;
static int got_sighup = 0;
static int nagle = 1;
static unsigned rcvbuf = G_GATE_RCVBUF;
static unsigned sndbuf = G_GATE_SNDBUF;
struct export {
char *e_path; /* path to device/file */
in_addr_t e_ip; /* remote IP address */
in_addr_t e_mask; /* IP mask */
unsigned e_flags; /* flags (RO/RW) */
SLIST_ENTRY(export) e_next;
};
static SLIST_HEAD(, export) exports_list =
SLIST_HEAD_INITIALIZER(&exports_list);
static void
usage(void)
{
fprintf(stderr, "usage: %s [-nv] [-a address] [-p port] [-R rcvbuf] "
"[-S sndbuf] [exports file]\n", getprogname());
exit(EXIT_FAILURE);
}
static char *
ip2str(in_addr_t ip)
{
static char sip[16];
snprintf(sip, sizeof(sip), "%u.%u.%u.%u",
((ip >> 24) & 0xff),
((ip >> 16) & 0xff),
((ip >> 8) & 0xff),
(ip & 0xff));
return (sip);
}
static in_addr_t
countmask(unsigned m)
{
in_addr_t mask;
if (m == 0) {
mask = 0x0;
} else {
mask = 1 << (32 - m);
mask--;
mask = ~mask;
}
return (mask);
}
static void
line_parse(char *line, unsigned lineno)
{
struct export *ex;
char *word, *path, *sflags;
unsigned flags, i, vmask;
in_addr_t ip, mask;
ip = mask = flags = vmask = 0;
path = NULL;
sflags = NULL;
for (i = 0, word = strtok(line, " \t"); word != NULL;
i++, word = strtok(NULL, " \t")) {
switch (i) {
case 0: /* IP address or host name */
ip = g_gate_str2ip(strsep(&word, "/"));
if (ip == INADDR_NONE) {
g_gate_xlog("Invalid IP/host name at line %u.",
lineno);
}
ip = ntohl(ip);
if (word == NULL)
vmask = 32;
else {
errno = 0;
vmask = strtoul(word, NULL, 10);
if (vmask == 0 && errno != 0) {
g_gate_xlog("Invalid IP mask value at "
"line %u.", lineno);
}
if ((unsigned)vmask > 32) {
g_gate_xlog("Invalid IP mask value at line %u.",
lineno);
}
}
mask = countmask(vmask);
break;
case 1: /* flags */
if (strcasecmp("rd", word) == 0 ||
strcasecmp("ro", word) == 0) {
flags = O_RDONLY;
} else if (strcasecmp("wo", word) == 0) {
flags = O_WRONLY;
} else if (strcasecmp("rw", word) == 0) {
flags = O_RDWR;
} else {
g_gate_xlog("Invalid value in flags field at "
"line %u.", lineno);
}
sflags = word;
break;
case 2: /* path */
if (strlen(word) >= MAXPATHLEN) {
g_gate_xlog("Path too long at line %u. ",
lineno);
}
path = word;
break;
default:
g_gate_xlog("Too many arguments at line %u. ", lineno);
}
}
if (i != 3)
g_gate_xlog("Too few arguments at line %u.", lineno);
ex = malloc(sizeof(*ex));
if (ex == NULL)
g_gate_xlog("No enough memory.");
ex->e_path = strdup(path);
if (ex->e_path == NULL)
g_gate_xlog("No enough memory.");
/* Made 'and' here. */
ex->e_ip = (ip & mask);
ex->e_mask = mask;
ex->e_flags = flags;
SLIST_INSERT_HEAD(&exports_list, ex, e_next);
g_gate_log(LOG_DEBUG, "Added %s/%u %s %s to exports list.",
ip2str(ex->e_ip), vmask, path, sflags);
}
static void
exports_clear(void)
{
struct export *ex;
while (!SLIST_EMPTY(&exports_list)) {
ex = SLIST_FIRST(&exports_list);
SLIST_REMOVE_HEAD(&exports_list, e_next);
free(ex);
}
}
#define EXPORTS_LINE_SIZE 2048
static void
exports_get(void)
{
char buf[EXPORTS_LINE_SIZE], *line;
unsigned lineno = 0, objs = 0, len;
FILE *fd;
exports_clear();
fd = fopen(exports, "r");
if (fd == NULL) {
g_gate_xlog("Cannot open exports file (%s): %s.", exports,
strerror(errno));
}
g_gate_log(LOG_INFO, "Reading exports file (%s).", exports);
for (;;) {
if (fgets(buf, sizeof(buf), fd) == NULL) {
if (feof(fd))
break;
g_gate_xlog("Error while reading exports file: %s.",
strerror(errno));
}
/* Increase line count. */
lineno++;
/* Skip spaces and tabs. */
for (line = buf; *line == ' ' || *line == '\t'; ++line)
;
/* Empty line, comment or empty line at the end of file. */
if (*line == '\n' || *line == '#' || *line == '\0')
continue;
len = strlen(line);
if (line[len - 1] == '\n') {
/* Remove new line char. */
line[len - 1] = '\0';
} else {
if (!feof(fd))
g_gate_xlog("Line %u too long.", lineno);
}
line_parse(line, lineno);
objs++;
}
fclose(fd);
if (objs == 0)
g_gate_xlog("There are no objects to export.");
g_gate_log(LOG_INFO, "Exporting %u object(s).", objs);
}
static struct export *
exports_find(struct sockaddr *s, const char *path)
{
struct export *ex;
in_addr_t ip;
ip = htonl(((struct sockaddr_in *)(void *)s)->sin_addr.s_addr);
SLIST_FOREACH(ex, &exports_list, e_next) {
if ((ip & ex->e_mask) != ex->e_ip)
continue;
if (path != NULL && strcmp(path, ex->e_path) != 0)
continue;
g_gate_log(LOG_INFO, "Connection from: %s.", ip2str(ip));
return (ex);
}
g_gate_log(LOG_INFO, "Unauthorized connection from: %s.", ip2str(ip));
return (NULL);
}
static void
sendfail(int sfd, int error, const char *fmt, ...)
{
struct g_gate_sinit sinit;
va_list ap;
int data;
sinit.gs_error = error;
g_gate_swap2n_sinit(&sinit);
data = send(sfd, &sinit, sizeof(sinit), 0);
g_gate_swap2h_sinit(&sinit);
if (data == -1) {
g_gate_xlog("Error while sending initial packet: %s.",
strerror(errno));
}
if (fmt != NULL) {
va_start(ap, fmt);
g_gate_xvlog(fmt, ap);
/* NOTREACHED */
va_end(ap);
}
exit(EXIT_FAILURE);
}
static void
serve(int sfd, struct sockaddr *s)
{
struct g_gate_cinit cinit;
struct g_gate_sinit sinit;
struct g_gate_hdr hdr;
struct export *ex;
char ipmask[32]; /* 32 == strlen("xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx")+1 */
size_t bufsize;
int32_t error;
int fd, flags;
ssize_t data;
char *buf;
g_gate_log(LOG_DEBUG, "Receiving initial packet.");
data = recv(sfd, &cinit, sizeof(cinit), MSG_WAITALL);
g_gate_swap2h_cinit(&cinit);
if (data == -1) {
g_gate_xlog("Error while receiving initial packet: %s.",
strerror(errno));
}
ex = exports_find(s, cinit.gc_path);
if (ex == NULL) {
sendfail(sfd, EINVAL, "Requested path isn't exported: %s.",
strerror(errno));
}
error = 0;
strlcpy(ipmask, ip2str(ex->e_ip), sizeof(ipmask));
strlcat(ipmask, "/", sizeof(ipmask));
strlcat(ipmask, ip2str(ex->e_mask), sizeof(ipmask));
if ((cinit.gc_flags & G_GATE_FLAG_READONLY) != 0) {
if (ex->e_flags == O_WRONLY) {
g_gate_log(LOG_ERR, "Read-only access requested, but "
"%s (%s) is exported write-only.", ex->e_path,
ipmask);
error = EPERM;
} else {
sinit.gs_flags = G_GATE_FLAG_READONLY;
}
} else if ((cinit.gc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
if (ex->e_flags == O_RDONLY) {
g_gate_log(LOG_ERR, "Write-only access requested, but "
"%s (%s) is exported read-only.", ex->e_path,
ipmask);
error = EPERM;
} else {
sinit.gs_flags = G_GATE_FLAG_WRITEONLY;
}
} else {
if (ex->e_flags == O_RDONLY) {
g_gate_log(LOG_ERR, "Read-write access requested, but "
"%s (%s) is exported read-only.", ex->e_path,
ipmask);
error = EPERM;
} else if (ex->e_flags == O_WRONLY) {
g_gate_log(LOG_ERR, "Read-write access requested, but "
"%s (%s) is exported write-only.", ex->e_path,
ipmask);
error = EPERM;
} else {
sinit.gs_flags = 0;
}
}
if (error != 0)
sendfail(sfd, error, NULL);
flags = g_gate_openflags(sinit.gs_flags);;
fd = open(ex->e_path, flags);
if (fd < 0) {
sendfail(sfd, errno, "Error while opening %s: %s.", ex->e_path,
strerror(errno));
}
g_gate_log(LOG_DEBUG, "Sending initial packet.");
/*
* This field isn't used by ggc(8) for now.
* It should be used in future when user don't give device size.
*/
sinit.gs_mediasize = g_gate_mediasize(fd);
sinit.gs_sectorsize = g_gate_sectorsize(fd);
sinit.gs_error = 0;
g_gate_swap2n_sinit(&sinit);
data = send(sfd, &sinit, sizeof(sinit), 0);
g_gate_swap2h_sinit(&sinit);
if (data == -1) {
sendfail(sfd, errno, "Error while sending initial packet: %s.",
strerror(errno));
}
bufsize = G_GATE_BUFSIZE_START;
buf = malloc(bufsize);
if (buf == NULL)
g_gate_xlog("No enough memory.");
g_gate_log(LOG_DEBUG, "New process: %u.", getpid());
for (;;) {
/*
* Receive request.
*/
data = recv(sfd, &hdr, sizeof(hdr), MSG_WAITALL);
if (data == 0) {
g_gate_log(LOG_DEBUG, "Process %u exiting.", getpid());
exit(EXIT_SUCCESS);
} else if (data == -1) {
g_gate_xlog("Error while receiving hdr packet: %s.",
strerror(errno));
} else if (data != sizeof(hdr)) {
g_gate_xlog("Malformed hdr packet received.");
}
g_gate_log(LOG_DEBUG, "Received hdr packet.");
g_gate_swap2h_hdr(&hdr);
/*
* Increase buffer if there is need to.
*/
if (hdr.gh_length > bufsize) {
bufsize = hdr.gh_length;
g_gate_log(LOG_DEBUG, "Increasing buffer to %u.",
bufsize);
buf = realloc(buf, bufsize);
if (buf == NULL)
g_gate_xlog("No enough memory.");
}
if (hdr.gh_cmd == BIO_READ) {
if (pread(fd, buf, hdr.gh_length,
hdr.gh_offset) == -1) {
error = errno;
g_gate_log(LOG_ERR, "Error while reading data "
"(offset=%ju, size=%zu): %s.",
(uintmax_t)hdr.gh_offset,
(size_t)hdr.gh_length, strerror(error));
} else {
error = 0;
}
hdr.gh_error = error;
g_gate_swap2n_hdr(&hdr);
if (send(sfd, &hdr, sizeof(hdr), 0) == -1) {
g_gate_xlog("Error while sending status: %s.",
strerror(errno));
}
g_gate_swap2h_hdr(&hdr);
/* Send data only if there was no error while pread(). */
if (error == 0) {
data = send(sfd, buf, hdr.gh_length, 0);
if (data == -1) {
g_gate_xlog("Error while sending data: "
"%s.", strerror(errno));
}
g_gate_log(LOG_DEBUG, "Sent %d bytes "
"(offset=%ju, size=%zu).", data,
(uintmax_t)hdr.gh_offset,
(size_t)hdr.gh_length);
}
} else /* if (hdr.gh_cmd == BIO_WRITE) */ {
g_gate_log(LOG_DEBUG, "Waiting for %u bytes of data...",
hdr.gh_length);
data = recv(sfd, buf, hdr.gh_length, MSG_WAITALL);
if (data == -1) {
g_gate_xlog("Error while receiving data: %s.",
strerror(errno));
}
if (pwrite(fd, buf, hdr.gh_length, hdr.gh_offset) == -1) {
error = errno;
g_gate_log(LOG_ERR, "Error while writing data "
"(offset=%llu, size=%u): %s.",
hdr.gh_offset, hdr.gh_length,
strerror(error));
} else {
error = 0;
}
hdr.gh_error = error;
g_gate_swap2n_hdr(&hdr);
if (send(sfd, &hdr, sizeof(hdr), 0) == -1) {
g_gate_xlog("Error while sending status: %s.",
strerror(errno));
}
g_gate_swap2h_hdr(&hdr);
g_gate_log(LOG_DEBUG, "Received %d bytes (offset=%llu, "
"size=%u).", data, hdr.gh_offset, hdr.gh_length);
}
g_gate_log(LOG_DEBUG, "Tick.");
}
}
static void
huphandler(int sig __unused)
{
got_sighup = 1;
}
int
main(int argc, char *argv[])
{
struct sockaddr_in serv;
struct sockaddr from;
in_addr_t bindaddr;
socklen_t fromlen;
struct timeval tv;
int on, sfd, tmpsfd;
pid_t childpid;
unsigned bsize, port;
bindaddr = htonl(INADDR_ANY);
port = G_GATE_PORT;
for (;;) {
int ch;
ch = getopt(argc, argv, "a:hnp:R:S:v");
if (ch == -1)
break;
switch (ch) {
case 'a':
bindaddr = g_gate_str2ip(optarg);
if (bindaddr == INADDR_NONE) {
errx(EXIT_FAILURE,
"Invalid IP/host name to bind to.");
}
break;
case 'n':
nagle = 0;
break;
case 'p':
errno = 0;
port = strtoul(optarg, NULL, 10);
if (port == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid port.");
break;
case 'R':
errno = 0;
rcvbuf = strtoul(optarg, NULL, 10);
if (rcvbuf == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid rcvbuf.");
break;
case 'S':
errno = 0;
sndbuf = strtoul(optarg, NULL, 10);
if (sndbuf == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid sndbuf.");
break;
case 'v':
g_gate_verbose++;
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argv[0] != NULL)
exports = argv[0];
exports_get();
if (!g_gate_verbose) {
/* Run in daemon mode. */
if (daemon(0, 0) < 0)
g_gate_xlog("Can't daemonize: %s", strerror(errno));
}
signal(SIGCHLD, SIG_IGN);
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
g_gate_xlog("Can't open stream socket: %s.", strerror(errno));
bzero(&serv, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = bindaddr;
serv.sin_port = htons(port);
on = 1;
if (nagle) {
if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &on,
sizeof(on)) < 0) {
g_gate_xlog("setsockopt() error: %s.", strerror(errno));
}
}
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
g_gate_xlog("setsockopt(): %s.", strerror(errno));
bsize = rcvbuf;
if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, &bsize, sizeof(bsize)) < 0)
g_gate_xlog("setsockopt(): %s.", strerror(errno));
bsize = sndbuf;
if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, &bsize, sizeof(bsize)) < 0)
g_gate_xlog("setsockopt(): %s.", strerror(errno));
tv.tv_sec = 10;
tv.tv_usec = 0;
if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) ||
setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
g_gate_xlog("setsockopt() error: %s.", strerror(errno));
}
if (bind(sfd, (struct sockaddr *)&serv, sizeof(serv)) < 0)
g_gate_xlog("bind(): %s.", strerror(errno));
if (listen(sfd, 5) < 0)
g_gate_xlog("listen(): %s.", strerror(errno));
g_gate_log(LOG_INFO, "Listen on port: %d.", port);
signal(SIGHUP, huphandler);
for (;;) {
fromlen = sizeof(from);
tmpsfd = accept(sfd, &from, &fromlen);
if (tmpsfd < 0)
g_gate_xlog("accept(): %s.", strerror(errno));
if (got_sighup) {
got_sighup = 0;
exports_get();
}
if (exports_find(&from, NULL) == NULL) {
close(tmpsfd);
continue;
}
childpid = fork();
if (childpid < 0) {
g_gate_xlog("Cannot create child process: %s.",
strerror(errno));
} else if (childpid == 0) {
close(sfd);
serve(tmpsfd, &from);
/* NOTREACHED */
}
close(tmpsfd);
}
close(sfd);
exit(EXIT_SUCCESS);
}
|