ypserv.c revision 1.21 1 /* $NetBSD: ypserv.c,v 1.21 2007/12/15 19:44:57 perry 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.21 2007/12/15 19:44:57 perry Exp $");
37 #endif
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/wait.h>
42
43 #include <err.h>
44 #include <netdb.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <syslog.h>
50 #include <unistd.h>
51 #include <util.h>
52 #include <stdarg.h>
53 #include <errno.h>
54
55 #include <rpc/rpc.h>
56 #include <rpc/xdr.h>
57 #include <rpc/pmap_clnt.h>
58
59 #include <rpcsvc/yp_prot.h>
60
61 #include "ypdef.h"
62 #include "ypserv.h"
63
64 #ifdef LIBWRAP
65 #include <tcpd.h>
66
67 int allow_severity = LOG_DAEMON | LOG_INFO;
68 int deny_severity = LOG_DAEMON | LOG_WARNING;
69
70 /* XXX For ypserv_proc.c -- NOT THREAD SAFE! (like any of this code is) */
71 const char *clientstr;
72 const char *svcname;
73 #endif /* LIBWRAP */
74
75 int usedns;
76 #ifdef DEBUG
77 static int foreground = 1;
78 #else
79 static int foreground;
80 #endif
81
82 #ifdef LIBWRAP
83 int lflag;
84 #endif
85
86 static struct bindsock {
87 sa_family_t family;
88 int type;
89 int proto;
90 const char *name;
91 } socklist[] = {
92 { AF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp" },
93 { AF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp" },
94 { AF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp6" },
95 { AF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp6" },
96 };
97
98 static void usage(void) __dead;
99 static int bind_resv_port(int, sa_family_t, in_port_t);
100
101 static void
102 _msgout(int level, const char *msg, ...)
103 {
104 va_list ap;
105 va_start(ap, msg);
106 if (foreground)
107 vwarnx(msg, ap);
108 else
109 vsyslog(level, msg, ap);
110 va_end(ap);
111 }
112
113 static void
114 ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
115 {
116 union {
117 char * ypproc_domain_2_arg;
118 char * ypproc_domain_nonack_2_arg;
119 struct ypreq_key ypproc_match_2_arg;
120 struct ypreq_nokey ypproc_first_2_arg;
121 struct ypreq_key ypproc_next_2_arg;
122 struct ypreq_xfr ypproc_xfr_2_arg;
123 struct ypreq_nokey ypproc_all_2_arg;
124 struct ypreq_nokey ypproc_master_2_arg;
125 struct ypreq_nokey ypproc_order_2_arg;
126 char * ypproc_maplist_2_arg;
127 } argument;
128 void *argp = &argument;
129 char *result;
130 xdrproc_t xdr_argument, xdr_result;
131 void *(*local)(void *, struct svc_req *);
132 #ifdef LIBWRAP
133 struct request_info req;
134 struct sockaddr *caller;
135 #define SVCNAME(x) svcname = x
136 #else
137 #define SVCNAME(x) /* nothing */
138 #endif
139
140 #ifdef LIBWRAP
141 caller = svc_getrpccaller(transp)->buf;
142 (void)request_init(&req, RQ_DAEMON, getprogname(), RQ_CLIENT_SIN,
143 caller, NULL);
144 sock_methods(&req);
145 #endif
146
147 switch (rqstp->rq_proc) {
148 case YPPROC_NULL:
149 xdr_argument = xdr_void;
150 xdr_result = xdr_void;
151 local = ypproc_null_2_svc;
152 SVCNAME("null_2");
153 break;
154
155 case YPPROC_DOMAIN:
156 xdr_argument = xdr_ypdomain_wrap_string;
157 xdr_result = xdr_bool;
158 local = ypproc_domain_2_svc;
159 SVCNAME("domain_2");
160 break;
161
162 case YPPROC_DOMAIN_NONACK:
163 xdr_argument = xdr_ypdomain_wrap_string;
164 xdr_result = xdr_bool;
165 local = ypproc_domain_nonack_2_svc;
166 SVCNAME("domain_nonack_2");
167 break;
168
169 case YPPROC_MATCH:
170 xdr_argument = xdr_ypreq_key;
171 xdr_result = xdr_ypresp_val;
172 local = ypproc_match_2_svc;
173 SVCNAME("match_2");
174 break;
175
176 case YPPROC_FIRST:
177 xdr_argument = xdr_ypreq_nokey;
178 xdr_result = xdr_ypresp_key_val;
179 local = ypproc_first_2_svc;
180 SVCNAME("first_2");
181 break;
182
183 case YPPROC_NEXT:
184 xdr_argument = xdr_ypreq_key;
185 xdr_result = xdr_ypresp_key_val;
186 local = ypproc_next_2_svc;
187 SVCNAME("next_2");
188 break;
189
190 case YPPROC_XFR:
191 xdr_argument = xdr_ypreq_xfr;
192 xdr_result = xdr_ypresp_xfr;
193 local = ypproc_xfr_2_svc;
194 SVCNAME("xfer_2");
195 break;
196
197 case YPPROC_CLEAR:
198 xdr_argument = xdr_void;
199 xdr_result = xdr_void;
200 local = ypproc_clear_2_svc;
201 SVCNAME("clear_2");
202 break;
203
204 case YPPROC_ALL:
205 xdr_argument = xdr_ypreq_nokey;
206 xdr_result = xdr_ypresp_all;
207 local = ypproc_all_2_svc;
208 SVCNAME("all_2");
209 break;
210
211 case YPPROC_MASTER:
212 xdr_argument = xdr_ypreq_nokey;
213 xdr_result = xdr_ypresp_master;
214 local = ypproc_master_2_svc;
215 SVCNAME("master_2");
216 break;
217
218 case YPPROC_ORDER:
219 xdr_argument = xdr_ypreq_nokey;
220 xdr_result = xdr_ypresp_order;
221 local = ypproc_order_2_svc;
222 SVCNAME("order_2");
223 break;
224
225 case YPPROC_MAPLIST:
226 xdr_argument = xdr_ypdomain_wrap_string;
227 xdr_result = xdr_ypresp_maplist;
228 local = ypproc_maplist_2_svc;
229 SVCNAME("maplist_2");
230 break;
231
232 default:
233 svcerr_noproc(transp);
234 return;
235 }
236
237 #ifdef LIBWRAP
238 clientstr = eval_client(&req);
239
240 if (hosts_access(&req) == 0) {
241 syslog(deny_severity,
242 "%s: refused request from %.500s", svcname, clientstr);
243 svcerr_auth(transp, AUTH_FAILED);
244 return;
245 }
246 #endif
247
248 (void)memset(&argument, 0, sizeof (argument));
249 if (!svc_getargs(transp, xdr_argument, argp)) {
250 svcerr_decode(transp);
251 return;
252 }
253 result = (*local)(&argument, rqstp);
254 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
255 svcerr_systemerr(transp);
256 }
257 if (!svc_freeargs(transp, xdr_argument, argp)) {
258 _msgout(LOG_ERR, "unable to free arguments");
259 exit(1);
260 }
261 return;
262 }
263
264 /*
265 * limited NIS version 1 support: the null, domain, and domain_nonack
266 * request/reply format is identical between v1 and v2. SunOS4's ypbind
267 * makes v1 domain_nonack calls.
268 */
269 static void
270 ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
271 {
272 switch (rqstp->rq_proc) {
273 case YPPROC_NULL:
274 case YPPROC_DOMAIN:
275 case YPPROC_DOMAIN_NONACK:
276 ypprog_2(rqstp, transp);
277 return;
278
279 default:
280 svcerr_noproc(transp);
281 return;
282 }
283 }
284
285 int
286 main(int argc, char *argv[])
287 {
288 SVCXPRT *xprt;
289 struct netconfig *cfg = NULL;
290 int s;
291 struct sigaction sa;
292 struct bindsock *bs;
293 in_port_t port = 0;
294 int ch, xcreated = 0, one = 1;
295
296 setprogname(argv[0]);
297
298 #ifdef LIBWRAP
299 #define GETOPTSTR "dflp:"
300 #else
301 #define GETOPTSTR "dfp:"
302 #endif
303 while ((ch = getopt(argc, argv, GETOPTSTR)) != -1) {
304 switch (ch) {
305 case 'd':
306 usedns = 1;
307 break;
308 case 'f':
309 foreground = 1;
310 break;
311 case 'p':
312 port = atoi(optarg);
313 break;
314 #ifdef LIBWRAP
315 case 'l':
316 lflag = 1;
317 break;
318 #endif
319 default:
320 usage();
321 }
322 }
323
324 #undef GETOPTSTR
325
326 /* This program must be run by root. */
327 if (geteuid() != 0)
328 errx(1, "must run as root");
329
330 if (foreground == 0 && daemon(0, 0))
331 err(1, "can't detach");
332
333 openlog("ypserv", LOG_PID, LOG_DAEMON);
334 syslog(LOG_INFO, "starting");
335 (void)pidfile(NULL);
336
337 (void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS, NULL);
338 (void) rpcb_unset((u_int)YPPROG, (u_int)YPVERS_ORIG, NULL);
339
340
341 ypdb_init(); /* init db stuff */
342
343 sa.sa_handler = SIG_IGN;
344 sa.sa_flags = SA_NOCLDWAIT;
345 if (sigemptyset(&sa.sa_mask)) {
346 _msgout(LOG_ERR, "sigemptyset: %s", strerror(errno));
347 exit(1);
348 }
349 if (sigaction(SIGCHLD, &sa, NULL)) {
350 _msgout(LOG_ERR, "sigaction: %s", strerror(errno));
351 exit(1);
352 }
353
354 for (bs = socklist;
355 bs < &socklist[sizeof(socklist) / sizeof(socklist[0])]; bs++) {
356
357 if ((s = socket(bs->family, bs->type, bs->proto)) == -1)
358 continue;
359
360 if (bs->family == AF_INET6) {
361 /*
362 * We're doing host-based access checks here, so don't
363 * allow v4-in-v6 to confuse things.
364 */
365 if (setsockopt(s, IPPROTO_IPV6,
366 IPV6_V6ONLY, &one, sizeof(one)) == -1) {
367 _msgout(LOG_ERR,
368 "can't disable v4-in-v6 on %s socket",
369 bs->name);
370 exit(1);
371 }
372 }
373
374 if ((cfg = getnetconfigent(bs->name)) == NULL) {
375 _msgout(LOG_ERR,
376 "unable to get network configuration for %s port",
377 bs->name);
378 goto out;
379 }
380
381 if (bind_resv_port(s, bs->family, port) != 0)
382 goto out;
383
384 if (bs->type == SOCK_STREAM) {
385 (void)listen(s, SOMAXCONN);
386 xprt = svc_vc_create(s, 0, 0);
387 } else {
388 xprt = svc_dg_create(s, 0, 0);
389 }
390
391 if (xprt == NULL) {
392 _msgout(LOG_WARNING, "unable to create %s service",
393 bs->name);
394 goto out;
395 }
396 if (svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS_ORIG, ypprog_1,
397 cfg) == 0 ||
398 svc_reg(xprt, (u_int)YPPROG, (u_int)YPVERS, ypprog_2,
399 cfg) == 0) {
400 _msgout(LOG_WARNING, "unable to register %s service",
401 bs->name);
402 goto out;
403 }
404 xcreated++;
405 freenetconfigent(cfg);
406 continue;
407 out:
408 if (s != -1)
409 (void)close(s);
410 if (cfg) {
411 freenetconfigent(cfg);
412 cfg = NULL;
413 }
414 }
415
416 if (xcreated == 0) {
417 _msgout(LOG_ERR, "unable to create any services");
418 exit(1);
419 }
420
421 svc_run();
422 _msgout(LOG_ERR, "svc_run returned");
423 exit(1);
424 /* NOTREACHED */
425 }
426
427 static void
428 usage(void)
429 {
430
431 #ifdef LIBWRAP
432 #define USAGESTR "Usage: %s [-d] [-l] [-p <port>]\n"
433 #else
434 #define USAGESTR "Usage: %s [-d] [-p <port>]\n"
435 #endif
436
437 (void)fprintf(stderr, USAGESTR, getprogname());
438 exit(1);
439
440 #undef USAGESTR
441 }
442
443 /*
444 * _yp_invalid_map: check if given map name isn't legal.
445 * returns non-zero if invalid
446 *
447 * XXX: this probably should be in libc/yp/yplib.c
448 */
449 int
450 _yp_invalid_map(const char *map)
451 {
452 if (map == NULL || *map == '\0')
453 return 1;
454
455 if (strlen(map) > YPMAXMAP)
456 return 1;
457
458 if (strchr(map, '/') != NULL)
459 return 1;
460
461 return 0;
462 }
463
464 static int
465 bind_resv_port(int sock, sa_family_t family, in_port_t port)
466 {
467 struct sockaddr *sa;
468 struct sockaddr_in sasin;
469 struct sockaddr_in6 sasin6;
470
471 switch (family) {
472 case AF_INET:
473 (void)memset(&sasin, 0, sizeof(sasin));
474 sasin.sin_len = sizeof(sasin);
475 sasin.sin_family = family;
476 sasin.sin_port = htons(port);
477 sa = (struct sockaddr *)(void *)&sasin;
478 break;
479 case AF_INET6:
480 (void)memset(&sasin6, 0, sizeof(sasin6));
481 sasin6.sin6_len = sizeof(sasin6);
482 sasin6.sin6_family = family;
483 sasin6.sin6_port = htons(port);
484 sa = (struct sockaddr *)(void *)&sasin6;
485 break;
486 default:
487 _msgout(LOG_ERR, "Unsupported address family %d", family);
488 return -1;
489 }
490 if (bindresvport_sa(sock, sa) == -1) {
491 _msgout(LOG_ERR, "Cannot bind to reserved port %d (%s)", port,
492 strerror(errno));
493 return -1;
494 }
495 return 0;
496 }
497