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