1 /* $OpenBSD: rusersd.c,v 1.21 2019/06/28 13:32:53 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 AUTHOR `AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 34 #include <fcntl.h> 35 #include <stdio.h> 36 #include <signal.h> 37 #include <unistd.h> 38 #include <stdlib.h> 39 #include <pwd.h> 40 #include <syslog.h> 41 #include <rpc/rpc.h> 42 #include <rpcsvc/rusers.h> /* New version */ 43 #include <rpcsvc/rnusers.h> /* Old version */ 44 #include <rpc/pmap_clnt.h> 45 #include <utmp.h> 46 47 extern void rusers_service(struct svc_req *, SVCXPRT *); 48 49 int from_inetd = 1; 50 int utmp_fd; 51 52 /* ARGSUSED */ 53 static void 54 cleanup(int signo) 55 { 56 (void) pmap_unset(RUSERSPROG, RUSERSVERS_3); /* XXX signal races */ 57 (void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE); 58 (void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG); 59 _exit(0); 60 } 61 62 int 63 main(int argc, char *argv[]) 64 { 65 int sock = 0, proto = 0; 66 socklen_t fromlen; 67 struct sockaddr_storage from; 68 struct passwd *pw; 69 SVCXPRT *transp; 70 71 if ((utmp_fd = open(_PATH_UTMP, O_RDONLY)) == -1) { 72 syslog(LOG_ERR, "cannot open %s", _PATH_UTMP); 73 exit(1); 74 } 75 76 openlog("rpc.rusersd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON); 77 78 pw = getpwnam("_rusersd"); 79 if (!pw) { 80 syslog(LOG_ERR, "no such user _rusersd"); 81 exit(1); 82 } 83 if (chroot("/var/empty") == -1) { 84 syslog(LOG_ERR, "cannot chdir to /var/empty."); 85 exit(1); 86 } 87 chdir("/"); 88 89 setgroups(1, &pw->pw_gid); 90 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid); 91 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid); 92 93 /* 94 * See if inetd started us 95 */ 96 fromlen = sizeof(from); 97 if (getsockname(0, (struct sockaddr *)&from, &fromlen) == -1) { 98 from_inetd = 0; 99 sock = RPC_ANYSOCK; 100 proto = IPPROTO_UDP; 101 } 102 103 if (!from_inetd) { 104 daemon(0, 0); 105 106 (void) pmap_unset(RUSERSPROG, RUSERSVERS_3); 107 (void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE); 108 (void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG); 109 110 (void) signal(SIGINT, cleanup); 111 (void) signal(SIGTERM, cleanup); 112 (void) signal(SIGHUP, cleanup); 113 } 114 115 transp = svcudp_create(sock); 116 if (transp == NULL) { 117 syslog(LOG_ERR, "cannot create udp service."); 118 exit(1); 119 } 120 if (!svc_register(transp, RUSERSPROG, RUSERSVERS_3, rusers_service, proto)) { 121 syslog(LOG_ERR, 122 "unable to register (RUSERSPROG, RUSERSVERS_3, %s).", 123 proto ? "udp" : "(inetd)"); 124 exit(1); 125 } 126 if (!svc_register(transp, RUSERSPROG, RUSERSVERS_IDLE, rusers_service, proto)) { 127 syslog(LOG_ERR, 128 "unable to register (RUSERSPROG, RUSERSVERS_IDLE, %s).", 129 proto ? "udp" : "(inetd)"); 130 exit(1); 131 } 132 if (!svc_register(transp, RUSERSPROG, RUSERSVERS_ORIG, rusers_service, proto)) { 133 syslog(LOG_ERR, 134 "unable to register (RUSERSPROG, RUSERSVERS_ORIG, %s).", 135 proto ? "udp" : "(inetd)"); 136 exit(1); 137 } 138 139 svc_run(); 140 syslog(LOG_ERR, "svc_run returned"); 141 exit(1); 142 } 143