1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1995, 1996 5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Bill Paul. 18 * 4. Neither the name of the author nor the names of any co-contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include "ypxfrd.h" 39 #include <err.h> 40 #include <fcntl.h> 41 #include <paths.h> 42 #include <stdio.h> 43 #include <stdlib.h> /* getenv, exit */ 44 #include <unistd.h> 45 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 46 #include <rpc/rpc_com.h> 47 #include <string.h> /* strcmp */ 48 #include <signal.h> 49 #include <sys/ttycom.h> /* TIOCNOTTY */ 50 #ifdef __cplusplus 51 #include <sysent.h> /* getdtablesize, open */ 52 #endif /* __cplusplus */ 53 #include <memory.h> 54 #include <sys/socket.h> 55 #include <netinet/in.h> 56 #include <syslog.h> 57 #include "ypxfrd_extern.h" 58 #include <sys/wait.h> 59 #include <errno.h> 60 61 #ifndef SIG_PF 62 #define SIG_PF void(*)(int) 63 #endif 64 65 #ifdef DEBUG 66 #define RPC_SVC_FG 67 #endif 68 69 #define _RPCSVC_CLOSEDOWN 120 70 int _rpcpmstart; /* Started by a port monitor ? */ 71 static int _rpcfdtype; 72 /* Whether Stream or Datagram ? */ 73 /* States a server can be in wrt request */ 74 75 #define _IDLE 0 76 #define _SERVED 1 77 #define _SERVING 2 78 79 extern int _rpcsvcstate; /* Set when a request is serviced */ 80 81 char *progname = "rpc.ypxfrd"; 82 char *yp_dir = "/var/yp/"; 83 84 static void 85 _msgout(char *msg) 86 { 87 #ifdef RPC_SVC_FG 88 if (_rpcpmstart) 89 syslog(LOG_ERR, "%s", msg); 90 else 91 warnx("%s", msg); 92 #else 93 syslog(LOG_ERR, "%s", msg); 94 #endif 95 } 96 97 static void 98 closedown(int sig) 99 { 100 if (_rpcsvcstate == _IDLE) { 101 extern fd_set svc_fdset; 102 static int size; 103 int i, openfd; 104 105 if (_rpcfdtype == SOCK_DGRAM) 106 exit(0); 107 if (size == 0) { 108 size = getdtablesize(); 109 } 110 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 111 if (FD_ISSET(i, &svc_fdset)) 112 openfd++; 113 if (openfd <= 1) 114 exit(0); 115 } 116 if (_rpcsvcstate == _SERVED) 117 _rpcsvcstate = _IDLE; 118 119 (void) signal(SIGALRM, (SIG_PF) closedown); 120 (void) alarm(_RPCSVC_CLOSEDOWN/2); 121 } 122 123 124 static void 125 ypxfrd_svc_run(void) 126 { 127 #ifdef FD_SETSIZE 128 fd_set readfds; 129 #else 130 int readfds; 131 #endif /* def FD_SETSIZE */ 132 extern int forked; 133 int pid; 134 int fd_setsize = _rpc_dtablesize(); 135 136 /* Establish the identity of the parent ypserv process. */ 137 pid = getpid(); 138 139 for (;;) { 140 #ifdef FD_SETSIZE 141 readfds = svc_fdset; 142 #else 143 readfds = svc_fds; 144 #endif /* def FD_SETSIZE */ 145 switch (select(fd_setsize, &readfds, NULL, NULL, 146 (struct timeval *)0)) { 147 case -1: 148 if (errno == EINTR) { 149 continue; 150 } 151 warn("svc_run: - select failed"); 152 return; 153 case 0: 154 continue; 155 default: 156 svc_getreqset(&readfds); 157 if (forked && pid != getpid()) 158 exit(0); 159 } 160 } 161 } 162 163 static void reaper(int sig) 164 { 165 int status; 166 int saved_errno; 167 168 saved_errno = errno; 169 170 if (sig == SIGHUP) { 171 load_securenets(); 172 errno = saved_errno; 173 return; 174 } 175 176 if (sig == SIGCHLD) { 177 while (wait3(&status, WNOHANG, NULL) > 0) 178 children--; 179 } else { 180 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 181 exit(0); 182 } 183 184 errno = saved_errno; 185 return; 186 } 187 188 void 189 usage(void) 190 { 191 fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n"); 192 exit(0); 193 } 194 195 int 196 main(int argc, char *argv[]) 197 { 198 register SVCXPRT *transp = NULL; 199 int sock; 200 int proto = 0; 201 struct sockaddr_in saddr; 202 int asize = sizeof (saddr); 203 int ch; 204 205 while ((ch = getopt(argc, argv, "p:h")) != -1) { 206 switch (ch) { 207 case 'p': 208 yp_dir = optarg; 209 break; 210 default: 211 usage(); 212 break; 213 } 214 } 215 216 load_securenets(); 217 218 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 219 int ssize = sizeof (int); 220 221 if (saddr.sin_family != AF_INET) 222 exit(1); 223 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 224 (char *)&_rpcfdtype, &ssize) == -1) 225 exit(1); 226 sock = 0; 227 _rpcpmstart = 1; 228 proto = 0; 229 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 230 } else { 231 #ifndef RPC_SVC_FG 232 int size; 233 int pid, i; 234 235 pid = fork(); 236 if (pid < 0) 237 err(1, "fork"); 238 if (pid) 239 exit(0); 240 size = getdtablesize(); 241 for (i = 0; i < size; i++) 242 (void) close(i); 243 i = open(_PATH_CONSOLE, 2); 244 (void) dup2(i, 1); 245 (void) dup2(i, 2); 246 i = open(_PATH_TTY, 2); 247 if (i >= 0) { 248 (void) ioctl(i, TIOCNOTTY, (char *)NULL); 249 (void) close(i); 250 } 251 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 252 #endif 253 sock = RPC_ANYSOCK; 254 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 255 } 256 257 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 258 transp = svcudp_create(sock); 259 if (transp == NULL) { 260 _msgout("cannot create udp service."); 261 exit(1); 262 } 263 if (!_rpcpmstart) 264 proto = IPPROTO_UDP; 265 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 266 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp)."); 267 exit(1); 268 } 269 } 270 271 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 272 transp = svctcp_create(sock, 0, 0); 273 if (transp == NULL) { 274 _msgout("cannot create tcp service."); 275 exit(1); 276 } 277 if (!_rpcpmstart) 278 proto = IPPROTO_TCP; 279 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 280 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)."); 281 exit(1); 282 } 283 } 284 285 if (transp == (SVCXPRT *)NULL) { 286 _msgout("could not create a handle"); 287 exit(1); 288 } 289 if (_rpcpmstart) { 290 (void) signal(SIGALRM, (SIG_PF) closedown); 291 (void) alarm(_RPCSVC_CLOSEDOWN/2); 292 } 293 294 (void) signal(SIGPIPE, SIG_IGN); 295 (void) signal(SIGCHLD, (SIG_PF) reaper); 296 (void) signal(SIGTERM, (SIG_PF) reaper); 297 (void) signal(SIGINT, (SIG_PF) reaper); 298 (void) signal(SIGHUP, (SIG_PF) reaper); 299 300 ypxfrd_svc_run(); 301 _msgout("svc_run returned"); 302 exit(1); 303 /* NOTREACHED */ 304 } 305