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