1dfdcada3SDoug Rabson /* $NetBSD: rpcb_clnt.c,v 1.6 2000/07/16 06:41:43 itojun Exp $ */
2dfdcada3SDoug Rabson
32e322d37SHiroki Sato /*-
451369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
551369649SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2010, Oracle America, Inc.
72e322d37SHiroki Sato * All rights reserved.
8dfdcada3SDoug Rabson *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of the "Oracle America, Inc." nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
19dfdcada3SDoug Rabson *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
31dfdcada3SDoug Rabson */
32dfdcada3SDoug Rabson /*
33dfdcada3SDoug Rabson * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34dfdcada3SDoug Rabson */
35dfdcada3SDoug Rabson
36dfdcada3SDoug Rabson #include <sys/cdefs.h>
37dfdcada3SDoug Rabson /*
38dfdcada3SDoug Rabson * rpcb_clnt.c
39dfdcada3SDoug Rabson * interface to rpcbind rpc service.
40dfdcada3SDoug Rabson *
41dfdcada3SDoug Rabson * Copyright (C) 1988, Sun Microsystems, Inc.
42dfdcada3SDoug Rabson */
43dfdcada3SDoug Rabson
44dfdcada3SDoug Rabson #include "opt_inet6.h"
45dfdcada3SDoug Rabson
46dfdcada3SDoug Rabson #include <sys/param.h>
47dfdcada3SDoug Rabson #include <sys/systm.h>
48dfdcada3SDoug Rabson #include <sys/kernel.h>
49dfdcada3SDoug Rabson #include <sys/malloc.h>
50dfdcada3SDoug Rabson #include <sys/proc.h>
51dfdcada3SDoug Rabson #include <sys/socket.h>
52dfdcada3SDoug Rabson #include <sys/socketvar.h>
53dfdcada3SDoug Rabson
54dfdcada3SDoug Rabson #include <rpc/rpc.h>
55dfdcada3SDoug Rabson #include <rpc/rpcb_clnt.h>
56dfdcada3SDoug Rabson #include <rpc/rpcb_prot.h>
57dfdcada3SDoug Rabson
58ee31b83aSDoug Rabson #include <rpc/rpc_com.h>
59dfdcada3SDoug Rabson
60dfdcada3SDoug Rabson static struct timeval tottimeout = { 60, 0 };
61dfdcada3SDoug Rabson static const char nullstring[] = "\000";
62*54a567adSGleb Smirnoff static CLIENT *rpcb_clnt;
63dfdcada3SDoug Rabson
64*54a567adSGleb Smirnoff static void
local_rpcb(void * v __unused)65*54a567adSGleb Smirnoff local_rpcb(void *v __unused)
66dfdcada3SDoug Rabson {
67*54a567adSGleb Smirnoff rpcb_clnt = client_nl_create("rpcbind", RPCBPROG, RPCBVERS);
68*54a567adSGleb Smirnoff KASSERT(rpcb_clnt, ("%s: netlink client already exist", __func__));
69*54a567adSGleb Smirnoff clnt_control(rpcb_clnt, CLSET_RETRIES, &(int){6});
70*54a567adSGleb Smirnoff clnt_control(rpcb_clnt, CLSET_WAITCHAN, "rpcb");
71dfdcada3SDoug Rabson }
72*54a567adSGleb Smirnoff SYSINIT(rpcb_clnt, SI_SUB_VFS, SI_ORDER_SECOND, local_rpcb, NULL);
73dfdcada3SDoug Rabson
74dfdcada3SDoug Rabson /*
75dfdcada3SDoug Rabson * Set a mapping between program, version and address.
76dfdcada3SDoug Rabson * Calls the rpcbind service to do the mapping.
77dfdcada3SDoug Rabson */
78dfdcada3SDoug Rabson bool_t
rpcb_set(rpcprog_t program,rpcvers_t version,const struct netconfig * nconf,const struct netbuf * address)79dfdcada3SDoug Rabson rpcb_set(rpcprog_t program, rpcvers_t version,
80dfdcada3SDoug Rabson const struct netconfig *nconf, /* Network structure of transport */
81dfdcada3SDoug Rabson const struct netbuf *address) /* Services netconfig address */
82dfdcada3SDoug Rabson {
83dfdcada3SDoug Rabson bool_t rslt = FALSE;
84dfdcada3SDoug Rabson RPCB parms;
85dfdcada3SDoug Rabson #if 0
86dfdcada3SDoug Rabson char uidbuf[32];
87dfdcada3SDoug Rabson #endif
88dfdcada3SDoug Rabson struct netconfig nconfcopy;
89dfdcada3SDoug Rabson struct netbuf addresscopy;
90dfdcada3SDoug Rabson
91dfdcada3SDoug Rabson /* parameter checking */
92dfdcada3SDoug Rabson if (nconf == NULL) {
93dfdcada3SDoug Rabson rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
94dfdcada3SDoug Rabson return (FALSE);
95dfdcada3SDoug Rabson }
96dfdcada3SDoug Rabson if (address == NULL) {
97dfdcada3SDoug Rabson rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
98dfdcada3SDoug Rabson return (FALSE);
99dfdcada3SDoug Rabson }
100dfdcada3SDoug Rabson
101dfdcada3SDoug Rabson /* convert to universal */
102dfdcada3SDoug Rabson /*LINTED const castaway*/
103dfdcada3SDoug Rabson nconfcopy = *nconf;
104dfdcada3SDoug Rabson addresscopy = *address;
105dfdcada3SDoug Rabson parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);
106dfdcada3SDoug Rabson if (!parms.r_addr) {
107dfdcada3SDoug Rabson rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
108dfdcada3SDoug Rabson return (FALSE); /* no universal address */
109dfdcada3SDoug Rabson }
110dfdcada3SDoug Rabson parms.r_prog = program;
111dfdcada3SDoug Rabson parms.r_vers = version;
112dfdcada3SDoug Rabson parms.r_netid = nconf->nc_netid;
113dfdcada3SDoug Rabson #if 0
114dfdcada3SDoug Rabson /*
115dfdcada3SDoug Rabson * Though uid is not being used directly, we still send it for
116dfdcada3SDoug Rabson * completeness. For non-unix platforms, perhaps some other
117dfdcada3SDoug Rabson * string or an empty string can be sent.
118dfdcada3SDoug Rabson */
119dfdcada3SDoug Rabson (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
120dfdcada3SDoug Rabson parms.r_owner = uidbuf;
121dfdcada3SDoug Rabson #else
122dfdcada3SDoug Rabson parms.r_owner = "";
123dfdcada3SDoug Rabson #endif
124dfdcada3SDoug Rabson
125*54a567adSGleb Smirnoff CLNT_CALL(rpcb_clnt, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
126dfdcada3SDoug Rabson (char *)(void *)&parms, (xdrproc_t) xdr_bool,
127dfdcada3SDoug Rabson (char *)(void *)&rslt, tottimeout);
128dfdcada3SDoug Rabson
129dfdcada3SDoug Rabson free(parms.r_addr, M_RPC);
130dfdcada3SDoug Rabson return (rslt);
131dfdcada3SDoug Rabson }
132dfdcada3SDoug Rabson
133dfdcada3SDoug Rabson /*
134dfdcada3SDoug Rabson * Remove the mapping between program, version and netbuf address.
135dfdcada3SDoug Rabson * Calls the rpcbind service to do the un-mapping.
136dfdcada3SDoug Rabson * If netbuf is NULL, unset for all the transports, otherwise unset
137dfdcada3SDoug Rabson * only for the given transport.
138dfdcada3SDoug Rabson */
139dfdcada3SDoug Rabson bool_t
rpcb_unset(rpcprog_t program,rpcvers_t version,const struct netconfig * nconf)140dfdcada3SDoug Rabson rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
141dfdcada3SDoug Rabson {
142dfdcada3SDoug Rabson bool_t rslt = FALSE;
143dfdcada3SDoug Rabson RPCB parms;
144dfdcada3SDoug Rabson #if 0
145dfdcada3SDoug Rabson char uidbuf[32];
146dfdcada3SDoug Rabson #endif
147dfdcada3SDoug Rabson
148dfdcada3SDoug Rabson parms.r_prog = program;
149dfdcada3SDoug Rabson parms.r_vers = version;
150dfdcada3SDoug Rabson if (nconf)
151dfdcada3SDoug Rabson parms.r_netid = nconf->nc_netid;
152dfdcada3SDoug Rabson else {
153dfdcada3SDoug Rabson /*LINTED const castaway*/
154dfdcada3SDoug Rabson parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets all */
155dfdcada3SDoug Rabson }
156dfdcada3SDoug Rabson /*LINTED const castaway*/
157dfdcada3SDoug Rabson parms.r_addr = (char *)(uintptr_t) &nullstring[0];
158dfdcada3SDoug Rabson #if 0
159dfdcada3SDoug Rabson (void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
160dfdcada3SDoug Rabson parms.r_owner = uidbuf;
161dfdcada3SDoug Rabson #else
162dfdcada3SDoug Rabson parms.r_owner = "";
163dfdcada3SDoug Rabson #endif
164dfdcada3SDoug Rabson
165*54a567adSGleb Smirnoff CLNT_CALL(rpcb_clnt, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
166dfdcada3SDoug Rabson (char *)(void *)&parms, (xdrproc_t) xdr_bool,
167dfdcada3SDoug Rabson (char *)(void *)&rslt, tottimeout);
168dfdcada3SDoug Rabson
169dfdcada3SDoug Rabson return (rslt);
170dfdcada3SDoug Rabson }
171