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