1 /* $OpenBSD: nfsd.c,v 1.30 2010/04/14 23:54:36 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 if (argc == 1) 115 udpflag = 1; 116 while ((ch = getopt(argc, argv, "n:rtu")) != -1) 117 switch (ch) { 118 case 'n': 119 nfsdcnt = strtonum(optarg, 1, MAXNFSDCNT, &errstr); 120 if (errstr) 121 errx(1, "nfsd count is %s: %s", errstr, optarg); 122 break; 123 case 'r': 124 reregister = 1; 125 break; 126 case 't': 127 tcpflag = 1; 128 break; 129 case 'u': 130 udpflag = 1; 131 break; 132 default: 133 usage(); 134 }; 135 argv += optind; 136 argc -= optind; 137 138 /* 139 * XXX 140 * Backward compatibility, trailing number is the count of daemons. 141 */ 142 if (argc > 1) 143 usage(); 144 if (argc == 1) { 145 nfsdcnt = strtonum(argv[0], 1, MAXNFSDCNT, &errstr); 146 if (errstr) 147 errx(1, "nfsd count is %s: %s", errstr, argv[0]); 148 } 149 150 if (debug == 0) { 151 daemon(0, 0); 152 (void)signal(SIGHUP, SIG_IGN); 153 (void)signal(SIGINT, SIG_IGN); 154 (void)signal(SIGQUIT, SIG_IGN); 155 (void)signal(SIGSYS, nonfs); 156 } 157 (void)signal(SIGCHLD, reapchild); 158 159 if (reregister) { 160 if (udpflag && 161 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) || 162 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT))) 163 err(1, "can't register with portmap for UDP."); 164 if (tcpflag && 165 (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) || 166 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT))) 167 err(1, "can't register with portmap for TCP."); 168 return (0); 169 } 170 openlog("nfsd", LOG_PID, LOG_DAEMON); 171 172 for (i = 0; i < nfsdcnt; i++) { 173 switch (fork()) { 174 case -1: 175 syslog(LOG_ERR, "fork: %m"); 176 return (1); 177 case 0: 178 break; 179 default: 180 continue; 181 } 182 183 setproctitle("server"); 184 nsd.nsd_nfsd = NULL; 185 if (nfssvc(NFSSVC_NFSD, &nsd) < 0) { 186 syslog(LOG_ERR, "nfssvc: %m"); 187 return (1); 188 } 189 return (0); 190 } 191 192 /* If we are serving udp, set up the socket. */ 193 if (udpflag) { 194 if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 195 syslog(LOG_ERR, "can't create udp socket"); 196 return (1); 197 } 198 memset(&inetaddr, 0, sizeof inetaddr); 199 inetaddr.sin_family = AF_INET; 200 inetaddr.sin_addr.s_addr = INADDR_ANY; 201 inetaddr.sin_port = htons(NFS_PORT); 202 inetaddr.sin_len = sizeof(inetaddr); 203 if (bind(sock, (struct sockaddr *)&inetaddr, 204 sizeof(inetaddr)) < 0) { 205 syslog(LOG_ERR, "can't bind udp addr"); 206 return (1); 207 } 208 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_UDP, NFS_PORT) || 209 !pmap_set(RPCPROG_NFS, 3, IPPROTO_UDP, NFS_PORT)) { 210 syslog(LOG_ERR, "can't register with udp portmap"); 211 return (1); 212 } 213 nfsdargs.sock = sock; 214 nfsdargs.name = NULL; 215 nfsdargs.namelen = 0; 216 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) { 217 syslog(LOG_ERR, "can't Add UDP socket"); 218 return (1); 219 } 220 (void)close(sock); 221 } 222 223 /* Now set up the master server socket waiting for tcp connections. */ 224 on = 1; 225 connect_type_cnt = 0; 226 if (tcpflag) { 227 if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 228 syslog(LOG_ERR, "can't create tcp socket"); 229 return (1); 230 } 231 if (setsockopt(tcpsock, 232 SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) 233 syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m"); 234 memset(&inetaddr, 0, sizeof inetaddr); 235 inetaddr.sin_family = AF_INET; 236 inetaddr.sin_addr.s_addr = INADDR_ANY; 237 inetaddr.sin_port = htons(NFS_PORT); 238 inetaddr.sin_len = sizeof(inetaddr); 239 if (bind(tcpsock, (struct sockaddr *)&inetaddr, 240 sizeof (inetaddr)) < 0) { 241 syslog(LOG_ERR, "can't bind tcp addr"); 242 return (1); 243 } 244 if (listen(tcpsock, 5) < 0) { 245 syslog(LOG_ERR, "listen failed"); 246 return (1); 247 } 248 if (!pmap_set(RPCPROG_NFS, 2, IPPROTO_TCP, NFS_PORT) || 249 !pmap_set(RPCPROG_NFS, 3, IPPROTO_TCP, NFS_PORT)) { 250 syslog(LOG_ERR, "can't register tcp with portmap"); 251 return (1); 252 } 253 maxsock = tcpsock; 254 connect_type_cnt++; 255 } 256 257 if (connect_type_cnt == 0) 258 return (0); 259 260 setproctitle("master"); 261 262 /* 263 * Allocate space for the fd_set pointers and fill in sockbits 264 */ 265 fd_size = howmany(maxsock + 1, NFDBITS) * sizeof(fd_mask); 266 sockbits = malloc(fd_size); 267 ready = malloc(fd_size); 268 if (sockbits == NULL || ready == NULL) { 269 syslog(LOG_ERR, "cannot allocate memory"); 270 return (1); 271 } 272 memset(sockbits, 0, fd_size); 273 if (tcpflag) 274 FD_SET(tcpsock, sockbits); 275 276 /* 277 * Loop forever accepting connections and passing the sockets 278 * into the kernel for the mounts. 279 */ 280 for (;;) { 281 memcpy(ready, sockbits, fd_size); 282 if (connect_type_cnt > 1) { 283 if (select(maxsock + 1, 284 ready, NULL, NULL, NULL) < 1) { 285 syslog(LOG_ERR, "select failed: %m"); 286 return (1); 287 } 288 } 289 if (tcpflag && FD_ISSET(tcpsock, ready)) { 290 len = sizeof(inetpeer); 291 if ((msgsock = accept(tcpsock, 292 (struct sockaddr *)&inetpeer, &len)) < 0) { 293 syslog(LOG_ERR, "accept failed: %m"); 294 return (1); 295 } 296 memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero)); 297 if (setsockopt(msgsock, SOL_SOCKET, 298 SO_KEEPALIVE, &on, sizeof(on)) < 0) 299 syslog(LOG_ERR, 300 "setsockopt SO_KEEPALIVE: %m"); 301 nfsdargs.sock = msgsock; 302 nfsdargs.name = (caddr_t)&inetpeer; 303 nfsdargs.namelen = sizeof(inetpeer); 304 if (nfssvc(NFSSVC_ADDSOCK, &nfsdargs) < 0) { 305 syslog(LOG_ERR, "can't Add TCP socket"); 306 return (1); 307 } 308 (void)close(msgsock); 309 } 310 } 311 } 312 313 void 314 usage(void) 315 { 316 (void)fprintf(stderr, "usage: nfsd [-rtu] [-n num_servers]\n"); 317 exit(1); 318 } 319 320 /* ARGSUSED */ 321 void 322 nonfs(int signo) 323 { 324 int save_errno = errno; 325 struct syslog_data sdata = SYSLOG_DATA_INIT; 326 327 syslog_r(LOG_ERR, &sdata, "missing system call: NFS not available."); 328 errno = save_errno; 329 } 330 331 /* ARGSUSED */ 332 void 333 reapchild(int signo) 334 { 335 int save_errno = errno; 336 337 while (wait3(NULL, WNOHANG, NULL) > 0) 338 ; 339 errno = save_errno; 340 } 341