ypserv.c revision 1.5 1 1.5 thorpej /* $NetBSD: ypserv.c,v 1.5 1997/07/18 21:57:19 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*
4 1.1 thorpej * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
16 1.1 thorpej * must display the following acknowledgement:
17 1.1 thorpej * This product includes software developed by Mats O Jansson
18 1.1 thorpej * 4. The name of the author may not be used to endorse or promote products
19 1.1 thorpej * derived from this software without specific prior written permission.
20 1.1 thorpej *
21 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.1 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.1 thorpej * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 thorpej * SUCH DAMAGE.
32 1.1 thorpej */
33 1.1 thorpej
34 1.1 thorpej #include <sys/types.h>
35 1.1 thorpej #include <sys/socket.h>
36 1.1 thorpej #include <sys/wait.h>
37 1.1 thorpej
38 1.1 thorpej #include <netinet/in.h>
39 1.1 thorpej
40 1.5 thorpej #include <err.h>
41 1.1 thorpej #include <netdb.h>
42 1.1 thorpej #include <signal.h>
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <stdlib.h>
45 1.1 thorpej #include <string.h>
46 1.5 thorpej #include <syslog.h>
47 1.5 thorpej #include <unistd.h>
48 1.1 thorpej
49 1.1 thorpej #include <rpc/rpc.h>
50 1.1 thorpej #include <rpc/xdr.h>
51 1.1 thorpej #include <rpc/pmap_clnt.h>
52 1.1 thorpej
53 1.1 thorpej #include <rpcsvc/yp_prot.h>
54 1.1 thorpej
55 1.1 thorpej #include "acl.h"
56 1.1 thorpej #include "yplog.h"
57 1.1 thorpej #include "ypdef.h"
58 1.1 thorpej #include "ypserv.h"
59 1.1 thorpej
60 1.1 thorpej #ifdef __STDC__
61 1.1 thorpej #define SIG_PF void(*)(int)
62 1.1 thorpej #endif
63 1.1 thorpej
64 1.1 thorpej #ifdef DEBUG
65 1.1 thorpej #define RPC_SVC_FG
66 1.1 thorpej #endif
67 1.1 thorpej
68 1.1 thorpej #define _RPCSVC_CLOSEDOWN 120
69 1.1 thorpej static int _rpcpmstart; /* Started by a port monitor ? */
70 1.1 thorpej static int _rpcfdtype; /* Whether Stream or Datagram ? */
71 1.1 thorpej static int _rpcsvcdirty; /* Still serving ? */
72 1.1 thorpej
73 1.1 thorpej int usedns;
74 1.1 thorpej char *aclfile;
75 1.1 thorpej
76 1.1 thorpej extern char *__progname; /* from crt0.s */
77 1.1 thorpej extern char *optarg;
78 1.1 thorpej extern int optind;
79 1.1 thorpej
80 1.5 thorpej int main __P((int, char *[]));
81 1.1 thorpej void usage __P((void));
82 1.1 thorpej
83 1.5 thorpej void sighandler __P((int));
84 1.5 thorpej
85 1.5 thorpej static void closedown __P((void));
86 1.1 thorpej
87 1.1 thorpej static
88 1.1 thorpej void _msgout(char* msg)
89 1.1 thorpej {
90 1.1 thorpej #ifdef RPC_SVC_FG
91 1.1 thorpej if (_rpcpmstart)
92 1.1 thorpej syslog(LOG_ERR, msg);
93 1.1 thorpej else
94 1.1 thorpej warnx("%s", msg);
95 1.1 thorpej #else
96 1.1 thorpej syslog(LOG_ERR, msg);
97 1.1 thorpej #endif
98 1.1 thorpej }
99 1.1 thorpej
100 1.1 thorpej static void
101 1.1 thorpej closedown()
102 1.1 thorpej {
103 1.1 thorpej if (_rpcsvcdirty == 0) {
104 1.1 thorpej extern fd_set svc_fdset;
105 1.1 thorpej static int size;
106 1.1 thorpej int i, openfd;
107 1.1 thorpej
108 1.1 thorpej if (_rpcfdtype == SOCK_DGRAM)
109 1.1 thorpej exit(0);
110 1.1 thorpej if (size == 0) {
111 1.1 thorpej size = getdtablesize();
112 1.1 thorpej }
113 1.1 thorpej for (i = 0, openfd = 0; i < size && openfd < 2; i++)
114 1.1 thorpej if (FD_ISSET(i, &svc_fdset))
115 1.1 thorpej openfd++;
116 1.1 thorpej if (openfd <= (_rpcpmstart?0:1))
117 1.1 thorpej exit(0);
118 1.1 thorpej }
119 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
120 1.1 thorpej }
121 1.1 thorpej
122 1.1 thorpej static void
123 1.1 thorpej ypprog_2(struct svc_req *rqstp, register SVCXPRT *transp)
124 1.1 thorpej {
125 1.1 thorpej union {
126 1.1 thorpej char * ypproc_domain_2_arg;
127 1.1 thorpej char * ypproc_domain_nonack_2_arg;
128 1.1 thorpej struct ypreq_key ypproc_match_2_arg;
129 1.1 thorpej struct ypreq_nokey ypproc_first_2_arg;
130 1.1 thorpej struct ypreq_key ypproc_next_2_arg;
131 1.1 thorpej struct ypreq_xfr ypproc_xfr_2_arg;
132 1.1 thorpej struct ypreq_nokey ypproc_all_2_arg;
133 1.1 thorpej struct ypreq_nokey ypproc_master_2_arg;
134 1.1 thorpej struct ypreq_nokey ypproc_order_2_arg;
135 1.1 thorpej char * ypproc_maplist_2_arg;
136 1.1 thorpej } argument;
137 1.1 thorpej char *result;
138 1.1 thorpej xdrproc_t xdr_argument, xdr_result;
139 1.1 thorpej void *(*local) __P((void *, struct svc_req *));
140 1.1 thorpej
141 1.1 thorpej _rpcsvcdirty = 1;
142 1.1 thorpej switch (rqstp->rq_proc) {
143 1.1 thorpej case YPPROC_NULL:
144 1.1 thorpej xdr_argument = xdr_void;
145 1.1 thorpej xdr_result = xdr_void;
146 1.1 thorpej local = ypproc_null_2_svc;
147 1.1 thorpej break;
148 1.1 thorpej
149 1.1 thorpej case YPPROC_DOMAIN:
150 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
151 1.1 thorpej xdr_result = xdr_bool;
152 1.1 thorpej local = ypproc_domain_2_svc;
153 1.1 thorpej break;
154 1.1 thorpej
155 1.1 thorpej case YPPROC_DOMAIN_NONACK:
156 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
157 1.1 thorpej xdr_result = xdr_bool;
158 1.1 thorpej local = ypproc_domain_nonack_2_svc;
159 1.1 thorpej break;
160 1.1 thorpej
161 1.1 thorpej case YPPROC_MATCH:
162 1.1 thorpej xdr_argument = xdr_ypreq_key;
163 1.1 thorpej xdr_result = xdr_ypresp_val;
164 1.1 thorpej local = ypproc_match_2_svc;
165 1.1 thorpej break;
166 1.1 thorpej
167 1.1 thorpej case YPPROC_FIRST:
168 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
169 1.1 thorpej xdr_result = xdr_ypresp_key_val;
170 1.1 thorpej local = ypproc_first_2_svc;
171 1.1 thorpej break;
172 1.1 thorpej
173 1.1 thorpej case YPPROC_NEXT:
174 1.1 thorpej xdr_argument = xdr_ypreq_key;
175 1.1 thorpej xdr_result = xdr_ypresp_key_val;
176 1.1 thorpej local = ypproc_next_2_svc;
177 1.1 thorpej break;
178 1.1 thorpej
179 1.1 thorpej case YPPROC_XFR:
180 1.1 thorpej xdr_argument = xdr_ypreq_xfr;
181 1.1 thorpej xdr_result = xdr_ypresp_xfr;
182 1.1 thorpej local = ypproc_xfr_2_svc;
183 1.1 thorpej break;
184 1.1 thorpej
185 1.1 thorpej case YPPROC_CLEAR:
186 1.1 thorpej xdr_argument = xdr_void;
187 1.1 thorpej xdr_result = xdr_void;
188 1.1 thorpej local = ypproc_clear_2_svc;
189 1.1 thorpej break;
190 1.1 thorpej
191 1.1 thorpej case YPPROC_ALL:
192 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
193 1.1 thorpej xdr_result = xdr_ypresp_all;
194 1.1 thorpej local = ypproc_all_2_svc;
195 1.1 thorpej break;
196 1.1 thorpej
197 1.1 thorpej case YPPROC_MASTER:
198 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
199 1.1 thorpej xdr_result = xdr_ypresp_master;
200 1.1 thorpej local = ypproc_master_2_svc;
201 1.1 thorpej break;
202 1.1 thorpej
203 1.1 thorpej case YPPROC_ORDER:
204 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
205 1.1 thorpej xdr_result = xdr_ypresp_order;
206 1.1 thorpej local = ypproc_order_2_svc;
207 1.1 thorpej break;
208 1.1 thorpej
209 1.1 thorpej case YPPROC_MAPLIST:
210 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
211 1.1 thorpej xdr_result = xdr_ypresp_maplist;
212 1.1 thorpej local = ypproc_maplist_2_svc;
213 1.1 thorpej break;
214 1.1 thorpej
215 1.1 thorpej default:
216 1.1 thorpej svcerr_noproc(transp);
217 1.1 thorpej _rpcsvcdirty = 0;
218 1.1 thorpej return;
219 1.1 thorpej }
220 1.1 thorpej (void) memset((char *)&argument, 0, sizeof (argument));
221 1.1 thorpej if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
222 1.1 thorpej svcerr_decode(transp);
223 1.1 thorpej _rpcsvcdirty = 0;
224 1.1 thorpej return;
225 1.1 thorpej }
226 1.1 thorpej result = (*local)(&argument, rqstp);
227 1.1 thorpej if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
228 1.1 thorpej svcerr_systemerr(transp);
229 1.1 thorpej }
230 1.1 thorpej if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
231 1.1 thorpej _msgout("unable to free arguments");
232 1.1 thorpej exit(1);
233 1.1 thorpej }
234 1.1 thorpej _rpcsvcdirty = 0;
235 1.1 thorpej return;
236 1.1 thorpej }
237 1.1 thorpej
238 1.4 christos /*
239 1.4 christos * limited NIS version 1 support: the null, domain, and domain_nonack
240 1.4 christos * request/reply format is identical between v1 and v2. SunOS4's ypbind
241 1.4 christos * makes v1 domain_nonack calls.
242 1.4 christos */
243 1.4 christos static void
244 1.4 christos ypprog_1(struct svc_req *rqstp, register SVCXPRT *transp)
245 1.4 christos {
246 1.4 christos switch (rqstp->rq_proc) {
247 1.4 christos case YPPROC_NULL:
248 1.4 christos case YPPROC_DOMAIN:
249 1.4 christos case YPPROC_DOMAIN_NONACK:
250 1.4 christos ypprog_2(rqstp, transp);
251 1.4 christos return;
252 1.4 christos
253 1.4 christos default:
254 1.4 christos svcerr_noproc(transp);
255 1.4 christos _rpcsvcdirty = 0;
256 1.4 christos return;
257 1.4 christos }
258 1.4 christos }
259 1.4 christos
260 1.1 thorpej int
261 1.1 thorpej main(argc, argv)
262 1.1 thorpej int argc;
263 1.5 thorpej char *argv[];
264 1.1 thorpej {
265 1.1 thorpej register SVCXPRT *transp;
266 1.1 thorpej int sock, proto;
267 1.5 thorpej struct sigaction sa;
268 1.1 thorpej int xflag = 0;
269 1.2 ws int ch;
270 1.5 thorpej
271 1.5 thorpej transp = NULL; /* XXX gcc -Wuninitialized */
272 1.5 thorpej proto = 0; /* XXX gcc -Wuninitialized */
273 1.5 thorpej
274 1.1 thorpej while ((ch = getopt(argc, argv, "a:dx")) != -1) {
275 1.1 thorpej switch (ch) {
276 1.1 thorpej case 'a':
277 1.1 thorpej aclfile = optarg;
278 1.1 thorpej break;
279 1.1 thorpej
280 1.1 thorpej case 'd':
281 1.1 thorpej usedns = 1;
282 1.1 thorpej break;
283 1.1 thorpej
284 1.1 thorpej case 'x':
285 1.1 thorpej xflag = 1;
286 1.1 thorpej break;
287 1.1 thorpej
288 1.1 thorpej default:
289 1.1 thorpej usage();
290 1.1 thorpej }
291 1.1 thorpej }
292 1.1 thorpej
293 1.1 thorpej /* This program must be run by root. */
294 1.1 thorpej if (geteuid() != 0)
295 1.1 thorpej errx(1, "must run as root");
296 1.1 thorpej
297 1.1 thorpej /* Deal with the acl file. */
298 1.1 thorpej acl_parse(aclfile);
299 1.1 thorpej
300 1.1 thorpej if (xflag)
301 1.1 thorpej exit(1);
302 1.1 thorpej
303 1.3 mikel #ifndef RPC_SVC_FG
304 1.3 mikel if (daemon(0, 0))
305 1.3 mikel err(1, "can't detach");
306 1.3 mikel openlog("ypserv", LOG_PID, LOG_DAEMON);
307 1.3 mikel #endif
308 1.3 mikel
309 1.1 thorpej {
310 1.1 thorpej FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
311 1.1 thorpej
312 1.1 thorpej if (pidfile != NULL) {
313 1.1 thorpej fprintf(pidfile, "%d\n", getpid());
314 1.1 thorpej fclose(pidfile);
315 1.1 thorpej } else
316 1.1 thorpej err(1, "can't write PID file");
317 1.1 thorpej }
318 1.1 thorpej
319 1.1 thorpej sock = RPC_ANYSOCK;
320 1.1 thorpej (void) pmap_unset(YPPROG, YPVERS);
321 1.4 christos (void) pmap_unset(YPPROG, YPVERS_ORIG);
322 1.1 thorpej
323 1.1 thorpej ypopenlog(); /* open log file */
324 1.1 thorpej ypdb_init(); /* init db stuff */
325 1.1 thorpej
326 1.5 thorpej sa.sa_handler = sighandler;
327 1.5 thorpej sa.sa_flags = 0;
328 1.5 thorpej if (sigemptyset(&sa.sa_mask))
329 1.5 thorpej err(1, "sigemptyset");
330 1.5 thorpej if (sigaction(SIGCHLD, &sa, NULL) || sigaction(SIGHUP, &sa, NULL))
331 1.5 thorpej err(1, "sigaction");
332 1.1 thorpej
333 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
334 1.1 thorpej transp = svcudp_create(sock);
335 1.1 thorpej if (transp == NULL) {
336 1.1 thorpej _msgout("cannot create udp service.");
337 1.1 thorpej exit(1);
338 1.1 thorpej }
339 1.1 thorpej if (!_rpcpmstart)
340 1.1 thorpej proto = IPPROTO_UDP;
341 1.4 christos if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1, proto)) {
342 1.4 christos _msgout("unable to register (YPPROG, YPVERS_ORIG, udp).");
343 1.4 christos exit(1);
344 1.4 christos }
345 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
346 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, udp).");
347 1.1 thorpej exit(1);
348 1.1 thorpej }
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
352 1.1 thorpej if (_rpcpmstart)
353 1.1 thorpej transp = svcfd_create(sock, 0, 0);
354 1.1 thorpej else
355 1.1 thorpej transp = svctcp_create(sock, 0, 0);
356 1.1 thorpej if (transp == NULL) {
357 1.1 thorpej _msgout("cannot create tcp service.");
358 1.1 thorpej exit(1);
359 1.1 thorpej }
360 1.1 thorpej if (!_rpcpmstart)
361 1.1 thorpej proto = IPPROTO_TCP;
362 1.4 christos if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1, proto)) {
363 1.4 christos _msgout("unable to register (YPPROG, YPVERS_ORIG, tcp).");
364 1.4 christos exit(1);
365 1.4 christos }
366 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
367 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, tcp).");
368 1.1 thorpej exit(1);
369 1.1 thorpej }
370 1.1 thorpej }
371 1.1 thorpej
372 1.1 thorpej if (transp == (SVCXPRT *)NULL) {
373 1.1 thorpej _msgout("could not create a handle");
374 1.1 thorpej exit(1);
375 1.1 thorpej }
376 1.1 thorpej if (_rpcpmstart) {
377 1.1 thorpej (void) signal(SIGALRM, (SIG_PF) closedown);
378 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
379 1.1 thorpej }
380 1.1 thorpej svc_run();
381 1.1 thorpej _msgout("svc_run returned");
382 1.1 thorpej exit(1);
383 1.1 thorpej /* NOTREACHED */
384 1.1 thorpej }
385 1.1 thorpej
386 1.1 thorpej void
387 1.5 thorpej sighandler(sig)
388 1.5 thorpej int sig;
389 1.1 thorpej {
390 1.1 thorpej
391 1.5 thorpej if (sig == SIGHUP) {
392 1.5 thorpej acl_reset();
393 1.5 thorpej yplog("reread %s", aclfile ? aclfile : YP_SECURENET_FILE);
394 1.5 thorpej acl_parse(aclfile);
395 1.5 thorpej return;
396 1.5 thorpej }
397 1.5 thorpej
398 1.5 thorpej /* SIGCHLD */
399 1.1 thorpej while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
400 1.1 thorpej }
401 1.1 thorpej
402 1.1 thorpej void
403 1.1 thorpej usage()
404 1.1 thorpej {
405 1.1 thorpej
406 1.1 thorpej fprintf(stderr, "usage: %s [-a aclfile] [-d] [-x]\n", __progname);
407 1.1 thorpej exit(1);
408 1.1 thorpej }
409