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