xref: /freebsd/lib/libc/rpc/rpc_callmsg.c (revision 32c7dde816fd1d738a48af82bf490307cb7b4739)
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 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 /*
41  * rpc_callmsg.c
42  *
43  * Copyright (C) 1984, Sun Microsystems, Inc.
44  *
45  */
46 
47 #include "namespace.h"
48 #include <assert.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include <rpc/rpc.h>
53 #include "un-namespace.h"
54 
55 /*
56  * XDR a call message
57  */
58 bool_t
59 xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
60 {
61 	enum msg_type *prm_direction;
62 	int32_t *buf;
63 	struct opaque_auth *oa;
64 
65 	assert(xdrs != NULL);
66 	assert(cmsg != NULL);
67 
68 	if (xdrs->x_op == XDR_ENCODE) {
69 		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
70 			return (FALSE);
71 		}
72 		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
73 			return (FALSE);
74 		}
75 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
76 			+ RNDUP(cmsg->rm_call.cb_cred.oa_length)
77 			+ 2 * BYTES_PER_XDR_UNIT
78 			+ RNDUP(cmsg->rm_call.cb_verf.oa_length));
79 		if (buf != NULL) {
80 			IXDR_PUT_INT32(buf, cmsg->rm_xid);
81 			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
82 			if (cmsg->rm_direction != CALL) {
83 				return (FALSE);
84 			}
85 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
86 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
87 				return (FALSE);
88 			}
89 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
90 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
91 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
92 			oa = &cmsg->rm_call.cb_cred;
93 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
94 			IXDR_PUT_INT32(buf, oa->oa_length);
95 			if (oa->oa_length) {
96 				memmove(buf, oa->oa_base, oa->oa_length);
97 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
98 			}
99 			oa = &cmsg->rm_call.cb_verf;
100 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
101 			IXDR_PUT_INT32(buf, oa->oa_length);
102 			if (oa->oa_length) {
103 				memmove(buf, oa->oa_base, oa->oa_length);
104 				/* no real need....
105 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
106 				*/
107 			}
108 			return (TRUE);
109 		}
110 	}
111 	if (xdrs->x_op == XDR_DECODE) {
112 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
113 		if (buf != NULL) {
114 			cmsg->rm_xid = IXDR_GET_U_INT32(buf);
115 			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
116 			if (cmsg->rm_direction != CALL) {
117 				return (FALSE);
118 			}
119 			cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
120 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
121 				return (FALSE);
122 			}
123 			cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
124 			cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
125 			cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
126 			oa = &cmsg->rm_call.cb_cred;
127 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
128 			oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
129 			if (oa->oa_length) {
130 				if (oa->oa_length > MAX_AUTH_BYTES) {
131 					return (FALSE);
132 				}
133 				if (oa->oa_base == NULL) {
134 					oa->oa_base = (caddr_t)
135 					    mem_alloc(oa->oa_length);
136 					if (oa->oa_base == NULL)
137 						return (FALSE);
138 				}
139 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
140 				if (buf == NULL) {
141 					if (xdr_opaque(xdrs, oa->oa_base,
142 					    oa->oa_length) == FALSE) {
143 						return (FALSE);
144 					}
145 				} else {
146 					memmove(oa->oa_base, buf,
147 					    oa->oa_length);
148 					/* no real need....
149 					buf += RNDUP(oa->oa_length) /
150 						sizeof (int32_t);
151 					*/
152 				}
153 			}
154 			oa = &cmsg->rm_call.cb_verf;
155 			buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
156 			if (buf == NULL) {
157 				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
158 				    xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
159 					return (FALSE);
160 				}
161 			} else {
162 				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
163 				oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
164 			}
165 			if (oa->oa_length) {
166 				if (oa->oa_length > MAX_AUTH_BYTES) {
167 					return (FALSE);
168 				}
169 				if (oa->oa_base == NULL) {
170 					oa->oa_base = (caddr_t)
171 					    mem_alloc(oa->oa_length);
172 					if (oa->oa_base == NULL)
173 						return (FALSE);
174 				}
175 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
176 				if (buf == NULL) {
177 					if (xdr_opaque(xdrs, oa->oa_base,
178 					    oa->oa_length) == FALSE) {
179 						return (FALSE);
180 					}
181 				} else {
182 					memmove(oa->oa_base, buf,
183 					    oa->oa_length);
184 					/* no real need...
185 					buf += RNDUP(oa->oa_length) /
186 						sizeof (int32_t);
187 					*/
188 				}
189 			}
190 			return (TRUE);
191 		}
192 	}
193 	prm_direction = &cmsg->rm_direction;
194 	if (
195 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
196 	    xdr_enum(xdrs, (enum_t *) prm_direction) &&
197 	    (cmsg->rm_direction == CALL) &&
198 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
199 	    (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
200 	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) &&
201 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)) &&
202 	    xdr_rpcproc(xdrs, &(cmsg->rm_call.cb_proc)) &&
203 	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
204 		return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
205 	return (FALSE);
206 }
207