xref: /freebsd/usr.sbin/nfscbd/nfscbd.c (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 /*-
2  * Copyright (c) 2009 Rick Macklem, University of Guelph
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/linker.h>
34 #include <sys/module.h>
35 #include <sys/mount.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/stat.h>
39 #include <sys/ucred.h>
40 #include <sys/uio.h>
41 #include <sys/vnode.h>
42 #include <sys/wait.h>
43 
44 #include <nfs/nfssvc.h>
45 
46 #include <rpc/rpc.h>
47 
48 #include <fs/nfs/rpcv2.h>
49 #include <fs/nfs/nfsproto.h>
50 #include <fs/nfs/nfskpiport.h>
51 #include <fs/nfs/nfs.h>
52 
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <grp.h>
57 #include <netdb.h>
58 #include <pwd.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <syslog.h>
64 #include <unistd.h>
65 
66 /* Global defs */
67 #ifdef DEBUG
68 #define	syslog(e, s)	fprintf(stderr,(s))
69 static int	debug = 1;
70 #else
71 static int	debug = 0;
72 #endif
73 
74 static pid_t	children;
75 
76 static void	nonfs(int);
77 static void	reapchild(int);
78 static void	usage(void);
79 static void	cleanup(int);
80 static void	child_cleanup(int);
81 static void	nfscbd_exit(int);
82 static void	killchildren(void);
83 
84 /*
85  * Nfs callback server daemon.
86  *
87  * 1 - do file descriptor and signal cleanup
88  * 2 - fork the nfscbd(s)
89  * 4 - create callback server socket(s)
90  * 5 - set up server socket for rpc
91  *
92  * For connectionless protocols, just pass the socket into the kernel via.
93  * nfssvc().
94  * For connection based sockets, loop doing accepts. When you get a new
95  * socket from accept, pass the msgsock into the kernel via. nfssvc().
96  */
97 int
98 main(int argc, char *argv[])
99 {
100 	struct nfscbd_args nfscbdargs;
101 	struct nfsd_nfscbd_args nfscbdargs2;
102 	struct sockaddr_in inetaddr, inetpeer;
103 	fd_set ready, sockbits;
104 	int ch, connect_type_cnt, maxsock, msgsock, error;
105 	int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
106 	char *cp, princname[128];
107 	char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
108 	struct addrinfo *aip, hints;
109 	pid_t pid;
110 	short myport = NFSV4_CBPORT;
111 	socklen_t len;
112 
113 	if (modfind("nfscl") < 0) {
114 		/* Not present in kernel, try loading it */
115 		if (kldload("nfscl") < 0 ||
116 		    modfind("nfscl") < 0)
117 			errx(1, "nfscl is not available");
118 	}
119 	/*
120 	 * First, get our fully qualified host name, if possible.
121 	 */
122 	if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
123 		cp = strchr(myname, '.');
124 		if (cp != NULL && *(cp + 1) != '\0') {
125 			cp = myname;
126 		} else {
127 			/*
128 			 * No domain on myname, so try looking it up.
129 			 */
130 			cp = NULL;
131 			memset((void *)&hints, 0, sizeof (hints));
132 			hints.ai_flags = AI_CANONNAME;
133 			error = getaddrinfo(myname, NULL, &hints, &aip);
134 			if (error == 0) {
135 			    if (aip->ai_canonname != NULL &&
136 				(cp = strchr(aip->ai_canonname, '.')) != NULL
137 				&& *(cp + 1) != '\0') {
138 				    cp = aip->ai_canonname;
139 				    mustfreeai = 1;
140 			    } else {
141 				    freeaddrinfo(aip);
142 			    }
143 			}
144 		}
145 		if (cp == NULL)
146 			warnx("Can't get fully qualified host name");
147 		myfqdnname = cp;
148 	}
149 
150 	princname[0] = '\0';
151 #define	GETOPT	"p:P:"
152 #define	USAGE	"[ -p port_num ] [ -P client_principal ]"
153 	while ((ch = getopt(argc, argv, GETOPT)) != -1)
154 		switch (ch) {
155 		case 'p':
156 			myport = atoi(optarg);
157 			if (myport < 1) {
158 				warnx("port# non-positive, reset to %d",
159 				    NFSV4_CBPORT);
160 				myport = NFSV4_CBPORT;
161 			}
162 			break;
163 		case 'P':
164 			cp = optarg;
165 			if (cp != NULL && strlen(cp) > 0 &&
166 			    strlen(cp) < sizeof (princname)) {
167 				if (strchr(cp, '@') == NULL &&
168 				    myfqdnname != NULL)
169 					snprintf(princname, sizeof (princname),
170 					    "%s@%s", cp, myfqdnname);
171 				else
172 					strlcpy(princname, cp,
173 					    sizeof (princname));
174 			} else {
175 				warnx("client princ invalid. ignored\n");
176 			}
177 			break;
178 		default:
179 		case '?':
180 			usage();
181 		}
182 	argv += optind;
183 	argc -= optind;
184 
185 	if (argc > 0)
186 		usage();
187 
188 	if (mustfreeai)
189 		freeaddrinfo(aip);
190 	nfscbdargs2.principal = (const char *)princname;
191 	if (debug == 0) {
192 		daemon(0, 0);
193 		(void)signal(SIGTERM, SIG_IGN);
194 		(void)signal(SIGHUP, SIG_IGN);
195 		(void)signal(SIGINT, SIG_IGN);
196 		(void)signal(SIGQUIT, SIG_IGN);
197 	}
198 	(void)signal(SIGSYS, nonfs);
199 	(void)signal(SIGCHLD, reapchild);
200 
201 	openlog("nfscbd:", LOG_PID, LOG_DAEMON);
202 
203 	pid = fork();
204 	if (pid < 0) {
205 		syslog(LOG_ERR, "fork: %m");
206 		nfscbd_exit(1);
207 	} else if (pid > 0) {
208 		children = pid;
209 	} else {
210 		(void)signal(SIGUSR1, child_cleanup);
211 		setproctitle("server");
212 		nfssvc_flag = NFSSVC_NFSCBD;
213 		if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
214 			syslog(LOG_ERR, "nfssvc: %m");
215 			nfscbd_exit(1);
216 		}
217 		exit(0);
218 	}
219 	(void)signal(SIGUSR1, cleanup);
220 
221 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
222 		syslog(LOG_ERR, "can't create udp socket");
223 		nfscbd_exit(1);
224 	}
225 	memset(&inetaddr, 0, sizeof inetaddr);
226 	inetaddr.sin_family = AF_INET;
227 	inetaddr.sin_addr.s_addr = INADDR_ANY;
228 	inetaddr.sin_port = htons(myport);
229 	inetaddr.sin_len = sizeof(inetaddr);
230 	ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
231 	/* If bind() fails, this is a restart, so just skip UDP. */
232 	if (ret == 0) {
233 		len = sizeof(inetaddr);
234 		if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
235 			syslog(LOG_ERR, "can't get bound addr");
236 			nfscbd_exit(1);
237 		}
238 		nfscbdargs.port = ntohs(inetaddr.sin_port);
239 		if (nfscbdargs.port != myport) {
240 			syslog(LOG_ERR, "BAD PORT#");
241 			nfscbd_exit(1);
242 		}
243 		nfscbdargs.sock = sock;
244 		nfscbdargs.name = NULL;
245 		nfscbdargs.namelen = 0;
246 		if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
247 			syslog(LOG_ERR, "can't Add UDP socket");
248 			nfscbd_exit(1);
249 		}
250 	}
251 	(void)close(sock);
252 
253 	/* Now set up the master server socket waiting for tcp connections. */
254 	on = 1;
255 	FD_ZERO(&sockbits);
256 	connect_type_cnt = 0;
257 	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
258 		syslog(LOG_ERR, "can't create tcp socket");
259 		nfscbd_exit(1);
260 	}
261 	if (setsockopt(tcpsock,
262 	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
263 		syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
264 	/* sin_port is already set */
265 	inetaddr.sin_family = AF_INET;
266 	inetaddr.sin_addr.s_addr = INADDR_ANY;
267 	inetaddr.sin_port = htons(myport);
268 	inetaddr.sin_len = sizeof(inetaddr);
269 	if (bind(tcpsock,
270 	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
271 		syslog(LOG_ERR, "can't bind tcp addr");
272 		nfscbd_exit(1);
273 	}
274 	if (listen(tcpsock, 5) < 0) {
275 		syslog(LOG_ERR, "listen failed");
276 		nfscbd_exit(1);
277 	}
278 	FD_SET(tcpsock, &sockbits);
279 	maxsock = tcpsock;
280 	connect_type_cnt++;
281 
282 	setproctitle("master");
283 
284 	/*
285 	 * Loop forever accepting connections and passing the sockets
286 	 * into the kernel for the mounts.
287 	 */
288 	for (;;) {
289 		ready = sockbits;
290 		if (connect_type_cnt > 1) {
291 			if (select(maxsock + 1,
292 			    &ready, NULL, NULL, NULL) < 1) {
293 				syslog(LOG_ERR, "select failed: %m");
294 				nfscbd_exit(1);
295 			}
296 		}
297 		if (FD_ISSET(tcpsock, &ready)) {
298 			len = sizeof(inetpeer);
299 			if ((msgsock = accept(tcpsock,
300 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
301 				syslog(LOG_ERR, "accept failed: %m");
302 				nfscbd_exit(1);
303 			}
304 			memset(inetpeer.sin_zero, 0,
305 			    sizeof (inetpeer.sin_zero));
306 			if (setsockopt(msgsock, SOL_SOCKET,
307 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
308 				syslog(LOG_ERR,
309 				    "setsockopt SO_KEEPALIVE: %m");
310 			nfscbdargs.sock = msgsock;
311 			nfscbdargs.name = (caddr_t)&inetpeer;
312 			nfscbdargs.namelen = sizeof(inetpeer);
313 			nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
314 			(void)close(msgsock);
315 		}
316 	}
317 }
318 
319 static void
320 usage(void)
321 {
322 
323 	errx(1, "usage: nfscbd %s", USAGE);
324 }
325 
326 static void
327 nonfs(int signo __unused)
328 {
329 	syslog(LOG_ERR, "missing system call: NFS not available");
330 }
331 
332 static void
333 reapchild(int signo __unused)
334 {
335 	pid_t pid;
336 
337 	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
338 		if (pid == children)
339 			children = -1;
340 	}
341 }
342 
343 static void
344 killchildren(void)
345 {
346 
347 	if (children > 0)
348 		kill(children, SIGKILL);
349 }
350 
351 /*
352  * Cleanup master after SIGUSR1.
353  */
354 static void
355 cleanup(int signo __unused)
356 {
357 	nfscbd_exit(0);
358 }
359 
360 /*
361  * Cleanup child after SIGUSR1.
362  */
363 static void
364 child_cleanup(int signo __unused)
365 {
366 	exit(0);
367 }
368 
369 static void
370 nfscbd_exit(int status __unused)
371 {
372 	killchildren();
373 	exit(status);
374 }
375