xref: /openbsd/sbin/nfsd/nfsd.c (revision 404b540a)
1 /*	$OpenBSD: nfsd.c,v 1.28 2008/03/13 16:16:42 thib Exp $	*/
2 /*	$NetBSD: nfsd.c,v 1.19 1996/02/18 23:18:56 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static const char copyright[] =
38 "@(#) Copyright (c) 1989, 1993, 1994\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static const char sccsid[] = "@(#)nfsd.c	8.9 (Berkeley) 3/29/95";
45 #else
46 static const char rcsid[] = "$OpenBSD: nfsd.c,v 1.28 2008/03/13 16:16:42 thib Exp $";
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/ioctl.h>
52 #include <sys/stat.h>
53 #include <sys/wait.h>
54 #include <sys/uio.h>
55 #include <sys/ucred.h>
56 #include <sys/mount.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 
60 #include <rpc/rpc.h>
61 #include <rpc/pmap_clnt.h>
62 #include <rpc/pmap_prot.h>
63 
64 #include <nfs/rpcv2.h>
65 #include <nfs/nfsproto.h>
66 #include <nfs/nfs.h>
67 
68 #include <err.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <grp.h>
72 #include <pwd.h>
73 #include <signal.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <syslog.h>
78 #include <unistd.h>
79 
80 /* Global defs */
81 #ifdef DEBUG
82 #define	syslog(e, s)	fprintf(stderr,(s))
83 int	debug = 1;
84 #else
85 int	debug = 0;
86 #endif
87 
88 struct	nfsd_srvargs nsd;
89 
90 void	nonfs(int);
91 void	reapchild(int);
92 void	usage(void);
93 
94 #define	MAXNFSDCNT	20
95 #define	DEFNFSDCNT	 4
96 
97 /*
98  * Nfs server daemon mostly just a user context for nfssvc()
99  *
100  * 1 - do file descriptor and signal cleanup
101  * 2 - fork the nfsd(s)
102  * 3 - create server socket(s)
103  * 4 - register socket with portmap
104  *
105  * For connectionless protocols, just pass the socket into the kernel via.
106  * nfssvc().
107  * For connection based sockets, loop doing accepts. When you get a new
108  * socket from accept, pass the msgsock into the kernel via. nfssvc().
109  * The arguments are:
110  *	-r - reregister with portmapper
111  *	-t - support tcp nfs clients
112  *	-u - support udp nfs clients
113  * followed by "n" which is the number of nfsds' to fork off
114  */
115 int
116 main(int argc, char *argv[])
117 {
118 	struct nfsd_args nfsdargs;
119 	struct sockaddr_in inetaddr, inetpeer;
120 	fd_set *ready, *sockbits;
121 	size_t fd_size;
122 	int ch, connect_type_cnt, i, maxsock = 0, msgsock;
123 	int nfsdcnt = DEFNFSDCNT, on, reregister = 0, sock;
124 	int udpflag = 0, tcpflag = 0, tcpsock;
125 	const char *errstr = NULL;
126 	socklen_t len;
127 
128 	if (argc == 1)
129 		udpflag = 1;
130 	while ((ch = getopt(argc, argv, "n:rtu")) != -1)
131 		switch (ch) {
132 		case 'n':
133 			nfsdcnt = strtonum(optarg, 1, MAXNFSDCNT, &errstr);
134 			if (errstr) {
135 				warnx("nfsd count %s %s; reset to %d",
136 				    optarg, errstr, DEFNFSDCNT);
137 				nfsdcnt = DEFNFSDCNT;
138 			}
139 			break;
140 		case 'r':
141 			reregister = 1;
142 			break;
143 		case 't':
144 			tcpflag = 1;
145 			break;
146 		case 'u':
147 			udpflag = 1;
148 			break;
149 		default:
150 			usage();
151 		};
152 	argv += optind;
153 	argc -= optind;
154 
155 	/*
156 	 * XXX
157 	 * Backward compatibility, trailing number is the count of daemons.
158 	 */
159 	if (argc > 1)
160 		usage();
161 	if (argc == 1) {
162 		nfsdcnt = strtonum(argv[0], 1, MAXNFSDCNT, &errstr);
163 		if (errstr) {
164 			warnx("nfsd count %s %s; reset to %d",
165 			    argv[0], errstr, DEFNFSDCNT);
166 			nfsdcnt = DEFNFSDCNT;
167 		}
168 	}
169 
170 	if (debug == 0) {
171 		daemon(0, 0);
172 		(void)signal(SIGHUP, SIG_IGN);
173 		(void)signal(SIGINT, SIG_IGN);
174 		(void)signal(SIGQUIT, SIG_IGN);
175 		(void)signal(SIGSYS, nonfs);
176 	}
177 	(void)signal(SIGCHLD, reapchild);
178 
179 	if (reregister) {
180 		if (udpflag &&
181 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
182 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
183 			err(1, "can't register with portmap for UDP.");
184 		if (tcpflag &&
185 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
186 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
187 			err(1, "can't register with portmap for TCP.");
188 		return (0);
189 	}
190 	openlog("nfsd", LOG_PID, LOG_DAEMON);
191 
192 	for (i = 0; i < nfsdcnt; i++) {
193 		switch (fork()) {
194 		case -1:
195 			syslog(LOG_ERR, "fork: %m");
196 			return (1);
197 		case 0:
198 			break;
199 		default:
200 			continue;
201 		}
202 
203 		setproctitle("server");
204 		nsd.nsd_nfsd = NULL;
205 		if (nfssvc(NFSSVC_NFSD, &nsd) < 0) {
206 			syslog(LOG_ERR, "nfssvc: %m");
207 			return (1);
208 		}
209 		return (0);
210 	}
211 
212 	/* If we are serving udp, set up the socket. */
213 	if (udpflag) {
214 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
215 			syslog(LOG_ERR, "can't create udp socket");
216 			return (1);
217 		}
218 		memset(&inetaddr, 0, sizeof inetaddr);
219 		inetaddr.sin_family = AF_INET;
220 		inetaddr.sin_addr.s_addr = INADDR_ANY;
221 		inetaddr.sin_port = htons(NFS_PORT);
222 		inetaddr.sin_len = sizeof(inetaddr);
223 		if (bind(sock, (struct sockaddr *)&inetaddr,
224 		    sizeof(inetaddr)) < 0) {
225 			syslog(LOG_ERR, "can't bind udp addr");
226 			return (1);
227 		}
228 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
229 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
230 			syslog(LOG_ERR, "can't register with udp portmap");
231 			return (1);
232 		}
233 		nfsdargs.sock = sock;
234 		nfsdargs.name = NULL;
235 		nfsdargs.namelen = 0;
236 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
237 			syslog(LOG_ERR, "can't Add UDP socket");
238 			return (1);
239 		}
240 		(void)close(sock);
241 	}
242 
243 	/* Now set up the master server socket waiting for tcp connections. */
244 	on = 1;
245 	connect_type_cnt = 0;
246 	if (tcpflag) {
247 		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
248 			syslog(LOG_ERR, "can't create tcp socket");
249 			return (1);
250 		}
251 		if (setsockopt(tcpsock,
252 		    SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
253 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
254 		memset(&inetaddr, 0, sizeof inetaddr);
255 		inetaddr.sin_family = AF_INET;
256 		inetaddr.sin_addr.s_addr = INADDR_ANY;
257 		inetaddr.sin_port = htons(NFS_PORT);
258 		inetaddr.sin_len = sizeof(inetaddr);
259 		if (bind(tcpsock, (struct sockaddr *)&inetaddr,
260 		    sizeof (inetaddr)) < 0) {
261 			syslog(LOG_ERR, "can't bind tcp addr");
262 			return (1);
263 		}
264 		if (listen(tcpsock, 5) < 0) {
265 			syslog(LOG_ERR, "listen failed");
266 			return (1);
267 		}
268 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
269 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
270 			syslog(LOG_ERR, "can't register tcp with portmap");
271 			return (1);
272 		}
273 		maxsock = tcpsock;
274 		connect_type_cnt++;
275 	}
276 
277 	if (connect_type_cnt == 0)
278 		return (0);
279 
280 	setproctitle("master");
281 
282 	/*
283 	 * Allocate space for the fd_set pointers and fill in sockbits
284 	 */
285 	fd_size = howmany(maxsock + 1, NFDBITS) * sizeof(fd_mask);
286 	sockbits = malloc(fd_size);
287 	ready = malloc(fd_size);
288 	if (sockbits == NULL || ready == NULL) {
289 		syslog(LOG_ERR, "cannot allocate memory");
290 		return (1);
291 	}
292 	memset(sockbits, 0, fd_size);
293 	if (tcpflag)
294 		FD_SET(tcpsock, sockbits);
295 
296 	/*
297 	 * Loop forever accepting connections and passing the sockets
298 	 * into the kernel for the mounts.
299 	 */
300 	for (;;) {
301 		memcpy(ready, sockbits, fd_size);
302 		if (connect_type_cnt > 1) {
303 			if (select(maxsock + 1,
304 			    ready, NULL, NULL, NULL) < 1) {
305 				syslog(LOG_ERR, "select failed: %m");
306 				return (1);
307 			}
308 		}
309 		if (tcpflag && FD_ISSET(tcpsock, ready)) {
310 			len = sizeof(inetpeer);
311 			if ((msgsock = accept(tcpsock,
312 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
313 				syslog(LOG_ERR, "accept failed: %m");
314 				return (1);
315 			}
316 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
317 			if (setsockopt(msgsock, SOL_SOCKET,
318 			    SO_KEEPALIVE, &on, sizeof(on)) < 0)
319 				syslog(LOG_ERR,
320 				    "setsockopt SO_KEEPALIVE: %m");
321 			nfsdargs.sock = msgsock;
322 			nfsdargs.name = (caddr_t)&inetpeer;
323 			nfsdargs.namelen = sizeof(inetpeer);
324 			if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
325 				syslog(LOG_ERR, "can't Add TCP socket");
326 				return (1);
327 			}
328 			(void)close(msgsock);
329 		}
330 	}
331 }
332 
333 void
334 usage(void)
335 {
336 	(void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n");
337 	exit(1);
338 }
339 
340 /* ARGSUSED */
341 void
342 nonfs(int signo)
343 {
344 	int save_errno = errno;
345 	struct syslog_data sdata = SYSLOG_DATA_INIT;
346 
347 	syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available.");
348 	errno = save_errno;
349 }
350 
351 /* ARGSUSED */
352 void
353 reapchild(int signo)
354 {
355 	int save_errno = errno;
356 
357 	while (wait3(NULL, WNOHANG, NULL) > 0)
358 		;
359 	errno = save_errno;
360 }
361