4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <stdio_ext.h>
43 #include <netconfig.h>
45 #include <rpcsvc/yp_prot.h>
47 #include <sys/resource.h>
48 #include <sys/stropts.h>
50 #include <rpc/nettype.h>
59 #define _RPCSVC_CLOSEDOWN 120
60 #define YPBIND_ERR_ERR 1 /* Internal error */
61 #define YPBIND_ERR_NOSERV 2 /* No bound server for passed domain */
62 #define YPBIND_ERR_RESC 3 /* System resource allocation failure */
63 #define YPBIND_ERR_NODOMAIN 4 /* Domain doesn't exist */
65 static int _rpcpmstart
; /* Started by a port monitor ? */
66 static int _rpcsvcdirty
; /* Still serving ? */
67 int setok
= YPSETNONE
; /* who is allowed to ypset */
69 int cache_okay
= 0; /* if set, then bindings are cached in files */
71 extern int sigcld_event
;
72 extern void broadcast_proc_exit();
73 extern int __rpc_negotiate_uid();
74 extern bool_t
__rpcbind_is_up();
75 extern void ypbind_init_default();
76 static void set_signal_handlers();
77 static void clear_bindings();
78 static void unregister(int);
79 static int void_close(void *, int);
84 extern void cache_transport();
85 extern void clean_cache();
94 char domain
[256], servers
[300];
96 struct netconfig
*nconf
;
98 int loopback_found
= 0, udp_found
= 0;
101 int connmaxrec
= RPC_MAXDATASIZE
;
102 uint32_t inet_tpts
= 0, inet6_tpts
= 0;
103 uint32_t inet_desired_tpts
= 0, inet6_desired_tpts
= 0;
104 bool_t exclbind
= TRUE
;
106 if (geteuid() != 0) {
107 (void) fprintf(stderr
, "must be root to run %s\n", argv
[0]);
115 if (strcmp(*argv
, "-ypset") == 0) {
117 } else if (strcmp(*argv
, "-ypsetme") == 0) {
119 } else if (strcmp(*argv
, "-broadcast") == 0) {
123 "usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
130 if (setok
== YPSETALL
) {
132 "ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
134 if (setok
== YPSETLOCAL
) {
136 "ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
138 if (broadcast
== TRUE
) {
140 "ypbind -broadcast: allowing broadcast! \
141 (insecure and transport dependent)\n");
144 if (getdomainname(domain
, sizeof (domain
)) == 0) {
145 sprintf(servers
, "%s/%s/ypservers", BINDING
, domain
);
146 if (!broadcast
&& access(servers
, R_OK
) != 0) {
147 (void) fprintf(stderr
,
148 "%s: no info on servers - run ypinit -c\n", Argv
[0]);
152 (void) fprintf(stderr
, "%s: domainname not set - exiting\n",
157 getrlimit(RLIMIT_NOFILE
, &rl
);
158 rl
.rlim_cur
= rl
.rlim_max
;
159 setrlimit(RLIMIT_NOFILE
, &rl
);
161 (void) enable_extended_FILE_stdio(-1, -1);
163 openlog("ypbind", LOG_PID
, LOG_DAEMON
);
166 * If stdin looks like a TLI endpoint, we assume
167 * that we were started by a port monitor. If
168 * t_getstate fails with TBADF, this is not a
171 _rpcpmstart
= (t_getstate(0) != -1 || t_errno
!= TBADF
);
173 if (!__rpcbind_is_up()) {
174 msgout("terminating: rpcbind is not running");
180 * We were invoked by ypbind with the request on stdin.
182 * XXX - This is not the normal way ypbind is used
183 * and has never been tested.
186 struct netconfig
*nconf
= NULL
;
189 extern char *getenv();
192 * Set non-blocking mode and maximum record size for
193 * connection oriented RPC transports.
195 if (!rpc_control(RPC_SVC_CONNMAXREC_SET
, &connmaxrec
)) {
196 msgout("unable to set maximum RPC record size");
200 if ((netid
= getenv("NLSPROVIDER")) == NULL
) {
202 msgout("cannot get transport name");
204 } else if ((nconf
= getnetconfigent(netid
)) == NULL
) {
206 msgout("cannot get transport info");
210 pmclose
= (t_getstate(0) != T_DATAXFER
);
211 if ((transp
= svc_tli_create(0, nconf
, NULL
, 0, 0)) == NULL
) {
212 msgout("cannot create server handle");
216 if (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0) {
217 if ((setok
!= YPSETNONE
) &&
218 __rpc_negotiate_uid(transp
->xp_fd
)) {
220 "could not negotiate with loopback tranport %s",
225 freenetconfigent(nconf
);
226 if (!svc_reg(transp
, YPBINDPROG
, YPBINDVERS
, ypbindprog_3
, 0)) {
227 msgout("unable to register (YPBINDPROG, YPBINDVERS).");
230 if (!svc_reg(transp
, YPBINDPROG
, YPBINDVERS_2
,
233 "unable to register (YPBINDPROG, YPBINDVERS_2).");
236 /* version 2 and version 1 are the same as far as we care */
237 if (!svc_reg(transp
, YPBINDPROG
, YPBINDVERS_1
,
240 "unable to register (YPBINDPROG, YPBINDVERS_1).");
243 set_signal_handlers();
245 (void) signal(SIGALRM
, closedown
);
246 (void) alarm(_RPCSVC_CLOSEDOWN
);
249 ypbind_init_default();
252 msgout("svc_run returned");
258 * In normal operation, ypbind forks a child to do all the work
259 * so that it can run in background. But, if the parent exits
260 * too soon during system startup, clients will start trying to
261 * talk to the child ypbind before it is ready. This can cause
262 * spurious client errors.
264 * To prevent these problems, the parent process creates a pipe,
265 * which is inherited by the child, and waits for the child to
266 * close its end. This happens explicitly before the child goes
267 * into svc_run(), or as a side-effect of exiting.
269 if (pipe(pfd
) == -1) {
275 perror("cannot fork");
280 * The parent waits for the child to close its end of
281 * the pipe (to indicate that it is ready to process
282 * requests). The read blocks until the child does
283 * a close (the "domain" array is just a handy buffer).
286 read(pfd
[0], domain
, sizeof (domain
));
289 /* close all files except pfd[1] */
290 (void) fdwalk(void_close
, &pfd
[1]);
291 (void) open("/dev/null", O_RDONLY
);
292 (void) open("/dev/null", O_WRONLY
);
296 clean_cache(); /* make sure there are no left-over files */
297 cache_okay
= cache_check();
301 * Set non-blocking mode and maximum record size for
302 * connection oriented RPC transports.
304 if (!rpc_control(RPC_SVC_CONNMAXREC_SET
, &connmaxrec
)) {
305 msgout("unable to set maximum RPC record size");
309 * Prevent our non-priv udp and tcp ports bound w/wildcard addr
310 * from being hijacked by a bind to a more specific addr.
312 if (!rpc_control(__RPC_SVC_EXCLBIND_SET
, &exclbind
)) {
313 msgout("warning: unable to set udp/tcp EXCLBIND");
317 ypbind_init_default();
320 nc_handle
= __rpc_setconf("netpath"); /* open netconfig file */
321 if (nc_handle
== NULL
) {
322 syslog(LOG_ERR
, "could not read /etc/netconfig, exiting..");
327 * The parent waits for the child to close its end of
328 * the pipe (to indicate that it is ready to process
329 * requests). Now the non-diskless client will wait because the
330 * cache file is valid.
339 while (nconf
= __rpc_getconf(nc_handle
)) {
342 if (!__rpcbind_is_up()) {
343 msgout("terminating: rpcbind is not running");
346 if ((xprt
= svc_tp_create(ypbindprog_3
,
347 YPBINDPROG
, YPBINDVERS
, nconf
)) == NULL
) {
348 msgout("terminating: cannot create rpcbind handle");
352 cache_transport(nconf
, xprt
, YPBINDVERS
);
354 /* support ypbind V2 and V1, but only on udp/tcp transports */
355 if (((strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) ||
356 (strcmp(nconf
->nc_protofmly
, NC_INET6
))) &&
357 ((nconf
->nc_semantics
== NC_TPI_CLTS
) ||
358 (nconf
->nc_semantics
== NC_TPI_COTS_ORD
))) {
360 if (strcmp(nconf
->nc_protofmly
, NC_INET
)) {
361 inet_desired_tpts
|= 1 >> nconf
->nc_semantics
;
363 inet6_desired_tpts
|= 1 >> nconf
->nc_semantics
;
366 (void) rpcb_unset(YPBINDPROG
, YPBINDVERS_2
, nconf
);
367 if (!svc_reg(xprt
, YPBINDPROG
, YPBINDVERS_2
,
368 ypbindprog_2
, nconf
)) {
370 "unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
375 cache_transport(nconf
, xprt
, YPBINDVERS_2
);
377 /* For NC_INET, register v1 as well; error is fatal */
378 if (strcmp(nconf
->nc_protofmly
, NC_INET
) == 0) {
379 (void) rpcb_unset(YPBINDPROG
, YPBINDVERS_1
,
381 if (!svc_reg(xprt
, YPBINDPROG
, YPBINDVERS_1
,
382 ypbindprog_2
, nconf
)) {
384 "unable to register (YPBINDPROG, YPBINDVERS_1).");
389 cache_transport(nconf
, xprt
, YPBINDVERS_1
);
391 if (nconf
->nc_semantics
== NC_TPI_CLTS
)
394 if (strcmp(nconf
->nc_protofmly
, NC_INET
)) {
395 inet_tpts
|= 1 >> nconf
->nc_semantics
;
397 inet6_tpts
|= 1 >> nconf
->nc_semantics
;
400 if (strcmp(nconf
->nc_protofmly
, NC_LOOPBACK
) == 0) {
402 if ((setok
!= YPSETNONE
) &&
403 __rpc_negotiate_uid(xprt
->xp_fd
)) {
405 "could not negotiate with loopback tranport %s",
409 * On a diskless client:
410 * The parent waits for the child to close its end of
411 * the pipe (to indicate that it is ready to process
412 * requests). Now the diskless client will wait
413 * only if ypbind is registered on the loopback.
415 if ((!pipe_closed
) &&
416 ((nconf
->nc_semantics
== NC_TPI_COTS
) ||
417 (nconf
->nc_semantics
== NC_TPI_COTS_ORD
))) {
424 /* Did we manage to register all IPv4 or all IPv6 transports ? */
425 if (inet_tpts
!= 0 && inet_tpts
!= inet_desired_tpts
) {
427 "unable to register all %s transports, exiting..",
430 } else if (inet6_tpts
!= 0 && inet6_tpts
!= inet6_desired_tpts
) {
432 "unable to register all %s transports, exiting..",
441 __rpc_endconf(nc_handle
);
442 if (!loopback_found
) {
444 "could not find loopback transports, exiting..");
449 "could not find inet-clts (udp) transport, exiting..");
452 set_signal_handlers();
454 syslog(LOG_ERR
, "svc_run returned, exiting..");
460 * Callback function for fdwalk() to close all files.
463 void_close(void *pfdp
, int fd
)
465 if (fd
!= *(int *)pfdp
)
471 ypbindprog_3(rqstp
, transp
)
472 struct svc_req
*rqstp
;
473 register SVCXPRT
*transp
;
476 ypbind_domain ypbindproc_domain_3_arg
;
477 ypbind_setdom ypbindproc_setdom_3_arg
;
480 bool_t (*xdr_argument
)(), (*xdr_result
)();
484 broadcast_proc_exit();
487 switch (rqstp
->rq_proc
) {
488 case YPBINDPROC_NULL
:
489 xdr_argument
= xdr_void
;
490 xdr_result
= xdr_void
;
491 local
= (char *(*)()) ypbindproc_null_3
;
494 case YPBINDPROC_DOMAIN
:
495 xdr_argument
= xdr_ypbind_domain
;
496 xdr_result
= xdr_ypbind_resp
;
497 local
= (char *(*)()) ypbindproc_domain_3
;
500 case YPBINDPROC_SETDOM
:
501 xdr_argument
= xdr_ypbind_setdom
;
502 xdr_result
= xdr_void
;
503 local
= (char *(*)()) ypbindproc_setdom_3
;
507 svcerr_noproc(transp
);
511 (void) memset((char *)&argument
, 0, sizeof (argument
));
512 if (!svc_getargs(transp
, (xdrproc_t
)xdr_argument
, (char *)&argument
)) {
513 svcerr_decode(transp
);
517 if (rqstp
->rq_proc
== YPBINDPROC_SETDOM
)
518 result
= (*local
)(&argument
, rqstp
, transp
);
520 result
= (*local
)(&argument
, rqstp
);
521 if (result
!= NULL
&& !svc_sendreply(transp
, xdr_result
, result
)) {
522 svcerr_systemerr(transp
);
524 if (!svc_freeargs(transp
, (xdrproc_t
)xdr_argument
, (char *)&argument
)) {
525 syslog(LOG_ERR
, "unable to free arguments");
532 ypbindprog_2(rqstp
, transp
)
533 struct svc_req
*rqstp
;
534 register SVCXPRT
*transp
;
537 domainname_2 ypbindproc_domain_2_arg
;
538 ypbind_setdom_2 ypbindproc_setdom_2_arg
;
541 bool_t (*xdr_argument
)(), (*xdr_result
)();
545 broadcast_proc_exit();
548 switch (rqstp
->rq_proc
) {
549 case YPBINDPROC_NULL
:
550 xdr_argument
= xdr_void
;
551 xdr_result
= xdr_void
;
552 /* XXX - don't need two null procedures */
553 local
= (char *(*)()) ypbindproc_null_3
;
556 case YPBINDPROC_DOMAIN
:
557 xdr_argument
= (bool_t (*)())xdr_ypdomain_wrap_string
;
558 xdr_result
= xdr_ypbind_resp_2
;
559 local
= (char *(*)()) ypbindproc_domain_2
;
562 case YPBINDPROC_SETDOM
: /* not supported, fall through to error */
564 svcerr_noproc(transp
);
568 (void) memset((char *)&argument
, 0, sizeof (argument
));
569 if (!svc_getargs(transp
, (xdrproc_t
)xdr_argument
, (char *)&argument
)) {
570 svcerr_decode(transp
);
574 result
= (*local
)(&argument
, rqstp
);
575 if (result
!= NULL
&& !svc_sendreply(transp
, xdr_result
, result
)) {
576 svcerr_systemerr(transp
);
578 if (!svc_freeargs(transp
, (xdrproc_t
)xdr_argument
, (char *)&argument
)) {
579 syslog(LOG_ERR
, "unable to free arguments");
586 * We clear out any old bindings that might have been
587 * left behind. If there is already a ypbind running,
588 * it will no longer get requests. We are in control
589 * now. We ignore the error from rpcb_unset() because
590 * this is just a "best effort". If the rpcb_unset()
591 * does fail, we will get an error in svc_reg(). By
592 * using 0 for the last argument we are telling the
593 * portmapper to remove the bindings for all transports.
599 rpcb_unset(YPBINDPROG
, YPBINDVERS
, 0);
600 rpcb_unset(YPBINDPROG
, YPBINDVERS_2
, 0);
601 rpcb_unset(YPBINDPROG
, YPBINDVERS_1
, 0);
605 * This routine is called when we are killed (by most signals).
606 * It first tries to unregister with the portmapper. Then it
607 * resets the signal handler to the default so that if we get
608 * the same signal, we will just go away. We clean up our
609 * children by doing a hold in SIGTERM and then killing the
610 * process group (-getpid()) with SIGTERM. Finally, we redeliver
611 * the signal to ourselves (the handler was reset to the default)
612 * so that we will do the normal handling (e.g., coredump).
613 * If we can't kill ourselves, we get drastic and just exit
614 * after sleeping for a couple of seconds.
616 * This code was taken from the SunOS version of ypbind.
624 signal(code
, SIG_DFL
); /* to prevent recursive calls to unregister */
625 fprintf(stderr
, "ypbind: goind down on signal %d\n", code
);
628 kill(-getpid(), SIGTERM
); /* kill process group (i.e., children) */
630 kill(getpid(), code
); /* throw signal again */
637 set_signal_handlers()
641 for (i
= 1; i
<= SIGTERM
; i
++) {
644 else if (i
== SIGHUP
)
647 signal(i
, unregister
);
657 syslog(LOG_ERR
, msg
);
659 (void) fprintf(stderr
, "%s\n", msg
);
661 syslog(LOG_ERR
, msg
);
668 if (_rpcsvcdirty
== 0) {
672 if (t_getinfo(0, &tinfo
) || (tinfo
.servtype
== T_CLTS
))
675 for (i
= 0, openfd
= 0; i
< svc_max_pollfd
&& openfd
< 2; i
++)
676 if (svc_pollfd
[i
].fd
>= 0)
682 (void) alarm(_RPCSVC_CLOSEDOWN
);