Imported Upstream version 0.2.2
[platform/upstream/libtirpc.git] / src / 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
29 /*
30  * rpc_prot.c
31  *
32  * Copyright (C) 1984, Sun Microsystems, Inc.
33  *
34  * This set of routines implements the rpc message definition,
35  * its serializer and some common rpc utility routines.
36  * The routines are meant for various implementations of rpc -
37  * they are NOT for the rpc client or rpc service implementations!
38  * Because authentication stuff is easy and is part of rpc, the opaque
39  * routines are also in this program.
40  */
41
42 #include <sys/param.h>
43
44 #include <assert.h>
45
46 #include <rpc/rpc.h>
47
48 static void accepted(enum accept_stat, struct rpc_err *);
49 static void rejected(enum reject_stat, struct rpc_err *);
50
51 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
52
53 extern struct opaque_auth _null_auth;
54
55 /*
56  * XDR an opaque authentication struct
57  * (see auth.h)
58  */
59 bool_t
60 xdr_opaque_auth(xdrs, ap)
61         XDR *xdrs;
62         struct opaque_auth *ap;
63 {
64
65         assert(xdrs != NULL);
66         assert(ap != NULL);
67
68         if (xdr_enum(xdrs, &(ap->oa_flavor)))
69                 return (xdr_bytes(xdrs, &ap->oa_base,
70                         &ap->oa_length, MAX_AUTH_BYTES));
71         return (FALSE);
72 }
73
74 /*
75  * XDR a DES block
76  */
77 bool_t
78 xdr_des_block(xdrs, blkp)
79         XDR *xdrs;
80         des_block *blkp;
81 {
82
83         assert(xdrs != NULL);
84         assert(blkp != NULL);
85
86         return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
87 }
88
89 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
90
91 /*
92  * XDR the MSG_ACCEPTED part of a reply message union
93  */
94 bool_t
95 xdr_accepted_reply(xdrs, ar)
96         XDR *xdrs;   
97         struct accepted_reply *ar;
98 {
99
100         assert(xdrs != NULL);
101         assert(ar != NULL);
102
103         /* personalized union, rather than calling xdr_union */
104         if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
105                 return (FALSE);
106         if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
107                 return (FALSE);
108         switch (ar->ar_stat) {
109
110         case SUCCESS:
111                 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
112
113         case PROG_MISMATCH:
114                 if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
115                         return (FALSE);
116                 return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
117
118         case GARBAGE_ARGS:
119         case SYSTEM_ERR:
120         case PROC_UNAVAIL:
121         case PROG_UNAVAIL:
122                 break;
123         }
124         return (TRUE);  /* TRUE => open ended set of problems */
125 }
126
127 /*
128  * XDR the MSG_DENIED part of a reply message union
129  */
130 bool_t 
131 xdr_rejected_reply(xdrs, rr)
132         XDR *xdrs;
133         struct rejected_reply *rr;
134 {
135
136         assert(xdrs != NULL);
137         assert(rr != NULL);
138
139         /* personalized union, rather than calling xdr_union */
140         if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
141                 return (FALSE);
142         switch (rr->rj_stat) {
143
144         case RPC_MISMATCH:
145                 if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
146                         return (FALSE);
147                 return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
148
149         case AUTH_ERROR:
150                 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
151         }
152         /* NOTREACHED */
153         assert(0);
154         return (FALSE);
155 }
156
157 static const struct xdr_discrim reply_dscrm[3] = {
158         { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
159         { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
160         { __dontcare__, NULL_xdrproc_t } };
161
162 /*
163  * XDR a reply message
164  */
165 bool_t
166 xdr_replymsg(xdrs, rmsg)
167         XDR *xdrs;
168         struct rpc_msg *rmsg;
169 {
170         assert(xdrs != NULL);
171         assert(rmsg != NULL);
172
173         if (
174             xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) && 
175             xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
176             (rmsg->rm_direction == REPLY) )
177                 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
178                    (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
179                    NULL_xdrproc_t));
180         return (FALSE);
181 }
182
183
184 /*
185  * Serializes the "static part" of a call message header.
186  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
187  * The rm_xid is not really static, but the user can easily munge on the fly.
188  */
189 bool_t
190 xdr_callhdr(xdrs, cmsg)
191         XDR *xdrs;
192         struct rpc_msg *cmsg;
193 {
194
195         assert(xdrs != NULL);
196         assert(cmsg != NULL);
197
198         cmsg->rm_direction = CALL;
199         cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
200         if (
201             (xdrs->x_op == XDR_ENCODE) &&
202             xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
203             xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
204             xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
205             xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
206                 return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
207         return (FALSE);
208 }
209
210 /* ************************** Client utility routine ************* */
211
212 static void
213 accepted(acpt_stat, error)
214         enum accept_stat acpt_stat;
215         struct rpc_err *error;
216 {
217
218         assert(error != NULL);
219
220         switch (acpt_stat) {
221
222         case PROG_UNAVAIL:
223                 error->re_status = RPC_PROGUNAVAIL;
224                 return;
225
226         case PROG_MISMATCH:
227                 error->re_status = RPC_PROGVERSMISMATCH;
228                 return;
229
230         case PROC_UNAVAIL:
231                 error->re_status = RPC_PROCUNAVAIL;
232                 return;
233
234         case GARBAGE_ARGS:
235                 error->re_status = RPC_CANTDECODEARGS;
236                 return;
237
238         case SYSTEM_ERR:
239                 error->re_status = RPC_SYSTEMERROR;
240                 return;
241
242         case SUCCESS:
243                 error->re_status = RPC_SUCCESS;
244                 return;
245         }
246         /* NOTREACHED */
247         /* something's wrong, but we don't know what ... */
248         error->re_status = RPC_FAILED;
249         error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
250         error->re_lb.s2 = (int32_t)acpt_stat;
251 }
252
253 static void 
254 rejected(rjct_stat, error)
255         enum reject_stat rjct_stat;
256         struct rpc_err *error;
257 {
258
259         assert(error != NULL);
260
261         switch (rjct_stat) {
262         case RPC_MISMATCH:
263                 error->re_status = RPC_VERSMISMATCH;
264                 return;
265
266         case AUTH_ERROR:
267                 error->re_status = RPC_AUTHERROR;
268                 return;
269         }
270         /* something's wrong, but we don't know what ... */
271         /* NOTREACHED */
272         error->re_status = RPC_FAILED;
273         error->re_lb.s1 = (int32_t)MSG_DENIED;
274         error->re_lb.s2 = (int32_t)rjct_stat;
275 }
276
277 /*
278  * given a reply message, fills in the error
279  */
280 void
281 _seterr_reply(msg, error)
282         struct rpc_msg *msg;
283         struct rpc_err *error;
284 {
285
286         assert(msg != NULL);
287         assert(error != NULL);
288
289         /* optimized for normal, SUCCESSful case */
290         switch (msg->rm_reply.rp_stat) {
291
292         case MSG_ACCEPTED:
293                 if (msg->acpted_rply.ar_stat == SUCCESS) {
294                         error->re_status = RPC_SUCCESS;
295                         return;
296                 }
297                 accepted(msg->acpted_rply.ar_stat, error);
298                 break;
299
300         case MSG_DENIED:
301                 rejected(msg->rjcted_rply.rj_stat, error);
302                 break;
303
304         default:
305                 error->re_status = RPC_FAILED;
306                 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
307                 break;
308         }
309         switch (error->re_status) {
310
311         case RPC_VERSMISMATCH:
312                 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
313                 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
314                 break;
315
316         case RPC_AUTHERROR:
317                 error->re_why = msg->rjcted_rply.rj_why;
318                 break;
319
320         case RPC_PROGVERSMISMATCH:
321                 error->re_vers.low = msg->acpted_rply.ar_vers.low;
322                 error->re_vers.high = msg->acpted_rply.ar_vers.high;
323                 break;
324
325         case RPC_FAILED:
326         case RPC_SUCCESS:
327         case RPC_PROGNOTREGISTERED:
328         case RPC_PMAPFAILURE:
329         case RPC_UNKNOWNPROTO:
330         case RPC_UNKNOWNHOST:
331         case RPC_SYSTEMERROR:
332         case RPC_CANTDECODEARGS:
333         case RPC_PROCUNAVAIL:
334         case RPC_PROGUNAVAIL:
335         case RPC_TIMEDOUT:
336         case RPC_CANTRECV:
337         case RPC_CANTSEND:
338         case RPC_CANTDECODERES:
339         case RPC_CANTENCODEARGS:
340         default:
341                 break;
342         }
343 }