4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the Regents of the University of
36 #pragma ident "%Z%%M% %I% %E% SMI"
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.
48 #include <sys/param.h>
53 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
55 struct opaque_auth _null_auth
;
58 * XDR an opaque authentication struct
62 xdr_opaque_auth(XDR
*xdrs
, struct opaque_auth
*ap
)
64 if (xdr_enum(xdrs
, &(ap
->oa_flavor
)))
65 return (xdr_bytes(xdrs
, &ap
->oa_base
,
66 &ap
->oa_length
, MAX_AUTH_BYTES
));
74 xdr_des_block(XDR
*xdrs
, des_block
*blkp
)
76 return (xdr_opaque(xdrs
, (caddr_t
)blkp
, sizeof (des_block
)));
79 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
82 * XDR the MSG_ACCEPTED part of a reply message union
85 xdr_accepted_reply(XDR
*xdrs
, struct accepted_reply
*ar
)
87 /* personalized union, rather than calling xdr_union */
88 if (!xdr_opaque_auth(xdrs
, &(ar
->ar_verf
)))
90 if (!xdr_enum(xdrs
, (enum_t
*)&(ar
->ar_stat
)))
93 switch (ar
->ar_stat
) {
95 return ((*(ar
->ar_results
.proc
))(xdrs
, ar
->ar_results
.where
));
97 if (!xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.low
)))
99 return (xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.high
)));
101 return (TRUE
); /* TRUE => open ended set of problems */
105 * XDR the MSG_DENIED part of a reply message union
108 xdr_rejected_reply(XDR
*xdrs
, struct rejected_reply
*rr
)
110 /* personalized union, rather than calling xdr_union */
111 if (!xdr_enum(xdrs
, (enum_t
*)&(rr
->rj_stat
)))
113 switch (rr
->rj_stat
) {
115 if (!xdr_u_int(xdrs
, (uint_t
*)&(rr
->rj_vers
.low
)))
117 return (xdr_u_int(xdrs
, (uint_t
*)&(rr
->rj_vers
.high
)));
119 return (xdr_enum(xdrs
, (enum_t
*)&(rr
->rj_why
)));
125 * XDR a reply message
128 xdr_replymsg(XDR
*xdrs
, struct rpc_msg
*rmsg
)
130 struct xdr_discrim reply_dscrm
[3];
132 struct accepted_reply
*ar
;
133 struct opaque_auth
*oa
;
136 if (xdrs
->x_op
== XDR_ENCODE
&&
137 rmsg
->rm_reply
.rp_stat
== MSG_ACCEPTED
&&
138 rmsg
->rm_direction
== REPLY
&&
139 (buf
= XDR_INLINE(xdrs
, 6 * BYTES_PER_XDR_UNIT
+ (rndup
=
140 RNDUP(rmsg
->rm_reply
.rp_acpt
.ar_verf
.oa_length
)))) != NULL
) {
141 IXDR_PUT_INT32(buf
, rmsg
->rm_xid
);
142 IXDR_PUT_ENUM(buf
, rmsg
->rm_direction
);
143 IXDR_PUT_ENUM(buf
, rmsg
->rm_reply
.rp_stat
);
144 ar
= &rmsg
->rm_reply
.rp_acpt
;
146 IXDR_PUT_ENUM(buf
, oa
->oa_flavor
);
147 IXDR_PUT_INT32(buf
, oa
->oa_length
);
149 (void) memcpy(buf
, oa
->oa_base
, oa
->oa_length
);
150 /* LINTED pointer alignment */
151 buf
= (rpc_inline_t
*)(((caddr_t
)buf
) + oa
->oa_length
);
153 if ((rndup
= (rndup
- oa
->oa_length
)) > 0) {
154 (void) memset((caddr_t
)buf
, 0, rndup
);
155 /* LINTED pointer alignment */
156 buf
= (rpc_inline_t
*)(((caddr_t
)buf
) + rndup
);
159 * stat and rest of reply, copied from xdr_accepted_reply
161 IXDR_PUT_ENUM(buf
, ar
->ar_stat
);
162 switch (ar
->ar_stat
) {
164 return ((*(ar
->ar_results
.proc
))
165 (xdrs
, ar
->ar_results
.where
));
167 if (!xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.low
)))
169 return (xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.high
)));
173 if (xdrs
->x_op
== XDR_DECODE
&&
174 (buf
= XDR_INLINE(xdrs
, 3 * BYTES_PER_XDR_UNIT
)) != NULL
) {
175 rmsg
->rm_xid
= IXDR_GET_INT32(buf
);
176 rmsg
->rm_direction
= IXDR_GET_ENUM(buf
, enum msg_type
);
177 if (rmsg
->rm_direction
!= REPLY
)
179 rmsg
->rm_reply
.rp_stat
= IXDR_GET_ENUM(buf
, enum reply_stat
);
180 if (rmsg
->rm_reply
.rp_stat
!= MSG_ACCEPTED
) {
181 if (rmsg
->rm_reply
.rp_stat
== MSG_DENIED
)
182 return (xdr_rejected_reply(xdrs
,
183 &rmsg
->rm_reply
.rp_rjct
));
186 ar
= &rmsg
->rm_reply
.rp_acpt
;
188 buf
= XDR_INLINE(xdrs
, 2 * BYTES_PER_XDR_UNIT
);
190 oa
->oa_flavor
= IXDR_GET_ENUM(buf
, enum_t
);
191 oa
->oa_length
= IXDR_GET_INT32(buf
);
193 if (xdr_enum(xdrs
, &oa
->oa_flavor
) == FALSE
||
194 xdr_u_int(xdrs
, &oa
->oa_length
) == FALSE
)
198 if (oa
->oa_length
> MAX_AUTH_BYTES
)
200 if (oa
->oa_base
== NULL
) {
201 oa
->oa_base
= malloc(oa
->oa_length
);
202 if (oa
->oa_base
== NULL
) {
206 rpc_callerr
.re_status
= RPC_SYSTEMERROR
;
210 buf
= XDR_INLINE(xdrs
, RNDUP(oa
->oa_length
));
212 if (xdr_opaque(xdrs
, oa
->oa_base
,
213 oa
->oa_length
) == FALSE
)
216 (void) memcpy(oa
->oa_base
, buf
, oa
->oa_length
);
220 * stat and rest of reply, copied from
223 if (!xdr_enum(xdrs
, (enum_t
*)&ar
->ar_stat
))
225 switch (ar
->ar_stat
) {
227 return ((*(ar
->ar_results
.proc
))
228 (xdrs
, ar
->ar_results
.where
));
230 if (!xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.low
)))
232 return (xdr_u_int(xdrs
, (uint_t
*)&(ar
->ar_vers
.high
)));
237 reply_dscrm
[0].value
= (int)MSG_ACCEPTED
;
238 reply_dscrm
[0].proc
= (xdrproc_t
)xdr_accepted_reply
;
239 reply_dscrm
[1].value
= (int)MSG_DENIED
;
240 reply_dscrm
[1].proc
= (xdrproc_t
)xdr_rejected_reply
;
241 reply_dscrm
[2].value
= __dontcare__
;
242 reply_dscrm
[2].proc
= NULL_xdrproc_t
;
243 if (xdr_u_int(xdrs
, &(rmsg
->rm_xid
)) &&
244 xdr_enum(xdrs
, (enum_t
*)&(rmsg
->rm_direction
)) &&
245 (rmsg
->rm_direction
== REPLY
))
246 return (xdr_union(xdrs
, (enum_t
*)&(rmsg
->rm_reply
.rp_stat
),
247 (caddr_t
)&(rmsg
->rm_reply
.ru
),
248 reply_dscrm
, NULL_xdrproc_t
));
253 * Serializes the "static part" of a call message header.
254 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
255 * The rm_xid is not really static, but the user can easily munge on the fly.
258 xdr_callhdr(XDR
*xdrs
, struct rpc_msg
*cmsg
)
260 cmsg
->rm_direction
= CALL
;
261 cmsg
->rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
262 if (xdrs
->x_op
== XDR_ENCODE
&&
263 xdr_u_int(xdrs
, &(cmsg
->rm_xid
)) &&
264 xdr_enum(xdrs
, (enum_t
*)&(cmsg
->rm_direction
)) &&
265 xdr_u_int(xdrs
, (uint_t
*)&(cmsg
->rm_call
.cb_rpcvers
)) &&
266 xdr_u_int(xdrs
, (uint_t
*)&(cmsg
->rm_call
.cb_prog
))) {
267 return (xdr_u_int(xdrs
, (uint_t
*)&(cmsg
->rm_call
.cb_vers
)));
272 /* ************************** Client utility routine ************* */
275 accepted(enum accept_stat acpt_stat
, struct rpc_err
*error
)
280 error
->re_status
= RPC_PROGUNAVAIL
;
284 error
->re_status
= RPC_PROGVERSMISMATCH
;
288 error
->re_status
= RPC_PROCUNAVAIL
;
292 error
->re_status
= RPC_CANTDECODEARGS
;
296 error
->re_status
= RPC_SYSTEMERROR
;
300 error
->re_status
= RPC_SUCCESS
;
303 /* something's wrong, but we don't know what ... */
304 error
->re_status
= RPC_FAILED
;
305 error
->re_lb
.s1
= (int32_t)MSG_ACCEPTED
;
306 error
->re_lb
.s2
= (int32_t)acpt_stat
;
310 rejected(enum reject_stat rjct_stat
, struct rpc_err
*error
)
314 error
->re_status
= RPC_VERSMISMATCH
;
318 error
->re_status
= RPC_AUTHERROR
;
321 /* something's wrong, but we don't know what ... */
322 error
->re_status
= RPC_FAILED
;
323 error
->re_lb
.s1
= (int32_t)MSG_DENIED
;
324 error
->re_lb
.s2
= (int32_t)rjct_stat
;
328 * given a reply message, fills in the error
331 __seterr_reply(struct rpc_msg
*msg
, struct rpc_err
*error
)
333 /* optimized for normal, SUCCESSful case */
334 switch (msg
->rm_reply
.rp_stat
) {
336 if (msg
->acpted_rply
.ar_stat
== SUCCESS
) {
337 error
->re_status
= RPC_SUCCESS
;
340 accepted(msg
->acpted_rply
.ar_stat
, error
);
344 rejected(msg
->rjcted_rply
.rj_stat
, error
);
348 error
->re_status
= RPC_FAILED
;
349 error
->re_lb
.s1
= (int32_t)(msg
->rm_reply
.rp_stat
);
353 switch (error
->re_status
) {
354 case RPC_VERSMISMATCH
:
355 error
->re_vers
.low
= msg
->rjcted_rply
.rj_vers
.low
;
356 error
->re_vers
.high
= msg
->rjcted_rply
.rj_vers
.high
;
360 error
->re_why
= msg
->rjcted_rply
.rj_why
;
363 case RPC_PROGVERSMISMATCH
:
364 error
->re_vers
.low
= msg
->acpted_rply
.ar_vers
.low
;
365 error
->re_vers
.high
= msg
->acpted_rply
.ar_vers
.high
;