1 /*	$NetBSD: rpc_prot.c,v 1.21 2013/03/11 20:19:29 tron 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 <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: rpc_prot.c,v 1.21 2013/03/11 20:19:29 tron Exp $");
41 #endif
42 #endif
43 
44 /*
45  * rpc_prot.c
46  *
47  * Copyright (C) 1984, Sun Microsystems, Inc.
48  *
49  * This set of routines implements the rpc message definition,
50  * its serializer and some common rpc utility routines.
51  * The routines are meant for various implementations of rpc -
52  * they are NOT for the rpc client or rpc service implementations!
53  * Because authentication stuff is easy and is part of rpc, the opaque
54  * routines are also in this program.
55  */
56 
57 #include "namespace.h"
58 
59 #include <sys/param.h>
60 
61 #include <assert.h>
62 
63 #include <rpc/rpc.h>
64 
65 #ifdef __weak_alias
66 __weak_alias(xdr_accepted_reply,_xdr_accepted_reply)
67 __weak_alias(xdr_callhdr,_xdr_callhdr)
68 __weak_alias(xdr_des_block,_xdr_des_block)
69 __weak_alias(xdr_opaque_auth,_xdr_opaque_auth)
70 __weak_alias(xdr_rejected_reply,_xdr_rejected_reply)
71 __weak_alias(xdr_replymsg,_xdr_replymsg)
72 #endif
73 
74 static void accepted(enum accept_stat, struct rpc_err *);
75 static void rejected(enum reject_stat, struct rpc_err *);
76 
77 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
78 
79 /*
80  * XDR an opaque authentication struct
81  * (see auth.h)
82  */
83 bool_t
xdr_opaque_auth(XDR * xdrs,struct opaque_auth * ap)84 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
85 {
86 
87 	_DIAGASSERT(xdrs != NULL);
88 	_DIAGASSERT(ap != NULL);
89 
90 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
91 		return (xdr_bytes(xdrs, &ap->oa_base,
92 			&ap->oa_length, MAX_AUTH_BYTES));
93 	return (FALSE);
94 }
95 
96 /*
97  * XDR a DES block
98  */
99 bool_t
xdr_des_block(XDR * xdrs,des_block * blkp)100 xdr_des_block(XDR *xdrs, des_block *blkp)
101 {
102 
103 	_DIAGASSERT(xdrs != NULL);
104 	_DIAGASSERT(blkp != NULL);
105 
106 	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp,
107 	    (u_int)sizeof(des_block)));
108 }
109 
110 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
111 
112 /*
113  * XDR the MSG_ACCEPTED part of a reply message union
114  */
115 bool_t
xdr_accepted_reply(XDR * xdrs,struct accepted_reply * ar)116 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
117 {
118 
119 	_DIAGASSERT(xdrs != NULL);
120 	_DIAGASSERT(ar != NULL);
121 
122 	/* personalized union, rather than calling xdr_union */
123 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
124 		return (FALSE);
125 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(ar->ar_stat)))
126 		return (FALSE);
127 	switch (ar->ar_stat) {
128 
129 	case SUCCESS:
130 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
131 
132 	case PROG_MISMATCH:
133 		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
134 			return (FALSE);
135 		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
136 
137 	case GARBAGE_ARGS:
138 	case SYSTEM_ERR:
139 	case PROC_UNAVAIL:
140 	case PROG_UNAVAIL:
141 		break;
142 	}
143 	return (TRUE);  /* TRUE => open ended set of problems */
144 }
145 
146 /*
147  * XDR the MSG_DENIED part of a reply message union
148  */
149 bool_t
xdr_rejected_reply(XDR * xdrs,struct rejected_reply * rr)150 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
151 {
152 
153 	_DIAGASSERT(xdrs != NULL);
154 	_DIAGASSERT(rr != NULL);
155 
156 	/* personalized union, rather than calling xdr_union */
157 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_stat)))
158 		return (FALSE);
159 	switch (rr->rj_stat) {
160 
161 	case RPC_MISMATCH:
162 		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
163 			return (FALSE);
164 		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
165 
166 	case AUTH_ERROR:
167 		return (xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_why)));
168 	}
169 	/* NOTREACHED */
170 	return (FALSE);
171 }
172 
173 static const struct xdr_discrim reply_dscrm[3] = {
174 	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
175 	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
176 	{ __dontcare__, NULL_xdrproc_t } };
177 
178 /*
179  * XDR a reply message
180  */
181 bool_t
xdr_replymsg(XDR * xdrs,struct rpc_msg * rmsg)182 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
183 {
184 	_DIAGASSERT(xdrs != NULL);
185 	_DIAGASSERT(rmsg != NULL);
186 
187 	if (
188 	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
189 	    xdr_enum(xdrs, (enum_t *)(void *)&(rmsg->rm_direction)) &&
190 	    (rmsg->rm_direction == REPLY) )
191 		return (xdr_union(xdrs, (enum_t *)(void *)&(rmsg->rm_reply.rp_stat),
192 		   (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
193 		   NULL_xdrproc_t));
194 	return (FALSE);
195 }
196 
197 
198 /*
199  * Serializes the "static part" of a call message header.
200  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
201  * The rm_xid is not really static, but the user can easily munge on the fly.
202  */
203 bool_t
xdr_callhdr(XDR * xdrs,struct rpc_msg * cmsg)204 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
205 {
206 
207 	_DIAGASSERT(xdrs != NULL);
208 	_DIAGASSERT(cmsg != NULL);
209 
210 	cmsg->rm_direction = CALL;
211 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
212 	if (
213 	    (xdrs->x_op == XDR_ENCODE) &&
214 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
215 	    xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) &&
216 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
217 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
218 		return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
219 	return (FALSE);
220 }
221 
222 /* ************************** Client utility routine ************* */
223 
224 static void
accepted(enum accept_stat acpt_stat,struct rpc_err * error)225 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
226 {
227 
228 	_DIAGASSERT(error != NULL);
229 
230 	switch (acpt_stat) {
231 
232 	case PROG_UNAVAIL:
233 		error->re_status = RPC_PROGUNAVAIL;
234 		return;
235 
236 	case PROG_MISMATCH:
237 		error->re_status = RPC_PROGVERSMISMATCH;
238 		return;
239 
240 	case PROC_UNAVAIL:
241 		error->re_status = RPC_PROCUNAVAIL;
242 		return;
243 
244 	case GARBAGE_ARGS:
245 		error->re_status = RPC_CANTDECODEARGS;
246 		return;
247 
248 	case SYSTEM_ERR:
249 		error->re_status = RPC_SYSTEMERROR;
250 		return;
251 
252 	case SUCCESS:
253 		error->re_status = RPC_SUCCESS;
254 		return;
255 	}
256 	/* NOTREACHED */
257 	/* something's wrong, but we don't know what ... */
258 	error->re_status = RPC_FAILED;
259 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
260 	error->re_lb.s2 = (int32_t)acpt_stat;
261 }
262 
263 static void
rejected(enum reject_stat rjct_stat,struct rpc_err * error)264 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
265 {
266 
267 	_DIAGASSERT(error != NULL);
268 
269 	switch (rjct_stat) {
270 	case RPC_MISMATCH:
271 		error->re_status = RPC_VERSMISMATCH;
272 		return;
273 
274 	case AUTH_ERROR:
275 		error->re_status = RPC_AUTHERROR;
276 		return;
277 	}
278 	/* something's wrong, but we don't know what ... */
279 	/* NOTREACHED */
280 	error->re_status = RPC_FAILED;
281 	error->re_lb.s1 = (int32_t)MSG_DENIED;
282 	error->re_lb.s2 = (int32_t)rjct_stat;
283 }
284 
285 /*
286  * given a reply message, fills in the error
287  */
288 void
_seterr_reply(struct rpc_msg * msg,struct rpc_err * error)289 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
290 {
291 
292 	_DIAGASSERT(msg != NULL);
293 	_DIAGASSERT(error != NULL);
294 
295 	/* optimized for normal, SUCCESSful case */
296 	switch (msg->rm_reply.rp_stat) {
297 
298 	case MSG_ACCEPTED:
299 		if (msg->acpted_rply.ar_stat == SUCCESS) {
300 			error->re_status = RPC_SUCCESS;
301 			return;
302 		}
303 		accepted(msg->acpted_rply.ar_stat, error);
304 		break;
305 
306 	case MSG_DENIED:
307 		rejected(msg->rjcted_rply.rj_stat, error);
308 		break;
309 
310 	default:
311 		error->re_status = RPC_FAILED;
312 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
313 		break;
314 	}
315 	switch (error->re_status) {
316 
317 	case RPC_VERSMISMATCH:
318 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
319 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
320 		break;
321 
322 	case RPC_AUTHERROR:
323 		error->re_why = msg->rjcted_rply.rj_why;
324 		break;
325 
326 	case RPC_PROGVERSMISMATCH:
327 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
328 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
329 		break;
330 
331 	case RPC_FAILED:
332 	case RPC_SUCCESS:
333 	case RPC_PROGNOTREGISTERED:
334 	case RPC_PMAPFAILURE:
335 	case RPC_UNKNOWNPROTO:
336 	case RPC_UNKNOWNHOST:
337 	case RPC_SYSTEMERROR:
338 	case RPC_CANTDECODEARGS:
339 	case RPC_PROCUNAVAIL:
340 	case RPC_PROGUNAVAIL:
341 	case RPC_TIMEDOUT:
342 	case RPC_CANTRECV:
343 	case RPC_CANTSEND:
344 	case RPC_CANTDECODERES:
345 	case RPC_CANTENCODEARGS:
346 	default:
347 		break;
348 	}
349 }
350