1 /* $NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
42 * Copyright (C) 1984, Sun Microsystems, Inc.
44 * This set of routines implements the rpc message definition,
45 * its serializer and some common rpc utility routines.
46 * The routines are meant for various implementations of rpc -
47 * they are NOT for the rpc client or rpc service implementations!
48 * Because authentication stuff is easy and is part of rpc, the opaque
49 * routines are also in this program.
52 #include "namespace.h"
53 #include <sys/param.h>
58 #include "un-namespace.h"
60 static void accepted(enum accept_stat, struct rpc_err *);
61 static void rejected(enum reject_stat, struct rpc_err *);
63 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
65 extern struct opaque_auth _null_auth;
68 * XDR an opaque authentication struct
72 xdr_opaque_auth(xdrs, ap)
74 struct opaque_auth *ap;
80 if (xdr_enum(xdrs, &(ap->oa_flavor)))
81 return (xdr_bytes(xdrs, &ap->oa_base,
82 &ap->oa_length, MAX_AUTH_BYTES));
90 xdr_des_block(xdrs, blkp)
98 return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
101 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
104 * XDR the MSG_ACCEPTED part of a reply message union
107 xdr_accepted_reply(xdrs, ar)
109 struct accepted_reply *ar;
111 enum accept_stat *par_stat;
113 assert(xdrs != NULL);
116 par_stat = &ar->ar_stat;
118 /* personalized union, rather than calling xdr_union */
119 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
121 if (! xdr_enum(xdrs, (enum_t *) par_stat))
123 switch (ar->ar_stat) {
126 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
129 if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
131 return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
139 return (TRUE); /* TRUE => open ended set of problems */
143 * XDR the MSG_DENIED part of a reply message union
146 xdr_rejected_reply(xdrs, rr)
148 struct rejected_reply *rr;
150 enum reject_stat *prj_stat;
151 enum auth_stat *prj_why;
153 assert(xdrs != NULL);
156 prj_stat = &rr->rj_stat;
158 /* personalized union, rather than calling xdr_union */
159 if (! xdr_enum(xdrs, (enum_t *) prj_stat))
161 switch (rr->rj_stat) {
164 if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
166 return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
169 prj_why = &rr->rj_why;
170 return (xdr_enum(xdrs, (enum_t *) prj_why));
177 static const struct xdr_discrim reply_dscrm[3] = {
178 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
179 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
180 { __dontcare__, NULL_xdrproc_t } };
183 * XDR a reply message
186 xdr_replymsg(xdrs, rmsg)
188 struct rpc_msg *rmsg;
190 enum msg_type *prm_direction;
191 enum reply_stat *prp_stat;
193 assert(xdrs != NULL);
194 assert(rmsg != NULL);
196 prm_direction = &rmsg->rm_direction;
197 prp_stat = &rmsg->rm_reply.rp_stat;
200 xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
201 xdr_enum(xdrs, (enum_t *) prm_direction) &&
202 (rmsg->rm_direction == REPLY) )
203 return (xdr_union(xdrs, (enum_t *) prp_stat,
204 (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
211 * Serializes the "static part" of a call message header.
212 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
213 * The rm_xid is not really static, but the user can easily munge on the fly.
216 xdr_callhdr(xdrs, cmsg)
218 struct rpc_msg *cmsg;
220 enum msg_type *prm_direction;
222 assert(xdrs != NULL);
223 assert(cmsg != NULL);
225 prm_direction = &cmsg->rm_direction;
227 cmsg->rm_direction = CALL;
228 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
230 (xdrs->x_op == XDR_ENCODE) &&
231 xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
232 xdr_enum(xdrs, (enum_t *) prm_direction) &&
233 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
234 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
235 return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
239 /* ************************** Client utility routine ************* */
242 accepted(acpt_stat, error)
243 enum accept_stat acpt_stat;
244 struct rpc_err *error;
247 assert(error != NULL);
252 error->re_status = RPC_PROGUNAVAIL;
256 error->re_status = RPC_PROGVERSMISMATCH;
260 error->re_status = RPC_PROCUNAVAIL;
264 error->re_status = RPC_CANTDECODEARGS;
268 error->re_status = RPC_SYSTEMERROR;
272 error->re_status = RPC_SUCCESS;
276 /* something's wrong, but we don't know what ... */
277 error->re_status = RPC_FAILED;
278 error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
279 error->re_lb.s2 = (int32_t)acpt_stat;
283 rejected(rjct_stat, error)
284 enum reject_stat rjct_stat;
285 struct rpc_err *error;
288 assert(error != NULL);
292 error->re_status = RPC_VERSMISMATCH;
296 error->re_status = RPC_AUTHERROR;
299 /* something's wrong, but we don't know what ... */
301 error->re_status = RPC_FAILED;
302 error->re_lb.s1 = (int32_t)MSG_DENIED;
303 error->re_lb.s2 = (int32_t)rjct_stat;
307 * given a reply message, fills in the error
310 _seterr_reply(msg, error)
312 struct rpc_err *error;
316 assert(error != NULL);
318 /* optimized for normal, SUCCESSful case */
319 switch (msg->rm_reply.rp_stat) {
322 if (msg->acpted_rply.ar_stat == SUCCESS) {
323 error->re_status = RPC_SUCCESS;
326 accepted(msg->acpted_rply.ar_stat, error);
330 rejected(msg->rjcted_rply.rj_stat, error);
334 error->re_status = RPC_FAILED;
335 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
338 switch (error->re_status) {
340 case RPC_VERSMISMATCH:
341 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
342 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
346 error->re_why = msg->rjcted_rply.rj_why;
349 case RPC_PROGVERSMISMATCH:
350 error->re_vers.low = msg->acpted_rply.ar_vers.low;
351 error->re_vers.high = msg->acpted_rply.ar_vers.high;
356 case RPC_PROGNOTREGISTERED:
357 case RPC_PMAPFAILURE:
358 case RPC_UNKNOWNPROTO:
359 case RPC_UNKNOWNHOST:
360 case RPC_SYSTEMERROR:
361 case RPC_CANTDECODEARGS:
362 case RPC_PROCUNAVAIL:
363 case RPC_PROGUNAVAIL:
367 case RPC_CANTDECODERES:
368 case RPC_CANTENCODEARGS: