]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/rpc/rpc_prot.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.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  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char *sccsid2 = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
34 static char *sccsid = "@(#)rpc_prot.c   2.3 88/08/07 4.0 RPCSRC";
35 #endif
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40  * rpc_prot.c
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  *
44  * This set of routines implements the rpc message definition,
45  * its serializer and some common rpc utility routines.
46  * The routines are meant for various implementations of rpc -
47  * they are NOT for the rpc client or rpc service implementations!
48  * Because authentication stuff is easy and is part of rpc, the opaque
49  * routines are also in this program.
50  */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56
57 #include <rpc/types.h>
58 #include <rpc/xdr.h>
59 #include <rpc/auth.h>
60 #include <rpc/clnt.h>
61 #include <rpc/rpc_msg.h>
62
63 MALLOC_DEFINE(M_RPC, "rpc", "Remote Procedure Call");
64
65 #define assert(exp)     KASSERT(exp, ("bad arguments"))
66
67 static enum clnt_stat accepted(enum accept_stat, struct rpc_err *);
68 static enum clnt_stat rejected(enum reject_stat, struct rpc_err *);
69
70 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
71
72 struct opaque_auth _null_auth;
73
74 /*
75  * XDR an opaque authentication struct
76  * (see auth.h)
77  */
78 bool_t
79 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
80 {
81
82         assert(xdrs != NULL);
83         assert(ap != NULL);
84
85         if (xdr_enum(xdrs, &(ap->oa_flavor)))
86                 return (xdr_bytes(xdrs, &ap->oa_base,
87                         &ap->oa_length, MAX_AUTH_BYTES));
88         return (FALSE);
89 }
90
91 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
92
93 /*
94  * XDR the MSG_ACCEPTED part of a reply message union
95  */
96 bool_t
97 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
98 {
99         enum accept_stat *par_stat;
100
101         assert(xdrs != NULL);
102         assert(ar != NULL);
103
104         par_stat = &ar->ar_stat;
105
106         /* personalized union, rather than calling xdr_union */
107         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
108                 return (FALSE);
109         if (! xdr_enum(xdrs, (enum_t *) par_stat))
110                 return (FALSE);
111         switch (ar->ar_stat) {
112
113         case SUCCESS:
114                 if (ar->ar_results.proc != (xdrproc_t) xdr_void)
115                         return ((*(ar->ar_results.proc))(xdrs,
116                                 ar->ar_results.where));
117                 else
118                         return (TRUE);
119
120         case PROG_MISMATCH:
121                 if (! xdr_uint32_t(xdrs, &(ar->ar_vers.low)))
122                         return (FALSE);
123                 return (xdr_uint32_t(xdrs, &(ar->ar_vers.high)));
124
125         case GARBAGE_ARGS:
126         case SYSTEM_ERR:
127         case PROC_UNAVAIL:
128         case PROG_UNAVAIL:
129                 break;
130         }
131         return (TRUE);  /* TRUE => open ended set of problems */
132 }
133
134 /*
135  * XDR the MSG_DENIED part of a reply message union
136  */
137 bool_t 
138 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
139 {
140         enum reject_stat *prj_stat;
141         enum auth_stat *prj_why;
142
143         assert(xdrs != NULL);
144         assert(rr != NULL);
145
146         prj_stat = &rr->rj_stat;
147
148         /* personalized union, rather than calling xdr_union */
149         if (! xdr_enum(xdrs, (enum_t *) prj_stat))
150                 return (FALSE);
151         switch (rr->rj_stat) {
152
153         case RPC_MISMATCH:
154                 if (! xdr_uint32_t(xdrs, &(rr->rj_vers.low)))
155                         return (FALSE);
156                 return (xdr_uint32_t(xdrs, &(rr->rj_vers.high)));
157
158         case AUTH_ERROR:
159                 prj_why = &rr->rj_why;
160                 return (xdr_enum(xdrs, (enum_t *) prj_why));
161         }
162         /* NOTREACHED */
163         assert(0);
164         return (FALSE);
165 }
166
167 static const struct xdr_discrim reply_dscrm[3] = {
168         { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
169         { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
170         { __dontcare__, NULL_xdrproc_t } };
171
172 /*
173  * XDR a reply message
174  */
175 bool_t
176 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
177 {
178         int32_t *buf;
179         enum msg_type *prm_direction;
180         enum reply_stat *prp_stat;
181
182         assert(xdrs != NULL);
183         assert(rmsg != NULL);
184
185         if (xdrs->x_op == XDR_DECODE) {
186                 buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
187                 if (buf != NULL) {
188                         rmsg->rm_xid = IXDR_GET_UINT32(buf);
189                         rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
190                         if (rmsg->rm_direction != REPLY) {
191                                 return (FALSE);
192                         }
193                         rmsg->rm_reply.rp_stat =
194                                 IXDR_GET_ENUM(buf, enum reply_stat);
195                         if (rmsg->rm_reply.rp_stat == MSG_ACCEPTED)
196                                 return (xdr_accepted_reply(xdrs,
197                                         &rmsg->acpted_rply));
198                         else if (rmsg->rm_reply.rp_stat == MSG_DENIED)
199                                 return (xdr_rejected_reply(xdrs,
200                                         &rmsg->rjcted_rply));
201                         else
202                                 return (FALSE);
203                 }
204         }
205
206         prm_direction = &rmsg->rm_direction;
207         prp_stat = &rmsg->rm_reply.rp_stat;
208
209         if (
210             xdr_uint32_t(xdrs, &(rmsg->rm_xid)) && 
211             xdr_enum(xdrs, (enum_t *) prm_direction) &&
212             (rmsg->rm_direction == REPLY) )
213                 return (xdr_union(xdrs, (enum_t *) prp_stat,
214                    (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
215                    NULL_xdrproc_t));
216         return (FALSE);
217 }
218
219
220 /*
221  * Serializes the "static part" of a call message header.
222  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
223  * The rm_xid is not really static, but the user can easily munge on the fly.
224  */
225 bool_t
226 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
227 {
228         enum msg_type *prm_direction;
229
230         assert(xdrs != NULL);
231         assert(cmsg != NULL);
232
233         prm_direction = &cmsg->rm_direction;
234
235         cmsg->rm_direction = CALL;
236         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
237         if (
238             (xdrs->x_op == XDR_ENCODE) &&
239             xdr_uint32_t(xdrs, &(cmsg->rm_xid)) &&
240             xdr_enum(xdrs, (enum_t *) prm_direction) &&
241             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
242             xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
243                 return (xdr_uint32_t(xdrs, &(cmsg->rm_call.cb_vers)));
244         return (FALSE);
245 }
246
247 /* ************************** Client utility routine ************* */
248
249 static enum clnt_stat
250 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
251 {
252
253         assert(error != NULL);
254
255         switch (acpt_stat) {
256
257         case PROG_UNAVAIL:
258                 error->re_status = RPC_PROGUNAVAIL;
259                 return (RPC_PROGUNAVAIL);
260
261         case PROG_MISMATCH:
262                 error->re_status = RPC_PROGVERSMISMATCH;
263                 return (RPC_PROGVERSMISMATCH);
264
265         case PROC_UNAVAIL:
266                 return (RPC_PROCUNAVAIL);
267
268         case GARBAGE_ARGS:
269                 return (RPC_CANTDECODEARGS);
270
271         case SYSTEM_ERR:
272                 return (RPC_SYSTEMERROR);
273
274         case SUCCESS:
275                 return (RPC_SUCCESS);
276         }
277         /* NOTREACHED */
278         /* something's wrong, but we don't know what ... */
279         error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
280         error->re_lb.s2 = (int32_t)acpt_stat;
281         return (RPC_FAILED);
282 }
283
284 static enum clnt_stat
285 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
286 {
287
288         assert(error != NULL);
289
290         switch (rjct_stat) {
291         case RPC_MISMATCH:
292                 return (RPC_VERSMISMATCH);
293
294         case AUTH_ERROR:
295                 return (RPC_AUTHERROR);
296         }
297         /* something's wrong, but we don't know what ... */
298         /* NOTREACHED */
299         error->re_lb.s1 = (int32_t)MSG_DENIED;
300         error->re_lb.s2 = (int32_t)rjct_stat;
301         return (RPC_FAILED);
302 }
303
304 /*
305  * given a reply message, fills in the error
306  */
307 enum clnt_stat
308 _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
309 {
310         enum clnt_stat stat;
311
312         assert(msg != NULL);
313         assert(error != NULL);
314
315         /* optimized for normal, SUCCESSful case */
316         switch (msg->rm_reply.rp_stat) {
317
318         case MSG_ACCEPTED:
319                 if (msg->acpted_rply.ar_stat == SUCCESS) {
320                         stat = RPC_SUCCESS;
321                         return (stat);
322                 }
323                 stat = accepted(msg->acpted_rply.ar_stat, error);
324                 break;
325
326         case MSG_DENIED:
327                 stat = rejected(msg->rjcted_rply.rj_stat, error);
328                 break;
329
330         default:
331                 stat = RPC_FAILED;
332                 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
333                 break;
334         }
335         error->re_status = stat;
336
337         switch (stat) {
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
372         return (stat);
373 }