xref: /openbsd-src/sbin/nfsd/nfsd.c (revision 5ea00adc69349571c870d13e59cf7779262adf44)
1 /*	$OpenBSD: nfsd.c,v 1.44 2025/01/11 18:21:02 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 	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 	if (!tcpflag)
242 		return (0);
243 
244 	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
245 		syslog(LOG_ERR, "can't create tcp socket");
246 		return (1);
247 	}
248 	if (setsockopt(tcpsock,
249 	    SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
250 		syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %s", strerror(errno));
251 	memset(&inetaddr, 0, sizeof inetaddr);
252 	inetaddr.sin_family = AF_INET;
253 	inetaddr.sin_addr.s_addr = INADDR_ANY;
254 	inetaddr.sin_port = htons(NFS_PORT);
255 	inetaddr.sin_len = sizeof(inetaddr);
256 	if (bind(tcpsock, (struct sockaddr *)&inetaddr,
257 	    sizeof (inetaddr)) == -1) {
258 		syslog(LOG_ERR, "can't bind tcp addr");
259 		return (1);
260 	}
261 	if (listen(tcpsock, 5) == -1) {
262 		syslog(LOG_ERR, "listen failed");
263 		return (1);
264 	}
265 	if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) ||
266 	    !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) {
267 		syslog(LOG_ERR, "can't register tcp with portmap");
268 		return (1);
269 	}
270 
271 	setproctitle("master");
272 
273 	/*
274 	 * Loop forever accepting connections and passing the sockets
275 	 * into the kernel for the mounts.
276 	 */
277 	for (;;) {
278 		struct sockaddr_in	inetpeer;
279 		int ret, msgsock;
280 		socklen_t len = sizeof(inetpeer);
281 
282 		if ((msgsock = accept(tcpsock,
283 		    (struct sockaddr *)&inetpeer, &len)) == -1) {
284 			if (errno == EWOULDBLOCK || errno == EINTR ||
285 			    errno == ECONNABORTED)
286 				continue;
287 			syslog(LOG_ERR, "accept failed: %s", strerror(errno));
288 			return (1);
289 		}
290 		memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero));
291 		if (setsockopt(msgsock, SOL_SOCKET,
292 		    SO_KEEPALIVE, &on, sizeof(on)) == -1)
293 			syslog(LOG_ERR,
294 			    "setsockopt SO_KEEPALIVE: %s", strerror(errno));
295 		nfsdargs.sock = msgsock;
296 		nfsdargs.name = (caddr_t)&inetpeer;
297 		nfsdargs.namelen = len;
298 		if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) == -1) {
299 			syslog(LOG_ERR, "can't Add TCP socket");
300 		}
301 		(void)close(msgsock);
302 	}
303 }
304 
305 void
306 usage(void)
307 {
308 	(void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n");
309 	exit(1);
310 }
311 
312 void
313 nonfs(int signo)
314 {
315 	int save_errno = errno;
316 	struct syslog_data sdata = SYSLOG_DATA_INIT;
317 
318 	syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available.");
319 	errno = save_errno;
320 }
321 
322 void
323 reapchild(int signo)
324 {
325 	int save_errno = errno;
326 
327 	while (wait3(NULL, WNOHANG, NULL) > 0)
328 		continue;
329 	errno = save_errno;
330 }
331