xref: /freebsd/lib/libc/rpc/rpc_soc.c (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1 /*	$NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, 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 Sun Microsystems, 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	"@(#)rpc_soc.c	1.17	94/04/24 SMI" */
34 
35 /*
36  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
37  * In addition, portions of such source code were derived from Berkeley
38  * 4.3 BSD under license from the Regents of the University of
39  * California.
40  */
41 
42 #if defined(LIBC_SCCS) && !defined(lint)
43 static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
44 #endif
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #ifdef PORTMAP
49 /*
50  * rpc_soc.c
51  *
52  * The backward compatibility routines for the earlier implementation
53  * of RPC, where the only transports supported were tcp/ip and udp/ip.
54  * Based on berkeley socket abstraction, now implemented on the top
55  * of TLI/Streams
56  */
57 
58 #include "namespace.h"
59 #include "reentrant.h"
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <stdio.h>
63 #include <rpc/rpc.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_prot.h>
66 #include <rpc/nettype.h>
67 #include <syslog.h>
68 #include <netinet/in.h>
69 #include <netdb.h>
70 #include <errno.h>
71 #include <syslog.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <unistd.h>
75 #include "un-namespace.h"
76 
77 #include "rpc_com.h"
78 #include "mt_misc.h"
79 
80 static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
81     int *, u_int, u_int, char *);
82 static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
83 static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
84 
85 /* XXX */
86 #define IN4_LOCALHOST_STRING    "127.0.0.1"
87 #define IN6_LOCALHOST_STRING    "::1"
88 
89 /*
90  * A common clnt create routine
91  */
92 static CLIENT *
93 clnt_com_create(struct sockaddr_in *raddr, rpcprog_t prog, rpcvers_t vers, int *sockp,
94     u_int sendsz, u_int recvsz, char *tp)
95 {
96 	CLIENT *cl;
97 	int madefd = FALSE;
98 	int fd = *sockp;
99 	struct netconfig *nconf;
100 	struct netbuf bindaddr;
101 
102 	mutex_lock(&rpcsoc_lock);
103 	if ((nconf = __rpc_getconfip(tp)) == NULL) {
104 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
105 		mutex_unlock(&rpcsoc_lock);
106 		return (NULL);
107 	}
108 	if (fd == RPC_ANYSOCK) {
109 		fd = __rpc_nconf2fd(nconf);
110 		if (fd == -1)
111 			goto syserror;
112 		madefd = TRUE;
113 	}
114 
115 	if (raddr->sin_port == 0) {
116 		u_int proto;
117 		u_short sport;
118 
119 		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
120 		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
121 		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
122 		    proto);
123 		if (sport == 0) {
124 			goto err;
125 		}
126 		raddr->sin_port = htons(sport);
127 		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
128 	}
129 
130 	/* Transform sockaddr_in to netbuf */
131 	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
132 	bindaddr.buf = raddr;
133 
134 	bindresvport(fd, NULL);
135 	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
136 				sendsz, recvsz);
137 	if (cl) {
138 		if (madefd == TRUE) {
139 			/*
140 			 * The fd should be closed while destroying the handle.
141 			 */
142 			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
143 			*sockp = fd;
144 		}
145 		(void) freenetconfigent(nconf);
146 		mutex_unlock(&rpcsoc_lock);
147 		return (cl);
148 	}
149 	goto err;
150 
151 syserror:
152 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
153 	rpc_createerr.cf_error.re_errno = errno;
154 
155 err:	if (madefd == TRUE)
156 		(void)_close(fd);
157 	(void) freenetconfigent(nconf);
158 	mutex_unlock(&rpcsoc_lock);
159 	return (NULL);
160 }
161 
162 CLIENT *
163 clntudp_bufcreate(struct sockaddr_in *raddr, u_long prog, u_long vers,
164     struct timeval wait, int *sockp, u_int sendsz, u_int recvsz)
165 {
166 	CLIENT *cl;
167 
168 	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
169 	    sendsz, recvsz, "udp");
170 	if (cl == NULL) {
171 		return (NULL);
172 	}
173 	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
174 	return (cl);
175 }
176 
177 CLIENT *
178 clntudp_create(struct sockaddr_in *raddr, u_long program, u_long version,
179     struct timeval wait, int *sockp)
180 {
181 
182 	return clntudp_bufcreate(raddr, program, version, wait, sockp,
183 					UDPMSGSIZE, UDPMSGSIZE);
184 }
185 
186 CLIENT *
187 clnttcp_create(struct sockaddr_in *raddr, u_long prog, u_long vers, int *sockp,
188     u_int sendsz, u_int recvsz)
189 {
190 
191 	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
192 	    sendsz, recvsz, "tcp");
193 }
194 
195 CLIENT *
196 clntraw_create(u_long prog, u_long vers)
197 {
198 
199 	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
200 }
201 
202 /*
203  * A common server create routine
204  */
205 static SVCXPRT *
206 svc_com_create(int fd, u_int sendsize, u_int recvsize, char *netid)
207 {
208 	struct netconfig *nconf;
209 	SVCXPRT *svc;
210 	int madefd = FALSE;
211 	int port;
212 	struct sockaddr_in sin;
213 
214 	if ((nconf = __rpc_getconfip(netid)) == NULL) {
215 		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
216 		return (NULL);
217 	}
218 	if (fd == RPC_ANYSOCK) {
219 		fd = __rpc_nconf2fd(nconf);
220 		if (fd == -1) {
221 			(void) freenetconfigent(nconf);
222 			(void) syslog(LOG_ERR,
223 			"svc%s_create: could not open connection", netid);
224 			return (NULL);
225 		}
226 		madefd = TRUE;
227 	}
228 
229 	memset(&sin, 0, sizeof sin);
230 	sin.sin_family = AF_INET;
231 	bindresvport(fd, &sin);
232 	_listen(fd, SOMAXCONN);
233 	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
234 	(void) freenetconfigent(nconf);
235 	if (svc == NULL) {
236 		if (madefd)
237 			(void)_close(fd);
238 		return (NULL);
239 	}
240 	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
241 	svc->xp_port = ntohs(port);
242 	return (svc);
243 }
244 
245 SVCXPRT *
246 svctcp_create(int fd, u_int sendsize, u_int recvsize)
247 {
248 
249 	return svc_com_create(fd, sendsize, recvsize, "tcp");
250 }
251 
252 SVCXPRT *
253 svcudp_bufcreate(int fd, u_int sendsz, u_int recvsz)
254 {
255 
256 	return svc_com_create(fd, sendsz, recvsz, "udp");
257 }
258 
259 SVCXPRT *
260 svcfd_create(int fd, u_int sendsize, u_int recvsize)
261 {
262 
263 	return svc_fd_create(fd, sendsize, recvsize);
264 }
265 
266 
267 SVCXPRT *
268 svcudp_create(int fd)
269 {
270 
271 	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
272 }
273 
274 SVCXPRT *
275 svcraw_create(void)
276 {
277 
278 	return svc_raw_create();
279 }
280 
281 int
282 get_myaddress(struct sockaddr_in *addr)
283 {
284 
285 	memset((void *) addr, 0, sizeof(*addr));
286 	addr->sin_family = AF_INET;
287 	addr->sin_port = htons(PMAPPORT);
288 	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
289 	return (0);
290 }
291 
292 /*
293  * For connectionless "udp" transport. Obsoleted by rpc_call().
294  */
295 int
296 callrpc(const char *host, int prognum, int versnum, int procnum,
297     xdrproc_t inproc, void *in, xdrproc_t outproc, void *out)
298 {
299 
300 	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
301 	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
302 }
303 
304 /*
305  * For connectionless kind of transport. Obsoleted by rpc_reg()
306  */
307 int
308 registerrpc(int prognum, int versnum, int procnum,
309     char *(*progname)(char [UDPMSGSIZE]),
310     xdrproc_t inproc, xdrproc_t outproc)
311 {
312 
313 	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
314 	    (rpcproc_t)procnum, progname, inproc, outproc, "udp");
315 }
316 
317 /*
318  * All the following clnt_broadcast stuff is convulated; it supports
319  * the earlier calling style of the callback function
320  */
321 static thread_key_t	clnt_broadcast_key;
322 static resultproc_t	clnt_broadcast_result_main;
323 static once_t		clnt_broadcast_once = ONCE_INITIALIZER;
324 
325 static void
326 clnt_broadcast_key_init(void)
327 {
328 
329 	thr_keycreate(&clnt_broadcast_key, free);
330 }
331 
332 /*
333  * Need to translate the netbuf address into sockaddr_in address.
334  * Dont care about netid here.
335  */
336 /* ARGSUSED */
337 static bool_t
338 rpc_wrap_bcast(char *resultp, struct netbuf *addr, struct netconfig *nconf)
339 /*
340  *	char *resultp;		// results of the call
341  *	struct netbuf *addr;	// address of the guy who responded
342  *	struct netconfig *nconf; // Netconf of the transport
343  */
344 {
345 	resultproc_t clnt_broadcast_result;
346 
347 	if (strcmp(nconf->nc_netid, "udp"))
348 		return (FALSE);
349 	if (thr_main())
350 		clnt_broadcast_result = clnt_broadcast_result_main;
351 	else
352 		clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
353 	return (*clnt_broadcast_result)(resultp,
354 				(struct sockaddr_in *)addr->buf);
355 }
356 
357 /*
358  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
359  */
360 enum clnt_stat
361 clnt_broadcast(u_long prog, u_long vers, u_long proc, xdrproc_t xargs,
362     void *argsp, xdrproc_t xresults, void *resultsp, resultproc_t eachresult)
363 /*
364  *	u_long		prog;		// program number
365  *	u_long		vers;		// version number
366  *	u_long		proc;		// procedure number
367  *	xdrproc_t	xargs;		// xdr routine for args
368  *	void	       *argsp;		// pointer to args
369  *	xdrproc_t	xresults;	// xdr routine for results
370  *	void	       *resultsp;	// pointer to results
371  *	resultproc_t	eachresult;	// call with each result obtained
372  */
373 {
374 
375 	if (thr_main())
376 		clnt_broadcast_result_main = eachresult;
377 	else {
378 		thr_once(&clnt_broadcast_once, clnt_broadcast_key_init);
379 		thr_setspecific(clnt_broadcast_key, (void *) eachresult);
380 	}
381 	return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
382 	    (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
383 	    (resultproc_t) rpc_wrap_bcast, "udp");
384 }
385 
386 /*
387  * Create the client des authentication object. Obsoleted by
388  * authdes_seccreate().
389  */
390 AUTH *
391 authdes_create(char *servername, u_int window, struct sockaddr *syncaddr,
392     des_block *ckey)
393 /*
394  *	char *servername;		// network name of server
395  *	u_int window;			// time to live
396  *	struct sockaddr *syncaddr;	// optional hostaddr to sync with
397  *	des_block *ckey;		// optional conversation key to use
398  */
399 {
400 	AUTH *dummy;
401 	AUTH *nauth;
402 	char hostname[NI_MAXHOST];
403 
404 	if (syncaddr) {
405 		/*
406 		 * Change addr to hostname, because that is the way
407 		 * new interface takes it.
408 		 */
409 		if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
410 		    sizeof hostname, NULL, 0, 0) != 0)
411 			goto fallback;
412 
413 		nauth = authdes_seccreate(servername, window, hostname, ckey);
414 		return (nauth);
415 	}
416 fallback:
417 	dummy = authdes_seccreate(servername, window, NULL, ckey);
418 	return (dummy);
419 }
420 
421 /*
422  * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
423  */
424 CLIENT *
425 clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
426     u_int sendsz, u_int recvsz)
427 {
428 	struct netbuf *svcaddr;
429 	CLIENT *cl;
430 	int len;
431 
432 	cl = NULL;
433 	svcaddr = NULL;
434 	if ((raddr->sun_len == 0) ||
435 	   ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
436 	   ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
437 		free(svcaddr);
438 		rpc_createerr.cf_stat = RPC_SYSTEMERROR;
439 		rpc_createerr.cf_error.re_errno = errno;
440 		return(cl);
441 	}
442 	if (*sockp < 0) {
443 		*sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
444 		len = raddr->sun_len = SUN_LEN(raddr);
445 		if ((*sockp < 0) || (_connect(*sockp,
446 		    (struct sockaddr *)raddr, len) < 0)) {
447 			rpc_createerr.cf_stat = RPC_SYSTEMERROR;
448 			rpc_createerr.cf_error.re_errno = errno;
449 			if (*sockp != -1)
450 				(void)_close(*sockp);
451 			goto done;
452 		}
453 	}
454 	svcaddr->buf = raddr;
455 	svcaddr->len = raddr->sun_len;
456 	svcaddr->maxlen = sizeof (struct sockaddr_un);
457 	cl = clnt_vc_create(*sockp, svcaddr, prog,
458 	    vers, sendsz, recvsz);
459 done:
460 	free(svcaddr->buf);
461 	free(svcaddr);
462 	return(cl);
463 }
464 
465 /*
466  * Creates, registers, and returns a (rpc) unix based transporter.
467  * Obsoleted by svc_vc_create().
468  */
469 SVCXPRT *
470 svcunix_create(int sock, u_int sendsize, u_int recvsize, char *path)
471 {
472 	struct netconfig *nconf;
473 	void *localhandle;
474 	struct sockaddr_un sun;
475 	struct sockaddr *sa;
476 	struct t_bind taddr;
477 	SVCXPRT *xprt;
478 	int addrlen;
479 
480 	xprt = (SVCXPRT *)NULL;
481 	localhandle = setnetconfig();
482 	while ((nconf = getnetconfig(localhandle)) != NULL) {
483 		if (nconf->nc_protofmly != NULL &&
484 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
485 			break;
486 	}
487 	if (nconf == NULL)
488 		goto done;
489 
490 	if ((sock = __rpc_nconf2fd(nconf)) < 0)
491 		goto done;
492 
493 	memset(&sun, 0, sizeof sun);
494 	sun.sun_family = AF_LOCAL;
495 	if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
496 	    sizeof(sun.sun_path))
497 		goto done;
498 	sun.sun_len = SUN_LEN(&sun);
499 	addrlen = sizeof (struct sockaddr_un);
500 	sa = (struct sockaddr *)&sun;
501 
502 	if (_bind(sock, sa, addrlen) < 0)
503 		goto done;
504 
505 	taddr.addr.len = taddr.addr.maxlen = addrlen;
506 	taddr.addr.buf = malloc(addrlen);
507 	if (taddr.addr.buf == NULL)
508 		goto done;
509 	memcpy(taddr.addr.buf, sa, addrlen);
510 
511 	if (nconf->nc_semantics != NC_TPI_CLTS) {
512 		if (_listen(sock, SOMAXCONN) < 0) {
513 			free(taddr.addr.buf);
514 			goto done;
515 		}
516 	}
517 
518 	xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
519 
520 done:
521 	endnetconfig(localhandle);
522 	return(xprt);
523 }
524 
525 /*
526  * Like svunix_create(), except the routine takes any *open* UNIX file
527  * descriptor as its first input. Obsoleted by svc_fd_create();
528  */
529 SVCXPRT *
530 svcunixfd_create(int fd, u_int sendsize, u_int recvsize)
531 {
532  	return (svc_fd_create(fd, sendsize, recvsize));
533 }
534 
535 #endif /* PORTMAP */
536