xref: /freebsd/sys/rpc/rpcb_clnt.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
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 
63 static CLIENT *local_rpcb(void);
64 
65 /* XXX */
66 #define IN4_LOCALHOST_STRING	"127.0.0.1"
67 #define IN6_LOCALHOST_STRING	"::1"
68 
69 /*
70  * This routine will return a client handle that is connected to the local
71  * rpcbind. Returns NULL on error and free's everything.
72  */
73 static CLIENT *
74 local_rpcb(void)
75 {
76 	CLIENT *client;
77 	struct socket *so;
78 	size_t tsize;
79 	struct sockaddr_un sun;
80 	int error;
81 
82 	/*
83 	 * Try connecting to the local rpcbind through a local socket
84 	 * first. If this doesn't work, try all transports defined in
85 	 * the netconfig file.
86 	 */
87 	memset(&sun, 0, sizeof sun);
88 	so = NULL;
89 	error = socreate(AF_LOCAL, &so, SOCK_STREAM, 0, curthread->td_ucred,
90 	    curthread);
91 	if (error)
92 		return (NULL);
93 	sun.sun_family = AF_LOCAL;
94 	strcpy(sun.sun_path, _PATH_RPCBINDSOCK);
95 	sun.sun_len = SUN_LEN(&sun);
96 
97 	tsize = __rpc_get_t_size(AF_LOCAL, 0, 0);
98 	client = clnt_vc_create(so, (struct sockaddr *)&sun, (rpcprog_t)RPCBPROG,
99 	    (rpcvers_t)RPCBVERS, tsize, tsize, 1);
100 
101 	if (client != NULL) {
102 		/* Mark the socket to be closed in destructor */
103 		(void) CLNT_CONTROL(client, CLSET_FD_CLOSE, NULL);
104 		return client;
105 	}
106 
107 	/* Nobody needs this socket anymore; free the descriptor. */
108 	soclose(so);
109 
110 	return (NULL);
111 }
112 
113 /*
114  * Set a mapping between program, version and address.
115  * Calls the rpcbind service to do the mapping.
116  */
117 bool_t
118 rpcb_set(rpcprog_t program, rpcvers_t version,
119     const struct netconfig *nconf,	/* Network structure of transport */
120     const struct netbuf *address)	/* Services netconfig address */
121 {
122 	CLIENT *client;
123 	bool_t rslt = FALSE;
124 	RPCB parms;
125 #if 0
126 	char uidbuf[32];
127 #endif
128 	struct netconfig nconfcopy;
129 	struct netbuf addresscopy;
130 
131 	/* parameter checking */
132 	if (nconf == NULL) {
133 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
134 		return (FALSE);
135 	}
136 	if (address == NULL) {
137 		rpc_createerr.cf_stat = RPC_UNKNOWNADDR;
138 		return (FALSE);
139 	}
140 	client = local_rpcb();
141 	if (! client) {
142 		return (FALSE);
143 	}
144 
145 	/* convert to universal */
146 	/*LINTED const castaway*/
147 	nconfcopy = *nconf;
148 	addresscopy = *address;
149 	parms.r_addr = taddr2uaddr(&nconfcopy, &addresscopy);
150 	if (!parms.r_addr) {
151 		CLNT_DESTROY(client);
152 		rpc_createerr.cf_stat = RPC_N2AXLATEFAILURE;
153 		return (FALSE); /* no universal address */
154 	}
155 	parms.r_prog = program;
156 	parms.r_vers = version;
157 	parms.r_netid = nconf->nc_netid;
158 #if 0
159 	/*
160 	 * Though uid is not being used directly, we still send it for
161 	 * completeness.  For non-unix platforms, perhaps some other
162 	 * string or an empty string can be sent.
163 	 */
164 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
165 	parms.r_owner = uidbuf;
166 #else
167 	parms.r_owner = "";
168 #endif
169 
170 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_SET, (xdrproc_t) xdr_rpcb,
171 	    (char *)(void *)&parms, (xdrproc_t) xdr_bool,
172 	    (char *)(void *)&rslt, tottimeout);
173 
174 	CLNT_DESTROY(client);
175 	free(parms.r_addr, M_RPC);
176 	return (rslt);
177 }
178 
179 /*
180  * Remove the mapping between program, version and netbuf address.
181  * Calls the rpcbind service to do the un-mapping.
182  * If netbuf is NULL, unset for all the transports, otherwise unset
183  * only for the given transport.
184  */
185 bool_t
186 rpcb_unset(rpcprog_t program, rpcvers_t version, const struct netconfig *nconf)
187 {
188 	CLIENT *client;
189 	bool_t rslt = FALSE;
190 	RPCB parms;
191 #if 0
192 	char uidbuf[32];
193 #endif
194 
195 	client = local_rpcb();
196 	if (! client) {
197 		return (FALSE);
198 	}
199 
200 	parms.r_prog = program;
201 	parms.r_vers = version;
202 	if (nconf)
203 		parms.r_netid = nconf->nc_netid;
204 	else {
205 		/*LINTED const castaway*/
206 		parms.r_netid = (char *)(uintptr_t) &nullstring[0]; /* unsets  all */
207 	}
208 	/*LINTED const castaway*/
209 	parms.r_addr = (char *)(uintptr_t) &nullstring[0];
210 #if 0
211 	(void) snprintf(uidbuf, sizeof uidbuf, "%d", geteuid());
212 	parms.r_owner = uidbuf;
213 #else
214 	parms.r_owner = "";
215 #endif
216 
217 	CLNT_CALL(client, (rpcproc_t)RPCBPROC_UNSET, (xdrproc_t) xdr_rpcb,
218 	    (char *)(void *)&parms, (xdrproc_t) xdr_bool,
219 	    (char *)(void *)&rslt, tottimeout);
220 
221 	CLNT_DESTROY(client);
222 	return (rslt);
223 }
224