Merge from vendor branch GCC:
[dragonfly.git] / lib / libc / rpc / rpc_prot.c
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  * $FreeBSD: src/lib/libc/rpc/rpc_prot.c,v 1.8 1999/08/28 00:00:46 peter Exp $
32  * $DragonFly: src/lib/libc/rpc/rpc_prot.c,v 1.4 2005/11/13 12:27:04 swildner Exp $
33  */
34
35 /*
36  * rpc_prot.c
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * This set of routines implements the rpc message definition,
41  * its serializer and some common rpc utility routines.
42  * The routines are meant for various implementations of rpc -
43  * they are NOT for the rpc client or rpc service implementations!
44  * Because authentication stuff is easy and is part of rpc, the opaque
45  * routines are also in this program.
46  */
47
48 #include <sys/param.h>
49
50 #include <rpc/rpc.h>
51
52 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
53
54 struct opaque_auth _null_auth;
55
56 /*
57  * XDR an opaque authentication struct
58  * (see auth.h)
59  */
60 bool_t
61 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
62 {
63
64         if (xdr_enum(xdrs, &(ap->oa_flavor)))
65                 return (xdr_bytes(xdrs, &ap->oa_base,
66                         &ap->oa_length, MAX_AUTH_BYTES));
67         return (FALSE);
68 }
69
70 /*
71  * XDR a DES block
72  */
73 bool_t
74 xdr_des_block(XDR *xdrs, des_block *blkp)
75 {
76         return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block)));
77 }
78
79 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
80
81 /*
82  * XDR the MSG_ACCEPTED part of a reply message union
83  */
84 bool_t
85 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
86 {
87
88         /* personalized union, rather than calling xdr_union */
89         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
90                 return (FALSE);
91         if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
92                 return (FALSE);
93         switch (ar->ar_stat) {
94
95         case SUCCESS:
96                 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
97
98         case PROG_MISMATCH:
99                 if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
100                         return (FALSE);
101                 return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
102         default:
103                 break;
104         }
105         return (TRUE);  /* TRUE => open ended set of problems */
106 }
107
108 /*
109  * XDR the MSG_DENIED part of a reply message union
110  */
111 bool_t
112 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
113 {
114
115         /* personalized union, rather than calling xdr_union */
116         if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
117                 return (FALSE);
118         switch (rr->rj_stat) {
119
120         case RPC_MISMATCH:
121                 if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
122                         return (FALSE);
123                 return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
124
125         case AUTH_ERROR:
126                 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
127         }
128         return (FALSE);
129 }
130
131 static struct xdr_discrim reply_dscrm[3] = {
132         { (int)MSG_ACCEPTED, xdr_accepted_reply },
133         { (int)MSG_DENIED, xdr_rejected_reply },
134         { __dontcare__, NULL_xdrproc_t } };
135
136 /*
137  * XDR a reply message
138  */
139 bool_t
140 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
141 {
142         if (
143             xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
144             xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
145             (rmsg->rm_direction == REPLY) )
146                 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
147                    (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));
148         return (FALSE);
149 }
150
151
152 /*
153  * Serializes the "static part" of a call message header.
154  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
155  * The rm_xid is not really static, but the user can easily munge on the fly.
156  */
157 bool_t
158 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
159 {
160
161         cmsg->rm_direction = CALL;
162         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
163         if (
164             (xdrs->x_op == XDR_ENCODE) &&
165             xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
166             xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
167             xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
168             xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
169             return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
170         return (FALSE);
171 }
172
173 /* ************************** Client utility routine ************* */
174
175 static void
176 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
177 {
178
179         switch (acpt_stat) {
180
181         case PROG_UNAVAIL:
182                 error->re_status = RPC_PROGUNAVAIL;
183                 return;
184
185         case PROG_MISMATCH:
186                 error->re_status = RPC_PROGVERSMISMATCH;
187                 return;
188
189         case PROC_UNAVAIL:
190                 error->re_status = RPC_PROCUNAVAIL;
191                 return;
192
193         case GARBAGE_ARGS:
194                 error->re_status = RPC_CANTDECODEARGS;
195                 return;
196
197         case SYSTEM_ERR:
198                 error->re_status = RPC_SYSTEMERROR;
199                 return;
200
201         case SUCCESS:
202                 error->re_status = RPC_SUCCESS;
203                 return;
204         }
205         /* something's wrong, but we don't know what ... */
206         error->re_status = RPC_FAILED;
207         error->re_lb.s1 = (long)MSG_ACCEPTED;
208         error->re_lb.s2 = (long)acpt_stat;
209 }
210
211 static void
212 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
213 {
214
215         switch (rjct_stat) {
216
217         case RPC_VERSMISMATCH:
218                 error->re_status = RPC_VERSMISMATCH;
219                 return;
220
221         case AUTH_ERROR:
222                 error->re_status = RPC_AUTHERROR;
223                 return;
224         default:
225                 break;
226         }
227         /* something's wrong, but we don't know what ... */
228         error->re_status = RPC_FAILED;
229         error->re_lb.s1 = (long)MSG_DENIED;
230         error->re_lb.s2 = (long)rjct_stat;
231 }
232
233 /*
234  * given a reply message, fills in the error
235  */
236 void
237 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
238 {
239
240         /* optimized for normal, SUCCESSful case */
241         switch (msg->rm_reply.rp_stat) {
242
243         case MSG_ACCEPTED:
244                 if (msg->acpted_rply.ar_stat == SUCCESS) {
245                         error->re_status = RPC_SUCCESS;
246                         return;
247                 };
248                 accepted(msg->acpted_rply.ar_stat, error);
249                 break;
250
251         case MSG_DENIED:
252                 rejected(msg->rjcted_rply.rj_stat, error);
253                 break;
254
255         default:
256                 error->re_status = RPC_FAILED;
257                 error->re_lb.s1 = (long)(msg->rm_reply.rp_stat);
258                 break;
259         }
260         switch (error->re_status) {
261
262         case RPC_VERSMISMATCH:
263                 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
264                 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
265                 break;
266
267         case RPC_AUTHERROR:
268                 error->re_why = msg->rjcted_rply.rj_why;
269                 break;
270
271         case RPC_PROGVERSMISMATCH:
272                 error->re_vers.low = msg->acpted_rply.ar_vers.low;
273                 error->re_vers.high = msg->acpted_rply.ar_vers.high;
274                 break;
275         default:
276                 break;
277         }
278 }