c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER START
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The contents of this file are subject to the terms of the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Common Development and Distribution License (the "License").
ed22c7109fc5dd9e1b7a5d0333bdc7ad2718e2abYuri Pankov * You may not use this file except in compliance with the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * See the License for the specific language governing permissions
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and limitations under the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If applicable, add the following below this CDDL HEADER, with the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER END
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Use is subject to license terms.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* All Rights Reserved */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Portions of this source code were derived from Berkeley
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * under license from the Regents of the University of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * California.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define YPBIND_ERR_NOSERV 2 /* No bound server for passed domain */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define YPBIND_ERR_RESC 3 /* System resource allocation failure */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define YPBIND_ERR_NODOMAIN 4 /* Domain doesn't exist */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int _rpcpmstart; /* Started by a port monitor ? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint setok = YPSETNONE; /* who is allowed to ypset */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint cache_okay = 0; /* if set, then bindings are cached in files */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void unregister(int);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int void_close(void *, int);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint32_t inet_desired_tpts = 0, inet6_desired_tpts = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (geteuid() != 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr, "must be root to run %s\n", argv[0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (argc > 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -broadcast: allowing broadcast! \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe(insecure and transport dependent)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (getdomainname(domain, sizeof (domain)) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sprintf(servers, "%s/%s/ypservers", BINDING, domain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!broadcast && access(servers, R_OK) != 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "%s: no info on servers - run ypinit -c\n", Argv[0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr, "%s: domainname not set - exiting\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If stdin looks like a TLI endpoint, we assume
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * that we were started by a port monitor. If
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * t_getstate fails with TBADF, this is not a
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * TLI endpoint.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcpmstart = (t_getstate(0) != -1 || t_errno != TBADF);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * We were invoked by ypbind with the request on stdin.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * XXX - This is not the normal way ypbind is used
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and has never been tested.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe extern char *getenv();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Set non-blocking mode and maximum record size for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * connection oriented RPC transports.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to set maximum RPC record size");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if ((nconf = getnetconfigent(netid)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not negotiate with loopback tranport %s",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(transp, YPBINDPROG, YPBINDVERS, ypbindprog_3, 0)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to register (YPBINDPROG, YPBINDVERS).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_2).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* version 2 and version 1 are the same as far as we care */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_1).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* NOTREACHED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * In normal operation, ypbind forks a child to do all the work
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * so that it can run in background. But, if the parent exits
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * too soon during system startup, clients will start trying to
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * talk to the child ypbind before it is ready. This can cause
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * spurious client errors.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * To prevent these problems, the parent process creates a pipe,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * which is inherited by the child, and waits for the child to
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * close its end. This happens explicitly before the child goes
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * into svc_run(), or as a side-effect of exiting.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The parent waits for the child to close its end of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the pipe (to indicate that it is ready to process
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * requests). The read blocks until the child does
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * a close (the "domain" array is just a handy buffer).
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* close all files except pfd[1] */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clean_cache(); /* make sure there are no left-over files */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Set non-blocking mode and maximum record size for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * connection oriented RPC transports.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to set maximum RPC record size");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Prevent our non-priv udp and tcp ports bound w/wildcard addr
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * from being hijacked by a bind to a more specific addr.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("warning: unable to set udp/tcp EXCLBIND");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nc_handle = __rpc_setconf("netpath"); /* open netconfig file */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "could not read /etc/netconfig, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The parent waits for the child to close its end of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the pipe (to indicate that it is ready to process
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * requests). Now the non-diskless client will wait because the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * cache file is valid.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("terminating: cannot create rpcbind handle");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* support ypbind V2 and V1, but only on udp/tcp transports */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet6_desired_tpts |= 1 >> nconf->nc_semantics;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) rpcb_unset(YPBINDPROG, YPBINDVERS_2, nconf);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* For NC_INET, register v1 as well; error is fatal */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_1).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not negotiate with loopback tranport %s",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * On a diskless client:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The parent waits for the child to close its end of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the pipe (to indicate that it is ready to process
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * requests). Now the diskless client will wait
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * only if ypbind is registered on the loopback.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Did we manage to register all IPv4 or all IPv6 transports ? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (inet_tpts != 0 && inet_tpts != inet_desired_tpts) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register all %s transports, exiting..",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (inet6_tpts != 0 && inet6_tpts != inet6_desired_tpts) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register all %s transports, exiting..",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not find loopback transports, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not find inet-clts (udp) transport, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "svc_run returned, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* NOTREACHED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Callback function for fdwalk() to close all files.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) memset((char *)&argument, 0, sizeof (argument));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* XXX - don't need two null procedures */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = (bool_t (*)())xdr_ypdomain_wrap_string;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_SETDOM: /* not supported, fall through to error */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) memset((char *)&argument, 0, sizeof (argument));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * We clear out any old bindings that might have been
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * left behind. If there is already a ypbind running,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * it will no longer get requests. We are in control
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * now. We ignore the error from rpcb_unset() because
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * this is just a "best effort". If the rpcb_unset()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * does fail, we will get an error in svc_reg(). By
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * using 0 for the last argument we are telling the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * portmapper to remove the bindings for all transports.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This routine is called when we are killed (by most signals).
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * It first tries to unregister with the portmapper. Then it
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * resets the signal handler to the default so that if we get
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the same signal, we will just go away. We clean up our
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * children by doing a hold in SIGTERM and then killing the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * process group (-getpid()) with SIGTERM. Finally, we redeliver
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the signal to ourselves (the handler was reset to the default)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * so that we will do the normal handling (e.g., coredump).
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If we can't kill ourselves, we get drastic and just exit
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * after sleeping for a couple of seconds.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This code was taken from the SunOS version of ypbind.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe signal(code, SIG_DFL); /* to prevent recursive calls to unregister */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fprintf(stderr, "ypbind: goind down on signal %d\n", code);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe kill(-getpid(), SIGTERM); /* kill process group (i.e., children) */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else if (i == SIGHUP)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (t_getinfo(0, &tinfo) || (tinfo.servtype == T_CLTS))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)