ypserv.c revision 1.7 1 1.7 lukem /* $NetBSD: ypserv.c,v 1.7 1997/10/15 05:01:24 lukem 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.7 lukem #include <sys/cdefs.h>
35 1.7 lukem #ifndef lint
36 1.7 lukem __RCSID("$NetBSD: ypserv.c,v 1.7 1997/10/15 05:01:24 lukem Exp $");
37 1.7 lukem #endif
38 1.7 lukem
39 1.1 thorpej #include <sys/types.h>
40 1.1 thorpej #include <sys/socket.h>
41 1.1 thorpej #include <sys/wait.h>
42 1.1 thorpej
43 1.1 thorpej #include <netinet/in.h>
44 1.1 thorpej
45 1.5 thorpej #include <err.h>
46 1.1 thorpej #include <netdb.h>
47 1.1 thorpej #include <signal.h>
48 1.1 thorpej #include <stdio.h>
49 1.1 thorpej #include <stdlib.h>
50 1.1 thorpej #include <string.h>
51 1.5 thorpej #include <syslog.h>
52 1.5 thorpej #include <unistd.h>
53 1.1 thorpej
54 1.1 thorpej #include <rpc/rpc.h>
55 1.1 thorpej #include <rpc/xdr.h>
56 1.1 thorpej #include <rpc/pmap_clnt.h>
57 1.1 thorpej
58 1.1 thorpej #include <rpcsvc/yp_prot.h>
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
83 1.5 thorpej int main __P((int, char *[]));
84 1.1 thorpej void usage __P((void));
85 1.1 thorpej
86 1.5 thorpej void sighandler __P((int));
87 1.5 thorpej
88 1.5 thorpej static void closedown __P((void));
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.6 lukem ypprog_2(struct svc_req *rqstp, 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.6 lukem ypprog_1(struct svc_req *rqstp, 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.5 thorpej char *argv[];
267 1.1 thorpej {
268 1.6 lukem SVCXPRT *transp;
269 1.1 thorpej int sock, proto;
270 1.5 thorpej struct sigaction sa;
271 1.1 thorpej int xflag = 0;
272 1.2 ws int ch;
273 1.5 thorpej
274 1.5 thorpej transp = NULL; /* XXX gcc -Wuninitialized */
275 1.5 thorpej proto = 0; /* XXX gcc -Wuninitialized */
276 1.5 thorpej
277 1.1 thorpej while ((ch = getopt(argc, argv, "a:dx")) != -1) {
278 1.1 thorpej switch (ch) {
279 1.1 thorpej case 'a':
280 1.1 thorpej aclfile = optarg;
281 1.1 thorpej break;
282 1.1 thorpej
283 1.1 thorpej case 'd':
284 1.1 thorpej usedns = 1;
285 1.1 thorpej break;
286 1.1 thorpej
287 1.1 thorpej case 'x':
288 1.1 thorpej xflag = 1;
289 1.1 thorpej break;
290 1.1 thorpej
291 1.1 thorpej default:
292 1.1 thorpej usage();
293 1.1 thorpej }
294 1.1 thorpej }
295 1.1 thorpej
296 1.1 thorpej /* This program must be run by root. */
297 1.1 thorpej if (geteuid() != 0)
298 1.1 thorpej errx(1, "must run as root");
299 1.1 thorpej
300 1.1 thorpej /* Deal with the acl file. */
301 1.1 thorpej acl_parse(aclfile);
302 1.1 thorpej
303 1.1 thorpej if (xflag)
304 1.1 thorpej exit(1);
305 1.1 thorpej
306 1.3 mikel #ifndef RPC_SVC_FG
307 1.3 mikel if (daemon(0, 0))
308 1.3 mikel err(1, "can't detach");
309 1.3 mikel openlog("ypserv", LOG_PID, LOG_DAEMON);
310 1.3 mikel #endif
311 1.3 mikel
312 1.1 thorpej {
313 1.1 thorpej FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
314 1.1 thorpej
315 1.1 thorpej if (pidfile != NULL) {
316 1.1 thorpej fprintf(pidfile, "%d\n", getpid());
317 1.1 thorpej fclose(pidfile);
318 1.1 thorpej } else
319 1.1 thorpej err(1, "can't write PID file");
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej sock = RPC_ANYSOCK;
323 1.1 thorpej (void) pmap_unset(YPPROG, YPVERS);
324 1.4 christos (void) pmap_unset(YPPROG, YPVERS_ORIG);
325 1.1 thorpej
326 1.1 thorpej ypopenlog(); /* open log file */
327 1.1 thorpej ypdb_init(); /* init db stuff */
328 1.1 thorpej
329 1.5 thorpej sa.sa_handler = sighandler;
330 1.5 thorpej sa.sa_flags = 0;
331 1.5 thorpej if (sigemptyset(&sa.sa_mask))
332 1.5 thorpej err(1, "sigemptyset");
333 1.5 thorpej if (sigaction(SIGCHLD, &sa, NULL) || sigaction(SIGHUP, &sa, NULL))
334 1.5 thorpej err(1, "sigaction");
335 1.1 thorpej
336 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
337 1.1 thorpej transp = svcudp_create(sock);
338 1.1 thorpej if (transp == NULL) {
339 1.1 thorpej _msgout("cannot create udp service.");
340 1.1 thorpej exit(1);
341 1.1 thorpej }
342 1.6 lukem if (transp->xp_port >= IPPORT_RESERVED) {
343 1.6 lukem _msgout("udp service not bound to a privileged port.");
344 1.6 lukem exit(1);
345 1.6 lukem }
346 1.1 thorpej if (!_rpcpmstart)
347 1.1 thorpej proto = IPPROTO_UDP;
348 1.6 lukem if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
349 1.6 lukem proto)) {
350 1.6 lukem _msgout(
351 1.6 lukem "unable to register (YPPROG, YPVERS_ORIG, udp).");
352 1.4 christos exit(1);
353 1.4 christos }
354 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
355 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, udp).");
356 1.1 thorpej exit(1);
357 1.1 thorpej }
358 1.1 thorpej }
359 1.1 thorpej
360 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
361 1.1 thorpej if (_rpcpmstart)
362 1.1 thorpej transp = svcfd_create(sock, 0, 0);
363 1.1 thorpej else
364 1.1 thorpej transp = svctcp_create(sock, 0, 0);
365 1.1 thorpej if (transp == NULL) {
366 1.1 thorpej _msgout("cannot create tcp service.");
367 1.1 thorpej exit(1);
368 1.1 thorpej }
369 1.6 lukem if (transp->xp_port >= IPPORT_RESERVED) {
370 1.6 lukem _msgout("tcp service not bound to a privileged port.");
371 1.6 lukem exit(1);
372 1.6 lukem }
373 1.1 thorpej if (!_rpcpmstart)
374 1.1 thorpej proto = IPPROTO_TCP;
375 1.6 lukem if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
376 1.6 lukem proto)) {
377 1.6 lukem _msgout(
378 1.6 lukem "unable to register (YPPROG, YPVERS_ORIG, tcp).");
379 1.4 christos exit(1);
380 1.4 christos }
381 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
382 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, tcp).");
383 1.1 thorpej exit(1);
384 1.1 thorpej }
385 1.1 thorpej }
386 1.1 thorpej
387 1.1 thorpej if (transp == (SVCXPRT *)NULL) {
388 1.1 thorpej _msgout("could not create a handle");
389 1.1 thorpej exit(1);
390 1.1 thorpej }
391 1.1 thorpej if (_rpcpmstart) {
392 1.1 thorpej (void) signal(SIGALRM, (SIG_PF) closedown);
393 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
394 1.1 thorpej }
395 1.1 thorpej svc_run();
396 1.1 thorpej _msgout("svc_run returned");
397 1.1 thorpej exit(1);
398 1.1 thorpej /* NOTREACHED */
399 1.1 thorpej }
400 1.1 thorpej
401 1.1 thorpej void
402 1.5 thorpej sighandler(sig)
403 1.5 thorpej int sig;
404 1.1 thorpej {
405 1.1 thorpej
406 1.5 thorpej if (sig == SIGHUP) {
407 1.5 thorpej acl_reset();
408 1.5 thorpej yplog("reread %s", aclfile ? aclfile : YP_SECURENET_FILE);
409 1.5 thorpej acl_parse(aclfile);
410 1.5 thorpej return;
411 1.5 thorpej }
412 1.5 thorpej
413 1.5 thorpej /* SIGCHLD */
414 1.1 thorpej while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
415 1.1 thorpej }
416 1.1 thorpej
417 1.1 thorpej void
418 1.1 thorpej usage()
419 1.1 thorpej {
420 1.1 thorpej
421 1.1 thorpej fprintf(stderr, "usage: %s [-a aclfile] [-d] [-x]\n", __progname);
422 1.1 thorpej exit(1);
423 1.7 lukem }
424 1.7 lukem
425 1.7 lukem
426 1.7 lukem /*
427 1.7 lukem * _yp_invalid_map: check if given map name isn't legal.
428 1.7 lukem * returns non-zero if invalid
429 1.7 lukem *
430 1.7 lukem * XXX: this probably should be in libc/yp/yplib.c
431 1.7 lukem */
432 1.7 lukem int
433 1.7 lukem _yp_invalid_map(map)
434 1.7 lukem const char *map;
435 1.7 lukem {
436 1.7 lukem if (map == NULL || *map == '\0')
437 1.7 lukem return 1;
438 1.7 lukem
439 1.7 lukem if (strlen(map) > YPMAXMAP)
440 1.7 lukem return 1;
441 1.7 lukem
442 1.7 lukem if (strchr(map, '/') != NULL)
443 1.7 lukem return 1;
444 1.7 lukem
445 1.7 lukem return 0;
446 1.1 thorpej }
447