rpc_prot.c revision 74462
1/*	$NetBSD: rpc_prot.c,v 1.16 2000/06/02 23:11:13 fvdl 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)
34static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
35static char *sccsid = "@(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";
36static char *rcsid = "$FreeBSD: head/lib/libc/rpc/rpc_prot.c 74462 2001-03-19 12:50:13Z alfred $";
37#endif
38
39/*
40 * rpc_prot.c
41 *
42 * Copyright (C) 1984, Sun Microsystems, Inc.
43 *
44 * This set of routines implements the rpc message definition,
45 * its serializer and some common rpc utility routines.
46 * The routines are meant for various implementations of rpc -
47 * they are NOT for the rpc client or rpc service implementations!
48 * Because authentication stuff is easy and is part of rpc, the opaque
49 * routines are also in this program.
50 */
51
52#include "namespace.h"
53#include <sys/param.h>
54
55#include <assert.h>
56
57#include <rpc/rpc.h>
58#include "un-namespace.h"
59
60static void accepted __P((enum accept_stat, struct rpc_err *));
61static void rejected __P((enum reject_stat, struct rpc_err *));
62
63/* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
64
65extern struct opaque_auth _null_auth;
66
67/*
68 * XDR an opaque authentication struct
69 * (see auth.h)
70 */
71bool_t
72xdr_opaque_auth(xdrs, ap)
73	XDR *xdrs;
74	struct opaque_auth *ap;
75{
76
77	assert(xdrs != NULL);
78	assert(ap != NULL);
79
80	if (xdr_enum(xdrs, &(ap->oa_flavor)))
81		return (xdr_bytes(xdrs, &ap->oa_base,
82			&ap->oa_length, MAX_AUTH_BYTES));
83	return (FALSE);
84}
85
86/*
87 * XDR a DES block
88 */
89bool_t
90xdr_des_block(xdrs, blkp)
91	XDR *xdrs;
92	des_block *blkp;
93{
94
95	assert(xdrs != NULL);
96	assert(blkp != NULL);
97
98	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
99}
100
101/* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
102
103/*
104 * XDR the MSG_ACCEPTED part of a reply message union
105 */
106bool_t
107xdr_accepted_reply(xdrs, ar)
108	XDR *xdrs;
109	struct accepted_reply *ar;
110{
111
112	assert(xdrs != NULL);
113	assert(ar != NULL);
114
115	/* personalized union, rather than calling xdr_union */
116	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
117		return (FALSE);
118	if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
119		return (FALSE);
120	switch (ar->ar_stat) {
121
122	case SUCCESS:
123		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
124
125	case PROG_MISMATCH:
126		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
127			return (FALSE);
128		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
129
130	case GARBAGE_ARGS:
131	case SYSTEM_ERR:
132	case PROC_UNAVAIL:
133	case PROG_UNAVAIL:
134		break;
135	}
136	return (TRUE);  /* TRUE => open ended set of problems */
137}
138
139/*
140 * XDR the MSG_DENIED part of a reply message union
141 */
142bool_t
143xdr_rejected_reply(xdrs, rr)
144	XDR *xdrs;
145	struct rejected_reply *rr;
146{
147
148	assert(xdrs != NULL);
149	assert(rr != NULL);
150
151	/* personalized union, rather than calling xdr_union */
152	if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
153		return (FALSE);
154	switch (rr->rj_stat) {
155
156	case RPC_MISMATCH:
157		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
158			return (FALSE);
159		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
160
161	case AUTH_ERROR:
162		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
163	}
164	/* NOTREACHED */
165	assert(0);
166	return (FALSE);
167}
168
169static const struct xdr_discrim reply_dscrm[3] = {
170	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
171	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
172	{ __dontcare__, NULL_xdrproc_t } };
173
174/*
175 * XDR a reply message
176 */
177bool_t
178xdr_replymsg(xdrs, rmsg)
179	XDR *xdrs;
180	struct rpc_msg *rmsg;
181{
182	assert(xdrs != NULL);
183	assert(rmsg != NULL);
184
185	if (
186	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
187	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
188	    (rmsg->rm_direction == REPLY) )
189		return (xdr_union(xdrs, (enum_t *)&(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 */
201bool_t
202xdr_callhdr(xdrs, cmsg)
203	XDR *xdrs;
204	struct rpc_msg *cmsg;
205{
206
207	assert(xdrs != NULL);
208	assert(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 *)&(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
224static void
225accepted(acpt_stat, error)
226	enum accept_stat acpt_stat;
227	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
265static void
266rejected(rjct_stat, error)
267	enum reject_stat rjct_stat;
268	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 */
292void
293_seterr_reply(msg, error)
294	struct rpc_msg *msg;
295	struct rpc_err *error;
296{
297
298	assert(msg != NULL);
299	assert(error != NULL);
300
301	/* optimized for normal, SUCCESSful case */
302	switch (msg->rm_reply.rp_stat) {
303
304	case MSG_ACCEPTED:
305		if (msg->acpted_rply.ar_stat == SUCCESS) {
306			error->re_status = RPC_SUCCESS;
307			return;
308		}
309		accepted(msg->acpted_rply.ar_stat, error);
310		break;
311
312	case MSG_DENIED:
313		rejected(msg->rjcted_rply.rj_stat, error);
314		break;
315
316	default:
317		error->re_status = RPC_FAILED;
318		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
319		break;
320	}
321	switch (error->re_status) {
322
323	case RPC_VERSMISMATCH:
324		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
325		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
326		break;
327
328	case RPC_AUTHERROR:
329		error->re_why = msg->rjcted_rply.rj_why;
330		break;
331
332	case RPC_PROGVERSMISMATCH:
333		error->re_vers.low = msg->acpted_rply.ar_vers.low;
334		error->re_vers.high = msg->acpted_rply.ar_vers.high;
335		break;
336
337	case RPC_FAILED:
338	case RPC_SUCCESS:
339	case RPC_PROGNOTREGISTERED:
340	case RPC_PMAPFAILURE:
341	case RPC_UNKNOWNPROTO:
342	case RPC_UNKNOWNHOST:
343	case RPC_SYSTEMERROR:
344	case RPC_CANTDECODEARGS:
345	case RPC_PROCUNAVAIL:
346	case RPC_PROGUNAVAIL:
347	case RPC_TIMEDOUT:
348	case RPC_CANTRECV:
349	case RPC_CANTSEND:
350	case RPC_CANTDECODERES:
351	case RPC_CANTENCODEARGS:
352	default:
353		break;
354	}
355}
356