1 /* $OpenBSD: rstatd.c,v 1.23 2009/10/27 23:59:31 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1993, John Brezak 5 * 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. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <signal.h> 38 #include <pwd.h> 39 #include <syslog.h> 40 #include <errno.h> 41 #include <stdlib.h> 42 #include <rpc/rpc.h> 43 #include <rpcsvc/rstat.h> 44 45 extern int __svc_fdsetsize; 46 extern fd_set *__svc_fdset; 47 extern void svc_getreqset2(fd_set *, int); 48 extern void rstat_service(struct svc_req *, SVCXPRT *); 49 50 void my_svc_run(void); 51 52 int from_inetd = 1; /* started from inetd ? */ 53 int closedown = 20; /* how long to wait before going dormant */ 54 55 volatile sig_atomic_t gotsig; 56 57 /* ARGSUSED */ 58 static void 59 getsig(int signo) 60 { 61 gotsig = 1; 62 } 63 64 65 int 66 main(int argc, char *argv[]) 67 { 68 int sock = 0, proto = 0; 69 socklen_t fromlen; 70 struct passwd *pw; 71 struct sockaddr_storage from; 72 SVCXPRT *transp; 73 74 openlog("rpc.rstatd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON); 75 76 if ((pw = getpwnam("_rstatd")) == NULL) { 77 syslog(LOG_ERR, "no such user _rstatd"); 78 exit(1); 79 } 80 if (chroot("/var/empty") == -1) { 81 syslog(LOG_ERR, "cannot chdir to /var/empty."); 82 exit(1); 83 } 84 chdir("/"); 85 86 if (pw) { 87 setgroups(1, &pw->pw_gid); 88 setegid(pw->pw_gid); 89 setgid(pw->pw_gid); 90 seteuid(pw->pw_uid); 91 setuid(pw->pw_uid); 92 } 93 94 if (argc == 2) 95 closedown = atoi(argv[1]); 96 if (closedown <= 0) 97 closedown = 20; 98 99 /* 100 * See if inetd started us 101 */ 102 fromlen = sizeof(from); 103 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 104 from_inetd = 0; 105 sock = RPC_ANYSOCK; 106 proto = IPPROTO_UDP; 107 } 108 109 if (!from_inetd) { 110 daemon(0, 0); 111 112 (void)pmap_unset(RSTATPROG, RSTATVERS_TIME); 113 (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 114 (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG); 115 116 (void) signal(SIGINT, getsig); 117 (void) signal(SIGTERM, getsig); 118 (void) signal(SIGHUP, getsig); 119 } 120 121 transp = svcudp_create(sock); 122 if (transp == NULL) { 123 syslog(LOG_ERR, "cannot create udp service."); 124 exit(1); 125 } 126 if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) { 127 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp)."); 128 exit(1); 129 } 130 if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) { 131 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp)."); 132 exit(1); 133 } 134 if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) { 135 syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp)."); 136 exit(1); 137 } 138 139 my_svc_run(); 140 syslog(LOG_ERR, "svc_run returned"); 141 exit(1); 142 } 143 144 void 145 my_svc_run(void) 146 { 147 extern volatile sig_atomic_t wantupdatestat; 148 extern void updatestat(void); 149 struct pollfd *pfd = NULL, *newp; 150 int nready, saved_max_pollfd = 0; 151 152 for (;;) { 153 if (wantupdatestat) { 154 wantupdatestat = 0; 155 updatestat(); 156 } 157 if (gotsig) { 158 (void) pmap_unset(RSTATPROG, RSTATVERS_TIME); 159 (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 160 (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG); 161 exit(0); 162 } 163 if (svc_max_pollfd > saved_max_pollfd) { 164 newp = realloc(pfd, sizeof(*pfd) * svc_max_pollfd); 165 if (newp == NULL) { 166 free(pfd); 167 perror("svc_run: - realloc failed"); 168 return; 169 } 170 pfd = newp; 171 saved_max_pollfd = svc_max_pollfd; 172 } 173 memcpy(pfd, svc_pollfd, sizeof(*pfd) * svc_max_pollfd); 174 175 nready = poll(pfd, svc_max_pollfd, INFTIM); 176 switch (nready) { 177 case -1: 178 if (errno == EINTR) 179 continue; 180 perror("svc_run: - poll failed"); 181 free(pfd); 182 return; 183 case 0: 184 continue; 185 default: 186 svc_getreq_poll(pfd, nready); 187 } 188 } 189 } 190