rpc_soc.c revision 92941
1/*	$NetBSD: rpc_soc.c,v 1.6 2000/07/06 03:10:35 christos Exp $	*/
2/*	$FreeBSD: head/lib/libc/rpc/rpc_soc.c 92941 2002-03-22 09:22:15Z obrien $ */
3
4/*
5 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6 * unrestricted use provided that this legend is included on all tape
7 * media and as a part of the software program in whole or part.  Users
8 * may copy or modify Sun RPC without charge, but are not authorized
9 * to license or distribute it to anyone else except as part of a product or
10 * program developed by the user.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California  94043
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 0
43#if !defined(lint) && defined(SCCSIDS)
44static char sccsid[] = "@(#)rpc_soc.c 1.41 89/05/02 Copyr 1988 Sun Micro";
45#endif
46#endif
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
79extern mutex_t	rpcsoc_lock;
80
81static CLIENT *clnt_com_create(struct sockaddr_in *, rpcprog_t, rpcvers_t,
82    int *, u_int, u_int, char *);
83static SVCXPRT *svc_com_create(int, u_int, u_int, char *);
84static bool_t rpc_wrap_bcast(char *, struct netbuf *, struct netconfig *);
85
86/* XXX */
87#define IN4_LOCALHOST_STRING    "127.0.0.1"
88#define IN6_LOCALHOST_STRING    "::1"
89
90/*
91 * A common clnt create routine
92 */
93static CLIENT *
94clnt_com_create(raddr, prog, vers, sockp, sendsz, recvsz, tp)
95	struct sockaddr_in *raddr;
96	rpcprog_t prog;
97	rpcvers_t vers;
98	int *sockp;
99	u_int sendsz;
100	u_int recvsz;
101	char *tp;
102{
103	CLIENT *cl;
104	int madefd = FALSE;
105	int fd = *sockp;
106	struct netconfig *nconf;
107	struct netbuf bindaddr;
108
109	mutex_lock(&rpcsoc_lock);
110	if ((nconf = __rpc_getconfip(tp)) == NULL) {
111		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
112		mutex_unlock(&rpcsoc_lock);
113		return (NULL);
114	}
115	if (fd == RPC_ANYSOCK) {
116		fd = __rpc_nconf2fd(nconf);
117		if (fd == -1)
118			goto syserror;
119		madefd = TRUE;
120	}
121
122	if (raddr->sin_port == 0) {
123		u_int proto;
124		u_short sport;
125
126		mutex_unlock(&rpcsoc_lock);	/* pmap_getport is recursive */
127		proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
128		sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
129		    proto);
130		if (sport == 0) {
131			goto err;
132		}
133		raddr->sin_port = htons(sport);
134		mutex_lock(&rpcsoc_lock);	/* pmap_getport is recursive */
135	}
136
137	/* Transform sockaddr_in to netbuf */
138	bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
139	bindaddr.buf = raddr;
140
141	bindresvport(fd, NULL);
142	cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
143				sendsz, recvsz);
144	if (cl) {
145		if (madefd == TRUE) {
146			/*
147			 * The fd should be closed while destroying the handle.
148			 */
149			(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
150			*sockp = fd;
151		}
152		(void) freenetconfigent(nconf);
153		mutex_unlock(&rpcsoc_lock);
154		return (cl);
155	}
156	goto err;
157
158syserror:
159	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
160	rpc_createerr.cf_error.re_errno = errno;
161
162err:	if (madefd == TRUE)
163		(void)_close(fd);
164	(void) freenetconfigent(nconf);
165	mutex_unlock(&rpcsoc_lock);
166	return (NULL);
167}
168
169CLIENT *
170clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
171	struct sockaddr_in *raddr;
172	u_long prog;
173	u_long vers;
174	struct timeval wait;
175	int *sockp;
176	u_int sendsz;
177	u_int recvsz;
178{
179	CLIENT *cl;
180
181	cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
182	    sendsz, recvsz, "udp");
183	if (cl == NULL) {
184		return (NULL);
185	}
186	(void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, (char *)(void *)&wait);
187	return (cl);
188}
189
190CLIENT *
191clntudp_create(raddr, program, version, wait, sockp)
192	struct sockaddr_in *raddr;
193	u_long program;
194	u_long version;
195	struct timeval wait;
196	int *sockp;
197{
198
199	return clntudp_bufcreate(raddr, program, version, wait, sockp,
200					UDPMSGSIZE, UDPMSGSIZE);
201}
202
203CLIENT *
204clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
205	struct sockaddr_in *raddr;
206	u_long prog;
207	u_long vers;
208	int *sockp;
209	u_int sendsz;
210	u_int recvsz;
211{
212
213	return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
214	    sendsz, recvsz, "tcp");
215}
216
217CLIENT *
218clntraw_create(prog, vers)
219	u_long prog;
220	u_long vers;
221{
222
223	return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
224}
225
226/*
227 * A common server create routine
228 */
229static SVCXPRT *
230svc_com_create(fd, sendsize, recvsize, netid)
231	int fd;
232	u_int sendsize;
233	u_int recvsize;
234	char *netid;
235{
236	struct netconfig *nconf;
237	SVCXPRT *svc;
238	int madefd = FALSE;
239	int port;
240	struct sockaddr_in sin;
241
242	if ((nconf = __rpc_getconfip(netid)) == NULL) {
243		(void) syslog(LOG_ERR, "Could not get %s transport", netid);
244		return (NULL);
245	}
246	if (fd == RPC_ANYSOCK) {
247		fd = __rpc_nconf2fd(nconf);
248		if (fd == -1) {
249			(void) freenetconfigent(nconf);
250			(void) syslog(LOG_ERR,
251			"svc%s_create: could not open connection", netid);
252			return (NULL);
253		}
254		madefd = TRUE;
255	}
256
257	memset(&sin, 0, sizeof sin);
258	sin.sin_family = AF_INET;
259	bindresvport(fd, &sin);
260	_listen(fd, SOMAXCONN);
261	svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
262	(void) freenetconfigent(nconf);
263	if (svc == NULL) {
264		if (madefd)
265			(void)_close(fd);
266		return (NULL);
267	}
268	port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
269	svc->xp_port = ntohs(port);
270	return (svc);
271}
272
273SVCXPRT *
274svctcp_create(fd, sendsize, recvsize)
275	int fd;
276	u_int sendsize;
277	u_int recvsize;
278{
279
280	return svc_com_create(fd, sendsize, recvsize, "tcp");
281}
282
283SVCXPRT *
284svcudp_bufcreate(fd, sendsz, recvsz)
285	int fd;
286	u_int sendsz, recvsz;
287{
288
289	return svc_com_create(fd, sendsz, recvsz, "udp");
290}
291
292SVCXPRT *
293svcfd_create(fd, sendsize, recvsize)
294	int fd;
295	u_int sendsize;
296	u_int recvsize;
297{
298
299	return svc_fd_create(fd, sendsize, recvsize);
300}
301
302
303SVCXPRT *
304svcudp_create(fd)
305	int fd;
306{
307
308	return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
309}
310
311SVCXPRT *
312svcraw_create()
313{
314
315	return svc_raw_create();
316}
317
318int
319get_myaddress(addr)
320	struct sockaddr_in *addr;
321{
322
323	memset((void *) addr, 0, sizeof(*addr));
324	addr->sin_family = AF_INET;
325	addr->sin_port = htons(PMAPPORT);
326	addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
327	return (0);
328}
329
330/*
331 * For connectionless "udp" transport. Obsoleted by rpc_call().
332 */
333int
334callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
335	char *host;
336	int prognum, versnum, procnum;
337	xdrproc_t inproc, outproc;
338	void *in, *out;
339{
340
341	return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
342	    (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
343}
344
345/*
346 * For connectionless kind of transport. Obsoleted by rpc_reg()
347 */
348int
349registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
350	int prognum, versnum, procnum;
351	char *(*progname)(char [UDPMSGSIZE]);
352	xdrproc_t inproc, outproc;
353{
354
355	return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
356	    (rpcproc_t)procnum, progname, inproc, outproc, "udp");
357}
358
359/*
360 * All the following clnt_broadcast stuff is convulated; it supports
361 * the earlier calling style of the callback function
362 */
363static thread_key_t	clnt_broadcast_key;
364static resultproc_t	clnt_broadcast_result_main;
365
366/*
367 * Need to translate the netbuf address into sockaddr_in address.
368 * Dont care about netid here.
369 */
370/* ARGSUSED */
371static bool_t
372rpc_wrap_bcast(resultp, addr, nconf)
373	char *resultp;		/* results of the call */
374	struct netbuf *addr;	/* address of the guy who responded */
375	struct netconfig *nconf; /* Netconf of the transport */
376{
377	resultproc_t clnt_broadcast_result;
378
379	if (strcmp(nconf->nc_netid, "udp"))
380		return (FALSE);
381	if (thr_main())
382		clnt_broadcast_result = clnt_broadcast_result_main;
383	else
384		clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
385	return (*clnt_broadcast_result)(resultp,
386				(struct sockaddr_in *)addr->buf);
387}
388
389/*
390 * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
391 */
392enum clnt_stat
393clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
394	u_long		prog;		/* program number */
395	u_long		vers;		/* version number */
396	u_long		proc;		/* procedure number */
397	xdrproc_t	xargs;		/* xdr routine for args */
398	caddr_t		argsp;		/* pointer to args */
399	xdrproc_t	xresults;	/* xdr routine for results */
400	caddr_t		resultsp;	/* pointer to results */
401	resultproc_t	eachresult;	/* call with each result obtained */
402{
403	extern mutex_t tsd_lock;
404
405	if (thr_main())
406		clnt_broadcast_result_main = eachresult;
407	else {
408		if (clnt_broadcast_key == 0) {
409			mutex_lock(&tsd_lock);
410			if (clnt_broadcast_key == 0)
411				thr_keycreate(&clnt_broadcast_key, free);
412			mutex_unlock(&tsd_lock);
413		}
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 */
425AUTH *
426authdes_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	}
448fallback:
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 */
456CLIENT *
457clntunix_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);
499done:
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 */
509SVCXPRT *
510svcunix_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
564done:
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 */
573SVCXPRT *
574svcunixfd_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