* libgssrpc.exports: Don't export internals
[krb5.git] / src / lib / rpc / clnt_udp.c
1 /* @(#)clnt_udp.c       2.2 88/08/01 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  * 
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  * 
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  * 
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  * 
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  * 
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
32 #endif
33
34 /*
35  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  */
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <gssrpc/rpc.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #if defined(sun)
46 #include <sys/filio.h>
47 #endif
48 #include <netdb.h>
49 #include <errno.h>
50 #include <string.h>
51 #include <gssrpc/pmap_clnt.h>
52 #include <errno.h>
53
54
55 /*
56  * UDP bases client side rpc operations
57  */
58 static enum clnt_stat   clntudp_call(CLIENT *, rpcproc_t, xdrproc_t, void *,
59                                      xdrproc_t, void *, struct timeval);
60 static void             clntudp_abort(CLIENT *);
61 static void             clntudp_geterr(CLIENT *, struct rpc_err *);
62 static bool_t           clntudp_freeres(CLIENT *, xdrproc_t, void *);
63 static bool_t           clntudp_control(CLIENT *, int, void *);
64 static void             clntudp_destroy(CLIENT *);
65
66 static struct clnt_ops udp_ops = {
67         clntudp_call,
68         clntudp_abort,
69         clntudp_geterr,
70         clntudp_freeres,
71         clntudp_destroy,
72         clntudp_control
73 };
74
75 /* 
76  * Private data kept per client handle
77  */
78 struct cu_data {
79         int                cu_sock;
80         bool_t             cu_closeit;
81         struct sockaddr_in cu_raddr;
82         int                cu_rlen;
83         struct sockaddr_in cu_laddr;
84         int                cu_llen;
85         struct timeval     cu_wait;
86         struct timeval     cu_total;
87         struct rpc_err     cu_error;
88         XDR                cu_outxdrs;
89         u_int              cu_xdrpos;
90         u_int              cu_sendsz;
91         char               *cu_outbuf;
92         u_int              cu_recvsz;
93         char               cu_inbuf[1];
94 };
95
96 /*
97  * Create a UDP based client handle.
98  * If *sockp<0, *sockp is set to a newly created UPD socket.
99  * If raddr->sin_port is 0 a binder on the remote machine
100  * is consulted for the correct port number.
101  * NB: It is the clients responsibility to close *sockp.
102  * NB: The rpch->cl_auth is initialized to null authentication.
103  *     Caller may wish to set this something more useful.
104  *
105  * wait is the amount of time used between retransmitting a call if
106  * no response has been heard;  retransmition occurs until the actual
107  * rpc call times out.
108  *
109  * sendsz and recvsz are the maximum allowable packet sizes that can be
110  * sent and received.
111  */
112 CLIENT *
113 clntudp_bufcreate(
114         struct sockaddr_in *raddr,
115         rpcprog_t program,
116         rpcvers_t version,
117         struct timeval wait,
118         register int *sockp,
119         u_int sendsz,
120         u_int recvsz)
121 {
122         CLIENT *cl;
123         register struct cu_data *cu = 0;
124         struct timeval now;
125         struct rpc_msg call_msg;
126
127         cl = (CLIENT *)mem_alloc(sizeof(CLIENT));
128         if (cl == NULL) {
129                 (void) fprintf(stderr, "clntudp_create: out of memory\n");
130                 rpc_createerr.cf_stat = RPC_SYSTEMERROR;
131                 rpc_createerr.cf_error.re_errno = errno;
132                 goto fooy;
133         }
134         sendsz = ((sendsz + 3) / 4) * 4;
135         recvsz = ((recvsz + 3) / 4) * 4;
136         cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz);
137         if (cu == NULL) {
138                 (void) fprintf(stderr, "clntudp_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         cu->cu_outbuf = &cu->cu_inbuf[recvsz];
144
145         (void)gettimeofday(&now, (struct timezone *)0);
146         if (raddr->sin_port == 0) {
147                 u_short port;
148                 if ((port =
149                     pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) {
150                         goto fooy;
151                 }
152                 raddr->sin_port = htons(port);
153         }
154         cl->cl_ops = &udp_ops;
155         cl->cl_private = (caddr_t)cu;
156         cu->cu_raddr = *raddr;
157         cu->cu_rlen = sizeof (cu->cu_raddr);
158         cu->cu_wait = wait;
159         cu->cu_total.tv_sec = -1;
160         cu->cu_total.tv_usec = -1;
161         cu->cu_sendsz = sendsz;
162         cu->cu_recvsz = recvsz;
163         call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec;
164         call_msg.rm_direction = CALL;
165         call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
166         call_msg.rm_call.cb_prog = program;
167         call_msg.rm_call.cb_vers = version;
168         xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf,
169             sendsz, XDR_ENCODE);
170         if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) {
171                 goto fooy;
172         }
173         cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
174         if (*sockp < 0) {
175                 int dontblock = 1;
176
177                 *sockp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
178                 if (*sockp < 0) {
179                         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
180                         rpc_createerr.cf_error.re_errno = errno;
181                         goto fooy;
182                 }
183                 /* attempt to bind to prov port */
184                 (void)bindresvport(*sockp, (struct sockaddr_in *)0);
185                 /* the sockets rpc controls are non-blocking */
186                 (void)ioctl(*sockp, FIONBIO, (char *) &dontblock);
187                 cu->cu_closeit = TRUE;
188         } else {
189                 cu->cu_closeit = FALSE;
190         }
191         if (connect(*sockp, (struct sockaddr *)raddr, sizeof(*raddr)) < 0)
192              goto fooy;
193              cu->cu_llen = sizeof(cu->cu_laddr);
194         if (getsockname(*sockp, (struct sockaddr *)&cu->cu_laddr, &cu->cu_llen) < 0)
195              goto fooy;
196         
197         cu->cu_sock = *sockp;
198         cl->cl_auth = authnone_create();
199         return (cl);
200 fooy:
201         if (cu)
202                 mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz);
203         if (cl)
204                 mem_free((caddr_t)cl, sizeof(CLIENT));
205         return ((CLIENT *)NULL);
206 }
207
208 CLIENT *
209 clntudp_create(
210         struct sockaddr_in *raddr,
211         rpcprog_t program,
212         rpcvers_t version,
213         struct timeval wait,
214         register int *sockp)
215 {
216
217         return(clntudp_bufcreate(raddr, program, version, wait, sockp,
218             UDPMSGSIZE, UDPMSGSIZE));
219 }
220
221 static enum clnt_stat 
222 clntudp_call(
223         register CLIENT *cl,            /* client handle */
224         rpcproc_t       proc,           /* procedure number */
225         xdrproc_t       xargs,          /* xdr routine for args */
226         void *          argsp,          /* pointer to args */
227         xdrproc_t       xresults,       /* xdr routine for results */
228         void *          resultsp,       /* pointer to results */
229         struct timeval  utimeout        /* seconds to wait before
230                                          * giving up */
231         )
232 {
233         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
234         register XDR *xdrs;
235         register int outlen;
236         register int inlen;
237         int fromlen;
238 #ifdef FD_SETSIZE
239         fd_set readfds;
240         fd_set mask;
241 #else
242         int readfds;
243         register int mask;
244 #endif /* def FD_SETSIZE */
245         struct sockaddr_in from;
246         struct rpc_msg reply_msg;
247         XDR reply_xdrs;
248         struct timeval time_waited, seltimeout;
249         bool_t ok;
250         int nrefreshes = 2;     /* number of times to refresh cred */
251         struct timeval timeout;
252         long procl = proc;
253
254         if (cu->cu_total.tv_usec == -1) {
255                 timeout = utimeout;     /* use supplied timeout */
256         } else {
257                 timeout = cu->cu_total; /* use default timeout */
258         }
259
260         time_waited.tv_sec = 0;
261         time_waited.tv_usec = 0;
262 call_again:
263         xdrs = &(cu->cu_outxdrs);
264         xdrs->x_op = XDR_ENCODE;
265         XDR_SETPOS(xdrs, cu->cu_xdrpos);
266         /*
267          * the transaction is the first thing in the out buffer
268          */
269         (*(uint32_t *)(void *)(cu->cu_outbuf))++;
270         if ((! XDR_PUTLONG(xdrs, &procl)) ||
271             (! AUTH_MARSHALL(cl->cl_auth, xdrs)) ||
272             (! AUTH_WRAP(cl->cl_auth, xdrs, xargs, argsp)))
273                 return (cu->cu_error.re_status = RPC_CANTENCODEARGS);
274         outlen = (int)XDR_GETPOS(xdrs);
275
276 send_again:
277         if (send(cu->cu_sock, cu->cu_outbuf, (u_int)outlen, 0) != outlen) {
278                 cu->cu_error.re_errno = errno;
279                 return (cu->cu_error.re_status = RPC_CANTSEND);
280         }
281
282         /*
283          * Hack to provide rpc-based message passing
284          */
285         if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
286                 return (cu->cu_error.re_status = RPC_TIMEDOUT);
287         }
288         /*
289          * sub-optimal code appears here because we have
290          * some clock time to spare while the packets are in flight.
291          * (We assume that this is actually only executed once.)
292          */
293         reply_msg.acpted_rply.ar_verf = gssrpc__null_auth;
294         reply_msg.acpted_rply.ar_results.where = NULL;
295         reply_msg.acpted_rply.ar_results.proc = xdr_void;
296 #ifdef FD_SETSIZE
297         FD_ZERO(&mask);
298         FD_SET(cu->cu_sock, &mask);
299 #else
300         mask = 1 << cu->cu_sock;
301 #endif /* def FD_SETSIZE */
302         for (;;) {
303                 readfds = mask;
304                 seltimeout = cu->cu_wait;
305                 switch (select(gssrpc__rpc_dtablesize(), &readfds, (fd_set *)NULL, 
306                                (fd_set *)NULL, &seltimeout)) {
307
308                 case 0:
309                         time_waited.tv_sec += cu->cu_wait.tv_sec;
310                         time_waited.tv_usec += cu->cu_wait.tv_usec;
311                         while (time_waited.tv_usec >= 1000000) {
312                                 time_waited.tv_sec++;
313                                 time_waited.tv_usec -= 1000000;
314                         }
315                         if ((time_waited.tv_sec < timeout.tv_sec) ||
316                                 ((time_waited.tv_sec == timeout.tv_sec) &&
317                                 (time_waited.tv_usec < timeout.tv_usec)))
318                                 goto send_again;        
319                         return (cu->cu_error.re_status = RPC_TIMEDOUT);
320
321                 /*
322                  * buggy in other cases because time_waited is not being
323                  * updated.
324                  */
325                 case -1:
326                         if (errno == EINTR)
327                                 continue;       
328                         cu->cu_error.re_errno = errno;
329                         return (cu->cu_error.re_status = RPC_CANTRECV);
330                 }
331                 do {
332                         fromlen = sizeof(struct sockaddr);
333                         inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, 
334                                 cu->cu_recvsz, 0,
335                                 (struct sockaddr *)&from, &fromlen);
336                 } while (inlen < 0 && errno == EINTR);
337                 if (inlen < 0) {
338                         if (errno == EWOULDBLOCK)
339                                 continue;       
340                         cu->cu_error.re_errno = errno;
341                         return (cu->cu_error.re_status = RPC_CANTRECV);
342                 }
343                 if (inlen < sizeof(uint32_t))
344                         continue;       
345                 /* see if reply transaction id matches sent id */
346                 if (*((uint32_t *)(void *)(cu->cu_inbuf)) != 
347                     *((uint32_t *)(void *)(cu->cu_outbuf)))
348                         continue;       
349                 /* we now assume we have the proper reply */
350                 break;
351         }
352
353         /*
354          * now decode and validate the response
355          */
356         xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE);
357         ok = xdr_replymsg(&reply_xdrs, &reply_msg);
358         /* XDR_DESTROY(&reply_xdrs);  save a few cycles on noop destroy */
359         if (ok) {
360                 gssrpc__seterr_reply(&reply_msg, &(cu->cu_error));
361                 if (cu->cu_error.re_status == RPC_SUCCESS) {
362                         if (! AUTH_VALIDATE(cl->cl_auth,
363                                 &reply_msg.acpted_rply.ar_verf)) {
364                                 cu->cu_error.re_status = RPC_AUTHERROR;
365                                 cu->cu_error.re_why = AUTH_INVALIDRESP;
366                         } else if (! AUTH_UNWRAP(cl->cl_auth, &reply_xdrs,
367                                                  xresults, resultsp)) {
368                              if (cu->cu_error.re_status == RPC_SUCCESS)
369                                   cu->cu_error.re_status = RPC_CANTDECODERES;
370                         }
371                 }  /* end successful completion */
372                 else {
373                         /* maybe our credentials need to be refreshed ... */
374                         if (nrefreshes > 0 &&
375                             AUTH_REFRESH(cl->cl_auth, &reply_msg)) {
376                                 nrefreshes--;
377                                 goto call_again;
378                         }
379                 }  /* end of unsuccessful completion */
380                 /* free verifier */
381                 if ((reply_msg.rm_reply.rp_stat == MSG_ACCEPTED) &&
382                     (reply_msg.acpted_rply.ar_verf.oa_base != NULL)) {
383                     xdrs->x_op = XDR_FREE;
384                     (void)xdr_opaque_auth(xdrs,
385                                           &(reply_msg.acpted_rply.ar_verf));
386                 } 
387         }  /* end of valid reply message */
388         else {
389                 /*
390                  * It's possible for xdr_replymsg() to fail partway
391                  * through its attempt to decode the result from the
392                  * server. If this happens, it will leave the reply
393                  * structure partially populated with dynamically
394                  * allocated memory. (This can happen if someone uses
395                  * clntudp_bufcreate() to create a CLIENT handle and
396                  * specifies a receive buffer size that is too small.)
397                  * This memory must be free()ed to avoid a leak.
398                  */
399                 enum xdr_op op = reply_xdrs.x_op;
400                 reply_xdrs.x_op = XDR_FREE;
401                 xdr_replymsg(&reply_xdrs, &reply_msg);
402                 reply_xdrs.x_op = op;
403                 return (RPC_CANTDECODERES);
404                 cu->cu_error.re_status = RPC_CANTDECODERES;
405         }
406         return (cu->cu_error.re_status);
407 }
408
409 static void
410 clntudp_geterr(
411         CLIENT *cl,
412         struct rpc_err *errp)
413 {
414         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
415
416         *errp = cu->cu_error;
417 }
418
419
420 static bool_t
421 clntudp_freeres(
422         CLIENT *cl,
423         xdrproc_t xdr_res,
424         void *res_ptr)
425 {
426         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
427         register XDR *xdrs = &(cu->cu_outxdrs);
428
429         xdrs->x_op = XDR_FREE;
430         return ((*xdr_res)(xdrs, res_ptr));
431 }
432
433
434 /*ARGSUSED*/
435 static void 
436 clntudp_abort(CLIENT *h)
437 {
438 }
439
440 static bool_t
441 clntudp_control(
442         CLIENT *cl,
443         int request,
444         void *info)
445 {
446         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
447         
448         switch (request) {
449         case CLSET_TIMEOUT:
450                 cu->cu_total = *(struct timeval *)info;
451                 break;
452         case CLGET_TIMEOUT:
453                 *(struct timeval *)info = cu->cu_total;
454                 break;
455         case CLSET_RETRY_TIMEOUT:
456                 cu->cu_wait = *(struct timeval *)info;
457                 break;
458         case CLGET_RETRY_TIMEOUT:
459                 *(struct timeval *)info = cu->cu_wait;
460                 break;
461         case CLGET_SERVER_ADDR:
462                 *(struct sockaddr_in *)info = cu->cu_raddr;
463                 break;
464         case CLGET_LOCAL_ADDR:
465                 *(struct sockaddr_in *)info = cu->cu_laddr;
466                 break;
467         default:
468                 return (FALSE);
469         }
470         return (TRUE);
471 }
472         
473 static void
474 clntudp_destroy(CLIENT *cl)
475 {
476         register struct cu_data *cu = (struct cu_data *)cl->cl_private;
477
478         if (cu->cu_closeit) {
479                 (void)close(cu->cu_sock);
480         }
481         XDR_DESTROY(&(cu->cu_outxdrs));
482         mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz));
483         mem_free((caddr_t)cl, sizeof(CLIENT));
484 }