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