1 /* $OpenBSD: nfsd.c,v 1.31 2010/04/17 16:27:49 krw Exp $ */ 2 /* $NetBSD: nfsd.c,v 1.19 1996/02/18 23:18:56 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 1989, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/param.h> 37 #include <sys/ioctl.h> 38 #include <sys/stat.h> 39 #include <sys/wait.h> 40 #include <sys/uio.h> 41 #include <sys/ucred.h> 42 #include <sys/mount.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 46 #include <rpc/rpc.h> 47 #include <rpc/pmap_clnt.h> 48 #include <rpc/pmap_prot.h> 49 50 #include <nfs/rpcv2.h> 51 #include <nfs/nfsproto.h> 52 #include <nfs/nfs.h> 53 54 #include <err.h> 55 #include <errno.h> 56 #include <fcntl.h> 57 #include <grp.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 int debug = 1; 70 #else 71 int debug = 0; 72 #endif 73 74 struct nfsd_srvargs nsd; 75 76 void nonfs(int); 77 void reapchild(int); 78 void usage(void); 79 80 #define MAXNFSDCNT 20 81 #define DEFNFSDCNT 4 82 83 /* 84 * Nfs server daemon mostly just a user context for nfssvc() 85 * 86 * 1 - do file descriptor and signal cleanup 87 * 2 - fork the nfsd(s) 88 * 3 - create server socket(s) 89 * 4 - register socket with portmap 90 * 91 * For connectionless protocols, just pass the socket into the kernel via. 92 * nfssvc(). 93 * For connection based sockets, loop doing accepts. When you get a new 94 * socket from accept, pass the msgsock into the kernel via. nfssvc(). 95 * The arguments are: 96 * -r - reregister with portmapper 97 * -t - support tcp nfs clients 98 * -u - support udp nfs clients 99 * followed by "n" which is the number of nfsds' to fork off 100 */ 101 int 102 main(int argc, char *argv[]) 103 { 104 struct nfsd_args nfsdargs; 105 struct sockaddr_in inetaddr, inetpeer; 106 fd_set *ready, *sockbits; 107 size_t fd_size; 108 int ch, connect_type_cnt, i, maxsock = 0, msgsock; 109 int nfsdcnt = DEFNFSDCNT, on, reregister = 0, sock; 110 int udpflag = 0, tcpflag = 0, tcpsock; 111 const char *errstr = NULL; 112 socklen_t len; 113 114 /* Start by writing to both console and log. */ 115 openlog("nfsd", LOG_PID | LOG_PERROR, LOG_DAEMON); 116 117 if (argc == 1) 118 udpflag = 1; 119 while ((ch = getopt(argc, argv, "n:rtu")) != -1) 120 switch (ch) { 121 case 'n': 122 nfsdcnt = strtonum(optarg, 1, MAXNFSDCNT, &errstr); 123 if (errstr) { 124 syslog(LOG_ERR, "nfsd count is %s: %s", errstr, optarg); 125 return(1); 126 } 127 break; 128 case 'r': 129 reregister = 1; 130 break; 131 case 't': 132 tcpflag = 1; 133 break; 134 case 'u': 135 udpflag = 1; 136 break; 137 default: 138 usage(); 139 }; 140 argv += optind; 141 argc -= optind; 142 143 /* 144 * XXX 145 * Backward compatibility, trailing number is the count of daemons. 146 */ 147 if (argc > 1) 148 usage(); 149 if (argc == 1) { 150 nfsdcnt = strtonum(argv[0], 1, MAXNFSDCNT, &errstr); 151 if (errstr) { 152 syslog(LOG_ERR, "nfsd count is %s: %s", errstr, optarg); 153 return(1); 154 } 155 } 156 157 if (debug == 0) { 158 daemon(0, 0); 159 (void)signal(SIGHUP, SIG_IGN); 160 (void)signal(SIGINT, SIG_IGN); 161 (void)signal(SIGQUIT, SIG_IGN); 162 (void)signal(SIGSYS, nonfs); 163 } 164 (void)signal(SIGCHLD, reapchild); 165 166 if (reregister) { 167 if (udpflag && 168 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) || 169 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT))) { 170 syslog(LOG_ERR, "can't register with portmap for UDP (%m)."); 171 return (1); 172 } 173 if (tcpflag && 174 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) || 175 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT))) { 176 syslog(LOG_ERR, "can't register with portmap for TCP (%m)."); 177 return (1); 178 } 179 return (0); 180 } 181 182 /* Cut back to writing to log only. */ 183 closelog(); 184 openlog("nfsd", LOG_PID, LOG_DAEMON); 185 186 for (i = 0; i < nfsdcnt; i++) { 187 switch (fork()) { 188 case -1: 189 syslog(LOG_ERR, "fork: %m"); 190 return (1); 191 case 0: 192 break; 193 default: 194 continue; 195 } 196 197 setproctitle("server"); 198 nsd.nsd_nfsd = NULL; 199 if (nfssvc(NFSSVC_NFSD, &nsd) < 0) { 200 syslog(LOG_ERR, "nfssvc: %m"); 201 return (1); 202 } 203 return (0); 204 } 205 206 /* If we are serving udp, set up the socket. */ 207 if (udpflag) { 208 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 209 syslog(LOG_ERR, "can't create udp socket"); 210 return (1); 211 } 212 memset(&inetaddr, 0, sizeof inetaddr); 213 inetaddr.sin_family = AF_INET; 214 inetaddr.sin_addr.s_addr = INADDR_ANY; 215 inetaddr.sin_port = htons(NFS_PORT); 216 inetaddr.sin_len = sizeof(inetaddr); 217 if (bind(sock, (struct sockaddr *)&inetaddr, 218 sizeof(inetaddr)) < 0) { 219 syslog(LOG_ERR, "can't bind udp addr"); 220 return (1); 221 } 222 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) || 223 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) { 224 syslog(LOG_ERR, "can't register with udp portmap"); 225 return (1); 226 } 227 nfsdargs.sock = sock; 228 nfsdargs.name = NULL; 229 nfsdargs.namelen = 0; 230 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) { 231 syslog(LOG_ERR, "can't Add UDP socket"); 232 return (1); 233 } 234 (void)close(sock); 235 } 236 237 /* Now set up the master server socket waiting for tcp connections. */ 238 on = 1; 239 connect_type_cnt = 0; 240 if (tcpflag) { 241 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 242 syslog(LOG_ERR, "can't create tcp socket"); 243 return (1); 244 } 245 if (setsockopt(tcpsock, 246 SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 247 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m"); 248 memset(&inetaddr, 0, sizeof inetaddr); 249 inetaddr.sin_family = AF_INET; 250 inetaddr.sin_addr.s_addr = INADDR_ANY; 251 inetaddr.sin_port = htons(NFS_PORT); 252 inetaddr.sin_len = sizeof(inetaddr); 253 if (bind(tcpsock, (struct sockaddr *)&inetaddr, 254 sizeof (inetaddr)) < 0) { 255 syslog(LOG_ERR, "can't bind tcp addr"); 256 return (1); 257 } 258 if (listen(tcpsock, 5) < 0) { 259 syslog(LOG_ERR, "listen failed"); 260 return (1); 261 } 262 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) || 263 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) { 264 syslog(LOG_ERR, "can't register tcp with portmap"); 265 return (1); 266 } 267 maxsock = tcpsock; 268 connect_type_cnt++; 269 } 270 271 if (connect_type_cnt == 0) 272 return (0); 273 274 setproctitle("master"); 275 276 /* 277 * Allocate space for the fd_set pointers and fill in sockbits 278 */ 279 fd_size = howmany(maxsock + 1, NFDBITS) * sizeof(fd_mask); 280 sockbits = malloc(fd_size); 281 ready = malloc(fd_size); 282 if (sockbits == NULL || ready == NULL) { 283 syslog(LOG_ERR, "cannot allocate memory"); 284 return (1); 285 } 286 memset(sockbits, 0, fd_size); 287 if (tcpflag) 288 FD_SET(tcpsock, sockbits); 289 290 /* 291 * Loop forever accepting connections and passing the sockets 292 * into the kernel for the mounts. 293 */ 294 for (;;) { 295 memcpy(ready, sockbits, fd_size); 296 if (connect_type_cnt > 1) { 297 if (select(maxsock + 1, 298 ready, NULL, NULL, NULL) < 1) { 299 syslog(LOG_ERR, "select failed: %m"); 300 return (1); 301 } 302 } 303 if (tcpflag && FD_ISSET(tcpsock, ready)) { 304 len = sizeof(inetpeer); 305 if ((msgsock = accept(tcpsock, 306 (struct sockaddr *)&inetpeer, &len)) < 0) { 307 syslog(LOG_ERR, "accept failed: %m"); 308 return (1); 309 } 310 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero)); 311 if (setsockopt(msgsock, SOL_SOCKET, 312 SO_KEEPALIVE, &on, sizeof(on)) < 0) 313 syslog(LOG_ERR, 314 "setsockopt SO_KEEPALIVE: %m"); 315 nfsdargs.sock = msgsock; 316 nfsdargs.name = (caddr_t)&inetpeer; 317 nfsdargs.namelen = sizeof(inetpeer); 318 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) { 319 syslog(LOG_ERR, "can't Add TCP socket"); 320 return (1); 321 } 322 (void)close(msgsock); 323 } 324 } 325 } 326 327 void 328 usage(void) 329 { 330 (void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n"); 331 exit(1); 332 } 333 334 /* ARGSUSED */ 335 void 336 nonfs(int signo) 337 { 338 int save_errno = errno; 339 struct syslog_data sdata = SYSLOG_DATA_INIT; 340 341 syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available."); 342 errno = save_errno; 343 } 344 345 /* ARGSUSED */ 346 void 347 reapchild(int signo) 348 { 349 int save_errno = errno; 350 351 while (wait3(NULL, WNOHANG, NULL) > 0) 352 ; 353 errno = save_errno; 354 } 355