18360efbdSAlfred Perlstein /* $NetBSD: rpc_callmsg.c,v 1.16 2000/07/14 08:40:42 fvdl Exp $ */
28360efbdSAlfred Perlstein
32e322d37SHiroki Sato /*-
4*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
5*8a16b7a1SPedro F. Giffuni *
62e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc.
72e322d37SHiroki Sato * All rights reserved.
899064799SGarrett Wollman *
92e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without
102e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met:
112e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice,
122e322d37SHiroki Sato * this list of conditions and the following disclaimer.
132e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice,
142e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation
152e322d37SHiroki Sato * and/or other materials provided with the distribution.
162e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its
172e322d37SHiroki Sato * contributors may be used to endorse or promote products derived
182e322d37SHiroki Sato * from this software without specific prior written permission.
1999064799SGarrett Wollman *
202e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
212e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
222e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
232e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
242e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE.
3199064799SGarrett Wollman */
3299064799SGarrett Wollman
3399064799SGarrett Wollman /*
3499064799SGarrett Wollman * rpc_callmsg.c
3599064799SGarrett Wollman *
3699064799SGarrett Wollman * Copyright (C) 1984, Sun Microsystems, Inc.
3799064799SGarrett Wollman *
3899064799SGarrett Wollman */
3999064799SGarrett Wollman
408360efbdSAlfred Perlstein #include "namespace.h"
418360efbdSAlfred Perlstein #include <assert.h>
424c3af266SPoul-Henning Kamp #include <stdlib.h>
434c3af266SPoul-Henning Kamp #include <string.h>
448360efbdSAlfred Perlstein
4599064799SGarrett Wollman #include <rpc/rpc.h>
468360efbdSAlfred Perlstein #include "un-namespace.h"
4799064799SGarrett Wollman
4899064799SGarrett Wollman /*
4999064799SGarrett Wollman * XDR a call message
5099064799SGarrett Wollman */
5199064799SGarrett Wollman bool_t
xdr_callmsg(XDR * xdrs,struct rpc_msg * cmsg)52587cf682SCraig Rodrigues xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
5399064799SGarrett Wollman {
54102c7c92SJohn Birrell enum msg_type *prm_direction;
558360efbdSAlfred Perlstein int32_t *buf;
568360efbdSAlfred Perlstein struct opaque_auth *oa;
578360efbdSAlfred Perlstein
588360efbdSAlfred Perlstein assert(xdrs != NULL);
598360efbdSAlfred Perlstein assert(cmsg != NULL);
6099064799SGarrett Wollman
6199064799SGarrett Wollman if (xdrs->x_op == XDR_ENCODE) {
6299064799SGarrett Wollman if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
6399064799SGarrett Wollman return (FALSE);
6499064799SGarrett Wollman }
6599064799SGarrett Wollman if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
6699064799SGarrett Wollman return (FALSE);
6799064799SGarrett Wollman }
6899064799SGarrett Wollman buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
6999064799SGarrett Wollman + RNDUP(cmsg->rm_call.cb_cred.oa_length)
7099064799SGarrett Wollman + 2 * BYTES_PER_XDR_UNIT
7199064799SGarrett Wollman + RNDUP(cmsg->rm_call.cb_verf.oa_length));
7299064799SGarrett Wollman if (buf != NULL) {
738360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, cmsg->rm_xid);
7499064799SGarrett Wollman IXDR_PUT_ENUM(buf, cmsg->rm_direction);
7599064799SGarrett Wollman if (cmsg->rm_direction != CALL) {
7699064799SGarrett Wollman return (FALSE);
7799064799SGarrett Wollman }
788360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
7999064799SGarrett Wollman if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
8099064799SGarrett Wollman return (FALSE);
8199064799SGarrett Wollman }
828360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
838360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
848360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
8599064799SGarrett Wollman oa = &cmsg->rm_call.cb_cred;
8699064799SGarrett Wollman IXDR_PUT_ENUM(buf, oa->oa_flavor);
878360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, oa->oa_length);
8899064799SGarrett Wollman if (oa->oa_length) {
898360efbdSAlfred Perlstein memmove(buf, oa->oa_base, oa->oa_length);
90ec3ca1a2SPeter Wemm buf += RNDUP(oa->oa_length) / sizeof (int32_t);
9199064799SGarrett Wollman }
9299064799SGarrett Wollman oa = &cmsg->rm_call.cb_verf;
9399064799SGarrett Wollman IXDR_PUT_ENUM(buf, oa->oa_flavor);
948360efbdSAlfred Perlstein IXDR_PUT_INT32(buf, oa->oa_length);
9599064799SGarrett Wollman if (oa->oa_length) {
968360efbdSAlfred Perlstein memmove(buf, oa->oa_base, oa->oa_length);
9799064799SGarrett Wollman /* no real need....
98ec3ca1a2SPeter Wemm buf += RNDUP(oa->oa_length) / sizeof (int32_t);
9999064799SGarrett Wollman */
10099064799SGarrett Wollman }
10199064799SGarrett Wollman return (TRUE);
10299064799SGarrett Wollman }
10399064799SGarrett Wollman }
10499064799SGarrett Wollman if (xdrs->x_op == XDR_DECODE) {
10599064799SGarrett Wollman buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
10699064799SGarrett Wollman if (buf != NULL) {
1078360efbdSAlfred Perlstein cmsg->rm_xid = IXDR_GET_U_INT32(buf);
10899064799SGarrett Wollman cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
10999064799SGarrett Wollman if (cmsg->rm_direction != CALL) {
11099064799SGarrett Wollman return (FALSE);
11199064799SGarrett Wollman }
1128360efbdSAlfred Perlstein cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
11399064799SGarrett Wollman if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
11499064799SGarrett Wollman return (FALSE);
11599064799SGarrett Wollman }
1168360efbdSAlfred Perlstein cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
1178360efbdSAlfred Perlstein cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
1188360efbdSAlfred Perlstein cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
11999064799SGarrett Wollman oa = &cmsg->rm_call.cb_cred;
12099064799SGarrett Wollman oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
1218360efbdSAlfred Perlstein oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
12299064799SGarrett Wollman if (oa->oa_length) {
12399064799SGarrett Wollman if (oa->oa_length > MAX_AUTH_BYTES) {
12499064799SGarrett Wollman return (FALSE);
12599064799SGarrett Wollman }
12699064799SGarrett Wollman if (oa->oa_base == NULL) {
12799064799SGarrett Wollman oa->oa_base = (caddr_t)
12899064799SGarrett Wollman mem_alloc(oa->oa_length);
1298360efbdSAlfred Perlstein if (oa->oa_base == NULL)
1308360efbdSAlfred Perlstein return (FALSE);
13199064799SGarrett Wollman }
13299064799SGarrett Wollman buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
13399064799SGarrett Wollman if (buf == NULL) {
13499064799SGarrett Wollman if (xdr_opaque(xdrs, oa->oa_base,
13599064799SGarrett Wollman oa->oa_length) == FALSE) {
13699064799SGarrett Wollman return (FALSE);
13799064799SGarrett Wollman }
13899064799SGarrett Wollman } else {
1398360efbdSAlfred Perlstein memmove(oa->oa_base, buf,
14099064799SGarrett Wollman oa->oa_length);
14199064799SGarrett Wollman /* no real need....
14299064799SGarrett Wollman buf += RNDUP(oa->oa_length) /
143ec3ca1a2SPeter Wemm sizeof (int32_t);
14499064799SGarrett Wollman */
14599064799SGarrett Wollman }
14699064799SGarrett Wollman }
14799064799SGarrett Wollman oa = &cmsg->rm_call.cb_verf;
14899064799SGarrett Wollman buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
14999064799SGarrett Wollman if (buf == NULL) {
15099064799SGarrett Wollman if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
15199064799SGarrett Wollman xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
15299064799SGarrett Wollman return (FALSE);
15399064799SGarrett Wollman }
15499064799SGarrett Wollman } else {
15599064799SGarrett Wollman oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
1568360efbdSAlfred Perlstein oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
15799064799SGarrett Wollman }
15899064799SGarrett Wollman if (oa->oa_length) {
15999064799SGarrett Wollman if (oa->oa_length > MAX_AUTH_BYTES) {
16099064799SGarrett Wollman return (FALSE);
16199064799SGarrett Wollman }
16299064799SGarrett Wollman if (oa->oa_base == NULL) {
16399064799SGarrett Wollman oa->oa_base = (caddr_t)
16499064799SGarrett Wollman mem_alloc(oa->oa_length);
1658360efbdSAlfred Perlstein if (oa->oa_base == NULL)
1668360efbdSAlfred Perlstein return (FALSE);
16799064799SGarrett Wollman }
16899064799SGarrett Wollman buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
16999064799SGarrett Wollman if (buf == NULL) {
17099064799SGarrett Wollman if (xdr_opaque(xdrs, oa->oa_base,
17199064799SGarrett Wollman oa->oa_length) == FALSE) {
17299064799SGarrett Wollman return (FALSE);
17399064799SGarrett Wollman }
17499064799SGarrett Wollman } else {
1758360efbdSAlfred Perlstein memmove(oa->oa_base, buf,
17699064799SGarrett Wollman oa->oa_length);
17799064799SGarrett Wollman /* no real need...
17899064799SGarrett Wollman buf += RNDUP(oa->oa_length) /
179ec3ca1a2SPeter Wemm sizeof (int32_t);
18099064799SGarrett Wollman */
18199064799SGarrett Wollman }
18299064799SGarrett Wollman }
18399064799SGarrett Wollman return (TRUE);
18499064799SGarrett Wollman }
18599064799SGarrett Wollman }
186102c7c92SJohn Birrell prm_direction = &cmsg->rm_direction;
18799064799SGarrett Wollman if (
188ec3ca1a2SPeter Wemm xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
189102c7c92SJohn Birrell xdr_enum(xdrs, (enum_t *) prm_direction) &&
19099064799SGarrett Wollman (cmsg->rm_direction == CALL) &&
191478eb9e9SPedro F. Giffuni xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
19299064799SGarrett Wollman (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
193478eb9e9SPedro F. Giffuni xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) &&
194478eb9e9SPedro F. Giffuni xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)) &&
195478eb9e9SPedro F. Giffuni xdr_rpcproc(xdrs, &(cmsg->rm_call.cb_proc)) &&
19699064799SGarrett Wollman xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
19799064799SGarrett Wollman return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
19899064799SGarrett Wollman return (FALSE);
19999064799SGarrett Wollman }
200