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