Merge from vendor branch GCC:
[dragonfly.git] / lib / libc / rpc / clnt_unix.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)clnt_unix.c 1.37 87/10/05 Copyr 1984 Sun Micro
30  * @(#)clnt_unix.c      2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/clnt_unix.c,v 1.5 2000/01/27 23:06:37 jasone Exp $
32  * $DragonFly: src/lib/libc/rpc/clnt_unix.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
33  */
34
35 /*
36  * clnt_unix.c, Implements a AF_UNIX based, client side RPC.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * AF_UNIX based RPC supports 'batched calls'.
41  * A sequence of calls may be batched-up in a send buffer.  The rpc call
42  * return immediately to the client even though the call was not necessarily
43  * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
44  * the rpc timeout value is zero (see clnt.h, rpc).
45  *
46  * Clients should NOT casually batch calls that in fact return results; that is,
47  * the server side should be aware that a call is batched and not produce any
48  * return message.  Batched calls that produce many result messages can
49  * deadlock (netlock) the client and the server....
50  *
51  * Now go hang yourself.
52  */
53
54 #include "namespace.h"
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <string.h>
59 #include <rpc/rpc.h>
60 #include <sys/uio.h>
61 #include <sys/socket.h>
62 #include <sys/un.h>
63 #include <netdb.h>
64 #include <errno.h>
65 #include <rpc/pmap_clnt.h>
66 #include "un-namespace.h"
67
68 #define MCALL_MSG_SIZE 24
69
70 static int      readunix();
71 static int      writeunix();
72
73 static enum clnt_stat   clntunix_call();
74 static void             clntunix_abort();
75 static void             clntunix_geterr();
76 static bool_t           clntunix_freeres();
77 static bool_t           clntunix_control();
78 static void             clntunix_destroy();
79
80 static struct clnt_ops unix_ops = {
81         clntunix_call,
82         clntunix_abort,
83         clntunix_geterr,
84         clntunix_freeres,
85         clntunix_destroy,
86         clntunix_control
87 };
88
89 struct ct_data {
90         int             ct_sock;
91         bool_t          ct_closeit;
92         struct timeval  ct_wait;
93         bool_t          ct_waitset;       /* wait set by clnt_control? */
94         struct sockaddr_un ct_addr;
95         struct rpc_err  ct_error;
96         char            ct_mcall[MCALL_MSG_SIZE];       /* marshalled callmsg */
97         u_int           ct_mpos;                        /* pos after marshal */
98         XDR             ct_xdrs;
99 };
100
101 /*
102  * Create a client handle for a unix/ip connection.
103  * If *sockp<0, *sockp is set to a newly created TCP socket and it is
104  * connected to raddr.  If *sockp non-negative then
105  * raddr is ignored.  The rpc/unix package does buffering
106  * similar to stdio, so the client must pick send and receive buffer sizes,];
107  * 0 => use the default.
108  * If raddr->sin_port is 0, then a binder on the remote machine is
109  * consulted for the right port number.
110  * NB: *sockp is copied into a private area.
111  * NB: It is the clients responsibility to close *sockp.
112  * NB: The rpch->cl_auth is set null authentication.  Caller may wish to set this
113  * something more useful.
114  */
115 CLIENT *
116 clntunix_create(struct sockaddr_un *raddr, u_long prog, u_long vers, int *sockp,
117                 u_int sendsz, u_int recvsz)
118 {
119         CLIENT *h;
120         struct ct_data *ct = NULL;
121         struct timeval now;
122         struct rpc_msg call_msg;
123         static u_int32_t disrupt;
124         int len;
125
126         if (disrupt == 0)
127                 disrupt = (u_int32_t)(long)raddr;
128
129         h  = (CLIENT *)mem_alloc(sizeof(*h));
130         if (h == NULL) {
131                 fprintf(stderr, "clntunix_create: out of memory\n");
132                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
133                 rpc_createerr.cf_error.re_errno = errno;
134                 goto fooy;
135         }
136         ct = (struct ct_data *)mem_alloc(sizeof(*ct));
137         if (ct == NULL) {
138                 fprintf(stderr, "clntunix_create: out of memory\n");
139                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
140                 rpc_createerr.cf_error.re_errno = errno;
141                 goto fooy;
142         }
143
144         /*
145          * If no socket given, open one
146          */
147         if (*sockp < 0) {
148                 *sockp = _socket(AF_UNIX, SOCK_STREAM, 0);
149                 len = strlen(raddr->sun_path) + sizeof(raddr->sun_family) +
150                         sizeof(raddr->sun_len) + 1;
151                 raddr->sun_len = len;
152                 if ((*sockp < 0)
153                     || (_connect(*sockp, (struct sockaddr *)raddr, len) < 0)) {
154                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
155                         rpc_createerr.cf_error.re_errno = errno;
156                         if (*sockp != -1)
157                                 _close(*sockp);
158                         goto fooy;
159                 }
160                 ct->ct_closeit = TRUE;
161         } else {
162                 ct->ct_closeit = FALSE;
163         }
164
165         /*
166          * Set up private data struct
167          */
168         ct->ct_sock = *sockp;
169         ct->ct_wait.tv_usec = 0;
170         ct->ct_waitset = FALSE;
171         ct->ct_addr = *raddr;
172
173         /*
174          * Initialize call message
175          */
176         gettimeofday(&now, (struct timezone *)0);
177         call_msg.rm_xid = (++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec;
178         call_msg.rm_direction = CALL;
179         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
180         call_msg.rm_call.cb_prog = prog;
181         call_msg.rm_call.cb_vers = vers;
182
183         /*
184          * pre-serialize the static part of the call msg and stash it away
185          */
186         xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE,
187             XDR_ENCODE);
188         if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) {
189                 if (ct->ct_closeit) {
190                         _close(*sockp);
191                 }
192                 goto fooy;
193         }
194         ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs));
195         XDR_DESTROY(&(ct->ct_xdrs));
196
197         /*
198          * Create a client handle which uses xdrrec for serialization
199          * and authnone for authentication.
200          */
201         xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz,
202             (caddr_t)ct, readunix, writeunix);
203         h->cl_ops = &unix_ops;
204         h->cl_private = (caddr_t) ct;
205         h->cl_auth = authnone_create();
206         return (h);
207
208 fooy:
209         /*
210          * Something goofed, free stuff and barf
211          */
212         if (ct)
213                 mem_free((caddr_t)ct, sizeof(struct ct_data));
214         if (h)
215                 mem_free((caddr_t)h, sizeof(CLIENT));
216         return ((CLIENT *)NULL);
217 }
218
219 static enum clnt_stat
220 clntunix_call(CLIENT *h, u_long proc, xdrproc_t xdr_args, caddr_t args_ptr,
221               xdrproc_t xdr_results, caddr_t results_ptr,
222               struct timeval timeout)
223 {
224         struct ct_data *ct = (struct ct_data *) h->cl_private;
225         XDR *xdrs = &(ct->ct_xdrs);
226         struct rpc_msg reply_msg;
227         u_long x_id;
228         u_int32_t *msg_x_id = (u_int32_t *)(ct->ct_mcall);      /* yuk */
229         bool_t shipnow;
230         int refreshes = 2;
231
232         if (!ct->ct_waitset) {
233                 ct->ct_wait = timeout;
234         }
235
236         shipnow =
237             (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0
238             && timeout.tv_usec == 0) ? FALSE : TRUE;
239
240 call_again:
241         xdrs->x_op = XDR_ENCODE;
242         ct->ct_error.re_status = RPC_SUCCESS;
243         x_id = ntohl(--(*msg_x_id));
244         if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) ||
245             (! XDR_PUTLONG(xdrs, (long *)&proc)) ||
246             (! AUTH_MARSHALL(h->cl_auth, xdrs)) ||
247             (! (*xdr_args)(xdrs, args_ptr))) {
248                 if (ct->ct_error.re_status == RPC_SUCCESS)
249                         ct->ct_error.re_status = RPC_CANTENCODEARGS;
250                 xdrrec_endofrecord(xdrs, TRUE);
251                 return (ct->ct_error.re_status);
252         }
253         if (! xdrrec_endofrecord(xdrs, shipnow))
254                 return (ct->ct_error.re_status = RPC_CANTSEND);
255         if (! shipnow)
256                 return (RPC_SUCCESS);
257         /*
258          * Hack to provide rpc-based message passing
259          */
260         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
261                 return(ct->ct_error.re_status = RPC_TIMEDOUT);
262         }
263
264
265         /*
266          * Keep receiving until we get a valid transaction id
267          */
268         xdrs->x_op = XDR_DECODE;
269         while (TRUE) {
270                 reply_msg.acpted_rply.ar_verf = _null_auth;
271                 reply_msg.acpted_rply.ar_results.where = NULL;
272                 reply_msg.acpted_rply.ar_results.proc = xdr_void;
273                 if (! xdrrec_skiprecord(xdrs))
274                         return (ct->ct_error.re_status);
275                 /* now decode and validate the response header */
276                 if (! xdr_replymsg(xdrs, &reply_msg)) {
277                         if (ct->ct_error.re_status == RPC_SUCCESS)
278                                 continue;
279                         return (ct->ct_error.re_status);
280                 }
281                 if (reply_msg.rm_xid == x_id)
282                         break;
283         }
284
285         /*
286          * process header
287          */
288         _seterr_reply(&reply_msg, &(ct->ct_error));
289         if (ct->ct_error.re_status == RPC_SUCCESS) {
290                 if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
291                         ct->ct_error.re_status = RPC_AUTHERROR;
292                         ct->ct_error.re_why = AUTH_INVALIDRESP;
293                 } else if (! (*xdr_results)(xdrs, results_ptr)) {
294                         if (ct->ct_error.re_status == RPC_SUCCESS)
295                                 ct->ct_error.re_status = RPC_CANTDECODERES;
296                 }
297                 /* free verifier ... */
298                 if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
299                         xdrs->x_op = XDR_FREE;
300                         xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
301                 }
302         }  /* end successful completion */
303         else {
304                 /* maybe our credentials need to be refreshed ... */
305                 if (refreshes-- && AUTH_REFRESH(h->cl_auth))
306                         goto call_again;
307         }  /* end of unsuccessful completion */
308         return (ct->ct_error.re_status);
309 }
310
311 static void
312 clntunix_geterr(CLIENT *h, struct rpc_err *errp)
313 {
314         struct ct_data *ct =
315             (struct ct_data *) h->cl_private;
316
317         *errp = ct->ct_error;
318 }
319
320 static bool_t
321 clntunix_freeres(CLIENT *cl, xdrproc_t xdr_res, caddr_t res_ptr)
322 {
323         struct ct_data *ct = (struct ct_data *)cl->cl_private;
324         XDR *xdrs = &(ct->ct_xdrs);
325
326         xdrs->x_op = XDR_FREE;
327         return ((*xdr_res)(xdrs, res_ptr));
328 }
329
330 static void
331 clntunix_abort(void)
332 {
333 }
334
335
336 static bool_t
337 clntunix_control(CLIENT *cl, int request, char *info)
338 {
339         struct ct_data *ct = (struct ct_data *)cl->cl_private;
340         struct timeval *tv;
341         int len;
342
343         switch (request) {
344         case CLSET_FD_CLOSE:
345                 ct->ct_closeit = TRUE;
346                 break;
347         case CLSET_FD_NCLOSE:
348                 ct->ct_closeit = FALSE;
349                 break;
350         case CLSET_TIMEOUT:
351                 if (info == NULL)
352                         return(FALSE);
353                 tv = (struct timeval *)info;
354                 ct->ct_wait.tv_sec = tv->tv_sec;
355                 ct->ct_wait.tv_usec = tv->tv_usec;
356                 ct->ct_waitset = TRUE;
357                 break;
358         case CLGET_TIMEOUT:
359                 if (info == NULL)
360                         return(FALSE);
361                 *(struct timeval *)info = ct->ct_wait;
362                 break;
363         case CLGET_SERVER_ADDR:
364                 if (info == NULL)
365                         return(FALSE);
366                 *(struct sockaddr_un *)info = ct->ct_addr;
367                 break;
368         case CLGET_FD:
369                 if (info == NULL)
370                         return(FALSE);
371                 *(int *)info = ct->ct_sock;
372                 break;
373         case CLGET_XID:
374                 /*
375                  * use the knowledge that xid is the
376                  * first element in the call structure *.
377                  * This will get the xid of the PREVIOUS call
378                  */
379                 if (info == NULL)
380                         return(FALSE);
381                 *(u_long *)info = ntohl(*(u_long *)ct->ct_mcall);
382                 break;
383         case CLSET_XID:
384                 /* This will set the xid of the NEXT call */
385                 if (info == NULL)
386                         return(FALSE);
387                 *(u_long *)ct->ct_mcall =  htonl(*(u_long *)info - 1);
388                 /* decrement by 1 as clntunix_call() increments once */
389         case CLGET_VERS:
390                 /*
391                  * This RELIES on the information that, in the call body,
392                  * the version number field is the fifth field from the
393                  * begining of the RPC header. MUST be changed if the
394                  * call_struct is changed
395                  */
396                 if (info == NULL)
397                         return(FALSE);
398                 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
399                                                 4 * BYTES_PER_XDR_UNIT));
400                 break;
401         case CLSET_VERS:
402                 if (info == NULL)
403                         return(FALSE);
404                 *(u_long *)(ct->ct_mcall + 4 * BYTES_PER_XDR_UNIT)
405                                 = htonl(*(u_long *)info);
406                 break;
407         case CLGET_PROG:
408                 /*
409                  * This RELIES on the information that, in the call body,
410                  * the program number field is the  field from the
411                  * begining of the RPC header. MUST be changed if the
412                  * call_struct is changed
413                  */
414                 if (info == NULL)
415                         return(FALSE);
416                 *(u_long *)info = ntohl(*(u_long *)(ct->ct_mcall +
417                                                 3 * BYTES_PER_XDR_UNIT));
418                 break;
419         case CLSET_PROG:
420                 if (info == NULL)
421                         return(FALSE);
422                 *(u_long *)(ct->ct_mcall + 3 * BYTES_PER_XDR_UNIT)
423                                 = htonl(*(u_long *)info);
424                 break;
425         case CLGET_LOCAL_ADDR:
426                 len = sizeof(struct sockaddr);
427                 if (_getsockname(ct->ct_sock, (struct sockaddr *)info, &len) <0)
428                         return(FALSE);
429                 break;
430         case CLGET_RETRY_TIMEOUT:
431         case CLSET_RETRY_TIMEOUT:
432         case CLGET_SVC_ADDR:
433         case CLSET_SVC_ADDR:
434         case CLSET_PUSH_TIMOD:
435         case CLSET_POP_TIMOD:
436         default:
437                 return (FALSE);
438         }
439         return (TRUE);
440 }
441
442
443 static void
444 clntunix_destroy(CLIENT *h)
445 {
446         struct ct_data *ct =
447             (struct ct_data *) h->cl_private;
448
449         if (ct->ct_closeit) {
450                 _close(ct->ct_sock);
451         }
452         XDR_DESTROY(&(ct->ct_xdrs));
453         mem_free((caddr_t)ct, sizeof(struct ct_data));
454         mem_free((caddr_t)h, sizeof(CLIENT));
455 }
456
457 /*
458  * _read() and _write() are replaced with _recvmsg()/_sendmsg() so that
459  * we can pass ancillary control data. In this case, the data constists
460  * of credential information which the kernel will fill in for us.
461  * XXX: This code is specific to FreeBSD and will not work on other
462  * platforms without the requisite kernel modifications.
463  */
464 struct cmessage {
465         struct cmsghdr cmsg;
466         struct cmsgcred cmcred;
467 };
468
469 static int
470 __msgread(int sock, void *buf, size_t cnt)
471 {
472         struct iovec iov[1];
473         struct msghdr msg;
474         struct cmessage cm;
475
476         bzero((char *)&cm, sizeof(cm));
477         iov[0].iov_base = buf;
478         iov[0].iov_len = cnt;
479
480         msg.msg_iov = iov;
481         msg.msg_iovlen = 1;
482         msg.msg_name = NULL;
483         msg.msg_namelen = 0;
484         msg.msg_control = (caddr_t)&cm;
485         msg.msg_controllen = sizeof(struct cmessage);
486         msg.msg_flags = 0;
487
488         return(_recvmsg(sock, &msg, 0));
489 }
490
491 static int
492 __msgwrite(int sock, void *buf, size_t cnt)
493 {
494         struct iovec iov[1];
495         struct msghdr msg;
496         struct cmessage cm;
497
498         bzero((char *)&cm, sizeof(cm));
499         iov[0].iov_base = buf;
500         iov[0].iov_len = cnt;
501
502         cm.cmsg.cmsg_type = SCM_CREDS;
503         cm.cmsg.cmsg_level = SOL_SOCKET;
504         cm.cmsg.cmsg_len = sizeof(struct cmessage);
505
506         msg.msg_iov = iov;
507         msg.msg_iovlen = 1;
508         msg.msg_name = NULL;
509         msg.msg_namelen = 0;
510         msg.msg_control = (caddr_t)&cm;
511         msg.msg_controllen = sizeof(struct cmessage);
512         msg.msg_flags = 0;
513
514         return(_sendmsg(sock, &msg, 0));
515 }
516
517 /*
518  * Interface between xdr serializer and unix connection.
519  * Behaves like the system calls, read & write, but keeps some error state
520  * around for the rpc level.
521  */
522 static int
523 readunix(struct ct_data *ct, caddr_t buf, int len)
524 {
525         fd_set *fds, readfds;
526         struct timeval start, after, duration, delta, tmp, tv;
527         int r, save_errno;
528
529         if (len == 0)
530                 return (0);
531
532         if (ct->ct_sock + 1 > FD_SETSIZE) {
533                 int bytes = howmany(ct->ct_sock + 1, NFDBITS) * sizeof(fd_mask);
534                 fds = (fd_set *)malloc(bytes);
535                 if (fds == NULL)
536                         return (-1);
537                 memset(fds, 0, bytes);
538         } else {
539                 fds = &readfds;
540                 FD_ZERO(fds);
541         }
542
543         gettimeofday(&start, NULL);
544         delta = ct->ct_wait;
545         while (TRUE) {
546                 /* XXX we know the other bits are still clear */
547                 FD_SET(ct->ct_sock, fds);
548                 tv = delta;     /* in case select writes back */
549                 r = _select(ct->ct_sock+1, fds, NULL, NULL, &tv);
550                 save_errno = errno;
551
552                 gettimeofday(&after, NULL);
553                 timersub(&start, &after, &duration);
554                 timersub(&delta, &duration, &tmp);
555                 delta = tmp;
556                 if (delta.tv_sec < 0 || !timerisset(&delta))
557                         r = 0;
558
559                 switch (r) {
560                 case 0:
561                         if (fds != &readfds)
562                                 free(fds);
563                         ct->ct_error.re_status = RPC_TIMEDOUT;
564                         return (-1);
565
566                 case -1:
567                         if (errno == EINTR)
568                                 continue;
569                         if (fds != &readfds)
570                                 free(fds);
571                         ct->ct_error.re_status = RPC_CANTRECV;
572                         ct->ct_error.re_errno = save_errno;
573                         return (-1);
574                 }
575                 break;
576         }
577         switch (len = __msgread(ct->ct_sock, buf, len)) {
578
579         case 0:
580                 /* premature eof */
581                 ct->ct_error.re_errno = ECONNRESET;
582                 ct->ct_error.re_status = RPC_CANTRECV;
583                 len = -1;  /* it's really an error */
584                 break;
585
586         case -1:
587                 ct->ct_error.re_errno = errno;
588                 ct->ct_error.re_status = RPC_CANTRECV;
589                 break;
590         }
591         return (len);
592 }
593
594 static int
595 writeunix(struct ct_data *ct, caddr_t buf, int len)
596 {
597         int i, cnt;
598
599         for (cnt = len; cnt > 0; cnt -= i, buf += i) {
600                 if ((i = __msgwrite(ct->ct_sock, buf, cnt)) == -1) {
601                         ct->ct_error.re_errno = errno;
602                         ct->ct_error.re_status = RPC_CANTSEND;
603                         return (-1);
604                 }
605         }
606         return (len);
607 }