17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 545916cd2Sjpk * Common Development and Distribution License (the "License"). 645916cd2Sjpk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2245916cd2Sjpk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 25*8f6d9daeSMarcel Telka /* 26*8f6d9daeSMarcel Telka * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 27*8f6d9daeSMarcel Telka */ 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 41*8f6d9daeSMarcel Telka * This is an implementation of RCPBIND according the RFC 1833: Binding 42*8f6d9daeSMarcel Telka * Protocols for ONC RPC Version 2. The RFC specifies three versions of the 43*8f6d9daeSMarcel Telka * binding protocol: 44*8f6d9daeSMarcel Telka * 45*8f6d9daeSMarcel Telka * 1) RPCBIND Version 3 (Section 2.2.1 of the RFC) 46*8f6d9daeSMarcel Telka * 2) RPCBIND, Version 4 (Section 2.2.2 of the RFC) 47*8f6d9daeSMarcel Telka * 3) Port Mapper Program Protocol (Section 3 of the RFC) 48*8f6d9daeSMarcel Telka * 49*8f6d9daeSMarcel Telka * Where the "Port Mapper Program Protocol" is refered as Version 2 of the 50*8f6d9daeSMarcel Telka * binding protocol. The implementation of the Version 2 of the binding 51*8f6d9daeSMarcel Telka * protocol is compiled in only in a case the PORTMAP macro is defined (by 52*8f6d9daeSMarcel Telka * default it is defined). 53*8f6d9daeSMarcel Telka * 54*8f6d9daeSMarcel Telka * The implementation is based on top of the networking services library - 55*8f6d9daeSMarcel Telka * libnsl(3lib) and uses Automatic MT mode (see rcp_control(3nsl) and 56*8f6d9daeSMarcel Telka * svc_run(3nsl) for more details). 57*8f6d9daeSMarcel Telka * 58*8f6d9daeSMarcel Telka * Usually, when a thread handles an RPCBIND procedure (one that arrived from a 59*8f6d9daeSMarcel Telka * client), it obtains the data for the response internally, and immediately 60*8f6d9daeSMarcel Telka * sends the response back to the client. The only exception to this rule are 61*8f6d9daeSMarcel Telka * remote (aka indirect) RPC calls, for example RPCBPROC_INDIRECT. Such 62*8f6d9daeSMarcel Telka * procedures are designed to forward the RPC request from the client to some 63*8f6d9daeSMarcel Telka * other RPC service specified by the client, wait for the result, and forward 64*8f6d9daeSMarcel Telka * the result back to the client. This is implemented in rpcbproc_callit_com(). 657c478bd9Sstevel@tonic-gate * 66*8f6d9daeSMarcel Telka * The response from the other (remote) RPC service is handled in 67*8f6d9daeSMarcel Telka * handle_reply(), where the thread waiting in rpcbproc_callit_com() is woken 68*8f6d9daeSMarcel Telka * up to finish the handling and to send (forward) the response back to the 69*8f6d9daeSMarcel Telka * client. 70*8f6d9daeSMarcel Telka * 71*8f6d9daeSMarcel Telka * The thread implementing the indirect RPC call might be blocked in the 72*8f6d9daeSMarcel Telka * rpcbproc_callit_com() waiting for the response from the other RPC service 73*8f6d9daeSMarcel Telka * for very long time. During this time the thread is unable to handle other 74*8f6d9daeSMarcel Telka * RPCBIND requests. To avoid a case when all threads are waiting in 75*8f6d9daeSMarcel Telka * rpcbproc_callit_com() and there is no free thread able to handle other 76*8f6d9daeSMarcel Telka * RPCBIND requests, the implementation has reserved eight threads to never be 77*8f6d9daeSMarcel Telka * used for the remote RPC calls. The number of active remote RPC calls is in 78*8f6d9daeSMarcel Telka * rpcb_rmtcalls, the upper limit of such calls is in rpcb_rmtcalls_max. 79*8f6d9daeSMarcel Telka * 80*8f6d9daeSMarcel Telka * In addition to the worker threads described above, there are two other 81*8f6d9daeSMarcel Telka * threads. The logthread() thread is responsible for asynchronous logging to 82*8f6d9daeSMarcel Telka * syslog. The terminate() thread is signal handler responsible for reload of 83*8f6d9daeSMarcel Telka * the rpcbind configuration (on SIGHUP), or for gracefully shutting down 84*8f6d9daeSMarcel Telka * rpcbind (otherwise). 85*8f6d9daeSMarcel Telka * 86*8f6d9daeSMarcel Telka * There are two global lists used for holding the information about the 87*8f6d9daeSMarcel Telka * registered services: list_rbl is for Version 3 and 4 of the binding 88*8f6d9daeSMarcel Telka * protocol, and list_pml is for Version 2. To protect these lists, two global 89*8f6d9daeSMarcel Telka * readers/writer locks are defined and heavily used across the rpcbind 90*8f6d9daeSMarcel Telka * implementation: list_rbl_lock protecting list_rbl, and list_pml_lock, 91*8f6d9daeSMarcel Telka * protecting list_pml. 92*8f6d9daeSMarcel Telka * 93*8f6d9daeSMarcel Telka * The defined locking order is: list_rbl_lock first, list_pml_lock second. 947c478bd9Sstevel@tonic-gate */ 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate #include <dlfcn.h> 977c478bd9Sstevel@tonic-gate #include <stdio.h> 98004388ebScasper #include <stdio_ext.h> 997c478bd9Sstevel@tonic-gate #include <sys/types.h> 1007c478bd9Sstevel@tonic-gate #include <unistd.h> 1017c478bd9Sstevel@tonic-gate #include <stdlib.h> 1027c478bd9Sstevel@tonic-gate #include <string.h> 1037c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 1047c478bd9Sstevel@tonic-gate #include <netconfig.h> 1057c478bd9Sstevel@tonic-gate #include <netdir.h> 1067c478bd9Sstevel@tonic-gate #include <errno.h> 1077c478bd9Sstevel@tonic-gate #include <sys/wait.h> 1087c478bd9Sstevel@tonic-gate #include <signal.h> 1097c478bd9Sstevel@tonic-gate #include <string.h> 1107c478bd9Sstevel@tonic-gate #include <stdlib.h> 1117c478bd9Sstevel@tonic-gate #include <thread.h> 1127c478bd9Sstevel@tonic-gate #include <synch.h> 1137c478bd9Sstevel@tonic-gate #include <stdarg.h> 1147c478bd9Sstevel@tonic-gate #ifdef PORTMAP 1157c478bd9Sstevel@tonic-gate #include <netinet/in.h> 1167c478bd9Sstevel@tonic-gate #endif 1177c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 1187c478bd9Sstevel@tonic-gate #include <sys/termios.h> 1197c478bd9Sstevel@tonic-gate #include "rpcbind.h" 1207c478bd9Sstevel@tonic-gate #include <sys/syslog.h> 1217c478bd9Sstevel@tonic-gate #include <sys/stat.h> 1227c478bd9Sstevel@tonic-gate #include <syslog.h> 1237c478bd9Sstevel@tonic-gate #include <string.h> 1247c478bd9Sstevel@tonic-gate #include <sys/time.h> 1257c478bd9Sstevel@tonic-gate #include <sys/resource.h> 1267c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h> 1277c478bd9Sstevel@tonic-gate #include <priv_utils.h> 1287c478bd9Sstevel@tonic-gate #include <libscf.h> 1299e293969SRichard Lowe #include <sys/ccompile.h> 130*8f6d9daeSMarcel Telka #include <zone.h> 131*8f6d9daeSMarcel Telka #include <ctype.h> 132*8f6d9daeSMarcel Telka #include <limits.h> 133*8f6d9daeSMarcel Telka #include <assert.h> 1347c478bd9Sstevel@tonic-gate 135*8f6d9daeSMarcel Telka static sigset_t sigwaitset; 1367c478bd9Sstevel@tonic-gate 137*8f6d9daeSMarcel Telka static void terminate(void); 1387c478bd9Sstevel@tonic-gate static void detachfromtty(void); 1397c478bd9Sstevel@tonic-gate static void parseargs(int, char *[]); 1407c478bd9Sstevel@tonic-gate static void rbllist_add(ulong_t, ulong_t, struct netconfig *, struct netbuf *); 1417c478bd9Sstevel@tonic-gate static int init_transport(struct netconfig *); 1427c478bd9Sstevel@tonic-gate static int check_netconfig(void); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate static boolean_t check_hostserv(struct netconfig *, const char *, const char *); 14545916cd2Sjpk static int setopt_reuseaddr(int); 14645916cd2Sjpk static int setopt_anon_mlp(int); 14745916cd2Sjpk static int setup_callit(int); 1487c478bd9Sstevel@tonic-gate 149*8f6d9daeSMarcel Telka static void rpcb_check_init(void); 150*8f6d9daeSMarcel Telka 1517c478bd9Sstevel@tonic-gate /* Global variables */ 1527c478bd9Sstevel@tonic-gate int debugging = 0; /* Tell me what's going on */ 1537c478bd9Sstevel@tonic-gate static int ipv6flag = 0; 1547c478bd9Sstevel@tonic-gate int doabort = 0; /* When debugging, do an abort on errors */ 155*8f6d9daeSMarcel Telka static int listen_backlog; 156*8f6d9daeSMarcel Telka static const int reserved_threads = 8; 157*8f6d9daeSMarcel Telka 158*8f6d9daeSMarcel Telka /* 159*8f6d9daeSMarcel Telka * list_rbl_lock protects list_rbl 160*8f6d9daeSMarcel Telka * lock order: list_rbl_lock, list_pml_lock 161*8f6d9daeSMarcel Telka */ 162*8f6d9daeSMarcel Telka rwlock_t list_rbl_lock = DEFAULTRWLOCK; 1637c478bd9Sstevel@tonic-gate rpcblist_ptr list_rbl; /* A list of version 3/4 rpcbind services */ 164*8f6d9daeSMarcel Telka 1657c478bd9Sstevel@tonic-gate char *loopback_dg; /* Datagram loopback transport, for set and unset */ 1667c478bd9Sstevel@tonic-gate char *loopback_vc; /* COTS loopback transport, for set and unset */ 1677c478bd9Sstevel@tonic-gate char *loopback_vc_ord; /* COTS_ORD loopback transport, for set and unset */ 1687c478bd9Sstevel@tonic-gate 169*8f6d9daeSMarcel Telka volatile boolean_t verboselog = B_FALSE; 170*8f6d9daeSMarcel Telka volatile boolean_t wrap_enabled = B_FALSE; 171*8f6d9daeSMarcel Telka volatile boolean_t allow_indirect = B_TRUE; 172*8f6d9daeSMarcel Telka volatile boolean_t local_only = B_TRUE; 173064ed339Sjjj 1747c478bd9Sstevel@tonic-gate /* Local Variable */ 1757c478bd9Sstevel@tonic-gate static int warmstart = 0; /* Grab a old copy of registrations */ 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate #ifdef PORTMAP 178*8f6d9daeSMarcel Telka /* 179*8f6d9daeSMarcel Telka * list_pml_lock protects list_pml 180*8f6d9daeSMarcel Telka * lock order: list_rbl_lock, list_pml_lock 181*8f6d9daeSMarcel Telka */ 182*8f6d9daeSMarcel Telka rwlock_t list_pml_lock = DEFAULTRWLOCK; 1837c478bd9Sstevel@tonic-gate PMAPLIST *list_pml; /* A list of version 2 rpcbind services */ 184*8f6d9daeSMarcel Telka 1857c478bd9Sstevel@tonic-gate char *udptrans; /* Name of UDP transport */ 1867c478bd9Sstevel@tonic-gate char *tcptrans; /* Name of TCP transport */ 1877c478bd9Sstevel@tonic-gate char *udp_uaddr; /* Universal UDP address */ 1887c478bd9Sstevel@tonic-gate char *tcp_uaddr; /* Universal TCP address */ 1897c478bd9Sstevel@tonic-gate #endif 1907c478bd9Sstevel@tonic-gate static char servname[] = "rpcbind"; 1917c478bd9Sstevel@tonic-gate static char superuser[] = "superuser"; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate static const char daemon_dir[] = DAEMON_DIR; 1947c478bd9Sstevel@tonic-gate 195*8f6d9daeSMarcel Telka static void 196*8f6d9daeSMarcel Telka block_signals(void) 197*8f6d9daeSMarcel Telka { 198*8f6d9daeSMarcel Telka (void) sigemptyset(&sigwaitset); 199*8f6d9daeSMarcel Telka (void) sigaddset(&sigwaitset, SIGINT); 200*8f6d9daeSMarcel Telka (void) sigaddset(&sigwaitset, SIGTERM); 201*8f6d9daeSMarcel Telka (void) sigaddset(&sigwaitset, SIGQUIT); 202*8f6d9daeSMarcel Telka (void) sigaddset(&sigwaitset, SIGHUP); 203*8f6d9daeSMarcel Telka (void) sigprocmask(SIG_BLOCK, &sigwaitset, NULL); 204*8f6d9daeSMarcel Telka 205*8f6d9daeSMarcel Telka /* ignore other signals that could get sent */ 206*8f6d9daeSMarcel Telka (void) signal(SIGUSR1, SIG_IGN); 207*8f6d9daeSMarcel Telka (void) signal(SIGUSR2, SIG_IGN); 208*8f6d9daeSMarcel Telka } 209*8f6d9daeSMarcel Telka 2107c478bd9Sstevel@tonic-gate int 2117c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate struct netconfig *nconf; 2147c478bd9Sstevel@tonic-gate void *nc_handle; /* Net config handle */ 2157c478bd9Sstevel@tonic-gate struct rlimit rl; 216*8f6d9daeSMarcel Telka int rpc_svc_fdunlim = 1; 217*8f6d9daeSMarcel Telka int rpc_svc_mode = RPC_SVC_MT_AUTO; 2187c478bd9Sstevel@tonic-gate int maxrecsz = RPC_MAXDATASIZE; 21945916cd2Sjpk boolean_t can_do_mlp; 2207c478bd9Sstevel@tonic-gate 221*8f6d9daeSMarcel Telka block_signals(); 2227c478bd9Sstevel@tonic-gate 223*8f6d9daeSMarcel Telka parseargs(argc, argv); 2247c478bd9Sstevel@tonic-gate 225*8f6d9daeSMarcel Telka if (getrlimit(RLIMIT_NOFILE, &rl) != 0) { 226*8f6d9daeSMarcel Telka syslog(LOG_ERR, "getrlimit failed"); 227*8f6d9daeSMarcel Telka } else { 228*8f6d9daeSMarcel Telka rl.rlim_cur = rl.rlim_max; 229*8f6d9daeSMarcel Telka if (setrlimit(RLIMIT_NOFILE, &rl) != 0) 230*8f6d9daeSMarcel Telka syslog(LOG_ERR, "setrlimit failed"); 2317c478bd9Sstevel@tonic-gate } 232004388ebScasper (void) enable_extended_FILE_stdio(-1, -1); 233004388ebScasper 2347c478bd9Sstevel@tonic-gate openlog("rpcbind", LOG_CONS, LOG_DAEMON); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Create the daemon directory in /var/run 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate if (mkdir(daemon_dir, DAEMON_DIR_MODE) == 0 || errno == EEXIST) { 2407c478bd9Sstevel@tonic-gate chmod(daemon_dir, DAEMON_DIR_MODE); 2417c478bd9Sstevel@tonic-gate chown(daemon_dir, DAEMON_UID, DAEMON_GID); 2427c478bd9Sstevel@tonic-gate } else { 2437c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "failed to create \"%s\": %m", daemon_dir); 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate 2467c478bd9Sstevel@tonic-gate /* 2477c478bd9Sstevel@tonic-gate * These privileges are required for the t_bind check rpcbind uses 2487c478bd9Sstevel@tonic-gate * to determine whether a service is still live or not. 2497c478bd9Sstevel@tonic-gate */ 25045916cd2Sjpk can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP); 2517c478bd9Sstevel@tonic-gate if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, DAEMON_UID, 25245916cd2Sjpk DAEMON_GID, PRIV_NET_PRIVADDR, PRIV_SYS_NFS, 25345916cd2Sjpk can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) { 2547c478bd9Sstevel@tonic-gate fprintf(stderr, "Insufficient privileges\n"); 2557c478bd9Sstevel@tonic-gate exit(1); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 258*8f6d9daeSMarcel Telka myzone = getzoneid(); 259*8f6d9daeSMarcel Telka 260*8f6d9daeSMarcel Telka /* 261*8f6d9daeSMarcel Telka * Set number of file descriptors to unlimited 262*8f6d9daeSMarcel Telka */ 263*8f6d9daeSMarcel Telka if (!rpc_control(RPC_SVC_USE_POLLFD, &rpc_svc_fdunlim)) { 264*8f6d9daeSMarcel Telka syslog(LOG_INFO, "unable to set number of FD to unlimited"); 265*8f6d9daeSMarcel Telka } 266*8f6d9daeSMarcel Telka 267*8f6d9daeSMarcel Telka /* 268*8f6d9daeSMarcel Telka * Tell RPC that we want automatic thread mode. 269*8f6d9daeSMarcel Telka * A new thread will be spawned for each request. 270*8f6d9daeSMarcel Telka */ 271*8f6d9daeSMarcel Telka if (!rpc_control(RPC_SVC_MTMODE_SET, &rpc_svc_mode)) { 272*8f6d9daeSMarcel Telka syslog(LOG_ERR, "unable to set automatic MT mode"); 273*8f6d9daeSMarcel Telka exit(1); 274*8f6d9daeSMarcel Telka } 275*8f6d9daeSMarcel Telka 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * Enable non-blocking mode and maximum record size checks for 2787c478bd9Sstevel@tonic-gate * connection oriented transports. 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) { 2817c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "unable to set RPC max record size"); 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate /* 2867c478bd9Sstevel@tonic-gate * rpcbind is the first application to encounter the 2877c478bd9Sstevel@tonic-gate * various netconfig files. check_netconfig() verifies 2887c478bd9Sstevel@tonic-gate * that they are set up correctly and complains loudly 2897c478bd9Sstevel@tonic-gate * if not. 2907c478bd9Sstevel@tonic-gate */ 2917c478bd9Sstevel@tonic-gate int trouble; 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate trouble = check_netconfig(); 2947c478bd9Sstevel@tonic-gate if (trouble) { 295*8f6d9daeSMarcel Telka syslog(LOG_ERR, "%s: found %d errors with network " 296*8f6d9daeSMarcel Telka "configuration files. Exiting.", argv[0], trouble); 297*8f6d9daeSMarcel Telka fprintf(stderr, "%s: found %d errors with network " 298*8f6d9daeSMarcel Telka "configuration files. Exiting.\n", 2999e293969SRichard Lowe argv[0], trouble); 3007c478bd9Sstevel@tonic-gate exit(1); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate } 303*8f6d9daeSMarcel Telka 304*8f6d9daeSMarcel Telka loopback_dg = ""; 305*8f6d9daeSMarcel Telka loopback_vc = ""; 306*8f6d9daeSMarcel Telka loopback_vc_ord = ""; 307*8f6d9daeSMarcel Telka #ifdef PORTMAP 308*8f6d9daeSMarcel Telka udptrans = ""; 309*8f6d9daeSMarcel Telka tcptrans = ""; 310*8f6d9daeSMarcel Telka #endif 311*8f6d9daeSMarcel Telka 3127c478bd9Sstevel@tonic-gate ipv6flag = Is_ipv6present(); 3137c478bd9Sstevel@tonic-gate rpcb_check_init(); 314*8f6d9daeSMarcel Telka 315*8f6d9daeSMarcel Telka nc_handle = setnetconfig(); /* open netconfig file */ 316*8f6d9daeSMarcel Telka if (nc_handle == NULL) { 317*8f6d9daeSMarcel Telka syslog(LOG_ERR, "could not read /etc/netconfig"); 318*8f6d9daeSMarcel Telka exit(1); 319*8f6d9daeSMarcel Telka } 320*8f6d9daeSMarcel Telka while ((nconf = getnetconfig(nc_handle)) != NULL) { 3217c478bd9Sstevel@tonic-gate if (nconf->nc_flag & NC_VISIBLE) 3227c478bd9Sstevel@tonic-gate init_transport(nconf); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate endnetconfig(nc_handle); 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate if ((loopback_dg[0] == NULL) && (loopback_vc[0] == NULL) && 3279e293969SRichard Lowe (loopback_vc_ord[0] == NULL)) { 3287c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "could not find loopback transports"); 3297c478bd9Sstevel@tonic-gate exit(1); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate if (warmstart) { 3337c478bd9Sstevel@tonic-gate read_warmstart(); 3347c478bd9Sstevel@tonic-gate } 335*8f6d9daeSMarcel Telka 336*8f6d9daeSMarcel Telka /* Create terminate signal handler for graceful exit */ 337*8f6d9daeSMarcel Telka if (thr_create(NULL, 0, (void *(*)(void *))terminate, NULL, 0, NULL)) { 338*8f6d9daeSMarcel Telka syslog(LOG_ERR, "Failed to create terminate thread"); 339*8f6d9daeSMarcel Telka exit(1); 340*8f6d9daeSMarcel Telka } 341*8f6d9daeSMarcel Telka 3427c478bd9Sstevel@tonic-gate if (debugging) { 3437c478bd9Sstevel@tonic-gate printf("rpcbind debugging enabled."); 3447c478bd9Sstevel@tonic-gate if (doabort) { 3457c478bd9Sstevel@tonic-gate printf(" Will abort on errors!\n"); 3467c478bd9Sstevel@tonic-gate } else { 3477c478bd9Sstevel@tonic-gate printf("\n"); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate } else { 3507c478bd9Sstevel@tonic-gate detachfromtty(); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* These are basic privileges we do not need */ 3547c478bd9Sstevel@tonic-gate __fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_SESSION, 3557c478bd9Sstevel@tonic-gate PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL); 3567c478bd9Sstevel@tonic-gate 357*8f6d9daeSMarcel Telka svc_run(); 3587c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "svc_run returned unexpectedly"); 3597c478bd9Sstevel@tonic-gate rpcbind_abort(); 3607c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate /* 3647c478bd9Sstevel@tonic-gate * Increments a counter each time a problem is found with the network 3657c478bd9Sstevel@tonic-gate * configuration information. 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate static int 3687c478bd9Sstevel@tonic-gate check_netconfig(void) 3697c478bd9Sstevel@tonic-gate { 3707c478bd9Sstevel@tonic-gate void *nc; 3717c478bd9Sstevel@tonic-gate void *dlcookie; 3727c478bd9Sstevel@tonic-gate int busted = 0; 3737c478bd9Sstevel@tonic-gate int i; 3747c478bd9Sstevel@tonic-gate int lo_clts_found = 0, lo_cots_found = 0, lo_cotsord_found = 0; 3757c478bd9Sstevel@tonic-gate struct netconfig *nconf, *np; 3767c478bd9Sstevel@tonic-gate struct stat sb; 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate nc = setnetconfig(); 3797c478bd9Sstevel@tonic-gate if (nc == NULL) { 3807c478bd9Sstevel@tonic-gate if (debugging) 3817c478bd9Sstevel@tonic-gate fprintf(stderr, 3829e293969SRichard Lowe "setnetconfig() failed: %s\n", nc_sperror()); 3837c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "setnetconfig() failed: %s", nc_sperror()); 3847c478bd9Sstevel@tonic-gate return (1); 3857c478bd9Sstevel@tonic-gate } 386*8f6d9daeSMarcel Telka while ((np = getnetconfig(nc)) != NULL) { 3877c478bd9Sstevel@tonic-gate if ((np->nc_flag & NC_VISIBLE) == 0) 3887c478bd9Sstevel@tonic-gate continue; 3897c478bd9Sstevel@tonic-gate if (debugging) 3907c478bd9Sstevel@tonic-gate fprintf(stderr, "checking netid \"%s\"\n", 3919e293969SRichard Lowe np->nc_netid); 3927c478bd9Sstevel@tonic-gate if (strcmp(np->nc_protofmly, NC_LOOPBACK) == 0) 3937c478bd9Sstevel@tonic-gate switch (np->nc_semantics) { 3947c478bd9Sstevel@tonic-gate case NC_TPI_CLTS: 3957c478bd9Sstevel@tonic-gate lo_clts_found = 1; 3967c478bd9Sstevel@tonic-gate break; 3977c478bd9Sstevel@tonic-gate 3987c478bd9Sstevel@tonic-gate case NC_TPI_COTS: 3997c478bd9Sstevel@tonic-gate lo_cots_found = 1; 4007c478bd9Sstevel@tonic-gate break; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate case NC_TPI_COTS_ORD: 4037c478bd9Sstevel@tonic-gate lo_cotsord_found = 1; 4047c478bd9Sstevel@tonic-gate break; 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate if (stat(np->nc_device, &sb) == -1 && errno == ENOENT) { 4077c478bd9Sstevel@tonic-gate if (debugging) 4087c478bd9Sstevel@tonic-gate fprintf(stderr, "\tdevice %s does not exist\n", 4099e293969SRichard Lowe np->nc_device); 4107c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "netid %s: device %s does not exist", 4119e293969SRichard Lowe np->nc_netid, np->nc_device); 4127c478bd9Sstevel@tonic-gate busted++; 4137c478bd9Sstevel@tonic-gate } else 4147c478bd9Sstevel@tonic-gate if (debugging) 4157c478bd9Sstevel@tonic-gate fprintf(stderr, "\tdevice %s present\n", 4169e293969SRichard Lowe np->nc_device); 4177c478bd9Sstevel@tonic-gate for (i = 0; i < np->nc_nlookups; i++) { 4187c478bd9Sstevel@tonic-gate char *libname = np->nc_lookups[i]; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate if ((dlcookie = dlopen(libname, RTLD_LAZY)) == NULL) { 4217c478bd9Sstevel@tonic-gate char *dlerrstr; 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate dlerrstr = dlerror(); 4247c478bd9Sstevel@tonic-gate if (debugging) { 425*8f6d9daeSMarcel Telka fprintf(stderr, "\tnetid %s: dlopen of " 426*8f6d9daeSMarcel Telka "name-to-address library %s " 427*8f6d9daeSMarcel Telka "failed\ndlerror: %s", 4289e293969SRichard Lowe np->nc_netid, libname, 4299e293969SRichard Lowe dlerrstr ? dlerrstr : ""); 4307c478bd9Sstevel@tonic-gate } 431*8f6d9daeSMarcel Telka syslog(LOG_ERR, "netid %s: dlopen of " 432*8f6d9daeSMarcel Telka "name-to-address library %s failed", 4339e293969SRichard Lowe np->nc_netid, libname); 4347c478bd9Sstevel@tonic-gate if (dlerrstr) 4357c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s", dlerrstr); 4367c478bd9Sstevel@tonic-gate busted++; 4377c478bd9Sstevel@tonic-gate } else { 4387c478bd9Sstevel@tonic-gate if (debugging) 439*8f6d9daeSMarcel Telka fprintf(stderr, "\tdlopen of " 440*8f6d9daeSMarcel Telka "name-to-address library %s " 441*8f6d9daeSMarcel Telka "succeeded\n", libname); 4427c478bd9Sstevel@tonic-gate (void) dlclose(dlcookie); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate } 4457c478bd9Sstevel@tonic-gate nconf = getnetconfigent(np->nc_netid); 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate if (!check_hostserv(nconf, HOST_SELF, "")) 4487c478bd9Sstevel@tonic-gate busted++; 4497c478bd9Sstevel@tonic-gate if (!check_hostserv(nconf, HOST_SELF_CONNECT, "")) 4507c478bd9Sstevel@tonic-gate busted++; 4517c478bd9Sstevel@tonic-gate if (!check_hostserv(nconf, HOST_SELF, "rpcbind")) 4527c478bd9Sstevel@tonic-gate busted++; 4537c478bd9Sstevel@tonic-gate if (!check_hostserv(nconf, HOST_SELF_CONNECT, "rpcbind")) 4547c478bd9Sstevel@tonic-gate busted++; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate freenetconfigent(nconf); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate endnetconfig(nc); 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate if (lo_clts_found) { 4617c478bd9Sstevel@tonic-gate if (debugging) 4627c478bd9Sstevel@tonic-gate fprintf(stderr, "Found CLTS loopback transport\n"); 4637c478bd9Sstevel@tonic-gate } else { 4647c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "no CLTS loopback transport found\n"); 4657c478bd9Sstevel@tonic-gate if (debugging) 4667c478bd9Sstevel@tonic-gate fprintf(stderr, "no CLTS loopback transport found\n"); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate if (lo_cots_found) { 4697c478bd9Sstevel@tonic-gate if (debugging) 4707c478bd9Sstevel@tonic-gate fprintf(stderr, "Found COTS loopback transport\n"); 4717c478bd9Sstevel@tonic-gate } else { 4727c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "no COTS loopback transport found\n"); 4737c478bd9Sstevel@tonic-gate if (debugging) 4747c478bd9Sstevel@tonic-gate fprintf(stderr, "no COTS loopback transport found\n"); 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate if (lo_cotsord_found) { 4777c478bd9Sstevel@tonic-gate if (debugging) 4787c478bd9Sstevel@tonic-gate fprintf(stderr, "Found COTS ORD loopback transport\n"); 4797c478bd9Sstevel@tonic-gate } else { 4807c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "no COTS ORD loopback transport found\n"); 4817c478bd9Sstevel@tonic-gate if (debugging) 4827c478bd9Sstevel@tonic-gate fprintf(stderr, 4839e293969SRichard Lowe "no COTS ORD loopback transport found\n"); 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate return (busted); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate /* 4907c478bd9Sstevel@tonic-gate * Adds the entry into the rpcbind database. 4917c478bd9Sstevel@tonic-gate * If PORTMAP, then for UDP and TCP, it adds the entries for version 2 also 4927c478bd9Sstevel@tonic-gate * Returns 0 if succeeds, else fails 4937c478bd9Sstevel@tonic-gate */ 4947c478bd9Sstevel@tonic-gate static int 4957c478bd9Sstevel@tonic-gate init_transport(struct netconfig *nconf) 4967c478bd9Sstevel@tonic-gate { 4977c478bd9Sstevel@tonic-gate int fd; 4987c478bd9Sstevel@tonic-gate struct t_bind *taddr, *baddr; 4997c478bd9Sstevel@tonic-gate SVCXPRT *my_xprt; 5007c478bd9Sstevel@tonic-gate struct nd_addrlist *nas; 5017c478bd9Sstevel@tonic-gate struct nd_hostserv hs; 5027c478bd9Sstevel@tonic-gate static int msgprt = 0; 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate if ((nconf->nc_semantics != NC_TPI_CLTS) && 5059e293969SRichard Lowe (nconf->nc_semantics != NC_TPI_COTS) && 5069e293969SRichard Lowe (nconf->nc_semantics != NC_TPI_COTS_ORD)) 5077c478bd9Sstevel@tonic-gate return (1); /* not my type */ 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && !ipv6flag) { 5107c478bd9Sstevel@tonic-gate if (!msgprt) 511*8f6d9daeSMarcel Telka syslog(LOG_DEBUG, "/etc/netconfig has IPv6 entries but " 512*8f6d9daeSMarcel Telka "IPv6 is not configured"); 5137c478bd9Sstevel@tonic-gate msgprt++; 5147c478bd9Sstevel@tonic-gate return (1); 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) { 5187c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: cannot open connection: %s", 5199e293969SRichard Lowe nconf->nc_netid, t_errlist[t_errno]); 5207c478bd9Sstevel@tonic-gate return (1); 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate 52345916cd2Sjpk if (is_system_labeled() && 52445916cd2Sjpk (strcmp(nconf->nc_protofmly, NC_INET) == 0 || 52545916cd2Sjpk strcmp(nconf->nc_protofmly, NC_INET6) == 0) && 52645916cd2Sjpk setopt_anon_mlp(fd) == -1) { 52745916cd2Sjpk syslog(LOG_ERR, "%s: couldn't set SO_ANON_MLP option", 52845916cd2Sjpk nconf->nc_netid); 52945916cd2Sjpk } 53045916cd2Sjpk 5317c478bd9Sstevel@tonic-gate /* 5327c478bd9Sstevel@tonic-gate * Negotiate for returning the ucred of the caller. This should 5337c478bd9Sstevel@tonic-gate * done before enabling the endpoint for service via 5347c478bd9Sstevel@tonic-gate * t_bind() so that requests to rpcbind contain the uid. 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate svc_fd_negotiate_ucred(fd); 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate taddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR); 5397c478bd9Sstevel@tonic-gate baddr = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR); 5407c478bd9Sstevel@tonic-gate if ((baddr == NULL) || (taddr == NULL)) { 5417c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: cannot allocate netbuf: %s", 5429e293969SRichard Lowe nconf->nc_netid, t_errlist[t_errno]); 5437c478bd9Sstevel@tonic-gate exit(1); 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate /* Get rpcbind's address on this transport */ 5477c478bd9Sstevel@tonic-gate hs.h_host = HOST_SELF; 5487c478bd9Sstevel@tonic-gate hs.h_serv = servname; 5497c478bd9Sstevel@tonic-gate if (netdir_getbyname(nconf, &hs, &nas)) 5507c478bd9Sstevel@tonic-gate goto error; 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate /* Copy the address */ 5537c478bd9Sstevel@tonic-gate taddr->addr.len = nas->n_addrs->len; 5547c478bd9Sstevel@tonic-gate memcpy(taddr->addr.buf, nas->n_addrs->buf, (int)nas->n_addrs->len); 5557c478bd9Sstevel@tonic-gate netdir_free((char *)nas, ND_ADDRLIST); 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_CLTS) 5587c478bd9Sstevel@tonic-gate taddr->qlen = 0; 5597c478bd9Sstevel@tonic-gate else 5607c478bd9Sstevel@tonic-gate taddr->qlen = listen_backlog; 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 5637c478bd9Sstevel@tonic-gate /* 5647c478bd9Sstevel@tonic-gate * Sm: If we are running then set SO_REUSEADDR option 5657c478bd9Sstevel@tonic-gate * so that we can bind to our preferred address even if 5667c478bd9Sstevel@tonic-gate * previous connections are in FIN_WAIT state 5677c478bd9Sstevel@tonic-gate */ 5687c478bd9Sstevel@tonic-gate if (setopt_reuseaddr(fd) == -1) { 5697c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Couldn't set SO_REUSEADDR option"); 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate if (t_bind(fd, taddr, baddr) != 0) { 5747c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: cannot bind: %s", 5759e293969SRichard Lowe nconf->nc_netid, t_errlist[t_errno]); 5767c478bd9Sstevel@tonic-gate goto error; 5777c478bd9Sstevel@tonic-gate } 5787c478bd9Sstevel@tonic-gate 579*8f6d9daeSMarcel Telka if (nconf->nc_semantics != NC_TPI_CLTS && taddr->qlen != baddr->qlen) 580*8f6d9daeSMarcel Telka syslog(LOG_NOTICE, "%s: unable to set listen backlog to %d " 581*8f6d9daeSMarcel Telka "(negotiated: %d)", nconf->nc_netid, taddr->qlen, 582*8f6d9daeSMarcel Telka baddr->qlen); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate if (memcmp(taddr->addr.buf, baddr->addr.buf, (int)baddr->addr.len)) { 5857c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: address in use", nconf->nc_netid); 5867c478bd9Sstevel@tonic-gate goto error; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate 589*8f6d9daeSMarcel Telka my_xprt = svc_tli_create(fd, nconf, baddr, 0, 0); 590*8f6d9daeSMarcel Telka if (my_xprt == NULL) { 5917c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: could not create service", 5929e293969SRichard Lowe nconf->nc_netid); 5937c478bd9Sstevel@tonic-gate goto error; 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* set up multicast address for RPC CALL_IT, IPv6 */ 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if ((strcmp(nconf->nc_protofmly, NC_INET6) == 0) && 5997c478bd9Sstevel@tonic-gate (strcmp(nconf->nc_proto, NC_UDP) == 0)) { 6007c478bd9Sstevel@tonic-gate if (setup_callit(fd) < 0) { 601*8f6d9daeSMarcel Telka syslog(LOG_ERR, "Unable to join IPv6 multicast group " 602*8f6d9daeSMarcel Telka "for rpc broadcast %s", RPCB_MULTICAST_ADDR); 6037c478bd9Sstevel@tonic-gate } 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 607*8f6d9daeSMarcel Telka svc_control(my_xprt, SVCSET_KEEPALIVE, (void *) TRUE); 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate #ifdef PORTMAP 6117c478bd9Sstevel@tonic-gate /* 6127c478bd9Sstevel@tonic-gate * Register both the versions for tcp/ip and udp/ip 6137c478bd9Sstevel@tonic-gate */ 6147c478bd9Sstevel@tonic-gate if ((strcmp(nconf->nc_protofmly, NC_INET) == 0) && 6159e293969SRichard Lowe ((strcmp(nconf->nc_proto, NC_TCP) == 0) || 6169e293969SRichard Lowe (strcmp(nconf->nc_proto, NC_UDP) == 0))) { 6177c478bd9Sstevel@tonic-gate PMAPLIST *pml; 6187c478bd9Sstevel@tonic-gate 6197c478bd9Sstevel@tonic-gate if (!svc_register(my_xprt, PMAPPROG, PMAPVERS, 6209e293969SRichard Lowe pmap_service, NULL)) { 6217c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "could not register on %s", 6229e293969SRichard Lowe nconf->nc_netid); 6237c478bd9Sstevel@tonic-gate goto error; 6247c478bd9Sstevel@tonic-gate } 625*8f6d9daeSMarcel Telka pml = malloc(sizeof (PMAPLIST)); 626*8f6d9daeSMarcel Telka if (pml == NULL) { 6277c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "no memory!"); 6287c478bd9Sstevel@tonic-gate exit(1); 6297c478bd9Sstevel@tonic-gate } 6307c478bd9Sstevel@tonic-gate pml->pml_map.pm_prog = PMAPPROG; 6317c478bd9Sstevel@tonic-gate pml->pml_map.pm_vers = PMAPVERS; 6327c478bd9Sstevel@tonic-gate pml->pml_map.pm_port = PMAPPORT; 6337c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_proto, NC_TCP) == 0) { 6347c478bd9Sstevel@tonic-gate if (tcptrans[0]) { 6357c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 6367c478bd9Sstevel@tonic-gate "cannot have more than one TCP transport"); 6377c478bd9Sstevel@tonic-gate goto error; 6387c478bd9Sstevel@tonic-gate } 6397c478bd9Sstevel@tonic-gate tcptrans = strdup(nconf->nc_netid); 6407c478bd9Sstevel@tonic-gate pml->pml_map.pm_prot = IPPROTO_TCP; 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate /* Let's snarf the universal address */ 6437c478bd9Sstevel@tonic-gate /* "h1.h2.h3.h4.p1.p2" */ 6447c478bd9Sstevel@tonic-gate tcp_uaddr = taddr2uaddr(nconf, &baddr->addr); 6457c478bd9Sstevel@tonic-gate } else { 6467c478bd9Sstevel@tonic-gate if (udptrans[0]) { 6477c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 6487c478bd9Sstevel@tonic-gate "cannot have more than one UDP transport"); 6497c478bd9Sstevel@tonic-gate goto error; 6507c478bd9Sstevel@tonic-gate } 6517c478bd9Sstevel@tonic-gate udptrans = strdup(nconf->nc_netid); 6527c478bd9Sstevel@tonic-gate pml->pml_map.pm_prot = IPPROTO_UDP; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate /* Let's snarf the universal address */ 6557c478bd9Sstevel@tonic-gate /* "h1.h2.h3.h4.p1.p2" */ 6567c478bd9Sstevel@tonic-gate udp_uaddr = taddr2uaddr(nconf, &baddr->addr); 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate pml->pml_next = list_pml; 6597c478bd9Sstevel@tonic-gate list_pml = pml; 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate /* Add version 3 information */ 662*8f6d9daeSMarcel Telka pml = malloc(sizeof (PMAPLIST)); 663*8f6d9daeSMarcel Telka if (pml == NULL) { 6647c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "no memory!"); 6657c478bd9Sstevel@tonic-gate exit(1); 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate pml->pml_map = list_pml->pml_map; 6687c478bd9Sstevel@tonic-gate pml->pml_map.pm_vers = RPCBVERS; 6697c478bd9Sstevel@tonic-gate pml->pml_next = list_pml; 6707c478bd9Sstevel@tonic-gate list_pml = pml; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate /* Add version 4 information */ 673*8f6d9daeSMarcel Telka pml = malloc(sizeof (PMAPLIST)); 674*8f6d9daeSMarcel Telka if (pml == NULL) { 6757c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "no memory!"); 6767c478bd9Sstevel@tonic-gate exit(1); 6777c478bd9Sstevel@tonic-gate } 6787c478bd9Sstevel@tonic-gate pml->pml_map = list_pml->pml_map; 6797c478bd9Sstevel@tonic-gate pml->pml_map.pm_vers = RPCBVERS4; 6807c478bd9Sstevel@tonic-gate pml->pml_next = list_pml; 6817c478bd9Sstevel@tonic-gate list_pml = pml; 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate /* Also add version 2 stuff to rpcbind list */ 6847c478bd9Sstevel@tonic-gate rbllist_add(PMAPPROG, PMAPVERS, nconf, &baddr->addr); 6857c478bd9Sstevel@tonic-gate } 6867c478bd9Sstevel@tonic-gate #endif 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate /* version 3 registration */ 6897c478bd9Sstevel@tonic-gate if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS, rpcb_service_3, NULL)) { 6907c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "could not register %s version 3", 6919e293969SRichard Lowe nconf->nc_netid); 6927c478bd9Sstevel@tonic-gate goto error; 6937c478bd9Sstevel@tonic-gate } 6947c478bd9Sstevel@tonic-gate rbllist_add(RPCBPROG, RPCBVERS, nconf, &baddr->addr); 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate /* version 4 registration */ 6977c478bd9Sstevel@tonic-gate if (!svc_reg(my_xprt, RPCBPROG, RPCBVERS4, rpcb_service_4, NULL)) { 6987c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "could not register %s version 4", 6999e293969SRichard Lowe nconf->nc_netid); 7007c478bd9Sstevel@tonic-gate goto error; 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate rbllist_add(RPCBPROG, RPCBVERS4, nconf, &baddr->addr); 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) { 7057c478bd9Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_CLTS) 7067c478bd9Sstevel@tonic-gate loopback_dg = strdup(nconf->nc_netid); 7077c478bd9Sstevel@tonic-gate else if (nconf->nc_semantics == NC_TPI_COTS) 7087c478bd9Sstevel@tonic-gate loopback_vc = strdup(nconf->nc_netid); 7097c478bd9Sstevel@tonic-gate else if (nconf->nc_semantics == NC_TPI_COTS_ORD) 7107c478bd9Sstevel@tonic-gate loopback_vc_ord = strdup(nconf->nc_netid); 7117c478bd9Sstevel@tonic-gate } 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate /* decide if bound checking works for this transport */ 714*8f6d9daeSMarcel Telka (void) add_bndlist(nconf, taddr, baddr); 715*8f6d9daeSMarcel Telka 7167c478bd9Sstevel@tonic-gate /* 7177c478bd9Sstevel@tonic-gate * rmtcall only supported on CLTS transports for now. 7187c478bd9Sstevel@tonic-gate */ 719*8f6d9daeSMarcel Telka if (nconf->nc_semantics == NC_TPI_CLTS) 720*8f6d9daeSMarcel Telka (void) create_rmtcall_fd(nconf); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate (void) t_free((char *)taddr, T_BIND); 7237c478bd9Sstevel@tonic-gate (void) t_free((char *)baddr, T_BIND); 7247c478bd9Sstevel@tonic-gate return (0); 7257c478bd9Sstevel@tonic-gate error: 7267c478bd9Sstevel@tonic-gate (void) t_free((char *)taddr, T_BIND); 7277c478bd9Sstevel@tonic-gate (void) t_free((char *)baddr, T_BIND); 7287c478bd9Sstevel@tonic-gate (void) t_close(fd); 7297c478bd9Sstevel@tonic-gate return (1); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate static void 7337c478bd9Sstevel@tonic-gate rbllist_add(ulong_t prog, ulong_t vers, struct netconfig *nconf, 734*8f6d9daeSMarcel Telka struct netbuf *addr) 7357c478bd9Sstevel@tonic-gate { 7367c478bd9Sstevel@tonic-gate rpcblist_ptr rbl; 7377c478bd9Sstevel@tonic-gate 738*8f6d9daeSMarcel Telka rbl = malloc(sizeof (rpcblist)); 739*8f6d9daeSMarcel Telka if (rbl == NULL) { 7407c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "no memory!"); 7417c478bd9Sstevel@tonic-gate exit(1); 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate rbl->rpcb_map.r_prog = prog; 7457c478bd9Sstevel@tonic-gate rbl->rpcb_map.r_vers = vers; 7467c478bd9Sstevel@tonic-gate rbl->rpcb_map.r_netid = strdup(nconf->nc_netid); 7477c478bd9Sstevel@tonic-gate rbl->rpcb_map.r_addr = taddr2uaddr(nconf, addr); 748*8f6d9daeSMarcel Telka if (rbl->rpcb_map.r_addr == NULL) 749*8f6d9daeSMarcel Telka rbl->rpcb_map.r_addr = strdup(""); 7507c478bd9Sstevel@tonic-gate rbl->rpcb_map.r_owner = strdup(superuser); 751*8f6d9daeSMarcel Telka 752*8f6d9daeSMarcel Telka if (rbl->rpcb_map.r_netid == NULL || rbl->rpcb_map.r_addr == NULL || 753*8f6d9daeSMarcel Telka rbl->rpcb_map.r_owner == NULL) { 754*8f6d9daeSMarcel Telka syslog(LOG_ERR, "no memory!"); 755*8f6d9daeSMarcel Telka exit(1); 756*8f6d9daeSMarcel Telka } 757*8f6d9daeSMarcel Telka 7587c478bd9Sstevel@tonic-gate rbl->rpcb_next = list_rbl; /* Attach to global list */ 7597c478bd9Sstevel@tonic-gate list_rbl = rbl; 7607c478bd9Sstevel@tonic-gate } 7617c478bd9Sstevel@tonic-gate 7627c478bd9Sstevel@tonic-gate /* 763*8f6d9daeSMarcel Telka * Catch the signal and die, if not SIGHUP 7647c478bd9Sstevel@tonic-gate */ 7657c478bd9Sstevel@tonic-gate static void 766*8f6d9daeSMarcel Telka terminate(void) 7677c478bd9Sstevel@tonic-gate { 768*8f6d9daeSMarcel Telka int sig; 769*8f6d9daeSMarcel Telka 770*8f6d9daeSMarcel Telka for (;;) { 771*8f6d9daeSMarcel Telka sig = sigwait(&sigwaitset); 772*8f6d9daeSMarcel Telka if (sig == SIGHUP) { 773*8f6d9daeSMarcel Telka rpcb_check_init(); 774*8f6d9daeSMarcel Telka continue; 775*8f6d9daeSMarcel Telka } 776*8f6d9daeSMarcel Telka if (sig != -1 || errno != EINTR) 777*8f6d9daeSMarcel Telka break; 778*8f6d9daeSMarcel Telka } 779*8f6d9daeSMarcel Telka 780*8f6d9daeSMarcel Telka syslog(LOG_ERR, "rpcbind terminating on signal %d.", sig); 781*8f6d9daeSMarcel Telka 782*8f6d9daeSMarcel Telka rw_wrlock(&list_rbl_lock); 783*8f6d9daeSMarcel Telka #ifdef PORTMAP 784*8f6d9daeSMarcel Telka rw_wrlock(&list_pml_lock); 785*8f6d9daeSMarcel Telka #endif 7867c478bd9Sstevel@tonic-gate write_warmstart(); /* Dump yourself */ 7877c478bd9Sstevel@tonic-gate 788*8f6d9daeSMarcel Telka exit(2); 789064ed339Sjjj } 790064ed339Sjjj 7917c478bd9Sstevel@tonic-gate void 7927c478bd9Sstevel@tonic-gate rpcbind_abort(void) 7937c478bd9Sstevel@tonic-gate { 794*8f6d9daeSMarcel Telka /* 795*8f6d9daeSMarcel Telka * We need to hold write locks to make sure 796*8f6d9daeSMarcel Telka * write_warmstart() is executed exactly once 797*8f6d9daeSMarcel Telka */ 798*8f6d9daeSMarcel Telka rw_wrlock(&list_rbl_lock); 799*8f6d9daeSMarcel Telka #ifdef PORTMAP 800*8f6d9daeSMarcel Telka rw_wrlock(&list_pml_lock); 801*8f6d9daeSMarcel Telka #endif 8027c478bd9Sstevel@tonic-gate write_warmstart(); /* Dump yourself */ 803*8f6d9daeSMarcel Telka 8047c478bd9Sstevel@tonic-gate abort(); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate 8077c478bd9Sstevel@tonic-gate /* 8087c478bd9Sstevel@tonic-gate * detach from tty 8097c478bd9Sstevel@tonic-gate */ 8107c478bd9Sstevel@tonic-gate static void 8117c478bd9Sstevel@tonic-gate detachfromtty(void) 8127c478bd9Sstevel@tonic-gate { 8137c478bd9Sstevel@tonic-gate close(0); 8147c478bd9Sstevel@tonic-gate close(1); 8157c478bd9Sstevel@tonic-gate close(2); 8167c478bd9Sstevel@tonic-gate switch (forkall()) { 8177c478bd9Sstevel@tonic-gate case (pid_t)-1: 8187c478bd9Sstevel@tonic-gate perror("fork"); 8197c478bd9Sstevel@tonic-gate break; 8207c478bd9Sstevel@tonic-gate case 0: 8217c478bd9Sstevel@tonic-gate break; 8227c478bd9Sstevel@tonic-gate default: 8237c478bd9Sstevel@tonic-gate exit(0); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate setsid(); 8267c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDWR, 0); 8277c478bd9Sstevel@tonic-gate dup(0); 8287c478bd9Sstevel@tonic-gate dup(0); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate 831*8f6d9daeSMarcel Telka static int 832*8f6d9daeSMarcel Telka convert_int(int *val, char *str) 833*8f6d9daeSMarcel Telka { 834*8f6d9daeSMarcel Telka long lval; 835*8f6d9daeSMarcel Telka 836*8f6d9daeSMarcel Telka if (str == NULL || !isdigit(*str)) 837*8f6d9daeSMarcel Telka return (-1); 838*8f6d9daeSMarcel Telka 839*8f6d9daeSMarcel Telka lval = strtol(str, &str, 10); 840*8f6d9daeSMarcel Telka if (*str != '\0' || lval > INT_MAX) 841*8f6d9daeSMarcel Telka return (-2); 842*8f6d9daeSMarcel Telka 843*8f6d9daeSMarcel Telka *val = (int)lval; 844*8f6d9daeSMarcel Telka return (0); 845*8f6d9daeSMarcel Telka } 846*8f6d9daeSMarcel Telka 847*8f6d9daeSMarcel Telka static int get_smf_iprop(const char *, int, int, int); 848*8f6d9daeSMarcel Telka 8497c478bd9Sstevel@tonic-gate /* get command line options */ 8507c478bd9Sstevel@tonic-gate static void 8517c478bd9Sstevel@tonic-gate parseargs(int argc, char *argv[]) 8527c478bd9Sstevel@tonic-gate { 8537c478bd9Sstevel@tonic-gate int c; 8547c478bd9Sstevel@tonic-gate int tmp; 8557c478bd9Sstevel@tonic-gate 856*8f6d9daeSMarcel Telka listen_backlog = get_smf_iprop("listen_backlog", 64, 1, INT_MAX); 857*8f6d9daeSMarcel Telka 8587c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "dwal:")) != EOF) { 8597c478bd9Sstevel@tonic-gate switch (c) { 8607c478bd9Sstevel@tonic-gate case 'd': 8617c478bd9Sstevel@tonic-gate debugging = 1; 8627c478bd9Sstevel@tonic-gate break; 8637c478bd9Sstevel@tonic-gate case 'a': 8647c478bd9Sstevel@tonic-gate doabort = 1; /* when debugging, do an abort on */ 8657c478bd9Sstevel@tonic-gate break; /* errors; for rpcbind developers */ 8667c478bd9Sstevel@tonic-gate /* only! */ 8677c478bd9Sstevel@tonic-gate case 'w': 8687c478bd9Sstevel@tonic-gate warmstart = 1; 8697c478bd9Sstevel@tonic-gate break; 8707c478bd9Sstevel@tonic-gate 8717c478bd9Sstevel@tonic-gate case 'l': 872*8f6d9daeSMarcel Telka if (convert_int(&tmp, optarg) != 0 || tmp < 1) { 873*8f6d9daeSMarcel Telka (void) fprintf(stderr, "%s: invalid " 874*8f6d9daeSMarcel Telka "listen_backlog option, using defaults\n", 875*8f6d9daeSMarcel Telka argv[0]); 876*8f6d9daeSMarcel Telka break; 8777c478bd9Sstevel@tonic-gate } 878*8f6d9daeSMarcel Telka listen_backlog = tmp; 8797c478bd9Sstevel@tonic-gate break; 8807c478bd9Sstevel@tonic-gate default: /* error */ 8817c478bd9Sstevel@tonic-gate fprintf(stderr, 882*8f6d9daeSMarcel Telka "usage: rpcbind [-d] [-w] [-l listen_backlog]\n"); 8837c478bd9Sstevel@tonic-gate exit(1); 8847c478bd9Sstevel@tonic-gate } 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate if (doabort && !debugging) { 8879e293969SRichard Lowe fprintf(stderr, 8889e293969SRichard Lowe "-a (abort) specified without -d " 8899e293969SRichard Lowe "(debugging) -- ignored.\n"); 8909e293969SRichard Lowe doabort = 0; 8917c478bd9Sstevel@tonic-gate } 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate static int 89545916cd2Sjpk setopt_int(int fd, int level, int name, int value) 8967c478bd9Sstevel@tonic-gate { 8977c478bd9Sstevel@tonic-gate struct t_optmgmt req, resp; 89845916cd2Sjpk struct { 89945916cd2Sjpk struct opthdr opt; 90045916cd2Sjpk int value; 90145916cd2Sjpk } optdata; 9027c478bd9Sstevel@tonic-gate 90345916cd2Sjpk optdata.opt.level = level; 90445916cd2Sjpk optdata.opt.name = name; 90545916cd2Sjpk optdata.opt.len = sizeof (int); 9067c478bd9Sstevel@tonic-gate 90745916cd2Sjpk optdata.value = value; 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate req.flags = T_NEGOTIATE; 91045916cd2Sjpk req.opt.len = sizeof (optdata); 91145916cd2Sjpk req.opt.buf = (char *)&optdata; 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate resp.flags = 0; 91445916cd2Sjpk resp.opt.buf = (char *)&optdata; 91545916cd2Sjpk resp.opt.maxlen = sizeof (optdata); 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 9187c478bd9Sstevel@tonic-gate t_error("t_optmgmt"); 9197c478bd9Sstevel@tonic-gate return (-1); 9207c478bd9Sstevel@tonic-gate } 9217c478bd9Sstevel@tonic-gate return (0); 9227c478bd9Sstevel@tonic-gate } 9237c478bd9Sstevel@tonic-gate 92445916cd2Sjpk static int 92545916cd2Sjpk setopt_reuseaddr(int fd) 92645916cd2Sjpk { 92745916cd2Sjpk return (setopt_int(fd, SOL_SOCKET, SO_REUSEADDR, 1)); 92845916cd2Sjpk } 92945916cd2Sjpk 93045916cd2Sjpk static int 93145916cd2Sjpk setopt_anon_mlp(int fd) 93245916cd2Sjpk { 93345916cd2Sjpk return (setopt_int(fd, SOL_SOCKET, SO_ANON_MLP, 1)); 93445916cd2Sjpk } 93545916cd2Sjpk 9367c478bd9Sstevel@tonic-gate static int 9377c478bd9Sstevel@tonic-gate setup_callit(int fd) 9387c478bd9Sstevel@tonic-gate { 9397c478bd9Sstevel@tonic-gate struct ipv6_mreq mreq; 9407c478bd9Sstevel@tonic-gate struct t_optmgmt req, resp; 9417c478bd9Sstevel@tonic-gate struct opthdr *opt; 9427c478bd9Sstevel@tonic-gate char reqbuf[ sizeof (struct ipv6_mreq) + 24]; 9437c478bd9Sstevel@tonic-gate struct ipv6_mreq *pmreq; 9447c478bd9Sstevel@tonic-gate 9457c478bd9Sstevel@tonic-gate opt = (struct opthdr *)reqbuf; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate opt->level = IPPROTO_IPV6; 9487c478bd9Sstevel@tonic-gate opt->name = IPV6_ADD_MEMBERSHIP; 9497c478bd9Sstevel@tonic-gate opt->len = sizeof (struct ipv6_mreq); 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate /* multicast address */ 9527c478bd9Sstevel@tonic-gate (void) inet_pton(AF_INET6, RPCB_MULTICAST_ADDR, 9539e293969SRichard Lowe mreq.ipv6mr_multiaddr.s6_addr); 9547c478bd9Sstevel@tonic-gate mreq.ipv6mr_interface = 0; 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate /* insert it into opt */ 9577c478bd9Sstevel@tonic-gate pmreq = (struct ipv6_mreq *)&reqbuf[sizeof (struct opthdr)]; 9587c478bd9Sstevel@tonic-gate memcpy(pmreq, &mreq, sizeof (struct ipv6_mreq)); 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate req.flags = T_NEGOTIATE; 9617c478bd9Sstevel@tonic-gate req.opt.len = sizeof (struct opthdr) + opt->len; 9627c478bd9Sstevel@tonic-gate req.opt.buf = (char *)opt; 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate resp.flags = 0; 9657c478bd9Sstevel@tonic-gate resp.opt.buf = reqbuf; 9667c478bd9Sstevel@tonic-gate resp.opt.maxlen = sizeof (reqbuf); 9677c478bd9Sstevel@tonic-gate 9687c478bd9Sstevel@tonic-gate if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 9697c478bd9Sstevel@tonic-gate t_error("t_optmgmt"); 9707c478bd9Sstevel@tonic-gate return (-1); 9717c478bd9Sstevel@tonic-gate } 9727c478bd9Sstevel@tonic-gate return (0); 9737c478bd9Sstevel@tonic-gate } 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate static boolean_t 9767c478bd9Sstevel@tonic-gate check_hostserv(struct netconfig *nconf, const char *host, const char *serv) 9777c478bd9Sstevel@tonic-gate { 9787c478bd9Sstevel@tonic-gate struct nd_hostserv nh; 9797c478bd9Sstevel@tonic-gate struct nd_addrlist *na; 9807c478bd9Sstevel@tonic-gate const char *hostname = host; 9817c478bd9Sstevel@tonic-gate const char *servname = serv; 9827c478bd9Sstevel@tonic-gate int retval; 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate if (strcmp(host, HOST_SELF) == 0) 9857c478bd9Sstevel@tonic-gate hostname = "HOST_SELF"; 9867c478bd9Sstevel@tonic-gate else if (strcmp(host, HOST_SELF_CONNECT) == 0) 9877c478bd9Sstevel@tonic-gate hostname = "HOST_SELF_CONNECT"; 9887c478bd9Sstevel@tonic-gate 9897c478bd9Sstevel@tonic-gate if (serv[0] == '\0') 9907c478bd9Sstevel@tonic-gate servname = "<any>"; 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate nh.h_host = (char *)host; 9937c478bd9Sstevel@tonic-gate nh.h_serv = (char *)serv; 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate retval = netdir_getbyname(nconf, &nh, &na); 9967c478bd9Sstevel@tonic-gate if (retval != ND_OK || na->n_cnt == 0) { 9977c478bd9Sstevel@tonic-gate if (retval == ND_OK) 9987c478bd9Sstevel@tonic-gate netdir_free(na, ND_ADDRLIST); 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate syslog(LOG_ALERT, "netid %s: cannot find an address for host " 10017c478bd9Sstevel@tonic-gate "%s, service \"%s\"", nconf->nc_netid, hostname, servname); 10027c478bd9Sstevel@tonic-gate if (debugging) { 10037c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tnetdir_getbyname for %s, " 10047c478bd9Sstevel@tonic-gate "service \"%s\" failed\n", hostname, servname); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate return (B_FALSE); 10077c478bd9Sstevel@tonic-gate } 10087c478bd9Sstevel@tonic-gate netdir_free(na, ND_ADDRLIST); 10097c478bd9Sstevel@tonic-gate 10107c478bd9Sstevel@tonic-gate if (debugging) { 10117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\tnetdir_getbyname for %s, service " 10127c478bd9Sstevel@tonic-gate "service \"%s\" succeeded\n", hostname, servname); 10137c478bd9Sstevel@tonic-gate } 10147c478bd9Sstevel@tonic-gate return (B_TRUE); 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate /* Maximum outstanding syslog requests */ 10187c478bd9Sstevel@tonic-gate #define MAXLOG 100 10197c478bd9Sstevel@tonic-gate /* Maximum length: the messages generated are fairly short; no hostnames. */ 10207c478bd9Sstevel@tonic-gate #define MAXMSG 128 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate typedef struct logmsg { 10237c478bd9Sstevel@tonic-gate struct logmsg *log_next; 10247c478bd9Sstevel@tonic-gate int log_pri; 10257c478bd9Sstevel@tonic-gate char log_msg[MAXMSG]; 10267c478bd9Sstevel@tonic-gate } logmsg; 10277c478bd9Sstevel@tonic-gate 10287c478bd9Sstevel@tonic-gate static logmsg *loghead = NULL; 10297c478bd9Sstevel@tonic-gate static logmsg **logtailp = &loghead; 10307c478bd9Sstevel@tonic-gate static mutex_t logmutex = DEFAULTMUTEX; 10317c478bd9Sstevel@tonic-gate static cond_t logcond = DEFAULTCV; 10327c478bd9Sstevel@tonic-gate static int logcount = 0; 10337c478bd9Sstevel@tonic-gate 1034*8f6d9daeSMarcel Telka /* ARGSUSED */ 10359e293969SRichard Lowe static void * __NORETURN 10367c478bd9Sstevel@tonic-gate logthread(void *arg) 10377c478bd9Sstevel@tonic-gate { 1038*8f6d9daeSMarcel Telka for (;;) { 10397c478bd9Sstevel@tonic-gate logmsg *msg; 10407c478bd9Sstevel@tonic-gate (void) mutex_lock(&logmutex); 10417c478bd9Sstevel@tonic-gate while ((msg = loghead) == NULL) 10427c478bd9Sstevel@tonic-gate (void) cond_wait(&logcond, &logmutex); 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate loghead = msg->log_next; 10457c478bd9Sstevel@tonic-gate logcount--; 10467c478bd9Sstevel@tonic-gate if (loghead == NULL) { 10477c478bd9Sstevel@tonic-gate logtailp = &loghead; 10487c478bd9Sstevel@tonic-gate logcount = 0; 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate (void) mutex_unlock(&logmutex); 10517c478bd9Sstevel@tonic-gate syslog(msg->log_pri, "%s", msg->log_msg); 10527c478bd9Sstevel@tonic-gate free(msg); 10537c478bd9Sstevel@tonic-gate } 10547c478bd9Sstevel@tonic-gate /* NOTREACHED */ 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate 10570ea5e3a5Sjjj static boolean_t 10580ea5e3a5Sjjj get_smf_prop(const char *var, boolean_t def_val) 10597c478bd9Sstevel@tonic-gate { 10607c478bd9Sstevel@tonic-gate scf_simple_prop_t *prop; 10610ea5e3a5Sjjj uint8_t *val; 10620ea5e3a5Sjjj boolean_t res = def_val; 10637c478bd9Sstevel@tonic-gate 10640ea5e3a5Sjjj prop = scf_simple_prop_get(NULL, NULL, "config", var); 1065*8f6d9daeSMarcel Telka if (prop != NULL) { 10660ea5e3a5Sjjj if ((val = scf_simple_prop_next_boolean(prop)) != NULL) 10670ea5e3a5Sjjj res = (*val == 0) ? B_FALSE : B_TRUE; 10687c478bd9Sstevel@tonic-gate scf_simple_prop_free(prop); 10697c478bd9Sstevel@tonic-gate } 10707c478bd9Sstevel@tonic-gate 10710ea5e3a5Sjjj if (prop == NULL || val == NULL) { 10720ea5e3a5Sjjj syslog(LOG_ALERT, "no value for config/%s (%s). " 10730ea5e3a5Sjjj "Using default \"%s\"", var, scf_strerror(scf_error()), 10740ea5e3a5Sjjj def_val ? "true" : "false"); 10757c478bd9Sstevel@tonic-gate } 10767c478bd9Sstevel@tonic-gate 10770ea5e3a5Sjjj return (res); 10780ea5e3a5Sjjj } 10790ea5e3a5Sjjj 1080*8f6d9daeSMarcel Telka static int 1081*8f6d9daeSMarcel Telka get_smf_iprop(const char *var, int def_val, int min, int max) 1082*8f6d9daeSMarcel Telka { 1083*8f6d9daeSMarcel Telka scf_simple_prop_t *prop; 1084*8f6d9daeSMarcel Telka int64_t *val; 1085*8f6d9daeSMarcel Telka int res = def_val; 1086*8f6d9daeSMarcel Telka 1087*8f6d9daeSMarcel Telka prop = scf_simple_prop_get(NULL, NULL, "config", var); 1088*8f6d9daeSMarcel Telka if (prop != NULL) { 1089*8f6d9daeSMarcel Telka if ((val = scf_simple_prop_next_integer(prop)) != NULL) { 1090*8f6d9daeSMarcel Telka if (*val < min || *val > max) 1091*8f6d9daeSMarcel Telka syslog(LOG_ALERT, "value for config/%s out of " 1092*8f6d9daeSMarcel Telka "range. Using default %d", var, def_val); 1093*8f6d9daeSMarcel Telka else 1094*8f6d9daeSMarcel Telka res = (int)*val; 1095*8f6d9daeSMarcel Telka } 1096*8f6d9daeSMarcel Telka scf_simple_prop_free(prop); 1097*8f6d9daeSMarcel Telka } 1098*8f6d9daeSMarcel Telka 1099*8f6d9daeSMarcel Telka if (prop == NULL || val == NULL) { 1100*8f6d9daeSMarcel Telka syslog(LOG_ALERT, "no value for config/%s (%s). " 1101*8f6d9daeSMarcel Telka "Using default %d", var, scf_strerror(scf_error()), 1102*8f6d9daeSMarcel Telka def_val); 1103*8f6d9daeSMarcel Telka } 1104*8f6d9daeSMarcel Telka 1105*8f6d9daeSMarcel Telka return (res); 1106*8f6d9daeSMarcel Telka } 1107*8f6d9daeSMarcel Telka 11080ea5e3a5Sjjj /* 1109064ed339Sjjj * Initialize: read the configuration parameters from SMF. 1110064ed339Sjjj * This function must be idempotent because it can be called from the 1111*8f6d9daeSMarcel Telka * signal handler. 11120ea5e3a5Sjjj */ 1113*8f6d9daeSMarcel Telka static void 11140ea5e3a5Sjjj rpcb_check_init(void) 11150ea5e3a5Sjjj { 11160ea5e3a5Sjjj thread_t tid; 1117*8f6d9daeSMarcel Telka int max_threads; 1118064ed339Sjjj static int thr_running; 11190ea5e3a5Sjjj 11200ea5e3a5Sjjj wrap_enabled = get_smf_prop("enable_tcpwrappers", B_FALSE); 11210ea5e3a5Sjjj verboselog = get_smf_prop("verbose_logging", B_FALSE); 11220ea5e3a5Sjjj allow_indirect = get_smf_prop("allow_indirect", B_TRUE); 1123*8f6d9daeSMarcel Telka local_only = get_smf_prop("local_only", B_TRUE); 11247c478bd9Sstevel@tonic-gate 1125064ed339Sjjj if (wrap_enabled && !thr_running) { 11267c478bd9Sstevel@tonic-gate (void) thr_create(NULL, 0, logthread, NULL, THR_DETACHED, &tid); 1127064ed339Sjjj thr_running = 1; 1128064ed339Sjjj } 1129*8f6d9daeSMarcel Telka 1130*8f6d9daeSMarcel Telka /* 1131*8f6d9daeSMarcel Telka * Set the maximum number of threads. 1132*8f6d9daeSMarcel Telka */ 1133*8f6d9daeSMarcel Telka max_threads = get_smf_iprop("max_threads", 72, 1, INT_MAX); 1134*8f6d9daeSMarcel Telka if (!rpc_control(RPC_SVC_THRMAX_SET, &max_threads)) { 1135*8f6d9daeSMarcel Telka int tmp; 1136*8f6d9daeSMarcel Telka 1137*8f6d9daeSMarcel Telka /* 1138*8f6d9daeSMarcel Telka * The following rpc_control() call cannot fail 1139*8f6d9daeSMarcel Telka */ 1140*8f6d9daeSMarcel Telka if (!rpc_control(RPC_SVC_THRMAX_GET, &tmp)) 1141*8f6d9daeSMarcel Telka assert(0); 1142*8f6d9daeSMarcel Telka 1143*8f6d9daeSMarcel Telka if (tmp != max_threads) { 1144*8f6d9daeSMarcel Telka syslog(LOG_ERR, "setting max_threads to %d failed, " 1145*8f6d9daeSMarcel Telka "using %d worker threads", max_threads, tmp); 1146*8f6d9daeSMarcel Telka max_threads = tmp; 1147*8f6d9daeSMarcel Telka } 1148*8f6d9daeSMarcel Telka } 1149*8f6d9daeSMarcel Telka 1150*8f6d9daeSMarcel Telka /* 1151*8f6d9daeSMarcel Telka * Set rpcb_rmtcalls_max. 1152*8f6d9daeSMarcel Telka */ 1153*8f6d9daeSMarcel Telka if (max_threads < reserved_threads) 1154*8f6d9daeSMarcel Telka set_rpcb_rmtcalls_max(0); 1155*8f6d9daeSMarcel Telka else 1156*8f6d9daeSMarcel Telka set_rpcb_rmtcalls_max(max_threads - reserved_threads); 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate 11597c478bd9Sstevel@tonic-gate /* 11607c478bd9Sstevel@tonic-gate * qsyslog() - queue a request for syslog(); if syslog blocks, the other 11617c478bd9Sstevel@tonic-gate * thread blocks; we make sure we don't run out of memory by allowing 11627c478bd9Sstevel@tonic-gate * only a limited number of outstandig syslog() requests. 11637c478bd9Sstevel@tonic-gate */ 11647c478bd9Sstevel@tonic-gate void 11657c478bd9Sstevel@tonic-gate qsyslog(int pri, const char *fmt, ...) 11667c478bd9Sstevel@tonic-gate { 11677c478bd9Sstevel@tonic-gate logmsg *msg = malloc(sizeof (*msg)); 11687c478bd9Sstevel@tonic-gate va_list ap; 11697c478bd9Sstevel@tonic-gate 11707c478bd9Sstevel@tonic-gate if (msg == NULL) 11717c478bd9Sstevel@tonic-gate return; 11727c478bd9Sstevel@tonic-gate 11737c478bd9Sstevel@tonic-gate msg->log_pri = pri; 11747c478bd9Sstevel@tonic-gate 11757c478bd9Sstevel@tonic-gate va_start(ap, fmt); 11767c478bd9Sstevel@tonic-gate (void) vsnprintf(msg->log_msg, sizeof (msg->log_msg), fmt, ap); 11777c478bd9Sstevel@tonic-gate va_end(ap); 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate msg->log_next = NULL; 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate (void) mutex_lock(&logmutex); 11827c478bd9Sstevel@tonic-gate if (logcount < MAXLOG) { 1183*8f6d9daeSMarcel Telka if (logcount == 0) 1184*8f6d9daeSMarcel Telka (void) cond_signal(&logcond); 11857c478bd9Sstevel@tonic-gate logcount++; 11867c478bd9Sstevel@tonic-gate *logtailp = msg; 11877c478bd9Sstevel@tonic-gate logtailp = &msg->log_next; 1188*8f6d9daeSMarcel Telka (void) mutex_unlock(&logmutex); 11897c478bd9Sstevel@tonic-gate } else { 1190*8f6d9daeSMarcel Telka (void) mutex_unlock(&logmutex); 11917c478bd9Sstevel@tonic-gate free(msg); 11927c478bd9Sstevel@tonic-gate } 11937c478bd9Sstevel@tonic-gate } 1194