Imported Upstream version 0.2.5
[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 #ifdef SOCK_CLOEXEC
108                 if (fd == -1) {
109                         if ((flags & SOCK_CLOEXEC) && have_cloexec <= 0) {
110                                 fd = __rpc_nconf2fd(nconf);
111                                 if (fd == -1)
112                                         goto syserror;
113                                 if (flags & SOCK_CLOEXEC) {
114                                         have_cloexec = -1;
115                                         fcntl(fd, F_SETFD, FD_CLOEXEC);
116                                 }
117                         } else
118                                 goto syserror;
119                 } else if (flags & SOCK_CLOEXEC)
120                         have_cloexec = 1;
121 #else
122                 if (fd == -1)
123                         goto syserror;
124 #endif
125                 madefd = TRUE;
126         }
127
128         if (raddr->sin_port == 0) {
129                 u_int proto;
130                 u_short sport;
131
132                 mutex_unlock(&rpcsoc_lock);     /* pmap_getport is recursive */
133                 proto = strcmp(tp, "udp") == 0 ? IPPROTO_UDP : IPPROTO_TCP;
134                 sport = pmap_getport(raddr, (u_long)prog, (u_long)vers,
135                     proto);
136                 if (sport == 0) {
137                         goto err;
138                 }
139                 raddr->sin_port = htons(sport);
140                 mutex_lock(&rpcsoc_lock);       /* pmap_getport is recursive */
141         }
142
143         /* Transform sockaddr_in to netbuf */
144         bindaddr.maxlen = bindaddr.len =  sizeof (struct sockaddr_in);
145         bindaddr.buf = raddr;
146
147         bindresvport(fd, NULL);
148         cl = clnt_tli_create(fd, nconf, &bindaddr, prog, vers,
149                                 sendsz, recvsz);
150         if (cl) {
151                 if (madefd == TRUE) {
152                         /*
153                          * The fd should be closed while destroying the handle.
154                          */
155                         (void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
156                         *sockp = fd;
157                 }
158                 (void) freenetconfigent(nconf);
159                 mutex_unlock(&rpcsoc_lock);
160                 return (cl);
161         }
162         goto err;
163
164 syserror:
165         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
166         rpc_createerr.cf_error.re_errno = errno;
167
168 err:    if (madefd == TRUE)
169                 (void)close(fd);
170         (void) freenetconfigent(nconf);
171         mutex_unlock(&rpcsoc_lock);
172         return (NULL);
173 }
174
175 CLIENT *
176 __libc_clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz, flags)
177         struct sockaddr_in *raddr;
178         u_long prog;
179         u_long vers;
180         struct timeval wait;
181         int *sockp;
182         u_int sendsz;
183         u_int recvsz;
184         int flags;
185 {
186         CLIENT *cl;
187
188         cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
189             sendsz, recvsz, "udp", flags);
190         if (cl == NULL) {
191                 return (NULL);
192         }
193         (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
194         return (cl);
195 }
196
197 CLIENT *
198 clntudp_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
199         struct sockaddr_in *raddr;
200         u_long prog;
201         u_long vers;
202         struct timeval wait;
203         int *sockp;
204         u_int sendsz;
205         u_int recvsz;
206 {
207         CLIENT *cl;
208
209         cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
210             sendsz, recvsz, "udp", 0);
211         if (cl == NULL) {
212                 return (NULL);
213         }
214         (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
215         return (cl);
216 }
217
218 CLIENT *
219 clntudp_create(raddr, program, version, wait, sockp)
220         struct sockaddr_in *raddr;
221         u_long program;
222         u_long version;
223         struct timeval wait;
224         int *sockp;
225 {
226         return clntudp_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
227 }
228
229 CLIENT *
230 clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
231         struct sockaddr_in *raddr;
232         u_long prog;
233         u_long vers;
234         int *sockp;
235         u_int sendsz;
236         u_int recvsz;
237 {
238         return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
239             sendsz, recvsz, "tcp", 0);
240 }
241
242 /* IPv6 version of clnt*_*create */
243
244 #ifdef INET6_NOT_USED
245
246 CLIENT *
247 clntudp6_bufcreate(raddr, prog, vers, wait, sockp, sendsz, recvsz)
248         struct sockaddr_in6 *raddr;
249         u_long prog;
250         u_long vers;
251         struct timeval wait;
252         int *sockp;
253         u_int sendsz;
254         u_int recvsz;
255 {
256         CLIENT *cl;
257
258         cl = clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
259             sendsz, recvsz, "udp6", 0);
260         if (cl == NULL) {
261                 return (NULL);
262         }
263         (void) CLNT_CONTROL(cl, CLSET_RETRY_TIMEOUT, &wait);
264         return (cl);
265 }
266
267 CLIENT *
268 clntudp6_create(raddr, program, version, wait, sockp)
269         struct sockaddr_in6 *raddr;
270         u_long program;
271         u_long version;
272         struct timeval wait;
273         int *sockp;
274 {
275         return clntudp6_bufcreate(raddr, program, version, wait, sockp, UDPMSGSIZE, UDPMSGSIZE);
276 }
277
278 CLIENT *
279 clnttcp6_create(raddr, prog, vers, sockp, sendsz, recvsz)
280         struct sockaddr_in6 *raddr;
281         u_long prog;
282         u_long vers;
283         int *sockp;
284         u_int sendsz;
285         u_int recvsz;
286 {
287         return clnt_com_create(raddr, (rpcprog_t)prog, (rpcvers_t)vers, sockp,
288             sendsz, recvsz, "tcp6", 0);
289 }
290
291 #endif
292
293 CLIENT *
294 clntraw_create(prog, vers)
295         u_long prog;
296         u_long vers;
297 {
298         return clnt_raw_create((rpcprog_t)prog, (rpcvers_t)vers);
299 }
300
301 /*
302  * A common server create routine
303  */
304 static SVCXPRT *
305 svc_com_create(fd, sendsize, recvsize, netid)
306         int fd;
307         u_int sendsize;
308         u_int recvsize;
309         char *netid;
310 {
311         struct netconfig *nconf;
312         SVCXPRT *svc;
313         int madefd = FALSE;
314         int port;
315         struct sockaddr_in sin;
316
317         if ((nconf = __rpc_getconfip(netid)) == NULL) {
318                 (void) syslog(LOG_ERR, "Could not get %s transport", netid);
319                 return (NULL);
320         }
321         if (fd == RPC_ANYSOCK) {
322                 fd = __rpc_nconf2fd(nconf);
323                 if (fd == -1) {
324                         (void) freenetconfigent(nconf);
325                         (void) syslog(LOG_ERR,
326                         "svc%s_create: could not open connection", netid);
327                         return (NULL);
328                 }
329                 madefd = TRUE;
330         }
331
332         memset(&sin, 0, sizeof sin);
333         sin.sin_family = AF_INET;
334         bindresvport(fd, &sin);
335         listen(fd, SOMAXCONN);
336         svc = svc_tli_create(fd, nconf, NULL, sendsize, recvsize);
337         (void) freenetconfigent(nconf);
338         if (svc == NULL) {
339                 if (madefd)
340                         (void)close(fd);
341                 return (NULL);
342         }
343         port = (((struct sockaddr_in *)svc->xp_ltaddr.buf)->sin_port);
344         svc->xp_port = ntohs(port);
345         return (svc);
346 }
347
348 SVCXPRT *
349 svctcp_create(fd, sendsize, recvsize)
350         int fd;
351         u_int sendsize;
352         u_int recvsize;
353 {
354
355         return svc_com_create(fd, sendsize, recvsize, "tcp");
356 }
357
358
359
360 SVCXPRT *
361 svcudp_bufcreate(fd, sendsz, recvsz)
362         int fd;
363         u_int sendsz, recvsz;
364 {
365
366         return svc_com_create(fd, sendsz, recvsz, "udp");
367 }
368
369
370
371 SVCXPRT *
372 svcfd_create(fd, sendsize, recvsize)
373         int fd;
374         u_int sendsize;
375         u_int recvsize;
376 {
377
378         return svc_fd_create(fd, sendsize, recvsize);
379 }
380
381
382 SVCXPRT *
383 svcudp_create(fd)
384         int fd;
385 {
386
387         return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp");
388 }
389
390
391 SVCXPRT *
392 svcraw_create()
393 {
394
395         return svc_raw_create();
396 }
397
398
399 /* IPV6 version */
400 #ifdef INET6_NOT_USED
401 SVCXPRT *
402 svcudp6_bufcreate(fd, sendsz, recvsz)
403         int fd;
404         u_int sendsz, recvsz;
405 {
406         return svc_com_create(fd, sendsz, recvsz, "udp6");
407 }
408
409
410 SVCXPRT *
411 svctcp6_create(fd, sendsize, recvsize)
412         int fd;
413         u_int sendsize;
414         u_int recvsize;
415 {
416         return svc_com_create(fd, sendsize, recvsize, "tcp6");
417 }
418
419
420 SVCXPRT *
421 svcudp6_create(fd)
422         int fd;
423 {
424         return svc_com_create(fd, UDPMSGSIZE, UDPMSGSIZE, "udp6");
425 }
426 #endif
427
428 int
429 get_myaddress(addr)
430         struct sockaddr_in *addr;
431 {
432
433         memset((void *) addr, 0, sizeof(*addr));
434         addr->sin_family = AF_INET;
435         addr->sin_port = htons(PMAPPORT);
436         addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
437         return (0);
438 }
439
440 /*
441  * For connectionless "udp" transport. Obsoleted by rpc_call().
442  */
443 int
444 callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
445         const char *host;
446         int prognum, versnum, procnum;
447         xdrproc_t inproc, outproc;
448         void *in, *out;
449 {
450
451         return (int)rpc_call(host, (rpcprog_t)prognum, (rpcvers_t)versnum,
452             (rpcproc_t)procnum, inproc, in, outproc, out, "udp");
453 }
454
455 /*
456  * For connectionless kind of transport. Obsoleted by rpc_reg()
457  */
458 int
459 registerrpc(prognum, versnum, procnum, progname, inproc, outproc)
460         int prognum, versnum, procnum;
461         char *(*progname)(char [UDPMSGSIZE]);
462         xdrproc_t inproc, outproc;
463 {
464
465         return rpc_reg((rpcprog_t)prognum, (rpcvers_t)versnum,
466             (rpcproc_t)procnum, progname, inproc, outproc, "udp");
467 }
468
469 /*
470  * All the following clnt_broadcast stuff is convulated; it supports
471  * the earlier calling style of the callback function
472  */
473 extern thread_key_t     clnt_broadcast_key;
474
475 /*
476  * Need to translate the netbuf address into sockaddr_in address.
477  * Dont care about netid here.
478  */
479 /* ARGSUSED */
480 static bool_t
481 rpc_wrap_bcast(resultp, addr, nconf)
482         char *resultp;          /* results of the call */
483         struct netbuf *addr;    /* address of the guy who responded */
484         struct netconfig *nconf; /* Netconf of the transport */
485 {
486         resultproc_t clnt_broadcast_result;
487
488         if (strcmp(nconf->nc_netid, "udp"))
489                 return (FALSE);
490         clnt_broadcast_result = (resultproc_t)thr_getspecific(clnt_broadcast_key);
491         return (*clnt_broadcast_result)(resultp,
492                                 (struct sockaddr_in *)addr->buf);
493 }
494
495 /*
496  * Broadcasts on UDP transport. Obsoleted by rpc_broadcast().
497  */
498 enum clnt_stat
499 clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult)
500         u_long          prog;           /* program number */
501         u_long          vers;           /* version number */
502         u_long          proc;           /* procedure number */
503         xdrproc_t       xargs;          /* xdr routine for args */
504         void           *argsp;          /* pointer to args */
505         xdrproc_t       xresults;       /* xdr routine for results */
506         void           *resultsp;       /* pointer to results */
507         resultproc_t    eachresult;     /* call with each result obtained */
508 {
509         extern mutex_t tsd_lock;
510
511         if (clnt_broadcast_key == KEY_INITIALIZER) {
512                 mutex_lock(&tsd_lock);
513                 if (clnt_broadcast_key == KEY_INITIALIZER)
514                         thr_keycreate(&clnt_broadcast_key, free);
515                 mutex_unlock(&tsd_lock);
516         }
517         thr_setspecific(clnt_broadcast_key, (void *) eachresult);
518         return rpc_broadcast((rpcprog_t)prog, (rpcvers_t)vers,
519             (rpcproc_t)proc, xargs, argsp, xresults, resultsp,
520             (resultproc_t) rpc_wrap_bcast, "udp");
521 }
522
523 /*
524  * Create the client des authentication object. Obsoleted by
525  * authdes_seccreate().
526  */
527 AUTH *
528 authdes_create(servername, window, syncaddr, ckey)
529         char *servername;               /* network name of server */
530         u_int window;                   /* time to live */
531         struct sockaddr *syncaddr;      /* optional hostaddr to sync with */
532         des_block *ckey;                /* optional conversation key to use */
533 {
534         AUTH *dummy;
535         AUTH *nauth;
536         char hostname[NI_MAXHOST];
537
538         if (syncaddr) {
539                 /*
540                  * Change addr to hostname, because that is the way
541                  * new interface takes it.
542                  */
543                 if (getnameinfo(syncaddr, sizeof(syncaddr), hostname,
544                     sizeof hostname, NULL, 0, 0) != 0)
545                         goto fallback;
546
547                 nauth = authdes_seccreate(servername, window, hostname, ckey);
548                 return (nauth);
549         }
550 fallback:
551         dummy = authdes_seccreate(servername, window, NULL, ckey);
552         return (dummy);
553 }
554
555 /*
556  * Create a client handle for a unix connection. Obsoleted by clnt_vc_create()
557  */
558 CLIENT *
559 clntunix_create(raddr, prog, vers, sockp, sendsz, recvsz)
560         struct sockaddr_un *raddr;
561         u_long prog;
562         u_long vers;
563         int *sockp;
564         u_int sendsz;
565         u_int recvsz;
566 {
567         struct netbuf *svcaddr;
568         CLIENT *cl;
569         int len;
570
571         cl = NULL;
572         svcaddr = NULL;
573         if (((svcaddr = malloc(sizeof(struct netbuf))) == NULL ) ||
574             ((svcaddr->buf = malloc(sizeof(struct sockaddr_un))) == NULL)) {
575                 if (svcaddr != NULL)
576                         free(svcaddr);
577                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
578                 rpc_createerr.cf_error.re_errno = errno;
579                 return(cl);
580         }
581         if (*sockp < 0) {
582                 *sockp = socket(AF_LOCAL, SOCK_STREAM, 0);
583                 len = SUN_LEN(raddr);
584                 if ((*sockp < 0) || (connect(*sockp,
585                     (struct sockaddr *)raddr, len) < 0)) {
586                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
587                         rpc_createerr.cf_error.re_errno = errno;
588                         if (*sockp != -1)
589                                 (void)close(*sockp);
590                         goto done;
591                 }
592         }
593         svcaddr->buf = raddr;
594         svcaddr->len = sizeof(raddr);
595         svcaddr->maxlen = sizeof (struct sockaddr_un);
596         cl = clnt_vc_create(*sockp, svcaddr, prog,
597             vers, sendsz, recvsz);
598 done:
599         free(svcaddr->buf);
600         free(svcaddr);
601         return(cl);
602 }
603
604 /*
605  * Creates, registers, and returns a (rpc) unix based transporter.
606  * Obsoleted by svc_vc_create().
607  */
608 SVCXPRT *
609 svcunix_create(sock, sendsize, recvsize, path)
610         int sock;
611         u_int sendsize;
612         u_int recvsize;
613         char *path;
614 {
615         struct netconfig *nconf;
616         void *localhandle;
617         struct sockaddr_un sun;
618         struct sockaddr *sa;
619         struct t_bind taddr;
620         SVCXPRT *xprt;
621         int addrlen;
622
623         xprt = (SVCXPRT *)NULL;
624         localhandle = setnetconfig();
625         while ((nconf = getnetconfig(localhandle)) != NULL) {
626                 if (nconf->nc_protofmly != NULL &&
627                     strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
628                         break;
629         }
630         if (nconf == NULL)
631                 return(xprt);
632
633         if ((sock = __rpc_nconf2fd(nconf)) < 0)
634                 goto done;
635
636         memset(&sun, 0, sizeof sun);
637         sun.sun_family = AF_LOCAL;
638         strncpy(sun.sun_path, path, sizeof(sun.sun_path));
639         addrlen = sizeof(struct sockaddr_un);
640         sa = (struct sockaddr *)&sun;
641
642         if (bind(sock, sa, addrlen) < 0)
643                 goto done;
644
645         taddr.addr.len = taddr.addr.maxlen = addrlen;
646         taddr.addr.buf = malloc(addrlen);
647         if (taddr.addr.buf == NULL)
648                 goto done;
649         memcpy(taddr.addr.buf, sa, addrlen);
650
651         if (nconf->nc_semantics != NC_TPI_CLTS) {
652                 if (listen(sock, SOMAXCONN) < 0) {
653                         free(taddr.addr.buf);
654                         goto done;
655                 }
656         }
657
658         xprt = (SVCXPRT *)svc_tli_create(sock, nconf, &taddr, sendsize, recvsize);
659
660 done:
661         endnetconfig(localhandle);
662         return(xprt);
663 }
664
665 /*
666  * Like svunix_create(), except the routine takes any *open* UNIX file
667  * descriptor as its first input. Obsoleted by svc_fd_create();
668  */
669 SVCXPRT *
670 svcunixfd_create(fd, sendsize, recvsize)
671         int fd;
672         u_int sendsize;
673         u_int recvsize;
674 {
675         return (svc_fd_create(fd, sendsize, recvsize));
676 }
677
678 #endif /* PORTMAP */