1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate /*
38*0Sstevel@tonic-gate  * rpc_calmsg.c
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <sys/param.h>
42*0Sstevel@tonic-gate #include <sys/types.h>
43*0Sstevel@tonic-gate #include <sys/t_lock.h>
44*0Sstevel@tonic-gate #include <sys/systm.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include <rpc/types.h>
47*0Sstevel@tonic-gate #include <rpc/xdr.h>
48*0Sstevel@tonic-gate #include <rpc/auth.h>
49*0Sstevel@tonic-gate #include <rpc/clnt.h>
50*0Sstevel@tonic-gate #include <rpc/rpc_msg.h>
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * XDR a call message
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate bool_t
56*0Sstevel@tonic-gate xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	int32_t *buf;
59*0Sstevel@tonic-gate 	struct opaque_auth *oa;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	if (xdrs->x_op == XDR_ENCODE) {
62*0Sstevel@tonic-gate 		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES)
63*0Sstevel@tonic-gate 			return (FALSE);
64*0Sstevel@tonic-gate 		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES)
65*0Sstevel@tonic-gate 			return (FALSE);
66*0Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT +
67*0Sstevel@tonic-gate 		    RNDUP(cmsg->rm_call.cb_cred.oa_length) +
68*0Sstevel@tonic-gate 		    2 * BYTES_PER_XDR_UNIT +
69*0Sstevel@tonic-gate 		    RNDUP(cmsg->rm_call.cb_verf.oa_length));
70*0Sstevel@tonic-gate 		if (buf != NULL) {
71*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, cmsg->rm_xid);
72*0Sstevel@tonic-gate 			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
73*0Sstevel@tonic-gate 			if (cmsg->rm_direction != CALL)
74*0Sstevel@tonic-gate 				return (FALSE);
75*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
76*0Sstevel@tonic-gate 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
77*0Sstevel@tonic-gate 				return (FALSE);
78*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
79*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
80*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
81*0Sstevel@tonic-gate 			oa = &cmsg->rm_call.cb_cred;
82*0Sstevel@tonic-gate 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
83*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, oa->oa_length);
84*0Sstevel@tonic-gate 			if (oa->oa_length) {
85*0Sstevel@tonic-gate 				bcopy(oa->oa_base, buf, oa->oa_length);
86*0Sstevel@tonic-gate 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
87*0Sstevel@tonic-gate 			}
88*0Sstevel@tonic-gate 			oa = &cmsg->rm_call.cb_verf;
89*0Sstevel@tonic-gate 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
90*0Sstevel@tonic-gate 			IXDR_PUT_INT32(buf, oa->oa_length);
91*0Sstevel@tonic-gate 			if (oa->oa_length)
92*0Sstevel@tonic-gate 				bcopy(oa->oa_base, buf, oa->oa_length);
93*0Sstevel@tonic-gate 			return (TRUE);
94*0Sstevel@tonic-gate 		}
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate 	if (xdrs->x_op == XDR_DECODE) {
97*0Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
98*0Sstevel@tonic-gate 		if (buf != NULL) {
99*0Sstevel@tonic-gate 			cmsg->rm_xid = IXDR_GET_INT32(buf);
100*0Sstevel@tonic-gate 			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
101*0Sstevel@tonic-gate 			if (cmsg->rm_direction != CALL)
102*0Sstevel@tonic-gate 				return (FALSE);
103*0Sstevel@tonic-gate 			cmsg->rm_call.cb_rpcvers = IXDR_GET_INT32(buf);
104*0Sstevel@tonic-gate 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
105*0Sstevel@tonic-gate 				return (FALSE);
106*0Sstevel@tonic-gate 			cmsg->rm_call.cb_prog = IXDR_GET_INT32(buf);
107*0Sstevel@tonic-gate 			cmsg->rm_call.cb_vers = IXDR_GET_INT32(buf);
108*0Sstevel@tonic-gate 			cmsg->rm_call.cb_proc = IXDR_GET_INT32(buf);
109*0Sstevel@tonic-gate 			oa = &cmsg->rm_call.cb_cred;
110*0Sstevel@tonic-gate 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
111*0Sstevel@tonic-gate 			oa->oa_length = IXDR_GET_INT32(buf);
112*0Sstevel@tonic-gate 			if (oa->oa_length) {
113*0Sstevel@tonic-gate 				if (oa->oa_length > MAX_AUTH_BYTES)
114*0Sstevel@tonic-gate 					return (FALSE);
115*0Sstevel@tonic-gate 				if (oa->oa_base == NULL)
116*0Sstevel@tonic-gate 					oa->oa_base = (caddr_t)
117*0Sstevel@tonic-gate 					    mem_alloc(oa->oa_length);
118*0Sstevel@tonic-gate 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
119*0Sstevel@tonic-gate 				if (buf == NULL) {
120*0Sstevel@tonic-gate 					if (xdr_opaque(xdrs, oa->oa_base,
121*0Sstevel@tonic-gate 					    oa->oa_length) == FALSE)
122*0Sstevel@tonic-gate 						return (FALSE);
123*0Sstevel@tonic-gate 				} else {
124*0Sstevel@tonic-gate 					bcopy(buf, oa->oa_base, oa->oa_length);
125*0Sstevel@tonic-gate 				}
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 			oa = &cmsg->rm_call.cb_verf;
128*0Sstevel@tonic-gate 			buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
129*0Sstevel@tonic-gate 			if (buf == NULL) {
130*0Sstevel@tonic-gate 				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
131*0Sstevel@tonic-gate 				    xdr_u_int(xdrs, &oa->oa_length) == FALSE)
132*0Sstevel@tonic-gate 					return (FALSE);
133*0Sstevel@tonic-gate 			} else {
134*0Sstevel@tonic-gate 				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
135*0Sstevel@tonic-gate 				oa->oa_length = IXDR_GET_INT32(buf);
136*0Sstevel@tonic-gate 			}
137*0Sstevel@tonic-gate 			if (oa->oa_length) {
138*0Sstevel@tonic-gate 				if (oa->oa_length > MAX_AUTH_BYTES)
139*0Sstevel@tonic-gate 					return (FALSE);
140*0Sstevel@tonic-gate 				if (oa->oa_base == NULL)
141*0Sstevel@tonic-gate 					oa->oa_base = (caddr_t)
142*0Sstevel@tonic-gate 					    mem_alloc(oa->oa_length);
143*0Sstevel@tonic-gate 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
144*0Sstevel@tonic-gate 				if (buf == NULL) {
145*0Sstevel@tonic-gate 					if (xdr_opaque(xdrs, oa->oa_base,
146*0Sstevel@tonic-gate 					    oa->oa_length) == FALSE)
147*0Sstevel@tonic-gate 						return (FALSE);
148*0Sstevel@tonic-gate 				} else {
149*0Sstevel@tonic-gate 					bcopy(buf, oa->oa_base, oa->oa_length);
150*0Sstevel@tonic-gate 				}
151*0Sstevel@tonic-gate 			}
152*0Sstevel@tonic-gate 			return (TRUE);
153*0Sstevel@tonic-gate 		}
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	if (xdr_u_int(xdrs, &(cmsg->rm_xid)) &&
157*0Sstevel@tonic-gate 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
158*0Sstevel@tonic-gate 	    cmsg->rm_direction == CALL &&
159*0Sstevel@tonic-gate 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
160*0Sstevel@tonic-gate 	    cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION &&
161*0Sstevel@tonic-gate 	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) &&
162*0Sstevel@tonic-gate 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)) &&
163*0Sstevel@tonic-gate 	    xdr_rpcproc(xdrs, &(cmsg->rm_call.cb_proc)) &&
164*0Sstevel@tonic-gate 	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)))
165*0Sstevel@tonic-gate 		return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	return (FALSE);
168*0Sstevel@tonic-gate }
169