rpc_prot.c revision 22993
1229675Sadrian/*
2229675Sadrian * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3229675Sadrian * unrestricted use provided that this legend is included on all tape
4276486Sngie * media and as a part of the software program in whole or part.  Users
5229675Sadrian * may copy or modify Sun RPC without charge, but are not authorized
6229675Sadrian * to license or distribute it to anyone else except as part of a product or
7229675Sadrian * program developed by the user.
8229675Sadrian *
9229675Sadrian * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10229675Sadrian * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11229675Sadrian * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12229675Sadrian *
13229675Sadrian * Sun RPC is provided with no support and without any obligation on the
14229675Sadrian * part of Sun Microsystems, Inc. to assist in its use, correction,
15229675Sadrian * modification or enhancement.
16229675Sadrian *
17229675Sadrian * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18229675Sadrian * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19229675Sadrian * OR ANY PART THEREOF.
20229675Sadrian *
21229675Sadrian * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22229675Sadrian * or profits or other special, indirect and consequential damages, even if
23229675Sadrian * Sun has been advised of the possibility of such damages.
24229675Sadrian *
25229675Sadrian * Sun Microsystems, Inc.
26229675Sadrian * 2550 Garcia Avenue
27229675Sadrian * Mountain View, California  94043
28229675Sadrian */
29229675Sadrian
30229675Sadrian#if defined(LIBC_SCCS) && !defined(lint)
31229675Sadrian/*static char *sccsid = "from: @(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";*/
32229675Sadrian/*static char *sccsid = "from: @(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";*/
33229675Sadrianstatic char *rcsid = "$Id$";
34229675Sadrian#endif
35229675Sadrian
36229675Sadrian/*
37229675Sadrian * rpc_prot.c
38229675Sadrian *
39229675Sadrian * Copyright (C) 1984, Sun Microsystems, Inc.
40229675Sadrian *
41229675Sadrian * This set of routines implements the rpc message definition,
42229675Sadrian * its serializer and some common rpc utility routines.
43229675Sadrian * The routines are meant for various implementations of rpc -
44229675Sadrian * they are NOT for the rpc client or rpc service implementations!
45229675Sadrian * Because authentication stuff is easy and is part of rpc, the opaque
46229675Sadrian * routines are also in this program.
47229675Sadrian */
48229675Sadrian
49229675Sadrian#include <sys/param.h>
50229675Sadrian
51229675Sadrian#include <rpc/rpc.h>
52229675Sadrian
53229675Sadrian/* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
54229675Sadrian
55229675Sadrianstruct opaque_auth _null_auth;
56229675Sadrian
57229675Sadrian/*
58239885Sadrian * XDR an opaque authentication struct
59229675Sadrian * (see auth.h)
60229675Sadrian */
61229675Sadrianbool_t
62229675Sadrianxdr_opaque_auth(xdrs, ap)
63229675Sadrian	register XDR *xdrs;
64229675Sadrian	register struct opaque_auth *ap;
65229675Sadrian{
66229675Sadrian
67229675Sadrian	if (xdr_enum(xdrs, &(ap->oa_flavor)))
68229675Sadrian		return (xdr_bytes(xdrs, &ap->oa_base,
69229675Sadrian			&ap->oa_length, MAX_AUTH_BYTES));
70229675Sadrian	return (FALSE);
71229675Sadrian}
72229675Sadrian
73229675Sadrian/*
74229675Sadrian * XDR a DES block
75229675Sadrian */
76229675Sadrianbool_t
77229675Sadrianxdr_des_block(xdrs, blkp)
78229675Sadrian	register XDR *xdrs;
79229675Sadrian	register des_block *blkp;
80229675Sadrian{
81229675Sadrian	return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block)));
82229675Sadrian}
83229675Sadrian
84229675Sadrian/* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
85229675Sadrian
86229675Sadrian/*
87229675Sadrian * XDR the MSG_ACCEPTED part of a reply message union
88229675Sadrian */
89229675Sadrianbool_t
90229675Sadrianxdr_accepted_reply(xdrs, ar)
91229675Sadrian	register XDR *xdrs;
92229675Sadrian	register struct accepted_reply *ar;
93229675Sadrian{
94229675Sadrian
95229675Sadrian	/* personalized union, rather than calling xdr_union */
96229675Sadrian	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
97229675Sadrian		return (FALSE);
98229675Sadrian	if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
99229675Sadrian		return (FALSE);
100229675Sadrian	switch (ar->ar_stat) {
101239885Sadrian
102229675Sadrian	case SUCCESS:
103229675Sadrian		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
104229675Sadrian
105229675Sadrian	case PROG_MISMATCH:
106229675Sadrian		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
107229675Sadrian			return (FALSE);
108229675Sadrian		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
109229675Sadrian	default:
110229675Sadrian		break;
111229675Sadrian	}
112229675Sadrian	return (TRUE);  /* TRUE => open ended set of problems */
113229675Sadrian}
114229675Sadrian
115229675Sadrian/*
116 * XDR the MSG_DENIED part of a reply message union
117 */
118bool_t
119xdr_rejected_reply(xdrs, rr)
120	register XDR *xdrs;
121	register struct rejected_reply *rr;
122{
123
124	/* personalized union, rather than calling xdr_union */
125	if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
126		return (FALSE);
127	switch (rr->rj_stat) {
128
129	case RPC_MISMATCH:
130		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
131			return (FALSE);
132		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
133
134	case AUTH_ERROR:
135		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
136	}
137	return (FALSE);
138}
139
140static struct xdr_discrim reply_dscrm[3] = {
141	{ (int)MSG_ACCEPTED, xdr_accepted_reply },
142	{ (int)MSG_DENIED, xdr_rejected_reply },
143	{ __dontcare__, NULL_xdrproc_t } };
144
145/*
146 * XDR a reply message
147 */
148bool_t
149xdr_replymsg(xdrs, rmsg)
150	register XDR *xdrs;
151	register struct rpc_msg *rmsg;
152{
153	if (
154	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
155	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
156	    (rmsg->rm_direction == REPLY) )
157		return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
158		   (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));
159	return (FALSE);
160}
161
162
163/*
164 * Serializes the "static part" of a call message header.
165 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
166 * The rm_xid is not really static, but the user can easily munge on the fly.
167 */
168bool_t
169xdr_callhdr(xdrs, cmsg)
170	register XDR *xdrs;
171	register struct rpc_msg *cmsg;
172{
173
174	cmsg->rm_direction = CALL;
175	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
176	if (
177	    (xdrs->x_op == XDR_ENCODE) &&
178	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
179	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
180	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
181	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
182	    return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
183	return (FALSE);
184}
185
186/* ************************** Client utility routine ************* */
187
188static void
189accepted(acpt_stat, error)
190	register enum accept_stat acpt_stat;
191	register struct rpc_err *error;
192{
193
194	switch (acpt_stat) {
195
196	case PROG_UNAVAIL:
197		error->re_status = RPC_PROGUNAVAIL;
198		return;
199
200	case PROG_MISMATCH:
201		error->re_status = RPC_PROGVERSMISMATCH;
202		return;
203
204	case PROC_UNAVAIL:
205		error->re_status = RPC_PROCUNAVAIL;
206		return;
207
208	case GARBAGE_ARGS:
209		error->re_status = RPC_CANTDECODEARGS;
210		return;
211
212	case SYSTEM_ERR:
213		error->re_status = RPC_SYSTEMERROR;
214		return;
215
216	case SUCCESS:
217		error->re_status = RPC_SUCCESS;
218		return;
219	}
220	/* something's wrong, but we don't know what ... */
221	error->re_status = RPC_FAILED;
222	error->re_lb.s1 = (long)MSG_ACCEPTED;
223	error->re_lb.s2 = (long)acpt_stat;
224}
225
226static void
227rejected(rjct_stat, error)
228	register enum reject_stat rjct_stat;
229	register struct rpc_err *error;
230{
231
232	switch (rjct_stat) {
233
234	case RPC_VERSMISMATCH:
235		error->re_status = RPC_VERSMISMATCH;
236		return;
237
238	case AUTH_ERROR:
239		error->re_status = RPC_AUTHERROR;
240		return;
241	default:
242		break;
243	}
244	/* something's wrong, but we don't know what ... */
245	error->re_status = RPC_FAILED;
246	error->re_lb.s1 = (long)MSG_DENIED;
247	error->re_lb.s2 = (long)rjct_stat;
248}
249
250/*
251 * given a reply message, fills in the error
252 */
253void
254_seterr_reply(msg, error)
255	register struct rpc_msg *msg;
256	register struct rpc_err *error;
257{
258
259	/* optimized for normal, SUCCESSful case */
260	switch (msg->rm_reply.rp_stat) {
261
262	case MSG_ACCEPTED:
263		if (msg->acpted_rply.ar_stat == SUCCESS) {
264			error->re_status = RPC_SUCCESS;
265			return;
266		};
267		accepted(msg->acpted_rply.ar_stat, error);
268		break;
269
270	case MSG_DENIED:
271		rejected(msg->rjcted_rply.rj_stat, error);
272		break;
273
274	default:
275		error->re_status = RPC_FAILED;
276		error->re_lb.s1 = (long)(msg->rm_reply.rp_stat);
277		break;
278	}
279	switch (error->re_status) {
280
281	case RPC_VERSMISMATCH:
282		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
283		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
284		break;
285
286	case RPC_AUTHERROR:
287		error->re_why = msg->rjcted_rply.rj_why;
288		break;
289
290	case RPC_PROGVERSMISMATCH:
291		error->re_vers.low = msg->acpted_rply.ar_vers.low;
292		error->re_vers.high = msg->acpted_rply.ar_vers.high;
293		break;
294	default:
295		break;
296	}
297}
298