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