]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/nfscbd/nfscbd.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / nfscbd / nfscbd.c
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, len, 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
112         if (modfind("nfscl") < 0) {
113                 /* Not present in kernel, try loading it */
114                 if (kldload("nfscl") < 0 ||
115                     modfind("nfscl") < 0)
116                         errx(1, "nfscl is not available");
117         }
118         /*
119          * First, get our fully qualified host name, if possible.
120          */
121         if (gethostname(myname, MAXHOSTNAMELEN) >= 0) {
122                 cp = strchr(myname, '.');
123                 if (cp != NULL && *(cp + 1) != '\0') {
124                         cp = myname;
125                 } else {
126                         /*
127                          * No domain on myname, so try looking it up.
128                          */
129                         cp = NULL;
130                         memset((void *)&hints, 0, sizeof (hints));
131                         hints.ai_flags = AI_CANONNAME;
132                         error = getaddrinfo(myname, NULL, &hints, &aip);
133                         if (error == 0) {
134                             if (aip->ai_canonname != NULL &&
135                                 (cp = strchr(aip->ai_canonname, '.')) != NULL
136                                 && *(cp + 1) != '\0') {
137                                     cp = aip->ai_canonname;
138                                     mustfreeai = 1;
139                             } else {
140                                     freeaddrinfo(aip);
141                             }
142                         }
143                 }
144                 if (cp == NULL)
145                         warnx("Can't get fully qualified host name");
146                 myfqdnname = cp;
147         }
148
149         princname[0] = '\0';
150 #define GETOPT  "p:P:"
151 #define USAGE   "[ -p port_num ] [ -P client_principal ]"
152         while ((ch = getopt(argc, argv, GETOPT)) != -1)
153                 switch (ch) {
154                 case 'p':
155                         myport = atoi(optarg);
156                         if (myport < 1) {
157                                 warnx("port# non-positive, reset to %d",
158                                     NFSV4_CBPORT);
159                                 myport = NFSV4_CBPORT;
160                         }
161                         break;
162                 case 'P':
163                         cp = optarg;
164                         if (cp != NULL && strlen(cp) > 0 &&
165                             strlen(cp) < sizeof (princname)) {
166                                 if (strchr(cp, '@') == NULL &&
167                                     myfqdnname != NULL)
168                                         snprintf(princname, sizeof (princname),
169                                             "%s@%s", cp, myfqdnname);
170                                 else
171                                         strlcpy(princname, cp,
172                                             sizeof (princname));
173                         } else {
174                                 warnx("client princ invalid. ignored\n");
175                         }
176                         break;
177                 default:
178                 case '?':
179                         usage();
180                 };
181         argv += optind;
182         argc -= optind;
183
184         if (argc > 0)
185                 usage();
186
187         if (mustfreeai)
188                 freeaddrinfo(aip);
189         nfscbdargs2.principal = (const char *)princname;
190         if (debug == 0) {
191                 daemon(0, 0);
192                 (void)signal(SIGTERM, SIG_IGN);
193                 (void)signal(SIGHUP, SIG_IGN);
194                 (void)signal(SIGINT, SIG_IGN);
195                 (void)signal(SIGQUIT, SIG_IGN);
196         }
197         (void)signal(SIGSYS, nonfs);
198         (void)signal(SIGCHLD, reapchild);
199
200         openlog("nfscbd:", LOG_PID, LOG_DAEMON);
201
202         pid = fork();
203         if (pid < 0) {
204                 syslog(LOG_ERR, "fork: %m");
205                 nfscbd_exit(1);
206         } else if (pid > 0) {
207                 children = pid;
208         } else {
209                 (void)signal(SIGUSR1, child_cleanup);
210                 setproctitle("server");
211                 nfssvc_flag = NFSSVC_NFSCBD;
212                 if (nfssvc(nfssvc_flag, &nfscbdargs2) < 0) {
213                         syslog(LOG_ERR, "nfssvc: %m");
214                         nfscbd_exit(1);
215                 }
216                 exit(0);
217         }
218         (void)signal(SIGUSR1, cleanup);
219
220         if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
221                 syslog(LOG_ERR, "can't create udp socket");
222                 nfscbd_exit(1);
223         }
224         memset(&inetaddr, 0, sizeof inetaddr);
225         inetaddr.sin_family = AF_INET;
226         inetaddr.sin_addr.s_addr = INADDR_ANY;
227         inetaddr.sin_port = htons(myport);
228         inetaddr.sin_len = sizeof(inetaddr);
229         ret = bind(sock, (struct sockaddr *)&inetaddr, sizeof(inetaddr));
230         /* If bind() fails, this is a restart, so just skip UDP. */
231         if (ret == 0) {
232                 len = sizeof(inetaddr);
233                 if (getsockname(sock, (struct sockaddr *)&inetaddr, &len) < 0){
234                         syslog(LOG_ERR, "can't get bound addr");
235                         nfscbd_exit(1);
236                 }
237                 nfscbdargs.port = ntohs(inetaddr.sin_port);
238                 if (nfscbdargs.port != myport) {
239                         syslog(LOG_ERR, "BAD PORT#");
240                         nfscbd_exit(1);
241                 }
242                 nfscbdargs.sock = sock;
243                 nfscbdargs.name = NULL;
244                 nfscbdargs.namelen = 0;
245                 if (nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs) < 0) {
246                         syslog(LOG_ERR, "can't Add UDP socket");
247                         nfscbd_exit(1);
248                 }
249         }
250         (void)close(sock);
251
252         /* Now set up the master server socket waiting for tcp connections. */
253         on = 1;
254         FD_ZERO(&sockbits);
255         connect_type_cnt = 0;
256         if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
257                 syslog(LOG_ERR, "can't create tcp socket");
258                 nfscbd_exit(1);
259         }
260         if (setsockopt(tcpsock,
261             SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0)
262                 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m");
263         /* sin_port is already set */
264         inetaddr.sin_family = AF_INET;
265         inetaddr.sin_addr.s_addr = INADDR_ANY;
266         inetaddr.sin_port = htons(myport);
267         inetaddr.sin_len = sizeof(inetaddr);
268         if (bind(tcpsock,
269             (struct sockaddr *)&inetaddr, sizeof (inetaddr)) < 0) {
270                 syslog(LOG_ERR, "can't bind tcp addr");
271                 nfscbd_exit(1);
272         }
273         if (listen(tcpsock, 5) < 0) {
274                 syslog(LOG_ERR, "listen failed");
275                 nfscbd_exit(1);
276         }
277         FD_SET(tcpsock, &sockbits);
278         maxsock = tcpsock;
279         connect_type_cnt++;
280
281         setproctitle("master");
282
283         /*
284          * Loop forever accepting connections and passing the sockets
285          * into the kernel for the mounts.
286          */
287         for (;;) {
288                 ready = sockbits;
289                 if (connect_type_cnt > 1) {
290                         if (select(maxsock + 1,
291                             &ready, NULL, NULL, NULL) < 1) {
292                                 syslog(LOG_ERR, "select failed: %m");
293                                 nfscbd_exit(1);
294                         }
295                 }
296                 if (FD_ISSET(tcpsock, &ready)) {
297                         len = sizeof(inetpeer);
298                         if ((msgsock = accept(tcpsock,
299                             (struct sockaddr *)&inetpeer, &len)) < 0) {
300                                 syslog(LOG_ERR, "accept failed: %m");
301                                 nfscbd_exit(1);
302                         }
303                         memset(inetpeer.sin_zero, 0,
304                             sizeof (inetpeer.sin_zero));
305                         if (setsockopt(msgsock, SOL_SOCKET,
306                             SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
307                                 syslog(LOG_ERR,
308                                     "setsockopt SO_KEEPALIVE: %m");
309                         nfscbdargs.sock = msgsock;
310                         nfscbdargs.name = (caddr_t)&inetpeer;
311                         nfscbdargs.namelen = sizeof(inetpeer);
312                         nfssvc(NFSSVC_CBADDSOCK, &nfscbdargs);
313                         (void)close(msgsock);
314                 }
315         }
316 }
317
318 static void
319 usage(void)
320 {
321
322         errx(1, "usage: nfscbd %s", USAGE);
323 }
324
325 static void
326 nonfs(int signo __unused)
327 {
328         syslog(LOG_ERR, "missing system call: NFS not available");
329 }
330
331 static void
332 reapchild(int signo __unused)
333 {
334         pid_t pid;
335
336         while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) {
337                 if (pid == children)
338                         children = -1;
339         }
340 }
341
342 static void
343 killchildren(void)
344 {
345
346         if (children > 0)
347                 kill(children, SIGKILL);
348 }
349
350 /*
351  * Cleanup master after SIGUSR1.
352  */
353 static void
354 cleanup(int signo __unused)
355 {
356         nfscbd_exit(0);
357 }
358
359 /*
360  * Cleanup child after SIGUSR1.
361  */
362 static void
363 child_cleanup(int signo __unused)
364 {
365         exit(0);
366 }
367
368 static void
369 nfscbd_exit(int status __unused)
370 {
371         killchildren();
372         exit(status);
373 }