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 #include "ypxfrd.h" 37 #include <err.h> 38 #include <fcntl.h> 39 #include <paths.h> 40 #include <stdio.h> 41 #include <stdlib.h> /* getenv, exit */ 42 #include <unistd.h> 43 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 44 #include <rpc/rpc_com.h> 45 #include <string.h> /* strcmp */ 46 #include <signal.h> 47 #include <sys/ttycom.h> /* TIOCNOTTY */ 48 #ifdef __cplusplus 49 #include <sysent.h> /* getdtablesize, open */ 50 #endif /* __cplusplus */ 51 #include <memory.h> 52 #include <sys/socket.h> 53 #include <netinet/in.h> 54 #include <syslog.h> 55 #include "ypxfrd_extern.h" 56 #include <sys/wait.h> 57 #include <errno.h> 58 59 #ifndef SIG_PF 60 #define SIG_PF void(*)(int) 61 #endif 62 63 #ifdef DEBUG 64 #define RPC_SVC_FG 65 #endif 66 67 #define _RPCSVC_CLOSEDOWN 120 68 int _rpcpmstart; /* Started by a port monitor ? */ 69 static int _rpcfdtype; 70 /* Whether Stream or Datagram ? */ 71 /* States a server can be in wrt request */ 72 73 #define _IDLE 0 74 #define _SERVED 1 75 #define _SERVING 2 76 77 extern int _rpcsvcstate; /* Set when a request is serviced */ 78 79 int debug; 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 } 186 187 void 188 usage(void) 189 { 190 fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n"); 191 exit(0); 192 } 193 194 int 195 main(int argc, char *argv[]) 196 { 197 register SVCXPRT *transp = NULL; 198 int sock; 199 int proto = 0; 200 struct sockaddr_in saddr; 201 int asize = sizeof (saddr); 202 int ch; 203 204 while ((ch = getopt(argc, argv, "p:h")) != -1) { 205 switch (ch) { 206 case 'p': 207 yp_dir = optarg; 208 break; 209 default: 210 usage(); 211 break; 212 } 213 } 214 215 load_securenets(); 216 217 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 218 int ssize = sizeof (int); 219 220 if (saddr.sin_family != AF_INET) 221 exit(1); 222 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 223 (char *)&_rpcfdtype, &ssize) == -1) 224 exit(1); 225 sock = 0; 226 _rpcpmstart = 1; 227 proto = 0; 228 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 229 } else { 230 #ifndef RPC_SVC_FG 231 int size; 232 int pid, i; 233 234 pid = fork(); 235 if (pid < 0) 236 err(1, "fork"); 237 if (pid) 238 exit(0); 239 size = getdtablesize(); 240 for (i = 0; i < size; i++) 241 (void) close(i); 242 i = open(_PATH_CONSOLE, 2); 243 (void) dup2(i, 1); 244 (void) dup2(i, 2); 245 i = open(_PATH_TTY, 2); 246 if (i >= 0) { 247 (void) ioctl(i, TIOCNOTTY, (char *)NULL); 248 (void) close(i); 249 } 250 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 251 #endif 252 sock = RPC_ANYSOCK; 253 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 254 } 255 256 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 257 transp = svcudp_create(sock); 258 if (transp == NULL) { 259 _msgout("cannot create udp service."); 260 exit(1); 261 } 262 if (!_rpcpmstart) 263 proto = IPPROTO_UDP; 264 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 265 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp)."); 266 exit(1); 267 } 268 } 269 270 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 271 transp = svctcp_create(sock, 0, 0); 272 if (transp == NULL) { 273 _msgout("cannot create tcp service."); 274 exit(1); 275 } 276 if (!_rpcpmstart) 277 proto = IPPROTO_TCP; 278 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 279 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)."); 280 exit(1); 281 } 282 } 283 284 if (transp == (SVCXPRT *)NULL) { 285 _msgout("could not create a handle"); 286 exit(1); 287 } 288 if (_rpcpmstart) { 289 (void) signal(SIGALRM, (SIG_PF) closedown); 290 (void) alarm(_RPCSVC_CLOSEDOWN/2); 291 } 292 293 (void) signal(SIGPIPE, SIG_IGN); 294 (void) signal(SIGCHLD, (SIG_PF) reaper); 295 (void) signal(SIGTERM, (SIG_PF) reaper); 296 (void) signal(SIGINT, (SIG_PF) reaper); 297 (void) signal(SIGHUP, (SIG_PF) reaper); 298 299 ypxfrd_svc_run(); 300 _msgout("svc_run returned"); 301 exit(1); 302 /* NOTREACHED */ 303 } 304