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