1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* $NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $ */ |
---|
4 | |
---|
5 | /*- |
---|
6 | * Copyright (c) 2009, Sun Microsystems, Inc. |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * - Redistributions of source code must retain the above copyright notice, |
---|
12 | * this list of conditions and the following disclaimer. |
---|
13 | * - Redistributions in binary form must reproduce the above copyright notice, |
---|
14 | * this list of conditions and the following disclaimer in the documentation |
---|
15 | * and/or other materials provided with the distribution. |
---|
16 | * - Neither the name of Sun Microsystems, Inc. nor the names of its |
---|
17 | * contributors may be used to endorse or promote products derived |
---|
18 | * from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
30 | * POSSIBILITY OF SUCH DAMAGE. |
---|
31 | */ |
---|
32 | |
---|
33 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
34 | static char *sccsid2 = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; |
---|
35 | static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC"; |
---|
36 | static char sccsid3[] = "@(#)clnt_vc.c 1.19 89/03/16 Copyr 1988 Sun Micro"; |
---|
37 | #endif |
---|
38 | #include <sys/cdefs.h> |
---|
39 | __FBSDID("$FreeBSD$"); |
---|
40 | |
---|
41 | /* |
---|
42 | * clnt_tcp.c, Implements a TCP/IP based, client side RPC. |
---|
43 | * |
---|
44 | * Copyright (C) 1984, Sun Microsystems, Inc. |
---|
45 | * |
---|
46 | * TCP based RPC supports 'batched calls'. |
---|
47 | * A sequence of calls may be batched-up in a send buffer. The rpc call |
---|
48 | * return immediately to the client even though the call was not necessarily |
---|
49 | * sent. The batching occurs if the results' xdr routine is NULL (0) AND |
---|
50 | * the rpc timeout value is zero (see clnt.h, rpc). |
---|
51 | * |
---|
52 | * Clients should NOT casually batch calls that in fact return results; that is, |
---|
53 | * the server side should be aware that a call is batched and not produce any |
---|
54 | * return message. Batched calls that produce many result messages can |
---|
55 | * deadlock (netlock) the client and the server.... |
---|
56 | * |
---|
57 | * Now go hang yourself. |
---|
58 | */ |
---|
59 | |
---|
60 | #include "namespace.h" |
---|
61 | #include "reentrant.h" |
---|
62 | #include <sys/types.h> |
---|
63 | #include <sys/poll.h> |
---|
64 | #include <sys/syslog.h> |
---|
65 | #include <sys/socket.h> |
---|
66 | #include <sys/un.h> |
---|
67 | #include <sys/uio.h> |
---|
68 | |
---|
69 | #include <arpa/inet.h> |
---|
70 | #include <assert.h> |
---|
71 | #include <err.h> |
---|
72 | #include <errno.h> |
---|
73 | #include <netdb.h> |
---|
74 | #include <stdio.h> |
---|
75 | #include <stdlib.h> |
---|
76 | #include <string.h> |
---|
77 | #include <unistd.h> |
---|
78 | #include <signal.h> |
---|
79 | |
---|
80 | #include <rpc/rpc.h> |
---|
81 | #include <rpc/rpcsec_gss.h> |
---|
82 | #include "un-namespace.h" |
---|
83 | #include "rpc_com.h" |
---|
84 | #include "mt_misc.h" |
---|
85 | |
---|
86 | #define MCALL_MSG_SIZE 24 |
---|
87 | |
---|
88 | struct cmessage { |
---|
89 | struct cmsghdr cmsg; |
---|
90 | struct cmsgcred cmcred; |
---|
91 | }; |
---|
92 | |
---|
93 | static enum clnt_stat clnt_vc_call(CLIENT *, rpcproc_t, xdrproc_t, void *, |
---|
94 | xdrproc_t, void *, struct timeval); |
---|
95 | static void clnt_vc_geterr(CLIENT *, struct rpc_err *); |
---|
96 | static bool_t clnt_vc_freeres(CLIENT *, xdrproc_t, void *); |
---|
97 | static void clnt_vc_abort(CLIENT *); |
---|
98 | static bool_t clnt_vc_control(CLIENT *, u_int, void *); |
---|
99 | static void clnt_vc_destroy(CLIENT *); |
---|
100 | static struct clnt_ops *clnt_vc_ops(void); |
---|
101 | static bool_t time_not_ok(struct timeval *); |
---|
102 | static int read_vc(void *, void *, int); |
---|
103 | static int write_vc(void *, void *, int); |
---|
104 | static int __msgwrite(int, void *, size_t); |
---|
105 | static int __msgread(int, void *, size_t); |
---|
106 | |
---|
107 | struct ct_data { |
---|
108 | int ct_fd; /* connection's fd */ |
---|
109 | bool_t ct_closeit; /* close it on destroy */ |
---|
110 | struct timeval ct_wait; /* wait interval in milliseconds */ |
---|
111 | bool_t ct_waitset; /* wait set by clnt_control? */ |
---|
112 | struct netbuf ct_addr; /* remote addr */ |
---|
113 | struct rpc_err ct_error; |
---|
114 | union { |
---|
115 | char ct_mcallc[MCALL_MSG_SIZE]; /* marshalled callmsg */ |
---|
116 | u_int32_t ct_mcalli; |
---|
117 | } ct_u; |
---|
118 | u_int ct_mpos; /* pos after marshal */ |
---|
119 | XDR ct_xdrs; /* XDR stream */ |
---|
120 | }; |
---|
121 | |
---|
122 | /* |
---|
123 | * This machinery implements per-fd locks for MT-safety. It is not |
---|
124 | * sufficient to do per-CLIENT handle locks for MT-safety because a |
---|
125 | * user may create more than one CLIENT handle with the same fd behind |
---|
126 | * it. Therfore, we allocate an array of flags (vc_fd_locks), protected |
---|
127 | * by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables |
---|
128 | * similarly protected. Vc_fd_lock[fd] == 1 => a call is activte on some |
---|
129 | * CLIENT handle created for that fd. |
---|
130 | * The current implementation holds locks across the entire RPC and reply. |
---|
131 | * Yes, this is silly, and as soon as this code is proven to work, this |
---|
132 | * should be the first thing fixed. One step at a time. |
---|
133 | */ |
---|
134 | static int *vc_fd_locks; |
---|
135 | static cond_t *vc_cv; |
---|
136 | #define release_fd_lock(fd, mask) { \ |
---|
137 | mutex_lock(&clnt_fd_lock); \ |
---|
138 | vc_fd_locks[fd] = 0; \ |
---|
139 | mutex_unlock(&clnt_fd_lock); \ |
---|
140 | thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL); \ |
---|
141 | cond_signal(&vc_cv[fd]); \ |
---|
142 | } |
---|
143 | |
---|
144 | static const char clnt_vc_errstr[] = "%s : %s"; |
---|
145 | static const char clnt_vc_str[] = "clnt_vc_create"; |
---|
146 | static const char clnt_read_vc_str[] = "read_vc"; |
---|
147 | static const char __no_mem_str[] = "out of memory"; |
---|
148 | |
---|
149 | /* |
---|
150 | * Create a client handle for a connection. |
---|
151 | * Default options are set, which the user can change using clnt_control()'s. |
---|
152 | * The rpc/vc package does buffering similar to stdio, so the client |
---|
153 | * must pick send and receive buffer sizes, 0 => use the default. |
---|
154 | * NB: fd is copied into a private area. |
---|
155 | * NB: The rpch->cl_auth is set null authentication. Caller may wish to |
---|
156 | * set this something more useful. |
---|
157 | * |
---|
158 | * fd should be an open socket |
---|
159 | */ |
---|
160 | CLIENT * |
---|
161 | clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz) |
---|
162 | int fd; /* open file descriptor */ |
---|
163 | const struct netbuf *raddr; /* servers address */ |
---|
164 | const rpcprog_t prog; /* program number */ |
---|
165 | const rpcvers_t vers; /* version number */ |
---|
166 | u_int sendsz; /* buffer recv size */ |
---|
167 | u_int recvsz; /* buffer send size */ |
---|
168 | { |
---|
169 | CLIENT *cl; /* client handle */ |
---|
170 | struct ct_data *ct = NULL; /* client handle */ |
---|
171 | struct timeval now; |
---|
172 | struct rpc_msg call_msg; |
---|
173 | static u_int32_t disrupt; |
---|
174 | sigset_t mask; |
---|
175 | sigset_t newmask; |
---|
176 | struct sockaddr_storage ss; |
---|
177 | socklen_t slen; |
---|
178 | struct __rpc_sockinfo si; |
---|
179 | |
---|
180 | if (disrupt == 0) |
---|
181 | disrupt = (u_int32_t)(long)raddr; |
---|
182 | |
---|
183 | cl = (CLIENT *)mem_alloc(sizeof (*cl)); |
---|
184 | ct = (struct ct_data *)mem_alloc(sizeof (*ct)); |
---|
185 | if ((cl == (CLIENT *)NULL) || (ct == (struct ct_data *)NULL)) { |
---|
186 | (void) syslog(LOG_ERR, clnt_vc_errstr, |
---|
187 | clnt_vc_str, __no_mem_str); |
---|
188 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
189 | rpc_createerr.cf_error.re_errno = errno; |
---|
190 | goto err; |
---|
191 | } |
---|
192 | ct->ct_addr.buf = NULL; |
---|
193 | sigfillset(&newmask); |
---|
194 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
195 | mutex_lock(&clnt_fd_lock); |
---|
196 | if (vc_fd_locks == (int *) NULL) { |
---|
197 | int cv_allocsz, fd_allocsz; |
---|
198 | int dtbsize = __rpc_dtbsize(); |
---|
199 | |
---|
200 | fd_allocsz = dtbsize * sizeof (int); |
---|
201 | vc_fd_locks = (int *) mem_alloc(fd_allocsz); |
---|
202 | if (vc_fd_locks == (int *) NULL) { |
---|
203 | mutex_unlock(&clnt_fd_lock); |
---|
204 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
205 | goto err; |
---|
206 | } else |
---|
207 | memset(vc_fd_locks, '\0', fd_allocsz); |
---|
208 | |
---|
209 | assert(vc_cv == (cond_t *) NULL); |
---|
210 | cv_allocsz = dtbsize * sizeof (cond_t); |
---|
211 | vc_cv = (cond_t *) mem_alloc(cv_allocsz); |
---|
212 | if (vc_cv == (cond_t *) NULL) { |
---|
213 | mem_free(vc_fd_locks, fd_allocsz); |
---|
214 | vc_fd_locks = (int *) NULL; |
---|
215 | mutex_unlock(&clnt_fd_lock); |
---|
216 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
217 | goto err; |
---|
218 | } else { |
---|
219 | int i; |
---|
220 | |
---|
221 | for (i = 0; i < dtbsize; i++) |
---|
222 | cond_init(&vc_cv[i], 0, (void *) 0); |
---|
223 | } |
---|
224 | } else |
---|
225 | assert(vc_cv != (cond_t *) NULL); |
---|
226 | |
---|
227 | /* |
---|
228 | * XXX - fvdl connecting while holding a mutex? |
---|
229 | */ |
---|
230 | slen = sizeof ss; |
---|
231 | if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) { |
---|
232 | if (errno != ENOTCONN) { |
---|
233 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
234 | rpc_createerr.cf_error.re_errno = errno; |
---|
235 | mutex_unlock(&clnt_fd_lock); |
---|
236 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
237 | goto err; |
---|
238 | } |
---|
239 | if (_connect(fd, (struct sockaddr *)raddr->buf, raddr->len) < 0){ |
---|
240 | rpc_createerr.cf_stat = RPC_SYSTEMERROR; |
---|
241 | rpc_createerr.cf_error.re_errno = errno; |
---|
242 | mutex_unlock(&clnt_fd_lock); |
---|
243 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
244 | goto err; |
---|
245 | } |
---|
246 | } |
---|
247 | mutex_unlock(&clnt_fd_lock); |
---|
248 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
249 | if (!__rpc_fd2sockinfo(fd, &si)) |
---|
250 | goto err; |
---|
251 | |
---|
252 | ct->ct_closeit = FALSE; |
---|
253 | |
---|
254 | /* |
---|
255 | * Set up private data struct |
---|
256 | */ |
---|
257 | ct->ct_fd = fd; |
---|
258 | ct->ct_wait.tv_usec = 0; |
---|
259 | ct->ct_waitset = FALSE; |
---|
260 | ct->ct_addr.buf = malloc(raddr->maxlen); |
---|
261 | if (ct->ct_addr.buf == NULL) |
---|
262 | goto err; |
---|
263 | memcpy(ct->ct_addr.buf, raddr->buf, raddr->len); |
---|
264 | ct->ct_addr.len = raddr->maxlen; |
---|
265 | ct->ct_addr.maxlen = raddr->maxlen; |
---|
266 | |
---|
267 | /* |
---|
268 | * Initialize call message |
---|
269 | */ |
---|
270 | (void)gettimeofday(&now, NULL); |
---|
271 | call_msg.rm_xid = ((u_int32_t)++disrupt) ^ __RPC_GETXID(&now); |
---|
272 | call_msg.rm_direction = CALL; |
---|
273 | call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; |
---|
274 | call_msg.rm_call.cb_prog = (u_int32_t)prog; |
---|
275 | call_msg.rm_call.cb_vers = (u_int32_t)vers; |
---|
276 | |
---|
277 | /* |
---|
278 | * pre-serialize the static part of the call msg and stash it away |
---|
279 | */ |
---|
280 | xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE, |
---|
281 | XDR_ENCODE); |
---|
282 | if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { |
---|
283 | if (ct->ct_closeit) { |
---|
284 | (void)_close(fd); |
---|
285 | } |
---|
286 | goto err; |
---|
287 | } |
---|
288 | ct->ct_mpos = XDR_GETPOS(&(ct->ct_xdrs)); |
---|
289 | XDR_DESTROY(&(ct->ct_xdrs)); |
---|
290 | assert(ct->ct_mpos + sizeof(uint32_t) <= MCALL_MSG_SIZE); |
---|
291 | |
---|
292 | /* |
---|
293 | * Create a client handle which uses xdrrec for serialization |
---|
294 | * and authnone for authentication. |
---|
295 | */ |
---|
296 | cl->cl_ops = clnt_vc_ops(); |
---|
297 | cl->cl_private = ct; |
---|
298 | cl->cl_auth = authnone_create(); |
---|
299 | sendsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsz); |
---|
300 | recvsz = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsz); |
---|
301 | xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, |
---|
302 | cl->cl_private, read_vc, write_vc); |
---|
303 | return (cl); |
---|
304 | |
---|
305 | err: |
---|
306 | if (ct) { |
---|
307 | if (ct->ct_addr.len) |
---|
308 | mem_free(ct->ct_addr.buf, ct->ct_addr.len); |
---|
309 | mem_free(ct, sizeof (struct ct_data)); |
---|
310 | } |
---|
311 | if (cl) |
---|
312 | mem_free(cl, sizeof (CLIENT)); |
---|
313 | return ((CLIENT *)NULL); |
---|
314 | } |
---|
315 | |
---|
316 | static enum clnt_stat |
---|
317 | clnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) |
---|
318 | CLIENT *cl; |
---|
319 | rpcproc_t proc; |
---|
320 | xdrproc_t xdr_args; |
---|
321 | void *args_ptr; |
---|
322 | xdrproc_t xdr_results; |
---|
323 | void *results_ptr; |
---|
324 | struct timeval timeout; |
---|
325 | { |
---|
326 | struct ct_data *ct = (struct ct_data *) cl->cl_private; |
---|
327 | XDR *xdrs = &(ct->ct_xdrs); |
---|
328 | struct rpc_msg reply_msg; |
---|
329 | u_int32_t x_id; |
---|
330 | u_int32_t *msg_x_id = &ct->ct_u.ct_mcalli; /* yuk */ |
---|
331 | bool_t shipnow; |
---|
332 | int refreshes = 2; |
---|
333 | sigset_t mask, newmask; |
---|
334 | int rpc_lock_value; |
---|
335 | bool_t reply_stat; |
---|
336 | |
---|
337 | assert(cl != NULL); |
---|
338 | |
---|
339 | sigfillset(&newmask); |
---|
340 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
341 | mutex_lock(&clnt_fd_lock); |
---|
342 | while (vc_fd_locks[ct->ct_fd]) |
---|
343 | cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); |
---|
344 | if (__isthreaded) |
---|
345 | rpc_lock_value = 1; |
---|
346 | else |
---|
347 | rpc_lock_value = 0; |
---|
348 | vc_fd_locks[ct->ct_fd] = rpc_lock_value; |
---|
349 | mutex_unlock(&clnt_fd_lock); |
---|
350 | if (!ct->ct_waitset) { |
---|
351 | /* If time is not within limits, we ignore it. */ |
---|
352 | if (time_not_ok(&timeout) == FALSE) |
---|
353 | ct->ct_wait = timeout; |
---|
354 | } |
---|
355 | |
---|
356 | shipnow = |
---|
357 | (xdr_results == NULL && timeout.tv_sec == 0 |
---|
358 | && timeout.tv_usec == 0) ? FALSE : TRUE; |
---|
359 | |
---|
360 | call_again: |
---|
361 | xdrs->x_op = XDR_ENCODE; |
---|
362 | ct->ct_error.re_status = RPC_SUCCESS; |
---|
363 | x_id = ntohl(--(*msg_x_id)); |
---|
364 | |
---|
365 | if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { |
---|
366 | if ((! XDR_PUTBYTES(xdrs, ct->ct_u.ct_mcallc, ct->ct_mpos)) || |
---|
367 | (! XDR_PUTINT32(xdrs, &proc)) || |
---|
368 | (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || |
---|
369 | (! (*xdr_args)(xdrs, args_ptr))) { |
---|
370 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
371 | ct->ct_error.re_status = RPC_CANTENCODEARGS; |
---|
372 | (void)xdrrec_endofrecord(xdrs, TRUE); |
---|
373 | release_fd_lock(ct->ct_fd, mask); |
---|
374 | return (ct->ct_error.re_status); |
---|
375 | } |
---|
376 | } else { |
---|
377 | *(uint32_t *) &ct->ct_u.ct_mcallc[ct->ct_mpos] = htonl(proc); |
---|
378 | if (! __rpc_gss_wrap(cl->cl_auth, ct->ct_u.ct_mcallc, |
---|
379 | ct->ct_mpos + sizeof(uint32_t), |
---|
380 | xdrs, xdr_args, args_ptr)) { |
---|
381 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
382 | ct->ct_error.re_status = RPC_CANTENCODEARGS; |
---|
383 | (void)xdrrec_endofrecord(xdrs, TRUE); |
---|
384 | release_fd_lock(ct->ct_fd, mask); |
---|
385 | return (ct->ct_error.re_status); |
---|
386 | } |
---|
387 | } |
---|
388 | if (! xdrrec_endofrecord(xdrs, shipnow)) { |
---|
389 | release_fd_lock(ct->ct_fd, mask); |
---|
390 | return (ct->ct_error.re_status = RPC_CANTSEND); |
---|
391 | } |
---|
392 | if (! shipnow) { |
---|
393 | release_fd_lock(ct->ct_fd, mask); |
---|
394 | return (RPC_SUCCESS); |
---|
395 | } |
---|
396 | /* |
---|
397 | * Hack to provide rpc-based message passing |
---|
398 | */ |
---|
399 | if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { |
---|
400 | release_fd_lock(ct->ct_fd, mask); |
---|
401 | return(ct->ct_error.re_status = RPC_TIMEDOUT); |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | /* |
---|
406 | * Keep receiving until we get a valid transaction id |
---|
407 | */ |
---|
408 | xdrs->x_op = XDR_DECODE; |
---|
409 | while (TRUE) { |
---|
410 | reply_msg.acpted_rply.ar_verf = _null_auth; |
---|
411 | reply_msg.acpted_rply.ar_results.where = NULL; |
---|
412 | reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void; |
---|
413 | if (! xdrrec_skiprecord(xdrs)) { |
---|
414 | release_fd_lock(ct->ct_fd, mask); |
---|
415 | return (ct->ct_error.re_status); |
---|
416 | } |
---|
417 | /* now decode and validate the response header */ |
---|
418 | if (! xdr_replymsg(xdrs, &reply_msg)) { |
---|
419 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
420 | continue; |
---|
421 | release_fd_lock(ct->ct_fd, mask); |
---|
422 | return (ct->ct_error.re_status); |
---|
423 | } |
---|
424 | if (reply_msg.rm_xid == x_id) |
---|
425 | break; |
---|
426 | } |
---|
427 | |
---|
428 | /* |
---|
429 | * process header |
---|
430 | */ |
---|
431 | _seterr_reply(&reply_msg, &(ct->ct_error)); |
---|
432 | if (ct->ct_error.re_status == RPC_SUCCESS) { |
---|
433 | if (! AUTH_VALIDATE(cl->cl_auth, |
---|
434 | &reply_msg.acpted_rply.ar_verf)) { |
---|
435 | ct->ct_error.re_status = RPC_AUTHERROR; |
---|
436 | ct->ct_error.re_why = AUTH_INVALIDRESP; |
---|
437 | } else { |
---|
438 | if (cl->cl_auth->ah_cred.oa_flavor != RPCSEC_GSS) { |
---|
439 | reply_stat = (*xdr_results)(xdrs, results_ptr); |
---|
440 | } else { |
---|
441 | reply_stat = __rpc_gss_unwrap(cl->cl_auth, |
---|
442 | xdrs, xdr_results, results_ptr); |
---|
443 | } |
---|
444 | if (! reply_stat) { |
---|
445 | if (ct->ct_error.re_status == RPC_SUCCESS) |
---|
446 | ct->ct_error.re_status = |
---|
447 | RPC_CANTDECODERES; |
---|
448 | } |
---|
449 | } |
---|
450 | /* free verifier ... */ |
---|
451 | if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { |
---|
452 | xdrs->x_op = XDR_FREE; |
---|
453 | (void)xdr_opaque_auth(xdrs, |
---|
454 | &(reply_msg.acpted_rply.ar_verf)); |
---|
455 | } |
---|
456 | } /* end successful completion */ |
---|
457 | else { |
---|
458 | /* maybe our credentials need to be refreshed ... */ |
---|
459 | if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg)) |
---|
460 | goto call_again; |
---|
461 | } /* end of unsuccessful completion */ |
---|
462 | release_fd_lock(ct->ct_fd, mask); |
---|
463 | return (ct->ct_error.re_status); |
---|
464 | } |
---|
465 | |
---|
466 | static void |
---|
467 | clnt_vc_geterr(cl, errp) |
---|
468 | CLIENT *cl; |
---|
469 | struct rpc_err *errp; |
---|
470 | { |
---|
471 | struct ct_data *ct; |
---|
472 | |
---|
473 | assert(cl != NULL); |
---|
474 | assert(errp != NULL); |
---|
475 | |
---|
476 | ct = (struct ct_data *) cl->cl_private; |
---|
477 | *errp = ct->ct_error; |
---|
478 | } |
---|
479 | |
---|
480 | static bool_t |
---|
481 | clnt_vc_freeres(cl, xdr_res, res_ptr) |
---|
482 | CLIENT *cl; |
---|
483 | xdrproc_t xdr_res; |
---|
484 | void *res_ptr; |
---|
485 | { |
---|
486 | struct ct_data *ct; |
---|
487 | XDR *xdrs; |
---|
488 | bool_t dummy; |
---|
489 | sigset_t mask; |
---|
490 | sigset_t newmask; |
---|
491 | |
---|
492 | assert(cl != NULL); |
---|
493 | |
---|
494 | ct = (struct ct_data *)cl->cl_private; |
---|
495 | xdrs = &(ct->ct_xdrs); |
---|
496 | |
---|
497 | sigfillset(&newmask); |
---|
498 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
499 | mutex_lock(&clnt_fd_lock); |
---|
500 | while (vc_fd_locks[ct->ct_fd]) |
---|
501 | cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); |
---|
502 | xdrs->x_op = XDR_FREE; |
---|
503 | dummy = (*xdr_res)(xdrs, res_ptr); |
---|
504 | mutex_unlock(&clnt_fd_lock); |
---|
505 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
506 | cond_signal(&vc_cv[ct->ct_fd]); |
---|
507 | |
---|
508 | return dummy; |
---|
509 | } |
---|
510 | |
---|
511 | /*ARGSUSED*/ |
---|
512 | static void |
---|
513 | clnt_vc_abort(cl) |
---|
514 | CLIENT *cl; |
---|
515 | { |
---|
516 | } |
---|
517 | |
---|
518 | static bool_t |
---|
519 | clnt_vc_control(cl, request, info) |
---|
520 | CLIENT *cl; |
---|
521 | u_int request; |
---|
522 | void *info; |
---|
523 | { |
---|
524 | struct ct_data *ct; |
---|
525 | void *infop = info; |
---|
526 | sigset_t mask; |
---|
527 | sigset_t newmask; |
---|
528 | int rpc_lock_value; |
---|
529 | |
---|
530 | assert(cl != NULL); |
---|
531 | |
---|
532 | ct = (struct ct_data *)cl->cl_private; |
---|
533 | |
---|
534 | sigfillset(&newmask); |
---|
535 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
536 | mutex_lock(&clnt_fd_lock); |
---|
537 | while (vc_fd_locks[ct->ct_fd]) |
---|
538 | cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock); |
---|
539 | if (__isthreaded) |
---|
540 | rpc_lock_value = 1; |
---|
541 | else |
---|
542 | rpc_lock_value = 0; |
---|
543 | vc_fd_locks[ct->ct_fd] = rpc_lock_value; |
---|
544 | mutex_unlock(&clnt_fd_lock); |
---|
545 | |
---|
546 | switch (request) { |
---|
547 | case CLSET_FD_CLOSE: |
---|
548 | ct->ct_closeit = TRUE; |
---|
549 | release_fd_lock(ct->ct_fd, mask); |
---|
550 | return (TRUE); |
---|
551 | case CLSET_FD_NCLOSE: |
---|
552 | ct->ct_closeit = FALSE; |
---|
553 | release_fd_lock(ct->ct_fd, mask); |
---|
554 | return (TRUE); |
---|
555 | default: |
---|
556 | break; |
---|
557 | } |
---|
558 | |
---|
559 | /* for other requests which use info */ |
---|
560 | if (info == NULL) { |
---|
561 | release_fd_lock(ct->ct_fd, mask); |
---|
562 | return (FALSE); |
---|
563 | } |
---|
564 | switch (request) { |
---|
565 | case CLSET_TIMEOUT: |
---|
566 | if (time_not_ok((struct timeval *)info)) { |
---|
567 | release_fd_lock(ct->ct_fd, mask); |
---|
568 | return (FALSE); |
---|
569 | } |
---|
570 | ct->ct_wait = *(struct timeval *)infop; |
---|
571 | ct->ct_waitset = TRUE; |
---|
572 | break; |
---|
573 | case CLGET_TIMEOUT: |
---|
574 | *(struct timeval *)infop = ct->ct_wait; |
---|
575 | break; |
---|
576 | case CLGET_SERVER_ADDR: |
---|
577 | (void) memcpy(info, ct->ct_addr.buf, (size_t)ct->ct_addr.len); |
---|
578 | break; |
---|
579 | case CLGET_FD: |
---|
580 | *(int *)info = ct->ct_fd; |
---|
581 | break; |
---|
582 | case CLGET_SVC_ADDR: |
---|
583 | /* The caller should not free this memory area */ |
---|
584 | *(struct netbuf *)info = ct->ct_addr; |
---|
585 | break; |
---|
586 | case CLSET_SVC_ADDR: /* set to new address */ |
---|
587 | release_fd_lock(ct->ct_fd, mask); |
---|
588 | return (FALSE); |
---|
589 | case CLGET_XID: |
---|
590 | /* |
---|
591 | * use the knowledge that xid is the |
---|
592 | * first element in the call structure |
---|
593 | * This will get the xid of the PREVIOUS call |
---|
594 | */ |
---|
595 | *(u_int32_t *)info = |
---|
596 | ntohl(*(u_int32_t *)(void *)&ct->ct_u.ct_mcalli); |
---|
597 | break; |
---|
598 | case CLSET_XID: |
---|
599 | /* This will set the xid of the NEXT call */ |
---|
600 | *(u_int32_t *)(void *)&ct->ct_u.ct_mcalli = |
---|
601 | htonl(*((u_int32_t *)info) + 1); |
---|
602 | /* increment by 1 as clnt_vc_call() decrements once */ |
---|
603 | break; |
---|
604 | case CLGET_VERS: |
---|
605 | /* |
---|
606 | * This RELIES on the information that, in the call body, |
---|
607 | * the version number field is the fifth field from the |
---|
608 | * begining of the RPC header. MUST be changed if the |
---|
609 | * call_struct is changed |
---|
610 | */ |
---|
611 | *(u_int32_t *)info = |
---|
612 | ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + |
---|
613 | 4 * BYTES_PER_XDR_UNIT)); |
---|
614 | break; |
---|
615 | |
---|
616 | case CLSET_VERS: |
---|
617 | *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + |
---|
618 | 4 * BYTES_PER_XDR_UNIT) = |
---|
619 | htonl(*(u_int32_t *)info); |
---|
620 | break; |
---|
621 | |
---|
622 | case CLGET_PROG: |
---|
623 | /* |
---|
624 | * This RELIES on the information that, in the call body, |
---|
625 | * the program number field is the fourth field from the |
---|
626 | * begining of the RPC header. MUST be changed if the |
---|
627 | * call_struct is changed |
---|
628 | */ |
---|
629 | *(u_int32_t *)info = |
---|
630 | ntohl(*(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + |
---|
631 | 3 * BYTES_PER_XDR_UNIT)); |
---|
632 | break; |
---|
633 | |
---|
634 | case CLSET_PROG: |
---|
635 | *(u_int32_t *)(void *)(ct->ct_u.ct_mcallc + |
---|
636 | 3 * BYTES_PER_XDR_UNIT) = |
---|
637 | htonl(*(u_int32_t *)info); |
---|
638 | break; |
---|
639 | |
---|
640 | default: |
---|
641 | release_fd_lock(ct->ct_fd, mask); |
---|
642 | return (FALSE); |
---|
643 | } |
---|
644 | release_fd_lock(ct->ct_fd, mask); |
---|
645 | return (TRUE); |
---|
646 | } |
---|
647 | |
---|
648 | |
---|
649 | static void |
---|
650 | clnt_vc_destroy(cl) |
---|
651 | CLIENT *cl; |
---|
652 | { |
---|
653 | struct ct_data *ct = (struct ct_data *) cl->cl_private; |
---|
654 | int ct_fd = ct->ct_fd; |
---|
655 | sigset_t mask; |
---|
656 | sigset_t newmask; |
---|
657 | |
---|
658 | assert(cl != NULL); |
---|
659 | |
---|
660 | ct = (struct ct_data *) cl->cl_private; |
---|
661 | |
---|
662 | sigfillset(&newmask); |
---|
663 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
664 | mutex_lock(&clnt_fd_lock); |
---|
665 | while (vc_fd_locks[ct_fd]) |
---|
666 | cond_wait(&vc_cv[ct_fd], &clnt_fd_lock); |
---|
667 | if (ct->ct_closeit && ct->ct_fd != -1) { |
---|
668 | (void)_close(ct->ct_fd); |
---|
669 | } |
---|
670 | XDR_DESTROY(&(ct->ct_xdrs)); |
---|
671 | if (ct->ct_addr.buf) |
---|
672 | free(ct->ct_addr.buf); |
---|
673 | mem_free(ct, sizeof(struct ct_data)); |
---|
674 | if (cl->cl_netid && cl->cl_netid[0]) |
---|
675 | mem_free(cl->cl_netid, strlen(cl->cl_netid) +1); |
---|
676 | if (cl->cl_tp && cl->cl_tp[0]) |
---|
677 | mem_free(cl->cl_tp, strlen(cl->cl_tp) +1); |
---|
678 | mem_free(cl, sizeof(CLIENT)); |
---|
679 | mutex_unlock(&clnt_fd_lock); |
---|
680 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
681 | cond_signal(&vc_cv[ct_fd]); |
---|
682 | } |
---|
683 | |
---|
684 | /* |
---|
685 | * Interface between xdr serializer and tcp connection. |
---|
686 | * Behaves like the system calls, read & write, but keeps some error state |
---|
687 | * around for the rpc level. |
---|
688 | */ |
---|
689 | static int |
---|
690 | read_vc(ctp, buf, len) |
---|
691 | void *ctp; |
---|
692 | void *buf; |
---|
693 | int len; |
---|
694 | { |
---|
695 | struct sockaddr sa; |
---|
696 | socklen_t sal; |
---|
697 | struct ct_data *ct = (struct ct_data *)ctp; |
---|
698 | struct pollfd fd; |
---|
699 | int milliseconds = (int)((ct->ct_wait.tv_sec * 1000) + |
---|
700 | (ct->ct_wait.tv_usec / 1000)); |
---|
701 | |
---|
702 | if (len == 0) |
---|
703 | return (0); |
---|
704 | fd.fd = ct->ct_fd; |
---|
705 | fd.events = POLLIN; |
---|
706 | for (;;) { |
---|
707 | switch (_poll(&fd, 1, milliseconds)) { |
---|
708 | case 0: |
---|
709 | ct->ct_error.re_status = RPC_TIMEDOUT; |
---|
710 | return (-1); |
---|
711 | |
---|
712 | case -1: |
---|
713 | if (errno == EINTR) |
---|
714 | continue; |
---|
715 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
716 | ct->ct_error.re_errno = errno; |
---|
717 | return (-1); |
---|
718 | } |
---|
719 | break; |
---|
720 | } |
---|
721 | |
---|
722 | sal = sizeof(sa); |
---|
723 | if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) && |
---|
724 | (sa.sa_family == AF_LOCAL)) { |
---|
725 | len = __msgread(ct->ct_fd, buf, (size_t)len); |
---|
726 | } else { |
---|
727 | len = _read(ct->ct_fd, buf, (size_t)len); |
---|
728 | } |
---|
729 | |
---|
730 | switch (len) { |
---|
731 | case 0: |
---|
732 | /* premature eof */ |
---|
733 | ct->ct_error.re_errno = ECONNRESET; |
---|
734 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
735 | len = -1; /* it's really an error */ |
---|
736 | break; |
---|
737 | |
---|
738 | case -1: |
---|
739 | ct->ct_error.re_errno = errno; |
---|
740 | ct->ct_error.re_status = RPC_CANTRECV; |
---|
741 | break; |
---|
742 | } |
---|
743 | return (len); |
---|
744 | } |
---|
745 | |
---|
746 | static int |
---|
747 | write_vc(ctp, buf, len) |
---|
748 | void *ctp; |
---|
749 | void *buf; |
---|
750 | int len; |
---|
751 | { |
---|
752 | struct sockaddr sa; |
---|
753 | socklen_t sal; |
---|
754 | struct ct_data *ct = (struct ct_data *)ctp; |
---|
755 | int i, cnt; |
---|
756 | |
---|
757 | sal = sizeof(sa); |
---|
758 | if ((_getpeername(ct->ct_fd, &sa, &sal) == 0) && |
---|
759 | (sa.sa_family == AF_LOCAL)) { |
---|
760 | for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) { |
---|
761 | if ((i = __msgwrite(ct->ct_fd, buf, |
---|
762 | (size_t)cnt)) == -1) { |
---|
763 | ct->ct_error.re_errno = errno; |
---|
764 | ct->ct_error.re_status = RPC_CANTSEND; |
---|
765 | return (-1); |
---|
766 | } |
---|
767 | } |
---|
768 | } else { |
---|
769 | for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) { |
---|
770 | if ((i = _write(ct->ct_fd, buf, (size_t)cnt)) == -1) { |
---|
771 | ct->ct_error.re_errno = errno; |
---|
772 | ct->ct_error.re_status = RPC_CANTSEND; |
---|
773 | return (-1); |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|
777 | return (len); |
---|
778 | } |
---|
779 | |
---|
780 | static struct clnt_ops * |
---|
781 | clnt_vc_ops() |
---|
782 | { |
---|
783 | static struct clnt_ops ops; |
---|
784 | sigset_t mask, newmask; |
---|
785 | |
---|
786 | /* VARIABLES PROTECTED BY ops_lock: ops */ |
---|
787 | |
---|
788 | sigfillset(&newmask); |
---|
789 | thr_sigsetmask(SIG_SETMASK, &newmask, &mask); |
---|
790 | mutex_lock(&ops_lock); |
---|
791 | if (ops.cl_call == NULL) { |
---|
792 | ops.cl_call = clnt_vc_call; |
---|
793 | ops.cl_abort = clnt_vc_abort; |
---|
794 | ops.cl_geterr = clnt_vc_geterr; |
---|
795 | ops.cl_freeres = clnt_vc_freeres; |
---|
796 | ops.cl_destroy = clnt_vc_destroy; |
---|
797 | ops.cl_control = clnt_vc_control; |
---|
798 | } |
---|
799 | mutex_unlock(&ops_lock); |
---|
800 | thr_sigsetmask(SIG_SETMASK, &(mask), NULL); |
---|
801 | return (&ops); |
---|
802 | } |
---|
803 | |
---|
804 | /* |
---|
805 | * Make sure that the time is not garbage. -1 value is disallowed. |
---|
806 | * Note this is different from time_not_ok in clnt_dg.c |
---|
807 | */ |
---|
808 | static bool_t |
---|
809 | time_not_ok(t) |
---|
810 | struct timeval *t; |
---|
811 | { |
---|
812 | return (t->tv_sec <= -1 || t->tv_sec > 100000000 || |
---|
813 | t->tv_usec <= -1 || t->tv_usec > 1000000); |
---|
814 | } |
---|
815 | |
---|
816 | static int |
---|
817 | __msgread(sock, buf, cnt) |
---|
818 | int sock; |
---|
819 | void *buf; |
---|
820 | size_t cnt; |
---|
821 | { |
---|
822 | struct iovec iov[1]; |
---|
823 | struct msghdr msg; |
---|
824 | union { |
---|
825 | struct cmsghdr cmsg; |
---|
826 | char control[CMSG_SPACE(sizeof(struct cmsgcred))]; |
---|
827 | } cm; |
---|
828 | |
---|
829 | bzero((char *)&cm, sizeof(cm)); |
---|
830 | iov[0].iov_base = buf; |
---|
831 | iov[0].iov_len = cnt; |
---|
832 | |
---|
833 | msg.msg_iov = iov; |
---|
834 | msg.msg_iovlen = 1; |
---|
835 | msg.msg_name = NULL; |
---|
836 | msg.msg_namelen = 0; |
---|
837 | msg.msg_control = (caddr_t)&cm; |
---|
838 | msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); |
---|
839 | msg.msg_flags = 0; |
---|
840 | |
---|
841 | return(_recvmsg(sock, &msg, 0)); |
---|
842 | } |
---|
843 | |
---|
844 | static int |
---|
845 | __msgwrite(sock, buf, cnt) |
---|
846 | int sock; |
---|
847 | void *buf; |
---|
848 | size_t cnt; |
---|
849 | { |
---|
850 | struct iovec iov[1]; |
---|
851 | struct msghdr msg; |
---|
852 | union { |
---|
853 | struct cmsghdr cmsg; |
---|
854 | char control[CMSG_SPACE(sizeof(struct cmsgcred))]; |
---|
855 | } cm; |
---|
856 | |
---|
857 | bzero((char *)&cm, sizeof(cm)); |
---|
858 | iov[0].iov_base = buf; |
---|
859 | iov[0].iov_len = cnt; |
---|
860 | |
---|
861 | cm.cmsg.cmsg_type = SCM_CREDS; |
---|
862 | cm.cmsg.cmsg_level = SOL_SOCKET; |
---|
863 | cm.cmsg.cmsg_len = CMSG_LEN(sizeof(struct cmsgcred)); |
---|
864 | |
---|
865 | msg.msg_iov = iov; |
---|
866 | msg.msg_iovlen = 1; |
---|
867 | msg.msg_name = NULL; |
---|
868 | msg.msg_namelen = 0; |
---|
869 | msg.msg_control = (caddr_t)&cm; |
---|
870 | msg.msg_controllen = CMSG_SPACE(sizeof(struct cmsgcred)); |
---|
871 | msg.msg_flags = 0; |
---|
872 | |
---|
873 | return(_sendmsg(sock, &msg, 0)); |
---|
874 | } |
---|