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