xref: /freebsd/usr.sbin/rpc.ypupdated/ypupdated_main.c (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  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 Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "ypupdate_prot.h"
39 #include <stdio.h>
40 #include <stdlib.h> /* getenv, exit */
41 #include <rpc/pmap_clnt.h> /* for pmap_unset */
42 #include <rpc/rpc_com.h>
43 #include <string.h> /* strcmp */
44 #include <signal.h>
45 #ifdef __cplusplus
46 #include <sysent.h> /* getdtablesize, open */
47 #endif /* __cplusplus */
48 #include <memory.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <syslog.h>
52 #include <sys/wait.h>
53 #include <errno.h>
54 #include <err.h>
55 #include <unistd.h>
56 #include "ypupdated_extern.h"
57 #include "yp_extern.h"
58 
59 #ifndef SIG_PF
60 #define	SIG_PF void(*)(int)
61 #endif
62 
63 #ifdef DEBUG
64 #define	RPC_SVC_FG
65 #endif
66 
67 #define	_RPCSVC_CLOSEDOWN 120
68 int _rpcpmstart;		/* Started by a port monitor ? */
69 static int _rpcfdtype;
70 		 /* Whether Stream or Datagram ? */
71 	/* States a server can be in wrt request */
72 
73 #define	_IDLE 0
74 #define	_SERVED 1
75 #define	_SERVING 2
76 
77 extern int _rpcsvcstate;	 /* Set when a request is serviced */
78 
79 int debug;
80 
81 char *progname = "rpc.ypupdated";
82 char *yp_dir = "/var/yp/";
83 
84 static void
85 _msgout(char* msg)
86 {
87 #ifdef RPC_SVC_FG
88 	if (_rpcpmstart)
89 		syslog(LOG_ERR, "%s", msg);
90 	else
91 		warnx("%s", msg);
92 #else
93 	syslog(LOG_ERR, "%s", msg);
94 #endif
95 }
96 
97 static void
98 closedown(int sig)
99 {
100 	if (_rpcsvcstate == _IDLE) {
101 		extern fd_set svc_fdset;
102 		static int size;
103 		int i, openfd;
104 
105 		if (_rpcfdtype == SOCK_DGRAM)
106 			exit(0);
107 		if (size == 0) {
108 			size = getdtablesize();
109 		}
110 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
111 			if (FD_ISSET(i, &svc_fdset))
112 				openfd++;
113 		if (openfd <= 1)
114 			exit(0);
115 	}
116 	if (_rpcsvcstate == _SERVED)
117 		_rpcsvcstate = _IDLE;
118 
119 	(void) signal(SIGALRM, (SIG_PF) closedown);
120 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
121 }
122 
123 static void
124 ypupdated_svc_run(void)
125 {
126 #ifdef FD_SETSIZE
127 	fd_set readfds;
128 #else
129 	int readfds;
130 #endif /* def FD_SETSIZE */
131 	extern int forked;
132 	int pid;
133 	int fd_setsize = _rpc_dtablesize();
134 
135 	/* Establish the identity of the parent ypupdated process. */
136 	pid = getpid();
137 
138 	for (;;) {
139 #ifdef FD_SETSIZE
140 		readfds = svc_fdset;
141 #else
142 		readfds = svc_fds;
143 #endif /* def FD_SETSIZE */
144 		switch (select(fd_setsize, &readfds, NULL, NULL,
145 			       (struct timeval *)0)) {
146 		case -1:
147 			if (errno == EINTR) {
148 				continue;
149 			}
150 			warn("svc_run: - select failed");
151 			return;
152 		case 0:
153 			continue;
154 		default:
155 			svc_getreqset(&readfds);
156 			if (forked && pid != getpid())
157 				exit(0);
158 		}
159 	}
160 }
161 
162 static void
163 reaper(int sig)
164 {
165 	int status;
166 
167 	if (sig == SIGHUP) {
168 #ifdef foo
169 		load_securenets();
170 #endif
171 		return;
172 	}
173 
174 	if (sig == SIGCHLD) {
175 		while (wait3(&status, WNOHANG, NULL) > 0)
176 			children--;
177 	} else {
178 		(void) pmap_unset(YPU_PROG, YPU_VERS);
179 		exit(0);
180 	}
181 }
182 
183 void
184 usage(void)
185 {
186 	fprintf(stderr, "rpc.ypupdatedd [-p path]\n");
187 	exit(0);
188 }
189 
190 int
191 main(int argc, char *argv[])
192 {
193 	register SVCXPRT *transp = NULL;
194 	int sock;
195 	int proto = 0;
196 	struct sockaddr_in saddr;
197 	int asize = sizeof (saddr);
198 	int ch;
199 
200 	while ((ch = getopt(argc, argv, "p:h")) != -1) {
201 		switch (ch) {
202 		case 'p':
203 			yp_dir = optarg;
204 			break;
205 		default:
206 			usage();
207 			break;
208 		}
209 	}
210 #ifdef foo
211 	load_securenets();
212 #endif
213 
214 	if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) {
215 		yp_error("failed to register AUTH_DES flavor");
216 		exit(1);
217 	}
218 
219 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
220 		int ssize = sizeof (int);
221 
222 		if (saddr.sin_family != AF_INET)
223 			exit(1);
224 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
225 				(char *)&_rpcfdtype, &ssize) == -1)
226 			exit(1);
227 		sock = 0;
228 		_rpcpmstart = 1;
229 		proto = 0;
230 		openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON);
231 	} else {
232 #ifndef RPC_SVC_FG
233 		if (daemon(0,0)) {
234 			err(1, "cannot fork");
235 		}
236 		openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON);
237 #endif
238 		sock = RPC_ANYSOCK;
239 		(void) pmap_unset(YPU_PROG, YPU_VERS);
240 	}
241 
242 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
243 		transp = svcudp_create(sock);
244 		if (transp == NULL) {
245 			_msgout("cannot create udp service.");
246 			exit(1);
247 		}
248 		if (!_rpcpmstart)
249 			proto = IPPROTO_UDP;
250 		if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
251 			_msgout("unable to register (YPU_PROG, YPU_VERS, udp).");
252 			exit(1);
253 		}
254 	}
255 
256 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
257 		transp = svctcp_create(sock, 0, 0);
258 		if (transp == NULL) {
259 			_msgout("cannot create tcp service.");
260 			exit(1);
261 		}
262 		if (!_rpcpmstart)
263 			proto = IPPROTO_TCP;
264 		if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
265 			_msgout("unable to register (YPU_PROG, YPU_VERS, tcp).");
266 			exit(1);
267 		}
268 	}
269 
270 	if (transp == (SVCXPRT *)NULL) {
271 		_msgout("could not create a handle");
272 		exit(1);
273 	}
274 	if (_rpcpmstart) {
275 		(void) signal(SIGALRM, (SIG_PF) closedown);
276 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
277 	}
278 
279 	(void) signal(SIGPIPE, SIG_IGN);
280 	(void) signal(SIGCHLD, (SIG_PF) reaper);
281 	(void) signal(SIGTERM, (SIG_PF) reaper);
282 	(void) signal(SIGINT, (SIG_PF) reaper);
283 	(void) signal(SIGHUP, (SIG_PF) reaper);
284 
285 	ypupdated_svc_run();
286 	_msgout("svc_run returned");
287 	exit(1);
288 	/* NOTREACHED */
289 }
290