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