xref: /freebsd/lib/libc/rpc/clnt_generic.c (revision 55141f2c8991b2a6adbf30bb0fe3e6cbc303f06d)
1 /*	$NetBSD: clnt_generic.c,v 1.18 2000/07/06 03:10:34 christos 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 /* #ident	"@(#)clnt_generic.c	1.40	99/04/21 SMI" */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char *sccsid2 = "from: @(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";
37 static char *sccsid = "from: @(#)clnt_generic.c	2.2 88/08/01 4.0 RPCSRC";
38 #endif
39 /*
40  * Copyright (c) 1986-1996,1998 by Sun Microsystems, Inc.
41  * All rights reserved.
42  */
43 #include "namespace.h"
44 #include "reentrant.h"
45 #include <sys/types.h>
46 #include <sys/fcntl.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <netinet/tcp.h>
50 #include <stdio.h>
51 #include <errno.h>
52 #include <netdb.h>
53 #include <syslog.h>
54 #include <rpc/rpc.h>
55 #include <rpc/nettype.h>
56 #include <string.h>
57 #include <stdlib.h>
58 #include <unistd.h>
59 #include "un-namespace.h"
60 #include "rpc_com.h"
61 
62 extern bool_t __rpc_is_local_host(const char *);
63 int __rpc_raise_fd(int);
64 
65 #ifndef NETIDLEN
66 #define	NETIDLEN 32
67 #endif
68 
69 
70 /*
71  * Generic client creation with version checking the value of
72  * vers_out is set to the highest server supported value
73  * vers_low <= vers_out <= vers_high  AND an error results
74  * if this can not be done.
75  *
76  * It calls clnt_create_vers_timed() with a NULL value for the timeout
77  * pointer, which indicates that the default timeout should be used.
78  */
79 CLIENT *
80 clnt_create_vers(const char *hostname, rpcprog_t prog, rpcvers_t *vers_out,
81 	rpcvers_t vers_low, rpcvers_t vers_high, const char *nettype)
82 {
83 
84 	return (clnt_create_vers_timed(hostname, prog, vers_out, vers_low,
85 				vers_high, nettype, NULL));
86 }
87 
88 /*
89  * This the routine has the same definition as clnt_create_vers(),
90  * except it takes an additional timeout parameter - a pointer to
91  * a timeval structure.  A NULL value for the pointer indicates
92  * that the default timeout value should be used.
93  */
94 CLIENT *
95 clnt_create_vers_timed(const char *hostname, rpcprog_t prog,
96     rpcvers_t *vers_out, rpcvers_t vers_low, rpcvers_t vers_high,
97     const char *nettype, const struct timeval *tp)
98 {
99 	CLIENT *clnt;
100 	struct timeval to;
101 	enum clnt_stat rpc_stat;
102 	struct rpc_err rpcerr;
103 
104 	clnt = clnt_create_timed(hostname, prog, vers_high, nettype, tp);
105 	if (clnt == NULL) {
106 		return (NULL);
107 	}
108 	to.tv_sec = 10;
109 	to.tv_usec = 0;
110 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
111 			(char *)NULL, (xdrproc_t)xdr_void, (char *)NULL, to);
112 	if (rpc_stat == RPC_SUCCESS) {
113 		*vers_out = vers_high;
114 		return (clnt);
115 	}
116 	while (rpc_stat == RPC_PROGVERSMISMATCH && vers_high > vers_low) {
117 		unsigned int minvers, maxvers;
118 
119 		clnt_geterr(clnt, &rpcerr);
120 		minvers = rpcerr.re_vers.low;
121 		maxvers = rpcerr.re_vers.high;
122 		if (maxvers < vers_high)
123 			vers_high = maxvers;
124 		else
125 			vers_high--;
126 		if (minvers > vers_low)
127 			vers_low = minvers;
128 		if (vers_low > vers_high) {
129 			goto error;
130 		}
131 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)&vers_high);
132 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t)xdr_void,
133 				(char *)NULL, (xdrproc_t)xdr_void,
134 				(char *)NULL, to);
135 		if (rpc_stat == RPC_SUCCESS) {
136 			*vers_out = vers_high;
137 			return (clnt);
138 		}
139 	}
140 	clnt_geterr(clnt, &rpcerr);
141 
142 error:
143 	rpc_createerr.cf_stat = rpc_stat;
144 	rpc_createerr.cf_error = rpcerr;
145 	clnt_destroy(clnt);
146 	return (NULL);
147 }
148 
149 /*
150  * Top level client creation routine.
151  * Generic client creation: takes (servers name, program-number, nettype) and
152  * returns client handle. Default options are set, which the user can
153  * change using the rpc equivalent of _ioctl()'s.
154  *
155  * It tries for all the netids in that particular class of netid until
156  * it succeeds.
157  * XXX The error message in the case of failure will be the one
158  * pertaining to the last create error.
159  *
160  * It calls clnt_create_timed() with the default timeout.
161  */
162 CLIENT *
163 clnt_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
164     const char *nettype)
165 {
166 
167 	return (clnt_create_timed(hostname, prog, vers, nettype, NULL));
168 }
169 
170 /*
171  * This the routine has the same definition as clnt_create(),
172  * except it takes an additional timeout parameter - a pointer to
173  * a timeval structure.  A NULL value for the pointer indicates
174  * that the default timeout value should be used.
175  *
176  * This function calls clnt_tp_create_timed().
177  */
178 CLIENT *
179 clnt_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
180     const char *netclass, const struct timeval *tp)
181 {
182 	struct netconfig *nconf;
183 	CLIENT *clnt = NULL;
184 	void *handle;
185 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
186 	struct rpc_err	save_cf_error;
187 	char nettype_array[NETIDLEN];
188 	char *nettype = &nettype_array[0];
189 
190 	if (netclass == NULL)
191 		nettype = NULL;
192 	else {
193 		size_t len = strlen(netclass);
194 		if (len >= sizeof (nettype_array)) {
195 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
196 			return (NULL);
197 		}
198 		strcpy(nettype, netclass);
199 	}
200 
201 	if ((handle = __rpc_setconf((char *)nettype)) == NULL) {
202 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
203 		return (NULL);
204 	}
205 	rpc_createerr.cf_stat = RPC_SUCCESS;
206 	while (clnt == NULL) {
207 		if ((nconf = __rpc_getconf(handle)) == NULL) {
208 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
209 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
210 			break;
211 		}
212 #ifdef CLNT_DEBUG
213 		printf("trying netid %s\n", nconf->nc_netid);
214 #endif
215 		clnt = clnt_tp_create_timed(hostname, prog, vers, nconf, tp);
216 		if (clnt)
217 			break;
218 		else
219 			/*
220 			 *	Since we didn't get a name-to-address
221 			 *	translation failure here, we remember
222 			 *	this particular error.  The object of
223 			 *	this is to enable us to return to the
224 			 *	caller a more-specific error than the
225 			 *	unhelpful ``Name to address translation
226 			 *	failed'' which might well occur if we
227 			 *	merely returned the last error (because
228 			 *	the local loopbacks are typically the
229 			 *	last ones in /etc/netconfig and the most
230 			 *	likely to be unable to translate a host
231 			 *	name).  We also check for a more
232 			 *	meaningful error than ``unknown host
233 			 *	name'' for the same reasons.
234 			 */
235 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE &&
236 			    rpc_createerr.cf_stat != RPC_UNKNOWNHOST) {
237 				save_cf_stat = rpc_createerr.cf_stat;
238 				save_cf_error = rpc_createerr.cf_error;
239 			}
240 	}
241 
242 	/*
243 	 *	Attempt to return an error more specific than ``Name to address
244 	 *	translation failed'' or ``unknown host name''
245 	 */
246 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE ||
247 				rpc_createerr.cf_stat == RPC_UNKNOWNHOST) &&
248 					(save_cf_stat != RPC_SUCCESS)) {
249 		rpc_createerr.cf_stat = save_cf_stat;
250 		rpc_createerr.cf_error = save_cf_error;
251 	}
252 	__rpc_endconf(handle);
253 	return (clnt);
254 }
255 
256 /*
257  * Generic client creation: takes (servers name, program-number, netconf) and
258  * returns client handle. Default options are set, which the user can
259  * change using the rpc equivalent of _ioctl()'s : clnt_control()
260  * It finds out the server address from rpcbind and calls clnt_tli_create().
261  *
262  * It calls clnt_tp_create_timed() with the default timeout.
263  */
264 CLIENT *
265 clnt_tp_create(const char *hostname, rpcprog_t prog, rpcvers_t vers,
266     const struct netconfig *nconf)
267 {
268 
269 	return (clnt_tp_create_timed(hostname, prog, vers, nconf, NULL));
270 }
271 
272 /*
273  * This has the same definition as clnt_tp_create(), except it
274  * takes an additional parameter - a pointer to a timeval structure.
275  * A NULL value for the timeout pointer indicates that the default
276  * value for the timeout should be used.
277  */
278 CLIENT *
279 clnt_tp_create_timed(const char *hostname, rpcprog_t prog, rpcvers_t vers,
280     const struct netconfig *nconf, const struct timeval *tp)
281 {
282 	struct netbuf *svcaddr;			/* servers address */
283 	CLIENT *cl = NULL;			/* client handle */
284 
285 	if (nconf == NULL) {
286 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
287 		return (NULL);
288 	}
289 
290 	/*
291 	 * Get the address of the server
292 	 */
293 	if ((svcaddr = __rpcb_findaddr_timed(prog, vers,
294 			(struct netconfig *)nconf, (char *)hostname,
295 			&cl, (struct timeval *)tp)) == NULL) {
296 		/* appropriate error number is set by rpcbind libraries */
297 		return (NULL);
298 	}
299 	if (cl == NULL) {
300 		cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
301 					prog, vers, 0, 0);
302 	} else {
303 		/* Reuse the CLIENT handle and change the appropriate fields */
304 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
305 			if (cl->cl_netid == NULL)
306 				cl->cl_netid = strdup(nconf->nc_netid);
307 			if (cl->cl_tp == NULL)
308 				cl->cl_tp = strdup(nconf->nc_device);
309 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
310 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
311 		} else {
312 			CLNT_DESTROY(cl);
313 			cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
314 					prog, vers, 0, 0);
315 		}
316 	}
317 	free(svcaddr->buf);
318 	free(svcaddr);
319 	return (cl);
320 }
321 
322 /*
323  * Generic client creation:  returns client handle.
324  * Default options are set, which the user can
325  * change using the rpc equivalent of _ioctl()'s : clnt_control().
326  * If fd is RPC_ANYFD, it will be opened using nconf.
327  * It will be bound if not so.
328  * If sizes are 0; appropriate defaults will be chosen.
329  */
330 CLIENT *
331 clnt_tli_create(int fd, const struct netconfig *nconf,
332 	struct netbuf *svcaddr, rpcprog_t prog, rpcvers_t vers,
333 	uint sendsz, uint recvsz)
334 {
335 	CLIENT *cl;			/* client handle */
336 	bool_t madefd = FALSE;		/* whether fd opened here */
337 	long servtype;
338 	int one = 1;
339 	struct __rpc_sockinfo si;
340 	extern int __rpc_minfd;
341 
342 	if (fd == RPC_ANYFD) {
343 		if (nconf == NULL) {
344 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
345 			return (NULL);
346 		}
347 
348 		fd = __rpc_nconf2fd(nconf);
349 
350 		if (fd == -1)
351 			goto err;
352 		if (fd < __rpc_minfd)
353 			fd = __rpc_raise_fd(fd);
354 		madefd = TRUE;
355 		servtype = nconf->nc_semantics;
356 		if (!__rpc_fd2sockinfo(fd, &si))
357 			goto err;
358 		bindresvport(fd, NULL);
359 	} else {
360 		if (!__rpc_fd2sockinfo(fd, &si))
361 			goto err;
362 		servtype = __rpc_socktype2seman(si.si_socktype);
363 		if (servtype == -1) {
364 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
365 			return (NULL);
366 		}
367 	}
368 
369 	if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
370 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;	/* XXX */
371 		goto err1;
372 	}
373 
374 	switch (servtype) {
375 	case NC_TPI_COTS:
376 		cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
377 		break;
378 	case NC_TPI_COTS_ORD:
379 		if (nconf && ((strcmp(nconf->nc_protofmly, "inet") == 0))) {
380 			_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one,
381 			    sizeof (one));
382 		}
383 		cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
384 		break;
385 	case NC_TPI_CLTS:
386 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
387 		break;
388 	default:
389 		goto err;
390 	}
391 
392 	if (cl == NULL)
393 		goto err1; /* borrow errors from clnt_dg/vc creates */
394 	if (nconf) {
395 		cl->cl_netid = strdup(nconf->nc_netid);
396 		cl->cl_tp = strdup(nconf->nc_device);
397 	} else {
398 		cl->cl_netid = "";
399 		cl->cl_tp = "";
400 	}
401 	if (madefd) {
402 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
403 /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
404 	}
405 
406 	return (cl);
407 
408 err:
409 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
410 	rpc_createerr.cf_error.re_errno = errno;
411 err1:	if (madefd)
412 		(void)_close(fd);
413 	return (NULL);
414 }
415 
416 /*
417  *  To avoid conflicts with the "magic" file descriptors (0, 1, and 2),
418  *  we try to not use them.  The __rpc_raise_fd() routine will dup
419  *  a descriptor to a higher value.  If we fail to do it, we continue
420  *  to use the old one (and hope for the best).
421  */
422 int __rpc_minfd = 3;
423 
424 int
425 __rpc_raise_fd(int fd)
426 {
427 	int nfd;
428 
429 	if (fd >= __rpc_minfd)
430 		return (fd);
431 
432 	if ((nfd = _fcntl(fd, F_DUPFD, __rpc_minfd)) == -1)
433 		return (fd);
434 
435 	if (_fsync(nfd) == -1) {
436 		_close(nfd);
437 		return (fd);
438 	}
439 
440 	if (_close(fd) == -1) {
441 		/* this is okay, we will syslog an error, then use the new fd */
442 		(void) syslog(LOG_ERR,
443 			"could not close() fd %d; mem & fd leak", fd);
444 	}
445 
446 	return (nfd);
447 }
448