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.
32 * Copyright (C) 1984, Sun Microsystems, Inc.
34 * This set of routines implements the rpc message definition,
35 * its serializer and some common rpc utility routines.
36 * The routines are meant for various implementations of rpc -
37 * they are NOT for the rpc client or rpc service implementations!
38 * Because authentication stuff is easy and is part of rpc, the opaque
39 * routines are also in this program.
42 #include <sys/param.h>
48 static void accepted(enum accept_stat, struct rpc_err *);
49 static void rejected(enum reject_stat, struct rpc_err *);
51 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
53 extern struct opaque_auth _null_auth;
56 * XDR an opaque authentication struct
60 xdr_opaque_auth(xdrs, ap)
62 struct opaque_auth *ap;
68 if (xdr_enum(xdrs, &(ap->oa_flavor)))
69 return (xdr_bytes(xdrs, &ap->oa_base,
70 &ap->oa_length, MAX_AUTH_BYTES));
78 xdr_des_block(xdrs, blkp)
86 return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
89 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
92 * XDR the MSG_ACCEPTED part of a reply message union
95 xdr_accepted_reply(xdrs, ar)
97 struct accepted_reply *ar;
100 assert(xdrs != NULL);
103 /* personalized union, rather than calling xdr_union */
104 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
106 if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
108 switch (ar->ar_stat) {
111 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
114 if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
116 return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
124 return (TRUE); /* TRUE => open ended set of problems */
128 * XDR the MSG_DENIED part of a reply message union
131 xdr_rejected_reply(xdrs, rr)
133 struct rejected_reply *rr;
136 assert(xdrs != NULL);
139 /* personalized union, rather than calling xdr_union */
140 if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
142 switch (rr->rj_stat) {
145 if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
147 return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
150 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
156 static const struct xdr_discrim reply_dscrm[3] = {
157 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
158 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
159 { __dontcare__, NULL_xdrproc_t } };
162 * XDR a reply message
165 xdr_replymsg(xdrs, rmsg)
167 struct rpc_msg *rmsg;
169 assert(xdrs != NULL);
170 assert(rmsg != NULL);
173 xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
174 xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
175 (rmsg->rm_direction == REPLY) )
176 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
177 (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
184 * Serializes the "static part" of a call message header.
185 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
186 * The rm_xid is not really static, but the user can easily munge on the fly.
189 xdr_callhdr(xdrs, cmsg)
191 struct rpc_msg *cmsg;
194 assert(xdrs != NULL);
195 assert(cmsg != NULL);
197 cmsg->rm_direction = CALL;
198 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
200 (xdrs->x_op == XDR_ENCODE) &&
201 xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
202 xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
203 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
204 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
205 return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
209 /* ************************** Client utility routine ************* */
212 accepted(acpt_stat, error)
213 enum accept_stat acpt_stat;
214 struct rpc_err *error;
217 assert(error != NULL);
222 error->re_status = RPC_PROGUNAVAIL;
226 error->re_status = RPC_PROGVERSMISMATCH;
230 error->re_status = RPC_PROCUNAVAIL;
234 error->re_status = RPC_CANTDECODEARGS;
238 error->re_status = RPC_SYSTEMERROR;
242 error->re_status = RPC_SUCCESS;
246 /* something's wrong, but we don't know what ... */
247 error->re_status = RPC_FAILED;
248 error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
249 error->re_lb.s2 = (int32_t)acpt_stat;
253 rejected(rjct_stat, error)
254 enum reject_stat rjct_stat;
255 struct rpc_err *error;
258 assert(error != NULL);
262 error->re_status = RPC_VERSMISMATCH;
266 error->re_status = RPC_AUTHERROR;
269 /* something's wrong, but we don't know what ... */
271 error->re_status = RPC_FAILED;
272 error->re_lb.s1 = (int32_t)MSG_DENIED;
273 error->re_lb.s2 = (int32_t)rjct_stat;
277 * given a reply message, fills in the error
280 _seterr_reply(msg, error)
282 struct rpc_err *error;
286 assert(error != NULL);
288 /* optimized for normal, SUCCESSful case */
289 switch (msg->rm_reply.rp_stat) {
292 if (msg->acpted_rply.ar_stat == SUCCESS) {
293 error->re_status = RPC_SUCCESS;
296 accepted(msg->acpted_rply.ar_stat, error);
300 rejected(msg->rjcted_rply.rj_stat, error);
304 error->re_status = RPC_FAILED;
305 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
308 switch (error->re_status) {
310 case RPC_VERSMISMATCH:
311 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
312 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
316 error->re_why = msg->rjcted_rply.rj_why;
319 case RPC_PROGVERSMISMATCH:
320 error->re_vers.low = msg->acpted_rply.ar_vers.low;
321 error->re_vers.high = msg->acpted_rply.ar_vers.high;
326 case RPC_PROGNOTREGISTERED:
327 case RPC_PMAPFAILURE:
328 case RPC_UNKNOWNPROTO:
329 case RPC_UNKNOWNHOST:
330 case RPC_SYSTEMERROR:
331 case RPC_CANTDECODEARGS:
332 case RPC_PROCUNAVAIL:
333 case RPC_PROGUNAVAIL:
337 case RPC_CANTDECODERES:
338 case RPC_CANTENCODEARGS: