xref: /openbsd/libexec/rpc.rstatd/rstatd.c (revision 7b36286a)
1 /*	$OpenBSD: rstatd.c,v 1.21 2005/09/16 23:50:33 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 #ifndef lint
32 static char rcsid[] = "$OpenBSD: rstatd.c,v 1.21 2005/09/16 23:50:33 deraadt Exp $";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #include <pwd.h>
43 #include <syslog.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <rpc/rpc.h>
47 #include <rpcsvc/rstat.h>
48 
49 extern int __svc_fdsetsize;
50 extern fd_set *__svc_fdset;
51 extern void svc_getreqset2(fd_set *, int);
52 extern void rstat_service(struct svc_req *, SVCXPRT *);
53 
54 void my_svc_run(void);
55 
56 int from_inetd = 1;	/* started from inetd ? */
57 int closedown = 20;	/* how long to wait before going dormant */
58 
59 volatile sig_atomic_t gotsig;
60 
61 /* ARGSUSED */
62 static void
63 getsig(int signo)
64 {
65 	gotsig = 1;
66 }
67 
68 
69 int
70 main(int argc, char *argv[])
71 {
72 	int sock = 0, proto = 0;
73 	socklen_t fromlen;
74 	struct passwd *pw;
75 	struct sockaddr_storage from;
76 	SVCXPRT *transp;
77 
78 	openlog("rpc.rstatd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON);
79 
80 	pw = getpwnam("_rstatd");
81 	if (!pw)
82 		pw = getpwnam("nobody");
83 	if (chroot("/var/empty") == -1) {
84 		syslog(LOG_ERR, "cannot chdir to /var/empty.");
85 		exit(1);
86 	}
87 	chdir("/");
88 
89 	if (pw) {
90 		setgroups(1, &pw->pw_gid);
91 		setegid(pw->pw_gid);
92 		setgid(pw->pw_gid);
93 		seteuid(pw->pw_uid);
94 		setuid(pw->pw_uid);
95 	}
96 
97 	if (argc == 2)
98 		closedown = atoi(argv[1]);
99 	if (closedown <= 0)
100 		closedown = 20;
101 
102 	/*
103 	 * See if inetd started us
104 	 */
105 	fromlen = sizeof(from);
106 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
107 		from_inetd = 0;
108 		sock = RPC_ANYSOCK;
109 		proto = IPPROTO_UDP;
110 	}
111 
112 	if (!from_inetd) {
113 		daemon(0, 0);
114 
115 		(void)pmap_unset(RSTATPROG, RSTATVERS_TIME);
116 		(void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
117 		(void)pmap_unset(RSTATPROG, RSTATVERS_ORIG);
118 
119 		(void) signal(SIGINT, getsig);
120 		(void) signal(SIGTERM, getsig);
121 		(void) signal(SIGHUP, getsig);
122 	}
123 
124 	transp = svcudp_create(sock);
125 	if (transp == NULL) {
126 		syslog(LOG_ERR, "cannot create udp service.");
127 		exit(1);
128 	}
129 	if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) {
130 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp).");
131 		exit(1);
132 	}
133 	if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) {
134 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp).");
135 		exit(1);
136 	}
137 	if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) {
138 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp).");
139 		exit(1);
140 	}
141 
142 	my_svc_run();
143 	syslog(LOG_ERR, "svc_run returned");
144 	exit(1);
145 }
146 
147 void
148 my_svc_run(void)
149 {
150 	extern volatile sig_atomic_t wantupdatestat;
151 	extern void updatestat(void);
152 	struct pollfd *pfd = NULL, *newp;
153 	int nready, saved_max_pollfd = 0;
154 
155 	for (;;) {
156 		if (wantupdatestat) {
157 			wantupdatestat = 0;
158 			updatestat();
159 		}
160 		if (gotsig) {
161 			(void) pmap_unset(RSTATPROG, RSTATVERS_TIME);
162 			(void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
163 			(void) pmap_unset(RSTATPROG, RSTATVERS_ORIG);
164 			exit(0);
165 		}
166 		if (svc_max_pollfd > saved_max_pollfd) {
167 			newp = realloc(pfd, sizeof(*pfd) * svc_max_pollfd);
168 			if (newp == NULL) {
169 				free(pfd);
170 				perror("svc_run: - realloc failed");
171 				return;
172 			}
173 			pfd = newp;
174 			saved_max_pollfd = svc_max_pollfd;
175 		}
176 		memcpy(pfd, svc_pollfd, sizeof(*pfd) * svc_max_pollfd);
177 
178 		nready = poll(pfd, svc_max_pollfd, INFTIM);
179 		switch (nready) {
180 		case -1:
181 			if (errno == EINTR)
182 				continue;
183 			perror("svc_run: - poll failed");
184 			free(pfd);
185 			return;
186 		case 0:
187 			continue;
188 		default:
189 			svc_getreq_poll(pfd, nready);
190 		}
191 	}
192 }
193