rpc_prot.c revision 1.18.58.1 1 1.18.58.1 riz /* $NetBSD: rpc_prot.c,v 1.18.58.1 2013/03/14 22:03:14 riz Exp $ */
2 1.2 cgd
3 1.1 cgd /*
4 1.18.58.1 riz * Copyright (c) 2010, Oracle America, Inc.
5 1.18.58.1 riz *
6 1.18.58.1 riz * Redistribution and use in source and binary forms, with or without
7 1.18.58.1 riz * modification, are permitted provided that the following conditions are
8 1.18.58.1 riz * met:
9 1.18.58.1 riz *
10 1.18.58.1 riz * * Redistributions of source code must retain the above copyright
11 1.18.58.1 riz * notice, this list of conditions and the following disclaimer.
12 1.18.58.1 riz * * Redistributions in binary form must reproduce the above
13 1.18.58.1 riz * copyright notice, this list of conditions and the following
14 1.18.58.1 riz * disclaimer in the documentation and/or other materials
15 1.18.58.1 riz * provided with the distribution.
16 1.18.58.1 riz * * Neither the name of the "Oracle America, Inc." nor the names of its
17 1.18.58.1 riz * contributors may be used to endorse or promote products derived
18 1.18.58.1 riz * from this software without specific prior written permission.
19 1.18.58.1 riz *
20 1.18.58.1 riz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 1.18.58.1 riz * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 1.18.58.1 riz * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 1.18.58.1 riz * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 1.18.58.1 riz * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 1.18.58.1 riz * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.18.58.1 riz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 1.18.58.1 riz * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.18.58.1 riz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.18.58.1 riz * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 1.18.58.1 riz * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 1.18.58.1 riz * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.4 christos #include <sys/cdefs.h>
35 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
36 1.4 christos #if 0
37 1.4 christos static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
38 1.4 christos static char *sccsid = "@(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC";
39 1.4 christos #else
40 1.18.58.1 riz __RCSID("$NetBSD: rpc_prot.c,v 1.18.58.1 2013/03/14 22:03:14 riz Exp $");
41 1.4 christos #endif
42 1.1 cgd #endif
43 1.1 cgd
44 1.1 cgd /*
45 1.1 cgd * rpc_prot.c
46 1.1 cgd *
47 1.1 cgd * Copyright (C) 1984, Sun Microsystems, Inc.
48 1.1 cgd *
49 1.1 cgd * This set of routines implements the rpc message definition,
50 1.1 cgd * its serializer and some common rpc utility routines.
51 1.1 cgd * The routines are meant for various implementations of rpc -
52 1.1 cgd * they are NOT for the rpc client or rpc service implementations!
53 1.1 cgd * Because authentication stuff is easy and is part of rpc, the opaque
54 1.1 cgd * routines are also in this program.
55 1.1 cgd */
56 1.1 cgd
57 1.5 jtc #include "namespace.h"
58 1.8 lukem
59 1.1 cgd #include <sys/param.h>
60 1.8 lukem
61 1.13 lukem #include <assert.h>
62 1.13 lukem
63 1.5 jtc #include <rpc/rpc.h>
64 1.1 cgd
65 1.5 jtc #ifdef __weak_alias
66 1.15 mycroft __weak_alias(xdr_accepted_reply,_xdr_accepted_reply)
67 1.15 mycroft __weak_alias(xdr_callhdr,_xdr_callhdr)
68 1.15 mycroft __weak_alias(xdr_des_block,_xdr_des_block)
69 1.15 mycroft __weak_alias(xdr_opaque_auth,_xdr_opaque_auth)
70 1.15 mycroft __weak_alias(xdr_rejected_reply,_xdr_rejected_reply)
71 1.15 mycroft __weak_alias(xdr_replymsg,_xdr_replymsg)
72 1.5 jtc #endif
73 1.1 cgd
74 1.4 christos static void accepted __P((enum accept_stat, struct rpc_err *));
75 1.4 christos static void rejected __P((enum reject_stat, struct rpc_err *));
76 1.4 christos
77 1.1 cgd /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * XDR an opaque authentication struct
81 1.1 cgd * (see auth.h)
82 1.1 cgd */
83 1.1 cgd bool_t
84 1.1 cgd xdr_opaque_auth(xdrs, ap)
85 1.8 lukem XDR *xdrs;
86 1.8 lukem struct opaque_auth *ap;
87 1.1 cgd {
88 1.1 cgd
89 1.13 lukem _DIAGASSERT(xdrs != NULL);
90 1.13 lukem _DIAGASSERT(ap != NULL);
91 1.13 lukem
92 1.1 cgd if (xdr_enum(xdrs, &(ap->oa_flavor)))
93 1.1 cgd return (xdr_bytes(xdrs, &ap->oa_base,
94 1.1 cgd &ap->oa_length, MAX_AUTH_BYTES));
95 1.1 cgd return (FALSE);
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * XDR a DES block
100 1.1 cgd */
101 1.1 cgd bool_t
102 1.1 cgd xdr_des_block(xdrs, blkp)
103 1.8 lukem XDR *xdrs;
104 1.8 lukem des_block *blkp;
105 1.1 cgd {
106 1.13 lukem
107 1.13 lukem _DIAGASSERT(xdrs != NULL);
108 1.13 lukem _DIAGASSERT(blkp != NULL);
109 1.13 lukem
110 1.10 christos return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
111 1.1 cgd }
112 1.1 cgd
113 1.1 cgd /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
114 1.1 cgd
115 1.1 cgd /*
116 1.1 cgd * XDR the MSG_ACCEPTED part of a reply message union
117 1.1 cgd */
118 1.1 cgd bool_t
119 1.1 cgd xdr_accepted_reply(xdrs, ar)
120 1.8 lukem XDR *xdrs;
121 1.8 lukem struct accepted_reply *ar;
122 1.1 cgd {
123 1.1 cgd
124 1.13 lukem _DIAGASSERT(xdrs != NULL);
125 1.13 lukem _DIAGASSERT(ar != NULL);
126 1.13 lukem
127 1.1 cgd /* personalized union, rather than calling xdr_union */
128 1.1 cgd if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
129 1.1 cgd return (FALSE);
130 1.18 christos if (! xdr_enum(xdrs, (enum_t *)(void *)&(ar->ar_stat)))
131 1.1 cgd return (FALSE);
132 1.1 cgd switch (ar->ar_stat) {
133 1.1 cgd
134 1.1 cgd case SUCCESS:
135 1.1 cgd return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
136 1.1 cgd
137 1.1 cgd case PROG_MISMATCH:
138 1.3 cgd if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
139 1.1 cgd return (FALSE);
140 1.3 cgd return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
141 1.4 christos
142 1.4 christos case GARBAGE_ARGS:
143 1.4 christos case SYSTEM_ERR:
144 1.4 christos case PROC_UNAVAIL:
145 1.4 christos case PROG_UNAVAIL:
146 1.4 christos break;
147 1.1 cgd }
148 1.1 cgd return (TRUE); /* TRUE => open ended set of problems */
149 1.1 cgd }
150 1.1 cgd
151 1.1 cgd /*
152 1.1 cgd * XDR the MSG_DENIED part of a reply message union
153 1.1 cgd */
154 1.1 cgd bool_t
155 1.1 cgd xdr_rejected_reply(xdrs, rr)
156 1.8 lukem XDR *xdrs;
157 1.8 lukem struct rejected_reply *rr;
158 1.1 cgd {
159 1.1 cgd
160 1.13 lukem _DIAGASSERT(xdrs != NULL);
161 1.13 lukem _DIAGASSERT(rr != NULL);
162 1.13 lukem
163 1.1 cgd /* personalized union, rather than calling xdr_union */
164 1.18 christos if (! xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_stat)))
165 1.1 cgd return (FALSE);
166 1.1 cgd switch (rr->rj_stat) {
167 1.1 cgd
168 1.1 cgd case RPC_MISMATCH:
169 1.3 cgd if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
170 1.1 cgd return (FALSE);
171 1.3 cgd return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
172 1.1 cgd
173 1.1 cgd case AUTH_ERROR:
174 1.18 christos return (xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_why)));
175 1.1 cgd }
176 1.10 christos /* NOTREACHED */
177 1.1 cgd return (FALSE);
178 1.1 cgd }
179 1.1 cgd
180 1.9 mycroft static const struct xdr_discrim reply_dscrm[3] = {
181 1.12 christos { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
182 1.12 christos { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
183 1.1 cgd { __dontcare__, NULL_xdrproc_t } };
184 1.1 cgd
185 1.1 cgd /*
186 1.1 cgd * XDR a reply message
187 1.1 cgd */
188 1.1 cgd bool_t
189 1.1 cgd xdr_replymsg(xdrs, rmsg)
190 1.8 lukem XDR *xdrs;
191 1.8 lukem struct rpc_msg *rmsg;
192 1.1 cgd {
193 1.13 lukem _DIAGASSERT(xdrs != NULL);
194 1.13 lukem _DIAGASSERT(rmsg != NULL);
195 1.13 lukem
196 1.1 cgd if (
197 1.3 cgd xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
198 1.18 christos xdr_enum(xdrs, (enum_t *)(void *)&(rmsg->rm_direction)) &&
199 1.1 cgd (rmsg->rm_direction == REPLY) )
200 1.18 christos return (xdr_union(xdrs, (enum_t *)(void *)&(rmsg->rm_reply.rp_stat),
201 1.10 christos (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
202 1.10 christos NULL_xdrproc_t));
203 1.1 cgd return (FALSE);
204 1.1 cgd }
205 1.1 cgd
206 1.1 cgd
207 1.1 cgd /*
208 1.1 cgd * Serializes the "static part" of a call message header.
209 1.1 cgd * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
210 1.1 cgd * The rm_xid is not really static, but the user can easily munge on the fly.
211 1.1 cgd */
212 1.1 cgd bool_t
213 1.1 cgd xdr_callhdr(xdrs, cmsg)
214 1.8 lukem XDR *xdrs;
215 1.8 lukem struct rpc_msg *cmsg;
216 1.1 cgd {
217 1.1 cgd
218 1.13 lukem _DIAGASSERT(xdrs != NULL);
219 1.13 lukem _DIAGASSERT(cmsg != NULL);
220 1.13 lukem
221 1.1 cgd cmsg->rm_direction = CALL;
222 1.1 cgd cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
223 1.1 cgd if (
224 1.1 cgd (xdrs->x_op == XDR_ENCODE) &&
225 1.3 cgd xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
226 1.18 christos xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) &&
227 1.3 cgd xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
228 1.3 cgd xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
229 1.11 lukem return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
230 1.1 cgd return (FALSE);
231 1.1 cgd }
232 1.1 cgd
233 1.1 cgd /* ************************** Client utility routine ************* */
234 1.1 cgd
235 1.1 cgd static void
236 1.1 cgd accepted(acpt_stat, error)
237 1.8 lukem enum accept_stat acpt_stat;
238 1.8 lukem struct rpc_err *error;
239 1.1 cgd {
240 1.1 cgd
241 1.13 lukem _DIAGASSERT(error != NULL);
242 1.13 lukem
243 1.1 cgd switch (acpt_stat) {
244 1.1 cgd
245 1.1 cgd case PROG_UNAVAIL:
246 1.1 cgd error->re_status = RPC_PROGUNAVAIL;
247 1.1 cgd return;
248 1.1 cgd
249 1.1 cgd case PROG_MISMATCH:
250 1.1 cgd error->re_status = RPC_PROGVERSMISMATCH;
251 1.1 cgd return;
252 1.1 cgd
253 1.1 cgd case PROC_UNAVAIL:
254 1.1 cgd error->re_status = RPC_PROCUNAVAIL;
255 1.1 cgd return;
256 1.1 cgd
257 1.1 cgd case GARBAGE_ARGS:
258 1.1 cgd error->re_status = RPC_CANTDECODEARGS;
259 1.1 cgd return;
260 1.1 cgd
261 1.1 cgd case SYSTEM_ERR:
262 1.1 cgd error->re_status = RPC_SYSTEMERROR;
263 1.1 cgd return;
264 1.1 cgd
265 1.1 cgd case SUCCESS:
266 1.1 cgd error->re_status = RPC_SUCCESS;
267 1.1 cgd return;
268 1.1 cgd }
269 1.10 christos /* NOTREACHED */
270 1.1 cgd /* something's wrong, but we don't know what ... */
271 1.1 cgd error->re_status = RPC_FAILED;
272 1.10 christos error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
273 1.10 christos error->re_lb.s2 = (int32_t)acpt_stat;
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd static void
277 1.1 cgd rejected(rjct_stat, error)
278 1.8 lukem enum reject_stat rjct_stat;
279 1.8 lukem struct rpc_err *error;
280 1.1 cgd {
281 1.1 cgd
282 1.13 lukem _DIAGASSERT(error != NULL);
283 1.13 lukem
284 1.1 cgd switch (rjct_stat) {
285 1.4 christos case RPC_MISMATCH:
286 1.1 cgd error->re_status = RPC_VERSMISMATCH;
287 1.1 cgd return;
288 1.1 cgd
289 1.1 cgd case AUTH_ERROR:
290 1.1 cgd error->re_status = RPC_AUTHERROR;
291 1.1 cgd return;
292 1.1 cgd }
293 1.1 cgd /* something's wrong, but we don't know what ... */
294 1.10 christos /* NOTREACHED */
295 1.1 cgd error->re_status = RPC_FAILED;
296 1.10 christos error->re_lb.s1 = (int32_t)MSG_DENIED;
297 1.10 christos error->re_lb.s2 = (int32_t)rjct_stat;
298 1.1 cgd }
299 1.1 cgd
300 1.1 cgd /*
301 1.1 cgd * given a reply message, fills in the error
302 1.1 cgd */
303 1.1 cgd void
304 1.1 cgd _seterr_reply(msg, error)
305 1.8 lukem struct rpc_msg *msg;
306 1.8 lukem struct rpc_err *error;
307 1.1 cgd {
308 1.13 lukem
309 1.13 lukem _DIAGASSERT(msg != NULL);
310 1.13 lukem _DIAGASSERT(error != NULL);
311 1.1 cgd
312 1.1 cgd /* optimized for normal, SUCCESSful case */
313 1.1 cgd switch (msg->rm_reply.rp_stat) {
314 1.1 cgd
315 1.1 cgd case MSG_ACCEPTED:
316 1.1 cgd if (msg->acpted_rply.ar_stat == SUCCESS) {
317 1.1 cgd error->re_status = RPC_SUCCESS;
318 1.1 cgd return;
319 1.11 lukem }
320 1.1 cgd accepted(msg->acpted_rply.ar_stat, error);
321 1.1 cgd break;
322 1.1 cgd
323 1.1 cgd case MSG_DENIED:
324 1.1 cgd rejected(msg->rjcted_rply.rj_stat, error);
325 1.1 cgd break;
326 1.1 cgd
327 1.1 cgd default:
328 1.1 cgd error->re_status = RPC_FAILED;
329 1.10 christos error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
330 1.1 cgd break;
331 1.1 cgd }
332 1.1 cgd switch (error->re_status) {
333 1.1 cgd
334 1.1 cgd case RPC_VERSMISMATCH:
335 1.1 cgd error->re_vers.low = msg->rjcted_rply.rj_vers.low;
336 1.1 cgd error->re_vers.high = msg->rjcted_rply.rj_vers.high;
337 1.1 cgd break;
338 1.1 cgd
339 1.1 cgd case RPC_AUTHERROR:
340 1.1 cgd error->re_why = msg->rjcted_rply.rj_why;
341 1.1 cgd break;
342 1.1 cgd
343 1.1 cgd case RPC_PROGVERSMISMATCH:
344 1.1 cgd error->re_vers.low = msg->acpted_rply.ar_vers.low;
345 1.1 cgd error->re_vers.high = msg->acpted_rply.ar_vers.high;
346 1.4 christos break;
347 1.4 christos
348 1.4 christos case RPC_FAILED:
349 1.4 christos case RPC_SUCCESS:
350 1.4 christos case RPC_PROGNOTREGISTERED:
351 1.4 christos case RPC_PMAPFAILURE:
352 1.4 christos case RPC_UNKNOWNPROTO:
353 1.4 christos case RPC_UNKNOWNHOST:
354 1.4 christos case RPC_SYSTEMERROR:
355 1.4 christos case RPC_CANTDECODEARGS:
356 1.4 christos case RPC_PROCUNAVAIL:
357 1.4 christos case RPC_PROGUNAVAIL:
358 1.4 christos case RPC_TIMEDOUT:
359 1.4 christos case RPC_CANTRECV:
360 1.4 christos case RPC_CANTSEND:
361 1.4 christos case RPC_CANTDECODERES:
362 1.4 christos case RPC_CANTENCODEARGS:
363 1.16 fvdl default:
364 1.1 cgd break;
365 1.1 cgd }
366 1.1 cgd }
367