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