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