xref: /onnv-gate/usr/src/uts/common/rpc/rpc_prot.c (revision 7387:0b3a92e31fd8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7387SRobert.Gordon@Sun.COM  * Common Development and Distribution License (the "License").
6*7387SRobert.Gordon@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7387SRobert.Gordon@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * rpc_prot.c
360Sstevel@tonic-gate  * This set of routines implements the rpc message definition,
370Sstevel@tonic-gate  * its serializer and some common rpc utility routines.
380Sstevel@tonic-gate  * The routines are meant for various implementations of rpc -
390Sstevel@tonic-gate  * they are NOT for the rpc client or rpc service implementations!
400Sstevel@tonic-gate  * Because authentication stuff is easy and is part of rpc, the opaque
410Sstevel@tonic-gate  * routines are also in this program.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <sys/param.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/t_lock.h>
480Sstevel@tonic-gate #include <sys/systm.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <rpc/types.h>
510Sstevel@tonic-gate #include <rpc/xdr.h>
520Sstevel@tonic-gate #include <rpc/auth.h>
530Sstevel@tonic-gate #include <rpc/clnt.h>
540Sstevel@tonic-gate #include <rpc/rpc_msg.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate struct opaque_auth _null_auth;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * XDR an opaque authentication struct
620Sstevel@tonic-gate  * (see auth.h)
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate bool_t
xdr_opaque_auth(XDR * xdrs,struct opaque_auth * ap)650Sstevel@tonic-gate xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	if (xdr_enum(xdrs, &(ap->oa_flavor))) {
680Sstevel@tonic-gate 		return (xdr_bytes(xdrs, &ap->oa_base,
690Sstevel@tonic-gate 		    &ap->oa_length, MAX_AUTH_BYTES));
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 	return (FALSE);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * XDR a DES block
760Sstevel@tonic-gate  */
770Sstevel@tonic-gate bool_t
xdr_des_block(XDR * xdrs,des_block * blkp)780Sstevel@tonic-gate xdr_des_block(XDR *xdrs, des_block *blkp)
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof (des_block)));
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * XDR the MSG_ACCEPTED part of a reply message union
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate bool_t
xdr_accepted_reply(XDR * xdrs,struct accepted_reply * ar)890Sstevel@tonic-gate xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	/* personalized union, rather than calling xdr_union */
920Sstevel@tonic-gate 	if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
930Sstevel@tonic-gate 		return (FALSE);
940Sstevel@tonic-gate 	if (!xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
950Sstevel@tonic-gate 		return (FALSE);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	switch (ar->ar_stat) {
980Sstevel@tonic-gate 	case SUCCESS:
990Sstevel@tonic-gate 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	case PROG_MISMATCH:
1020Sstevel@tonic-gate 		if (!xdr_rpcvers(xdrs, &(ar->ar_vers.low)))
1030Sstevel@tonic-gate 			return (FALSE);
1040Sstevel@tonic-gate 		return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	return (TRUE);  /* TRUE => open ended set of problems */
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * XDR the MSG_DENIED part of a reply message union
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate bool_t
xdr_rejected_reply(XDR * xdrs,struct rejected_reply * rr)1130Sstevel@tonic-gate xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
1140Sstevel@tonic-gate {
1150Sstevel@tonic-gate 	/* personalized union, rather than calling xdr_union */
1160Sstevel@tonic-gate 	if (!xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
1170Sstevel@tonic-gate 		return (FALSE);
1180Sstevel@tonic-gate 	switch (rr->rj_stat) {
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	case RPC_MISMATCH:
1210Sstevel@tonic-gate 		if (!xdr_rpcvers(xdrs, &(rr->rj_vers.low)))
1220Sstevel@tonic-gate 			return (FALSE);
1230Sstevel@tonic-gate 		return (xdr_rpcvers(xdrs, &(rr->rj_vers.high)));
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	case AUTH_ERROR:
1260Sstevel@tonic-gate 		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	return (FALSE);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static struct xdr_discrim reply_dscrm[3] = {
1320Sstevel@tonic-gate 	{ MSG_ACCEPTED, xdr_accepted_reply },
1330Sstevel@tonic-gate 	{ MSG_DENIED, xdr_rejected_reply },
1340Sstevel@tonic-gate 	{ __dontcare__, NULL_xdrproc_t }
1350Sstevel@tonic-gate };
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * XDR a reply message
1390Sstevel@tonic-gate  */
1400Sstevel@tonic-gate bool_t
xdr_replymsg(XDR * xdrs,struct rpc_msg * rmsg)1410Sstevel@tonic-gate xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate 	int32_t *buf;
1440Sstevel@tonic-gate 	struct accepted_reply *ar;
1450Sstevel@tonic-gate 	struct opaque_auth *oa;
1460Sstevel@tonic-gate 	uint_t rndup;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE &&
1490Sstevel@tonic-gate 	    rmsg->rm_reply.rp_stat == MSG_ACCEPTED &&
1500Sstevel@tonic-gate 	    rmsg->rm_direction == REPLY &&
1510Sstevel@tonic-gate 	    (buf = XDR_INLINE(xdrs, 6 * BYTES_PER_XDR_UNIT + (rndup =
1520Sstevel@tonic-gate 	    RNDUP(rmsg->rm_reply.rp_acpt.ar_verf.oa_length)))) != NULL) {
1530Sstevel@tonic-gate 		IXDR_PUT_INT32(buf, rmsg->rm_xid);
1540Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, rmsg->rm_direction);
1550Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, rmsg->rm_reply.rp_stat);
1560Sstevel@tonic-gate 		ar = &rmsg->rm_reply.rp_acpt;
1570Sstevel@tonic-gate 		oa = &ar->ar_verf;
1580Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, oa->oa_flavor);
1590Sstevel@tonic-gate 		IXDR_PUT_INT32(buf, oa->oa_length);
1600Sstevel@tonic-gate 		if (oa->oa_length) {
1610Sstevel@tonic-gate 			bcopy(oa->oa_base, buf, oa->oa_length);
1620Sstevel@tonic-gate 			buf = (int32_t *)(((caddr_t)buf) + oa->oa_length);
1630Sstevel@tonic-gate 			if ((rndup = (rndup - oa->oa_length)) > 0) {
1640Sstevel@tonic-gate 				bzero(buf, rndup);
1650Sstevel@tonic-gate 				buf = (int32_t *)(((caddr_t)buf) + rndup);
1660Sstevel@tonic-gate 			}
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 		/*
1690Sstevel@tonic-gate 		 * stat and rest of reply, copied from xdr_accepted_reply
1700Sstevel@tonic-gate 		 */
1710Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, ar->ar_stat);
1720Sstevel@tonic-gate 		switch (ar->ar_stat) {
1730Sstevel@tonic-gate 		case SUCCESS:
1740Sstevel@tonic-gate 			return ((*(ar->ar_results.proc))(xdrs,
1750Sstevel@tonic-gate 			    ar->ar_results.where));
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		case PROG_MISMATCH:
1780Sstevel@tonic-gate 			if (!xdr_rpcvers(xdrs, &(ar->ar_vers.low)))
1790Sstevel@tonic-gate 				return (FALSE);
1800Sstevel@tonic-gate 			return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));
1810Sstevel@tonic-gate 		}
1820Sstevel@tonic-gate 		return (TRUE);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE &&
1850Sstevel@tonic-gate 	    (buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT)) != NULL) {
1860Sstevel@tonic-gate 		rmsg->rm_xid = IXDR_GET_INT32(buf);
1870Sstevel@tonic-gate 		rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
1880Sstevel@tonic-gate 		if (rmsg->rm_direction != REPLY)
1890Sstevel@tonic-gate 			return (FALSE);
1900Sstevel@tonic-gate 		rmsg->rm_reply.rp_stat = IXDR_GET_ENUM(buf, enum reply_stat);
1910Sstevel@tonic-gate 		if (rmsg->rm_reply.rp_stat != MSG_ACCEPTED) {
1920Sstevel@tonic-gate 			if (rmsg->rm_reply.rp_stat == MSG_DENIED)
1930Sstevel@tonic-gate 				return (xdr_rejected_reply(xdrs,
1940Sstevel@tonic-gate 				    &rmsg->rm_reply.rp_rjct));
1950Sstevel@tonic-gate 			return (FALSE);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		ar = &rmsg->rm_reply.rp_acpt;
1980Sstevel@tonic-gate 		oa = &ar->ar_verf;
1990Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
2000Sstevel@tonic-gate 		if (buf != NULL) {
2010Sstevel@tonic-gate 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
2020Sstevel@tonic-gate 			oa->oa_length = IXDR_GET_INT32(buf);
2030Sstevel@tonic-gate 		} else {
2040Sstevel@tonic-gate 			if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
2050Sstevel@tonic-gate 			    xdr_u_int(xdrs, &oa->oa_length) == FALSE)
2060Sstevel@tonic-gate 				return (FALSE);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		if (oa->oa_length) {
2090Sstevel@tonic-gate 			if (oa->oa_length > MAX_AUTH_BYTES)
2100Sstevel@tonic-gate 				return (FALSE);
2110Sstevel@tonic-gate 			if (oa->oa_base == NULL) {
2120Sstevel@tonic-gate 				oa->oa_base = (caddr_t)
2130Sstevel@tonic-gate 				    mem_alloc(oa->oa_length);
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
2160Sstevel@tonic-gate 			if (buf == NULL) {
2170Sstevel@tonic-gate 				if (xdr_opaque(xdrs, oa->oa_base,
2180Sstevel@tonic-gate 				    oa->oa_length) == FALSE)
2190Sstevel@tonic-gate 					return (FALSE);
2200Sstevel@tonic-gate 			} else {
2210Sstevel@tonic-gate 				bcopy(buf, oa->oa_base, oa->oa_length);
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * stat and rest of reply, copied from
2260Sstevel@tonic-gate 		 * xdr_accepted_reply
2270Sstevel@tonic-gate 		 */
2280Sstevel@tonic-gate 		if (!xdr_enum(xdrs, (enum_t *)&ar->ar_stat))
2290Sstevel@tonic-gate 			return (FALSE);
2300Sstevel@tonic-gate 		switch (ar->ar_stat) {
2310Sstevel@tonic-gate 		case SUCCESS:
2320Sstevel@tonic-gate 			return ((*(ar->ar_results.proc))(xdrs,
2330Sstevel@tonic-gate 			    ar->ar_results.where));
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		case PROG_MISMATCH:
2360Sstevel@tonic-gate 			if (!xdr_rpcvers(xdrs, &ar->ar_vers.low))
2370Sstevel@tonic-gate 				return (FALSE);
2380Sstevel@tonic-gate 			return (xdr_rpcvers(xdrs, &ar->ar_vers.high));
2390Sstevel@tonic-gate 		}
2400Sstevel@tonic-gate 		return (TRUE);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (xdr_u_int(xdrs, &(rmsg->rm_xid)) &&
2440Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
2450Sstevel@tonic-gate 	    (rmsg->rm_direction == REPLY))
2460Sstevel@tonic-gate 		return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
2470Sstevel@tonic-gate 		    (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm,
2480Sstevel@tonic-gate 		    NULL_xdrproc_t));
2490Sstevel@tonic-gate 	return (FALSE);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * XDR a reply message header (encode only)
2540Sstevel@tonic-gate  */
2550Sstevel@tonic-gate bool_t
xdr_replymsg_hdr(XDR * xdrs,struct rpc_msg * rmsg)2560Sstevel@tonic-gate xdr_replymsg_hdr(XDR *xdrs, struct rpc_msg *rmsg)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	int32_t *buf;
2590Sstevel@tonic-gate 	struct accepted_reply *ar;
2600Sstevel@tonic-gate 	struct opaque_auth *oa;
2610Sstevel@tonic-gate 	uint_t rndup;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if (xdrs->x_op != XDR_ENCODE ||
2640Sstevel@tonic-gate 	    rmsg->rm_reply.rp_stat != MSG_ACCEPTED ||
2650Sstevel@tonic-gate 	    rmsg->rm_direction != REPLY)
2660Sstevel@tonic-gate 		return (FALSE);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	if ((buf = XDR_INLINE(xdrs, 6 * BYTES_PER_XDR_UNIT + (rndup =
2690Sstevel@tonic-gate 	    RNDUP(rmsg->rm_reply.rp_acpt.ar_verf.oa_length)))) != NULL) {
2700Sstevel@tonic-gate 		IXDR_PUT_INT32(buf, rmsg->rm_xid);
2710Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, rmsg->rm_direction);
2720Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, rmsg->rm_reply.rp_stat);
2730Sstevel@tonic-gate 		ar = &rmsg->rm_reply.rp_acpt;
2740Sstevel@tonic-gate 		oa = &ar->ar_verf;
2750Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, oa->oa_flavor);
2760Sstevel@tonic-gate 		IXDR_PUT_INT32(buf, oa->oa_length);
2770Sstevel@tonic-gate 		if (oa->oa_length) {
2780Sstevel@tonic-gate 			bcopy(oa->oa_base, buf, oa->oa_length);
2790Sstevel@tonic-gate 			buf = (int32_t *)(((caddr_t)buf) + oa->oa_length);
2800Sstevel@tonic-gate 			if ((rndup = (rndup - oa->oa_length)) > 0) {
281*7387SRobert.Gordon@Sun.COM 				bzero(buf, rndup);
282*7387SRobert.Gordon@Sun.COM 				buf = (int32_t *)(((caddr_t)buf) + rndup);
2830Sstevel@tonic-gate 			}
2840Sstevel@tonic-gate 		}
2850Sstevel@tonic-gate 		/*
2860Sstevel@tonic-gate 		 * stat and rest of reply, copied from xdr_accepted_reply
2870Sstevel@tonic-gate 		 */
2880Sstevel@tonic-gate 		IXDR_PUT_ENUM(buf, ar->ar_stat);
2890Sstevel@tonic-gate 		return (TRUE);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	if (xdr_u_int(xdrs, &(rmsg->rm_xid)) &&
2930Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
2940Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat)) &&
2950Sstevel@tonic-gate 	    xdr_opaque_auth(xdrs, &rmsg->rm_reply.rp_acpt.ar_verf) &&
2960Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_reply.rp_acpt.ar_stat)))
2970Sstevel@tonic-gate 		return (TRUE);
2980Sstevel@tonic-gate 	return (FALSE);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * XDR a reply message body (encode only)
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate bool_t
xdr_replymsg_body(XDR * xdrs,struct rpc_msg * rmsg)3050Sstevel@tonic-gate xdr_replymsg_body(XDR *xdrs, struct rpc_msg *rmsg)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	struct accepted_reply *ar;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (xdrs->x_op != XDR_ENCODE)
3100Sstevel@tonic-gate 		return (FALSE);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	ar = &rmsg->rm_reply.rp_acpt;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (ar->ar_results.proc == NULL)
3150Sstevel@tonic-gate 		return (TRUE);
3160Sstevel@tonic-gate 	return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * Serializes the "static part" of a call message header.
3210Sstevel@tonic-gate  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
3220Sstevel@tonic-gate  * The rm_xid is not really static, but the user can easily munge on the fly.
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate bool_t
xdr_callhdr(XDR * xdrs,struct rpc_msg * cmsg)3250Sstevel@tonic-gate xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	cmsg->rm_direction = CALL;
3280Sstevel@tonic-gate 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
3290Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE &&
3300Sstevel@tonic-gate 	    xdr_u_int(xdrs, &(cmsg->rm_xid)) &&
3310Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
3320Sstevel@tonic-gate 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
3330Sstevel@tonic-gate 	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)))
3340Sstevel@tonic-gate 		return (xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)));
3350Sstevel@tonic-gate 	return (FALSE);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /* ************************** Client utility routine ************* */
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate static void
accepted(enum accept_stat acpt_stat,struct rpc_err * error)3410Sstevel@tonic-gate accepted(enum accept_stat acpt_stat, struct rpc_err *error)
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	switch (acpt_stat) {
3440Sstevel@tonic-gate 	case PROG_UNAVAIL:
3450Sstevel@tonic-gate 		error->re_status = RPC_PROGUNAVAIL;
3460Sstevel@tonic-gate 		return;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	case PROG_MISMATCH:
3490Sstevel@tonic-gate 		error->re_status = RPC_PROGVERSMISMATCH;
3500Sstevel@tonic-gate 		return;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	case PROC_UNAVAIL:
3530Sstevel@tonic-gate 		error->re_status = RPC_PROCUNAVAIL;
3540Sstevel@tonic-gate 		return;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	case GARBAGE_ARGS:
3570Sstevel@tonic-gate 		error->re_status = RPC_CANTDECODEARGS;
3580Sstevel@tonic-gate 		return;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	case SYSTEM_ERR:
3610Sstevel@tonic-gate 		error->re_status = RPC_SYSTEMERROR;
3620Sstevel@tonic-gate 		return;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	case SUCCESS:
3650Sstevel@tonic-gate 		error->re_status = RPC_SUCCESS;
3660Sstevel@tonic-gate 		return;
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 	/* something's wrong, but we don't know what ... */
3690Sstevel@tonic-gate 	error->re_status = RPC_FAILED;
3700Sstevel@tonic-gate 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
3710Sstevel@tonic-gate 	error->re_lb.s2 = (int32_t)acpt_stat;
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate static void
rejected(enum reject_stat rjct_stat,struct rpc_err * error)3750Sstevel@tonic-gate rejected(enum reject_stat rjct_stat, struct rpc_err *error)
3760Sstevel@tonic-gate {
3770Sstevel@tonic-gate 	switch (rjct_stat) {
3780Sstevel@tonic-gate 	case RPC_VERSMISMATCH:
3790Sstevel@tonic-gate 		error->re_status = RPC_VERSMISMATCH;
3800Sstevel@tonic-gate 		return;
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	case AUTH_ERROR:
3830Sstevel@tonic-gate 		error->re_status = RPC_AUTHERROR;
3840Sstevel@tonic-gate 		return;
3850Sstevel@tonic-gate 	}
3860Sstevel@tonic-gate 	/* something's wrong, but we don't know what ... */
3870Sstevel@tonic-gate 	error->re_status = RPC_FAILED;
3880Sstevel@tonic-gate 	error->re_lb.s1 = (int32_t)MSG_DENIED;
3890Sstevel@tonic-gate 	error->re_lb.s2 = (int32_t)rjct_stat;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate /*
3930Sstevel@tonic-gate  * given a reply message, fills in the error
3940Sstevel@tonic-gate  */
3950Sstevel@tonic-gate void
_seterr_reply(struct rpc_msg * msg,struct rpc_err * error)3960Sstevel@tonic-gate _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	/* optimized for normal, SUCCESSful case */
3990Sstevel@tonic-gate 	switch (msg->rm_reply.rp_stat) {
4000Sstevel@tonic-gate 	case MSG_ACCEPTED:
4010Sstevel@tonic-gate 		if (msg->acpted_rply.ar_stat == SUCCESS) {
4020Sstevel@tonic-gate 			error->re_status = RPC_SUCCESS;
4030Sstevel@tonic-gate 			return;
4040Sstevel@tonic-gate 		};
4050Sstevel@tonic-gate 		accepted(msg->acpted_rply.ar_stat, error);
4060Sstevel@tonic-gate 		break;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	case MSG_DENIED:
4090Sstevel@tonic-gate 		rejected(msg->rjcted_rply.rj_stat, error);
4100Sstevel@tonic-gate 		break;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	default:
4130Sstevel@tonic-gate 		error->re_status = RPC_FAILED;
4140Sstevel@tonic-gate 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
4150Sstevel@tonic-gate 		break;
4160Sstevel@tonic-gate 	}
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	switch (error->re_status) {
4190Sstevel@tonic-gate 	case RPC_VERSMISMATCH:
4200Sstevel@tonic-gate 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
4210Sstevel@tonic-gate 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
4220Sstevel@tonic-gate 		break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	case RPC_AUTHERROR:
4250Sstevel@tonic-gate 		error->re_why = msg->rjcted_rply.rj_why;
4260Sstevel@tonic-gate 		break;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	case RPC_PROGVERSMISMATCH:
4290Sstevel@tonic-gate 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
4300Sstevel@tonic-gate 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
4310Sstevel@tonic-gate 		break;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate /*
4360Sstevel@tonic-gate  * given a reply message, frees the accepted verifier
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate bool_t
xdr_rpc_free_verifier(XDR * xdrs,struct rpc_msg * msg)4390Sstevel@tonic-gate xdr_rpc_free_verifier(XDR *xdrs, struct rpc_msg *msg)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	if (msg->rm_direction == REPLY &&
4420Sstevel@tonic-gate 	    msg->rm_reply.rp_stat == MSG_ACCEPTED &&
4430Sstevel@tonic-gate 	    msg->acpted_rply.ar_verf.oa_base != NULL) {
4440Sstevel@tonic-gate 		xdrs->x_op = XDR_FREE;
4450Sstevel@tonic-gate 		return (xdr_opaque_auth(xdrs, &(msg->acpted_rply.ar_verf)));
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 	return (TRUE);
4480Sstevel@tonic-gate }
449