]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/rpc/rpc_prot.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / 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 <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55
56 #include <rpc/types.h>
57 #include <rpc/xdr.h>
58 #include <rpc/auth.h>
59 #include <rpc/clnt.h>
60 #include <rpc/rpc_msg.h>
61
62 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
63
64 #define assert(exp)     KASSERT(exp, ("bad arguments"))
65
66 static enum clnt_stat accepted(enum accept_stat, struct rpc_err *);
67 static enum clnt_stat rejected(enum reject_stat, struct rpc_err *);
68
69 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
70
71 struct opaque_auth _null_auth;
72
73 /*
74  * XDR an opaque authentication struct
75  * (see auth.h)
76  */
77 bool_t
78 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
79 {
80
81         assert(xdrs != NULL);
82         assert(ap != NULL);
83
84         if (xdr_enum(xdrs, &(ap->oa_flavor)))
85                 return (xdr_bytes(xdrs, &ap->oa_base,
86                         &ap->oa_length, MAX_AUTH_BYTES));
87         return (FALSE);
88 }
89
90 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
91
92 /*
93  * XDR the MSG_ACCEPTED part of a reply message union
94  */
95 bool_t
96 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
97 {
98         enum accept_stat *par_stat;
99
100         assert(xdrs != NULL);
101         assert(ar != NULL);
102
103         par_stat = &ar->ar_stat;
104
105         /* personalized union, rather than calling xdr_union */
106         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
107                 return (FALSE);
108         if (! xdr_enum(xdrs, (enum_t *) par_stat))
109                 return (FALSE);
110         switch (ar->ar_stat) {
111
112         case SUCCESS:
113                 if (ar->ar_results.proc != (xdrproc_t) xdr_void)
114                         return ((*(ar->ar_results.proc))(xdrs,
115                                 ar->ar_results.where));
116                 else
117                         return (TRUE);
118
119         case PROG_MISMATCH:
120                 if (! xdr_uint32_t(xdrs, &(ar->ar_vers.low)))
121                         return (FALSE);
122                 return (xdr_uint32_t(xdrs, &(ar->ar_vers.high)));
123
124         case GARBAGE_ARGS:
125         case SYSTEM_ERR:
126         case PROC_UNAVAIL:
127         case PROG_UNAVAIL:
128                 break;
129         }
130         return (TRUE);  /* TRUE => open ended set of problems */
131 }
132
133 /*
134  * XDR the MSG_DENIED part of a reply message union
135  */
136 bool_t 
137 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
138 {
139         enum reject_stat *prj_stat;
140         enum auth_stat *prj_why;
141
142         assert(xdrs != NULL);
143         assert(rr != NULL);
144
145         prj_stat = &rr->rj_stat;
146
147         /* personalized union, rather than calling xdr_union */
148         if (! xdr_enum(xdrs, (enum_t *) prj_stat))
149                 return (FALSE);
150         switch (rr->rj_stat) {
151
152         case RPC_MISMATCH:
153                 if (! xdr_uint32_t(xdrs, &(rr->rj_vers.low)))
154                         return (FALSE);
155                 return (xdr_uint32_t(xdrs, &(rr->rj_vers.high)));
156
157         case AUTH_ERROR:
158                 prj_why = &rr->rj_why;
159                 return (xdr_enum(xdrs, (enum_t *) prj_why));
160         }
161         /* NOTREACHED */
162         assert(0);
163         return (FALSE);
164 }
165
166 static const struct xdr_discrim reply_dscrm[3] = {
167         { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
168         { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
169         { __dontcare__, NULL_xdrproc_t } };
170
171 /*
172  * XDR a reply message
173  */
174 bool_t
175 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
176 {
177         int32_t *buf;
178         enum msg_type *prm_direction;
179         enum reply_stat *prp_stat;
180
181         assert(xdrs != NULL);
182         assert(rmsg != NULL);
183
184         if (xdrs->x_op == XDR_DECODE) {
185                 buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
186                 if (buf != NULL) {
187                         rmsg->rm_xid = IXDR_GET_UINT32(buf);
188                         rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
189                         if (rmsg->rm_direction != REPLY) {
190                                 return (FALSE);
191                         }
192                         rmsg->rm_reply.rp_stat =
193                                 IXDR_GET_ENUM(buf, enum reply_stat);
194                         if (rmsg->rm_reply.rp_stat == MSG_ACCEPTED)
195                                 return (xdr_accepted_reply(xdrs,
196                                         &rmsg->acpted_rply));
197                         else if (rmsg->rm_reply.rp_stat == MSG_DENIED)
198                                 return (xdr_rejected_reply(xdrs,
199                                         &rmsg->rjcted_rply));
200                         else
201                                 return (FALSE);
202                 }
203         }
204
205         prm_direction = &rmsg->rm_direction;
206         prp_stat = &rmsg->rm_reply.rp_stat;
207
208         if (
209             xdr_uint32_t(xdrs, &(rmsg->rm_xid)) && 
210             xdr_enum(xdrs, (enum_t *) prm_direction) &&
211             (rmsg->rm_direction == REPLY) )
212                 return (xdr_union(xdrs, (enum_t *) prp_stat,
213                    (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
214                    NULL_xdrproc_t));
215         return (FALSE);
216 }
217
218
219 /*
220  * Serializes the "static part" of a call message header.
221  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
222  * The rm_xid is not really static, but the user can easily munge on the fly.
223  */
224 bool_t
225 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
226 {
227         enum msg_type *prm_direction;
228
229         assert(xdrs != NULL);
230         assert(cmsg != NULL);
231
232         prm_direction = &cmsg->rm_direction;
233
234         cmsg->rm_direction = CALL;
235         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
236         if (
237             (xdrs->x_op == XDR_ENCODE) &&
238             xdr_uint32_t(xdrs, &(cmsg->rm_xid)) &&
239             xdr_enum(xdrs, (enum_t *) prm_direction) &&
240             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
241             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
242                 return (xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_vers)));
243         return (FALSE);
244 }
245
246 /* ************************** Client utility routine ************* */
247
248 static enum clnt_stat
249 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
250 {
251
252         assert(error != NULL);
253
254         switch (acpt_stat) {
255
256         case PROG_UNAVAIL:
257                 error->re_status = RPC_PROGUNAVAIL;
258                 return (RPC_PROGUNAVAIL);
259
260         case PROG_MISMATCH:
261                 error->re_status = RPC_PROGVERSMISMATCH;
262                 return (RPC_PROGVERSMISMATCH);
263
264         case PROC_UNAVAIL:
265                 return (RPC_PROCUNAVAIL);
266
267         case GARBAGE_ARGS:
268                 return (RPC_CANTDECODEARGS);
269
270         case SYSTEM_ERR:
271                 return (RPC_SYSTEMERROR);
272
273         case SUCCESS:
274                 return (RPC_SUCCESS);
275         }
276         /* NOTREACHED */
277         /* something's wrong, but we don't know what ... */
278         error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
279         error->re_lb.s2 = (int32_t)acpt_stat;
280         return (RPC_FAILED);
281 }
282
283 static enum clnt_stat
284 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
285 {
286
287         assert(error != NULL);
288
289         switch (rjct_stat) {
290         case RPC_MISMATCH:
291                 return (RPC_VERSMISMATCH);
292
293         case AUTH_ERROR:
294                 return (RPC_AUTHERROR);
295         }
296         /* something's wrong, but we don't know what ... */
297         /* NOTREACHED */
298         error->re_lb.s1 = (int32_t)MSG_DENIED;
299         error->re_lb.s2 = (int32_t)rjct_stat;
300         return (RPC_FAILED);
301 }
302
303 /*
304  * given a reply message, fills in the error
305  */
306 enum clnt_stat
307 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
308 {
309         enum clnt_stat stat;
310
311         assert(msg != NULL);
312         assert(error != NULL);
313
314         /* optimized for normal, SUCCESSful case */
315         switch (msg->rm_reply.rp_stat) {
316
317         case MSG_ACCEPTED:
318                 if (msg->acpted_rply.ar_stat == SUCCESS) {
319                         stat = RPC_SUCCESS;
320                         return (stat);
321                 }
322                 stat = accepted(msg->acpted_rply.ar_stat, error);
323                 break;
324
325         case MSG_DENIED:
326                 stat = rejected(msg->rjcted_rply.rj_stat, error);
327                 break;
328
329         default:
330                 stat = RPC_FAILED;
331                 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
332                 break;
333         }
334         error->re_status = stat;
335
336         switch (stat) {
337
338         case RPC_VERSMISMATCH:
339                 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
340                 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
341                 break;
342
343         case RPC_AUTHERROR:
344                 error->re_why = msg->rjcted_rply.rj_why;
345                 break;
346
347         case RPC_PROGVERSMISMATCH:
348                 error->re_vers.low = msg->acpted_rply.ar_vers.low;
349                 error->re_vers.high = msg->acpted_rply.ar_vers.high;
350                 break;
351
352         case RPC_FAILED:
353         case RPC_SUCCESS:
354         case RPC_PROGNOTREGISTERED:
355         case RPC_PMAPFAILURE:
356         case RPC_UNKNOWNPROTO:
357         case RPC_UNKNOWNHOST:
358         case RPC_SYSTEMERROR:
359         case RPC_CANTDECODEARGS:
360         case RPC_PROCUNAVAIL:
361         case RPC_PROGUNAVAIL:
362         case RPC_TIMEDOUT:
363         case RPC_CANTRECV:
364         case RPC_CANTSEND:
365         case RPC_CANTDECODERES:
366         case RPC_CANTENCODEARGS:
367         default:
368                 break;
369         }
370
371         return (stat);
372 }