038169a5fce4399d36f6419bb7fe1dd48bd63e6b
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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
31 * clnt_unix.c, Implements a TCP/IP based, client side RPC.
33 * Copyright (C) 1984, Sun Microsystems, Inc.
35 * TCP based RPC supports 'batched calls'.
36 * A sequence of calls may be batched-up in a send buffer. The rpc call
37 * return immediately to the client even though the call was not necessarily
38 * sent. The batching occurs if the results' xdr routine is NULL (0) AND
39 * the rpc timeout value is zero (see clnt.h, rpc).
41 * Clients should NOT casually batch calls that in fact return results; that is,
42 * the server side should be aware that a call is batched and not produce any
43 * return message. Batched calls that produce many result messages can
44 * deadlock (netlock) the client and the server....
46 * Now go hang yourself.
59 #include <sys/socket.h>
60 #include <rpc/pmap_clnt.h>
66 extern u_long
_create_xid (void) attribute_hidden
;
68 #define MCALL_MSG_SIZE 24
74 struct timeval ct_wait
;
75 bool_t ct_waitset
; /* wait set by clnt_control? */
76 struct sockaddr_un ct_addr
;
77 struct rpc_err ct_error
;
78 char ct_mcall
[MCALL_MSG_SIZE
]; /* marshalled callmsg */
79 u_int ct_mpos
; /* pos after marshal */
83 static int readunix (char *, char *, int);
84 static int writeunix (char *, char *, int);
86 static enum clnt_stat
clntunix_call (CLIENT
*, u_long
, xdrproc_t
, caddr_t
,
87 xdrproc_t
, caddr_t
, struct timeval
);
88 static void clntunix_abort (void);
89 static void clntunix_geterr (CLIENT
*, struct rpc_err
*);
90 static bool_t
clntunix_freeres (CLIENT
*, xdrproc_t
, caddr_t
);
91 static bool_t
clntunix_control (CLIENT
*, int, char *);
92 static void clntunix_destroy (CLIENT
*);
94 static const struct clnt_ops unix_ops
=
105 * Create a client handle for a tcp/ip connection.
106 * If *sockp<0, *sockp is set to a newly created TCP socket and it is
107 * connected to raddr. If *sockp non-negative then
108 * raddr is ignored. The rpc/tcp package does buffering
109 * similar to stdio, so the client must pick send and receive buffer sizes,];
110 * 0 => use the default.
111 * If raddr->sin_port is 0, then a binder on the remote machine is
112 * consulted for the right port number.
113 * NB: *sockp is copied into a private area.
114 * NB: It is the clients responsibility to close *sockp.
115 * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
116 * something more useful.
119 clntunix_create (struct sockaddr_un
*raddr
, u_long prog
, u_long vers
,
120 int *sockp
, u_int sendsz
, u_int recvsz
)
123 struct ct_data
*ct
= (struct ct_data
*) mem_alloc (sizeof (*ct
));
124 struct rpc_msg call_msg
;
127 h
= (CLIENT
*) mem_alloc (sizeof (*h
));
128 if (h
== NULL
|| ct
== NULL
)
130 struct rpc_createerr
*ce
= &get_rpc_createerr ();
132 if (_IO_fwide (stderr
, 0) > 0)
133 (void) fwprintf (stderr
, L
"%s",
134 _("clntunix_create: out of memory\n"));
137 (void) fputs (_("clntunix_create: out of memory\n"), stderr
);
138 ce
->cf_stat
= RPC_SYSTEMERROR
;
139 ce
->cf_error
.re_errno
= ENOMEM
;
144 * If no socket given, open one
148 *sockp
= socket (AF_UNIX
, SOCK_STREAM
, 0);
149 len
= strlen (raddr
->sun_path
) + sizeof (raddr
->sun_family
) + 1;
151 || connect (*sockp
, (struct sockaddr
*) raddr
, len
) < 0)
153 struct rpc_createerr
*ce
= &get_rpc_createerr ();
154 ce
->cf_stat
= RPC_SYSTEMERROR
;
155 ce
->cf_error
.re_errno
= errno
;
160 ct
->ct_closeit
= TRUE
;
164 ct
->ct_closeit
= FALSE
;
168 * Set up private data struct
170 ct
->ct_sock
= *sockp
;
171 ct
->ct_wait
.tv_usec
= 0;
172 ct
->ct_waitset
= FALSE
;
173 ct
->ct_addr
= *raddr
;
176 * Initialize call message
178 call_msg
.rm_xid
= _create_xid ();
179 call_msg
.rm_direction
= CALL
;
180 call_msg
.rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
181 call_msg
.rm_call
.cb_prog
= prog
;
182 call_msg
.rm_call
.cb_vers
= vers
;
185 * pre-serialize the static part of the call msg and stash it away
187 xdrmem_create (&(ct
->ct_xdrs
), ct
->ct_mcall
, MCALL_MSG_SIZE
, XDR_ENCODE
);
188 if (!xdr_callhdr (&(ct
->ct_xdrs
), &call_msg
))
194 ct
->ct_mpos
= XDR_GETPOS (&(ct
->ct_xdrs
));
195 XDR_DESTROY (&(ct
->ct_xdrs
));
198 * Create a client handle which uses xdrrec for serialization
199 * and authnone for authentication.
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 ();
210 * Something goofed, free stuff and barf
212 mem_free ((caddr_t
) ct
, sizeof (struct ct_data
));
213 mem_free ((caddr_t
) h
, sizeof (CLIENT
));
214 return (CLIENT
*) NULL
;
216 libc_hidden_def(clntunix_create
)
218 static enum clnt_stat
219 clntunix_call (CLIENT
*h
, u_long proc
, xdrproc_t xdr_args
, caddr_t args_ptr
,
220 xdrproc_t xdr_results
, caddr_t results_ptr
,
221 struct timeval timeout
)
223 struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
224 XDR
*xdrs
= &(ct
->ct_xdrs
);
225 struct rpc_msg reply_msg
;
227 u_int32_t
*msg_x_id
= (u_int32_t
*) (ct
->ct_mcall
); /* yuk */
233 ct
->ct_wait
= timeout
;
237 (xdr_results
== (xdrproc_t
) 0 && ct
->ct_wait
.tv_sec
== 0
238 && ct
->ct_wait
.tv_usec
== 0) ? FALSE
: TRUE
;
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
)))
249 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
250 ct
->ct_error
.re_status
= RPC_CANTENCODEARGS
;
251 (void) xdrrec_endofrecord (xdrs
, TRUE
);
252 return ct
->ct_error
.re_status
;
254 if (!xdrrec_endofrecord (xdrs
, shipnow
))
255 return ct
->ct_error
.re_status
= RPC_CANTSEND
;
259 * Hack to provide rpc-based message passing
261 if (ct
->ct_wait
.tv_sec
== 0 && ct
->ct_wait
.tv_usec
== 0)
262 return ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
266 * Keep receiving until we get a valid transaction id
268 xdrs
->x_op
= XDR_DECODE
;
271 reply_msg
.acpted_rply
.ar_verf
= _null_auth
;
272 reply_msg
.acpted_rply
.ar_results
.where
= NULL
;
273 reply_msg
.acpted_rply
.ar_results
.proc
= (xdrproc_t
)xdr_void
;
274 if (!xdrrec_skiprecord (xdrs
))
275 return ct
->ct_error
.re_status
;
276 /* now decode and validate the response header */
277 if (!xdr_replymsg (xdrs
, &reply_msg
))
279 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
281 return ct
->ct_error
.re_status
;
283 if (reply_msg
.rm_xid
== x_id
)
290 _seterr_reply (&reply_msg
, &(ct
->ct_error
));
291 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
293 if (!AUTH_VALIDATE (h
->cl_auth
, &reply_msg
.acpted_rply
.ar_verf
))
295 ct
->ct_error
.re_status
= RPC_AUTHERROR
;
296 ct
->ct_error
.re_why
= AUTH_INVALIDRESP
;
298 else if (!(*xdr_results
) (xdrs
, results_ptr
))
300 if (ct
->ct_error
.re_status
== RPC_SUCCESS
)
301 ct
->ct_error
.re_status
= RPC_CANTDECODERES
;
303 /* free verifier ... */
304 if (reply_msg
.acpted_rply
.ar_verf
.oa_base
!= NULL
)
306 xdrs
->x_op
= XDR_FREE
;
307 (void) xdr_opaque_auth (xdrs
, &(reply_msg
.acpted_rply
.ar_verf
));
309 } /* end successful completion */
312 /* maybe our credentials need to be refreshed ... */
313 if (refreshes
-- && AUTH_REFRESH (h
->cl_auth
))
315 } /* end of unsuccessful completion */
316 return ct
->ct_error
.re_status
;
320 clntunix_geterr (CLIENT
*h
, struct rpc_err
*errp
)
322 struct ct_data
*ct
= (struct ct_data
*) h
->cl_private
;
324 *errp
= ct
->ct_error
;
328 clntunix_freeres (CLIENT
*cl
, xdrproc_t xdr_res
, caddr_t res_ptr
)
330 struct ct_data
*ct
= (struct ct_data
*) cl
->cl_private
;
331 XDR
*xdrs
= &(ct
->ct_xdrs
);
333 xdrs
->x_op
= XDR_FREE
;
334 return (*xdr_res
) (xdrs
, res_ptr
);
338 clntunix_abort (void)
343 clntunix_control (CLIENT
*cl
, int request
, char *info
)
345 struct ct_data
*ct
= (struct ct_data
*) cl
->cl_private
;
351 ct
->ct_closeit
= TRUE
;
353 case CLSET_FD_NCLOSE
:
354 ct
->ct_closeit
= FALSE
;
357 ct
->ct_wait
= *(struct timeval
*) info
;
360 *(struct timeval
*) info
= ct
->ct_wait
;
362 case CLGET_SERVER_ADDR
:
363 *(struct sockaddr_un
*) info
= ct
->ct_addr
;
366 *(int *)info
= ct
->ct_sock
;
370 * use the knowledge that xid is the
371 * first element in the call structure *.
372 * This will get the xid of the PREVIOUS call
374 *(u_long
*) info
= ntohl (*(u_long
*)ct
->ct_mcall
);
377 /* This will set the xid of the NEXT call */
378 *(u_long
*) ct
->ct_mcall
= htonl (*(u_long
*)info
- 1);
379 /* decrement by 1 as clntunix_call() increments once */
382 * This RELIES on the information that, in the call body,
383 * the version number field is the fifth field from the
384 * begining of the RPC header. MUST be changed if the
385 * call_struct is changed
387 *(u_long
*) info
= ntohl (*(u_long
*) (ct
->ct_mcall
388 + 4 * BYTES_PER_XDR_UNIT
));
391 *(u_long
*) (ct
->ct_mcall
+ 4 * BYTES_PER_XDR_UNIT
)
392 = htonl (*(u_long
*) info
);
396 * This RELIES on the information that, in the call body,
397 * the program number field is the field from the
398 * begining of the RPC header. MUST be changed if the
399 * call_struct is changed
401 *(u_long
*) info
= ntohl (*(u_long
*) (ct
->ct_mcall
402 + 3 * BYTES_PER_XDR_UNIT
));
405 *(u_long
*) (ct
->ct_mcall
+ 3 * BYTES_PER_XDR_UNIT
)
406 = htonl(*(u_long
*) info
);
408 /* The following are only possible with TI-RPC */
409 case CLGET_RETRY_TIMEOUT
:
410 case CLSET_RETRY_TIMEOUT
:
413 case CLSET_PUSH_TIMOD
:
414 case CLSET_POP_TIMOD
:
423 clntunix_destroy (CLIENT
*h
)
426 (struct ct_data
*) h
->cl_private
;
430 (void) close (ct
->ct_sock
);
432 XDR_DESTROY (&(ct
->ct_xdrs
));
433 mem_free ((caddr_t
) ct
, sizeof (struct ct_data
));
434 mem_free ((caddr_t
) h
, sizeof (CLIENT
));
438 __msgread (int sock
, void *data
, size_t cnt
)
442 #ifdef SCM_CREDENTIALS
443 /*static -why??*/ char cm
[CMSG_SPACE(sizeof (struct ucred
))];
454 #ifdef SCM_CREDENTIALS
455 msg
.msg_control
= (caddr_t
) &cm
;
456 msg
.msg_controllen
= CMSG_SPACE(sizeof (struct ucred
));
463 if (setsockopt (sock
, SOL_SOCKET
, SO_PASSCRED
, &on
, sizeof (on
)))
469 len
= recvmsg (sock
, &msg
, 0);
472 if (msg
.msg_flags
& MSG_CTRUNC
|| len
== 0)
483 __msgwrite (int sock
, void *data
, size_t cnt
)
485 #ifndef SCM_CREDENTIALS
486 /* We cannot implement this reliably. */
487 __set_errno (ENOSYS
);
492 struct cmsghdr
*cmsg
= alloca (CMSG_SPACE(sizeof (struct ucred
)));
496 /* XXX I'm not sure, if gete?id() is always correct, or if we should use
497 get?id(). But since keyserv needs geteuid(), we have no other chance.
498 It would be much better, if the kernel could pass both to the server. */
499 cred
.pid
= getpid ();
500 cred
.uid
= geteuid ();
501 cred
.gid
= getegid ();
503 memcpy (CMSG_DATA(cmsg
), &cred
, sizeof (struct ucred
));
504 cmsg
->cmsg_level
= SOL_SOCKET
;
505 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
506 cmsg
->cmsg_len
= sizeof(*cmsg
) + sizeof(struct ucred
);
515 msg
.msg_control
= cmsg
;
516 msg
.msg_controllen
= CMSG_ALIGN(cmsg
->cmsg_len
);
520 len
= sendmsg (sock
, &msg
, 0);
532 * Interface between xdr serializer and unix connection.
533 * Behaves like the system calls, read & write, but keeps some error state
534 * around for the rpc level.
537 readunix (char *ctptr
, char *buf
, int len
)
539 struct ct_data
*ct
= (struct ct_data
*) ctptr
;
541 int milliseconds
= ((ct
->ct_wait
.tv_sec
* 1000)
542 + (ct
->ct_wait
.tv_usec
/ 1000));
551 switch (poll (&fd
, 1, milliseconds
))
554 ct
->ct_error
.re_status
= RPC_TIMEDOUT
;
560 ct
->ct_error
.re_status
= RPC_CANTRECV
;
561 ct
->ct_error
.re_errno
= errno
;
566 switch (len
= __msgread (ct
->ct_sock
, buf
, len
))
571 ct
->ct_error
.re_errno
= ECONNRESET
;
572 ct
->ct_error
.re_status
= RPC_CANTRECV
;
573 len
= -1; /* it's really an error */
577 ct
->ct_error
.re_errno
= errno
;
578 ct
->ct_error
.re_status
= RPC_CANTRECV
;
585 writeunix (char *ctptr
, char *buf
, int len
)
588 struct ct_data
*ct
= (struct ct_data
*) ctptr
;
590 for (cnt
= len
; cnt
> 0; cnt
-= i
, buf
+= i
)
592 if ((i
= __msgwrite (ct
->ct_sock
, buf
, cnt
)) == -1)
594 ct
->ct_error
.re_errno
= errno
;
595 ct
->ct_error
.re_status
= RPC_CANTSEND
;