xref: /freebsd/lib/libc/rpc/rpc_prot.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /*	$NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009, Sun Microsystems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * - Redistributions of source code must retain the above copyright notice,
10  *   this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  *   this list of conditions and the following disclaimer in the documentation
13  *   and/or other materials provided with the distribution.
14  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15  *   contributors may be used to endorse or promote products derived
16  *   from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char *sccsid2 = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
33 static char *sccsid = "@(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * rpc_prot.c
40  *
41  * Copyright (C) 1984, Sun Microsystems, Inc.
42  *
43  * This set of routines implements the rpc message definition,
44  * its serializer and some common rpc utility routines.
45  * The routines are meant for various implementations of rpc -
46  * they are NOT for the rpc client or rpc service implementations!
47  * Because authentication stuff is easy and is part of rpc, the opaque
48  * routines are also in this program.
49  */
50 
51 #include "namespace.h"
52 #include <sys/param.h>
53 
54 #include <assert.h>
55 
56 #include <rpc/rpc.h>
57 #include "un-namespace.h"
58 
59 static void accepted(enum accept_stat, struct rpc_err *);
60 static void rejected(enum reject_stat, struct rpc_err *);
61 
62 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
63 
64 extern struct opaque_auth _null_auth;
65 
66 /*
67  * XDR an opaque authentication struct
68  * (see auth.h)
69  */
70 bool_t
71 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
72 {
73 
74 	assert(xdrs != NULL);
75 	assert(ap != NULL);
76 
77 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
78 		return (xdr_bytes(xdrs, &ap->oa_base,
79 			&ap->oa_length, MAX_AUTH_BYTES));
80 	return (FALSE);
81 }
82 
83 /*
84  * XDR a DES block
85  */
86 bool_t
87 xdr_des_block(XDR *xdrs, des_block *blkp)
88 {
89 
90 	assert(xdrs != NULL);
91 	assert(blkp != NULL);
92 
93 	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
94 }
95 
96 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
97 
98 /*
99  * XDR the MSG_ACCEPTED part of a reply message union
100  */
101 bool_t
102 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
103 {
104 	enum accept_stat *par_stat;
105 
106 	assert(xdrs != NULL);
107 	assert(ar != NULL);
108 
109 	par_stat = &ar->ar_stat;
110 
111 	/* personalized union, rather than calling xdr_union */
112 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
113 		return (FALSE);
114 	if (! xdr_enum(xdrs, (enum_t *) par_stat))
115 		return (FALSE);
116 	switch (ar->ar_stat) {
117 
118 	case SUCCESS:
119 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
120 
121 	case PROG_MISMATCH:
122 		if (!xdr_rpcvers(xdrs, &(ar->ar_vers.low)))
123 			return (FALSE);
124 		return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));
125 
126 	case GARBAGE_ARGS:
127 	case SYSTEM_ERR:
128 	case PROC_UNAVAIL:
129 	case PROG_UNAVAIL:
130 		break;
131 	}
132 	return (TRUE);  /* TRUE => open ended set of problems */
133 }
134 
135 /*
136  * XDR the MSG_DENIED part of a reply message union
137  */
138 bool_t
139 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
140 {
141 	enum reject_stat *prj_stat;
142 	enum auth_stat *prj_why;
143 
144 	assert(xdrs != NULL);
145 	assert(rr != NULL);
146 
147 	prj_stat = &rr->rj_stat;
148 
149 	/* personalized union, rather than calling xdr_union */
150 	if (! xdr_enum(xdrs, (enum_t *) prj_stat))
151 		return (FALSE);
152 	switch (rr->rj_stat) {
153 
154 	case RPC_MISMATCH:
155 		if (! xdr_rpcvers(xdrs, &(rr->rj_vers.low)))
156 			return (FALSE);
157 		return (xdr_rpcvers(xdrs, &(rr->rj_vers.high)));
158 
159 	case AUTH_ERROR:
160 		prj_why = &rr->rj_why;
161 		return (xdr_enum(xdrs, (enum_t *) prj_why));
162 	}
163 	/* NOTREACHED */
164 	assert(0);
165 	return (FALSE);
166 }
167 
168 static const struct xdr_discrim reply_dscrm[3] = {
169 	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
170 	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
171 	{ __dontcare__, NULL_xdrproc_t } };
172 
173 /*
174  * XDR a reply message
175  */
176 bool_t
177 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
178 {
179 	enum msg_type *prm_direction;
180 	enum reply_stat *prp_stat;
181 
182 	assert(xdrs != NULL);
183 	assert(rmsg != NULL);
184 
185 	prm_direction = &rmsg->rm_direction;
186 	prp_stat = &rmsg->rm_reply.rp_stat;
187 
188 	if (
189 	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
190 	    xdr_enum(xdrs, (enum_t *) prm_direction) &&
191 	    (rmsg->rm_direction == REPLY) )
192 		return (xdr_union(xdrs, (enum_t *) prp_stat,
193 		   (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
194 		   NULL_xdrproc_t));
195 	return (FALSE);
196 }
197 
198 
199 /*
200  * Serializes the "static part" of a call message header.
201  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
202  * The rm_xid is not really static, but the user can easily munge on the fly.
203  */
204 bool_t
205 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
206 {
207 	enum msg_type *prm_direction;
208 
209 	assert(xdrs != NULL);
210 	assert(cmsg != NULL);
211 
212 	prm_direction = &cmsg->rm_direction;
213 
214 	cmsg->rm_direction = CALL;
215 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
216 	if (
217 	    (xdrs->x_op == XDR_ENCODE) &&
218 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
219 	    xdr_enum(xdrs, (enum_t *) prm_direction) &&
220 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
221 	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) )
222 		return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
223 	return (FALSE);
224 }
225 
226 /* ************************** Client utility routine ************* */
227 
228 static void
229 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
230 {
231 
232 	assert(error != NULL);
233 
234 	switch (acpt_stat) {
235 
236 	case PROG_UNAVAIL:
237 		error->re_status = RPC_PROGUNAVAIL;
238 		return;
239 
240 	case PROG_MISMATCH:
241 		error->re_status = RPC_PROGVERSMISMATCH;
242 		return;
243 
244 	case PROC_UNAVAIL:
245 		error->re_status = RPC_PROCUNAVAIL;
246 		return;
247 
248 	case GARBAGE_ARGS:
249 		error->re_status = RPC_CANTDECODEARGS;
250 		return;
251 
252 	case SYSTEM_ERR:
253 		error->re_status = RPC_SYSTEMERROR;
254 		return;
255 
256 	case SUCCESS:
257 		error->re_status = RPC_SUCCESS;
258 		return;
259 	}
260 	/* NOTREACHED */
261 	/* something's wrong, but we don't know what ... */
262 	error->re_status = RPC_FAILED;
263 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
264 	error->re_lb.s2 = (int32_t)acpt_stat;
265 }
266 
267 static void
268 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
269 {
270 
271 	assert(error != NULL);
272 
273 	switch (rjct_stat) {
274 	case RPC_MISMATCH:
275 		error->re_status = RPC_VERSMISMATCH;
276 		return;
277 
278 	case AUTH_ERROR:
279 		error->re_status = RPC_AUTHERROR;
280 		return;
281 	}
282 	/* something's wrong, but we don't know what ... */
283 	/* NOTREACHED */
284 	error->re_status = RPC_FAILED;
285 	error->re_lb.s1 = (int32_t)MSG_DENIED;
286 	error->re_lb.s2 = (int32_t)rjct_stat;
287 }
288 
289 /*
290  * given a reply message, fills in the error
291  */
292 void
293 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
294 {
295 
296 	assert(msg != NULL);
297 	assert(error != NULL);
298 
299 	/* optimized for normal, SUCCESSful case */
300 	switch (msg->rm_reply.rp_stat) {
301 
302 	case MSG_ACCEPTED:
303 		if (msg->acpted_rply.ar_stat == SUCCESS) {
304 			error->re_status = RPC_SUCCESS;
305 			return;
306 		}
307 		accepted(msg->acpted_rply.ar_stat, error);
308 		break;
309 
310 	case MSG_DENIED:
311 		rejected(msg->rjcted_rply.rj_stat, error);
312 		break;
313 
314 	default:
315 		error->re_status = RPC_FAILED;
316 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
317 		break;
318 	}
319 	switch (error->re_status) {
320 
321 	case RPC_VERSMISMATCH:
322 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
323 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
324 		break;
325 
326 	case RPC_AUTHERROR:
327 		error->re_why = msg->rjcted_rply.rj_why;
328 		break;
329 
330 	case RPC_PROGVERSMISMATCH:
331 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
332 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
333 		break;
334 
335 	case RPC_FAILED:
336 	case RPC_SUCCESS:
337 	case RPC_PROGNOTREGISTERED:
338 	case RPC_PMAPFAILURE:
339 	case RPC_UNKNOWNPROTO:
340 	case RPC_UNKNOWNHOST:
341 	case RPC_SYSTEMERROR:
342 	case RPC_CANTDECODEARGS:
343 	case RPC_PROCUNAVAIL:
344 	case RPC_PROGUNAVAIL:
345 	case RPC_TIMEDOUT:
346 	case RPC_CANTRECV:
347 	case RPC_CANTSEND:
348 	case RPC_CANTDECODERES:
349 	case RPC_CANTENCODEARGS:
350 	default:
351 		break;
352 	}
353 }
354