forked from unfs3/unfs3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.c
1200 lines (1027 loc) · 28.7 KB
/
daemon.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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UNFS3 server framework
* Originally generated using rpcgen
* Portions (C) 2004, Pascal Schmidt
* see file LICENSE for license details
*/
#include "config.h"
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <rpc/rpc.h>
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <syslog.h>
#else /* WIN32 */
#include <winsock.h>
#endif /* WIN32 */
#include <fcntl.h>
#include <memory.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#ifdef HAVE_RPC_SVC_SOC_H
# include <rpc/svc_soc.h>
#endif
#if defined(HAVE_SVC_GETREQ_POLL) && HAVE_DECL_SVC_POLLFD
# include <sys/poll.h>
#endif
#include "nfs.h"
#include "mount.h"
#include "xdr.h"
#include "fh.h"
#include "fh_cache.h"
#include "fd_cache.h"
#include "user.h"
#include "daemon.h"
#include "backend.h"
#include "Config/exports.h"
#ifndef SIG_PF
#define SIG_PF void(*)(int)
#endif
#define UNFS_NAME "UNFS3 unfsd " PACKAGE_VERSION " (C) 2009, Pascal Schmidt <[email protected]>\n"
/* write verifier */
writeverf3 wverf;
/* readdir cookie */
cookie3 rcookie = 0;
/* options and default values */
int opt_detach = TRUE;
char *opt_exports = "/etc/exports";
int opt_cluster = FALSE;
char *opt_cluster_path = "/";
int opt_tcponly = FALSE;
unsigned int opt_nfs_port = NFS_PORT; /* 0 means RPC_ANYSOCK */
unsigned int opt_mount_port = NFS_PORT;
int opt_singleuser = FALSE;
int opt_brute_force = FALSE;
int opt_testconfig = FALSE;
struct in6_addr opt_bind_addr;
int opt_readable_executables = FALSE;
char *opt_pid_file = NULL;
/* Register with portmapper? */
int opt_portmapper = TRUE;
/*
* output message to syslog or stdout
*/
void logmsg(int prio, const char *fmt, ...)
{
va_list ap;
#if HAVE_VSYSLOG == 0
char mesg[1024];
#endif
va_start(ap, fmt);
if (opt_detach) {
#if HAVE_VSYSLOG == 1
vsyslog(prio, fmt, ap);
#else
vsnprintf(mesg, 1024, fmt, ap);
syslog(prio, mesg, 1024);
#endif
} else {
vprintf(fmt, ap);
putchar('\n');
}
va_end(ap);
}
/*
* return remote address from svc_req structure
*/
int get_remote(struct svc_req *rqstp, struct in6_addr *addr6)
{
const struct sockaddr *saddr;
memset(addr6, 0, sizeof(struct in6_addr));
saddr = (const struct sockaddr *) svc_getrpccaller(rqstp->rq_xprt)->buf;
if (saddr->sa_family == AF_INET6) {
memcpy(addr6, &((const struct sockaddr_in6*)saddr)->sin6_addr, sizeof(struct in6_addr));
return 0;
}
if (saddr->sa_family == AF_INET) {
((uint32_t*)addr6)[0] = 0;
((uint32_t*)addr6)[1] = 0;
((uint32_t*)addr6)[2] = htonl(0xffff);
((uint32_t*)addr6)[3] = ((const struct sockaddr_in*)saddr)->sin_addr.s_addr;
return 0;
}
errno = EAFNOSUPPORT;
return -1;
}
/*
* return remote port from svc_req structure
*/
short get_port(struct svc_req *rqstp)
{
return (svc_getcaller(rqstp->rq_xprt))->sin6_port;
}
/*
* return the socket type of the request (SOCK_STREAM or SOCK_DGRAM)
*/
int get_socket_type(struct svc_req *rqstp)
{
int v, res;
socklen_t l;
l = sizeof(v);
res = getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET, SO_TYPE, &v, &l);
if (res < 0) {
logmsg(LOG_CRIT, "unable to determine socket type");
return -1;
}
return v;
}
/*
* write current pid to a file
*/
static void create_pid_file(void)
{
char buf[16];
int fd, res, len;
if (!opt_pid_file)
return;
fd = backend_open_create(opt_pid_file, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
logmsg(LOG_WARNING, "failed to create pid file `%s'", opt_pid_file);
return;
}
#if defined(LOCK_EX) && defined(LOCK_NB)
res = backend_flock(fd, LOCK_EX | LOCK_NB);
if (res == -1) {
logmsg(LOG_WARNING, "failed to lock pid file `%s'", opt_pid_file);
backend_close(fd);
return;
}
#endif
sprintf(buf, "%i\n", backend_getpid());
len = strlen(buf);
res = backend_pwrite(fd, buf, len, 0);
backend_close(fd);
if (res != len) {
logmsg(LOG_WARNING, "failed to write pid file `%s'", opt_pid_file);
}
}
/*
* remove pid file
*/
static void remove_pid_file(void)
{
int res;
if (!opt_pid_file)
return;
res = backend_remove(opt_pid_file);
if (res == -1 && errno != ENOENT) {
logmsg(LOG_WARNING, "failed to remove pid file `%s'", opt_pid_file);
}
}
/*
* parse command line options
*/
static void parse_options(int argc, char **argv)
{
int opt = 0;
char *optstring = "bcC:de:hl:m:n:prstTuwi:";
while (opt != -1) {
opt = getopt(argc, argv, optstring);
switch (opt) {
case 'b':
opt_brute_force = TRUE;
break;
#ifdef WANT_CLUSTER
case 'c':
opt_cluster = TRUE;
break;
case 'C':
opt_cluster_path = optarg;
break;
#endif
case 'd':
printf(UNFS_NAME);
opt_detach = FALSE;
break;
case 'e':
#ifndef WIN32
if (optarg[0] != '/') {
/* A relative path won't work for re-reading the exports
file on SIGHUP, since we are changing directory */
fprintf(stderr, "Error: relative path to exports file\n");
exit(1);
}
#endif
opt_exports = optarg;
break;
case 'h':
printf(UNFS_NAME);
printf("Usage: %s [options]\n", argv[0]);
printf("\t-h display this short option summary\n");
printf("\t-u use unprivileged port for services\n");
printf("\t-d do not detach from terminal\n");
printf("\t-e <file> file to use instead of /etc/exports\n");
printf("\t-i <file> write daemon pid to given file\n");
#ifdef WANT_CLUSTER
printf("\t-c enable cluster extensions\n");
printf("\t-C <path> set path for cluster extensions\n");
#endif
printf("\t-n <port> port to use for NFS service\n");
printf("\t-m <port> port to use for MOUNT service\n");
printf
("\t-t TCP only, do not listen on UDP ports\n");
printf("\t-p do not register with portmap/rpcbind\n");
printf("\t-s single user mode\n");
printf("\t-b enable brute force file searching\n");
printf
("\t-l <addr> bind to interface with specified address\n");
printf
("\t-r report unreadable executables as readable\n");
printf("\t-T test exports file and exit\n");
exit(0);
break;
case 'l':
if (inet_pton(AF_INET6, optarg, &opt_bind_addr) != 1) {
struct in_addr in4;
if (inet_pton(AF_INET, optarg, &in4) != 1) {
fprintf(stderr, "Invalid bind address\n");
exit(1);
}
((uint32_t*)&opt_bind_addr)[0] = 0;
((uint32_t*)&opt_bind_addr)[1] = 0;
((uint32_t*)&opt_bind_addr)[2] = htonl(0xffff);
((uint32_t*)&opt_bind_addr)[3] = in4.s_addr;
}
break;
case 'm':
opt_mount_port = strtol(optarg, NULL, 10);
if (opt_mount_port == 0) {
fprintf(stderr, "Invalid port\n");
exit(1);
}
break;
case 'n':
opt_nfs_port = strtol(optarg, NULL, 10);
if (opt_nfs_port == 0) {
fprintf(stderr, "Invalid port\n");
exit(1);
}
break;
case 'p':
opt_portmapper = FALSE;
break;
case 'r':
opt_readable_executables = TRUE;
break;
case 's':
opt_singleuser = TRUE;
#ifndef WIN32
if (backend_getuid() == 0) {
logmsg(LOG_WARNING,
"Warning: running as root with -s is dangerous");
logmsg(LOG_WARNING,
"All clients will have root access to all exported files!");
}
#endif
break;
case 't':
opt_tcponly = TRUE;
break;
case 'T':
opt_testconfig = TRUE;
break;
case 'u':
opt_nfs_port = 0;
opt_mount_port = 0;
break;
case 'i':
opt_pid_file = optarg;
break;
case '?':
exit(1);
break;
}
}
}
/*
* signal handler and error exit function
*/
void daemon_exit(int error)
{
#ifndef WIN32
if (error == SIGHUP) {
get_squash_ids();
exports_parse();
return;
}
if (error == SIGUSR1) {
if (fh_cache_use > 0)
logmsg(LOG_INFO, "fh entries %i access %i hit %i miss %i",
fh_cache_max, fh_cache_use, fh_cache_hit,
fh_cache_use - fh_cache_hit);
else
logmsg(LOG_INFO, "fh cache unused");
logmsg(LOG_INFO, "open file descriptors: read %i, write %i",
fd_cache_readers, fd_cache_writers);
return;
}
#endif /* WIN32 */
if (opt_portmapper) {
svc_unreg(MOUNTPROG, MOUNTVERS1);
svc_unreg(MOUNTPROG, MOUNTVERS3);
}
if (opt_portmapper) {
svc_unreg(NFS3_PROGRAM, NFS_V3);
}
if (error == SIGSEGV)
logmsg(LOG_EMERG, "segmentation fault");
fd_cache_purge();
if (opt_detach)
closelog();
remove_pid_file();
backend_shutdown();
exit(1);
}
/*
* NFS service dispatch function
* generated by rpcgen
*/
static void nfs3_program_3(struct svc_req *rqstp, register SVCXPRT * transp)
{
union {
GETATTR3args nfsproc3_getattr_3_arg;
SETATTR3args nfsproc3_setattr_3_arg;
LOOKUP3args nfsproc3_lookup_3_arg;
ACCESS3args nfsproc3_access_3_arg;
READLINK3args nfsproc3_readlink_3_arg;
READ3args nfsproc3_read_3_arg;
WRITE3args nfsproc3_write_3_arg;
CREATE3args nfsproc3_create_3_arg;
MKDIR3args nfsproc3_mkdir_3_arg;
SYMLINK3args nfsproc3_symlink_3_arg;
MKNOD3args nfsproc3_mknod_3_arg;
REMOVE3args nfsproc3_remove_3_arg;
RMDIR3args nfsproc3_rmdir_3_arg;
RENAME3args nfsproc3_rename_3_arg;
LINK3args nfsproc3_link_3_arg;
READDIR3args nfsproc3_readdir_3_arg;
READDIRPLUS3args nfsproc3_readdirplus_3_arg;
FSSTAT3args nfsproc3_fsstat_3_arg;
FSINFO3args nfsproc3_fsinfo_3_arg;
PATHCONF3args nfsproc3_pathconf_3_arg;
COMMIT3args nfsproc3_commit_3_arg;
} argument;
char *result;
xdrproc_t _xdr_argument, _xdr_result;
char *(*local) (char *, struct svc_req *);
switch (rqstp->rq_proc) {
case NFSPROC3_NULL:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_void;
local = (char *(*)(char *, struct svc_req *)) nfsproc3_null_3_svc;
break;
case NFSPROC3_GETATTR:
_xdr_argument = (xdrproc_t) xdr_GETATTR3args;
_xdr_result = (xdrproc_t) xdr_GETATTR3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_getattr_3_svc;
break;
case NFSPROC3_SETATTR:
_xdr_argument = (xdrproc_t) xdr_SETATTR3args;
_xdr_result = (xdrproc_t) xdr_SETATTR3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_setattr_3_svc;
break;
case NFSPROC3_LOOKUP:
_xdr_argument = (xdrproc_t) xdr_LOOKUP3args;
_xdr_result = (xdrproc_t) xdr_LOOKUP3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_lookup_3_svc;
break;
case NFSPROC3_ACCESS:
_xdr_argument = (xdrproc_t) xdr_ACCESS3args;
_xdr_result = (xdrproc_t) xdr_ACCESS3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_access_3_svc;
break;
case NFSPROC3_READLINK:
_xdr_argument = (xdrproc_t) xdr_READLINK3args;
_xdr_result = (xdrproc_t) xdr_READLINK3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_readlink_3_svc;
break;
case NFSPROC3_READ:
_xdr_argument = (xdrproc_t) xdr_READ3args;
_xdr_result = (xdrproc_t) xdr_READ3res;
local = (char *(*)(char *, struct svc_req *)) nfsproc3_read_3_svc;
break;
case NFSPROC3_WRITE:
_xdr_argument = (xdrproc_t) xdr_WRITE3args;
_xdr_result = (xdrproc_t) xdr_WRITE3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_write_3_svc;
break;
case NFSPROC3_CREATE:
_xdr_argument = (xdrproc_t) xdr_CREATE3args;
_xdr_result = (xdrproc_t) xdr_CREATE3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_create_3_svc;
break;
case NFSPROC3_MKDIR:
_xdr_argument = (xdrproc_t) xdr_MKDIR3args;
_xdr_result = (xdrproc_t) xdr_MKDIR3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_mkdir_3_svc;
break;
case NFSPROC3_SYMLINK:
_xdr_argument = (xdrproc_t) xdr_SYMLINK3args;
_xdr_result = (xdrproc_t) xdr_SYMLINK3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_symlink_3_svc;
break;
case NFSPROC3_MKNOD:
_xdr_argument = (xdrproc_t) xdr_MKNOD3args;
_xdr_result = (xdrproc_t) xdr_MKNOD3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_mknod_3_svc;
break;
case NFSPROC3_REMOVE:
_xdr_argument = (xdrproc_t) xdr_REMOVE3args;
_xdr_result = (xdrproc_t) xdr_REMOVE3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_remove_3_svc;
break;
case NFSPROC3_RMDIR:
_xdr_argument = (xdrproc_t) xdr_RMDIR3args;
_xdr_result = (xdrproc_t) xdr_RMDIR3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_rmdir_3_svc;
break;
case NFSPROC3_RENAME:
_xdr_argument = (xdrproc_t) xdr_RENAME3args;
_xdr_result = (xdrproc_t) xdr_RENAME3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_rename_3_svc;
break;
case NFSPROC3_LINK:
_xdr_argument = (xdrproc_t) xdr_LINK3args;
_xdr_result = (xdrproc_t) xdr_LINK3res;
local = (char *(*)(char *, struct svc_req *)) nfsproc3_link_3_svc;
break;
case NFSPROC3_READDIR:
_xdr_argument = (xdrproc_t) xdr_READDIR3args;
_xdr_result = (xdrproc_t) xdr_READDIR3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_readdir_3_svc;
break;
case NFSPROC3_READDIRPLUS:
_xdr_argument = (xdrproc_t) xdr_READDIRPLUS3args;
_xdr_result = (xdrproc_t) xdr_READDIRPLUS3res;
local = (char *(*)(char *, struct svc_req *))
nfsproc3_readdirplus_3_svc;
break;
case NFSPROC3_FSSTAT:
_xdr_argument = (xdrproc_t) xdr_FSSTAT3args;
_xdr_result = (xdrproc_t) xdr_FSSTAT3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_fsstat_3_svc;
break;
case NFSPROC3_FSINFO:
_xdr_argument = (xdrproc_t) xdr_FSINFO3args;
_xdr_result = (xdrproc_t) xdr_FSINFO3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_fsinfo_3_svc;
break;
case NFSPROC3_PATHCONF:
_xdr_argument = (xdrproc_t) xdr_PATHCONF3args;
_xdr_result = (xdrproc_t) xdr_PATHCONF3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_pathconf_3_svc;
break;
case NFSPROC3_COMMIT:
_xdr_argument = (xdrproc_t) xdr_COMMIT3args;
_xdr_result = (xdrproc_t) xdr_COMMIT3res;
local =
(char *(*)(char *, struct svc_req *)) nfsproc3_commit_3_svc;
break;
default:
svcerr_noproc(transp);
return;
}
memset((char *) &argument, 0, sizeof(argument));
if (!svc_getargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
svcerr_decode(transp);
return;
}
result = (*local) ((char *) &argument, rqstp);
if (result != NULL &&
!svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
svcerr_systemerr(transp);
logmsg(LOG_CRIT, "unable to send RPC reply");
}
if (!svc_freeargs
(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
logmsg(LOG_CRIT, "unable to free XDR arguments");
}
return;
}
/*
* mount protocol dispatcher
* generated by rpcgen
*/
static void mountprog_3(struct svc_req *rqstp, register SVCXPRT * transp)
{
union {
dirpath mountproc_mnt_3_arg;
dirpath mountproc_umnt_3_arg;
} argument;
char *result;
xdrproc_t _xdr_argument, _xdr_result;
char *(*local) (char *, struct svc_req *);
switch (rqstp->rq_proc) {
case MOUNTPROC_NULL:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_void;
local =
(char *(*)(char *, struct svc_req *)) mountproc_null_3_svc;
break;
case MOUNTPROC_MNT:
_xdr_argument = (xdrproc_t) xdr_dirpath;
_xdr_result = (xdrproc_t) xdr_mountres3;
local = (char *(*)(char *, struct svc_req *)) mountproc_mnt_3_svc;
break;
case MOUNTPROC_DUMP:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_mountlist;
local =
(char *(*)(char *, struct svc_req *)) mountproc_dump_3_svc;
break;
case MOUNTPROC_UMNT:
_xdr_argument = (xdrproc_t) xdr_dirpath;
_xdr_result = (xdrproc_t) xdr_void;
local =
(char *(*)(char *, struct svc_req *)) mountproc_umnt_3_svc;
break;
case MOUNTPROC_UMNTALL:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_void;
local =
(char *(*)(char *, struct svc_req *)) mountproc_umntall_3_svc;
break;
case MOUNTPROC_EXPORT:
_xdr_argument = (xdrproc_t) xdr_void;
_xdr_result = (xdrproc_t) xdr_exports;
local =
(char *(*)(char *, struct svc_req *)) mountproc_export_3_svc;
break;
default:
svcerr_noproc(transp);
return;
}
memset((char *) &argument, 0, sizeof(argument));
if (!svc_getargs(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
svcerr_decode(transp);
return;
}
result = (*local) ((char *) &argument, rqstp);
if (result != NULL &&
!svc_sendreply(transp, (xdrproc_t) _xdr_result, result)) {
svcerr_systemerr(transp);
logmsg(LOG_CRIT, "unable to send RPC reply");
}
if (!svc_freeargs
(transp, (xdrproc_t) _xdr_argument, (caddr_t) & argument)) {
logmsg(LOG_CRIT, "unable to free XDR arguments");
}
return;
}
static void _register_service(SVCXPRT *transp,
const rpcprog_t prognum,
const char *progname,
const rpcvers_t versnum,
const char *versname,
void (*dispatch)(struct svc_req *, SVCXPRT *))
{
int type, domain;
socklen_t len;
const char *netid;
struct netconfig *nconf = NULL;
len = sizeof(type);
if (getsockopt(transp->xp_fd, SOL_SOCKET, SO_TYPE, &type, &len)) {
perror("getsockopt");
fprintf(stderr, "unable to register (%s, %s).\n",
progname, versname);
daemon_exit(0);
}
if (type == SOCK_STREAM)
netid = "tcp";
else
netid = "udp";
if (opt_portmapper) {
nconf = getnetconfigent(netid);
if (nconf == NULL) {
fprintf(stderr, "unable to get netconfig entry \"%s\"\n", netid);
daemon_exit(0);
}
}
if (!svc_reg(transp, prognum, versnum, dispatch, nconf)) {
fprintf(stderr, "unable to register (%s, %s, %s).\n",
progname, versname, netid);
daemon_exit(0);
}
if (nconf != NULL)
freenetconfigent(nconf);
if (nconf == NULL)
return;
len = sizeof(domain);
if (getsockopt(transp->xp_fd, SOL_SOCKET, SO_DOMAIN, &domain, &len)) {
perror("getsockopt");
fprintf(stderr, "unable to register (%s, %s).\n",
progname, versname);
daemon_exit(0);
}
if (domain != PF_INET6)
return;
if (type == SOCK_STREAM)
netid = "tcp6";
else
netid = "udp6";
nconf = getnetconfigent(netid);
if (nconf == NULL) {
fprintf(stderr, "unable to get netconfig entry \"%s\"\n", netid);
daemon_exit(0);
}
if (!svc_reg(transp, prognum, versnum, dispatch, nconf)) {
fprintf(stderr, "unable to register (%s, %s, %s).\n",
progname, versname, netid);
daemon_exit(0);
}
if (nconf != NULL)
freenetconfigent(nconf);
}
#define register_service(t, p, v, d) \
_register_service(t, p, #p, v, #v, d)
static void register_nfs_service(SVCXPRT * udptransp, SVCXPRT * tcptransp)
{
if (opt_portmapper) {
svc_unreg(NFS3_PROGRAM, NFS_V3);
}
if (udptransp != NULL) {
/* Register NFS service for UDP */
register_service(udptransp, NFS3_PROGRAM, NFS_V3, nfs3_program_3);
}
if (tcptransp != NULL) {
/* Register NFS service for TCP */
register_service(tcptransp, NFS3_PROGRAM, NFS_V3, nfs3_program_3);
}
}
static void register_mount_service(SVCXPRT * udptransp, SVCXPRT * tcptransp)
{
if (opt_portmapper) {
svc_unreg(MOUNTPROG, MOUNTVERS1);
svc_unreg(MOUNTPROG, MOUNTVERS3);
}
if (udptransp != NULL) {
/* Register MOUNT service (v1) for UDP */
register_service(udptransp, MOUNTPROG, MOUNTVERS1, mountprog_3);
/* Register MOUNT service (v3) for UDP */
register_service(udptransp, MOUNTPROG, MOUNTVERS3, mountprog_3);
}
if (tcptransp != NULL) {
/* Register MOUNT service (v1) for TCP */
register_service(tcptransp, MOUNTPROG, MOUNTVERS1, mountprog_3);
/* Register MOUNT service (v3) for TCP */
register_service(tcptransp, MOUNTPROG, MOUNTVERS3, mountprog_3);
}
}
static SVCXPRT *create_udp_transport(unsigned int port)
{
SVCXPRT *transp = NULL;
int sock;
sock = socket(PF_INET6, SOCK_DGRAM, 0);
if ((sock == -1) && (errno == EAFNOSUPPORT))
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror("socket");
fprintf(stderr, "Couldn't create a listening udp socket\n");
exit(1);
}
int domain;
socklen_t len;
const int on = 1;
const struct sockaddr *sin;
size_t sin_len;
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
len = sizeof(domain);
if (getsockopt(sock, SOL_SOCKET, SO_DOMAIN, &domain, &len)) {
perror("getsockopt");
fprintf(stderr, "Couldn't create a listening udp socket\n");
exit(1);
}
if (domain == PF_INET6) {
/* Make sure we null the entire sockaddr_in6 structure */
memset(&sin6, 0, sizeof(struct sockaddr_in6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
sin6.sin6_addr = opt_bind_addr;
sin = (const struct sockaddr*)&sin6;
sin_len = sizeof(sin6);
} else {
/* Make sure we null the entire sockaddr_in structure */
memset(&sin4, 0, sizeof(struct sockaddr_in));
sin4.sin_family = AF_INET;
sin4.sin_port = htons(port);
if (IN6_IS_ADDR_UNSPECIFIED(&opt_bind_addr)) {
sin4.sin_addr.s_addr = INADDR_ANY;
} else if (IN6_IS_ADDR_V4MAPPED(&opt_bind_addr) ||
IN6_IS_ADDR_V4COMPAT(&opt_bind_addr)) {
sin4.sin_addr.s_addr = ((uint32_t*)&opt_bind_addr)[3];
} else {
fprintf(stderr, "Invalid bind address specified\n");
exit(1);
}
sin = (const struct sockaddr*)&sin4;
sin_len = sizeof(sin4);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on));
if (bind(sock, sin, sin_len)) {
perror("bind");
fprintf(stderr, "Couldn't bind to udp port %d\n", port);
exit(1);
}
transp = svc_dg_create(sock, 0, 0);
if (transp == NULL) {
fprintf(stderr, "%s\n", "cannot create udp service.");
daemon_exit(0);
}
return transp;
}
static SVCXPRT *create_tcp_transport(unsigned int port)
{
SVCXPRT *transp = NULL;
int sock;
sock = socket(PF_INET6, SOCK_STREAM, 0);
if ((sock == -1) && (errno == EAFNOSUPPORT))
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("socket");
fprintf(stderr, "Couldn't create a listening tcp socket\n");
exit(1);
}
int domain;
socklen_t len;
const int on = 1;
const struct sockaddr *sin;
size_t sin_len;
struct sockaddr_in sin4;
struct sockaddr_in6 sin6;
len = sizeof(domain);
if (getsockopt(sock, SOL_SOCKET, SO_DOMAIN, &domain, &len)) {
perror("getsockopt");
fprintf(stderr, "Couldn't create a listening tcp socket\n");
exit(1);
}
if (domain == PF_INET6) {
/* Make sure we null the entire sockaddr_in6 structure */
memset(&sin6, 0, sizeof(struct sockaddr_in6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
sin6.sin6_addr = opt_bind_addr;
sin = (const struct sockaddr*)&sin6;
sin_len = sizeof(sin6);
} else {
/* Make sure we null the entire sockaddr_in structure */
memset(&sin4, 0, sizeof(struct sockaddr_in));
sin4.sin_family = AF_INET;
sin4.sin_port = htons(port);
if (IN6_IS_ADDR_UNSPECIFIED(&opt_bind_addr)) {
sin4.sin_addr.s_addr = INADDR_ANY;
} else if (IN6_IS_ADDR_V4MAPPED(&opt_bind_addr) ||
IN6_IS_ADDR_V4COMPAT(&opt_bind_addr)) {
sin4.sin_addr.s_addr = ((uint32_t*)&opt_bind_addr)[3];
} else {
fprintf(stderr, "Invalid bind address specified\n");
exit(1);
}
sin = (const struct sockaddr*)&sin4;
sin_len = sizeof(sin4);
}
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char *) &on, sizeof(on));
if (bind(sock, sin, sin_len)) {
perror("bind");
fprintf(stderr, "Couldn't bind to tcp port %d\n", port);
exit(1);
}
if (listen(sock, SOMAXCONN)) {
perror("listen");
fprintf(stderr, "Couldn't listen on tcp socket\n");
exit(1);
}
transp = svc_vc_create(sock, 0, 0);
if (transp == NULL) {
fprintf(stderr, "%s\n", "cannot create tcp service.");
daemon_exit(0);
}
return transp;
}
/* Run RPC service. This is our own implementation of svc_run(), which
allows us to handle other events as well. */
static void unfs3_svc_run(void)
{
#if defined(HAVE_SVC_GETREQ_POLL) && HAVE_DECL_SVC_POLLFD
int r;
#else
fd_set readfds;
struct timeval tv;
#endif
for (;;) {
fd_cache_close_inactive();
#if defined(HAVE_SVC_GETREQ_POLL) && HAVE_DECL_SVC_POLLFD
r = poll(svc_pollfd, svc_max_pollfd, 2*1000);
if (r < 0) {
if (errno == EINTR) {
continue;
}
perror("unfs3_svc_run: poll failed");
return;
}
else if (r)
svc_getreq_poll(svc_pollfd, r);
#else
readfds = svc_fdset;
tv.tv_sec = 1;
tv.tv_usec = 0;
/* Note: On Windows, it's not possible to call select with all sets
empty; to use it as a sleep function. In our case, however,
readfds should never be empty, since we always have our listen
socket. Well, at least hope that our Windows RPC library works