ypserv.c revision 1.3 1 1.3 mikel /* $NetBSD: ypserv.c,v 1.3 1997/03/05 07:19:52 mikel Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/types.h>
35 1.1 thorpej #include <sys/socket.h>
36 1.1 thorpej #include <sys/wait.h>
37 1.1 thorpej
38 1.1 thorpej #include <netinet/in.h>
39 1.1 thorpej
40 1.1 thorpej #include <memory.h>
41 1.1 thorpej #include <netdb.h>
42 1.1 thorpej #include <signal.h>
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <stdlib.h>
45 1.1 thorpej #include <string.h>
46 1.1 thorpej
47 1.1 thorpej #include <rpc/rpc.h>
48 1.1 thorpej #include <rpc/xdr.h>
49 1.1 thorpej #include <rpc/pmap_clnt.h>
50 1.1 thorpej
51 1.1 thorpej #include <rpcsvc/yp_prot.h>
52 1.1 thorpej
53 1.1 thorpej #ifdef SYSLOG
54 1.1 thorpej #include <syslog.h>
55 1.1 thorpej #else
56 1.1 thorpej #define LOG_ERR 1
57 1.1 thorpej #define openlog(a, b, c)
58 1.1 thorpej #endif
59 1.1 thorpej
60 1.1 thorpej #include "acl.h"
61 1.1 thorpej #include "yplog.h"
62 1.1 thorpej #include "ypdef.h"
63 1.1 thorpej #include "ypserv.h"
64 1.1 thorpej
65 1.1 thorpej #ifdef __STDC__
66 1.1 thorpej #define SIG_PF void(*)(int)
67 1.1 thorpej #endif
68 1.1 thorpej
69 1.1 thorpej #ifdef DEBUG
70 1.1 thorpej #define RPC_SVC_FG
71 1.1 thorpej #endif
72 1.1 thorpej
73 1.1 thorpej #define _RPCSVC_CLOSEDOWN 120
74 1.1 thorpej static int _rpcpmstart; /* Started by a port monitor ? */
75 1.1 thorpej static int _rpcfdtype; /* Whether Stream or Datagram ? */
76 1.1 thorpej static int _rpcsvcdirty; /* Still serving ? */
77 1.1 thorpej
78 1.1 thorpej int usedns;
79 1.1 thorpej char *aclfile;
80 1.1 thorpej
81 1.1 thorpej extern char *__progname; /* from crt0.s */
82 1.1 thorpej extern char *optarg;
83 1.1 thorpej extern int optind;
84 1.1 thorpej
85 1.1 thorpej void usage __P((void));
86 1.1 thorpej
87 1.1 thorpej void sig_child();
88 1.1 thorpej void sig_hup();
89 1.1 thorpej
90 1.1 thorpej static
91 1.1 thorpej void _msgout(char* msg)
92 1.1 thorpej {
93 1.1 thorpej #ifdef RPC_SVC_FG
94 1.1 thorpej if (_rpcpmstart)
95 1.1 thorpej syslog(LOG_ERR, msg);
96 1.1 thorpej else
97 1.1 thorpej warnx("%s", msg);
98 1.1 thorpej #else
99 1.1 thorpej syslog(LOG_ERR, msg);
100 1.1 thorpej #endif
101 1.1 thorpej }
102 1.1 thorpej
103 1.1 thorpej static void
104 1.1 thorpej closedown()
105 1.1 thorpej {
106 1.1 thorpej if (_rpcsvcdirty == 0) {
107 1.1 thorpej extern fd_set svc_fdset;
108 1.1 thorpej static int size;
109 1.1 thorpej int i, openfd;
110 1.1 thorpej
111 1.1 thorpej if (_rpcfdtype == SOCK_DGRAM)
112 1.1 thorpej exit(0);
113 1.1 thorpej if (size == 0) {
114 1.1 thorpej size = getdtablesize();
115 1.1 thorpej }
116 1.1 thorpej for (i = 0, openfd = 0; i < size && openfd < 2; i++)
117 1.1 thorpej if (FD_ISSET(i, &svc_fdset))
118 1.1 thorpej openfd++;
119 1.1 thorpej if (openfd <= (_rpcpmstart?0:1))
120 1.1 thorpej exit(0);
121 1.1 thorpej }
122 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
123 1.1 thorpej }
124 1.1 thorpej
125 1.1 thorpej static void
126 1.1 thorpej ypprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
127 1.1 thorpej {
128 1.1 thorpej union {
129 1.1 thorpej char * ypproc_domain_2_arg;
130 1.1 thorpej char * ypproc_domain_nonack_2_arg;
131 1.1 thorpej struct ypreq_key ypproc_match_2_arg;
132 1.1 thorpej struct ypreq_nokey ypproc_first_2_arg;
133 1.1 thorpej struct ypreq_key ypproc_next_2_arg;
134 1.1 thorpej struct ypreq_xfr ypproc_xfr_2_arg;
135 1.1 thorpej struct ypreq_nokey ypproc_all_2_arg;
136 1.1 thorpej struct ypreq_nokey ypproc_master_2_arg;
137 1.1 thorpej struct ypreq_nokey ypproc_order_2_arg;
138 1.1 thorpej char * ypproc_maplist_2_arg;
139 1.1 thorpej } argument;
140 1.1 thorpej char *result;
141 1.1 thorpej xdrproc_t xdr_argument, xdr_result;
142 1.1 thorpej void *(*local) __P((void *, struct svc_req *));
143 1.1 thorpej
144 1.1 thorpej _rpcsvcdirty = 1;
145 1.1 thorpej switch (rqstp->rq_proc) {
146 1.1 thorpej case YPPROC_NULL:
147 1.1 thorpej xdr_argument = xdr_void;
148 1.1 thorpej xdr_result = xdr_void;
149 1.1 thorpej local = ypproc_null_2_svc;
150 1.1 thorpej break;
151 1.1 thorpej
152 1.1 thorpej case YPPROC_DOMAIN:
153 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
154 1.1 thorpej xdr_result = xdr_bool;
155 1.1 thorpej local = ypproc_domain_2_svc;
156 1.1 thorpej break;
157 1.1 thorpej
158 1.1 thorpej case YPPROC_DOMAIN_NONACK:
159 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
160 1.1 thorpej xdr_result = xdr_bool;
161 1.1 thorpej local = ypproc_domain_nonack_2_svc;
162 1.1 thorpej break;
163 1.1 thorpej
164 1.1 thorpej case YPPROC_MATCH:
165 1.1 thorpej xdr_argument = xdr_ypreq_key;
166 1.1 thorpej xdr_result = xdr_ypresp_val;
167 1.1 thorpej local = ypproc_match_2_svc;
168 1.1 thorpej break;
169 1.1 thorpej
170 1.1 thorpej case YPPROC_FIRST:
171 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
172 1.1 thorpej xdr_result = xdr_ypresp_key_val;
173 1.1 thorpej local = ypproc_first_2_svc;
174 1.1 thorpej break;
175 1.1 thorpej
176 1.1 thorpej case YPPROC_NEXT:
177 1.1 thorpej xdr_argument = xdr_ypreq_key;
178 1.1 thorpej xdr_result = xdr_ypresp_key_val;
179 1.1 thorpej local = ypproc_next_2_svc;
180 1.1 thorpej break;
181 1.1 thorpej
182 1.1 thorpej case YPPROC_XFR:
183 1.1 thorpej xdr_argument = xdr_ypreq_xfr;
184 1.1 thorpej xdr_result = xdr_ypresp_xfr;
185 1.1 thorpej local = ypproc_xfr_2_svc;
186 1.1 thorpej break;
187 1.1 thorpej
188 1.1 thorpej case YPPROC_CLEAR:
189 1.1 thorpej xdr_argument = xdr_void;
190 1.1 thorpej xdr_result = xdr_void;
191 1.1 thorpej local = ypproc_clear_2_svc;
192 1.1 thorpej break;
193 1.1 thorpej
194 1.1 thorpej case YPPROC_ALL:
195 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
196 1.1 thorpej xdr_result = xdr_ypresp_all;
197 1.1 thorpej local = ypproc_all_2_svc;
198 1.1 thorpej break;
199 1.1 thorpej
200 1.1 thorpej case YPPROC_MASTER:
201 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
202 1.1 thorpej xdr_result = xdr_ypresp_master;
203 1.1 thorpej local = ypproc_master_2_svc;
204 1.1 thorpej break;
205 1.1 thorpej
206 1.1 thorpej case YPPROC_ORDER:
207 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
208 1.1 thorpej xdr_result = xdr_ypresp_order;
209 1.1 thorpej local = ypproc_order_2_svc;
210 1.1 thorpej break;
211 1.1 thorpej
212 1.1 thorpej case YPPROC_MAPLIST:
213 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
214 1.1 thorpej xdr_result = xdr_ypresp_maplist;
215 1.1 thorpej local = ypproc_maplist_2_svc;
216 1.1 thorpej break;
217 1.1 thorpej
218 1.1 thorpej default:
219 1.1 thorpej svcerr_noproc(transp);
220 1.1 thorpej _rpcsvcdirty = 0;
221 1.1 thorpej return;
222 1.1 thorpej }
223 1.1 thorpej (void) memset((char *)&argument, 0, sizeof (argument));
224 1.1 thorpej if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
225 1.1 thorpej svcerr_decode(transp);
226 1.1 thorpej _rpcsvcdirty = 0;
227 1.1 thorpej return;
228 1.1 thorpej }
229 1.1 thorpej result = (*local)(&argument, rqstp);
230 1.1 thorpej if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
231 1.1 thorpej svcerr_systemerr(transp);
232 1.1 thorpej }
233 1.1 thorpej if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
234 1.1 thorpej _msgout("unable to free arguments");
235 1.1 thorpej exit(1);
236 1.1 thorpej }
237 1.1 thorpej _rpcsvcdirty = 0;
238 1.1 thorpej return;
239 1.1 thorpej }
240 1.1 thorpej
241 1.1 thorpej int
242 1.1 thorpej main(argc, argv)
243 1.1 thorpej int argc;
244 1.1 thorpej char **argv;
245 1.1 thorpej {
246 1.1 thorpej register SVCXPRT *transp;
247 1.1 thorpej int sock, proto;
248 1.1 thorpej struct sockaddr_in saddr;
249 1.1 thorpej int asize = sizeof(saddr);
250 1.1 thorpej int xflag = 0;
251 1.2 ws int ch;
252 1.1 thorpej
253 1.1 thorpej while ((ch = getopt(argc, argv, "a:dx")) != -1) {
254 1.1 thorpej switch (ch) {
255 1.1 thorpej case 'a':
256 1.1 thorpej aclfile = optarg;
257 1.1 thorpej break;
258 1.1 thorpej
259 1.1 thorpej case 'd':
260 1.1 thorpej usedns = 1;
261 1.1 thorpej break;
262 1.1 thorpej
263 1.1 thorpej case 'x':
264 1.1 thorpej xflag = 1;
265 1.1 thorpej break;
266 1.1 thorpej
267 1.1 thorpej default:
268 1.1 thorpej usage();
269 1.1 thorpej }
270 1.1 thorpej }
271 1.1 thorpej
272 1.1 thorpej /* This program must be run by root. */
273 1.1 thorpej if (geteuid() != 0)
274 1.1 thorpej errx(1, "must run as root");
275 1.1 thorpej
276 1.1 thorpej /* Deal with the acl file. */
277 1.1 thorpej acl_parse(aclfile);
278 1.1 thorpej
279 1.1 thorpej if (xflag)
280 1.1 thorpej exit(1);
281 1.1 thorpej
282 1.3 mikel #ifndef RPC_SVC_FG
283 1.3 mikel if (daemon(0, 0))
284 1.3 mikel err(1, "can't detach");
285 1.3 mikel openlog("ypserv", LOG_PID, LOG_DAEMON);
286 1.3 mikel #endif
287 1.3 mikel
288 1.1 thorpej {
289 1.1 thorpej FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
290 1.1 thorpej
291 1.1 thorpej if (pidfile != NULL) {
292 1.1 thorpej fprintf(pidfile, "%d\n", getpid());
293 1.1 thorpej fclose(pidfile);
294 1.1 thorpej } else
295 1.1 thorpej err(1, "can't write PID file");
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej sock = RPC_ANYSOCK;
299 1.1 thorpej (void) pmap_unset(YPPROG, YPVERS);
300 1.1 thorpej
301 1.1 thorpej ypopenlog(); /* open log file */
302 1.1 thorpej ypdb_init(); /* init db stuff */
303 1.1 thorpej
304 1.1 thorpej (void)signal(SIGCHLD, sig_child);
305 1.1 thorpej (void)signal(SIGHUP, sig_hup);
306 1.1 thorpej
307 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
308 1.1 thorpej transp = svcudp_create(sock);
309 1.1 thorpej if (transp == NULL) {
310 1.1 thorpej _msgout("cannot create udp service.");
311 1.1 thorpej exit(1);
312 1.1 thorpej }
313 1.1 thorpej if (!_rpcpmstart)
314 1.1 thorpej proto = IPPROTO_UDP;
315 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
316 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, udp).");
317 1.1 thorpej exit(1);
318 1.1 thorpej }
319 1.1 thorpej }
320 1.1 thorpej
321 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
322 1.1 thorpej if (_rpcpmstart)
323 1.1 thorpej transp = svcfd_create(sock, 0, 0);
324 1.1 thorpej else
325 1.1 thorpej transp = svctcp_create(sock, 0, 0);
326 1.1 thorpej if (transp == NULL) {
327 1.1 thorpej _msgout("cannot create tcp service.");
328 1.1 thorpej exit(1);
329 1.1 thorpej }
330 1.1 thorpej if (!_rpcpmstart)
331 1.1 thorpej proto = IPPROTO_TCP;
332 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
333 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, tcp).");
334 1.1 thorpej exit(1);
335 1.1 thorpej }
336 1.1 thorpej }
337 1.1 thorpej
338 1.1 thorpej if (transp == (SVCXPRT *)NULL) {
339 1.1 thorpej _msgout("could not create a handle");
340 1.1 thorpej exit(1);
341 1.1 thorpej }
342 1.1 thorpej if (_rpcpmstart) {
343 1.1 thorpej (void) signal(SIGALRM, (SIG_PF) closedown);
344 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
345 1.1 thorpej }
346 1.1 thorpej svc_run();
347 1.1 thorpej _msgout("svc_run returned");
348 1.1 thorpej exit(1);
349 1.1 thorpej /* NOTREACHED */
350 1.1 thorpej }
351 1.1 thorpej
352 1.1 thorpej void
353 1.1 thorpej sig_child()
354 1.1 thorpej {
355 1.1 thorpej
356 1.1 thorpej while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
357 1.1 thorpej }
358 1.1 thorpej
359 1.1 thorpej void
360 1.1 thorpej sig_hup()
361 1.1 thorpej {
362 1.1 thorpej
363 1.1 thorpej acl_reset();
364 1.1 thorpej yplog("sig_hup: reread %s", aclfile ? aclfile : YP_SECURENET_FILE);
365 1.1 thorpej acl_parse(aclfile);
366 1.1 thorpej }
367 1.1 thorpej
368 1.1 thorpej void
369 1.1 thorpej usage()
370 1.1 thorpej {
371 1.1 thorpej
372 1.1 thorpej fprintf(stderr, "usage: %s [-a aclfile] [-d] [-x]\n", __progname);
373 1.1 thorpej exit(1);
374 1.1 thorpej }
375