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