nfscbd.c revision 192811
1192811Srmacklem/*-
2192811Srmacklem * Copyright (c) 2009 Rick Macklem, University of Guelph
3192811Srmacklem * All rights reserved.
4192811Srmacklem *
5192811Srmacklem * Redistribution and use in source and binary forms, with or without
6192811Srmacklem * modification, are permitted provided that the following conditions
7192811Srmacklem * are met:
8192811Srmacklem * 1. Redistributions of source code must retain the above copyright
9192811Srmacklem *    notice, this list of conditions and the following disclaimer.
10192811Srmacklem * 2. Redistributions in binary form must reproduce the above copyright
11192811Srmacklem *    notice, this list of conditions and the following disclaimer in the
12192811Srmacklem *    documentation and/or other materials provided with the distribution.
13192811Srmacklem *
14192811Srmacklem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15192811Srmacklem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16192811Srmacklem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17192811Srmacklem * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18192811Srmacklem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19192811Srmacklem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20192811Srmacklem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21192811Srmacklem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22192811Srmacklem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23192811Srmacklem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24192811Srmacklem * SUCH DAMAGE.
25192811Srmacklem *
26192811Srmacklem */
27192811Srmacklem
28192811Srmacklem#include <sys/cdefs.h>
29192811Srmacklem__FBSDID("$FreeBSD: head/usr.sbin/nfscbd/nfscbd.c 192811 2009-05-26 15:19:04Z rmacklem $");
30192811Srmacklem
31192811Srmacklem#include <sys/param.h>
32192811Srmacklem#include <sys/ioctl.h>
33192811Srmacklem#include <sys/linker.h>
34192811Srmacklem#include <sys/module.h>
35192811Srmacklem#include <sys/mount.h>
36192811Srmacklem#include <sys/socket.h>
37192811Srmacklem#include <sys/socketvar.h>
38192811Srmacklem#include <sys/stat.h>
39192811Srmacklem#include <sys/ucred.h>
40192811Srmacklem#include <sys/uio.h>
41192811Srmacklem#include <sys/vnode.h>
42192811Srmacklem#include <sys/wait.h>
43192811Srmacklem
44192811Srmacklem#include <nfs/nfssvc.h>
45192811Srmacklem
46192811Srmacklem#include <rpc/rpc.h>
47192811Srmacklem
48192811Srmacklem#include <fs/nfs/nfsproto.h>
49192811Srmacklem#include <fs/nfs/nfskpiport.h>
50192811Srmacklem#include <fs/nfs/nfs.h>
51192811Srmacklem#include <fs/nfs/rpcv2.h>
52192811Srmacklem
53192811Srmacklem#include <err.h>
54192811Srmacklem#include <errno.h>
55192811Srmacklem#include <fcntl.h>
56192811Srmacklem#include <grp.h>
57192811Srmacklem#include <netdb.h>
58192811Srmacklem#include <pwd.h>
59192811Srmacklem#include <signal.h>
60192811Srmacklem#include <stdio.h>
61192811Srmacklem#include <stdlib.h>
62192811Srmacklem#include <string.h>
63192811Srmacklem#include <syslog.h>
64192811Srmacklem#include <unistd.h>
65192811Srmacklem
66192811Srmacklem/* Global defs */
67192811Srmacklem#ifdef DEBUG
68192811Srmacklem#define	syslog(e, s)	fprintf(stderr,(s))
69192811Srmacklemint	debug = 1;
70192811Srmacklem#else
71192811Srmacklemint	debug = 0;
72192811Srmacklem#endif
73192811Srmacklem
74192811Srmacklempid_t children;
75192811Srmacklem
76192811Srmacklemvoid	nonfs(int);
77192811Srmacklemvoid	reapchild(int);
78192811Srmacklemvoid	usage(void);
79192811Srmacklemvoid	cleanup(int);
80192811Srmacklemvoid	child_cleanup(int);
81192811Srmacklemvoid	nfscbd_exit(int);
82192811Srmacklemvoid	killchildren(void);
83192811Srmacklem
84192811Srmacklem/*
85192811Srmacklem * Nfs callback server daemon.
86192811Srmacklem *
87192811Srmacklem * 1 - do file descriptor and signal cleanup
88192811Srmacklem * 2 - fork the nfscbd(s)
89192811Srmacklem * 4 - create callback server socket(s)
90192811Srmacklem * 5 - set up server socket for rpc
91192811Srmacklem *
92192811Srmacklem * For connectionless protocols, just pass the socket into the kernel via.
93192811Srmacklem * nfssvc().
94192811Srmacklem * For connection based sockets, loop doing accepts. When you get a new
95192811Srmacklem * socket from accept, pass the msgsock into the kernel via. nfssvc().
96192811Srmacklem */
97192811Srmacklemint
98192811Srmacklemmain(int argc, char *argv[], char **envp)
99192811Srmacklem{
100192811Srmacklem	struct group *grp;
101192811Srmacklem	struct nfscbd_args nfscbdargs;
102192811Srmacklem	struct nfsd_nfscbd_args nfscbdargs2;
103192811Srmacklem	struct passwd *pwd;
104192811Srmacklem	struct ucred *cr;
105192811Srmacklem	struct sockaddr_in inetaddr, inetpeer;
106192811Srmacklem	struct timeval ktv;
107192811Srmacklem	fd_set ready, sockbits;
108192811Srmacklem	int ch, connect_type_cnt, i, len, maxsock, msgsock, error;
109192811Srmacklem	int nfssvc_flag, on, sock, tcpsock, ret, mustfreeai = 0;
110192811Srmacklem	char *cp, **cpp, princname[128];
111192811Srmacklem	char myname[MAXHOSTNAMELEN], *myfqdnname = NULL;
112192811Srmacklem	struct addrinfo *aip, hints;
113192811Srmacklem	pid_t pid;
114192811Srmacklem	sigset_t signew;
115192811Srmacklem	short myport = NFSV4_CBPORT;
116192811Srmacklem
117192811Srmacklem	if (modfind("nfscl") < 0) {
118192811Srmacklem		/* Not present in kernel, try loading it */
119192811Srmacklem		if (kldload("nfscl") < 0 ||
120192811Srmacklem		    modfind("nfscl") < 0)
121192811Srmacklem			errx(1, "nfscl is not available");
122192811Srmacklem	}
123192811Srmacklem	/*
124192811Srmacklem	 * First, get our fully qualified host name, if possible.
125192811Srmacklem	 */
126192811Srmacklem	if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
127192811Srmacklem		cp = strchr(myname, '.');
128192811Srmacklem		if (cp != NULL && *(cp + 1) != '\0') {
129192811Srmacklem			cp = myname;
130192811Srmacklem		} else {
131192811Srmacklem			/*
132192811Srmacklem			 * No domain on myname, so try looking it up.
133192811Srmacklem			 */
134192811Srmacklem			cp = NULL;
135192811Srmacklem			memset((void *)&hints, 0, sizeof (hints));
136192811Srmacklem			hints.ai_flags = AI_CANONNAME;
137192811Srmacklem			error = getaddrinfo(myname, NULL, &hints, &aip);
138192811Srmacklem			if (error == 0) {
139192811Srmacklem			    if (aip->ai_canonname != NULL &&
140192811Srmacklem				(cp = strchr(aip->ai_canonname, '.')) != NULL
141192811Srmacklem				&& *(cp + 1) != '\0') {
142192811Srmacklem				    cp = aip->ai_canonname;
143192811Srmacklem				    mustfreeai = 1;
144192811Srmacklem			    } else {
145192811Srmacklem				    freeaddrinfo(aip);
146192811Srmacklem			    }
147192811Srmacklem			}
148192811Srmacklem		}
149192811Srmacklem		if (cp == NULL)
150192811Srmacklem			warnx("Can't get fully qualified host name");
151192811Srmacklem		myfqdnname = cp;
152192811Srmacklem	}
153192811Srmacklem
154192811Srmacklem	princname[0] = '\0';
155192811Srmacklem#define	GETOPT	"p:P:"
156192811Srmacklem#define	USAGE	"[ -p port_num ] [ -P client_principal ]"
157192811Srmacklem	while ((ch = getopt(argc, argv, GETOPT)) != -1)
158192811Srmacklem		switch (ch) {
159192811Srmacklem		case 'p':
160192811Srmacklem			myport = atoi(optarg);
161192811Srmacklem			if (myport < 1) {
162192811Srmacklem				warnx("port# non-positive, reset to %d",
163192811Srmacklem				    NFSV4_CBPORT);
164192811Srmacklem				myport = NFSV4_CBPORT;
165192811Srmacklem			}
166192811Srmacklem			break;
167192811Srmacklem		case 'P':
168192811Srmacklem			cp = optarg;
169192811Srmacklem			if (cp != NULL && strlen(cp) > 0 &&
170192811Srmacklem			    strlen(cp) < sizeof (princname)) {
171192811Srmacklem				if (strchr(cp, '@') == NULL &&
172192811Srmacklem				    myfqdnname != NULL)
173192811Srmacklem					snprintf(princname, sizeof (princname),
174192811Srmacklem					    "%s@%s", cp, myfqdnname);
175192811Srmacklem				else
176192811Srmacklem					strlcpy(princname, cp,
177192811Srmacklem					    sizeof (princname));
178192811Srmacklem			} else {
179192811Srmacklem				warnx("client princ invalid. ignored\n");
180192811Srmacklem			}
181192811Srmacklem			break;
182192811Srmacklem		default:
183192811Srmacklem		case '?':
184192811Srmacklem			usage();
185192811Srmacklem		};
186192811Srmacklem	argv += optind;
187192811Srmacklem	argc -= optind;
188192811Srmacklem
189192811Srmacklem	if (argc > 0)
190192811Srmacklem		usage();
191192811Srmacklem
192192811Srmacklem	if (mustfreeai)
193192811Srmacklem		freeaddrinfo(aip);
194192811Srmacklem	nfscbdargs2.principal = (const char *)princname;
195192811Srmacklem	if (debug == 0) {
196192811Srmacklem		daemon(0, 0);
197192811Srmacklem		(void)signal(SIGTERM, SIG_IGN);
198192811Srmacklem		(void)signal(SIGHUP, SIG_IGN);
199192811Srmacklem		(void)signal(SIGINT, SIG_IGN);
200192811Srmacklem		(void)signal(SIGQUIT, SIG_IGN);
201192811Srmacklem	}
202192811Srmacklem	(void)signal(SIGSYS, nonfs);
203192811Srmacklem	(void)signal(SIGCHLD, reapchild);
204192811Srmacklem
205192811Srmacklem	openlog("nfscbd:", LOG_PID, LOG_DAEMON);
206192811Srmacklem
207192811Srmacklem	pid = fork();
208192811Srmacklem	if (pid < 0) {
209192811Srmacklem		syslog(LOG_ERR, "fork: %m");
210192811Srmacklem		nfscbd_exit(1);
211192811Srmacklem	} else if (pid > 0) {
212192811Srmacklem		children = pid;
213192811Srmacklem	} else {
214192811Srmacklem		(void)signal(SIGUSR1, child_cleanup);
215192811Srmacklem		setproctitle("server");
216192811Srmacklem		nfssvc_flag = NFSSVC_NFSCBD;
217192811Srmacklem		if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
218192811Srmacklem			syslog(LOG_ERR, "nfssvc: %m");
219192811Srmacklem			nfscbd_exit(1);
220192811Srmacklem		}
221192811Srmacklem		exit(0);
222192811Srmacklem	}
223192811Srmacklem	(void)signal(SIGUSR1, cleanup);
224192811Srmacklem
225192811Srmacklem	if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
226192811Srmacklem		syslog(LOG_ERR, "can't create udp socket");
227192811Srmacklem		nfscbd_exit(1);
228192811Srmacklem	}
229192811Srmacklem	memset(&inetaddr, 0, sizeof inetaddr);
230192811Srmacklem	inetaddr.sin_family = AF_INET;
231192811Srmacklem	inetaddr.sin_addr.s_addr = INADDR_ANY;
232192811Srmacklem	inetaddr.sin_port = htons(myport);
233192811Srmacklem	inetaddr.sin_len = sizeof(inetaddr);
234192811Srmacklem	ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
235192811Srmacklem	/* If bind() fails, this is a restart, so just skip UDP. */
236192811Srmacklem	if (ret == 0) {
237192811Srmacklem		len = sizeof(inetaddr);
238192811Srmacklem		if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
239192811Srmacklem			syslog(LOG_ERR, "can't get bound addr");
240192811Srmacklem			nfscbd_exit(1);
241192811Srmacklem		}
242192811Srmacklem		nfscbdargs.port = ntohs(inetaddr.sin_port);
243192811Srmacklem		if (nfscbdargs.port != myport) {
244192811Srmacklem			syslog(LOG_ERR, "BAD PORT#");
245192811Srmacklem			nfscbd_exit(1);
246192811Srmacklem		}
247192811Srmacklem		nfscbdargs.sock = sock;
248192811Srmacklem		nfscbdargs.name = NULL;
249192811Srmacklem		nfscbdargs.namelen = 0;
250192811Srmacklem		if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
251192811Srmacklem			syslog(LOG_ERR, "can't Add UDP socket");
252192811Srmacklem			nfscbd_exit(1);
253192811Srmacklem		}
254192811Srmacklem	}
255192811Srmacklem	(void)close(sock);
256192811Srmacklem
257192811Srmacklem	/* Now set up the master server socket waiting for tcp connections. */
258192811Srmacklem	on = 1;
259192811Srmacklem	FD_ZERO(&sockbits);
260192811Srmacklem	connect_type_cnt = 0;
261192811Srmacklem	if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
262192811Srmacklem		syslog(LOG_ERR, "can't create tcp socket");
263192811Srmacklem		nfscbd_exit(1);
264192811Srmacklem	}
265192811Srmacklem	if (setsockopt(tcpsock,
266192811Srmacklem	    SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
267192811Srmacklem		syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
268192811Srmacklem	/* sin_port is already set */
269192811Srmacklem	inetaddr.sin_family = AF_INET;
270192811Srmacklem	inetaddr.sin_addr.s_addr = INADDR_ANY;
271192811Srmacklem	inetaddr.sin_port = htons(myport);
272192811Srmacklem	inetaddr.sin_len = sizeof(inetaddr);
273192811Srmacklem	if (bind(tcpsock,
274192811Srmacklem	    (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
275192811Srmacklem		syslog(LOG_ERR, "can't bind tcp addr");
276192811Srmacklem		nfscbd_exit(1);
277192811Srmacklem	}
278192811Srmacklem	if (listen(tcpsock, 5) < 0) {
279192811Srmacklem		syslog(LOG_ERR, "listen failed");
280192811Srmacklem		nfscbd_exit(1);
281192811Srmacklem	}
282192811Srmacklem	FD_SET(tcpsock, &sockbits);
283192811Srmacklem	maxsock = tcpsock;
284192811Srmacklem	connect_type_cnt++;
285192811Srmacklem
286192811Srmacklem	setproctitle("master");
287192811Srmacklem
288192811Srmacklem	/*
289192811Srmacklem	 * Loop forever accepting connections and passing the sockets
290192811Srmacklem	 * into the kernel for the mounts.
291192811Srmacklem	 */
292192811Srmacklem	for (;;) {
293192811Srmacklem		ready = sockbits;
294192811Srmacklem		if (connect_type_cnt > 1) {
295192811Srmacklem			if (select(maxsock + 1,
296192811Srmacklem			    &ready, NULL, NULL, NULL) < 1) {
297192811Srmacklem				syslog(LOG_ERR, "select failed: %m");
298192811Srmacklem				nfscbd_exit(1);
299192811Srmacklem			}
300192811Srmacklem		}
301192811Srmacklem		if (FD_ISSET(tcpsock, &ready)) {
302192811Srmacklem			len = sizeof(inetpeer);
303192811Srmacklem			if ((msgsock = accept(tcpsock,
304192811Srmacklem			    (struct sockaddr *)&inetpeer, &len)) < 0) {
305192811Srmacklem				syslog(LOG_ERR, "accept failed: %m");
306192811Srmacklem				nfscbd_exit(1);
307192811Srmacklem			}
308192811Srmacklem			memset(inetpeer.sin_zero, 0,
309192811Srmacklem			    sizeof (inetpeer.sin_zero));
310192811Srmacklem			if (setsockopt(msgsock, SOL_SOCKET,
311192811Srmacklem			    SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
312192811Srmacklem				syslog(LOG_ERR,
313192811Srmacklem				    "setsockopt SO_KEEPALIVE: %m");
314192811Srmacklem			nfscbdargs.sock = msgsock;
315192811Srmacklem			nfscbdargs.name = (caddr_t)&inetpeer;
316192811Srmacklem			nfscbdargs.namelen = sizeof(inetpeer);
317192811Srmacklem			nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
318192811Srmacklem			(void)close(msgsock);
319192811Srmacklem		}
320192811Srmacklem	}
321192811Srmacklem}
322192811Srmacklem
323192811Srmacklemvoid
324192811Srmacklemusage(void)
325192811Srmacklem{
326192811Srmacklem
327192811Srmacklem	errx(1, "usage: nfscbd %s", USAGE);
328192811Srmacklem}
329192811Srmacklem
330192811Srmacklemvoid
331192811Srmacklemnonfs(int signo)
332192811Srmacklem{
333192811Srmacklem	syslog(LOG_ERR, "missing system call: NFS not available");
334192811Srmacklem}
335192811Srmacklem
336192811Srmacklemvoid
337192811Srmacklemreapchild(int signo)
338192811Srmacklem{
339192811Srmacklem	pid_t pid;
340192811Srmacklem	int i;
341192811Srmacklem
342192811Srmacklem	while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
343192811Srmacklem		if (pid == children)
344192811Srmacklem			children = -1;
345192811Srmacklem	}
346192811Srmacklem}
347192811Srmacklem
348192811Srmacklemvoid
349192811Srmacklemkillchildren(void)
350192811Srmacklem{
351192811Srmacklem	int i;
352192811Srmacklem
353192811Srmacklem	if (children > 0)
354192811Srmacklem		kill(children, SIGKILL);
355192811Srmacklem}
356192811Srmacklem
357192811Srmacklem/*
358192811Srmacklem * Cleanup master after SIGUSR1.
359192811Srmacklem */
360192811Srmacklemvoid
361192811Srmacklemcleanup(int signo)
362192811Srmacklem{
363192811Srmacklem	nfscbd_exit(0);
364192811Srmacklem}
365192811Srmacklem
366192811Srmacklem/*
367192811Srmacklem * Cleanup child after SIGUSR1.
368192811Srmacklem */
369192811Srmacklemvoid
370192811Srmacklemchild_cleanup(int signo)
371192811Srmacklem{
372192811Srmacklem	exit(0);
373192811Srmacklem}
374192811Srmacklem
375192811Srmacklemvoid
376192811Srmacklemnfscbd_exit(int status)
377192811Srmacklem{
378192811Srmacklem	killchildren();
379192811Srmacklem	exit(status);
380192811Srmacklem}
381