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