2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 * @(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro
29 * @(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC
30 * $NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl Exp $
31 * $FreeBSD: src/lib/libc/rpc/rpc_prot.c,v 1.13 2007/11/20 01:51:20 jb Exp $
37 * Copyright (C) 1984, Sun Microsystems, Inc.
39 * This set of routines implements the rpc message definition,
40 * its serializer and some common rpc utility routines.
41 * The routines are meant for various implementations of rpc -
42 * they are NOT for the rpc client or rpc service implementations!
43 * Because authentication stuff is easy and is part of rpc, the opaque
44 * routines are also in this program.
47 #include "namespace.h"
48 #include <sys/param.h>
53 #include "un-namespace.h"
55 static void accepted(enum accept_stat, struct rpc_err *);
56 static void rejected(enum reject_stat, struct rpc_err *);
58 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
60 extern struct opaque_auth _null_auth;
63 * XDR an opaque authentication struct
67 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
73 if (xdr_enum(xdrs, &(ap->oa_flavor)))
74 return (xdr_bytes(xdrs, &ap->oa_base,
75 &ap->oa_length, MAX_AUTH_BYTES));
83 xdr_des_block(XDR *xdrs, des_block *blkp)
89 return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
92 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
95 * XDR the MSG_ACCEPTED part of a reply message union
98 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
100 enum accept_stat *par_stat;
102 assert(xdrs != NULL);
105 par_stat = &ar->ar_stat;
107 /* personalized union, rather than calling xdr_union */
108 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
110 if (! xdr_enum(xdrs, (enum_t *) par_stat))
112 switch (ar->ar_stat) {
115 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
118 if (!xdr_rpcvers(xdrs, &(ar->ar_vers.low)))
120 return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));
128 return (TRUE); /* TRUE => open ended set of problems */
132 * XDR the MSG_DENIED part of a reply message union
135 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
137 enum reject_stat *prj_stat;
138 enum auth_stat *prj_why;
140 assert(xdrs != NULL);
143 prj_stat = &rr->rj_stat;
145 /* personalized union, rather than calling xdr_union */
146 if (! xdr_enum(xdrs, (enum_t *) prj_stat))
148 switch (rr->rj_stat) {
151 if (! xdr_rpcvers(xdrs, &(rr->rj_vers.low)))
153 return (xdr_rpcvers(xdrs, &(rr->rj_vers.high)));
156 prj_why = &rr->rj_why;
157 return (xdr_enum(xdrs, (enum_t *) prj_why));
164 static const struct xdr_discrim reply_dscrm[3] = {
165 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
166 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
167 { __dontcare__, NULL_xdrproc_t } };
170 * XDR a reply message
173 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
175 enum msg_type *prm_direction;
176 enum reply_stat *prp_stat;
178 assert(xdrs != NULL);
179 assert(rmsg != NULL);
181 prm_direction = &rmsg->rm_direction;
182 prp_stat = &rmsg->rm_reply.rp_stat;
185 xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
186 xdr_enum(xdrs, (enum_t *) prm_direction) &&
187 (rmsg->rm_direction == REPLY) )
188 return (xdr_union(xdrs, (enum_t *) prp_stat,
189 (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
196 * Serializes the "static part" of a call message header.
197 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
198 * The rm_xid is not really static, but the user can easily munge on the fly.
201 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
203 enum msg_type *prm_direction;
205 assert(xdrs != NULL);
206 assert(cmsg != NULL);
208 prm_direction = &cmsg->rm_direction;
210 cmsg->rm_direction = CALL;
211 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
213 (xdrs->x_op == XDR_ENCODE) &&
214 xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
215 xdr_enum(xdrs, (enum_t *) prm_direction) &&
216 xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
217 xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) )
218 return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
222 /* ************************** Client utility routine ************* */
225 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
228 assert(error != NULL);
233 error->re_status = RPC_PROGUNAVAIL;
237 error->re_status = RPC_PROGVERSMISMATCH;
241 error->re_status = RPC_PROCUNAVAIL;
245 error->re_status = RPC_CANTDECODEARGS;
249 error->re_status = RPC_SYSTEMERROR;
253 error->re_status = RPC_SUCCESS;
257 /* something's wrong, but we don't know what ... */
258 error->re_status = RPC_FAILED;
259 error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
260 error->re_lb.s2 = (int32_t)acpt_stat;
264 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
267 assert(error != NULL);
271 error->re_status = RPC_VERSMISMATCH;
275 error->re_status = RPC_AUTHERROR;
278 /* something's wrong, but we don't know what ... */
280 error->re_status = RPC_FAILED;
281 error->re_lb.s1 = (int32_t)MSG_DENIED;
282 error->re_lb.s2 = (int32_t)rjct_stat;
286 * given a reply message, fills in the error
289 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
293 assert(error != NULL);
295 /* optimized for normal, SUCCESSful case */
296 switch (msg->rm_reply.rp_stat) {
299 if (msg->acpted_rply.ar_stat == SUCCESS) {
300 error->re_status = RPC_SUCCESS;
303 accepted(msg->acpted_rply.ar_stat, error);
307 rejected(msg->rjcted_rply.rj_stat, error);
311 error->re_status = RPC_FAILED;
312 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
315 switch (error->re_status) {
317 case RPC_VERSMISMATCH:
318 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
319 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
323 error->re_why = msg->rjcted_rply.rj_why;
326 case RPC_PROGVERSMISMATCH:
327 error->re_vers.low = msg->acpted_rply.ar_vers.low;
328 error->re_vers.high = msg->acpted_rply.ar_vers.high;
333 case RPC_PROGNOTREGISTERED:
334 case RPC_PMAPFAILURE:
335 case RPC_UNKNOWNPROTO:
336 case RPC_UNKNOWNHOST:
337 case RPC_SYSTEMERROR:
338 case RPC_CANTDECODEARGS:
339 case RPC_PROCUNAVAIL:
340 case RPC_PROGUNAVAIL:
344 case RPC_CANTDECODERES:
345 case RPC_CANTENCODEARGS: