xref: /openbsd/sbin/nfsd/nfsd.c (revision 9c7bd2b0)
1 /*	$OpenBSD: nfsd.c,v 1.45 2025/01/16 12:46:03 kn 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 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <sys/uio.h>
40 #include <sys/ucred.h>
41 #include <sys/mount.h>
42 #include <sys/socket.h>
43 
44 #include <rpc/rpc.h>
45 #include <rpc/pmap_clnt.h>
46 #include <rpc/pmap_prot.h>
47 
48 #include <nfs/rpcv2.h>
49 #include <nfs/nfsproto.h>
50 #include <nfs/nfs.h>
51 
52 #include <err.h>
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <signal.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <unistd.h>
63 
64 /* Global defs */
65 #ifdef DEBUG
66 #define	syslog(e, s, ...)			\
67 do {						\
68 	fprintf(stderr, (s), ##__VA_ARGS__);	\
69 	fprintf(stderr, "\n");			\
70 } while (0)
71 int	debug = 1;
72 #else
73 int	debug = 0;
74 #endif
75 
76 struct	nfsd_srvargs nsd;
77 
78 void	nonfs(int);
79 void	reapchild(int);
80 void	usage(void);
81 
82 #define	MAXNFSDCNT	20
83 #define	DEFNFSDCNT	 4
84 
85 /*
86  * Nfs server daemon mostly just a user context for nfssvc()
87  *
88  * 1 - do file descriptor and signal cleanup
89  * 2 - fork the nfsd(s)
90  * 3 - create server socket(s)
91  * 4 - register socket with portmap
92  *
93  * For connectionless protocols, just pass the socket into the kernel via.
94  * nfssvc().
95  * For connection based sockets, loop doing accepts. When you get a new
96  * socket from accept, pass the msgsock into the kernel via. nfssvc().
97  * The arguments are:
98  *	-r - reregister with portmapper
99  *	-t - support tcp nfs clients
100  *	-u - support udp nfs clients
101  * followed by "n" which is the number of nfsds' to fork off
102  */
103 int
104 main(int argc, char *argv[])
105 {
106 	struct nfsd_args nfsdargs;
107 	struct sockaddr_in inetaddr;
108 	int ch, i;
109 	int nfsdcnt = DEFNFSDCNT, on, reregister = 0, sock;
110 	int udpflag = 0, tcpflag = 0, tcpsock;
111 	const char *errstr = NULL;
112 
113 	/* Start by writing to both console and log. */
114 	openlog("nfsd", LOG_PID | LOG_PERROR, LOG_DAEMON);
115 
116 	if (unveil("/", "") == -1) {
117 		syslog(LOG_ERR, "unveil /: %s", strerror(errno));
118 		return (1);
119 	}
120 	if (unveil(NULL, NULL) == -1) {
121 		syslog(LOG_ERR, "unveil: %s", strerror(errno));
122 		return (1);
123 	}
124 
125 	while ((ch = getopt(argc, argv, "n:rtu")) != -1)
126 		switch (ch) {
127 		case 'n':
128 			nfsdcnt = strtonum(optarg, 1, MAXNFSDCNT, &errstr);
129 			if (errstr) {
130 				syslog(LOG_ERR, "nfsd count is %s: %s", errstr, optarg);
131 				return(1);
132 			}
133 			break;
134 		case 'r':
135 			reregister = 1;
136 			break;
137 		case 't':
138 			tcpflag = 1;
139 			break;
140 		case 'u':
141 			udpflag = 1;
142 			break;
143 		default:
144 			usage();
145 		}
146 	argv += optind;
147 	argc -= optind;
148 
149 	if (!(tcpflag || udpflag))
150 		udpflag = 1;
151 
152 	/*
153 	 * XXX
154 	 * Backward compatibility, trailing number is the count of daemons.
155 	 */
156 	if (argc > 1)
157 		usage();
158 	if (argc == 1) {
159 		nfsdcnt = strtonum(argv[0], 1, MAXNFSDCNT, &errstr);
160 		if (errstr) {
161 			syslog(LOG_ERR, "nfsd count is %s: %s", errstr, optarg);
162 			return(1);
163 		}
164 	}
165 
166 	if (debug == 0) {
167 		daemon(0, 0);
168 		(void)signal(SIGHUP, SIG_IGN);
169 		(void)signal(SIGINT, SIG_IGN);
170 		(void)signal(SIGQUIT, SIG_IGN);
171 		(void)signal(SIGSYS, nonfs);
172 	}
173 	(void)signal(SIGCHLD, reapchild);
174 
175 	if (reregister) {
176 		if (udpflag &&
177 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
178 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT))) {
179 			syslog(LOG_ERR, "can't register with portmap for UDP (%s).",
180 			    strerror(errno));
181 			return (1);
182 		}
183 		if (tcpflag &&
184 		    (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
185 		     !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT))) {
186 			syslog(LOG_ERR, "can't register with portmap for TCP (%s).",
187 			    strerror(errno));
188 			return (1);
189 		}
190 		return (0);
191 	}
192 
193 	/* Cut back to writing to log only. */
194 	closelog();
195 	openlog("nfsd", LOG_PID, LOG_DAEMON);
196 
197 	for (i = 0; i < nfsdcnt; i++) {
198 		switch (fork()) {
199 		case -1:
200 			syslog(LOG_ERR, "fork: %s", strerror(errno));
201 			return (1);
202 		case 0:
203 			break;
204 		default:
205 			continue;
206 		}
207 
208 		setproctitle("server");
209 		nsd.nsd_nfsd = NULL;
210 		if (nfssvc(NFSSVC_NFSD, &nsd) == -1) {
211 			syslog(LOG_ERR, "nfssvc: %s", strerror(errno));
212 			return (1);
213 		}
214 		return (0);
215 	}
216 
217 	/* If we are serving udp, set up the socket. */
218 	if (udpflag) {
219 		if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
220 			syslog(LOG_ERR, "can't create udp socket");
221 			return (1);
222 		}
223 		memset(&inetaddr, 0, sizeof inetaddr);
224 		inetaddr.sin_family = AF_INET;
225 		inetaddr.sin_addr.s_addr = INADDR_ANY;
226 		inetaddr.sin_port = htons(NFS_PORT);
227 		inetaddr.sin_len = sizeof(inetaddr);
228 		if (bind(sock, (struct sockaddr *)&inetaddr,
229 		    sizeof(inetaddr)) == -1) {
230 			syslog(LOG_ERR, "can't bind udp addr");
231 			return (1);
232 		}
233 		if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) ||
234 		    !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) {
235 			syslog(LOG_ERR, "can't register with udp portmap");
236 			return (1);
237 		}
238 		nfsdargs.sock = sock;
239 		nfsdargs.name = NULL;
240 		nfsdargs.namelen = 0;
241 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) == -1) {
242 			syslog(LOG_ERR, "can't Add UDP socket");
243 			return (1);
244 		}
245 		(void)close(sock);
246 	}
247 
248 	/* Now set up the master server socket waiting for tcp connections. */
249 	on = 1;
250 	if (!tcpflag)
251 		return (0);
252 
253 	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
254 		syslog(LOG_ERR, "can't create tcp socket");
255 		return (1);
256 	}
257 	if (setsockopt(tcpsock,
258 	    SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
259 		syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %s", strerror(errno));
260 	memset(&inetaddr, 0, sizeof inetaddr);
261 	inetaddr.sin_family = AF_INET;
262 	inetaddr.sin_addr.s_addr = INADDR_ANY;
263 	inetaddr.sin_port = htons(NFS_PORT);
264 	inetaddr.sin_len = sizeof(inetaddr);
265 	if (bind(tcpsock, (struct sockaddr *)&inetaddr,
266 	    sizeof (inetaddr)) == -1) {
267 		syslog(LOG_ERR, "can't bind tcp addr");
268 		return (1);
269 	}
270 	if (listen(tcpsock, 5) == -1) {
271 		syslog(LOG_ERR, "listen failed");
272 		return (1);
273 	}
274 	if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
275 	    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
276 		syslog(LOG_ERR, "can't register tcp with portmap");
277 		return (1);
278 	}
279 
280 	setproctitle("master");
281 
282 	/*
283 	 * Loop forever accepting connections and passing the sockets
284 	 * into the kernel for the mounts.
285 	 */
286 	for (;;) {
287 		struct sockaddr_in	inetpeer;
288 		int ret, msgsock;
289 		socklen_t len = sizeof(inetpeer);
290 
291 		if ((msgsock = accept(tcpsock,
292 		    (struct sockaddr *)&inetpeer, &len)) == -1) {
293 			if (errno == EWOULDBLOCK || errno == EINTR ||
294 			    errno == ECONNABORTED)
295 				continue;
296 			syslog(LOG_ERR, "accept failed: %s", strerror(errno));
297 			return (1);
298 		}
299 		memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
300 		if (setsockopt(msgsock, SOL_SOCKET,
301 		    SO_KEEPALIVE, &on, sizeof(on)) == -1)
302 			syslog(LOG_ERR,
303 			    "setsockopt SO_KEEPALIVE: %s", strerror(errno));
304 		nfsdargs.sock = msgsock;
305 		nfsdargs.name = (caddr_t)&inetpeer;
306 		nfsdargs.namelen = len;
307 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) == -1) {
308 			syslog(LOG_ERR, "can't Add TCP socket");
309 		}
310 		(void)close(msgsock);
311 	}
312 }
313 
314 void
315 usage(void)
316 {
317 	(void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n");
318 	exit(1);
319 }
320 
321 void
322 nonfs(int signo)
323 {
324 	int save_errno = errno;
325 	struct syslog_data sdata = SYSLOG_DATA_INIT;
326 
327 	syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available.");
328 	errno = save_errno;
329 }
330 
331 void
332 reapchild(int signo)
333 {
334 	int save_errno = errno;
335 
336 	while (wait3(NULL, WNOHANG, NULL) > 0)
337 		continue;
338 	errno = save_errno;
339 }
340