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 | |
---|
19 | /* |
---|
20 | * rpc_prot.c |
---|
21 | */ |
---|
22 | |
---|
23 | #include <rpc/rpc.h> |
---|
24 | |
---|
25 | /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */ |
---|
26 | |
---|
27 | struct opaque_auth _null_auth; |
---|
28 | |
---|
29 | /* |
---|
30 | * XDR an opaque authentication struct |
---|
31 | * (see auth.h) |
---|
32 | */ |
---|
33 | bool_t |
---|
34 | xdr_opaque_auth(xdrs, ap) |
---|
35 | register XDR *xdrs; |
---|
36 | register struct opaque_auth *ap; |
---|
37 | { |
---|
38 | |
---|
39 | if (xdr_enum(xdrs, &(ap->oa_flavor))) |
---|
40 | return (xdr_bytes(xdrs, &ap->oa_base, |
---|
41 | &ap->oa_length, MAX_AUTH_BYTES)); |
---|
42 | return (FALSE); |
---|
43 | } |
---|
44 | |
---|
45 | /* |
---|
46 | * XDR a DES block |
---|
47 | */ |
---|
48 | bool_t |
---|
49 | xdr_des_block(xdrs, blkp) |
---|
50 | register XDR *xdrs; |
---|
51 | register des_block *blkp; |
---|
52 | { |
---|
53 | return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block))); |
---|
54 | } |
---|
55 | |
---|
56 | /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */ |
---|
57 | |
---|
58 | /* |
---|
59 | * XDR the MSG_ACCEPTED part of a reply message union |
---|
60 | */ |
---|
61 | bool_t |
---|
62 | xdr_accepted_reply(xdrs, ar) |
---|
63 | register XDR *xdrs; |
---|
64 | register struct accepted_reply *ar; |
---|
65 | { |
---|
66 | |
---|
67 | /* personalized union, rather than calling xdr_union */ |
---|
68 | if (! xdr_opaque_auth(xdrs, &(ar->ar_verf))) |
---|
69 | return (FALSE); |
---|
70 | if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat))) |
---|
71 | return (FALSE); |
---|
72 | switch (ar->ar_stat) { |
---|
73 | |
---|
74 | case SUCCESS: |
---|
75 | return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where)); |
---|
76 | |
---|
77 | case PROG_MISMATCH: |
---|
78 | if (! xdr_u_long(xdrs, &(ar->ar_vers.low))) |
---|
79 | return (FALSE); |
---|
80 | return (xdr_u_long(xdrs, &(ar->ar_vers.high))); |
---|
81 | default: |
---|
82 | break; |
---|
83 | } |
---|
84 | return (TRUE); /* TRUE => open ended set of problems */ |
---|
85 | } |
---|
86 | |
---|
87 | /* |
---|
88 | * XDR the MSG_DENIED part of a reply message union |
---|
89 | */ |
---|
90 | bool_t |
---|
91 | xdr_rejected_reply(xdrs, rr) |
---|
92 | register XDR *xdrs; |
---|
93 | register struct rejected_reply *rr; |
---|
94 | { |
---|
95 | |
---|
96 | /* personalized union, rather than calling xdr_union */ |
---|
97 | if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat))) |
---|
98 | return (FALSE); |
---|
99 | switch (rr->rj_stat) { |
---|
100 | |
---|
101 | case RPC_MISMATCH: |
---|
102 | if (! xdr_u_long(xdrs, &(rr->rj_vers.low))) |
---|
103 | return (FALSE); |
---|
104 | return (xdr_u_long(xdrs, &(rr->rj_vers.high))); |
---|
105 | |
---|
106 | case AUTH_ERROR: |
---|
107 | return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why))); |
---|
108 | } |
---|
109 | return (FALSE); |
---|
110 | } |
---|
111 | |
---|
112 | static struct xdr_discrim reply_dscrm[3] = { |
---|
113 | { (int)MSG_ACCEPTED, xdr_accepted_reply }, |
---|
114 | { (int)MSG_DENIED, xdr_rejected_reply }, |
---|
115 | { __dontcare__, NULL_xdrproc_t } }; |
---|
116 | |
---|
117 | /* |
---|
118 | * XDR a reply message |
---|
119 | */ |
---|
120 | bool_t |
---|
121 | xdr_replymsg(xdrs, rmsg) |
---|
122 | register XDR *xdrs; |
---|
123 | register struct rpc_msg *rmsg; |
---|
124 | { |
---|
125 | if ( |
---|
126 | xdr_u_long(xdrs, &(rmsg->rm_xid)) && |
---|
127 | xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) && |
---|
128 | (rmsg->rm_direction == REPLY) ) |
---|
129 | return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat), |
---|
130 | (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t)); |
---|
131 | return (FALSE); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | /* |
---|
136 | * Serializes the "static part" of a call message header. |
---|
137 | * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers. |
---|
138 | * The rm_xid is not really static, but the user can easily munge on the fly. |
---|
139 | */ |
---|
140 | bool_t |
---|
141 | xdr_callhdr(xdrs, cmsg) |
---|
142 | register XDR *xdrs; |
---|
143 | register struct rpc_msg *cmsg; |
---|
144 | { |
---|
145 | |
---|
146 | cmsg->rm_direction = CALL; |
---|
147 | cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION; |
---|
148 | if ( |
---|
149 | (xdrs->x_op == XDR_ENCODE) && |
---|
150 | xdr_u_long(xdrs, &(cmsg->rm_xid)) && |
---|
151 | xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) && |
---|
152 | xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) && |
---|
153 | xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)) ) |
---|
154 | return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers))); |
---|
155 | return (FALSE); |
---|
156 | } |
---|
157 | |
---|
158 | /* ************************** Client utility routine ************* */ |
---|
159 | |
---|
160 | static void |
---|
161 | accepted(acpt_stat, error) |
---|
162 | register enum accept_stat acpt_stat; |
---|
163 | register struct rpc_err *error; |
---|
164 | { |
---|
165 | |
---|
166 | switch (acpt_stat) { |
---|
167 | |
---|
168 | case PROG_UNAVAIL: |
---|
169 | error->re_status = RPC_PROGUNAVAIL; |
---|
170 | return; |
---|
171 | |
---|
172 | case PROG_MISMATCH: |
---|
173 | error->re_status = RPC_PROGVERSMISMATCH; |
---|
174 | return; |
---|
175 | |
---|
176 | case PROC_UNAVAIL: |
---|
177 | error->re_status = RPC_PROCUNAVAIL; |
---|
178 | return; |
---|
179 | |
---|
180 | case GARBAGE_ARGS: |
---|
181 | error->re_status = RPC_CANTDECODEARGS; |
---|
182 | return; |
---|
183 | |
---|
184 | case SYSTEM_ERR: |
---|
185 | error->re_status = RPC_SYSTEMERROR; |
---|
186 | return; |
---|
187 | |
---|
188 | case SUCCESS: |
---|
189 | error->re_status = RPC_SUCCESS; |
---|
190 | return; |
---|
191 | } |
---|
192 | /* something's wrong, but we don't know what ... */ |
---|
193 | error->re_status = RPC_FAILED; |
---|
194 | error->re_lb.s1 = (long)MSG_ACCEPTED; |
---|
195 | error->re_lb.s2 = (long)acpt_stat; |
---|
196 | } |
---|
197 | |
---|
198 | static void |
---|
199 | rejected(rjct_stat, error) |
---|
200 | register enum reject_stat rjct_stat; |
---|
201 | register struct rpc_err *error; |
---|
202 | { |
---|
203 | |
---|
204 | switch (rjct_stat) { |
---|
205 | |
---|
206 | case RPC_VERSMISMATCH: |
---|
207 | error->re_status = RPC_VERSMISMATCH; |
---|
208 | return; |
---|
209 | |
---|
210 | case AUTH_ERROR: |
---|
211 | error->re_status = RPC_AUTHERROR; |
---|
212 | return; |
---|
213 | |
---|
214 | default: |
---|
215 | break; |
---|
216 | } |
---|
217 | /* something's wrong, but we don't know what ... */ |
---|
218 | error->re_status = RPC_FAILED; |
---|
219 | error->re_lb.s1 = (long)MSG_DENIED; |
---|
220 | error->re_lb.s2 = (long)rjct_stat; |
---|
221 | } |
---|
222 | |
---|
223 | /* |
---|
224 | * given a reply message, fills in the error |
---|
225 | */ |
---|
226 | void |
---|
227 | _seterr_reply(msg, error) |
---|
228 | register struct rpc_msg *msg; |
---|
229 | register struct rpc_err *error; |
---|
230 | { |
---|
231 | |
---|
232 | /* optimized for normal, SUCCESSful case */ |
---|
233 | switch (msg->rm_reply.rp_stat) { |
---|
234 | |
---|
235 | case MSG_ACCEPTED: |
---|
236 | if (msg->acpted_rply.ar_stat == SUCCESS) { |
---|
237 | error->re_status = RPC_SUCCESS; |
---|
238 | return; |
---|
239 | }; |
---|
240 | accepted(msg->acpted_rply.ar_stat, error); |
---|
241 | break; |
---|
242 | |
---|
243 | case MSG_DENIED: |
---|
244 | rejected(msg->rjcted_rply.rj_stat, error); |
---|
245 | break; |
---|
246 | |
---|
247 | default: |
---|
248 | error->re_status = RPC_FAILED; |
---|
249 | error->re_lb.s1 = (long)(msg->rm_reply.rp_stat); |
---|
250 | break; |
---|
251 | } |
---|
252 | switch (error->re_status) { |
---|
253 | |
---|
254 | case RPC_VERSMISMATCH: |
---|
255 | error->re_vers.low = msg->rjcted_rply.rj_vers.low; |
---|
256 | error->re_vers.high = msg->rjcted_rply.rj_vers.high; |
---|
257 | break; |
---|
258 | |
---|
259 | case RPC_AUTHERROR: |
---|
260 | error->re_why = msg->rjcted_rply.rj_why; |
---|
261 | break; |
---|
262 | |
---|
263 | case RPC_PROGVERSMISMATCH: |
---|
264 | error->re_vers.low = msg->acpted_rply.ar_vers.low; |
---|
265 | error->re_vers.high = msg->acpted_rply.ar_vers.high; |
---|
266 | break; |
---|
267 | |
---|
268 | default: |
---|
269 | break; |
---|
270 | } |
---|
271 | } |
---|