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