ypserv.c revision 1.8 1 /* $NetBSD: ypserv.c,v 1.8 1999/01/22 02:36:13 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
5 * 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 Mats O Jansson
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: ypserv.c,v 1.8 1999/01/22 02:36:13 thorpej Exp $");
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/wait.h>
42
43 #include <netinet/in.h>
44
45 #include <err.h>
46 #include <netdb.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <syslog.h>
52 #include <unistd.h>
53
54 #include <rpc/rpc.h>
55 #include <rpc/xdr.h>
56 #include <rpc/pmap_clnt.h>
57
58 #include <rpcsvc/yp_prot.h>
59
60 #include "ypdef.h"
61 #include "ypserv.h"
62
63 #ifdef LIBWRAP
64 #include <tcpd.h>
65
66 int allow_severity = LOG_DAEMON | LOG_INFO;
67 int deny_severity = LOG_DAEMON | LOG_WARNING;
68
69 /* XXX For ypserv_proc.c -- NOT THREAD SAFE! (like any of this code is) */
70 const char *clientstr;
71 const char *svcname;
72 #endif /* LIBWRAP */
73
74 #ifdef __STDC__
75 #define SIG_PF void(*)(int)
76 #endif
77
78 #ifdef DEBUG
79 #define RPC_SVC_FG
80 #endif
81
82 #define _RPCSVC_CLOSEDOWN 120
83 static int _rpcpmstart; /* Started by a port monitor ? */
84 static int _rpcfdtype; /* Whether Stream or Datagram ? */
85 static int _rpcsvcdirty; /* Still serving ? */
86
87 int usedns;
88 #ifdef LIBWRAP
89 int lflag;
90 #endif
91
92 extern char *__progname; /* from crt0.s */
93
94 int main __P((int, char *[]));
95 void usage __P((void));
96
97 void sighandler __P((int));
98
99 static void closedown __P((void));
100
101 static
102 void _msgout(char* msg)
103 {
104 #ifdef RPC_SVC_FG
105 if (_rpcpmstart)
106 syslog(LOG_ERR, msg);
107 else
108 warnx("%s", msg);
109 #else
110 syslog(LOG_ERR, msg);
111 #endif
112 }
113
114 static void
115 closedown()
116 {
117 if (_rpcsvcdirty == 0) {
118 extern fd_set svc_fdset;
119 static int size;
120 int i, openfd;
121
122 if (_rpcfdtype == SOCK_DGRAM)
123 exit(0);
124 if (size == 0) {
125 size = getdtablesize();
126 }
127 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
128 if (FD_ISSET(i, &svc_fdset))
129 openfd++;
130 if (openfd <= (_rpcpmstart?0:1))
131 exit(0);
132 }
133 (void) alarm(_RPCSVC_CLOSEDOWN);
134 }
135
136 static void
137 ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
138 {
139 union {
140 char * ypproc_domain_2_arg;
141 char * ypproc_domain_nonack_2_arg;
142 struct ypreq_key ypproc_match_2_arg;
143 struct ypreq_nokey ypproc_first_2_arg;
144 struct ypreq_key ypproc_next_2_arg;
145 struct ypreq_xfr ypproc_xfr_2_arg;
146 struct ypreq_nokey ypproc_all_2_arg;
147 struct ypreq_nokey ypproc_master_2_arg;
148 struct ypreq_nokey ypproc_order_2_arg;
149 char * ypproc_maplist_2_arg;
150 } argument;
151 char *result;
152 xdrproc_t xdr_argument, xdr_result;
153 void *(*local) __P((void *, struct svc_req *));
154 #ifdef LIBWRAP
155 struct request_info req;
156 struct sockaddr_in *caller;
157 #define SVCNAME(x) svcname = x
158 #else
159 #define SVCNAME(x) /* nothing */
160 #endif
161
162 _rpcsvcdirty = 1;
163
164 #ifdef LIBWRAP
165 caller = svc_getcaller(transp);
166 request_init(&req, RQ_DAEMON, __progname, RQ_CLIENT_SIN, caller, NULL);
167 sock_methods(&req);
168 #endif
169
170 switch (rqstp->rq_proc) {
171 case YPPROC_NULL:
172 xdr_argument = xdr_void;
173 xdr_result = xdr_void;
174 local = ypproc_null_2_svc;
175 SVCNAME("null_2");
176 break;
177
178 case YPPROC_DOMAIN:
179 xdr_argument = xdr_ypdomain_wrap_string;
180 xdr_result = xdr_bool;
181 local = ypproc_domain_2_svc;
182 SVCNAME("domain_2");
183 break;
184
185 case YPPROC_DOMAIN_NONACK:
186 xdr_argument = xdr_ypdomain_wrap_string;
187 xdr_result = xdr_bool;
188 local = ypproc_domain_nonack_2_svc;
189 SVCNAME("domain_nonack_2");
190 break;
191
192 case YPPROC_MATCH:
193 xdr_argument = xdr_ypreq_key;
194 xdr_result = xdr_ypresp_val;
195 local = ypproc_match_2_svc;
196 SVCNAME("match_2");
197 break;
198
199 case YPPROC_FIRST:
200 xdr_argument = xdr_ypreq_nokey;
201 xdr_result = xdr_ypresp_key_val;
202 local = ypproc_first_2_svc;
203 SVCNAME("first_2");
204 break;
205
206 case YPPROC_NEXT:
207 xdr_argument = xdr_ypreq_key;
208 xdr_result = xdr_ypresp_key_val;
209 local = ypproc_next_2_svc;
210 SVCNAME("next_2");
211 break;
212
213 case YPPROC_XFR:
214 xdr_argument = xdr_ypreq_xfr;
215 xdr_result = xdr_ypresp_xfr;
216 local = ypproc_xfr_2_svc;
217 SVCNAME("xfer_2");
218 break;
219
220 case YPPROC_CLEAR:
221 xdr_argument = xdr_void;
222 xdr_result = xdr_void;
223 local = ypproc_clear_2_svc;
224 SVCNAME("clear_2");
225 break;
226
227 case YPPROC_ALL:
228 xdr_argument = xdr_ypreq_nokey;
229 xdr_result = xdr_ypresp_all;
230 local = ypproc_all_2_svc;
231 SVCNAME("all_2");
232 break;
233
234 case YPPROC_MASTER:
235 xdr_argument = xdr_ypreq_nokey;
236 xdr_result = xdr_ypresp_master;
237 local = ypproc_master_2_svc;
238 SVCNAME("master_2");
239 break;
240
241 case YPPROC_ORDER:
242 xdr_argument = xdr_ypreq_nokey;
243 xdr_result = xdr_ypresp_order;
244 local = ypproc_order_2_svc;
245 SVCNAME("order_2");
246 break;
247
248 case YPPROC_MAPLIST:
249 xdr_argument = xdr_ypdomain_wrap_string;
250 xdr_result = xdr_ypresp_maplist;
251 local = ypproc_maplist_2_svc;
252 SVCNAME("maplist_2");
253 break;
254
255 default:
256 svcerr_noproc(transp);
257 _rpcsvcdirty = 0;
258 return;
259 }
260
261 #ifdef LIBWRAP
262 clientstr = eval_client(&req);
263
264 if (hosts_access(&req) == 0) {
265 syslog(deny_severity,
266 "%s: refused request from %.500s", svcname, clientstr);
267 svcerr_auth(transp, AUTH_FAILED);
268 _rpcsvcdirty = 0;
269 return;
270 }
271 #endif
272
273 (void) memset((char *)&argument, 0, sizeof (argument));
274 if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
275 svcerr_decode(transp);
276 _rpcsvcdirty = 0;
277 return;
278 }
279 result = (*local)(&argument, rqstp);
280 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
281 svcerr_systemerr(transp);
282 }
283 if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
284 _msgout("unable to free arguments");
285 exit(1);
286 }
287 _rpcsvcdirty = 0;
288 return;
289 }
290
291 /*
292 * limited NIS version 1 support: the null, domain, and domain_nonack
293 * request/reply format is identical between v1 and v2. SunOS4's ypbind
294 * makes v1 domain_nonack calls.
295 */
296 static void
297 ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
298 {
299 switch (rqstp->rq_proc) {
300 case YPPROC_NULL:
301 case YPPROC_DOMAIN:
302 case YPPROC_DOMAIN_NONACK:
303 ypprog_2(rqstp, transp);
304 return;
305
306 default:
307 svcerr_noproc(transp);
308 _rpcsvcdirty = 0;
309 return;
310 }
311 }
312
313 int
314 main(argc, argv)
315 int argc;
316 char *argv[];
317 {
318 SVCXPRT *transp;
319 int sock, proto;
320 struct sigaction sa;
321 int ch;
322
323 transp = NULL; /* XXX gcc -Wuninitialized */
324 proto = 0; /* XXX gcc -Wuninitialized */
325
326 #ifdef LIBWRAP
327 #define GETOPTSTR "dl"
328 #else
329 #define GETOPTSTR "d"
330 #endif
331
332 while ((ch = getopt(argc, argv, GETOPTSTR)) != -1) {
333 switch (ch) {
334 case 'd':
335 usedns = 1;
336 break;
337
338 #ifdef LIBWRAP
339 case 'l':
340 lflag = 1;
341 break;
342 #endif
343 default:
344 usage();
345 }
346 }
347
348 #undef GETOPTSTR
349
350 /* This program must be run by root. */
351 if (geteuid() != 0)
352 errx(1, "must run as root");
353
354 #ifndef RPC_SVC_FG
355 if (daemon(0, 0))
356 err(1, "can't detach");
357 #endif
358
359 openlog(__progname, LOG_PID, LOG_DAEMON);
360 syslog(LOG_INFO, "starting");
361
362 {
363 FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
364
365 if (pidfile != NULL) {
366 fprintf(pidfile, "%d\n", getpid());
367 fclose(pidfile);
368 } else {
369 _msgout("can't write PID file: %m");
370 exit(1);
371 }
372 }
373
374 sock = RPC_ANYSOCK;
375 (void) pmap_unset(YPPROG, YPVERS);
376 (void) pmap_unset(YPPROG, YPVERS_ORIG);
377
378 ypdb_init(); /* init db stuff */
379
380 sa.sa_handler = sighandler;
381 sa.sa_flags = 0;
382 if (sigemptyset(&sa.sa_mask)) {
383 _msgout("sigemptyset: %m");
384 exit(1);
385 }
386 if (sigaction(SIGCHLD, &sa, NULL)) {
387 _msgout("sigaction: %m");
388 exit(1);
389 }
390
391 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
392 transp = svcudp_create(sock);
393 if (transp == NULL) {
394 _msgout("cannot create udp service.");
395 exit(1);
396 }
397 if (transp->xp_port >= IPPORT_RESERVED) {
398 _msgout("udp service not bound to a privileged port.");
399 exit(1);
400 }
401 if (!_rpcpmstart)
402 proto = IPPROTO_UDP;
403 if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
404 proto)) {
405 _msgout(
406 "unable to register (YPPROG, YPVERS_ORIG, udp).");
407 exit(1);
408 }
409 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
410 _msgout("unable to register (YPPROG, YPVERS, udp).");
411 exit(1);
412 }
413 }
414
415 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
416 if (_rpcpmstart)
417 transp = svcfd_create(sock, 0, 0);
418 else
419 transp = svctcp_create(sock, 0, 0);
420 if (transp == NULL) {
421 _msgout("cannot create tcp service.");
422 exit(1);
423 }
424 if (transp->xp_port >= IPPORT_RESERVED) {
425 _msgout("tcp service not bound to a privileged port.");
426 exit(1);
427 }
428 if (!_rpcpmstart)
429 proto = IPPROTO_TCP;
430 if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
431 proto)) {
432 _msgout(
433 "unable to register (YPPROG, YPVERS_ORIG, tcp).");
434 exit(1);
435 }
436 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
437 _msgout("unable to register (YPPROG, YPVERS, tcp).");
438 exit(1);
439 }
440 }
441
442 if (transp == (SVCXPRT *)NULL) {
443 _msgout("could not create a handle");
444 exit(1);
445 }
446 if (_rpcpmstart) {
447 (void) signal(SIGALRM, (SIG_PF) closedown);
448 (void) alarm(_RPCSVC_CLOSEDOWN);
449 }
450 svc_run();
451 _msgout("svc_run returned");
452 exit(1);
453 /* NOTREACHED */
454 }
455
456 void
457 sighandler(sig)
458 int sig;
459 {
460
461 /* SIGCHLD */
462 while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
463 }
464
465 void
466 usage()
467 {
468
469 #ifdef LIBWRAP
470 #define USAGESTR "usage: %s [-d] [-l]\n"
471 #else
472 #define USAGESTR "usage: %s [-d]\n"
473 #endif
474
475 fprintf(stderr, USAGESTR, __progname);
476 exit(1);
477
478 #undef USAGESTR
479 }
480
481
482 /*
483 * _yp_invalid_map: check if given map name isn't legal.
484 * returns non-zero if invalid
485 *
486 * XXX: this probably should be in libc/yp/yplib.c
487 */
488 int
489 _yp_invalid_map(map)
490 const char *map;
491 {
492 if (map == NULL || *map == '\0')
493 return 1;
494
495 if (strlen(map) > YPMAXMAP)
496 return 1;
497
498 if (strchr(map, '/') != NULL)
499 return 1;
500
501 return 0;
502 }
503