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