/* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 2010, Oracle America, Inc.6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions are met:10* - Redistributions of source code must retain the above copyright notice,11* this list of conditions and the following disclaimer.12* - Redistributions in binary form must reproduce the above copyright notice,13* this list of conditions and the following disclaimer in the documentation14* and/or other materials provided with the distribution.15* - Neither the name of the "Oracle America, Inc." nor the names of its16* contributors may be used to endorse or promote products derived17* from this software without specific prior written permission.18*19* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"20* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE23* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR24* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF25* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS26* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN27* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE29* POSSIBILITY OF SUCH DAMAGE.30*/31/*32* Copyright (c) 1986-1991 by Sun Microsystems Inc.33*/3435#include <sys/cdefs.h>36/*37* rpcb_clnt.c38* interface to rpcbind rpc service.39*40* Copyright (C) 1988, Sun Microsystems, Inc.41*/4243#include "opt_inet6.h"4445#include <sys/param.h>46#include <sys/systm.h>47#include <sys/kernel.h>48#include <sys/malloc.h>49#include <sys/proc.h>50#include <sys/socket.h>51#include <sys/socketvar.h>5253#include <rpc/rpc.h>54#include <rpc/rpcb_clnt.h>55#include <rpc/rpcb_prot.h>5657#include <rpc/rpc_com.h>5859static struct timeval tottimeout = { 60, 0 };60static const char nullstring[] = "\000";61static CLIENT *rpcb_clnt;6263static void64local_rpcb(void *v __unused)65{66rpcb_clnt = client_nl_create("rpcbind", RPCBPROG, RPCBVERS);67KASSERT(rpcb_clnt, ("%s: netlink client already exist", __func__));68clnt_control(rpcb_clnt, CLSET_RETRIES, &(int){6});69clnt_control(rpcb_clnt, CLSET_WAITCHAN, "rpcb");70}71SYSINIT(rpcb_clnt, SI_SUB_VFS, SI_ORDER_SECOND, local_rpcb, NULL);7273/*74* Set a mapping between program, version and address.75* Calls the rpcbind service to do the mapping.76*/77bool_t78rpcb_set(rpcprog_t program, rpcvers_t version,79const struct netconfig *nconf, /* Network structure of transport */80const struct netbuf *address) /* Services netconfig address */81{82bool_t rslt = FALSE;83RPCB parms;84#if 085char uidbuf[32];86#endif87struct netconfig nconfcopy;88struct netbuf addresscopy;8990/* parameter checking */91if (nconf == NULL) {92rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;93return (FALSE);94}95if (address == NULL) {96rpc_createerr.cf_stat = RPC_UNKNOWNADDR;97return (FALSE);98}99100/* convert to universal */101/*LINTED const castaway*/102nconfcopy = *nconf;103addresscopy = *address;104parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);105if (!parms.r_addr) {106rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;107return (FALSE); /* no universal address */108}109parms.r_prog = program;110parms.r_vers = version;111parms.r_netid = nconf->nc_netid;112#if 0113/*114* Though uid is not being used directly, we still send it for115* completeness. For non-unix platforms, perhaps some other116* string or an empty string can be sent.117*/118(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());119parms.r_owner = uidbuf;120#else121parms.r_owner = "";122#endif123124CLNT_CALL(rpcb_clnt, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,125(char *)(void *)&parms, (xdrproc_t) xdr_bool,126(char *)(void *)&rslt, tottimeout);127128free(parms.r_addr, M_RPC);129return (rslt);130}131132/*133* Remove the mapping between program, version and netbuf address.134* Calls the rpcbind service to do the un-mapping.135* If netbuf is NULL, unset for all the transports, otherwise unset136* only for the given transport.137*/138bool_t139rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)140{141bool_t rslt = FALSE;142RPCB parms;143#if 0144char uidbuf[32];145#endif146147parms.r_prog = program;148parms.r_vers = version;149if (nconf)150parms.r_netid = nconf->nc_netid;151else {152/*LINTED const castaway*/153parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets all */154}155/*LINTED const castaway*/156parms.r_addr = (char *)(uintptr_t) &nullstring[0];157#if 0158(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());159parms.r_owner = uidbuf;160#else161parms.r_owner = "";162#endif163164CLNT_CALL(rpcb_clnt, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,165(char *)(void *)&parms, (xdrproc_t) xdr_bool,166(char *)(void *)&rslt, tottimeout);167168return (rslt);169}170171172