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