ypserv.c revision 1.6 1 1.6 lukem /* $NetBSD: ypserv.c,v 1.6 1997/10/08 13:45:21 lukem 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
78 1.5 thorpej int main __P((int, char *[]));
79 1.1 thorpej void usage __P((void));
80 1.1 thorpej
81 1.5 thorpej void sighandler __P((int));
82 1.5 thorpej
83 1.5 thorpej static void closedown __P((void));
84 1.1 thorpej
85 1.1 thorpej static
86 1.1 thorpej void _msgout(char* msg)
87 1.1 thorpej {
88 1.1 thorpej #ifdef RPC_SVC_FG
89 1.1 thorpej if (_rpcpmstart)
90 1.1 thorpej syslog(LOG_ERR, msg);
91 1.1 thorpej else
92 1.1 thorpej warnx("%s", msg);
93 1.1 thorpej #else
94 1.1 thorpej syslog(LOG_ERR, msg);
95 1.1 thorpej #endif
96 1.1 thorpej }
97 1.1 thorpej
98 1.1 thorpej static void
99 1.1 thorpej closedown()
100 1.1 thorpej {
101 1.1 thorpej if (_rpcsvcdirty == 0) {
102 1.1 thorpej extern fd_set svc_fdset;
103 1.1 thorpej static int size;
104 1.1 thorpej int i, openfd;
105 1.1 thorpej
106 1.1 thorpej if (_rpcfdtype == SOCK_DGRAM)
107 1.1 thorpej exit(0);
108 1.1 thorpej if (size == 0) {
109 1.1 thorpej size = getdtablesize();
110 1.1 thorpej }
111 1.1 thorpej for (i = 0, openfd = 0; i < size && openfd < 2; i++)
112 1.1 thorpej if (FD_ISSET(i, &svc_fdset))
113 1.1 thorpej openfd++;
114 1.1 thorpej if (openfd <= (_rpcpmstart?0:1))
115 1.1 thorpej exit(0);
116 1.1 thorpej }
117 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
118 1.1 thorpej }
119 1.1 thorpej
120 1.1 thorpej static void
121 1.6 lukem ypprog_2(struct svc_req *rqstp, SVCXPRT *transp)
122 1.1 thorpej {
123 1.1 thorpej union {
124 1.1 thorpej char * ypproc_domain_2_arg;
125 1.1 thorpej char * ypproc_domain_nonack_2_arg;
126 1.1 thorpej struct ypreq_key ypproc_match_2_arg;
127 1.1 thorpej struct ypreq_nokey ypproc_first_2_arg;
128 1.1 thorpej struct ypreq_key ypproc_next_2_arg;
129 1.1 thorpej struct ypreq_xfr ypproc_xfr_2_arg;
130 1.1 thorpej struct ypreq_nokey ypproc_all_2_arg;
131 1.1 thorpej struct ypreq_nokey ypproc_master_2_arg;
132 1.1 thorpej struct ypreq_nokey ypproc_order_2_arg;
133 1.1 thorpej char * ypproc_maplist_2_arg;
134 1.1 thorpej } argument;
135 1.1 thorpej char *result;
136 1.1 thorpej xdrproc_t xdr_argument, xdr_result;
137 1.1 thorpej void *(*local) __P((void *, struct svc_req *));
138 1.1 thorpej
139 1.1 thorpej _rpcsvcdirty = 1;
140 1.1 thorpej switch (rqstp->rq_proc) {
141 1.1 thorpej case YPPROC_NULL:
142 1.1 thorpej xdr_argument = xdr_void;
143 1.1 thorpej xdr_result = xdr_void;
144 1.1 thorpej local = ypproc_null_2_svc;
145 1.1 thorpej break;
146 1.1 thorpej
147 1.1 thorpej case YPPROC_DOMAIN:
148 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
149 1.1 thorpej xdr_result = xdr_bool;
150 1.1 thorpej local = ypproc_domain_2_svc;
151 1.1 thorpej break;
152 1.1 thorpej
153 1.1 thorpej case YPPROC_DOMAIN_NONACK:
154 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
155 1.1 thorpej xdr_result = xdr_bool;
156 1.1 thorpej local = ypproc_domain_nonack_2_svc;
157 1.1 thorpej break;
158 1.1 thorpej
159 1.1 thorpej case YPPROC_MATCH:
160 1.1 thorpej xdr_argument = xdr_ypreq_key;
161 1.1 thorpej xdr_result = xdr_ypresp_val;
162 1.1 thorpej local = ypproc_match_2_svc;
163 1.1 thorpej break;
164 1.1 thorpej
165 1.1 thorpej case YPPROC_FIRST:
166 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
167 1.1 thorpej xdr_result = xdr_ypresp_key_val;
168 1.1 thorpej local = ypproc_first_2_svc;
169 1.1 thorpej break;
170 1.1 thorpej
171 1.1 thorpej case YPPROC_NEXT:
172 1.1 thorpej xdr_argument = xdr_ypreq_key;
173 1.1 thorpej xdr_result = xdr_ypresp_key_val;
174 1.1 thorpej local = ypproc_next_2_svc;
175 1.1 thorpej break;
176 1.1 thorpej
177 1.1 thorpej case YPPROC_XFR:
178 1.1 thorpej xdr_argument = xdr_ypreq_xfr;
179 1.1 thorpej xdr_result = xdr_ypresp_xfr;
180 1.1 thorpej local = ypproc_xfr_2_svc;
181 1.1 thorpej break;
182 1.1 thorpej
183 1.1 thorpej case YPPROC_CLEAR:
184 1.1 thorpej xdr_argument = xdr_void;
185 1.1 thorpej xdr_result = xdr_void;
186 1.1 thorpej local = ypproc_clear_2_svc;
187 1.1 thorpej break;
188 1.1 thorpej
189 1.1 thorpej case YPPROC_ALL:
190 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
191 1.1 thorpej xdr_result = xdr_ypresp_all;
192 1.1 thorpej local = ypproc_all_2_svc;
193 1.1 thorpej break;
194 1.1 thorpej
195 1.1 thorpej case YPPROC_MASTER:
196 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
197 1.1 thorpej xdr_result = xdr_ypresp_master;
198 1.1 thorpej local = ypproc_master_2_svc;
199 1.1 thorpej break;
200 1.1 thorpej
201 1.1 thorpej case YPPROC_ORDER:
202 1.1 thorpej xdr_argument = xdr_ypreq_nokey;
203 1.1 thorpej xdr_result = xdr_ypresp_order;
204 1.1 thorpej local = ypproc_order_2_svc;
205 1.1 thorpej break;
206 1.1 thorpej
207 1.1 thorpej case YPPROC_MAPLIST:
208 1.1 thorpej xdr_argument = xdr_ypdomain_wrap_string;
209 1.1 thorpej xdr_result = xdr_ypresp_maplist;
210 1.1 thorpej local = ypproc_maplist_2_svc;
211 1.1 thorpej break;
212 1.1 thorpej
213 1.1 thorpej default:
214 1.1 thorpej svcerr_noproc(transp);
215 1.1 thorpej _rpcsvcdirty = 0;
216 1.1 thorpej return;
217 1.1 thorpej }
218 1.1 thorpej (void) memset((char *)&argument, 0, sizeof (argument));
219 1.1 thorpej if (!svc_getargs(transp, xdr_argument, (caddr_t) &argument)) {
220 1.1 thorpej svcerr_decode(transp);
221 1.1 thorpej _rpcsvcdirty = 0;
222 1.1 thorpej return;
223 1.1 thorpej }
224 1.1 thorpej result = (*local)(&argument, rqstp);
225 1.1 thorpej if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
226 1.1 thorpej svcerr_systemerr(transp);
227 1.1 thorpej }
228 1.1 thorpej if (!svc_freeargs(transp, xdr_argument, (caddr_t) &argument)) {
229 1.1 thorpej _msgout("unable to free arguments");
230 1.1 thorpej exit(1);
231 1.1 thorpej }
232 1.1 thorpej _rpcsvcdirty = 0;
233 1.1 thorpej return;
234 1.1 thorpej }
235 1.1 thorpej
236 1.4 christos /*
237 1.4 christos * limited NIS version 1 support: the null, domain, and domain_nonack
238 1.4 christos * request/reply format is identical between v1 and v2. SunOS4's ypbind
239 1.4 christos * makes v1 domain_nonack calls.
240 1.4 christos */
241 1.4 christos static void
242 1.6 lukem ypprog_1(struct svc_req *rqstp, SVCXPRT *transp)
243 1.4 christos {
244 1.4 christos switch (rqstp->rq_proc) {
245 1.4 christos case YPPROC_NULL:
246 1.4 christos case YPPROC_DOMAIN:
247 1.4 christos case YPPROC_DOMAIN_NONACK:
248 1.4 christos ypprog_2(rqstp, transp);
249 1.4 christos return;
250 1.4 christos
251 1.4 christos default:
252 1.4 christos svcerr_noproc(transp);
253 1.4 christos _rpcsvcdirty = 0;
254 1.4 christos return;
255 1.4 christos }
256 1.4 christos }
257 1.4 christos
258 1.1 thorpej int
259 1.1 thorpej main(argc, argv)
260 1.1 thorpej int argc;
261 1.5 thorpej char *argv[];
262 1.1 thorpej {
263 1.6 lukem SVCXPRT *transp;
264 1.1 thorpej int sock, proto;
265 1.5 thorpej struct sigaction sa;
266 1.1 thorpej int xflag = 0;
267 1.2 ws int ch;
268 1.5 thorpej
269 1.5 thorpej transp = NULL; /* XXX gcc -Wuninitialized */
270 1.5 thorpej proto = 0; /* XXX gcc -Wuninitialized */
271 1.5 thorpej
272 1.1 thorpej while ((ch = getopt(argc, argv, "a:dx")) != -1) {
273 1.1 thorpej switch (ch) {
274 1.1 thorpej case 'a':
275 1.1 thorpej aclfile = optarg;
276 1.1 thorpej break;
277 1.1 thorpej
278 1.1 thorpej case 'd':
279 1.1 thorpej usedns = 1;
280 1.1 thorpej break;
281 1.1 thorpej
282 1.1 thorpej case 'x':
283 1.1 thorpej xflag = 1;
284 1.1 thorpej break;
285 1.1 thorpej
286 1.1 thorpej default:
287 1.1 thorpej usage();
288 1.1 thorpej }
289 1.1 thorpej }
290 1.1 thorpej
291 1.1 thorpej /* This program must be run by root. */
292 1.1 thorpej if (geteuid() != 0)
293 1.1 thorpej errx(1, "must run as root");
294 1.1 thorpej
295 1.1 thorpej /* Deal with the acl file. */
296 1.1 thorpej acl_parse(aclfile);
297 1.1 thorpej
298 1.1 thorpej if (xflag)
299 1.1 thorpej exit(1);
300 1.1 thorpej
301 1.3 mikel #ifndef RPC_SVC_FG
302 1.3 mikel if (daemon(0, 0))
303 1.3 mikel err(1, "can't detach");
304 1.3 mikel openlog("ypserv", LOG_PID, LOG_DAEMON);
305 1.3 mikel #endif
306 1.3 mikel
307 1.1 thorpej {
308 1.1 thorpej FILE *pidfile = fopen(YPSERV_PID_PATH, "w");
309 1.1 thorpej
310 1.1 thorpej if (pidfile != NULL) {
311 1.1 thorpej fprintf(pidfile, "%d\n", getpid());
312 1.1 thorpej fclose(pidfile);
313 1.1 thorpej } else
314 1.1 thorpej err(1, "can't write PID file");
315 1.1 thorpej }
316 1.1 thorpej
317 1.1 thorpej sock = RPC_ANYSOCK;
318 1.1 thorpej (void) pmap_unset(YPPROG, YPVERS);
319 1.4 christos (void) pmap_unset(YPPROG, YPVERS_ORIG);
320 1.1 thorpej
321 1.1 thorpej ypopenlog(); /* open log file */
322 1.1 thorpej ypdb_init(); /* init db stuff */
323 1.1 thorpej
324 1.5 thorpej sa.sa_handler = sighandler;
325 1.5 thorpej sa.sa_flags = 0;
326 1.5 thorpej if (sigemptyset(&sa.sa_mask))
327 1.5 thorpej err(1, "sigemptyset");
328 1.5 thorpej if (sigaction(SIGCHLD, &sa, NULL) || sigaction(SIGHUP, &sa, NULL))
329 1.5 thorpej err(1, "sigaction");
330 1.1 thorpej
331 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
332 1.1 thorpej transp = svcudp_create(sock);
333 1.1 thorpej if (transp == NULL) {
334 1.1 thorpej _msgout("cannot create udp service.");
335 1.1 thorpej exit(1);
336 1.1 thorpej }
337 1.6 lukem if (transp->xp_port >= IPPORT_RESERVED) {
338 1.6 lukem _msgout("udp service not bound to a privileged port.");
339 1.6 lukem exit(1);
340 1.6 lukem }
341 1.1 thorpej if (!_rpcpmstart)
342 1.1 thorpej proto = IPPROTO_UDP;
343 1.6 lukem if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
344 1.6 lukem proto)) {
345 1.6 lukem _msgout(
346 1.6 lukem "unable to register (YPPROG, YPVERS_ORIG, udp).");
347 1.4 christos exit(1);
348 1.4 christos }
349 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
350 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, udp).");
351 1.1 thorpej exit(1);
352 1.1 thorpej }
353 1.1 thorpej }
354 1.1 thorpej
355 1.1 thorpej if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
356 1.1 thorpej if (_rpcpmstart)
357 1.1 thorpej transp = svcfd_create(sock, 0, 0);
358 1.1 thorpej else
359 1.1 thorpej transp = svctcp_create(sock, 0, 0);
360 1.1 thorpej if (transp == NULL) {
361 1.1 thorpej _msgout("cannot create tcp service.");
362 1.1 thorpej exit(1);
363 1.1 thorpej }
364 1.6 lukem if (transp->xp_port >= IPPORT_RESERVED) {
365 1.6 lukem _msgout("tcp service not bound to a privileged port.");
366 1.6 lukem exit(1);
367 1.6 lukem }
368 1.1 thorpej if (!_rpcpmstart)
369 1.1 thorpej proto = IPPROTO_TCP;
370 1.6 lukem if (!svc_register(transp, YPPROG, YPVERS_ORIG, ypprog_1,
371 1.6 lukem proto)) {
372 1.6 lukem _msgout(
373 1.6 lukem "unable to register (YPPROG, YPVERS_ORIG, tcp).");
374 1.4 christos exit(1);
375 1.4 christos }
376 1.1 thorpej if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
377 1.1 thorpej _msgout("unable to register (YPPROG, YPVERS, tcp).");
378 1.1 thorpej exit(1);
379 1.1 thorpej }
380 1.1 thorpej }
381 1.1 thorpej
382 1.1 thorpej if (transp == (SVCXPRT *)NULL) {
383 1.1 thorpej _msgout("could not create a handle");
384 1.1 thorpej exit(1);
385 1.1 thorpej }
386 1.1 thorpej if (_rpcpmstart) {
387 1.1 thorpej (void) signal(SIGALRM, (SIG_PF) closedown);
388 1.1 thorpej (void) alarm(_RPCSVC_CLOSEDOWN);
389 1.1 thorpej }
390 1.1 thorpej svc_run();
391 1.1 thorpej _msgout("svc_run returned");
392 1.1 thorpej exit(1);
393 1.1 thorpej /* NOTREACHED */
394 1.1 thorpej }
395 1.1 thorpej
396 1.1 thorpej void
397 1.5 thorpej sighandler(sig)
398 1.5 thorpej int sig;
399 1.1 thorpej {
400 1.1 thorpej
401 1.5 thorpej if (sig == SIGHUP) {
402 1.5 thorpej acl_reset();
403 1.5 thorpej yplog("reread %s", aclfile ? aclfile : YP_SECURENET_FILE);
404 1.5 thorpej acl_parse(aclfile);
405 1.5 thorpej return;
406 1.5 thorpej }
407 1.5 thorpej
408 1.5 thorpej /* SIGCHLD */
409 1.1 thorpej while (wait3((int *)NULL, WNOHANG, (struct rusage *)NULL) > 0);
410 1.1 thorpej }
411 1.1 thorpej
412 1.1 thorpej void
413 1.1 thorpej usage()
414 1.1 thorpej {
415 1.1 thorpej
416 1.1 thorpej fprintf(stderr, "usage: %s [-a aclfile] [-d] [-x]\n", __progname);
417 1.1 thorpej exit(1);
418 1.1 thorpej }
419