xref: /dragonfly/lib/libc/rpc/rpc_callmsg.c (revision 36a3d1d6)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro
30  * @(#)rpc_callmsg.c	2.1 88/07/29 4.0 RPCSRC
31  * $NetBSD: rpc_callmsg.c,v 1.16 2000/07/14 08:40:42 fvdl Exp $
32  * $FreeBSD: src/lib/libc/rpc/rpc_callmsg.c,v 1.13 2007/11/20 01:51:20 jb Exp $
33  * $DragonFly: src/lib/libc/rpc/rpc_callmsg.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
34  */
35 
36 /*
37  * rpc_callmsg.c
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  *
41  */
42 
43 #include "namespace.h"
44 #include <assert.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #include <rpc/rpc.h>
49 #include "un-namespace.h"
50 
51 /*
52  * XDR a call message
53  */
54 bool_t
55 xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
56 {
57 	enum msg_type *prm_direction;
58 	int32_t *buf;
59 	struct opaque_auth *oa;
60 
61 	assert(xdrs != NULL);
62 	assert(cmsg != NULL);
63 
64 	if (xdrs->x_op == XDR_ENCODE) {
65 		if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
66 			return (FALSE);
67 		}
68 		if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
69 			return (FALSE);
70 		}
71 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
72 			+ RNDUP(cmsg->rm_call.cb_cred.oa_length)
73 			+ 2 * BYTES_PER_XDR_UNIT
74 			+ RNDUP(cmsg->rm_call.cb_verf.oa_length));
75 		if (buf != NULL) {
76 			IXDR_PUT_INT32(buf, cmsg->rm_xid);
77 			IXDR_PUT_ENUM(buf, cmsg->rm_direction);
78 			if (cmsg->rm_direction != CALL) {
79 				return (FALSE);
80 			}
81 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
82 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
83 				return (FALSE);
84 			}
85 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
86 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
87 			IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
88 			oa = &cmsg->rm_call.cb_cred;
89 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
90 			IXDR_PUT_INT32(buf, oa->oa_length);
91 			if (oa->oa_length) {
92 				memmove(buf, oa->oa_base, oa->oa_length);
93 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
94 			}
95 			oa = &cmsg->rm_call.cb_verf;
96 			IXDR_PUT_ENUM(buf, oa->oa_flavor);
97 			IXDR_PUT_INT32(buf, oa->oa_length);
98 			if (oa->oa_length) {
99 				memmove(buf, oa->oa_base, oa->oa_length);
100 				/* no real need....
101 				buf += RNDUP(oa->oa_length) / sizeof (int32_t);
102 				*/
103 			}
104 			return (TRUE);
105 		}
106 	}
107 	if (xdrs->x_op == XDR_DECODE) {
108 		buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
109 		if (buf != NULL) {
110 			cmsg->rm_xid = IXDR_GET_U_INT32(buf);
111 			cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
112 			if (cmsg->rm_direction != CALL) {
113 				return (FALSE);
114 			}
115 			cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
116 			if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
117 				return (FALSE);
118 			}
119 			cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
120 			cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
121 			cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
122 			oa = &cmsg->rm_call.cb_cred;
123 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
124 			oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
125 			if (oa->oa_length) {
126 				if (oa->oa_length > MAX_AUTH_BYTES) {
127 					return (FALSE);
128 				}
129 				if (oa->oa_base == NULL) {
130 					oa->oa_base = (caddr_t)
131 					    mem_alloc(oa->oa_length);
132 					if (oa->oa_base == NULL)
133 						return (FALSE);
134 				}
135 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
136 				if (buf == NULL) {
137 					if (xdr_opaque(xdrs, oa->oa_base,
138 					    oa->oa_length) == FALSE) {
139 						return (FALSE);
140 					}
141 				} else {
142 					memmove(oa->oa_base, buf,
143 					    oa->oa_length);
144 					/* no real need....
145 					buf += RNDUP(oa->oa_length) /
146 						sizeof (int32_t);
147 					*/
148 				}
149 			}
150 			oa = &cmsg->rm_call.cb_verf;
151 			buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
152 			if (buf == NULL) {
153 				if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
154 				    xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
155 					return (FALSE);
156 				}
157 			} else {
158 				oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
159 				oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
160 			}
161 			if (oa->oa_length) {
162 				if (oa->oa_length > MAX_AUTH_BYTES) {
163 					return (FALSE);
164 				}
165 				if (oa->oa_base == NULL) {
166 					oa->oa_base = (caddr_t)
167 					    mem_alloc(oa->oa_length);
168 					if (oa->oa_base == NULL)
169 						return (FALSE);
170 				}
171 				buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
172 				if (buf == NULL) {
173 					if (xdr_opaque(xdrs, oa->oa_base,
174 					    oa->oa_length) == FALSE) {
175 						return (FALSE);
176 					}
177 				} else {
178 					memmove(oa->oa_base, buf,
179 					    oa->oa_length);
180 					/* no real need...
181 					buf += RNDUP(oa->oa_length) /
182 						sizeof (int32_t);
183 					*/
184 				}
185 			}
186 			return (TRUE);
187 		}
188 	}
189 	prm_direction = &cmsg->rm_direction;
190 	if (
191 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
192 	    xdr_enum(xdrs, (enum_t *) prm_direction) &&
193 	    (cmsg->rm_direction == CALL) &&
194 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
195 	    (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
196 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) &&
197 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)) &&
198 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_proc)) &&
199 	    xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
200 		return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
201 	return (FALSE);
202 }
203