xref: /freebsd/crypto/krb5/src/lib/rpc/rpc_prot.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* @(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright (c) 2010, Oracle America, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions are met:
9*7f2fe78bSCy Schubert  *
10*7f2fe78bSCy Schubert  *     * Redistributions of source code must retain the above copyright
11*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer.
12*7f2fe78bSCy Schubert  *
13*7f2fe78bSCy Schubert  *     * Redistributions in binary form must reproduce the above copyright
14*7f2fe78bSCy Schubert  *       notice, this list of conditions and the following disclaimer in
15*7f2fe78bSCy Schubert  *       the documentation and/or other materials provided with the
16*7f2fe78bSCy Schubert  *       distribution.
17*7f2fe78bSCy Schubert  *
18*7f2fe78bSCy Schubert  *     * Neither the name of the "Oracle America, Inc." nor the names of
19*7f2fe78bSCy Schubert  *       its contributors may be used to endorse or promote products
20*7f2fe78bSCy Schubert  *       derived from this software without specific prior written permission.
21*7f2fe78bSCy Schubert  *
22*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23*7f2fe78bSCy Schubert  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*7f2fe78bSCy Schubert  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25*7f2fe78bSCy Schubert  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26*7f2fe78bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27*7f2fe78bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
28*7f2fe78bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29*7f2fe78bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*7f2fe78bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*7f2fe78bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*7f2fe78bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*7f2fe78bSCy Schubert  */
34*7f2fe78bSCy Schubert #if !defined(lint) && defined(SCCSIDS)
35*7f2fe78bSCy Schubert static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
36*7f2fe78bSCy Schubert #endif
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /*
39*7f2fe78bSCy Schubert  * rpc_prot.c
40*7f2fe78bSCy Schubert  *
41*7f2fe78bSCy Schubert  * This set of routines implements the rpc message definition,
42*7f2fe78bSCy Schubert  * its serializer and some common rpc utility routines.
43*7f2fe78bSCy Schubert  * The routines are meant for various implementations of rpc -
44*7f2fe78bSCy Schubert  * they are NOT for the rpc client or rpc service implementations!
45*7f2fe78bSCy Schubert  * Because authentication stuff is easy and is part of rpc, the opaque
46*7f2fe78bSCy Schubert  * routines are also in this program.
47*7f2fe78bSCy Schubert  */
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert #include <sys/param.h>
50*7f2fe78bSCy Schubert 
51*7f2fe78bSCy Schubert #include <gssrpc/rpc.h>
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
54*7f2fe78bSCy Schubert 
55*7f2fe78bSCy Schubert /*
56*7f2fe78bSCy Schubert  * XDR an opaque authentication struct
57*7f2fe78bSCy Schubert  * (see auth.h)
58*7f2fe78bSCy Schubert  */
59*7f2fe78bSCy Schubert bool_t
xdr_opaque_auth(XDR * xdrs,struct opaque_auth * ap)60*7f2fe78bSCy Schubert xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
61*7f2fe78bSCy Schubert {
62*7f2fe78bSCy Schubert 
63*7f2fe78bSCy Schubert 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
64*7f2fe78bSCy Schubert 		return (xdr_bytes(xdrs, &ap->oa_base,
65*7f2fe78bSCy Schubert 			&ap->oa_length, MAX_AUTH_BYTES));
66*7f2fe78bSCy Schubert 	return (FALSE);
67*7f2fe78bSCy Schubert }
68*7f2fe78bSCy Schubert 
69*7f2fe78bSCy Schubert /*
70*7f2fe78bSCy Schubert  * XDR a DES block
71*7f2fe78bSCy Schubert  */
72*7f2fe78bSCy Schubert bool_t
xdr_des_block(XDR * xdrs,des_block * blkp)73*7f2fe78bSCy Schubert xdr_des_block(XDR *xdrs, des_block *blkp)
74*7f2fe78bSCy Schubert {
75*7f2fe78bSCy Schubert 	return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block)));
76*7f2fe78bSCy Schubert }
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
79*7f2fe78bSCy Schubert 
80*7f2fe78bSCy Schubert /*
81*7f2fe78bSCy Schubert  * XDR the MSG_ACCEPTED part of a reply message union
82*7f2fe78bSCy Schubert  */
83*7f2fe78bSCy Schubert bool_t
xdr_accepted_reply(XDR * xdrs,struct accepted_reply * ar)84*7f2fe78bSCy Schubert xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
85*7f2fe78bSCy Schubert {
86*7f2fe78bSCy Schubert 
87*7f2fe78bSCy Schubert 	/* personalized union, rather than calling xdr_union */
88*7f2fe78bSCy Schubert 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
89*7f2fe78bSCy Schubert 		return (FALSE);
90*7f2fe78bSCy Schubert 	if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
91*7f2fe78bSCy Schubert 		return (FALSE);
92*7f2fe78bSCy Schubert 	switch (ar->ar_stat) {
93*7f2fe78bSCy Schubert 
94*7f2fe78bSCy Schubert 	case SUCCESS:
95*7f2fe78bSCy Schubert 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert 	case PROG_MISMATCH:
98*7f2fe78bSCy Schubert 		if (! xdr_rpcvers(xdrs, &(ar->ar_vers.low)))
99*7f2fe78bSCy Schubert 			return (FALSE);
100*7f2fe78bSCy Schubert 		return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));
101*7f2fe78bSCy Schubert 
102*7f2fe78bSCy Schubert 	case GARBAGE_ARGS:
103*7f2fe78bSCy Schubert 	case SYSTEM_ERR:
104*7f2fe78bSCy Schubert 	case PROC_UNAVAIL:
105*7f2fe78bSCy Schubert 	case PROG_UNAVAIL:
106*7f2fe78bSCy Schubert 		break;
107*7f2fe78bSCy Schubert 	}
108*7f2fe78bSCy Schubert 	return (TRUE);  /* TRUE => open ended set of problems */
109*7f2fe78bSCy Schubert }
110*7f2fe78bSCy Schubert 
111*7f2fe78bSCy Schubert /*
112*7f2fe78bSCy Schubert  * XDR the MSG_DENIED part of a reply message union
113*7f2fe78bSCy Schubert  */
114*7f2fe78bSCy Schubert bool_t
xdr_rejected_reply(XDR * xdrs,struct rejected_reply * rr)115*7f2fe78bSCy Schubert xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
116*7f2fe78bSCy Schubert {
117*7f2fe78bSCy Schubert 
118*7f2fe78bSCy Schubert 	/* personalized union, rather than calling xdr_union */
119*7f2fe78bSCy Schubert 	if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
120*7f2fe78bSCy Schubert 		return (FALSE);
121*7f2fe78bSCy Schubert 	switch (rr->rj_stat) {
122*7f2fe78bSCy Schubert 
123*7f2fe78bSCy Schubert 	case RPC_MISMATCH:
124*7f2fe78bSCy Schubert 		if (! xdr_rpcvers(xdrs, &(rr->rj_vers.low)))
125*7f2fe78bSCy Schubert 			return (FALSE);
126*7f2fe78bSCy Schubert 		return (xdr_rpcvers(xdrs, &(rr->rj_vers.high)));
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert 	case AUTH_ERROR:
129*7f2fe78bSCy Schubert 		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
130*7f2fe78bSCy Schubert 	}
131*7f2fe78bSCy Schubert 	return (FALSE);
132*7f2fe78bSCy Schubert }
133*7f2fe78bSCy Schubert 
134*7f2fe78bSCy Schubert static struct xdr_discrim reply_dscrm[3] = {
135*7f2fe78bSCy Schubert 	{ (int)MSG_ACCEPTED, xdr_accepted_reply },
136*7f2fe78bSCy Schubert 	{ (int)MSG_DENIED, xdr_rejected_reply },
137*7f2fe78bSCy Schubert 	{ __dontcare__, NULL_xdrproc_t } };
138*7f2fe78bSCy Schubert 
139*7f2fe78bSCy Schubert /*
140*7f2fe78bSCy Schubert  * XDR a reply message
141*7f2fe78bSCy Schubert  */
142*7f2fe78bSCy Schubert bool_t
xdr_replymsg(XDR * xdrs,struct rpc_msg * rmsg)143*7f2fe78bSCy Schubert xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
144*7f2fe78bSCy Schubert {
145*7f2fe78bSCy Schubert 	if (
146*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(rmsg->rm_xid)) &&
147*7f2fe78bSCy Schubert 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
148*7f2fe78bSCy Schubert 	    (rmsg->rm_direction == REPLY) )
149*7f2fe78bSCy Schubert 		return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
150*7f2fe78bSCy Schubert 		   (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));
151*7f2fe78bSCy Schubert 	return (FALSE);
152*7f2fe78bSCy Schubert }
153*7f2fe78bSCy Schubert 
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert /*
156*7f2fe78bSCy Schubert  * Serializes the "static part" of a call message header.
157*7f2fe78bSCy Schubert  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
158*7f2fe78bSCy Schubert  * The rm_xid is not really static, but the user can easily munge on the fly.
159*7f2fe78bSCy Schubert  */
160*7f2fe78bSCy Schubert bool_t
xdr_callhdr(XDR * xdrs,struct rpc_msg * cmsg)161*7f2fe78bSCy Schubert xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
162*7f2fe78bSCy Schubert {
163*7f2fe78bSCy Schubert 
164*7f2fe78bSCy Schubert 	cmsg->rm_direction = CALL;
165*7f2fe78bSCy Schubert 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
166*7f2fe78bSCy Schubert 	if (
167*7f2fe78bSCy Schubert 	    (xdrs->x_op == XDR_ENCODE) &&
168*7f2fe78bSCy Schubert 	    xdr_u_int32(xdrs, &(cmsg->rm_xid)) &&
169*7f2fe78bSCy Schubert 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
170*7f2fe78bSCy Schubert 	    xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
171*7f2fe78bSCy Schubert 	    xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) )
172*7f2fe78bSCy Schubert 	    return (xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)));
173*7f2fe78bSCy Schubert 	return (FALSE);
174*7f2fe78bSCy Schubert }
175*7f2fe78bSCy Schubert 
176*7f2fe78bSCy Schubert /* ************************** Client utility routine ************* */
177*7f2fe78bSCy Schubert 
178*7f2fe78bSCy Schubert static void
accepted(enum accept_stat acpt_stat,struct rpc_err * error)179*7f2fe78bSCy Schubert accepted(enum accept_stat acpt_stat, struct rpc_err *error)
180*7f2fe78bSCy Schubert {
181*7f2fe78bSCy Schubert 
182*7f2fe78bSCy Schubert 	switch (acpt_stat) {
183*7f2fe78bSCy Schubert 
184*7f2fe78bSCy Schubert 	case PROG_UNAVAIL:
185*7f2fe78bSCy Schubert 		error->re_status = RPC_PROGUNAVAIL;
186*7f2fe78bSCy Schubert 		return;
187*7f2fe78bSCy Schubert 
188*7f2fe78bSCy Schubert 	case PROG_MISMATCH:
189*7f2fe78bSCy Schubert 		error->re_status = RPC_PROGVERSMISMATCH;
190*7f2fe78bSCy Schubert 		return;
191*7f2fe78bSCy Schubert 
192*7f2fe78bSCy Schubert 	case PROC_UNAVAIL:
193*7f2fe78bSCy Schubert 		error->re_status = RPC_PROCUNAVAIL;
194*7f2fe78bSCy Schubert 		return;
195*7f2fe78bSCy Schubert 
196*7f2fe78bSCy Schubert 	case GARBAGE_ARGS:
197*7f2fe78bSCy Schubert 		error->re_status = RPC_CANTDECODEARGS;
198*7f2fe78bSCy Schubert 		return;
199*7f2fe78bSCy Schubert 
200*7f2fe78bSCy Schubert 	case SYSTEM_ERR:
201*7f2fe78bSCy Schubert 		error->re_status = RPC_SYSTEMERROR;
202*7f2fe78bSCy Schubert 		return;
203*7f2fe78bSCy Schubert 
204*7f2fe78bSCy Schubert 	case SUCCESS:
205*7f2fe78bSCy Schubert 		error->re_status = RPC_SUCCESS;
206*7f2fe78bSCy Schubert 		return;
207*7f2fe78bSCy Schubert 	}
208*7f2fe78bSCy Schubert 	/* something's wrong, but we don't know what ... */
209*7f2fe78bSCy Schubert 	error->re_status = RPC_FAILED;
210*7f2fe78bSCy Schubert 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
211*7f2fe78bSCy Schubert 	error->re_lb.s2 = (int32_t)acpt_stat;
212*7f2fe78bSCy Schubert }
213*7f2fe78bSCy Schubert 
214*7f2fe78bSCy Schubert static void
rejected(enum reject_stat rjct_stat,struct rpc_err * error)215*7f2fe78bSCy Schubert rejected(enum reject_stat rjct_stat, struct rpc_err *error)
216*7f2fe78bSCy Schubert {
217*7f2fe78bSCy Schubert 
218*7f2fe78bSCy Schubert 	switch (rjct_stat) {
219*7f2fe78bSCy Schubert 
220*7f2fe78bSCy Schubert 	case RPC_MISMATCH:
221*7f2fe78bSCy Schubert 		error->re_status = RPC_VERSMISMATCH;
222*7f2fe78bSCy Schubert 		return;
223*7f2fe78bSCy Schubert 
224*7f2fe78bSCy Schubert 	case AUTH_ERROR:
225*7f2fe78bSCy Schubert 		error->re_status = RPC_AUTHERROR;
226*7f2fe78bSCy Schubert 		return;
227*7f2fe78bSCy Schubert 	}
228*7f2fe78bSCy Schubert 	/* something's wrong, but we don't know what ... */
229*7f2fe78bSCy Schubert 	error->re_status = RPC_FAILED;
230*7f2fe78bSCy Schubert 	error->re_lb.s1 = (int32_t)MSG_DENIED;
231*7f2fe78bSCy Schubert 	error->re_lb.s2 = (int32_t)rjct_stat;
232*7f2fe78bSCy Schubert }
233*7f2fe78bSCy Schubert 
234*7f2fe78bSCy Schubert /*
235*7f2fe78bSCy Schubert  * given a reply message, fills in the error
236*7f2fe78bSCy Schubert  */
237*7f2fe78bSCy Schubert void
gssrpc__seterr_reply(struct rpc_msg * msg,struct rpc_err * error)238*7f2fe78bSCy Schubert gssrpc__seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
239*7f2fe78bSCy Schubert {
240*7f2fe78bSCy Schubert 
241*7f2fe78bSCy Schubert 	/* optimized for normal, SUCCESSful case */
242*7f2fe78bSCy Schubert 	switch (msg->rm_reply.rp_stat) {
243*7f2fe78bSCy Schubert 
244*7f2fe78bSCy Schubert 	case MSG_ACCEPTED:
245*7f2fe78bSCy Schubert 		if (msg->acpted_rply.ar_stat == SUCCESS) {
246*7f2fe78bSCy Schubert 			error->re_status = RPC_SUCCESS;
247*7f2fe78bSCy Schubert 			return;
248*7f2fe78bSCy Schubert 		};
249*7f2fe78bSCy Schubert 		accepted(msg->acpted_rply.ar_stat, error);
250*7f2fe78bSCy Schubert 		break;
251*7f2fe78bSCy Schubert 
252*7f2fe78bSCy Schubert 	case MSG_DENIED:
253*7f2fe78bSCy Schubert 		rejected(msg->rjcted_rply.rj_stat, error);
254*7f2fe78bSCy Schubert 		break;
255*7f2fe78bSCy Schubert 
256*7f2fe78bSCy Schubert 	default:
257*7f2fe78bSCy Schubert 		error->re_status = RPC_FAILED;
258*7f2fe78bSCy Schubert 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
259*7f2fe78bSCy Schubert 		break;
260*7f2fe78bSCy Schubert 	}
261*7f2fe78bSCy Schubert 	switch (error->re_status) {
262*7f2fe78bSCy Schubert 
263*7f2fe78bSCy Schubert 	case RPC_VERSMISMATCH:
264*7f2fe78bSCy Schubert 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
265*7f2fe78bSCy Schubert 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
266*7f2fe78bSCy Schubert 		break;
267*7f2fe78bSCy Schubert 
268*7f2fe78bSCy Schubert 	case RPC_AUTHERROR:
269*7f2fe78bSCy Schubert 		error->re_why = msg->rjcted_rply.rj_why;
270*7f2fe78bSCy Schubert 		break;
271*7f2fe78bSCy Schubert 
272*7f2fe78bSCy Schubert 	case RPC_PROGVERSMISMATCH:
273*7f2fe78bSCy Schubert 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
274*7f2fe78bSCy Schubert 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
275*7f2fe78bSCy Schubert 		break;
276*7f2fe78bSCy Schubert 
277*7f2fe78bSCy Schubert 	case RPC_FAILED:
278*7f2fe78bSCy Schubert 	case RPC_SUCCESS:
279*7f2fe78bSCy Schubert 	case RPC_PROGNOTREGISTERED:
280*7f2fe78bSCy Schubert 	case RPC_PMAPFAILURE:
281*7f2fe78bSCy Schubert 	case RPC_UNKNOWNPROTO:
282*7f2fe78bSCy Schubert 	case RPC_UNKNOWNHOST:
283*7f2fe78bSCy Schubert 	case RPC_SYSTEMERROR:
284*7f2fe78bSCy Schubert 	case RPC_CANTDECODEARGS:
285*7f2fe78bSCy Schubert 	case RPC_PROCUNAVAIL:
286*7f2fe78bSCy Schubert 	case RPC_PROGUNAVAIL:
287*7f2fe78bSCy Schubert 	case RPC_TIMEDOUT:
288*7f2fe78bSCy Schubert 	case RPC_CANTRECV:
289*7f2fe78bSCy Schubert 	case RPC_CANTSEND:
290*7f2fe78bSCy Schubert 	case RPC_CANTDECODERES:
291*7f2fe78bSCy Schubert 	case RPC_CANTENCODEARGS:
292*7f2fe78bSCy Schubert 	default:
293*7f2fe78bSCy Schubert 		break;
294*7f2fe78bSCy Schubert 	}
295*7f2fe78bSCy Schubert }
296