4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley 4.3 BSD
31 * under license from the Regents of the University of California.
36 * This set of routines implements the rpc message definition,
37 * its serializer and some common rpc utility routines.
38 * The routines are meant for various implementations of rpc -
39 * they are NOT for the rpc client or rpc service implementations!
40 * Because authentication stuff is easy and is part of rpc, the opaque
41 * routines are also in this program.
44 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/t_lock.h>
48 #include <sys/systm.h>
50 #include <rpc/types.h>
54 #include <rpc/rpc_msg.h>
56 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
58 struct opaque_auth _null_auth
;
61 * XDR an opaque authentication struct
65 xdr_opaque_auth(XDR
*xdrs
, struct opaque_auth
*ap
)
67 if (xdr_enum(xdrs
, &(ap
->oa_flavor
))) {
68 return (xdr_bytes(xdrs
, &ap
->oa_base
,
69 &ap
->oa_length
, MAX_AUTH_BYTES
));
78 xdr_des_block(XDR
*xdrs
, des_block
*blkp
)
80 return (xdr_opaque(xdrs
, (caddr_t
)blkp
, sizeof (des_block
)));
83 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
86 * XDR the MSG_ACCEPTED part of a reply message union
89 xdr_accepted_reply(XDR
*xdrs
, struct accepted_reply
*ar
)
91 /* personalized union, rather than calling xdr_union */
92 if (!xdr_opaque_auth(xdrs
, &(ar
->ar_verf
)))
94 if (!xdr_enum(xdrs
, (enum_t
*)&(ar
->ar_stat
)))
97 switch (ar
->ar_stat
) {
99 return ((*(ar
->ar_results
.proc
))(xdrs
, ar
->ar_results
.where
));
102 if (!xdr_rpcvers(xdrs
, &(ar
->ar_vers
.low
)))
104 return (xdr_rpcvers(xdrs
, &(ar
->ar_vers
.high
)));
106 return (TRUE
); /* TRUE => open ended set of problems */
110 * XDR the MSG_DENIED part of a reply message union
113 xdr_rejected_reply(XDR
*xdrs
, struct rejected_reply
*rr
)
115 /* personalized union, rather than calling xdr_union */
116 if (!xdr_enum(xdrs
, (enum_t
*)&(rr
->rj_stat
)))
118 switch (rr
->rj_stat
) {
121 if (!xdr_rpcvers(xdrs
, &(rr
->rj_vers
.low
)))
123 return (xdr_rpcvers(xdrs
, &(rr
->rj_vers
.high
)));
126 return (xdr_enum(xdrs
, (enum_t
*)&(rr
->rj_why
)));
131 static struct xdr_discrim reply_dscrm
[3] = {
132 { MSG_ACCEPTED
, xdr_accepted_reply
},
133 { MSG_DENIED
, xdr_rejected_reply
},
134 { __dontcare__
, NULL_xdrproc_t
}
138 * XDR a reply message
141 xdr_replymsg(XDR
*xdrs
, struct rpc_msg
*rmsg
)
144 struct accepted_reply
*ar
;
145 struct opaque_auth
*oa
;
148 if (xdrs
->x_op
== XDR_ENCODE
&&
149 rmsg
->rm_reply
.rp_stat
== MSG_ACCEPTED
&&
150 rmsg
->rm_direction
== REPLY
&&
151 (buf
= XDR_INLINE(xdrs
, 6 * BYTES_PER_XDR_UNIT
+ (rndup
=
152 RNDUP(rmsg
->rm_reply
.rp_acpt
.ar_verf
.oa_length
)))) != NULL
) {
153 IXDR_PUT_INT32(buf
, rmsg
->rm_xid
);
154 IXDR_PUT_ENUM(buf
, rmsg
->rm_direction
);
155 IXDR_PUT_ENUM(buf
, rmsg
->rm_reply
.rp_stat
);
156 ar
= &rmsg
->rm_reply
.rp_acpt
;
158 IXDR_PUT_ENUM(buf
, oa
->oa_flavor
);
159 IXDR_PUT_INT32(buf
, oa
->oa_length
);
161 bcopy(oa
->oa_base
, buf
, oa
->oa_length
);
162 buf
= (int32_t *)(((caddr_t
)buf
) + oa
->oa_length
);
163 if ((rndup
= (rndup
- oa
->oa_length
)) > 0) {
165 buf
= (int32_t *)(((caddr_t
)buf
) + rndup
);
169 * stat and rest of reply, copied from xdr_accepted_reply
171 IXDR_PUT_ENUM(buf
, ar
->ar_stat
);
172 switch (ar
->ar_stat
) {
174 return ((*(ar
->ar_results
.proc
))(xdrs
,
175 ar
->ar_results
.where
));
178 if (!xdr_rpcvers(xdrs
, &(ar
->ar_vers
.low
)))
180 return (xdr_rpcvers(xdrs
, &(ar
->ar_vers
.high
)));
184 if (xdrs
->x_op
== XDR_DECODE
&&
185 (buf
= XDR_INLINE(xdrs
, 3 * BYTES_PER_XDR_UNIT
)) != NULL
) {
186 rmsg
->rm_xid
= IXDR_GET_INT32(buf
);
187 rmsg
->rm_direction
= IXDR_GET_ENUM(buf
, enum msg_type
);
188 if (rmsg
->rm_direction
!= REPLY
)
190 rmsg
->rm_reply
.rp_stat
= IXDR_GET_ENUM(buf
, enum reply_stat
);
191 if (rmsg
->rm_reply
.rp_stat
!= MSG_ACCEPTED
) {
192 if (rmsg
->rm_reply
.rp_stat
== MSG_DENIED
)
193 return (xdr_rejected_reply(xdrs
,
194 &rmsg
->rm_reply
.rp_rjct
));
197 ar
= &rmsg
->rm_reply
.rp_acpt
;
199 buf
= XDR_INLINE(xdrs
, 2 * BYTES_PER_XDR_UNIT
);
201 oa
->oa_flavor
= IXDR_GET_ENUM(buf
, enum_t
);
202 oa
->oa_length
= IXDR_GET_INT32(buf
);
204 if (xdr_enum(xdrs
, &oa
->oa_flavor
) == FALSE
||
205 xdr_u_int(xdrs
, &oa
->oa_length
) == FALSE
)
209 if (oa
->oa_length
> MAX_AUTH_BYTES
)
211 if (oa
->oa_base
== NULL
) {
212 oa
->oa_base
= (caddr_t
)
213 mem_alloc(oa
->oa_length
);
215 buf
= XDR_INLINE(xdrs
, RNDUP(oa
->oa_length
));
217 if (xdr_opaque(xdrs
, oa
->oa_base
,
218 oa
->oa_length
) == FALSE
)
221 bcopy(buf
, oa
->oa_base
, oa
->oa_length
);
225 * stat and rest of reply, copied from
228 if (!xdr_enum(xdrs
, (enum_t
*)&ar
->ar_stat
))
230 switch (ar
->ar_stat
) {
232 return ((*(ar
->ar_results
.proc
))(xdrs
,
233 ar
->ar_results
.where
));
236 if (!xdr_rpcvers(xdrs
, &ar
->ar_vers
.low
))
238 return (xdr_rpcvers(xdrs
, &ar
->ar_vers
.high
));
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
), reply_dscrm
,
253 * XDR a reply message header (encode only)
256 xdr_replymsg_hdr(XDR
*xdrs
, struct rpc_msg
*rmsg
)
259 struct accepted_reply
*ar
;
260 struct opaque_auth
*oa
;
263 if (xdrs
->x_op
!= XDR_ENCODE
||
264 rmsg
->rm_reply
.rp_stat
!= MSG_ACCEPTED
||
265 rmsg
->rm_direction
!= REPLY
)
268 if ((buf
= XDR_INLINE(xdrs
, 6 * BYTES_PER_XDR_UNIT
+ (rndup
=
269 RNDUP(rmsg
->rm_reply
.rp_acpt
.ar_verf
.oa_length
)))) != NULL
) {
270 IXDR_PUT_INT32(buf
, rmsg
->rm_xid
);
271 IXDR_PUT_ENUM(buf
, rmsg
->rm_direction
);
272 IXDR_PUT_ENUM(buf
, rmsg
->rm_reply
.rp_stat
);
273 ar
= &rmsg
->rm_reply
.rp_acpt
;
275 IXDR_PUT_ENUM(buf
, oa
->oa_flavor
);
276 IXDR_PUT_INT32(buf
, oa
->oa_length
);
278 bcopy(oa
->oa_base
, buf
, oa
->oa_length
);
279 buf
= (int32_t *)(((caddr_t
)buf
) + oa
->oa_length
);
280 if ((rndup
= (rndup
- oa
->oa_length
)) > 0) {
282 buf
= (int32_t *)(((caddr_t
)buf
) + rndup
);
286 * stat and rest of reply, copied from xdr_accepted_reply
288 IXDR_PUT_ENUM(buf
, ar
->ar_stat
);
292 if (xdr_u_int(xdrs
, &(rmsg
->rm_xid
)) &&
293 xdr_enum(xdrs
, (enum_t
*)&(rmsg
->rm_direction
)) &&
294 xdr_enum(xdrs
, (enum_t
*)&(rmsg
->rm_reply
.rp_stat
)) &&
295 xdr_opaque_auth(xdrs
, &rmsg
->rm_reply
.rp_acpt
.ar_verf
) &&
296 xdr_enum(xdrs
, (enum_t
*)&(rmsg
->rm_reply
.rp_acpt
.ar_stat
)))
302 * XDR a reply message body (encode only)
305 xdr_replymsg_body(XDR
*xdrs
, struct rpc_msg
*rmsg
)
307 struct accepted_reply
*ar
;
309 if (xdrs
->x_op
!= XDR_ENCODE
)
312 ar
= &rmsg
->rm_reply
.rp_acpt
;
314 if (ar
->ar_results
.proc
== NULL
)
316 return ((*(ar
->ar_results
.proc
))(xdrs
, ar
->ar_results
.where
));
320 * Serializes the "static part" of a call message header.
321 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
322 * The rm_xid is not really static, but the user can easily munge on the fly.
325 xdr_callhdr(XDR
*xdrs
, struct rpc_msg
*cmsg
)
327 cmsg
->rm_direction
= CALL
;
328 cmsg
->rm_call
.cb_rpcvers
= RPC_MSG_VERSION
;
329 if (xdrs
->x_op
== XDR_ENCODE
&&
330 xdr_u_int(xdrs
, &(cmsg
->rm_xid
)) &&
331 xdr_enum(xdrs
, (enum_t
*)&(cmsg
->rm_direction
)) &&
332 xdr_rpcvers(xdrs
, &(cmsg
->rm_call
.cb_rpcvers
)) &&
333 xdr_rpcprog(xdrs
, &(cmsg
->rm_call
.cb_prog
)))
334 return (xdr_rpcvers(xdrs
, &(cmsg
->rm_call
.cb_vers
)));
338 /* ************************** Client utility routine ************* */
341 accepted(enum accept_stat acpt_stat
, struct rpc_err
*error
)
345 error
->re_status
= RPC_PROGUNAVAIL
;
349 error
->re_status
= RPC_PROGVERSMISMATCH
;
353 error
->re_status
= RPC_PROCUNAVAIL
;
357 error
->re_status
= RPC_CANTDECODEARGS
;
361 error
->re_status
= RPC_SYSTEMERROR
;
365 error
->re_status
= RPC_SUCCESS
;
368 /* something's wrong, but we don't know what ... */
369 error
->re_status
= RPC_FAILED
;
370 error
->re_lb
.s1
= (int32_t)MSG_ACCEPTED
;
371 error
->re_lb
.s2
= (int32_t)acpt_stat
;
375 rejected(enum reject_stat rjct_stat
, struct rpc_err
*error
)
378 case RPC_VERSMISMATCH
:
379 error
->re_status
= RPC_VERSMISMATCH
;
383 error
->re_status
= RPC_AUTHERROR
;
386 /* something's wrong, but we don't know what ... */
387 error
->re_status
= RPC_FAILED
;
388 error
->re_lb
.s1
= (int32_t)MSG_DENIED
;
389 error
->re_lb
.s2
= (int32_t)rjct_stat
;
393 * given a reply message, fills in the error
396 _seterr_reply(struct rpc_msg
*msg
, struct rpc_err
*error
)
398 /* optimized for normal, SUCCESSful case */
399 switch (msg
->rm_reply
.rp_stat
) {
401 if (msg
->acpted_rply
.ar_stat
== SUCCESS
) {
402 error
->re_status
= RPC_SUCCESS
;
405 accepted(msg
->acpted_rply
.ar_stat
, error
);
409 rejected(msg
->rjcted_rply
.rj_stat
, error
);
413 error
->re_status
= RPC_FAILED
;
414 error
->re_lb
.s1
= (int32_t)(msg
->rm_reply
.rp_stat
);
418 switch (error
->re_status
) {
419 case RPC_VERSMISMATCH
:
420 error
->re_vers
.low
= msg
->rjcted_rply
.rj_vers
.low
;
421 error
->re_vers
.high
= msg
->rjcted_rply
.rj_vers
.high
;
425 error
->re_why
= msg
->rjcted_rply
.rj_why
;
428 case RPC_PROGVERSMISMATCH
:
429 error
->re_vers
.low
= msg
->acpted_rply
.ar_vers
.low
;
430 error
->re_vers
.high
= msg
->acpted_rply
.ar_vers
.high
;
436 * given a reply message, frees the accepted verifier
439 xdr_rpc_free_verifier(XDR
*xdrs
, struct rpc_msg
*msg
)
441 if (msg
->rm_direction
== REPLY
&&
442 msg
->rm_reply
.rp_stat
== MSG_ACCEPTED
&&
443 msg
->acpted_rply
.ar_verf
.oa_base
!= NULL
) {
444 xdrs
->x_op
= XDR_FREE
;
445 return (xdr_opaque_auth(xdrs
, &(msg
->acpted_rply
.ar_verf
)));