79a58c27325866111a1a01b939777af71be3661d
[platform/upstream/glibc.git] / sunrpc / rpc_prot.c
1 /* @(#)rpc_prot.c       2.3 88/08/07 4.0 RPCSRC */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 #if !defined(lint) && defined(SCCSIDS)
31 static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
32 #endif
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 <sys/param.h>
48
49 #include <rpc/rpc.h>
50
51 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
52
53 /*
54  * XDR an opaque authentication struct
55  * (see auth.h)
56  */
57 bool_t
58 xdr_opaque_auth (XDR *xdrs, struct opaque_auth *ap)
59 {
60
61   if (xdr_enum (xdrs, &(ap->oa_flavor)))
62     return xdr_bytes (xdrs, &ap->oa_base,
63                       &ap->oa_length, MAX_AUTH_BYTES);
64   return FALSE;
65 }
66
67 /*
68  * XDR a DES block
69  */
70 bool_t
71 xdr_des_block (XDR *xdrs, des_block *blkp)
72 {
73   return xdr_opaque (xdrs, (caddr_t) blkp, sizeof (des_block));
74 }
75
76 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
77
78 /*
79  * XDR the MSG_ACCEPTED part of a reply message union
80  */
81 bool_t
82 xdr_accepted_reply (XDR *xdrs, struct accepted_reply *ar)
83 {
84   /* personalized union, rather than calling xdr_union */
85   if (!xdr_opaque_auth (xdrs, &(ar->ar_verf)))
86     return FALSE;
87   if (!xdr_enum (xdrs, (enum_t *) & (ar->ar_stat)))
88     return FALSE;
89   switch (ar->ar_stat)
90     {
91     case SUCCESS:
92       return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
93     case PROG_MISMATCH:
94       if (!xdr_u_long (xdrs, &(ar->ar_vers.low)))
95         return FALSE;
96       return (xdr_u_long (xdrs, &(ar->ar_vers.high)));
97     default:
98       return TRUE;
99     }
100   return TRUE;          /* TRUE => open ended set of problems */
101 }
102
103 /*
104  * XDR the MSG_DENIED part of a reply message union
105  */
106 bool_t
107 xdr_rejected_reply (XDR *xdrs, struct rejected_reply *rr)
108 {
109   /* personalized union, rather than calling xdr_union */
110   if (!xdr_enum (xdrs, (enum_t *) & (rr->rj_stat)))
111     return FALSE;
112   switch (rr->rj_stat)
113     {
114     case RPC_MISMATCH:
115       if (!xdr_u_long (xdrs, &(rr->rj_vers.low)))
116         return FALSE;
117       return xdr_u_long (xdrs, &(rr->rj_vers.high));
118
119     case AUTH_ERROR:
120       return xdr_enum (xdrs, (enum_t *) & (rr->rj_why));
121     }
122   return FALSE;
123 }
124
125 static const struct xdr_discrim reply_dscrm[3] =
126 {
127   {(int) MSG_ACCEPTED, (xdrproc_t) xdr_accepted_reply},
128   {(int) MSG_DENIED, (xdrproc_t) xdr_rejected_reply},
129   {__dontcare__, NULL_xdrproc_t}};
130
131 /*
132  * XDR a reply message
133  */
134 bool_t
135 xdr_replymsg (xdrs, rmsg)
136      XDR *xdrs;
137      struct rpc_msg *rmsg;
138 {
139   if (xdr_u_long (xdrs, &(rmsg->rm_xid)) &&
140       xdr_enum (xdrs, (enum_t *) & (rmsg->rm_direction)) &&
141       (rmsg->rm_direction == REPLY))
142     return xdr_union (xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat),
143                       (caddr_t) & (rmsg->rm_reply.ru), reply_dscrm,
144                       NULL_xdrproc_t);
145   return FALSE;
146 }
147
148
149 /*
150  * Serializes the "static part" of a call message header.
151  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
152  * The rm_xid is not really static, but the user can easily munge on the fly.
153  */
154 bool_t
155 xdr_callhdr (xdrs, cmsg)
156      XDR *xdrs;
157      struct rpc_msg *cmsg;
158 {
159
160   cmsg->rm_direction = CALL;
161   cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
162   if (
163        (xdrs->x_op == XDR_ENCODE) &&
164        xdr_u_long (xdrs, &(cmsg->rm_xid)) &&
165        xdr_enum (xdrs, (enum_t *) & (cmsg->rm_direction)) &&
166        xdr_u_long (xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
167        xdr_u_long (xdrs, &(cmsg->rm_call.cb_prog)))
168     return xdr_u_long (xdrs, &(cmsg->rm_call.cb_vers));
169   return FALSE;
170 }
171
172 /* ************************** Client utility routine ************* */
173
174 static void
175 accepted (enum accept_stat acpt_stat,
176           struct rpc_err *error)
177 {
178   switch (acpt_stat)
179     {
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,
213           struct rpc_err *error)
214 {
215   switch (rjct_stat)
216     {
217     case RPC_VERSMISMATCH:
218       error->re_status = RPC_VERSMISMATCH;
219       return;
220     case AUTH_ERROR:
221       error->re_status = RPC_AUTHERROR;
222       return;
223     default:
224       /* something's wrong, but we don't know what ... */
225       error->re_status = RPC_FAILED;
226       error->re_lb.s1 = (long) MSG_DENIED;
227       error->re_lb.s2 = (long) rjct_stat;
228       return;
229     }
230 }
231
232 /*
233  * given a reply message, fills in the error
234  */
235 void
236 _seterr_reply (struct rpc_msg *msg,
237                struct rpc_err *error)
238 {
239   /* optimized for normal, SUCCESSful case */
240   switch (msg->rm_reply.rp_stat)
241     {
242     case MSG_ACCEPTED:
243       if (msg->acpted_rply.ar_stat == SUCCESS)
244         {
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
263     case RPC_VERSMISMATCH:
264       error->re_vers.low = msg->rjcted_rply.rj_vers.low;
265       error->re_vers.high = msg->rjcted_rply.rj_vers.high;
266       break;
267
268     case RPC_AUTHERROR:
269       error->re_why = msg->rjcted_rply.rj_why;
270       break;
271
272     case RPC_PROGVERSMISMATCH:
273       error->re_vers.low = msg->acpted_rply.ar_vers.low;
274       error->re_vers.high = msg->acpted_rply.ar_vers.high;
275       break;
276     default:
277       break;
278     }
279 }