xref: /openbsd-src/libexec/rpc.rusersd/rusersd.c (revision 5b133f3f277e80f096764111e64f3a1284acb179)
1 /*	$OpenBSD: rusersd.c,v 1.24 2023/03/08 04:43:06 guenther 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 static void
cleanup(int signo)53 cleanup(int signo)
54 {
55 	(void) pmap_unset(RUSERSPROG, RUSERSVERS_3);	/* XXX signal races */
56 	(void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);
57 	(void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG);
58 	_exit(0);
59 }
60 
61 int
main(int argc,char * argv[])62 main(int argc, char *argv[])
63 {
64 	int sock = 0, proto = 0;
65 	socklen_t fromlen;
66 	struct sockaddr_storage from;
67 	struct passwd *pw;
68 	SVCXPRT *transp;
69 
70 	if ((utmp_fd = open(_PATH_UTMP, O_RDONLY)) == -1) {
71 		syslog(LOG_ERR, "cannot open %s", _PATH_UTMP);
72 		exit(1);
73 	}
74 
75 	openlog("rpc.rusersd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON);
76 
77 	pw = getpwnam("_rusersd");
78 	if (!pw) {
79 		syslog(LOG_ERR, "no such user _rusersd");
80 		exit(1);
81 	}
82 
83 	if (unveil("/dev", "r") == -1) {
84 		syslog(LOG_ERR, "unveil /dev");
85 		exit(1);
86 	}
87 	if (unveil(NULL, NULL) == -1) {
88 		syslog(LOG_ERR, "unveil");
89 		exit(1);
90 	}
91 
92 	setgroups(1, &pw->pw_gid);
93 	setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid);
94 	setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
95 
96 	/*
97 	 * See if inetd started us
98 	 */
99 	fromlen = sizeof(from);
100 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) == -1) {
101 		from_inetd = 0;
102 		sock = RPC_ANYSOCK;
103 		proto = IPPROTO_UDP;
104 	}
105 
106 	if (!from_inetd) {
107 		daemon(0, 0);
108 
109 		(void) pmap_unset(RUSERSPROG, RUSERSVERS_3);
110 		(void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);
111 		(void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG);
112 
113 		(void) signal(SIGINT, cleanup);
114 		(void) signal(SIGTERM, cleanup);
115 		(void) signal(SIGHUP, cleanup);
116 	}
117 
118 	transp = svcudp_create(sock);
119 	if (transp == NULL) {
120 		syslog(LOG_ERR, "cannot create udp service.");
121 		exit(1);
122 	}
123 	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_3, rusers_service, proto)) {
124 		syslog(LOG_ERR,
125 		    "unable to register (RUSERSPROG, RUSERSVERS_3, %s).",
126 		    proto ? "udp" : "(inetd)");
127 		exit(1);
128 	}
129 	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_IDLE, rusers_service, proto)) {
130 		syslog(LOG_ERR,
131 		    "unable to register (RUSERSPROG, RUSERSVERS_IDLE, %s).",
132 		    proto ? "udp" : "(inetd)");
133 		exit(1);
134 	}
135 	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_ORIG, rusers_service, proto)) {
136 		syslog(LOG_ERR,
137 		    "unable to register (RUSERSPROG, RUSERSVERS_ORIG, %s).",
138 		    proto ? "udp" : "(inetd)");
139 		exit(1);
140 	}
141 
142 	svc_run();
143 	syslog(LOG_ERR, "svc_run returned");
144 	exit(1);
145 }
146