]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libc/rpc/rpc_soc.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libc / rpc / rpc_soc.c
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
364 /*
365  * Need to translate the netbuf address into sockaddr_in address.
366  * Dont care about netid here.
367  */
368 /* ARGSUSED */
369 static bool_t
370 rpc_wrap_bcast(resultp, addr, nconf)
371         char *resultp;          /* results of the call */
372         struct netbuf *addr;    /* address of the guy who responded */
373         struct netconfig *nconf; /* Netconf of the transport */
374 {
375         resultproc_t clnt_broadcast_result;
376
377         if (strcmp(nconf->nc_netid, "udp"))
378                 return (FALSE);
379         if (thr_main())
380                 clnt_broadcast_result = clnt_broadcast_result_main;
381         else
382                 clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
383         return (*clnt_broadcast_result)(resultp,
384                                 (struct sockaddr_in *)addr->buf);
385 }
386
387 /*
388  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
389  */
390 enum clnt_stat
391 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
392         u_long          prog;           /* program number */
393         u_long          vers;           /* version number */
394         u_long          proc;           /* procedure number */
395         xdrproc_t       xargs;          /* xdr routine for args */
396         void           *argsp;          /* pointer to args */
397         xdrproc_t       xresults;       /* xdr routine for results */
398         void           *resultsp;       /* pointer to results */
399         resultproc_t    eachresult;     /* call with each result obtained */
400 {
401
402         if (thr_main())
403                 clnt_broadcast_result_main = eachresult;
404         else {
405                 if (clnt_broadcast_key == 0) {
406                         mutex_lock(&tsd_lock);
407                         if (clnt_broadcast_key == 0)
408                                 thr_keycreate(&clnt_broadcast_key, free);
409                         mutex_unlock(&tsd_lock);
410                 }
411                 thr_setspecific(clnt_broadcast_key, (void *) eachresult);
412         }
413         return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
414             (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
415             (resultproc_t) rpc_wrap_bcast, "udp");
416 }
417
418 /*
419  * Create the client des authentication object. Obsoleted by
420  * authdes_seccreate().
421  */
422 AUTH *
423 authdes_create(servername, window, syncaddr, ckey)
424         char *servername;               /* network name of server */
425         u_int window;                   /* time to live */
426         struct sockaddr *syncaddr;      /* optional hostaddr to sync with */
427         des_block *ckey;                /* optional conversation key to use */
428 {
429         AUTH *dummy;
430         AUTH *nauth;
431         char hostname[NI_MAXHOST];
432
433         if (syncaddr) {
434                 /*
435                  * Change addr to hostname, because that is the way
436                  * new interface takes it.
437                  */
438                 if (getnameinfo(syncaddr, syncaddr->sa_len, hostname,
439                     sizeof hostname, NULL, 0, 0) != 0)
440                         goto fallback;
441
442                 nauth = authdes_seccreate(servername, window, hostname, ckey);
443                 return (nauth);
444         }
445 fallback:
446         dummy = authdes_seccreate(servername, window, NULL, ckey);
447         return (dummy);
448 }
449
450 /*
451  * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
452  */
453 CLIENT *
454 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
455         struct sockaddr_un *raddr;
456         u_long prog;
457         u_long vers;
458         int *sockp;
459         u_int sendsz;
460         u_int recvsz;
461 {
462         struct netbuf *svcaddr;
463         struct netconfig *nconf;
464         CLIENT *cl;
465         int len;
466
467         cl = NULL;
468         nconf = NULL;
469         svcaddr = NULL;
470         if ((raddr->sun_len == 0) ||
471            ((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
472            ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
473                 if (svcaddr != NULL)
474                         free(svcaddr);
475                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
476                 rpc_createerr.cf_error.re_errno = errno;
477                 return(cl);
478         }
479         if (*sockp < 0) {
480                 *sockp = _socket(AF_LOCAL, SOCK_STREAM, 0);
481                 len = raddr->sun_len = SUN_LEN(raddr);
482                 if ((*sockp < 0) || (_connect(*sockp,
483                     (struct sockaddr *)raddr, len) < 0)) {
484                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
485                         rpc_createerr.cf_error.re_errno = errno;
486                         if (*sockp != -1)
487                                 (void)_close(*sockp);
488                         goto done;
489                 }
490         }
491         svcaddr->buf = raddr;
492         svcaddr->len = raddr->sun_len;
493         svcaddr->maxlen = sizeof (struct sockaddr_un);
494         cl = clnt_vc_create(*sockp, svcaddr, prog,
495             vers, sendsz, recvsz);
496 done:
497         free(svcaddr->buf);
498         free(svcaddr);
499         return(cl);
500 }
501
502 /*
503  * Creates, registers, and returns a (rpc) unix based transporter.
504  * Obsoleted by svc_vc_create().
505  */
506 SVCXPRT *
507 svcunix_create(sock, sendsize, recvsize, path)
508         int sock;
509         u_int sendsize;
510         u_int recvsize;
511         char *path;
512 {
513         struct netconfig *nconf;
514         void *localhandle;
515         struct sockaddr_un sun;
516         struct sockaddr *sa;
517         struct t_bind taddr;
518         SVCXPRT *xprt;
519         int addrlen;
520
521         xprt = (SVCXPRT *)NULL;
522         localhandle = setnetconfig();
523         while ((nconf = getnetconfig(localhandle)) != NULL) {
524                 if (nconf->nc_protofmly != NULL &&
525                     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
526                         break;
527         }
528         if (nconf == NULL)
529                 return(xprt);
530
531         if ((sock = __rpc_nconf2fd(nconf)) < 0)
532                 goto done;
533
534         memset(&sun, 0, sizeof sun);
535         sun.sun_family = AF_LOCAL;
536         if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
537             sizeof(sun.sun_path))
538                 goto done;
539         sun.sun_len = SUN_LEN(&sun);
540         addrlen = sizeof (struct sockaddr_un);
541         sa = (struct sockaddr *)&sun;
542
543         if (_bind(sock, sa, addrlen) < 0)
544                 goto done;
545
546         taddr.addr.len = taddr.addr.maxlen = addrlen;
547         taddr.addr.buf = malloc(addrlen);
548         if (taddr.addr.buf == NULL)
549                 goto done;
550         memcpy(taddr.addr.buf, sa, addrlen);
551
552         if (nconf->nc_semantics != NC_TPI_CLTS) {
553                 if (_listen(sock, SOMAXCONN) < 0) {
554                         free(taddr.addr.buf);
555                         goto done;
556                 }
557         }
558
559         xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
560
561 done:
562         endnetconfig(localhandle);
563         return(xprt);
564 }
565
566 /*
567  * Like svunix_create(), except the routine takes any *open* UNIX file
568  * descriptor as its first input. Obsoleted by svc_fd_create();
569  */
570 SVCXPRT *
571 svcunixfd_create(fd, sendsize, recvsize)
572         int fd;
573         u_int sendsize;
574         u_int recvsize;
575 {
576         return (svc_fd_create(fd, sendsize, recvsize));
577 }
578
579 #endif /* PORTMAP */