xref: /freebsd/crypto/krb5/src/lib/rpc/rpc_callmsg.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* @(#)rpc_callmsg.c	2.1 88/07/29 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *     * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *     * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *       the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *       distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  *     * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert  *       its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert  *       derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert  *
22*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert  * rpc_callmsg.c
40*7f2fe78bSCy Schubert  */
41*7f2fe78bSCy Schubert 
42*7f2fe78bSCy Schubert #include <sys/param.h>
43*7f2fe78bSCy Schubert #include <string.h>
44*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
45*7f2fe78bSCy Schubert 
46*7f2fe78bSCy Schubert /*
47*7f2fe78bSCy Schubert  * XDR a call message
48*7f2fe78bSCy Schubert  */
49*7f2fe78bSCy Schubert bool_t
xdr_callmsg(XDR * xdrs,struct rpc_msg * cmsg)50*7f2fe78bSCy Schubert xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
51*7f2fe78bSCy Schubert {
52*7f2fe78bSCy Schubert 	rpc_inline_t *buf;
53*7f2fe78bSCy Schubert 	struct opaque_auth *oa;
54*7f2fe78bSCy Schubert 
55*7f2fe78bSCy Schubert 	if (xdrs->x_op == XDR_ENCODE) {
56*7f2fe78bSCy Schubert 		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
57*7f2fe78bSCy Schubert 			return (FALSE);
58*7f2fe78bSCy Schubert 		}
59*7f2fe78bSCy Schubert 		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
60*7f2fe78bSCy Schubert 			return (FALSE);
61*7f2fe78bSCy Schubert 		}
62*7f2fe78bSCy Schubert 		buf = (rpc_inline_t *) XDR_INLINE(xdrs, (int) (
63*7f2fe78bSCy Schubert 		        8 * BYTES_PER_XDR_UNIT
64*7f2fe78bSCy Schubert 			+ RNDUP(cmsg->rm_call.cb_cred.oa_length)
65*7f2fe78bSCy Schubert 			+ 2 * BYTES_PER_XDR_UNIT
66*7f2fe78bSCy Schubert 			+ RNDUP(cmsg->rm_call.cb_verf.oa_length)));
67*7f2fe78bSCy Schubert 		if (buf != NULL) {
68*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, cmsg->rm_xid);
69*7f2fe78bSCy Schubert 			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
70*7f2fe78bSCy Schubert 			if (cmsg->rm_direction != CALL) {
71*7f2fe78bSCy Schubert 				return (FALSE);
72*7f2fe78bSCy Schubert 			}
73*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, cmsg->rm_call.cb_rpcvers);
74*7f2fe78bSCy Schubert 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
75*7f2fe78bSCy Schubert 				return (FALSE);
76*7f2fe78bSCy Schubert 			}
77*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, cmsg->rm_call.cb_prog);
78*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, cmsg->rm_call.cb_vers);
79*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, cmsg->rm_call.cb_proc);
80*7f2fe78bSCy Schubert 			oa = &cmsg->rm_call.cb_cred;
81*7f2fe78bSCy Schubert 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
82*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, oa->oa_length);
83*7f2fe78bSCy Schubert 			if (oa->oa_length) {
84*7f2fe78bSCy Schubert 				memmove((caddr_t)buf, oa->oa_base,
85*7f2fe78bSCy Schubert 					oa->oa_length);
86*7f2fe78bSCy Schubert 				buf += RNDUP(oa->oa_length) / BYTES_PER_XDR_UNIT;
87*7f2fe78bSCy Schubert 			}
88*7f2fe78bSCy Schubert 			oa = &cmsg->rm_call.cb_verf;
89*7f2fe78bSCy Schubert 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
90*7f2fe78bSCy Schubert 			IXDR_PUT_LONG(buf, oa->oa_length);
91*7f2fe78bSCy Schubert 			if (oa->oa_length) {
92*7f2fe78bSCy Schubert 				memmove((caddr_t)buf, oa->oa_base,
93*7f2fe78bSCy Schubert 					oa->oa_length);
94*7f2fe78bSCy Schubert 				/* no real need....
95*7f2fe78bSCy Schubert 				buf += RNDUP(oa->oa_length) / BYTES_PER_XDR_UNIT;
96*7f2fe78bSCy Schubert 				*/
97*7f2fe78bSCy Schubert 			}
98*7f2fe78bSCy Schubert 			return (TRUE);
99*7f2fe78bSCy Schubert 		}
100*7f2fe78bSCy Schubert 	}
101*7f2fe78bSCy Schubert 	if (xdrs->x_op == XDR_DECODE) {
102*7f2fe78bSCy Schubert 		buf = (rpc_inline_t *) XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
103*7f2fe78bSCy Schubert 		if (buf != NULL) {
104*7f2fe78bSCy Schubert 			cmsg->rm_xid = IXDR_GET_LONG(buf);
105*7f2fe78bSCy Schubert 			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
106*7f2fe78bSCy Schubert 			if (cmsg->rm_direction != CALL) {
107*7f2fe78bSCy Schubert 				return (FALSE);
108*7f2fe78bSCy Schubert 			}
109*7f2fe78bSCy Schubert 			cmsg->rm_call.cb_rpcvers = IXDR_GET_LONG(buf);
110*7f2fe78bSCy Schubert 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
111*7f2fe78bSCy Schubert 				return (FALSE);
112*7f2fe78bSCy Schubert 			}
113*7f2fe78bSCy Schubert 			cmsg->rm_call.cb_prog = IXDR_GET_LONG(buf);
114*7f2fe78bSCy Schubert 			cmsg->rm_call.cb_vers = IXDR_GET_LONG(buf);
115*7f2fe78bSCy Schubert 			cmsg->rm_call.cb_proc = IXDR_GET_LONG(buf);
116*7f2fe78bSCy Schubert 			oa = &cmsg->rm_call.cb_cred;
117*7f2fe78bSCy Schubert 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
118*7f2fe78bSCy Schubert 			oa->oa_length = IXDR_GET_LONG(buf);
119*7f2fe78bSCy Schubert 			if (oa->oa_length) {
120*7f2fe78bSCy Schubert 				if (oa->oa_length > MAX_AUTH_BYTES) {
121*7f2fe78bSCy Schubert 					return (FALSE);
122*7f2fe78bSCy Schubert 				}
123*7f2fe78bSCy Schubert 				if (oa->oa_base == NULL) {
124*7f2fe78bSCy Schubert 					oa->oa_base = (caddr_t)
125*7f2fe78bSCy Schubert 						mem_alloc(oa->oa_length);
126*7f2fe78bSCy Schubert 				}
127*7f2fe78bSCy Schubert 				buf = (rpc_inline_t *)
128*7f2fe78bSCy Schubert 				   XDR_INLINE(xdrs, (int)RNDUP(oa->oa_length));
129*7f2fe78bSCy Schubert 				if (buf == NULL) {
130*7f2fe78bSCy Schubert 					if (xdr_opaque(xdrs, oa->oa_base,
131*7f2fe78bSCy Schubert 					    oa->oa_length) == FALSE) {
132*7f2fe78bSCy Schubert 						return (FALSE);
133*7f2fe78bSCy Schubert 					}
134*7f2fe78bSCy Schubert 				} else {
135*7f2fe78bSCy Schubert 					memmove(oa->oa_base, (caddr_t)buf,
136*7f2fe78bSCy Schubert 						oa->oa_length);
137*7f2fe78bSCy Schubert 					/* no real need....
138*7f2fe78bSCy Schubert 					buf += RNDUP(oa->oa_length) /
139*7f2fe78bSCy Schubert 						BYTES_PER_XDR_UNIT;
140*7f2fe78bSCy Schubert 					*/
141*7f2fe78bSCy Schubert 				}
142*7f2fe78bSCy Schubert 			}
143*7f2fe78bSCy Schubert 			oa = &cmsg->rm_call.cb_verf;
144*7f2fe78bSCy Schubert 			buf = (rpc_inline_t *)
145*7f2fe78bSCy Schubert 			   XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
146*7f2fe78bSCy Schubert 			if (buf == NULL) {
147*7f2fe78bSCy Schubert 				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
148*7f2fe78bSCy Schubert 				    xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
149*7f2fe78bSCy Schubert 					return (FALSE);
150*7f2fe78bSCy Schubert 				}
151*7f2fe78bSCy Schubert 			} else {
152*7f2fe78bSCy Schubert 				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
153*7f2fe78bSCy Schubert 				oa->oa_length = IXDR_GET_LONG(buf);
154*7f2fe78bSCy Schubert 			}
155*7f2fe78bSCy Schubert 			if (oa->oa_length) {
156*7f2fe78bSCy Schubert 				if (oa->oa_length > MAX_AUTH_BYTES) {
157*7f2fe78bSCy Schubert 					return (FALSE);
158*7f2fe78bSCy Schubert 				}
159*7f2fe78bSCy Schubert 				if (oa->oa_base == NULL) {
160*7f2fe78bSCy Schubert 					oa->oa_base = (caddr_t)
161*7f2fe78bSCy Schubert 						mem_alloc(oa->oa_length);
162*7f2fe78bSCy Schubert 				}
163*7f2fe78bSCy Schubert 				buf = (rpc_inline_t *)
164*7f2fe78bSCy Schubert 				   XDR_INLINE(xdrs, (int)RNDUP(oa->oa_length));
165*7f2fe78bSCy Schubert 				if (buf == NULL) {
166*7f2fe78bSCy Schubert 					if (xdr_opaque(xdrs, oa->oa_base,
167*7f2fe78bSCy Schubert 					    oa->oa_length) == FALSE) {
168*7f2fe78bSCy Schubert 						return (FALSE);
169*7f2fe78bSCy Schubert 					}
170*7f2fe78bSCy Schubert 				} else {
171*7f2fe78bSCy Schubert 					memmove(oa->oa_base, (caddr_t) buf,
172*7f2fe78bSCy Schubert 					    oa->oa_length);
173*7f2fe78bSCy Schubert 					/* no real need...
174*7f2fe78bSCy Schubert 					buf += RNDUP(oa->oa_length) /
175*7f2fe78bSCy Schubert 						BYTES_PER_XDR_UNIT;
176*7f2fe78bSCy Schubert 					*/
177*7f2fe78bSCy Schubert 				}
178*7f2fe78bSCy Schubert 			}
179*7f2fe78bSCy Schubert 			return (TRUE);
180*7f2fe78bSCy Schubert 		}
181*7f2fe78bSCy Schubert 	}
182*7f2fe78bSCy Schubert 	if (
183*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_xid)) &&
184*7f2fe78bSCy Schubert 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
185*7f2fe78bSCy Schubert 	    (cmsg->rm_direction == CALL) &&
186*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
187*7f2fe78bSCy Schubert 	    (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
188*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_call.cb_prog)) &&
189*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_call.cb_vers)) &&
190*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_call.cb_proc)) &&
191*7f2fe78bSCy Schubert 	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
192*7f2fe78bSCy Schubert 	    return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
193*7f2fe78bSCy Schubert 	return (FALSE);
194*7f2fe78bSCy Schubert }
195