174462Salfred/*	$NetBSD: rpc_callmsg.c,v 1.16 2000/07/14 08:40:42 fvdl Exp $	*/
274462Salfred
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
68870Srgrimes *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
17258578Shrs *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * POSSIBILITY OF SUCH DAMAGE.
291901Swollman */
301901Swollman
311901Swollman#if defined(LIBC_SCCS) && !defined(lint)
32136581Sobrienstatic char *sccsid2 = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro";
3374462Salfredstatic char *sccsid = "@(#)rpc_callmsg.c	2.1 88/07/29 4.0 RPCSRC";
341901Swollman#endif
3592990Sobrien#include <sys/cdefs.h>
3692990Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/libc/rpc/rpc_callmsg.c 301769 2016-06-09 22:18:25Z pfg $");
371901Swollman
381901Swollman/*
391901Swollman * rpc_callmsg.c
401901Swollman *
411901Swollman * Copyright (C) 1984, Sun Microsystems, Inc.
421901Swollman *
431901Swollman */
441901Swollman
4574462Salfred#include "namespace.h"
4674462Salfred#include <assert.h>
4711666Sphk#include <stdlib.h>
4811666Sphk#include <string.h>
4974462Salfred
501901Swollman#include <rpc/rpc.h>
5174462Salfred#include "un-namespace.h"
521901Swollman
531901Swollman/*
541901Swollman * XDR a call message
551901Swollman */
561901Swollmanbool_t
57288113Srodrigcxdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
581901Swollman{
59173763Sjb	enum msg_type *prm_direction;
6074462Salfred	int32_t *buf;
6174462Salfred	struct opaque_auth *oa;
621901Swollman
6374462Salfred	assert(xdrs != NULL);
6474462Salfred	assert(cmsg != NULL);
6574462Salfred
661901Swollman	if (xdrs->x_op == XDR_ENCODE) {
671901Swollman		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
681901Swollman			return (FALSE);
691901Swollman		}
701901Swollman		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
711901Swollman			return (FALSE);
721901Swollman		}
731901Swollman		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
741901Swollman			+ RNDUP(cmsg->rm_call.cb_cred.oa_length)
751901Swollman			+ 2 * BYTES_PER_XDR_UNIT
761901Swollman			+ RNDUP(cmsg->rm_call.cb_verf.oa_length));
771901Swollman		if (buf != NULL) {
7874462Salfred			IXDR_PUT_INT32(buf, cmsg->rm_xid);
791901Swollman			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
801901Swollman			if (cmsg->rm_direction != CALL) {
811901Swollman				return (FALSE);
821901Swollman			}
8374462Salfred			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
841901Swollman			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
851901Swollman				return (FALSE);
861901Swollman			}
8774462Salfred			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
8874462Salfred			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
8974462Salfred			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
901901Swollman			oa = &cmsg->rm_call.cb_cred;
911901Swollman			IXDR_PUT_ENUM(buf, oa->oa_flavor);
9274462Salfred			IXDR_PUT_INT32(buf, oa->oa_length);
931901Swollman			if (oa->oa_length) {
9474462Salfred				memmove(buf, oa->oa_base, oa->oa_length);
9521082Speter				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
961901Swollman			}
971901Swollman			oa = &cmsg->rm_call.cb_verf;
981901Swollman			IXDR_PUT_ENUM(buf, oa->oa_flavor);
9974462Salfred			IXDR_PUT_INT32(buf, oa->oa_length);
1001901Swollman			if (oa->oa_length) {
10174462Salfred				memmove(buf, oa->oa_base, oa->oa_length);
1021901Swollman				/* no real need....
10321082Speter				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
1041901Swollman				*/
1051901Swollman			}
1061901Swollman			return (TRUE);
1071901Swollman		}
1081901Swollman	}
1091901Swollman	if (xdrs->x_op == XDR_DECODE) {
1101901Swollman		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
1111901Swollman		if (buf != NULL) {
11274462Salfred			cmsg->rm_xid = IXDR_GET_U_INT32(buf);
1131901Swollman			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
1141901Swollman			if (cmsg->rm_direction != CALL) {
1151901Swollman				return (FALSE);
1161901Swollman			}
11774462Salfred			cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
1181901Swollman			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
1191901Swollman				return (FALSE);
1201901Swollman			}
12174462Salfred			cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
12274462Salfred			cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
12374462Salfred			cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
1241901Swollman			oa = &cmsg->rm_call.cb_cred;
1251901Swollman			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
12674462Salfred			oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
1271901Swollman			if (oa->oa_length) {
1281901Swollman				if (oa->oa_length > MAX_AUTH_BYTES) {
1291901Swollman					return (FALSE);
1301901Swollman				}
1311901Swollman				if (oa->oa_base == NULL) {
1321901Swollman					oa->oa_base = (caddr_t)
13374462Salfred					    mem_alloc(oa->oa_length);
13474462Salfred					if (oa->oa_base == NULL)
13574462Salfred						return (FALSE);
1361901Swollman				}
1371901Swollman				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
1381901Swollman				if (buf == NULL) {
1391901Swollman					if (xdr_opaque(xdrs, oa->oa_base,
1401901Swollman					    oa->oa_length) == FALSE) {
1411901Swollman						return (FALSE);
1421901Swollman					}
1431901Swollman				} else {
14474462Salfred					memmove(oa->oa_base, buf,
1451901Swollman					    oa->oa_length);
1461901Swollman					/* no real need....
1471901Swollman					buf += RNDUP(oa->oa_length) /
14821082Speter						sizeof (int32_t);
1491901Swollman					*/
1501901Swollman				}
1511901Swollman			}
1521901Swollman			oa = &cmsg->rm_call.cb_verf;
1531901Swollman			buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
1541901Swollman			if (buf == NULL) {
1551901Swollman				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
1561901Swollman				    xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
1571901Swollman					return (FALSE);
1581901Swollman				}
1591901Swollman			} else {
1601901Swollman				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
16174462Salfred				oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
1621901Swollman			}
1631901Swollman			if (oa->oa_length) {
1641901Swollman				if (oa->oa_length > MAX_AUTH_BYTES) {
1651901Swollman					return (FALSE);
1661901Swollman				}
1671901Swollman				if (oa->oa_base == NULL) {
1681901Swollman					oa->oa_base = (caddr_t)
16974462Salfred					    mem_alloc(oa->oa_length);
17074462Salfred					if (oa->oa_base == NULL)
17174462Salfred						return (FALSE);
1721901Swollman				}
1731901Swollman				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
1741901Swollman				if (buf == NULL) {
1751901Swollman					if (xdr_opaque(xdrs, oa->oa_base,
1761901Swollman					    oa->oa_length) == FALSE) {
1771901Swollman						return (FALSE);
1781901Swollman					}
1791901Swollman				} else {
18074462Salfred					memmove(oa->oa_base, buf,
1811901Swollman					    oa->oa_length);
1821901Swollman					/* no real need...
1831901Swollman					buf += RNDUP(oa->oa_length) /
18421082Speter						sizeof (int32_t);
1851901Swollman					*/
1861901Swollman				}
1871901Swollman			}
1881901Swollman			return (TRUE);
1891901Swollman		}
1901901Swollman	}
191173763Sjb	prm_direction = &cmsg->rm_direction;
1921901Swollman	if (
19321082Speter	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
194173763Sjb	    xdr_enum(xdrs, (enum_t *) prm_direction) &&
1951901Swollman	    (cmsg->rm_direction == CALL) &&
196301769Spfg	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
1971901Swollman	    (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
198301769Spfg	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) &&
199301769Spfg	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)) &&
200301769Spfg	    xdr_rpcproc(xdrs, &(cmsg->rm_call.cb_proc)) &&
2011901Swollman	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
20274462Salfred		return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
2031901Swollman	return (FALSE);
2041901Swollman}
205