ypserv.c revision 1.4 1 1.4 christos /* $NetBSD: ypserv.c,v 1.4 1997/04/17 17:46:17 christos 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.4 christos /*
242 1.4 christos * limited NIS version 1 support: the null, domain, and domain_nonack
243 1.4 christos * request/reply format is identical between v1 and v2. SunOS4's ypbind
244 1.4 christos * makes v1 domain_nonack calls.
245 1.4 christos */
246 1.4 christos static void
247 1.4 christos ypprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
248 1.4 christos {
249 1.4 christos switch (rqstp->rq_proc) {
250 1.4 christos case YPPROC_NULL:
251 1.4 christos case YPPROC_DOMAIN:
252 1.4 christos case YPPROC_DOMAIN_NONACK:
253 1.4 christos ypprog_2(rqstp, transp);
254 1.4 christos return;
255 1.4 christos
256 1.4 christos default:
257 1.4 christos svcerr_noproc(transp);
258 1.4 christos _rpcsvcdirty = 0;
259 1.4 christos return;
260 1.4 christos }
261 1.4 christos }
262 1.4 christos
263 1.1 thorpej int
264 1.1 thorpej main(argc, argv)
265 1.1 thorpej int argc;
266 1.1 thorpej char **argv;
267 1.1 thorpej {
268 1.1 thorpej register SVCXPRT *transp;
269 1.1 thorpej int sock, proto;
270 1.1 thorpej struct sockaddr_in saddr;
271 1.1 thorpej int asize = sizeof(saddr);
272 1.1 thorpej int xflag = 0;
273 1.2 ws int ch;
274 1.1 thorpej
275 1.1 thorpej while ((ch = getopt(argc, argv, "a:dx")) != -1) {
276 1.1 thorpej switch (ch) {
277 1.1 thorpej case 'a':
278 1.1 thorpej aclfile = optarg;
279 1.1 thorpej break;
280 1.1 thorpej
281 1.1 thorpej case 'd':
282 1.1 thorpej usedns = 1;
283 1.1 thorpej break;
284 1.1 thorpej
285 1.1 thorpej case 'x':
286 1.1 thorpej xflag = 1;
287 1.1 thorpej break;
288 1.1 thorpej
289 1.1 thorpej default:
290 1.1 thorpej usage();
291 1.1 thorpej }
292 1.1 thorpej }
293 1.1 thorpej
294 1.1 thorpej /* This program must be run by root. */
295 1.1 thorpej if (geteuid() != 0)
296 1.1 thorpej errx(1, "must run as root");
297 1.1 thorpej
298 1.1 thorpej /* Deal with the acl file. */
299 1.1 thorpej acl_parse(aclfile);
300 1.1 thorpej
301 1.1 thorpej if (xflag)
302 1.1 thorpej exit(1);
303 1.1 thorpej
304 1.3 mikel #ifndef RPC_SVC_FG
305 1.3 mikel if (daemon(0, 0))
306 1.3 mikel err(1, "can't detach");
307 1.3 mikel openlog("ypserv", LOG_PID, LOG_DAEMON);
308 1.3 mikel #endif
309 1.3 mikel
310 1.1 thorpej {
311 1.1 thorpej FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
312 1.1 thorpej
313 1.1 thorpej if (pidfile != NULL) {
314 1.1 thorpej fprintf(pidfile, "%d\n", getpid());
315 1.1 thorpej fclose(pidfile);
316 1.1 thorpej } else
317 1.1 thorpej err(1, "can't write PID file");
318 1.1 thorpej }
319 1.1 thorpej
320 1.1 thorpej sock = RPC_ANYSOCK;
321 1.1 thorpej (void) pmap_unset(YPPROG, YPVERS);
322 1.4 christos (void) pmap_unset(YPPROG, YPVERS_ORIG);
323 1.1 thorpej
324 1.1 thorpej ypopenlog(); /* open log file */
325 1.1 thorpej ypdb_init(); /* init db stuff */
326 1.1 thorpej
327 1.1 thorpej (void)signal(SIGCHLD, sig_child);
328 1.1 thorpej (void)signal(SIGHUP, sig_hup);
329 1.1 thorpej
330 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
331 1.1 thorpej transp = svcudp_create(sock);
332 1.1 thorpej if (transp == NULL) {
333 1.1 thorpej _msgout("cannot create udp service.");
334 1.1 thorpej exit(1);
335 1.1 thorpej }
336 1.1 thorpej if (!_rpcpmstart)
337 1.1 thorpej proto = IPPROTO_UDP;
338 1.4 christos if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1, proto)) {
339 1.4 christos _msgout("unable to register (YPPROG, YPVERS_ORIG, udp).");
340 1.4 christos exit(1);
341 1.4 christos }
342 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
343 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, udp).");
344 1.1 thorpej exit(1);
345 1.1 thorpej }
346 1.1 thorpej }
347 1.1 thorpej
348 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
349 1.1 thorpej if (_rpcpmstart)
350 1.1 thorpej transp = svcfd_create(sock, 0, 0);
351 1.1 thorpej else
352 1.1 thorpej transp = svctcp_create(sock, 0, 0);
353 1.1 thorpej if (transp == NULL) {
354 1.1 thorpej _msgout("cannot create tcp service.");
355 1.1 thorpej exit(1);
356 1.1 thorpej }
357 1.1 thorpej if (!_rpcpmstart)
358 1.1 thorpej proto = IPPROTO_TCP;
359 1.4 christos if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1, proto)) {
360 1.4 christos _msgout("unable to register (YPPROG, YPVERS_ORIG, tcp).");
361 1.4 christos exit(1);
362 1.4 christos }
363 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
364 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, tcp).");
365 1.1 thorpej exit(1);
366 1.1 thorpej }
367 1.1 thorpej }
368 1.1 thorpej
369 1.1 thorpej if (transp == (SVCXPRT *)NULL) {
370 1.1 thorpej _msgout("could not create a handle");
371 1.1 thorpej exit(1);
372 1.1 thorpej }
373 1.1 thorpej if (_rpcpmstart) {
374 1.1 thorpej (void) signal(SIGALRM, (SIG_PF) closedown);
375 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
376 1.1 thorpej }
377 1.1 thorpej svc_run();
378 1.1 thorpej _msgout("svc_run returned");
379 1.1 thorpej exit(1);
380 1.1 thorpej /* NOTREACHED */
381 1.1 thorpej }
382 1.1 thorpej
383 1.1 thorpej void
384 1.1 thorpej sig_child()
385 1.1 thorpej {
386 1.1 thorpej
387 1.1 thorpej while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
388 1.1 thorpej }
389 1.1 thorpej
390 1.1 thorpej void
391 1.1 thorpej sig_hup()
392 1.1 thorpej {
393 1.1 thorpej
394 1.1 thorpej acl_reset();
395 1.1 thorpej yplog("sig_hup: reread %s", aclfile ? aclfile : YP_SECURENET_FILE);
396 1.1 thorpej acl_parse(aclfile);
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej void
400 1.1 thorpej usage()
401 1.1 thorpej {
402 1.1 thorpej
403 1.1 thorpej fprintf(stderr, "usage: %s [-a aclfile] [-d] [-x]\n", __progname);
404 1.1 thorpej exit(1);
405 1.1 thorpej }
406