1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  */
9 /* @(#)rpc_prot.c   2.3 88/08/07 4.0 RPCSRC */
10 /*
11  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
12  * unrestricted use provided that this legend is included on all tape
13  * media and as a part of the software program in whole or part.  Users
14  * may copy or modify Sun RPC without charge, but are not authorized
15  * to license or distribute it to anyone else except as part of a product or
16  * program developed by the user.
17  *
18  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
19  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
21  *
22  * Sun RPC is provided with no support and without any obligation on the
23  * part of Sun Microsystems, Inc. to assist in its use, correction,
24  * modification or enhancement.
25  *
26  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
27  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
28  * OR ANY PART THEREOF.
29  *
30  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
31  * or profits or other special, indirect and consequential damages, even if
32  * Sun has been advised of the possibility of such damages.
33  *
34  * Sun Microsystems, Inc.
35  * 2550 Garcia Avenue
36  * Mountain View, California  94043
37  */
38 #if !defined(lint) && defined(SCCSIDS)
39 static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
40 #endif
41 
42 /*
43  * rpc_prot.c
44  *
45  * Copyright (C) 1984, Sun Microsystems, Inc.
46  *
47  * This set of routines implements the rpc message definition,
48  * its serializer and some common rpc utility routines.
49  * The routines are meant for various implementations of rpc -
50  * they are NOT for the rpc client or rpc service implementations!
51  * Because authentication stuff is easy and is part of rpc, the opaque
52  * routines are also in this program.
53  */
54 
55 #include <rpc/rpc.h>
56 
57 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
58 
59 /*
60  * XDR an opaque authentication struct
61  * (see auth.h)
62  */
xdr_opaque_auth(XDR * xdrs,struct opaque_auth * ap)63 bool_t xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
64 {
65 
66     if (xdr_enum(xdrs, &(ap->oa_flavor)))
67         return (xdr_bytes(xdrs, &ap->oa_base,
68                           &ap->oa_length, MAX_AUTH_BYTES));
69     return (FALSE);
70 }
71 
72 /*
73  * XDR a DES block
74  */
xdr_des_block(XDR * xdrs,des_block * blkp)75 bool_t xdr_des_block(XDR *xdrs, des_block *blkp)
76 {
77     return (xdr_opaque(xdrs, (char*) blkp, sizeof(des_block)));
78 }
79 
80 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
81 
82 /*
83  * XDR the MSG_ACCEPTED part of a reply message union
84  */
xdr_accepted_reply(XDR * xdrs,struct accepted_reply * ar)85 static bool_t xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
86 {
87 
88     /* personalized union, rather than calling xdr_union */
89     if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
90         return (FALSE);
91     if (!xdr_enum(xdrs, (enum_t *) & (ar->ar_stat)))
92         return (FALSE);
93     switch (ar->ar_stat) {
94 
95     case SUCCESS:
96         return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where));
97 
98     case PROG_MISMATCH:
99         if (!xdr_u_long(xdrs, &(ar->ar_vers.low)))
100             return (FALSE);
101         return (xdr_u_long(xdrs, &(ar->ar_vers.high)));
102     }
103     return (TRUE);              /* TRUE => open ended set of problems */
104 }
105 
106 /*
107  * XDR the MSG_DENIED part of a reply message union
108  */
xdr_rejected_reply(XDR * xdrs,struct rejected_reply * rr)109 static bool_t xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
110 {
111 
112     /* personalized union, rather than calling xdr_union */
113     if (!xdr_enum(xdrs, (enum_t *) & (rr->rj_stat)))
114         return (FALSE);
115     switch (rr->rj_stat) {
116 
117     case RPC_MISMATCH:
118         if (!xdr_u_long(xdrs, &(rr->rj_vers.low)))
119             return (FALSE);
120         return (xdr_u_long(xdrs, &(rr->rj_vers.high)));
121 
122     case AUTH_ERROR:
123         return (xdr_enum(xdrs, (enum_t *) & (rr->rj_why)));
124     }
125     return (FALSE);
126 }
127 
128 static struct xdr_discrim reply_dscrm[3] = {
129     {(int) MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply},
130     {(int) MSG_DENIED, (xdrproc_t)xdr_rejected_reply},
131     {__dontcare__, NULL_xdrproc_t}
132 };
133 
134 /*
135  * XDR a reply message
136  */
xdr_replymsg(XDR * xdrs,struct rpc_msg * rmsg)137 bool_t xdr_replymsg(XDR *xdrs, 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                           (char*) & (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  */
xdr_callhdr(XDR * xdrs,struct rpc_msg * cmsg)154 bool_t xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
155 {
156 
157     cmsg->rm_direction = CALL;
158     cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
159     if (
160         (xdrs->x_op == XDR_ENCODE) &&
161         xdr_u_long(xdrs, &(cmsg->rm_xid)) &&
162         xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) &&
163         xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
164         xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)))
165             return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)));
166     return (FALSE);
167 }
168 
169 /* ************************** Client utility routine ************* */
170 
accepted(enum accept_stat acpt_stat,struct rpc_err * error)171 static void accepted(enum accept_stat acpt_stat, struct rpc_err *error)
172 {
173 
174     switch (acpt_stat) {
175 
176     case PROG_UNAVAIL:
177         error->re_status = RPC_PROGUNAVAIL;
178         return;
179 
180     case PROG_MISMATCH:
181         error->re_status = RPC_PROGVERSMISMATCH;
182         return;
183 
184     case PROC_UNAVAIL:
185         error->re_status = RPC_PROCUNAVAIL;
186         return;
187 
188     case GARBAGE_ARGS:
189         error->re_status = RPC_CANTDECODEARGS;
190         return;
191 
192     case SYSTEM_ERR:
193         error->re_status = RPC_SYSTEMERROR;
194         return;
195 
196     case SUCCESS:
197         error->re_status = RPC_SUCCESS;
198         return;
199     }
200     /* something's wrong, but we don't know what ... */
201     error->re_status = RPC_FAILED;
202     error->re_lb.s1 = (long) MSG_ACCEPTED;
203     error->re_lb.s2 = (long) acpt_stat;
204 }
205 
rejected(enum reject_stat rjct_stat,struct rpc_err * error)206 static void rejected(enum reject_stat rjct_stat, struct rpc_err *error)
207 {
208 
209     switch (rjct_stat) {
210 
211     case RPC_VERSMISMATCH:
212         error->re_status = RPC_VERSMISMATCH;
213         return;
214 
215     case AUTH_ERROR:
216         error->re_status = RPC_AUTHERROR;
217         return;
218     }
219     /* something's wrong, but we don't know what ... */
220     error->re_status = RPC_FAILED;
221     error->re_lb.s1 = (long) MSG_DENIED;
222     error->re_lb.s2 = (long) rjct_stat;
223 }
224 
225 /*
226  * given a reply message, fills in the error
227  */
_seterr_reply(struct rpc_msg * msg,struct rpc_err * error)228 void _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
229 {
230 
231     /* optimized for normal, SUCCESSful case */
232     switch (msg->rm_reply.rp_stat) {
233 
234     case MSG_ACCEPTED:
235         if (msg->acpted_rply.ar_stat == SUCCESS) {
236             error->re_status = RPC_SUCCESS;
237             return;
238         };
239         accepted((enum accept_stat)msg->acpted_rply.ar_stat, error);
240         break;
241 
242     case MSG_DENIED:
243         rejected((enum reject_stat)msg->rjcted_rply.rj_stat, error);
244         break;
245 
246     default:
247         error->re_status = RPC_FAILED;
248         error->re_lb.s1 = (long) (msg->rm_reply.rp_stat);
249         break;
250     }
251     switch (error->re_status) {
252 
253     case RPC_VERSMISMATCH:
254         error->re_vers.low = msg->rjcted_rply.rj_vers.low;
255         error->re_vers.high = msg->rjcted_rply.rj_vers.high;
256         break;
257 
258     case RPC_AUTHERROR:
259         error->re_why = msg->rjcted_rply.rj_why;
260         break;
261 
262     case RPC_PROGVERSMISMATCH:
263         error->re_vers.low = msg->acpted_rply.ar_vers.low;
264         error->re_vers.high = msg->acpted_rply.ar_vers.high;
265         break;
266     }
267 }
268