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