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