xref: /freebsd/usr.sbin/nfscbd/nfscbd.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <sys/param.h>
30 #include <sys/ioctl.h>
31 #include <sys/linker.h>
32 #include <sys/module.h>
33 #include <sys/mount.h>
34 #include <sys/socket.h>
35 #include <sys/socketvar.h>
36 #include <sys/stat.h>
37 #include <sys/ucred.h>
38 #include <sys/uio.h>
39 #include <sys/vnode.h>
40 #include <sys/wait.h>
41 
42 #include <nfs/nfssvc.h>
43 
44 #include <rpc/rpc.h>
45 
46 #include <fs/nfs/rpcv2.h>
47 #include <fs/nfs/nfsproto.h>
48 #include <fs/nfs/nfskpiport.h>
49 #include <fs/nfs/nfs.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <grp.h>
55 #include <netdb.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)	fprintf(stderr,(s))
67 static int	debug = 1;
68 #else
69 static int	debug = 0;
70 #endif
71 
72 static pid_t	children;
73 
74 static void	nonfs(int);
75 static void	reapchild(int);
76 static void	usage(void);
77 static void	cleanup(int);
78 static void	child_cleanup(int);
79 static void	nfscbd_exit(int);
80 static void	killchildren(void);
81 
82 /*
83  * Nfs callback server daemon.
84  *
85  * 1 - do file descriptor and signal cleanup
86  * 2 - fork the nfscbd(s)
87  * 4 - create callback server socket(s)
88  * 5 - set up server socket for rpc
89  *
90  * For connectionless protocols, just pass the socket into the kernel via.
91  * nfssvc().
92  * For connection based sockets, loop doing accepts. When you get a new
93  * socket from accept, pass the msgsock into the kernel via. nfssvc().
94  */
95 int
96 main(int argc, char *argv[])
97 {
98 	struct nfscbd_args nfscbdargs;
99 	struct nfsd_nfscbd_args nfscbdargs2;
100 	struct sockaddr_in inetaddr, inetpeer;
101 	fd_set ready, sockbits;
102 	int ch, connect_type_cnt, maxsock, msgsock, error;
103 	int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
104 	char *cp, princname[128];
105 	char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
106 	struct addrinfo *aip, hints;
107 	pid_t pid;
108 	short myport = NFSV4_CBPORT;
109 	socklen_t len;
110 
111 	if (modfind("nfscl") < 0) {
112 		/* Not present in kernel, try loading it */
113 		if (kldload("nfscl") < 0 ||
114 		    modfind("nfscl") < 0)
115 			errx(1, "nfscl is not available");
116 	}
117 	/*
118 	 * First, get our fully qualified host name, if possible.
119 	 */
120 	if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
121 		cp = strchr(myname, '.');
122 		if (cp != NULL && *(cp + 1) != '\0') {
123 			cp = myname;
124 		} else {
125 			/*
126 			 * No domain on myname, so try looking it up.
127 			 */
128 			cp = NULL;
129 			memset((void *)&hints, 0, sizeof (hints));
130 			hints.ai_flags = AI_CANONNAME;
131 			error = getaddrinfo(myname, NULL, &hints, &aip);
132 			if (error == 0) {
133 			    if (aip->ai_canonname != NULL &&
134 				(cp = strchr(aip->ai_canonname, '.')) != NULL
135 				&& *(cp + 1) != '\0') {
136 				    cp = aip->ai_canonname;
137 				    mustfreeai = 1;
138 			    } else {
139 				    freeaddrinfo(aip);
140 			    }
141 			}
142 		}
143 		if (cp == NULL)
144 			warnx("Can't get fully qualified host name");
145 		myfqdnname = cp;
146 	}
147 
148 	princname[0] = '\0';
149 #define	GETOPT	"p:P:"
150 #define	USAGE	"[ -p port_num ] [ -P client_principal ]"
151 	while ((ch = getopt(argc, argv, GETOPT)) != -1)
152 		switch (ch) {
153 		case 'p':
154 			myport = atoi(optarg);
155 			if (myport < 1) {
156 				warnx("port# non-positive, reset to %d",
157 				    NFSV4_CBPORT);
158 				myport = NFSV4_CBPORT;
159 			}
160 			break;
161 		case 'P':
162 			cp = optarg;
163 			if (cp != NULL && strlen(cp) > 0 &&
164 			    strlen(cp) < sizeof (princname)) {
165 				if (strchr(cp, '@') == NULL &&
166 				    myfqdnname != NULL)
167 					snprintf(princname, sizeof (princname),
168 					    "%s@%s", cp, myfqdnname);
169 				else
170 					strlcpy(princname, cp,
171 					    sizeof (princname));
172 			} else {
173 				warnx("client princ invalid. ignored\n");
174 			}
175 			break;
176 		default:
177 		case '?':
178 			usage();
179 		}
180 	argv += optind;
181 	argc -= optind;
182 
183 	if (argc > 0)
184 		usage();
185 
186 	if (mustfreeai)
187 		freeaddrinfo(aip);
188 	nfscbdargs2.principal = (const char *)princname;
189 	if (debug == 0) {
190 		daemon(0, 0);
191 		(void)signal(SIGTERM, SIG_IGN);
192 		(void)signal(SIGHUP, SIG_IGN);
193 		(void)signal(SIGINT, SIG_IGN);
194 		(void)signal(SIGQUIT, SIG_IGN);
195 	}
196 	(void)signal(SIGSYS, nonfs);
197 	(void)signal(SIGCHLD, reapchild);
198 
199 	openlog("nfscbd:", LOG_PID, LOG_DAEMON);
200 
201 	pid = fork();
202 	if (pid < 0) {
203 		syslog(LOG_ERR, "fork: %m");
204 		nfscbd_exit(1);
205 	} else if (pid > 0) {
206 		children = pid;
207 	} else {
208 		(void)signal(SIGUSR1, child_cleanup);
209 		setproctitle("server");
210 		nfssvc_flag = NFSSVC_NFSCBD;
211 		if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
212 			syslog(LOG_ERR, "nfssvc: %m");
213 			nfscbd_exit(1);
214 		}
215 		exit(0);
216 	}
217 	(void)signal(SIGUSR1, cleanup);
218 
219 	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
220 		syslog(LOG_ERR, "can't create udp socket");
221 		nfscbd_exit(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(myport);
227 	inetaddr.sin_len = sizeof(inetaddr);
228 	ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
229 	/* If bind() fails, this is a restart, so just skip UDP. */
230 	if (ret == 0) {
231 		len = sizeof(inetaddr);
232 		if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
233 			syslog(LOG_ERR, "can't get bound addr");
234 			nfscbd_exit(1);
235 		}
236 		nfscbdargs.port = ntohs(inetaddr.sin_port);
237 		if (nfscbdargs.port != myport) {
238 			syslog(LOG_ERR, "BAD PORT#");
239 			nfscbd_exit(1);
240 		}
241 		nfscbdargs.sock = sock;
242 		nfscbdargs.name = NULL;
243 		nfscbdargs.namelen = 0;
244 		if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
245 			syslog(LOG_ERR, "can't Add UDP socket");
246 			nfscbd_exit(1);
247 		}
248 	}
249 	(void)close(sock);
250 
251 	/* Now set up the master server socket waiting for tcp connections. */
252 	on = 1;
253 	FD_ZERO(&sockbits);
254 	connect_type_cnt = 0;
255 	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
256 		syslog(LOG_ERR, "can't create tcp socket");
257 		nfscbd_exit(1);
258 	}
259 	if (setsockopt(tcpsock,
260 	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
261 		syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
262 	/* sin_port is already set */
263 	inetaddr.sin_family = AF_INET;
264 	inetaddr.sin_addr.s_addr = INADDR_ANY;
265 	inetaddr.sin_port = htons(myport);
266 	inetaddr.sin_len = sizeof(inetaddr);
267 	if (bind(tcpsock,
268 	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
269 		syslog(LOG_ERR, "can't bind tcp addr");
270 		nfscbd_exit(1);
271 	}
272 	if (listen(tcpsock, 5) < 0) {
273 		syslog(LOG_ERR, "listen failed");
274 		nfscbd_exit(1);
275 	}
276 	FD_SET(tcpsock, &sockbits);
277 	maxsock = tcpsock;
278 	connect_type_cnt++;
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 		ready = sockbits;
288 		if (connect_type_cnt > 1) {
289 			if (select(maxsock + 1,
290 			    &ready, NULL, NULL, NULL) < 1) {
291 				syslog(LOG_ERR, "select failed: %m");
292 				nfscbd_exit(1);
293 			}
294 		}
295 		if (FD_ISSET(tcpsock, &ready)) {
296 			len = sizeof(inetpeer);
297 			if ((msgsock = accept(tcpsock,
298 			    (struct sockaddr *)&inetpeer, &len)) < 0) {
299 				syslog(LOG_ERR, "accept failed: %m");
300 				nfscbd_exit(1);
301 			}
302 			memset(inetpeer.sin_zero, 0,
303 			    sizeof (inetpeer.sin_zero));
304 			if (setsockopt(msgsock, SOL_SOCKET,
305 			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
306 				syslog(LOG_ERR,
307 				    "setsockopt SO_KEEPALIVE: %m");
308 			nfscbdargs.sock = msgsock;
309 			nfscbdargs.name = (caddr_t)&inetpeer;
310 			nfscbdargs.namelen = sizeof(inetpeer);
311 			nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
312 			(void)close(msgsock);
313 		}
314 	}
315 }
316 
317 static void
318 usage(void)
319 {
320 
321 	errx(1, "usage: nfscbd %s", USAGE);
322 }
323 
324 static void
325 nonfs(int signo __unused)
326 {
327 	syslog(LOG_ERR, "missing system call: NFS not available");
328 }
329 
330 static void
331 reapchild(int signo __unused)
332 {
333 	pid_t pid;
334 
335 	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
336 		if (pid == children)
337 			children = -1;
338 	}
339 }
340 
341 static void
342 killchildren(void)
343 {
344 
345 	if (children > 0)
346 		kill(children, SIGKILL);
347 }
348 
349 /*
350  * Cleanup master after SIGUSR1.
351  */
352 static void
353 cleanup(int signo __unused)
354 {
355 	nfscbd_exit(0);
356 }
357 
358 /*
359  * Cleanup child after SIGUSR1.
360  */
361 static void
362 child_cleanup(int signo __unused)
363 {
364 	exit(0);
365 }
366 
367 static void
368 nfscbd_exit(int status __unused)
369 {
370 	killchildren();
371 	exit(status);
372 }
373