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