1 /* 2 * Copyright (c) 1995, 1996 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 * $FreeBSD: src/usr.sbin/rpc.ypupdated/ypupdated_main.c,v 1.10 2004/05/24 12:28:27 stefanf Exp $ 33 */ 34 35 #include "ypupdate_prot.h" 36 #include <stdio.h> 37 #include <stdlib.h> /* getenv, exit */ 38 #include <rpc/pmap_clnt.h> /* for pmap_unset */ 39 #include <rpc/rpc_com.h> 40 #include <string.h> /* strcmp */ 41 #include <signal.h> 42 #ifdef __cplusplus 43 #include <sysent.h> /* getdtablesize, open */ 44 #endif /* __cplusplus */ 45 #include <memory.h> 46 #include <sys/socket.h> 47 #include <netinet/in.h> 48 #include <syslog.h> 49 #include <sys/wait.h> 50 #include <errno.h> 51 #include <err.h> 52 #include <unistd.h> 53 #include "ypupdated_extern.h" 54 #include "yp_extern.h" 55 56 #ifndef SIG_PF 57 #define SIG_PF void(*)(int) 58 #endif 59 60 #ifdef DEBUG 61 #define RPC_SVC_FG 62 #endif 63 64 #define _RPCSVC_CLOSEDOWN 120 65 int _rpcpmstart; /* Started by a port monitor ? */ 66 static int _rpcfdtype; 67 /* Whether Stream or Datagram ? */ 68 /* States a server can be in wrt request */ 69 70 #define _IDLE 0 71 #define _SERVED 1 72 #define _SERVING 2 73 74 extern int _rpcsvcstate; /* Set when a request is serviced */ 75 76 char *progname = "rpc.ypupdated"; 77 const char *yp_dir = "/var/yp/"; 78 79 static void 80 _msgout(char *msg) 81 { 82 #ifdef RPC_SVC_FG 83 if (_rpcpmstart) 84 syslog(LOG_ERR, "%s", msg); 85 else 86 warnx("%s", msg); 87 #else 88 syslog(LOG_ERR, "%s", msg); 89 #endif 90 } 91 92 static void 93 closedown(int sig) 94 { 95 if (_rpcsvcstate == _IDLE) { 96 extern fd_set svc_fdset; 97 static int size; 98 int i, openfd; 99 100 if (_rpcfdtype == SOCK_DGRAM) 101 exit(0); 102 if (size == 0) { 103 size = getdtablesize(); 104 } 105 for (i = 0, openfd = 0; i < size && openfd < 2; i++) 106 if (FD_ISSET(i, &svc_fdset)) 107 openfd++; 108 if (openfd <= 1) 109 exit(0); 110 } 111 if (_rpcsvcstate == _SERVED) 112 _rpcsvcstate = _IDLE; 113 114 signal(SIGALRM, (SIG_PF) closedown); 115 alarm(_RPCSVC_CLOSEDOWN/2); 116 } 117 118 static void 119 ypupdated_svc_run(void) 120 { 121 #ifdef FD_SETSIZE 122 fd_set readfds; 123 #else 124 int readfds; 125 #endif /* def FD_SETSIZE */ 126 extern int forked; 127 int pid; 128 int fd_setsize = _rpc_dtablesize(); 129 130 /* Establish the identity of the parent ypupdated process. */ 131 pid = getpid(); 132 133 for (;;) { 134 #ifdef FD_SETSIZE 135 readfds = svc_fdset; 136 #else 137 readfds = svc_fds; 138 #endif /* def FD_SETSIZE */ 139 switch (select(fd_setsize, &readfds, NULL, NULL, NULL)) { 140 case -1: 141 if (errno == EINTR) { 142 continue; 143 } 144 warn("svc_run: - select failed"); 145 return; 146 case 0: 147 continue; 148 default: 149 svc_getreqset(&readfds); 150 if (forked && pid != getpid()) 151 exit(0); 152 } 153 } 154 } 155 156 static void 157 reaper(int sig) 158 { 159 int status; 160 161 if (sig == SIGHUP) { 162 #ifdef foo 163 load_securenets(); 164 #endif 165 return; 166 } 167 168 if (sig == SIGCHLD) { 169 while (wait3(&status, WNOHANG, NULL) > 0) 170 children--; 171 } else { 172 pmap_unset(YPU_PROG, YPU_VERS); 173 exit(0); 174 } 175 } 176 177 void 178 usage(void) 179 { 180 fprintf(stderr, "rpc.ypupdatedd [-p path]\n"); 181 exit(0); 182 } 183 184 int 185 main(int argc, char *argv[]) 186 { 187 SVCXPRT *transp = NULL; 188 int sock; 189 int proto = 0; 190 struct sockaddr_in saddr; 191 int asize = sizeof (saddr); 192 int ch; 193 194 while ((ch = getopt(argc, argv, "p:h")) != -1) { 195 switch (ch) { 196 case 'p': 197 yp_dir = optarg; 198 break; 199 default: 200 usage(); 201 break; 202 } 203 } 204 #ifdef foo 205 load_securenets(); 206 #endif 207 208 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) { 209 yp_error("failed to register AUTH_DES flavor"); 210 exit(1); 211 } 212 213 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 214 int ssize = sizeof (int); 215 216 if (saddr.sin_family != AF_INET) 217 exit(1); 218 if (getsockopt(0, SOL_SOCKET, SO_TYPE, 219 (char *)&_rpcfdtype, &ssize) == -1) 220 exit(1); 221 sock = 0; 222 _rpcpmstart = 1; 223 proto = 0; 224 openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON); 225 } else { 226 #ifndef RPC_SVC_FG 227 if (daemon(0,0)) { 228 err(1, "cannot fork"); 229 } 230 openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON); 231 #endif 232 sock = RPC_ANYSOCK; 233 pmap_unset(YPU_PROG, YPU_VERS); 234 } 235 236 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 237 transp = svcudp_create(sock); 238 if (transp == NULL) { 239 _msgout("cannot create udp service."); 240 exit(1); 241 } 242 if (!_rpcpmstart) 243 proto = IPPROTO_UDP; 244 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 245 _msgout("unable to register (YPU_PROG, YPU_VERS, udp)."); 246 exit(1); 247 } 248 } 249 250 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 251 transp = svctcp_create(sock, 0, 0); 252 if (transp == NULL) { 253 _msgout("cannot create tcp service."); 254 exit(1); 255 } 256 if (!_rpcpmstart) 257 proto = IPPROTO_TCP; 258 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 259 _msgout("unable to register (YPU_PROG, YPU_VERS, tcp)."); 260 exit(1); 261 } 262 } 263 264 if (transp == NULL) { 265 _msgout("could not create a handle"); 266 exit(1); 267 } 268 if (_rpcpmstart) { 269 signal(SIGALRM, (SIG_PF) closedown); 270 alarm(_RPCSVC_CLOSEDOWN/2); 271 } 272 273 signal(SIGPIPE, SIG_IGN); 274 signal(SIGCHLD, (SIG_PF) reaper); 275 signal(SIGTERM, (SIG_PF) reaper); 276 signal(SIGINT, (SIG_PF) reaper); 277 signal(SIGHUP, (SIG_PF) reaper); 278 279 ypupdated_svc_run(); 280 _msgout("svc_run returned"); 281 exit(1); 282 /* NOTREACHED */ 283 } 284