xref: /netbsd-src/lib/libc/rpc/rpc_prot.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: rpc_prot.c,v 1.20 2012/03/20 17:14:50 matt Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";
37 #else
38 __RCSID("$NetBSD: rpc_prot.c,v 1.20 2012/03/20 17:14:50 matt Exp $");
39 #endif
40 #endif
41 
42 /*
43  * rpc_prot.c
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  *
47  * This set of routines implements the rpc message definition,
48  * its serializer and some common rpc utility routines.
49  * The routines are meant for various implementations of rpc -
50  * they are NOT for the rpc client or rpc service implementations!
51  * Because authentication stuff is easy and is part of rpc, the opaque
52  * routines are also in this program.
53  */
54 
55 #include "namespace.h"
56 
57 #include <sys/param.h>
58 
59 #include <assert.h>
60 
61 #include <rpc/rpc.h>
62 
63 #ifdef __weak_alias
64 __weak_alias(xdr_accepted_reply,_xdr_accepted_reply)
65 __weak_alias(xdr_callhdr,_xdr_callhdr)
66 __weak_alias(xdr_des_block,_xdr_des_block)
67 __weak_alias(xdr_opaque_auth,_xdr_opaque_auth)
68 __weak_alias(xdr_rejected_reply,_xdr_rejected_reply)
69 __weak_alias(xdr_replymsg,_xdr_replymsg)
70 #endif
71 
72 static void accepted(enum accept_stat, struct rpc_err *);
73 static void rejected(enum reject_stat, struct rpc_err *);
74 
75 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
76 
77 /*
78  * XDR an opaque authentication struct
79  * (see auth.h)
80  */
81 bool_t
82 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
83 {
84 
85 	_DIAGASSERT(xdrs != NULL);
86 	_DIAGASSERT(ap != NULL);
87 
88 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
89 		return (xdr_bytes(xdrs, &ap->oa_base,
90 			&ap->oa_length, MAX_AUTH_BYTES));
91 	return (FALSE);
92 }
93 
94 /*
95  * XDR a DES block
96  */
97 bool_t
98 xdr_des_block(XDR *xdrs, des_block *blkp)
99 {
100 
101 	_DIAGASSERT(xdrs != NULL);
102 	_DIAGASSERT(blkp != NULL);
103 
104 	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp,
105 	    (u_int)sizeof(des_block)));
106 }
107 
108 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
109 
110 /*
111  * XDR the MSG_ACCEPTED part of a reply message union
112  */
113 bool_t
114 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
115 {
116 
117 	_DIAGASSERT(xdrs != NULL);
118 	_DIAGASSERT(ar != NULL);
119 
120 	/* personalized union, rather than calling xdr_union */
121 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
122 		return (FALSE);
123 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(ar->ar_stat)))
124 		return (FALSE);
125 	switch (ar->ar_stat) {
126 
127 	case SUCCESS:
128 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
129 
130 	case PROG_MISMATCH:
131 		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
132 			return (FALSE);
133 		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
134 
135 	case GARBAGE_ARGS:
136 	case SYSTEM_ERR:
137 	case PROC_UNAVAIL:
138 	case PROG_UNAVAIL:
139 		break;
140 	}
141 	return (TRUE);  /* TRUE => open ended set of problems */
142 }
143 
144 /*
145  * XDR the MSG_DENIED part of a reply message union
146  */
147 bool_t
148 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
149 {
150 
151 	_DIAGASSERT(xdrs != NULL);
152 	_DIAGASSERT(rr != NULL);
153 
154 	/* personalized union, rather than calling xdr_union */
155 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_stat)))
156 		return (FALSE);
157 	switch (rr->rj_stat) {
158 
159 	case RPC_MISMATCH:
160 		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
161 			return (FALSE);
162 		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
163 
164 	case AUTH_ERROR:
165 		return (xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_why)));
166 	}
167 	/* NOTREACHED */
168 	return (FALSE);
169 }
170 
171 static const struct xdr_discrim reply_dscrm[3] = {
172 	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
173 	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
174 	{ __dontcare__, NULL_xdrproc_t } };
175 
176 /*
177  * XDR a reply message
178  */
179 bool_t
180 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
181 {
182 	_DIAGASSERT(xdrs != NULL);
183 	_DIAGASSERT(rmsg != NULL);
184 
185 	if (
186 	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
187 	    xdr_enum(xdrs, (enum_t *)(void *)&(rmsg->rm_direction)) &&
188 	    (rmsg->rm_direction == REPLY) )
189 		return (xdr_union(xdrs, (enum_t *)(void *)&(rmsg->rm_reply.rp_stat),
190 		   (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
191 		   NULL_xdrproc_t));
192 	return (FALSE);
193 }
194 
195 
196 /*
197  * Serializes the "static part" of a call message header.
198  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
199  * The rm_xid is not really static, but the user can easily munge on the fly.
200  */
201 bool_t
202 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
203 {
204 
205 	_DIAGASSERT(xdrs != NULL);
206 	_DIAGASSERT(cmsg != NULL);
207 
208 	cmsg->rm_direction = CALL;
209 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
210 	if (
211 	    (xdrs->x_op == XDR_ENCODE) &&
212 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
213 	    xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) &&
214 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
215 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
216 		return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
217 	return (FALSE);
218 }
219 
220 /* ************************** Client utility routine ************* */
221 
222 static void
223 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
224 {
225 
226 	_DIAGASSERT(error != NULL);
227 
228 	switch (acpt_stat) {
229 
230 	case PROG_UNAVAIL:
231 		error->re_status = RPC_PROGUNAVAIL;
232 		return;
233 
234 	case PROG_MISMATCH:
235 		error->re_status = RPC_PROGVERSMISMATCH;
236 		return;
237 
238 	case PROC_UNAVAIL:
239 		error->re_status = RPC_PROCUNAVAIL;
240 		return;
241 
242 	case GARBAGE_ARGS:
243 		error->re_status = RPC_CANTDECODEARGS;
244 		return;
245 
246 	case SYSTEM_ERR:
247 		error->re_status = RPC_SYSTEMERROR;
248 		return;
249 
250 	case SUCCESS:
251 		error->re_status = RPC_SUCCESS;
252 		return;
253 	}
254 	/* NOTREACHED */
255 	/* something's wrong, but we don't know what ... */
256 	error->re_status = RPC_FAILED;
257 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
258 	error->re_lb.s2 = (int32_t)acpt_stat;
259 }
260 
261 static void
262 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
263 {
264 
265 	_DIAGASSERT(error != NULL);
266 
267 	switch (rjct_stat) {
268 	case RPC_MISMATCH:
269 		error->re_status = RPC_VERSMISMATCH;
270 		return;
271 
272 	case AUTH_ERROR:
273 		error->re_status = RPC_AUTHERROR;
274 		return;
275 	}
276 	/* something's wrong, but we don't know what ... */
277 	/* NOTREACHED */
278 	error->re_status = RPC_FAILED;
279 	error->re_lb.s1 = (int32_t)MSG_DENIED;
280 	error->re_lb.s2 = (int32_t)rjct_stat;
281 }
282 
283 /*
284  * given a reply message, fills in the error
285  */
286 void
287 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
288 {
289 
290 	_DIAGASSERT(msg != NULL);
291 	_DIAGASSERT(error != NULL);
292 
293 	/* optimized for normal, SUCCESSful case */
294 	switch (msg->rm_reply.rp_stat) {
295 
296 	case MSG_ACCEPTED:
297 		if (msg->acpted_rply.ar_stat == SUCCESS) {
298 			error->re_status = RPC_SUCCESS;
299 			return;
300 		}
301 		accepted(msg->acpted_rply.ar_stat, error);
302 		break;
303 
304 	case MSG_DENIED:
305 		rejected(msg->rjcted_rply.rj_stat, error);
306 		break;
307 
308 	default:
309 		error->re_status = RPC_FAILED;
310 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
311 		break;
312 	}
313 	switch (error->re_status) {
314 
315 	case RPC_VERSMISMATCH:
316 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
317 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
318 		break;
319 
320 	case RPC_AUTHERROR:
321 		error->re_why = msg->rjcted_rply.rj_why;
322 		break;
323 
324 	case RPC_PROGVERSMISMATCH:
325 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
326 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
327 		break;
328 
329 	case RPC_FAILED:
330 	case RPC_SUCCESS:
331 	case RPC_PROGNOTREGISTERED:
332 	case RPC_PMAPFAILURE:
333 	case RPC_UNKNOWNPROTO:
334 	case RPC_UNKNOWNHOST:
335 	case RPC_SYSTEMERROR:
336 	case RPC_CANTDECODEARGS:
337 	case RPC_PROCUNAVAIL:
338 	case RPC_PROGUNAVAIL:
339 	case RPC_TIMEDOUT:
340 	case RPC_CANTRECV:
341 	case RPC_CANTSEND:
342 	case RPC_CANTDECODERES:
343 	case RPC_CANTENCODEARGS:
344 	default:
345 		break;
346 	}
347 }
348