10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1914Scasper * Common Development and Distribution License (the "License").
6*1914Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*1914Scasper * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate * under license from the Regents of the University of
320Sstevel@tonic-gate * California.
330Sstevel@tonic-gate */
340Sstevel@tonic-gate
350Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <stdio.h>
38*1914Scasper #include <stdio_ext.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <rpc/rpc.h>
420Sstevel@tonic-gate #include <memory.h>
430Sstevel@tonic-gate #include <netconfig.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
460Sstevel@tonic-gate #include "yp_b.h"
470Sstevel@tonic-gate #include <sys/resource.h>
480Sstevel@tonic-gate #include <sys/stropts.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate #include <rpc/nettype.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <tiuser.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate
550Sstevel@tonic-gate #ifdef DEBUG
560Sstevel@tonic-gate #define RPC_SVC_FG
570Sstevel@tonic-gate #endif
580Sstevel@tonic-gate
590Sstevel@tonic-gate #define _RPCSVC_CLOSEDOWN 120
600Sstevel@tonic-gate #define YPBIND_ERR_ERR 1 /* Internal error */
610Sstevel@tonic-gate #define YPBIND_ERR_NOSERV 2 /* No bound server for passed domain */
620Sstevel@tonic-gate #define YPBIND_ERR_RESC 3 /* System resource allocation failure */
630Sstevel@tonic-gate #define YPBIND_ERR_NODOMAIN 4 /* Domain doesn't exist */
640Sstevel@tonic-gate
650Sstevel@tonic-gate static int _rpcpmstart; /* Started by a port monitor ? */
660Sstevel@tonic-gate static int _rpcsvcdirty; /* Still serving ? */
670Sstevel@tonic-gate int setok = YPSETNONE; /* who is allowed to ypset */
680Sstevel@tonic-gate int broadcast = 0;
690Sstevel@tonic-gate int cache_okay = 0; /* if set, then bindings are cached in files */
700Sstevel@tonic-gate
710Sstevel@tonic-gate extern int sigcld_event;
720Sstevel@tonic-gate extern void broadcast_proc_exit();
730Sstevel@tonic-gate extern int __rpc_negotiate_uid();
740Sstevel@tonic-gate extern bool_t __rpcbind_is_up();
750Sstevel@tonic-gate extern void ypbind_init_default();
760Sstevel@tonic-gate static void set_signal_handlers();
770Sstevel@tonic-gate static void clear_bindings();
780Sstevel@tonic-gate static void unregister(int);
790Sstevel@tonic-gate static int void_close(void *, int);
800Sstevel@tonic-gate void closedown();
810Sstevel@tonic-gate void ypbindprog_3();
820Sstevel@tonic-gate void ypbindprog_2();
830Sstevel@tonic-gate void msgout();
840Sstevel@tonic-gate extern void cache_transport();
850Sstevel@tonic-gate extern void clean_cache();
860Sstevel@tonic-gate
87702Sth160488 int
main(argc,argv)880Sstevel@tonic-gate main(argc, argv)
890Sstevel@tonic-gate int argc;
900Sstevel@tonic-gate char **argv;
910Sstevel@tonic-gate {
920Sstevel@tonic-gate pid_t pid;
930Sstevel@tonic-gate int pfd[2];
940Sstevel@tonic-gate char domain[256], servers[300];
950Sstevel@tonic-gate char **Argv = argv;
960Sstevel@tonic-gate struct netconfig *nconf;
970Sstevel@tonic-gate void *nc_handle;
980Sstevel@tonic-gate int loopback_found = 0, udp_found = 0;
990Sstevel@tonic-gate int pipe_closed = 0;
1000Sstevel@tonic-gate struct rlimit rl;
1010Sstevel@tonic-gate int connmaxrec = RPC_MAXDATASIZE;
1020Sstevel@tonic-gate uint32_t inet_tpts = 0, inet6_tpts = 0;
1030Sstevel@tonic-gate uint32_t inet_desired_tpts = 0, inet6_desired_tpts = 0;
1040Sstevel@tonic-gate bool_t exclbind = TRUE;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate if (geteuid() != 0) {
1070Sstevel@tonic-gate (void) fprintf(stderr, "must be root to run %s\n", argv[0]);
1080Sstevel@tonic-gate exit(1);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate argc--;
1120Sstevel@tonic-gate argv++;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate while (argc > 0) {
1150Sstevel@tonic-gate if (strcmp(*argv, "-ypset") == 0) {
1160Sstevel@tonic-gate setok = YPSETALL;
1170Sstevel@tonic-gate } else if (strcmp(*argv, "-ypsetme") == 0) {
1180Sstevel@tonic-gate setok = YPSETLOCAL;
1190Sstevel@tonic-gate } else if (strcmp(*argv, "-broadcast") == 0) {
1200Sstevel@tonic-gate broadcast = TRUE;
1210Sstevel@tonic-gate } else {
1220Sstevel@tonic-gate fprintf(stderr,
1230Sstevel@tonic-gate "usage: ypbind [-broadcast] [-ypset] [-ypsetme]\n");
1240Sstevel@tonic-gate exit(1);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate argc--,
1270Sstevel@tonic-gate argv++;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate if (setok == YPSETALL) {
1310Sstevel@tonic-gate fprintf(stderr,
1320Sstevel@tonic-gate "ypbind -ypset: allowing ypset! (this is REALLY insecure)\n");
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate if (setok == YPSETLOCAL) {
1350Sstevel@tonic-gate fprintf(stderr,
1360Sstevel@tonic-gate "ypbind -ypsetme: allowing local ypset! (this is insecure)\n");
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate if (broadcast == TRUE) {
1390Sstevel@tonic-gate fprintf(stderr,
1400Sstevel@tonic-gate "ypbind -broadcast: allowing broadcast! \
1410Sstevel@tonic-gate (insecure and transport dependent)\n");
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (getdomainname(domain, sizeof (domain)) == 0) {
1450Sstevel@tonic-gate sprintf(servers, "%s/%s/ypservers", BINDING, domain);
1460Sstevel@tonic-gate if (!broadcast && access(servers, R_OK) != 0) {
1470Sstevel@tonic-gate (void) fprintf(stderr,
1480Sstevel@tonic-gate "%s: no info on servers - run ypinit -c\n", Argv[0]);
1490Sstevel@tonic-gate exit(1);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate } else {
1520Sstevel@tonic-gate (void) fprintf(stderr, "%s: domainname not set - exiting\n",
1530Sstevel@tonic-gate Argv[0]);
1540Sstevel@tonic-gate exit(1);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate getrlimit(RLIMIT_NOFILE, &rl);
1580Sstevel@tonic-gate rl.rlim_cur = rl.rlim_max;
1590Sstevel@tonic-gate setrlimit(RLIMIT_NOFILE, &rl);
1600Sstevel@tonic-gate
161*1914Scasper (void) enable_extended_FILE_stdio(-1, -1);
162*1914Scasper
1630Sstevel@tonic-gate openlog("ypbind", LOG_PID, LOG_DAEMON);
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * If stdin looks like a TLI endpoint, we assume
1670Sstevel@tonic-gate * that we were started by a port monitor. If
1680Sstevel@tonic-gate * t_getstate fails with TBADF, this is not a
1690Sstevel@tonic-gate * TLI endpoint.
1700Sstevel@tonic-gate */
1710Sstevel@tonic-gate _rpcpmstart = (t_getstate(0) != -1 || t_errno != TBADF);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (!__rpcbind_is_up()) {
1740Sstevel@tonic-gate msgout("terminating: rpcbind is not running");
1750Sstevel@tonic-gate exit(1);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate if (_rpcpmstart) {
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * We were invoked by ypbind with the request on stdin.
1810Sstevel@tonic-gate *
1820Sstevel@tonic-gate * XXX - This is not the normal way ypbind is used
1830Sstevel@tonic-gate * and has never been tested.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate char *netid;
1860Sstevel@tonic-gate struct netconfig *nconf = NULL;
1870Sstevel@tonic-gate SVCXPRT *transp;
1880Sstevel@tonic-gate int pmclose;
1890Sstevel@tonic-gate extern char *getenv();
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * Set non-blocking mode and maximum record size for
1930Sstevel@tonic-gate * connection oriented RPC transports.
1940Sstevel@tonic-gate */
1950Sstevel@tonic-gate if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
1960Sstevel@tonic-gate msgout("unable to set maximum RPC record size");
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate clear_bindings();
2000Sstevel@tonic-gate if ((netid = getenv("NLSPROVIDER")) == NULL) {
2010Sstevel@tonic-gate #ifdef DEBUG
2020Sstevel@tonic-gate msgout("cannot get transport name");
2030Sstevel@tonic-gate #endif
2040Sstevel@tonic-gate } else if ((nconf = getnetconfigent(netid)) == NULL) {
2050Sstevel@tonic-gate #ifdef DEBUG
2060Sstevel@tonic-gate msgout("cannot get transport info");
2070Sstevel@tonic-gate #endif
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate pmclose = (t_getstate(0) != T_DATAXFER);
2110Sstevel@tonic-gate if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {
2120Sstevel@tonic-gate msgout("cannot create server handle");
2130Sstevel@tonic-gate exit(1);
2140Sstevel@tonic-gate }
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
2170Sstevel@tonic-gate if ((setok != YPSETNONE) &&
2180Sstevel@tonic-gate __rpc_negotiate_uid(transp->xp_fd)) {
2190Sstevel@tonic-gate syslog(LOG_ERR,
2200Sstevel@tonic-gate "could not negotiate with loopback tranport %s",
2210Sstevel@tonic-gate nconf->nc_netid);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate if (nconf)
2250Sstevel@tonic-gate freenetconfigent(nconf);
2260Sstevel@tonic-gate if (!svc_reg(transp, YPBINDPROG, YPBINDVERS, ypbindprog_3, 0)) {
2270Sstevel@tonic-gate msgout("unable to register (YPBINDPROG, YPBINDVERS).");
2280Sstevel@tonic-gate exit(1);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_2,
2310Sstevel@tonic-gate ypbindprog_2, 0)) {
2320Sstevel@tonic-gate msgout(
2330Sstevel@tonic-gate "unable to register (YPBINDPROG, YPBINDVERS_2).");
2340Sstevel@tonic-gate exit(1);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate /* version 2 and version 1 are the same as far as we care */
2370Sstevel@tonic-gate if (!svc_reg(transp, YPBINDPROG, YPBINDVERS_1,
2380Sstevel@tonic-gate ypbindprog_2, 0)) {
2390Sstevel@tonic-gate msgout(
2400Sstevel@tonic-gate "unable to register (YPBINDPROG, YPBINDVERS_1).");
2410Sstevel@tonic-gate exit(1);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate set_signal_handlers();
2440Sstevel@tonic-gate if (pmclose) {
2450Sstevel@tonic-gate (void) signal(SIGALRM, closedown);
2460Sstevel@tonic-gate (void) alarm(_RPCSVC_CLOSEDOWN);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate #ifdef INIT_DEFAULT
2490Sstevel@tonic-gate ypbind_init_default();
2500Sstevel@tonic-gate #endif
2510Sstevel@tonic-gate svc_run();
2520Sstevel@tonic-gate msgout("svc_run returned");
2530Sstevel@tonic-gate exit(1);
2540Sstevel@tonic-gate /* NOTREACHED */
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate #ifndef RPC_SVC_FG
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * In normal operation, ypbind forks a child to do all the work
2590Sstevel@tonic-gate * so that it can run in background. But, if the parent exits
2600Sstevel@tonic-gate * too soon during system startup, clients will start trying to
2610Sstevel@tonic-gate * talk to the child ypbind before it is ready. This can cause
2620Sstevel@tonic-gate * spurious client errors.
2630Sstevel@tonic-gate *
2640Sstevel@tonic-gate * To prevent these problems, the parent process creates a pipe,
2650Sstevel@tonic-gate * which is inherited by the child, and waits for the child to
2660Sstevel@tonic-gate * close its end. This happens explicitly before the child goes
2670Sstevel@tonic-gate * into svc_run(), or as a side-effect of exiting.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate if (pipe(pfd) == -1) {
2700Sstevel@tonic-gate perror("pipe");
2710Sstevel@tonic-gate exit(1);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate pid = fork();
2740Sstevel@tonic-gate if (pid < 0) {
2750Sstevel@tonic-gate perror("cannot fork");
2760Sstevel@tonic-gate exit(1);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate if (pid) {
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * The parent waits for the child to close its end of
2810Sstevel@tonic-gate * the pipe (to indicate that it is ready to process
2820Sstevel@tonic-gate * requests). The read blocks until the child does
2830Sstevel@tonic-gate * a close (the "domain" array is just a handy buffer).
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate close(pfd[1]);
2860Sstevel@tonic-gate read(pfd[0], domain, sizeof (domain));
2870Sstevel@tonic-gate exit(0);
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate /* close all files except pfd[1] */
2900Sstevel@tonic-gate (void) fdwalk(void_close, &pfd[1]);
2910Sstevel@tonic-gate (void) open("/dev/null", O_RDONLY);
2920Sstevel@tonic-gate (void) open("/dev/null", O_WRONLY);
2930Sstevel@tonic-gate (void) dup(1);
2940Sstevel@tonic-gate setsid();
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate clean_cache(); /* make sure there are no left-over files */
2970Sstevel@tonic-gate cache_okay = cache_check();
2980Sstevel@tonic-gate cache_pid();
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * Set non-blocking mode and maximum record size for
3020Sstevel@tonic-gate * connection oriented RPC transports.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &connmaxrec)) {
3050Sstevel@tonic-gate msgout("unable to set maximum RPC record size");
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate * Prevent our non-priv udp and tcp ports bound w/wildcard addr
3100Sstevel@tonic-gate * from being hijacked by a bind to a more specific addr.
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
3130Sstevel@tonic-gate msgout("warning: unable to set udp/tcp EXCLBIND");
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate #ifdef INIT_DEFAULT
3170Sstevel@tonic-gate ypbind_init_default();
3180Sstevel@tonic-gate #endif
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate nc_handle = __rpc_setconf("netpath"); /* open netconfig file */
3210Sstevel@tonic-gate if (nc_handle == NULL) {
3220Sstevel@tonic-gate syslog(LOG_ERR, "could not read /etc/netconfig, exiting..");
3230Sstevel@tonic-gate exit(1);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * The parent waits for the child to close its end of
3280Sstevel@tonic-gate * the pipe (to indicate that it is ready to process
3290Sstevel@tonic-gate * requests). Now the non-diskless client will wait because the
3300Sstevel@tonic-gate * cache file is valid.
3310Sstevel@tonic-gate */
3320Sstevel@tonic-gate if (cache_okay) {
3330Sstevel@tonic-gate close(pfd[1]);
3340Sstevel@tonic-gate pipe_closed = 1;
3350Sstevel@tonic-gate }
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate clear_bindings();
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate while (nconf = __rpc_getconf(nc_handle)) {
3400Sstevel@tonic-gate SVCXPRT *xprt;
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate if (!__rpcbind_is_up()) {
3430Sstevel@tonic-gate msgout("terminating: rpcbind is not running");
3440Sstevel@tonic-gate exit(1);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate if ((xprt = svc_tp_create(ypbindprog_3,
3470Sstevel@tonic-gate YPBINDPROG, YPBINDVERS, nconf)) == NULL) {
3480Sstevel@tonic-gate msgout("terminating: cannot create rpcbind handle");
3490Sstevel@tonic-gate exit(1);
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate cache_transport(nconf, xprt, YPBINDVERS);
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate /* support ypbind V2 and V1, but only on udp/tcp transports */
3550Sstevel@tonic-gate if (((strcmp(nconf->nc_protofmly, NC_INET) == 0) ||
3560Sstevel@tonic-gate (strcmp(nconf->nc_protofmly, NC_INET6))) &&
3570Sstevel@tonic-gate ((nconf->nc_semantics == NC_TPI_CLTS) ||
3580Sstevel@tonic-gate (nconf->nc_semantics == NC_TPI_COTS_ORD))) {
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET)) {
3610Sstevel@tonic-gate inet_desired_tpts |= 1 >> nconf->nc_semantics;
3620Sstevel@tonic-gate } else {
3630Sstevel@tonic-gate inet6_desired_tpts |= 1 >> nconf->nc_semantics;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate (void) rpcb_unset(YPBINDPROG, YPBINDVERS_2, nconf);
3670Sstevel@tonic-gate if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_2,
3680Sstevel@tonic-gate ypbindprog_2, nconf)) {
3690Sstevel@tonic-gate syslog(LOG_INFO,
3700Sstevel@tonic-gate "unable to register (YPBINDPROG, YPBINDVERS_2) [%s]",
3710Sstevel@tonic-gate nconf->nc_netid);
3720Sstevel@tonic-gate continue;
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate
3750Sstevel@tonic-gate cache_transport(nconf, xprt, YPBINDVERS_2);
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate /* For NC_INET, register v1 as well; error is fatal */
3780Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
3790Sstevel@tonic-gate (void) rpcb_unset(YPBINDPROG, YPBINDVERS_1,
3800Sstevel@tonic-gate nconf);
3810Sstevel@tonic-gate if (!svc_reg(xprt, YPBINDPROG, YPBINDVERS_1,
3820Sstevel@tonic-gate ypbindprog_2, nconf)) {
3830Sstevel@tonic-gate syslog(LOG_ERR,
3840Sstevel@tonic-gate "unable to register (YPBINDPROG, YPBINDVERS_1).");
3850Sstevel@tonic-gate exit(1);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate cache_transport(nconf, xprt, YPBINDVERS_1);
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_CLTS)
3920Sstevel@tonic-gate udp_found++;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET)) {
3950Sstevel@tonic-gate inet_tpts |= 1 >> nconf->nc_semantics;
3960Sstevel@tonic-gate } else {
3970Sstevel@tonic-gate inet6_tpts |= 1 >> nconf->nc_semantics;
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
4010Sstevel@tonic-gate loopback_found++;
4020Sstevel@tonic-gate if ((setok != YPSETNONE) &&
4030Sstevel@tonic-gate __rpc_negotiate_uid(xprt->xp_fd)) {
4040Sstevel@tonic-gate syslog(LOG_ERR,
4050Sstevel@tonic-gate "could not negotiate with loopback tranport %s",
4060Sstevel@tonic-gate nconf->nc_netid);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate /*
4090Sstevel@tonic-gate * On a diskless client:
4100Sstevel@tonic-gate * The parent waits for the child to close its end of
4110Sstevel@tonic-gate * the pipe (to indicate that it is ready to process
4120Sstevel@tonic-gate * requests). Now the diskless client will wait
4130Sstevel@tonic-gate * only if ypbind is registered on the loopback.
4140Sstevel@tonic-gate */
4150Sstevel@tonic-gate if ((!pipe_closed) &&
4160Sstevel@tonic-gate ((nconf->nc_semantics == NC_TPI_COTS) ||
4170Sstevel@tonic-gate (nconf->nc_semantics == NC_TPI_COTS_ORD))) {
4180Sstevel@tonic-gate close(pfd[1]);
4190Sstevel@tonic-gate pipe_closed = 1;
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate /* Did we manage to register all IPv4 or all IPv6 transports ? */
4250Sstevel@tonic-gate if (inet_tpts != 0 && inet_tpts != inet_desired_tpts) {
4260Sstevel@tonic-gate syslog(LOG_ERR,
4270Sstevel@tonic-gate "unable to register all %s transports, exiting..",
4280Sstevel@tonic-gate NC_INET);
4290Sstevel@tonic-gate exit(1);
4300Sstevel@tonic-gate } else if (inet6_tpts != 0 && inet6_tpts != inet6_desired_tpts) {
4310Sstevel@tonic-gate syslog(LOG_ERR,
4320Sstevel@tonic-gate "unable to register all %s transports, exiting..",
4330Sstevel@tonic-gate NC_INET6);
4340Sstevel@tonic-gate exit(1);
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate if (!pipe_closed) {
4380Sstevel@tonic-gate close(pfd[1]);
4390Sstevel@tonic-gate pipe_closed = 1;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate __rpc_endconf(nc_handle);
4420Sstevel@tonic-gate if (!loopback_found) {
4430Sstevel@tonic-gate syslog(LOG_ERR,
4440Sstevel@tonic-gate "could not find loopback transports, exiting..");
4450Sstevel@tonic-gate exit(1);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate if (!udp_found) {
4480Sstevel@tonic-gate syslog(LOG_ERR,
4490Sstevel@tonic-gate "could not find inet-clts (udp) transport, exiting..");
4500Sstevel@tonic-gate exit(1);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate set_signal_handlers();
4530Sstevel@tonic-gate svc_run();
4540Sstevel@tonic-gate syslog(LOG_ERR, "svc_run returned, exiting..");
4550Sstevel@tonic-gate exit(1);
4560Sstevel@tonic-gate /* NOTREACHED */
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate * Callback function for fdwalk() to close all files.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate static int
void_close(void * pfdp,int fd)4630Sstevel@tonic-gate void_close(void *pfdp, int fd)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate if (fd != *(int *)pfdp)
4660Sstevel@tonic-gate (void) close(fd);
4670Sstevel@tonic-gate return (0);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate void
ypbindprog_3(rqstp,transp)4710Sstevel@tonic-gate ypbindprog_3(rqstp, transp)
4720Sstevel@tonic-gate struct svc_req *rqstp;
4730Sstevel@tonic-gate register SVCXPRT *transp;
4740Sstevel@tonic-gate {
4750Sstevel@tonic-gate union {
4760Sstevel@tonic-gate ypbind_domain ypbindproc_domain_3_arg;
4770Sstevel@tonic-gate ypbind_setdom ypbindproc_setdom_3_arg;
4780Sstevel@tonic-gate } argument;
4790Sstevel@tonic-gate char *result;
4800Sstevel@tonic-gate bool_t (*xdr_argument)(), (*xdr_result)();
4810Sstevel@tonic-gate char *(*local)();
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate if (sigcld_event)
4840Sstevel@tonic-gate broadcast_proc_exit();
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate _rpcsvcdirty = 1;
4870Sstevel@tonic-gate switch (rqstp->rq_proc) {
4880Sstevel@tonic-gate case YPBINDPROC_NULL:
4890Sstevel@tonic-gate xdr_argument = xdr_void;
4900Sstevel@tonic-gate xdr_result = xdr_void;
4910Sstevel@tonic-gate local = (char *(*)()) ypbindproc_null_3;
4920Sstevel@tonic-gate break;
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate case YPBINDPROC_DOMAIN:
4950Sstevel@tonic-gate xdr_argument = xdr_ypbind_domain;
4960Sstevel@tonic-gate xdr_result = xdr_ypbind_resp;
4970Sstevel@tonic-gate local = (char *(*)()) ypbindproc_domain_3;
4980Sstevel@tonic-gate break;
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate case YPBINDPROC_SETDOM:
5010Sstevel@tonic-gate xdr_argument = xdr_ypbind_setdom;
5020Sstevel@tonic-gate xdr_result = xdr_void;
5030Sstevel@tonic-gate local = (char *(*)()) ypbindproc_setdom_3;
5040Sstevel@tonic-gate break;
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate default:
5070Sstevel@tonic-gate svcerr_noproc(transp);
5080Sstevel@tonic-gate _rpcsvcdirty = 0;
5090Sstevel@tonic-gate return;
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate (void) memset((char *)&argument, 0, sizeof (argument));
5120Sstevel@tonic-gate if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5130Sstevel@tonic-gate svcerr_decode(transp);
5140Sstevel@tonic-gate _rpcsvcdirty = 0;
5150Sstevel@tonic-gate return;
5160Sstevel@tonic-gate }
5170Sstevel@tonic-gate if (rqstp->rq_proc == YPBINDPROC_SETDOM)
5180Sstevel@tonic-gate result = (*local)(&argument, rqstp, transp);
5190Sstevel@tonic-gate else
5200Sstevel@tonic-gate result = (*local)(&argument, rqstp);
5210Sstevel@tonic-gate if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
5220Sstevel@tonic-gate svcerr_systemerr(transp);
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5250Sstevel@tonic-gate syslog(LOG_ERR, "unable to free arguments");
5260Sstevel@tonic-gate exit(1);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate _rpcsvcdirty = 0;
5290Sstevel@tonic-gate }
5300Sstevel@tonic-gate
5310Sstevel@tonic-gate void
ypbindprog_2(rqstp,transp)5320Sstevel@tonic-gate ypbindprog_2(rqstp, transp)
5330Sstevel@tonic-gate struct svc_req *rqstp;
5340Sstevel@tonic-gate register SVCXPRT *transp;
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate union {
5370Sstevel@tonic-gate domainname_2 ypbindproc_domain_2_arg;
5380Sstevel@tonic-gate ypbind_setdom_2 ypbindproc_setdom_2_arg;
5390Sstevel@tonic-gate } argument;
5400Sstevel@tonic-gate char *result;
5410Sstevel@tonic-gate bool_t (*xdr_argument)(), (*xdr_result)();
5420Sstevel@tonic-gate char *(*local)();
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate if (sigcld_event)
5450Sstevel@tonic-gate broadcast_proc_exit();
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate _rpcsvcdirty = 1;
5480Sstevel@tonic-gate switch (rqstp->rq_proc) {
5490Sstevel@tonic-gate case YPBINDPROC_NULL:
5500Sstevel@tonic-gate xdr_argument = xdr_void;
5510Sstevel@tonic-gate xdr_result = xdr_void;
5520Sstevel@tonic-gate /* XXX - don't need two null procedures */
5530Sstevel@tonic-gate local = (char *(*)()) ypbindproc_null_3;
5540Sstevel@tonic-gate break;
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate case YPBINDPROC_DOMAIN:
5570Sstevel@tonic-gate xdr_argument = (bool_t (*)())xdr_ypdomain_wrap_string;
5580Sstevel@tonic-gate xdr_result = xdr_ypbind_resp_2;
5590Sstevel@tonic-gate local = (char *(*)()) ypbindproc_domain_2;
5600Sstevel@tonic-gate break;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate case YPBINDPROC_SETDOM: /* not supported, fall through to error */
5630Sstevel@tonic-gate default:
5640Sstevel@tonic-gate svcerr_noproc(transp);
5650Sstevel@tonic-gate _rpcsvcdirty = 0;
5660Sstevel@tonic-gate return;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate (void) memset((char *)&argument, 0, sizeof (argument));
5690Sstevel@tonic-gate if (!svc_getargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5700Sstevel@tonic-gate svcerr_decode(transp);
5710Sstevel@tonic-gate _rpcsvcdirty = 0;
5720Sstevel@tonic-gate return;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate result = (*local)(&argument, rqstp);
5750Sstevel@tonic-gate if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
5760Sstevel@tonic-gate svcerr_systemerr(transp);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, (char *)&argument)) {
5790Sstevel@tonic-gate syslog(LOG_ERR, "unable to free arguments");
5800Sstevel@tonic-gate exit(1);
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate _rpcsvcdirty = 0;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /*
5860Sstevel@tonic-gate * We clear out any old bindings that might have been
5870Sstevel@tonic-gate * left behind. If there is already a ypbind running,
5880Sstevel@tonic-gate * it will no longer get requests. We are in control
5890Sstevel@tonic-gate * now. We ignore the error from rpcb_unset() because
5900Sstevel@tonic-gate * this is just a "best effort". If the rpcb_unset()
5910Sstevel@tonic-gate * does fail, we will get an error in svc_reg(). By
5920Sstevel@tonic-gate * using 0 for the last argument we are telling the
5930Sstevel@tonic-gate * portmapper to remove the bindings for all transports.
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate static
5960Sstevel@tonic-gate void
clear_bindings()5970Sstevel@tonic-gate clear_bindings()
5980Sstevel@tonic-gate {
5990Sstevel@tonic-gate rpcb_unset(YPBINDPROG, YPBINDVERS, 0);
6000Sstevel@tonic-gate rpcb_unset(YPBINDPROG, YPBINDVERS_2, 0);
6010Sstevel@tonic-gate rpcb_unset(YPBINDPROG, YPBINDVERS_1, 0);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * This routine is called when we are killed (by most signals).
6060Sstevel@tonic-gate * It first tries to unregister with the portmapper. Then it
6070Sstevel@tonic-gate * resets the signal handler to the default so that if we get
6080Sstevel@tonic-gate * the same signal, we will just go away. We clean up our
6090Sstevel@tonic-gate * children by doing a hold in SIGTERM and then killing the
6100Sstevel@tonic-gate * process group (-getpid()) with SIGTERM. Finally, we redeliver
6110Sstevel@tonic-gate * the signal to ourselves (the handler was reset to the default)
6120Sstevel@tonic-gate * so that we will do the normal handling (e.g., coredump).
6130Sstevel@tonic-gate * If we can't kill ourselves, we get drastic and just exit
6140Sstevel@tonic-gate * after sleeping for a couple of seconds.
6150Sstevel@tonic-gate *
6160Sstevel@tonic-gate * This code was taken from the SunOS version of ypbind.
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate static
6190Sstevel@tonic-gate void
unregister(int code)6200Sstevel@tonic-gate unregister(int code)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate clear_bindings();
6230Sstevel@tonic-gate clean_cache();
6240Sstevel@tonic-gate signal(code, SIG_DFL); /* to prevent recursive calls to unregister */
6250Sstevel@tonic-gate fprintf(stderr, "ypbind: goind down on signal %d\n", code);
6260Sstevel@tonic-gate sighold(SIGCHLD);
6270Sstevel@tonic-gate sighold(SIGTERM);
6280Sstevel@tonic-gate kill(-getpid(), SIGTERM); /* kill process group (i.e., children) */
6290Sstevel@tonic-gate sigrelse(SIGTERM);
6300Sstevel@tonic-gate kill(getpid(), code); /* throw signal again */
6310Sstevel@tonic-gate sleep(2);
6320Sstevel@tonic-gate exit(-1);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate static
6360Sstevel@tonic-gate void
set_signal_handlers()6370Sstevel@tonic-gate set_signal_handlers()
6380Sstevel@tonic-gate {
6390Sstevel@tonic-gate int i;
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate for (i = 1; i <= SIGTERM; i++) {
6420Sstevel@tonic-gate if (i == SIGCHLD)
6430Sstevel@tonic-gate continue;
6440Sstevel@tonic-gate else if (i == SIGHUP)
6450Sstevel@tonic-gate signal(i, SIG_IGN);
6460Sstevel@tonic-gate else
6470Sstevel@tonic-gate signal(i, unregister);
6480Sstevel@tonic-gate }
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate void
msgout(msg)6520Sstevel@tonic-gate msgout(msg)
6530Sstevel@tonic-gate char *msg;
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate #ifdef RPC_SVC_FG
6560Sstevel@tonic-gate if (_rpcpmstart)
6570Sstevel@tonic-gate syslog(LOG_ERR, msg);
6580Sstevel@tonic-gate else
6590Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msg);
6600Sstevel@tonic-gate #else
6610Sstevel@tonic-gate syslog(LOG_ERR, msg);
6620Sstevel@tonic-gate #endif
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate void
closedown()6660Sstevel@tonic-gate closedown()
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate if (_rpcsvcdirty == 0) {
6690Sstevel@tonic-gate int i, openfd;
6700Sstevel@tonic-gate struct t_info tinfo;
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate if (t_getinfo(0, &tinfo) || (tinfo.servtype == T_CLTS))
6730Sstevel@tonic-gate exit(0);
6740Sstevel@tonic-gate
6750Sstevel@tonic-gate for (i = 0, openfd = 0; i < svc_max_pollfd && openfd < 2; i++)
6760Sstevel@tonic-gate if (svc_pollfd[i].fd >= 0)
6770Sstevel@tonic-gate openfd++;
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate if (openfd <= 1)
6800Sstevel@tonic-gate exit(0);
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate (void) alarm(_RPCSVC_CLOSEDOWN);
6830Sstevel@tonic-gate }
684