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