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