c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER START
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
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 *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * or http://www.opensolaris.org/os/licensing.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * See the License for the specific language governing permissions
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and limitations under the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
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 *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER END
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Use is subject to license terms.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* All Rights Reserved */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <stdio.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <stdio_ext.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <stdlib.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <signal.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <rpc/rpc.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <memory.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <netconfig.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <syslog.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <rpcsvc/yp_prot.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "yp_b.h"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/resource.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/stropts.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <unistd.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <rpc/nettype.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <string.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <tiuser.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef DEBUG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define RPC_SVC_FG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define _RPCSVC_CLOSEDOWN 120
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define YPBIND_ERR_ERR 1 /* Internal error */
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 Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int _rpcpmstart; /* Started by a port monitor ? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int _rpcsvcdirty; /* Still serving ? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint setok = YPSETNONE; /* who is allowed to ypset */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint broadcast = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint cache_okay = 0; /* if set, then bindings are cached in files */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern int sigcld_event;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern void broadcast_proc_exit();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern int __rpc_negotiate_uid();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern bool_t __rpcbind_is_up();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern void ypbind_init_default();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void set_signal_handlers();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void clear_bindings();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void unregister(int);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int void_close(void *, int);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid closedown();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid ypbindprog_3();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid ypbindprog_2();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid msgout();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern void cache_transport();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweextern void clean_cache();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweint
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowemain(argc, argv)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int argc;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char **argv;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pid_t pid;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int pfd[2];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char domain[256], servers[300];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char **Argv = argv;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct netconfig *nconf;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe void *nc_handle;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int loopback_found = 0, udp_found = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int pipe_closed = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct rlimit rl;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int connmaxrec = RPC_MAXDATASIZE;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint32_t inet_tpts = 0, inet6_tpts = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint32_t inet_desired_tpts = 0, inet6_desired_tpts = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe bool_t exclbind = TRUE;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (geteuid() != 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr, "must be root to run %s\n", argv[0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe argc--;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe argv++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (argc > 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(*argv, "-ypset") == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe setok = YPSETALL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (strcmp(*argv, "-ypsetme") == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe setok = YPSETLOCAL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (strcmp(*argv, "-broadcast") == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe broadcast = TRUE;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe argc--,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe argv++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (setok == YPSETALL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (setok == YPSETLOCAL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (broadcast == TRUE) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "ypbind -broadcast: allowing broadcast! \
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe(insecure and transport dependent)\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
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 (void) fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "%s: no info on servers - run ypinit -c\n", Argv[0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr, "%s: domainname not set - exiting\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe Argv[0]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe getrlimit(RLIMIT_NOFILE, &rl);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rl.rlim_cur = rl.rlim_max;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe setrlimit(RLIMIT_NOFILE, &rl);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) enable_extended_FILE_stdio(-1, -1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe openlog("ypbind", LOG_PID, LOG_DAEMON);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcpmstart = (t_getstate(0) != -1 || t_errno != TBADF);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!__rpcbind_is_up()) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("terminating: rpcbind is not running");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (_rpcpmstart) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * We were invoked by ypbind with the request on stdin.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * XXX - This is not the normal way ypbind is used
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and has never been tested.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *netid;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct netconfig *nconf = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SVCXPRT *transp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int pmclose;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe extern char *getenv();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Set non-blocking mode and maximum record size for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * connection oriented RPC transports.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to set maximum RPC record size");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clear_bindings();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((netid = getenv("NLSPROVIDER")) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef DEBUG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("cannot get transport name");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if ((nconf = getnetconfigent(netid)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef DEBUG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("cannot get transport info");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pmclose = (t_getstate(0) != T_DATAXFER);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("cannot create server handle");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((setok != YPSETNONE) &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe __rpc_negotiate_uid(transp->xp_fd)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not negotiate with loopback tranport %s",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nconf->nc_netid);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nconf)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe freenetconfigent(nconf);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(transp, YPBINDPROG, YPBINDVERS, ypbindprog_3, 0)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to register (YPBINDPROG, YPBINDVERS).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_2,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbindprog_2, 0)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout(
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_2).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* version 2 and version 1 are the same as far as we care */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbindprog_2, 0)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout(
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_1).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe set_signal_handlers();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pmclose) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) signal(SIGALRM, closedown);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) alarm(_RPCSVC_CLOSEDOWN);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef INIT_DEFAULT
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbind_init_default();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svc_run();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("svc_run returned");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* NOTREACHED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifndef RPC_SVC_FG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 *
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pipe(pfd) == -1) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe perror("pipe");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pid = fork();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pid < 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe perror("cannot fork");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pid) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe close(pfd[1]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe read(pfd[0], domain, sizeof (domain));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* close all files except pfd[1] */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fdwalk(void_close, &pfd[1]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) open("/dev/null", O_RDONLY);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) open("/dev/null", O_WRONLY);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) dup(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe setsid();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clean_cache(); /* make sure there are no left-over files */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cache_okay = cache_check();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cache_pid();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Set non-blocking mode and maximum record size for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * connection oriented RPC transports.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("unable to set maximum RPC record size");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("warning: unable to set udp/tcp EXCLBIND");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef INIT_DEFAULT
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbind_init_default();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nc_handle = __rpc_setconf("netpath"); /* open netconfig file */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nc_handle == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "could not read /etc/netconfig, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (cache_okay) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe close(pfd[1]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pipe_closed = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clear_bindings();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (nconf = __rpc_getconf(nc_handle)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SVCXPRT *xprt;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!__rpcbind_is_up()) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("terminating: rpcbind is not running");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((xprt = svc_tp_create(ypbindprog_3,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe YPBINDPROG, YPBINDVERS, nconf)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe msgout("terminating: cannot create rpcbind handle");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cache_transport(nconf, xprt, YPBINDVERS);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
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 (strcmp(nconf->nc_protofmly, NC_INET6))) &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ((nconf->nc_semantics == NC_TPI_CLTS) ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (nconf->nc_semantics == NC_TPI_COTS_ORD))) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_INET)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet_desired_tpts |= 1 >> nconf->nc_semantics;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet6_desired_tpts |= 1 >> nconf->nc_semantics;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) rpcb_unset(YPBINDPROG, YPBINDVERS_2, nconf);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_2,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbindprog_2, nconf)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_INFO,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nconf->nc_netid);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe continue;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cache_transport(nconf, xprt, YPBINDVERS_2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* For NC_INET, register v1 as well; error is fatal */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) rpcb_unset(YPBINDPROG, YPBINDVERS_1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nconf);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbindprog_2, nconf)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register (YPBINDPROG, YPBINDVERS_1).");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cache_transport(nconf, xprt, YPBINDVERS_1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nconf->nc_semantics == NC_TPI_CLTS)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe udp_found++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_INET)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet_tpts |= 1 >> nconf->nc_semantics;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe inet6_tpts |= 1 >> nconf->nc_semantics;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe loopback_found++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((setok != YPSETNONE) &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe __rpc_negotiate_uid(xprt->xp_fd)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not negotiate with loopback tranport %s",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nconf->nc_netid);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((!pipe_closed) &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ((nconf->nc_semantics == NC_TPI_COTS) ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (nconf->nc_semantics == NC_TPI_COTS_ORD))) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe close(pfd[1]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pipe_closed = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
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 syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register all %s transports, exiting..",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe NC_INET);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (inet6_tpts != 0 && inet6_tpts != inet6_desired_tpts) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "unable to register all %s transports, exiting..",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe NC_INET6);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!pipe_closed) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe close(pfd[1]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pipe_closed = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe __rpc_endconf(nc_handle);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!loopback_found) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not find loopback transports, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!udp_found) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "could not find inet-clts (udp) transport, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe set_signal_handlers();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svc_run();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "svc_run returned, exiting..");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* NOTREACHED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Callback function for fdwalk() to close all files.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid_close(void *pfdp, int fd)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (fd != *(int *)pfdp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) close(fd);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweypbindprog_3(rqstp, transp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct svc_req *rqstp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe register SVCXPRT *transp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe union {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbind_domain ypbindproc_domain_3_arg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbind_setdom ypbindproc_setdom_3_arg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } argument;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *result;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe bool_t (*xdr_argument)(), (*xdr_result)();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *(*local)();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (sigcld_event)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe broadcast_proc_exit();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (rqstp->rq_proc) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_NULL:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = xdr_void;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_result = xdr_void;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe local = (char *(*)()) ypbindproc_null_3;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_DOMAIN:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = xdr_ypbind_domain;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_result = xdr_ypbind_resp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe local = (char *(*)()) ypbindproc_domain_3;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_SETDOM:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = xdr_ypbind_setdom;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_result = xdr_void;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe local = (char *(*)()) ypbindproc_setdom_3;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_noproc(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) memset((char *)&argument, 0, sizeof (argument));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_decode(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (rqstp->rq_proc == YPBINDPROC_SETDOM)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe result = (*local)(&argument, rqstp, transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe result = (*local)(&argument, rqstp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_systemerr(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "unable to free arguments");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweypbindprog_2(rqstp, transp)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct svc_req *rqstp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe register SVCXPRT *transp;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe union {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe domainname_2 ypbindproc_domain_2_arg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ypbind_setdom_2 ypbindproc_setdom_2_arg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } argument;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *result;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe bool_t (*xdr_argument)(), (*xdr_result)();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *(*local)();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (sigcld_event)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe broadcast_proc_exit();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (rqstp->rq_proc) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_NULL:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = xdr_void;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_result = xdr_void;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* XXX - don't need two null procedures */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe local = (char *(*)()) ypbindproc_null_3;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_DOMAIN:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_argument = (bool_t (*)())xdr_ypdomain_wrap_string;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe xdr_result = xdr_ypbind_resp_2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe local = (char *(*)()) ypbindproc_domain_2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case YPBINDPROC_SETDOM: /* not supported, fall through to error */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_noproc(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) memset((char *)&argument, 0, sizeof (argument));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_decode(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe result = (*local)(&argument, rqstp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe svcerr_systemerr(transp);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, "unable to free arguments");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe _rpcsvcdirty = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
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 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweclear_bindings()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rpcb_unset(YPBINDPROG, YPBINDVERS, 0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rpcb_unset(YPBINDPROG, YPBINDVERS_2, 0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rpcb_unset(YPBINDPROG, YPBINDVERS_1, 0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
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 *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This code was taken from the SunOS version of ypbind.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweunregister(int code)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clear_bindings();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe clean_cache();
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 sighold(SIGCHLD);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sighold(SIGTERM);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe kill(-getpid(), SIGTERM); /* kill process group (i.e., children) */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sigrelse(SIGTERM);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe kill(getpid(), code); /* throw signal again */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sleep(2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweset_signal_handlers()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int i;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 1; i <= SIGTERM; i++) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (i == SIGCHLD)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe continue;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else if (i == SIGHUP)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe signal(i, SIG_IGN);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe signal(i, unregister);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowemsgout(msg)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *msg;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#ifdef RPC_SVC_FG
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (_rpcpmstart)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, msg);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr, "%s\n", msg);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe syslog(LOG_ERR, msg);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#endif
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweclosedown()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (_rpcsvcdirty == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int i, openfd;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe struct t_info tinfo;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (t_getinfo(0, &tinfo) || (tinfo.servtype == T_CLTS))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (svc_pollfd[i].fd >= 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe openfd++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (openfd <= 1)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe exit(0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) alarm(_RPCSVC_CLOSEDOWN);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe