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