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