xref: /openbsd-src/sbin/nfsd/nfsd.c (revision 09b20ce0965b0c782137f33fbf9911b7e02b571e)
1 /*	$OpenBSD: nfsd.c,v 1.27 2007/05/27 21:14:41 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.27 2007/05/27 21:14:41 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 	while ((ch = getopt(argc, argv, "n:rtu")) != -1)
129 		switch (ch) {
130 		case 'n':
131 			nfsdcnt = strtonum(optarg, 1, MAXNFSDCNT, &errstr);
132 			if (errstr) {
133 				warnx("nfsd count %s %s; reset to %d",
134 				    optarg, errstr, DEFNFSDCNT);
135 				nfsdcnt = DEFNFSDCNT;
136 			}
137 			break;
138 		case 'r':
139 			reregister = 1;
140 			break;
141 		case 't':
142 			tcpflag = 1;
143 			break;
144 		case 'u':
145 			udpflag = 1;
146 			break;
147 		default:
148 			usage();
149 		};
150 	argv += optind;
151 	argc -= optind;
152 
153 	/*
154 	 * XXX
155 	 * Backward compatibility, trailing number is the count of daemons.
156 	 */
157 	if (argc > 1)
158 		usage();
159 	if (argc == 1) {
160 		nfsdcnt = strtonum(argv[0], 1, MAXNFSDCNT, &errstr);
161 		if (errstr) {
162 			warnx("nfsd count %s %s; reset to %d",
163 			    argv[0], errstr, DEFNFSDCNT);
164 			nfsdcnt = DEFNFSDCNT;
165 		}
166 	}
167 
168 	if (debug == 0) {
169 		daemon(0, 0);
170 		(void)signal(SIGHUP, SIG_IGN);
171 		(void)signal(SIGINT, SIG_IGN);
172 		(void)signal(SIGQUIT, SIG_IGN);
173 		(void)signal(SIGSYS, nonfs);
174 	}
175 	(void)signal(SIGCHLD, reapchild);
176 
177 	if (reregister) {
178 		if (udpflag &&
179 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
180 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)))
181 			err(1, "can't register with portmap for UDP.");
182 		if (tcpflag &&
183 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
184 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)))
185 			err(1, "can't register with portmap for TCP.");
186 		return (0);
187 	}
188 	openlog("nfsd", LOG_PID, LOG_DAEMON);
189 
190 	for (i = 0; i < nfsdcnt; i++) {
191 		switch (fork()) {
192 		case -1:
193 			syslog(LOG_ERR, "fork: %m");
194 			return (1);
195 		case 0:
196 			break;
197 		default:
198 			continue;
199 		}
200 
201 		setproctitle("server");
202 		nsd.nsd_nfsd = NULL;
203 		if (nfssvc(NFSSVC_NFSD, &nsd) < 0) {
204 			syslog(LOG_ERR, "nfssvc: %m");
205 			return (1);
206 		}
207 		return (0);
208 	}
209 
210 	/* If we are serving udp, set up the socket. */
211 	if (udpflag) {
212 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
213 			syslog(LOG_ERR, "can't create udp socket");
214 			return (1);
215 		}
216 		memset(&inetaddr, 0, sizeof inetaddr);
217 		inetaddr.sin_family = AF_INET;
218 		inetaddr.sin_addr.s_addr = INADDR_ANY;
219 		inetaddr.sin_port = htons(NFS_PORT);
220 		inetaddr.sin_len = sizeof(inetaddr);
221 		if (bind(sock, (struct sockaddr *)&inetaddr,
222 		    sizeof(inetaddr)) < 0) {
223 			syslog(LOG_ERR, "can't bind udp addr");
224 			return (1);
225 		}
226 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
227 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
228 			syslog(LOG_ERR, "can't register with udp portmap");
229 			return (1);
230 		}
231 		nfsdargs.sock = sock;
232 		nfsdargs.name = NULL;
233 		nfsdargs.namelen = 0;
234 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
235 			syslog(LOG_ERR, "can't Add UDP socket");
236 			return (1);
237 		}
238 		(void)close(sock);
239 	}
240 
241 	/* Now set up the master server socket waiting for tcp connections. */
242 	on = 1;
243 	connect_type_cnt = 0;
244 	if (tcpflag) {
245 		if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
246 			syslog(LOG_ERR, "can't create tcp socket");
247 			return (1);
248 		}
249 		if (setsockopt(tcpsock,
250 		    SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
251 			syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
252 		memset(&inetaddr, 0, sizeof inetaddr);
253 		inetaddr.sin_family = AF_INET;
254 		inetaddr.sin_addr.s_addr = INADDR_ANY;
255 		inetaddr.sin_port = htons(NFS_PORT);
256 		inetaddr.sin_len = sizeof(inetaddr);
257 		if (bind(tcpsock, (struct sockaddr *)&inetaddr,
258 		    sizeof (inetaddr)) < 0) {
259 			syslog(LOG_ERR, "can't bind tcp addr");
260 			return (1);
261 		}
262 		if (listen(tcpsock, 5) < 0) {
263 			syslog(LOG_ERR, "listen failed");
264 			return (1);
265 		}
266 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
267 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
268 			syslog(LOG_ERR, "can't register tcp with portmap");
269 			return (1);
270 		}
271 		maxsock = tcpsock;
272 		connect_type_cnt++;
273 	}
274 
275 	if (connect_type_cnt == 0)
276 		return (0);
277 
278 	setproctitle("master");
279 
280 	/*
281 	 * Allocate space for the fd_set pointers and fill in sockbits
282 	 */
283 	fd_size = howmany(maxsock + 1, NFDBITS) * sizeof(fd_mask);
284 	sockbits = malloc(fd_size);
285 	ready = malloc(fd_size);
286 	if (sockbits == NULL || ready == NULL) {
287 		syslog(LOG_ERR, "cannot allocate memory");
288 		return (1);
289 	}
290 	memset(sockbits, 0, fd_size);
291 	if (tcpflag)
292 		FD_SET(tcpsock, sockbits);
293 
294 	/*
295 	 * Loop forever accepting connections and passing the sockets
296 	 * into the kernel for the mounts.
297 	 */
298 	for (;;) {
299 		memcpy(ready, sockbits, fd_size);
300 		if (connect_type_cnt > 1) {
301 			if (select(maxsock + 1,
302 			    ready, NULL, NULL, NULL) < 1) {
303 				syslog(LOG_ERR, "select failed: %m");
304 				return (1);
305 			}
306 		}
307 		if (tcpflag && FD_ISSET(tcpsock, ready)) {
308 			len = sizeof(inetpeer);
309 			if ((msgsock = accept(tcpsock,
310 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
311 				syslog(LOG_ERR, "accept failed: %m");
312 				return (1);
313 			}
314 			memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
315 			if (setsockopt(msgsock, SOL_SOCKET,
316 			    SO_KEEPALIVE, &on, sizeof(on)) < 0)
317 				syslog(LOG_ERR,
318 				    "setsockopt SO_KEEPALIVE: %m");
319 			nfsdargs.sock = msgsock;
320 			nfsdargs.name = (caddr_t)&inetpeer;
321 			nfsdargs.namelen = sizeof(inetpeer);
322 			if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) {
323 				syslog(LOG_ERR, "can't Add TCP socket");
324 				return (1);
325 			}
326 			(void)close(msgsock);
327 		}
328 	}
329 }
330 
331 void
332 usage(void)
333 {
334 	(void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n");
335 	exit(1);
336 }
337 
338 /* ARGSUSED */
339 void
340 nonfs(int signo)
341 {
342 	int save_errno = errno;
343 	struct syslog_data sdata = SYSLOG_DATA_INIT;
344 
345 	syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available.");
346 	errno = save_errno;
347 }
348 
349 /* ARGSUSED */
350 void
351 reapchild(int signo)
352 {
353 	int save_errno = errno;
354 
355 	while (wait3(NULL, WNOHANG, NULL) > 0)
356 		;
357 	errno = save_errno;
358 }
359