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