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