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 #include "ypupdate_prot.h"
37 #include <stdio.h>
38 #include <stdlib.h> /* getenv, exit */
39 #include <rpc/pmap_clnt.h> /* for pmap_unset */
40 #include <rpc/rpc_com.h>
41 #include <string.h> /* strcmp */
42 #include <signal.h>
43 #ifdef __cplusplus
44 #include <sysent.h> /* getdtablesize, open */
45 #endif /* __cplusplus */
46 #include <memory.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <syslog.h>
50 #include <sys/wait.h>
51 #include <errno.h>
52 #include <err.h>
53 #include <unistd.h>
54 #include "ypupdated_extern.h"
55 #include "yp_extern.h"
56
57 #ifndef SIG_PF
58 #define SIG_PF void(*)(int)
59 #endif
60
61 #ifdef DEBUG
62 #define RPC_SVC_FG
63 #endif
64
65 #define _RPCSVC_CLOSEDOWN 120
66 int _rpcpmstart; /* Started by a port monitor ? */
67 static int _rpcfdtype;
68 /* Whether Stream or Datagram ? */
69 /* States a server can be in wrt request */
70
71 #define _IDLE 0
72 #define _SERVED 1
73 #define _SERVING 2
74
75 extern int _rpcsvcstate; /* Set when a request is serviced */
76
77 int debug;
78
79 char *progname = "rpc.ypupdated";
80 char *yp_dir = "/var/yp/";
81
82 static void
_msgout(char * msg)83 _msgout(char* msg)
84 {
85 #ifdef RPC_SVC_FG
86 if (_rpcpmstart)
87 syslog(LOG_ERR, "%s", msg);
88 else
89 warnx("%s", msg);
90 #else
91 syslog(LOG_ERR, "%s", msg);
92 #endif
93 }
94
95 static void
closedown(int sig)96 closedown(int sig)
97 {
98 if (_rpcsvcstate == _IDLE) {
99 extern fd_set svc_fdset;
100 static int size;
101 int i, openfd;
102
103 if (_rpcfdtype == SOCK_DGRAM)
104 exit(0);
105 if (size == 0) {
106 size = getdtablesize();
107 }
108 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
109 if (FD_ISSET(i, &svc_fdset))
110 openfd++;
111 if (openfd <= 1)
112 exit(0);
113 }
114 if (_rpcsvcstate == _SERVED)
115 _rpcsvcstate = _IDLE;
116
117 (void) signal(SIGALRM, (SIG_PF) closedown);
118 (void) alarm(_RPCSVC_CLOSEDOWN/2);
119 }
120
121 static void
ypupdated_svc_run(void)122 ypupdated_svc_run(void)
123 {
124 #ifdef FD_SETSIZE
125 fd_set readfds;
126 #else
127 int readfds;
128 #endif /* def FD_SETSIZE */
129 extern int forked;
130 int pid;
131 int fd_setsize = _rpc_dtablesize();
132
133 /* Establish the identity of the parent ypupdated process. */
134 pid = getpid();
135
136 for (;;) {
137 #ifdef FD_SETSIZE
138 readfds = svc_fdset;
139 #else
140 readfds = svc_fds;
141 #endif /* def FD_SETSIZE */
142 switch (select(fd_setsize, &readfds, NULL, NULL,
143 (struct timeval *)0)) {
144 case -1:
145 if (errno == EINTR) {
146 continue;
147 }
148 warn("svc_run: - select failed");
149 return;
150 case 0:
151 continue;
152 default:
153 svc_getreqset(&readfds);
154 if (forked && pid != getpid())
155 exit(0);
156 }
157 }
158 }
159
160 static void
reaper(int sig)161 reaper(int sig)
162 {
163 int status;
164
165 if (sig == SIGHUP) {
166 #ifdef foo
167 load_securenets();
168 #endif
169 return;
170 }
171
172 if (sig == SIGCHLD) {
173 while (wait3(&status, WNOHANG, NULL) > 0)
174 children--;
175 } else {
176 (void) pmap_unset(YPU_PROG, YPU_VERS);
177 exit(0);
178 }
179 }
180
181 void
usage(void)182 usage(void)
183 {
184 fprintf(stderr, "rpc.ypupdatedd [-p path]\n");
185 exit(0);
186 }
187
188 int
main(int argc,char * argv[])189 main(int argc, char *argv[])
190 {
191 register SVCXPRT *transp = NULL;
192 int sock;
193 int proto = 0;
194 struct sockaddr_in saddr;
195 int asize = sizeof (saddr);
196 int ch;
197
198 while ((ch = getopt(argc, argv, "p:h")) != -1) {
199 switch (ch) {
200 case 'p':
201 yp_dir = optarg;
202 break;
203 default:
204 usage();
205 break;
206 }
207 }
208 #ifdef foo
209 load_securenets();
210 #endif
211
212 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) {
213 yp_error("failed to register AUTH_DES flavor");
214 exit(1);
215 }
216
217 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
218 int ssize = sizeof (int);
219
220 if (saddr.sin_family != AF_INET)
221 exit(1);
222 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
223 (char *)&_rpcfdtype, &ssize) == -1)
224 exit(1);
225 sock = 0;
226 _rpcpmstart = 1;
227 proto = 0;
228 openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON);
229 } else {
230 #ifndef RPC_SVC_FG
231 if (daemon(0,0)) {
232 err(1, "cannot fork");
233 }
234 openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON);
235 #endif
236 sock = RPC_ANYSOCK;
237 (void) pmap_unset(YPU_PROG, YPU_VERS);
238 }
239
240 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
241 transp = svcudp_create(sock);
242 if (transp == NULL) {
243 _msgout("cannot create udp service.");
244 exit(1);
245 }
246 if (!_rpcpmstart)
247 proto = IPPROTO_UDP;
248 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
249 _msgout("unable to register (YPU_PROG, YPU_VERS, udp).");
250 exit(1);
251 }
252 }
253
254 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
255 transp = svctcp_create(sock, 0, 0);
256 if (transp == NULL) {
257 _msgout("cannot create tcp service.");
258 exit(1);
259 }
260 if (!_rpcpmstart)
261 proto = IPPROTO_TCP;
262 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
263 _msgout("unable to register (YPU_PROG, YPU_VERS, tcp).");
264 exit(1);
265 }
266 }
267
268 if (transp == (SVCXPRT *)NULL) {
269 _msgout("could not create a handle");
270 exit(1);
271 }
272 if (_rpcpmstart) {
273 (void) signal(SIGALRM, (SIG_PF) closedown);
274 (void) alarm(_RPCSVC_CLOSEDOWN/2);
275 }
276
277 (void) signal(SIGPIPE, SIG_IGN);
278 (void) signal(SIGCHLD, (SIG_PF) reaper);
279 (void) signal(SIGTERM, (SIG_PF) reaper);
280 (void) signal(SIGINT, (SIG_PF) reaper);
281 (void) signal(SIGHUP, (SIG_PF) reaper);
282
283 ypupdated_svc_run();
284 _msgout("svc_run returned");
285 exit(1);
286 /* NOTREACHED */
287 }
288