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