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