nfsd.c revision 1.16 1 /* $NetBSD: nfsd.c,v 1.16 1995/03/18 14:59:00 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1989, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n";
43 #endif not lint
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)nfsd.c 8.7 (Berkeley) 2/22/94";
48 #else
49 static char rcsid[] = "$NetBSD: nfsd.c,v 1.16 1995/03/18 14:59:00 cgd Exp $";
50 #endif
51 #endif not lint
52
53 #include <sys/param.h>
54 #include <sys/syslog.h>
55 #include <sys/ioctl.h>
56 #include <sys/stat.h>
57 #include <sys/wait.h>
58 #include <sys/uio.h>
59 #include <sys/ucred.h>
60 #include <sys/mount.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63
64 #include <rpc/rpc.h>
65 #include <rpc/pmap_clnt.h>
66 #include <rpc/pmap_prot.h>
67
68 #ifdef ISO
69 #include <netiso/iso.h>
70 #endif
71 #include <nfs/rpcv2.h>
72 #include <nfs/nfsv2.h>
73 #include <nfs/nfs.h>
74
75 #ifdef KERBEROS
76 #include <kerberosIV/des.h>
77 #include <kerberosIV/krb.h>
78 #endif
79
80 #include <err.h>
81 #include <errno.h>
82 #include <fcntl.h>
83 #include <grp.h>
84 #include <pwd.h>
85 #include <signal.h>
86 #include <stdio.h>
87 #include <stdlib.h>
88 #include <strings.h>
89 #include <unistd.h>
90
91 /* Global defs */
92 #ifdef DEBUG
93 #define syslog(e, s) fprintf(stderr,(s))
94 int debug = 1;
95 #else
96 int debug = 0;
97 #endif
98
99 struct nfsd_srvargs nsd;
100
101 #ifdef KERBEROS
102 char lnam[ANAME_SZ];
103 KTEXT_ST kt;
104 AUTH_DAT auth;
105 char inst[INST_SZ];
106 #endif
107
108 void nonfs __P((int));
109 void reapchild __P((int));
110 void usage __P((void));
111
112 /*
113 * Nfs server daemon mostly just a user context for nfssvc()
114 *
115 * 1 - do file descriptor and signal cleanup
116 * 2 - fork the nfsd(s)
117 * 3 - create server socket(s)
118 * 4 - register socket with portmap
119 *
120 * For connectionless protocols, just pass the socket into the kernel via.
121 * nfssvc().
122 * For connection based sockets, loop doing accepts. When you get a new
123 * socket from accept, pass the msgsock into the kernel via. nfssvc().
124 * The arguments are:
125 * -c - support iso cltp clients
126 * -r - reregister with portmapper
127 * -t - support tcp nfs clients
128 * -u - support udp nfs clients
129 * followed by "n" which is the number of nfsds' to fork off
130 */
131 int
132 main(argc, argv, envp)
133 int argc;
134 char *argv[], *envp[];
135 {
136 extern int optind;
137 struct group *grp;
138 struct nfsd_args nfsdargs;
139 struct passwd *pwd;
140 struct ucred *cr;
141 struct sockaddr_in inetaddr, inetpeer;
142 #ifdef ISO
143 struct sockaddr_iso isoaddr, isopeer;
144 #endif
145 fd_set ready, sockbits;
146 int ch, cltpflag, connect_type_cnt, i, len, maxsock, msgsock;
147 int nfsdcnt, nfssvc_flag, on, reregister, sock, tcpflag, tcpsock;
148 int tp4cnt, tp4flag, tp4sock, tpipcnt, tpipflag, tpipsock, udpflag;
149 char *cp, **cpp;
150
151 #define MAXNFSDCNT 20
152 #define DEFNFSDCNT 4
153 nfsdcnt = DEFNFSDCNT;
154 cltpflag = reregister = tcpflag = tp4cnt = tp4flag = tpipcnt = 0;
155 tpipflag = udpflag = 0;
156 #ifdef ISO
157 #define GETOPT "cn:rtu"
158 #define USAGE "[-crtu] [-n num_servers]"
159 #else
160 #define GETOPT "n:rtu"
161 #define USAGE "[-rtu] [-n num_servers]"
162 #endif
163 while ((ch = getopt(argc, argv, GETOPT)) != EOF)
164 switch (ch) {
165 case 'n':
166 nfsdcnt = atoi(optarg);
167 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
168 warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
169 nfsdcnt = DEFNFSDCNT;
170 }
171 break;
172 case 'r':
173 reregister = 1;
174 break;
175 case 't':
176 tcpflag = 1;
177 break;
178 case 'u':
179 udpflag = 1;
180 break;
181 #ifdef ISO
182 case 'c':
183 cltpflag = 1;
184 break;
185 #ifdef notyet
186 case 'i':
187 tp4cnt = 1;
188 break;
189 case 'p':
190 tpipcnt = 1;
191 break;
192 #endif /* notyet */
193 #endif /* ISO */
194 default:
195 case '?':
196 usage();
197 };
198 argv += optind;
199 argc -= optind;
200
201 /*
202 * XXX
203 * Backward compatibility, trailing number is the count of daemons.
204 */
205 if (argc > 1)
206 usage();
207 if (argc == 1) {
208 nfsdcnt = atoi(argv[0]);
209 if (nfsdcnt < 1 || nfsdcnt > MAXNFSDCNT) {
210 warnx("nfsd count %d; reset to %d", DEFNFSDCNT);
211 nfsdcnt = DEFNFSDCNT;
212 }
213 }
214
215 if (debug == 0) {
216 daemon(0, 0);
217 (void)signal(SIGHUP, SIG_IGN);
218 (void)signal(SIGINT, SIG_IGN);
219 (void)signal(SIGQUIT, SIG_IGN);
220 (void)signal(SIGSYS, nonfs);
221 (void)signal(SIGTERM, SIG_IGN);
222 }
223 (void)signal(SIGCHLD, reapchild);
224
225 if (reregister) {
226 if (udpflag &&
227 !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT))
228 err(1, "can't register with portmap for UDP.");
229 if (tcpflag &&
230 !pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT))
231 err(1, "can't register with portmap for TCP.");
232 exit(0);
233 }
234 openlog("nfsd:", LOG_PID, LOG_DAEMON);
235
236 for (i = 0; i < nfsdcnt; i++) {
237 switch (fork()) {
238 case -1:
239 syslog(LOG_ERR, "fork: %m");
240 exit (1);
241 case 0:
242 break;
243 default:
244 continue;
245 }
246
247 setproctitle("server");
248 nfssvc_flag = NFSSVC_NFSD;
249 nsd.nsd_nfsd = NULL;
250 #ifdef KERBEROS
251 nsd.nsd_authstr = (char *)kt.dat;
252 #endif
253 while (nfssvc(nfssvc_flag, &nsd) < 0) {
254 if (errno != ENEEDAUTH) {
255 syslog(LOG_ERR, "nfssvc: %m");
256 exit(1);
257 }
258 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHINFAIL;
259 #ifdef KERBEROS
260 kt.length = nsd.nsd_authlen;
261 kt.mbz = 0;
262 (void)strcpy(inst, "*");
263 if (krb_rd_req(&kt, "rcmd",
264 inst, nsd.nsd_haddr, &auth, "") == RD_AP_OK &&
265 krb_kntoln(&auth, lnam) == KSUCCESS &&
266 (pwd = getpwnam(lnam)) != NULL) {
267 cr = &nsd.nsd_cr;
268 cr->cr_uid = pwd->pw_uid;
269 cr->cr_groups[0] = pwd->pw_gid;
270 cr->cr_ngroups = 1;
271 setgrent();
272 while ((grp = getgrent()) != NULL) {
273 if (grp->gr_gid == cr->cr_groups[0])
274 continue;
275 for (cpp = grp->gr_mem;
276 *cpp != NULL; ++cpp)
277 if (!strcmp(*cpp, lnam))
278 break;
279 if (*cpp == NULL)
280 continue;
281 cr->cr_groups[cr->cr_ngroups++]
282 = grp->gr_gid;
283 if (cr->cr_ngroups == NGROUPS)
284 break;
285 }
286 endgrent();
287 nfssvc_flag = NFSSVC_NFSD | NFSSVC_AUTHIN;
288 }
289 #endif /* KERBEROS */
290 }
291 exit(0);
292 }
293
294 /* If we are serving udp, set up the socket. */
295 if (udpflag) {
296 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
297 syslog(LOG_ERR, "can't create udp socket");
298 exit(1);
299 }
300 inetaddr.sin_family = AF_INET;
301 inetaddr.sin_addr.s_addr = INADDR_ANY;
302 inetaddr.sin_port = htons(NFS_PORT);
303 inetaddr.sin_len = sizeof(inetaddr);
304 if (bind(sock,
305 (struct sockaddr *)&inetaddr, sizeof(inetaddr)) < 0) {
306 syslog(LOG_ERR, "can't bind udp addr");
307 exit(1);
308 }
309 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
310 syslog(LOG_ERR, "can't register with udp portmap");
311 exit(1);
312 }
313 nfsdargs.sock = sock;
314 nfsdargs.name = NULL;
315 nfsdargs.namelen = 0;
316 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
317 syslog(LOG_ERR, "can't Add UDP socket");
318 exit(1);
319 }
320 (void)close(sock);
321 }
322
323 #ifdef ISO
324 /* If we are serving cltp, set up the socket. */
325 if (cltpflag) {
326 if ((sock = socket(AF_ISO, SOCK_DGRAM, 0)) < 0) {
327 syslog(LOG_ERR, "can't create cltp socket");
328 exit(1);
329 }
330 memset(&isoaddr, 0, sizeof(isoaddr));
331 isoaddr.siso_family = AF_ISO;
332 isoaddr.siso_tlen = 2;
333 cp = TSEL(&isoaddr);
334 *cp++ = (NFS_PORT >> 8);
335 *cp = (NFS_PORT & 0xff);
336 isoaddr.siso_len = sizeof(isoaddr);
337 if (bind(sock,
338 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
339 syslog(LOG_ERR, "can't bind cltp addr");
340 exit(1);
341 }
342 #ifdef notyet
343 /*
344 * XXX
345 * Someday this should probably use "rpcbind", the son of
346 * portmap.
347 */
348 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_UDP, NFS_PORT)) {
349 syslog(LOG_ERR, "can't register with udp portmap");
350 exit(1);
351 }
352 #endif /* notyet */
353 nfsdargs.sock = sock;
354 nfsdargs.name = NULL;
355 nfsdargs.namelen = 0;
356 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
357 syslog(LOG_ERR, "can't add UDP socket");
358 exit(1);
359 }
360 close(sock);
361 }
362 #endif /* ISO */
363
364 /* Now set up the master server socket waiting for tcp connections. */
365 on = 1;
366 FD_ZERO(&sockbits);
367 connect_type_cnt = 0;
368 if (tcpflag) {
369 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
370 syslog(LOG_ERR, "can't create tcp socket");
371 exit(1);
372 }
373 if (setsockopt(tcpsock,
374 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
375 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
376 inetaddr.sin_family = AF_INET;
377 inetaddr.sin_addr.s_addr = INADDR_ANY;
378 inetaddr.sin_port = htons(NFS_PORT);
379 inetaddr.sin_len = sizeof(inetaddr);
380 if (bind(tcpsock,
381 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
382 syslog(LOG_ERR, "can't bind tcp addr");
383 exit(1);
384 }
385 if (listen(tcpsock, 5) < 0) {
386 syslog(LOG_ERR, "listen failed");
387 exit(1);
388 }
389 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
390 syslog(LOG_ERR, "can't register tcp with portmap");
391 exit(1);
392 }
393 FD_SET(tcpsock, &sockbits);
394 maxsock = tcpsock;
395 connect_type_cnt++;
396 }
397
398 #ifdef notyet
399 /* Now set up the master server socket waiting for tp4 connections. */
400 if (tp4flag) {
401 if ((tp4sock = socket(AF_ISO, SOCK_SEQPACKET, 0)) < 0) {
402 syslog(LOG_ERR, "can't create tp4 socket");
403 exit(1);
404 }
405 if (setsockopt(tp4sock,
406 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
407 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
408 memset(&isoaddr, 0, sizeof(isoaddr));
409 isoaddr.siso_family = AF_ISO;
410 isoaddr.siso_tlen = 2;
411 cp = TSEL(&isoaddr);
412 *cp++ = (NFS_PORT >> 8);
413 *cp = (NFS_PORT & 0xff);
414 isoaddr.siso_len = sizeof(isoaddr);
415 if (bind(tp4sock,
416 (struct sockaddr *)&isoaddr, sizeof(isoaddr)) < 0) {
417 syslog(LOG_ERR, "can't bind tp4 addr");
418 exit(1);
419 }
420 if (listen(tp4sock, 5) < 0) {
421 syslog(LOG_ERR, "listen failed");
422 exit(1);
423 }
424 /*
425 * XXX
426 * Someday this should probably use "rpcbind", the son of
427 * portmap.
428 */
429 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
430 syslog(LOG_ERR, "can't register tcp with portmap");
431 exit(1);
432 }
433 FD_SET(tp4sock, &sockbits);
434 maxsock = tp4sock;
435 connect_type_cnt++;
436 }
437
438 /* Now set up the master server socket waiting for tpip connections. */
439 if (tpipflag) {
440 if ((tpipsock = socket(AF_INET, SOCK_SEQPACKET, 0)) < 0) {
441 syslog(LOG_ERR, "can't create tpip socket");
442 exit(1);
443 }
444 if (setsockopt(tpipsock,
445 SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
446 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
447 inetaddr.sin_family = AF_INET;
448 inetaddr.sin_addr.s_addr = INADDR_ANY;
449 inetaddr.sin_port = htons(NFS_PORT);
450 inetaddr.sin_len = sizeof(inetaddr);
451 if (bind(tpipsock,
452 (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
453 syslog(LOG_ERR, "can't bind tcp addr");
454 exit(1);
455 }
456 if (listen(tpipsock, 5) < 0) {
457 syslog(LOG_ERR, "listen failed");
458 exit(1);
459 }
460 /*
461 * XXX
462 * Someday this should probably use "rpcbind", the son of
463 * portmap.
464 */
465 if (!pmap_set(RPCPROG_NFS, NFS_VER2, IPPROTO_TCP, NFS_PORT)) {
466 syslog(LOG_ERR, "can't register tcp with portmap");
467 exit(1);
468 }
469 FD_SET(tpipsock, &sockbits);
470 maxsock = tpipsock;
471 connect_type_cnt++;
472 }
473 #endif /* notyet */
474
475 if (connect_type_cnt == 0)
476 exit(0);
477
478 setproctitle("master");
479
480 /*
481 * Loop forever accepting connections and passing the sockets
482 * into the kernel for the mounts.
483 */
484 for (;;) {
485 ready = sockbits;
486 if (connect_type_cnt > 1) {
487 if (select(maxsock + 1,
488 &ready, NULL, NULL, NULL) < 1) {
489 syslog(LOG_ERR, "select failed: %m");
490 exit(1);
491 }
492 }
493 if (tcpflag && FD_ISSET(tcpsock, &ready)) {
494 len = sizeof(inetpeer);
495 if ((msgsock = accept(tcpsock,
496 (struct sockaddr *)&inetpeer, &len)) < 0) {
497 syslog(LOG_ERR, "accept failed: %m");
498 exit(1);
499 }
500 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
501 if (setsockopt(msgsock, SOL_SOCKET,
502 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
503 syslog(LOG_ERR,
504 "setsockopt SO_KEEPALIVE: %m");
505 nfsdargs.sock = msgsock;
506 nfsdargs.name = (caddr_t)&inetpeer;
507 nfsdargs.namelen = sizeof(inetpeer);
508 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
509 (void)close(msgsock);
510 }
511 #ifdef notyet
512 if (tp4flag && FD_ISSET(tp4sock, &ready)) {
513 len = sizeof(isopeer);
514 if ((msgsock = accept(tp4sock,
515 (struct sockaddr *)&isopeer, &len)) < 0) {
516 syslog(LOG_ERR, "accept failed: %m");
517 exit(1);
518 }
519 if (setsockopt(msgsock, SOL_SOCKET,
520 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
521 syslog(LOG_ERR,
522 "setsockopt SO_KEEPALIVE: %m");
523 nfsdargs.sock = msgsock;
524 nfsdargs.name = (caddr_t)&isopeer;
525 nfsdargs.namelen = len;
526 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
527 (void)close(msgsock);
528 }
529 if (tpipflag && FD_ISSET(tpipsock, &ready)) {
530 len = sizeof(inetpeer);
531 if ((msgsock = accept(tpipsock,
532 (struct sockaddr *)&inetpeer, &len)) < 0) {
533 syslog(LOG_ERR, "Accept failed: %m");
534 exit(1);
535 }
536 if (setsockopt(msgsock, SOL_SOCKET,
537 SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
538 syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m");
539 nfsdargs.sock = msgsock;
540 nfsdargs.name = (caddr_t)&inetpeer;
541 nfsdargs.namelen = len;
542 nfssvc(NFSSVC_ADDSOCK, &nfsdargs);
543 (void)close(msgsock);
544 }
545 #endif /* notyet */
546 }
547 }
548
549 void
550 usage()
551 {
552
553 (void)fprintf(stderr, "usage: nfsd %s\n", USAGE);
554 exit(1);
555 }
556
557 void
558 nonfs(signo)
559 int signo;
560 {
561
562 syslog(LOG_ERR, "missing system call: NFS not available.");
563 }
564
565 void
566 reapchild(signo)
567 int signo;
568 {
569
570 while (wait3((int *)0, WNOHANG, (struct rusage *)0) > 0);
571 }
572