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