xref: /minix3/lib/libc/rpc/rpc_callmsg.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: rpc_callmsg.c,v 1.20 2013/03/11 20:19:29 tron Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
6*84d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
10*84d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc  *       provided with the distribution.
16*84d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
20*84d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)rpc_callmsg.c	2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: rpc_callmsg.c,v 1.20 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras 
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras  * rpc_callmsg.c
462fe8fb19SBen Gras  *
472fe8fb19SBen Gras  * Copyright (C) 1984, Sun Microsystems, Inc.
482fe8fb19SBen Gras  *
492fe8fb19SBen Gras  */
502fe8fb19SBen Gras 
512fe8fb19SBen Gras #include "namespace.h"
522fe8fb19SBen Gras 
532fe8fb19SBen Gras #include <sys/param.h>
542fe8fb19SBen Gras 
552fe8fb19SBen Gras #include <assert.h>
562fe8fb19SBen Gras #include <stdlib.h>
572fe8fb19SBen Gras #include <string.h>
582fe8fb19SBen Gras 
592fe8fb19SBen Gras #include <rpc/rpc.h>
602fe8fb19SBen Gras 
612fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(xdr_callmsg,_xdr_callmsg)622fe8fb19SBen Gras __weak_alias(xdr_callmsg,_xdr_callmsg)
632fe8fb19SBen Gras #endif
642fe8fb19SBen Gras 
652fe8fb19SBen Gras /*
662fe8fb19SBen Gras  * XDR a call message
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras bool_t
69f14fb602SLionel Sambuc xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
702fe8fb19SBen Gras {
712fe8fb19SBen Gras 	int32_t *buf;
722fe8fb19SBen Gras 	struct opaque_auth *oa;
732fe8fb19SBen Gras 
742fe8fb19SBen Gras 	_DIAGASSERT(xdrs != NULL);
752fe8fb19SBen Gras 	_DIAGASSERT(cmsg != NULL);
762fe8fb19SBen Gras 
772fe8fb19SBen Gras 	if (xdrs->x_op == XDR_ENCODE) {
782fe8fb19SBen Gras 		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
792fe8fb19SBen Gras 			return (FALSE);
802fe8fb19SBen Gras 		}
812fe8fb19SBen Gras 		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
822fe8fb19SBen Gras 			return (FALSE);
832fe8fb19SBen Gras 		}
842fe8fb19SBen Gras 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
852fe8fb19SBen Gras 			+ RNDUP(cmsg->rm_call.cb_cred.oa_length)
862fe8fb19SBen Gras 			+ 2 * BYTES_PER_XDR_UNIT
872fe8fb19SBen Gras 			+ RNDUP(cmsg->rm_call.cb_verf.oa_length));
882fe8fb19SBen Gras 		if (buf != NULL) {
892fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, cmsg->rm_xid);
902fe8fb19SBen Gras 			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
912fe8fb19SBen Gras 			if (cmsg->rm_direction != CALL) {
922fe8fb19SBen Gras 				return (FALSE);
932fe8fb19SBen Gras 			}
942fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
952fe8fb19SBen Gras 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
962fe8fb19SBen Gras 				return (FALSE);
972fe8fb19SBen Gras 			}
982fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
992fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
1002fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
1012fe8fb19SBen Gras 			oa = &cmsg->rm_call.cb_cred;
1022fe8fb19SBen Gras 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
1032fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, oa->oa_length);
1042fe8fb19SBen Gras 			if (oa->oa_length) {
1052fe8fb19SBen Gras 				memmove(buf, oa->oa_base, oa->oa_length);
1062fe8fb19SBen Gras 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
1072fe8fb19SBen Gras 			}
1082fe8fb19SBen Gras 			oa = &cmsg->rm_call.cb_verf;
1092fe8fb19SBen Gras 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
1102fe8fb19SBen Gras 			IXDR_PUT_INT32(buf, oa->oa_length);
1112fe8fb19SBen Gras 			if (oa->oa_length) {
1122fe8fb19SBen Gras 				memmove(buf, oa->oa_base, oa->oa_length);
1132fe8fb19SBen Gras 				/* no real need....
1142fe8fb19SBen Gras 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
1152fe8fb19SBen Gras 				*/
1162fe8fb19SBen Gras 			}
1172fe8fb19SBen Gras 			return (TRUE);
1182fe8fb19SBen Gras 		}
1192fe8fb19SBen Gras 	}
1202fe8fb19SBen Gras 	if (xdrs->x_op == XDR_DECODE) {
1212fe8fb19SBen Gras 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
1222fe8fb19SBen Gras 		if (buf != NULL) {
1232fe8fb19SBen Gras 			cmsg->rm_xid = IXDR_GET_U_INT32(buf);
1242fe8fb19SBen Gras 			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
1252fe8fb19SBen Gras 			if (cmsg->rm_direction != CALL) {
1262fe8fb19SBen Gras 				return (FALSE);
1272fe8fb19SBen Gras 			}
1282fe8fb19SBen Gras 			cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
1292fe8fb19SBen Gras 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
1302fe8fb19SBen Gras 				return (FALSE);
1312fe8fb19SBen Gras 			}
1322fe8fb19SBen Gras 			cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
1332fe8fb19SBen Gras 			cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
1342fe8fb19SBen Gras 			cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
1352fe8fb19SBen Gras 			oa = &cmsg->rm_call.cb_cred;
1362fe8fb19SBen Gras 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
1372fe8fb19SBen Gras 			oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
1382fe8fb19SBen Gras 			if (oa->oa_length) {
1392fe8fb19SBen Gras 				if (oa->oa_length > MAX_AUTH_BYTES) {
1402fe8fb19SBen Gras 					return (FALSE);
1412fe8fb19SBen Gras 				}
1422fe8fb19SBen Gras 				if (oa->oa_base == NULL) {
1432fe8fb19SBen Gras 					oa->oa_base = mem_alloc(oa->oa_length);
1442fe8fb19SBen Gras 					if (oa->oa_base == NULL)
1452fe8fb19SBen Gras 						return (FALSE);
1462fe8fb19SBen Gras 				}
1472fe8fb19SBen Gras 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
1482fe8fb19SBen Gras 				if (buf == NULL) {
1492fe8fb19SBen Gras 					if (xdr_opaque(xdrs, oa->oa_base,
1502fe8fb19SBen Gras 					    oa->oa_length) == FALSE) {
1512fe8fb19SBen Gras 						return (FALSE);
1522fe8fb19SBen Gras 					}
1532fe8fb19SBen Gras 				} else {
1542fe8fb19SBen Gras 					memmove(oa->oa_base, buf,
1552fe8fb19SBen Gras 					    oa->oa_length);
1562fe8fb19SBen Gras 					/* no real need....
1572fe8fb19SBen Gras 					buf += RNDUP(oa->oa_length) /
1582fe8fb19SBen Gras 						sizeof (int32_t);
1592fe8fb19SBen Gras 					*/
1602fe8fb19SBen Gras 				}
1612fe8fb19SBen Gras 			}
1622fe8fb19SBen Gras 			oa = &cmsg->rm_call.cb_verf;
1632fe8fb19SBen Gras 			buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
1642fe8fb19SBen Gras 			if (buf == NULL) {
1652fe8fb19SBen Gras 				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
1662fe8fb19SBen Gras 				    xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
1672fe8fb19SBen Gras 					return (FALSE);
1682fe8fb19SBen Gras 				}
1692fe8fb19SBen Gras 			} else {
1702fe8fb19SBen Gras 				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
1712fe8fb19SBen Gras 				oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
1722fe8fb19SBen Gras 			}
1732fe8fb19SBen Gras 			if (oa->oa_length) {
1742fe8fb19SBen Gras 				if (oa->oa_length > MAX_AUTH_BYTES) {
1752fe8fb19SBen Gras 					return (FALSE);
1762fe8fb19SBen Gras 				}
1772fe8fb19SBen Gras 				if (oa->oa_base == NULL) {
1782fe8fb19SBen Gras 					oa->oa_base = mem_alloc(oa->oa_length);
1792fe8fb19SBen Gras 					if (oa->oa_base == NULL)
1802fe8fb19SBen Gras 						return (FALSE);
1812fe8fb19SBen Gras 				}
1822fe8fb19SBen Gras 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
1832fe8fb19SBen Gras 				if (buf == NULL) {
1842fe8fb19SBen Gras 					if (xdr_opaque(xdrs, oa->oa_base,
1852fe8fb19SBen Gras 					    oa->oa_length) == FALSE) {
1862fe8fb19SBen Gras 						return (FALSE);
1872fe8fb19SBen Gras 					}
1882fe8fb19SBen Gras 				} else {
1892fe8fb19SBen Gras 					memmove(oa->oa_base, buf,
1902fe8fb19SBen Gras 					    oa->oa_length);
1912fe8fb19SBen Gras 					/* no real need...
1922fe8fb19SBen Gras 					buf += RNDUP(oa->oa_length) /
1932fe8fb19SBen Gras 						sizeof (int32_t);
1942fe8fb19SBen Gras 					*/
1952fe8fb19SBen Gras 				}
1962fe8fb19SBen Gras 			}
1972fe8fb19SBen Gras 			return (TRUE);
1982fe8fb19SBen Gras 		}
1992fe8fb19SBen Gras 	}
2002fe8fb19SBen Gras 	if (
2012fe8fb19SBen Gras 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
2022fe8fb19SBen Gras 	    xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) &&
2032fe8fb19SBen Gras 	    (cmsg->rm_direction == CALL) &&
2042fe8fb19SBen Gras 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
2052fe8fb19SBen Gras 	    (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
2062fe8fb19SBen Gras 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) &&
2072fe8fb19SBen Gras 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)) &&
2082fe8fb19SBen Gras 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_proc)) &&
2092fe8fb19SBen Gras 	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
2102fe8fb19SBen Gras 		return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
2112fe8fb19SBen Gras 	return (FALSE);
2122fe8fb19SBen Gras }
213