ypserv.c revision 1.9 1 /* $NetBSD: ypserv.c,v 1.9 1999/02/18 06:03:47 abs 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.9 1999/02/18 06:03:47 abs 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 #define _RPCSVC_CLOSEDOWN 120
79 static int _rpcpmstart; /* Started by a port monitor ? */
80 static int _rpcfdtype; /* Whether Stream or Datagram ? */
81 static int _rpcsvcdirty; /* Still serving ? */
82
83 int usedns;
84 #ifdef DEBUG
85 int foreground = 1;
86 #else
87 int foreground;
88 #endif
89
90 #ifdef LIBWRAP
91 int lflag;
92 #endif
93
94 extern char *__progname; /* from crt0.s */
95
96 int main __P((int, char *[]));
97 void usage __P((void));
98
99 void sighandler __P((int));
100
101 static void closedown __P((void));
102
103 static
104 void _msgout(char* msg)
105 {
106 if (foreground && ! _rpcpmstart)
107 warnx("%s", msg);
108 else
109 syslog(LOG_ERR, msg);
110 }
111
112 static void
113 closedown()
114 {
115 if (_rpcsvcdirty == 0) {
116 extern fd_set svc_fdset;
117 static int size;
118 int i, openfd;
119
120 if (_rpcfdtype == SOCK_DGRAM)
121 exit(0);
122 if (size == 0) {
123 size = getdtablesize();
124 }
125 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
126 if (FD_ISSET(i, &svc_fdset))
127 openfd++;
128 if (openfd <= (_rpcpmstart?0:1))
129 exit(0);
130 }
131 (void) alarm(_RPCSVC_CLOSEDOWN);
132 }
133
134 static void
135 ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
136 {
137 union {
138 char * ypproc_domain_2_arg;
139 char * ypproc_domain_nonack_2_arg;
140 struct ypreq_key ypproc_match_2_arg;
141 struct ypreq_nokey ypproc_first_2_arg;
142 struct ypreq_key ypproc_next_2_arg;
143 struct ypreq_xfr ypproc_xfr_2_arg;
144 struct ypreq_nokey ypproc_all_2_arg;
145 struct ypreq_nokey ypproc_master_2_arg;
146 struct ypreq_nokey ypproc_order_2_arg;
147 char * ypproc_maplist_2_arg;
148 } argument;
149 char *result;
150 xdrproc_t xdr_argument, xdr_result;
151 void *(*local) __P((void *, struct svc_req *));
152 #ifdef LIBWRAP
153 struct request_info req;
154 struct sockaddr_in *caller;
155 #define SVCNAME(x) svcname = x
156 #else
157 #define SVCNAME(x) /* nothing */
158 #endif
159
160 _rpcsvcdirty = 1;
161
162 #ifdef LIBWRAP
163 caller = svc_getcaller(transp);
164 request_init(&req, RQ_DAEMON, __progname, RQ_CLIENT_SIN, caller, NULL);
165 sock_methods(&req);
166 #endif
167
168 switch (rqstp->rq_proc) {
169 case YPPROC_NULL:
170 xdr_argument = xdr_void;
171 xdr_result = xdr_void;
172 local = ypproc_null_2_svc;
173 SVCNAME("null_2");
174 break;
175
176 case YPPROC_DOMAIN:
177 xdr_argument = xdr_ypdomain_wrap_string;
178 xdr_result = xdr_bool;
179 local = ypproc_domain_2_svc;
180 SVCNAME("domain_2");
181 break;
182
183 case YPPROC_DOMAIN_NONACK:
184 xdr_argument = xdr_ypdomain_wrap_string;
185 xdr_result = xdr_bool;
186 local = ypproc_domain_nonack_2_svc;
187 SVCNAME("domain_nonack_2");
188 break;
189
190 case YPPROC_MATCH:
191 xdr_argument = xdr_ypreq_key;
192 xdr_result = xdr_ypresp_val;
193 local = ypproc_match_2_svc;
194 SVCNAME("match_2");
195 break;
196
197 case YPPROC_FIRST:
198 xdr_argument = xdr_ypreq_nokey;
199 xdr_result = xdr_ypresp_key_val;
200 local = ypproc_first_2_svc;
201 SVCNAME("first_2");
202 break;
203
204 case YPPROC_NEXT:
205 xdr_argument = xdr_ypreq_key;
206 xdr_result = xdr_ypresp_key_val;
207 local = ypproc_next_2_svc;
208 SVCNAME("next_2");
209 break;
210
211 case YPPROC_XFR:
212 xdr_argument = xdr_ypreq_xfr;
213 xdr_result = xdr_ypresp_xfr;
214 local = ypproc_xfr_2_svc;
215 SVCNAME("xfer_2");
216 break;
217
218 case YPPROC_CLEAR:
219 xdr_argument = xdr_void;
220 xdr_result = xdr_void;
221 local = ypproc_clear_2_svc;
222 SVCNAME("clear_2");
223 break;
224
225 case YPPROC_ALL:
226 xdr_argument = xdr_ypreq_nokey;
227 xdr_result = xdr_ypresp_all;
228 local = ypproc_all_2_svc;
229 SVCNAME("all_2");
230 break;
231
232 case YPPROC_MASTER:
233 xdr_argument = xdr_ypreq_nokey;
234 xdr_result = xdr_ypresp_master;
235 local = ypproc_master_2_svc;
236 SVCNAME("master_2");
237 break;
238
239 case YPPROC_ORDER:
240 xdr_argument = xdr_ypreq_nokey;
241 xdr_result = xdr_ypresp_order;
242 local = ypproc_order_2_svc;
243 SVCNAME("order_2");
244 break;
245
246 case YPPROC_MAPLIST:
247 xdr_argument = xdr_ypdomain_wrap_string;
248 xdr_result = xdr_ypresp_maplist;
249 local = ypproc_maplist_2_svc;
250 SVCNAME("maplist_2");
251 break;
252
253 default:
254 svcerr_noproc(transp);
255 _rpcsvcdirty = 0;
256 return;
257 }
258
259 #ifdef LIBWRAP
260 clientstr = eval_client(&req);
261
262 if (hosts_access(&req) == 0) {
263 syslog(deny_severity,
264 "%s: refused request from %.500s", svcname, clientstr);
265 svcerr_auth(transp, AUTH_FAILED);
266 _rpcsvcdirty = 0;
267 return;
268 }
269 #endif
270
271 (void) memset((char *)&argument, 0, sizeof (argument));
272 if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
273 svcerr_decode(transp);
274 _rpcsvcdirty = 0;
275 return;
276 }
277 result = (*local)(&argument, rqstp);
278 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
279 svcerr_systemerr(transp);
280 }
281 if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
282 _msgout("unable to free arguments");
283 exit(1);
284 }
285 _rpcsvcdirty = 0;
286 return;
287 }
288
289 /*
290 * limited NIS version 1 support: the null, domain, and domain_nonack
291 * request/reply format is identical between v1 and v2. SunOS4's ypbind
292 * makes v1 domain_nonack calls.
293 */
294 static void
295 ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
296 {
297 switch (rqstp->rq_proc) {
298 case YPPROC_NULL:
299 case YPPROC_DOMAIN:
300 case YPPROC_DOMAIN_NONACK:
301 ypprog_2(rqstp, transp);
302 return;
303
304 default:
305 svcerr_noproc(transp);
306 _rpcsvcdirty = 0;
307 return;
308 }
309 }
310
311 int
312 main(argc, argv)
313 int argc;
314 char *argv[];
315 {
316 SVCXPRT *transp;
317 int sock, proto;
318 struct sigaction sa;
319 int ch;
320
321 transp = NULL; /* XXX gcc -Wuninitialized */
322 proto = 0; /* XXX gcc -Wuninitialized */
323
324 #ifdef LIBWRAP
325 #define GETOPTSTR "dfl"
326 #else
327 #define GETOPTSTR "df"
328 #endif
329
330 while ((ch = getopt(argc, argv, GETOPTSTR)) != -1) {
331 switch (ch) {
332 case 'd':
333 usedns = 1;
334 break;
335 case 'f':
336 foreground = 1;
337 break;
338
339 #ifdef LIBWRAP
340 case 'l':
341 lflag = 1;
342 break;
343 #endif
344 default:
345 usage();
346 }
347 }
348
349 #undef GETOPTSTR
350
351 /* This program must be run by root. */
352 if (geteuid() != 0)
353 errx(1, "must run as root");
354
355 if (!foreground && daemon(0, 0))
356 err(1, "can't detach");
357
358 openlog(__progname, LOG_PID, LOG_DAEMON);
359 syslog(LOG_INFO, "starting");
360
361 {
362 FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
363
364 if (pidfile != NULL) {
365 fprintf(pidfile, "%d\n", getpid());
366 fclose(pidfile);
367 } else {
368 _msgout("can't write PID file: %m");
369 exit(1);
370 }
371 }
372
373 sock = RPC_ANYSOCK;
374 (void) pmap_unset(YPPROG, YPVERS);
375 (void) pmap_unset(YPPROG, YPVERS_ORIG);
376
377 ypdb_init(); /* init db stuff */
378
379 sa.sa_handler = sighandler;
380 sa.sa_flags = 0;
381 if (sigemptyset(&sa.sa_mask)) {
382 _msgout("sigemptyset: %m");
383 exit(1);
384 }
385 if (sigaction(SIGCHLD, &sa, NULL)) {
386 _msgout("sigaction: %m");
387 exit(1);
388 }
389
390 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
391 transp = svcudp_create(sock);
392 if (transp == NULL) {
393 _msgout("cannot create udp service.");
394 exit(1);
395 }
396 if (transp->xp_port >= IPPORT_RESERVED) {
397 _msgout("udp service not bound to a privileged port.");
398 exit(1);
399 }
400 if (!_rpcpmstart)
401 proto = IPPROTO_UDP;
402 if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
403 proto)) {
404 _msgout(
405 "unable to register (YPPROG, YPVERS_ORIG, udp).");
406 exit(1);
407 }
408 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
409 _msgout("unable to register (YPPROG, YPVERS, udp).");
410 exit(1);
411 }
412 }
413
414 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
415 if (_rpcpmstart)
416 transp = svcfd_create(sock, 0, 0);
417 else
418 transp = svctcp_create(sock, 0, 0);
419 if (transp == NULL) {
420 _msgout("cannot create tcp service.");
421 exit(1);
422 }
423 if (transp->xp_port >= IPPORT_RESERVED) {
424 _msgout("tcp service not bound to a privileged port.");
425 exit(1);
426 }
427 if (!_rpcpmstart)
428 proto = IPPROTO_TCP;
429 if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
430 proto)) {
431 _msgout(
432 "unable to register (YPPROG, YPVERS_ORIG, tcp).");
433 exit(1);
434 }
435 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
436 _msgout("unable to register (YPPROG, YPVERS, tcp).");
437 exit(1);
438 }
439 }
440
441 if (transp == (SVCXPRT *)NULL) {
442 _msgout("could not create a handle");
443 exit(1);
444 }
445 if (_rpcpmstart) {
446 (void) signal(SIGALRM, (SIG_PF) closedown);
447 (void) alarm(_RPCSVC_CLOSEDOWN);
448 }
449 svc_run();
450 _msgout("svc_run returned");
451 exit(1);
452 /* NOTREACHED */
453 }
454
455 void
456 sighandler(sig)
457 int sig;
458 {
459
460 /* SIGCHLD */
461 while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
462 }
463
464 void
465 usage()
466 {
467
468 #ifdef LIBWRAP
469 #define USAGESTR "usage: %s [-d] [-l]\n"
470 #else
471 #define USAGESTR "usage: %s [-d]\n"
472 #endif
473
474 fprintf(stderr, USAGESTR, __progname);
475 exit(1);
476
477 #undef USAGESTR
478 }
479
480
481 /*
482 * _yp_invalid_map: check if given map name isn't legal.
483 * returns non-zero if invalid
484 *
485 * XXX: this probably should be in libc/yp/yplib.c
486 */
487 int
488 _yp_invalid_map(map)
489 const char *map;
490 {
491 if (map == NULL || *map == '\0')
492 return 1;
493
494 if (strlen(map) > YPMAXMAP)
495 return 1;
496
497 if (strchr(map, '/') != NULL)
498 return 1;
499
500 return 0;
501 }
502