Libinfo-517.200.9.tar.gz
[apple/libinfo.git] / rpc.subproj / rpc_prot.c
1 /*
2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";*/
55 static char *rcsid = "$Id: rpc_prot.c,v 1.3 2003/06/23 17:24:59 majka Exp $";
56 #endif
57
58 /*
59 * rpc_prot.c
60 *
61 * Copyright (C) 1984, Sun Microsystems, Inc.
62 *
63 * This set of routines implements the rpc message definition,
64 * its serializer and some common rpc utility routines.
65 * The routines are meant for various implementations of rpc -
66 * they are NOT for the rpc client or rpc service implementations!
67 * Because authentication stuff is easy and is part of rpc, the opaque
68 * routines are also in this program.
69 */
70
71 #include "libinfo_common.h"
72
73 #include <sys/param.h>
74
75 #include <rpc/rpc.h>
76
77 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
78
79 #if defined(__APPLE__)
80 extern
81 #endif
82 struct opaque_auth _null_auth;
83
84 /*
85 * XDR an opaque authentication struct
86 * (see auth.h)
87 */
88 bool_t
89 xdr_opaque_auth(xdrs, ap)
90 register XDR *xdrs;
91 register struct opaque_auth *ap;
92 {
93
94 if (xdr_enum(xdrs, &(ap->oa_flavor)))
95 return (xdr_bytes(xdrs, &ap->oa_base,
96 &ap->oa_length, MAX_AUTH_BYTES));
97 return (FALSE);
98 }
99
100 /*
101 * XDR a DES block
102 */
103 LIBINFO_EXPORT
104 bool_t
105 xdr_des_block(xdrs, blkp)
106 register XDR *xdrs;
107 register des_block *blkp;
108 {
109 return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block)));
110 }
111
112 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
113
114 /*
115 * XDR the MSG_ACCEPTED part of a reply message union
116 */
117 bool_t
118 xdr_accepted_reply(xdrs, ar)
119 register XDR *xdrs;
120 register struct accepted_reply *ar;
121 {
122
123 /* personalized union, rather than calling xdr_union */
124 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
125 return (FALSE);
126 if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
127 return (FALSE);
128 switch (ar->ar_stat) {
129
130 case SUCCESS:
131 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where, 0));
132
133 case PROG_MISMATCH:
134 if (! xdr_u_long(xdrs, &(ar->ar_vers.low)))
135 return (FALSE);
136 return (xdr_u_long(xdrs, &(ar->ar_vers.high)));
137 default: break;
138 }
139 return (TRUE); /* TRUE => open ended set of problems */
140 }
141
142 /*
143 * XDR the MSG_DENIED part of a reply message union
144 */
145 bool_t
146 xdr_rejected_reply(xdrs, rr)
147 register XDR *xdrs;
148 register struct rejected_reply *rr;
149 {
150
151 /* personalized union, rather than calling xdr_union */
152 if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
153 return (FALSE);
154 switch (rr->rj_stat) {
155
156 case RPC_MISMATCH:
157 if (! xdr_u_long(xdrs, &(rr->rj_vers.low)))
158 return (FALSE);
159 return (xdr_u_long(xdrs, &(rr->rj_vers.high)));
160
161 case AUTH_ERROR:
162 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
163 }
164 return (FALSE);
165 }
166
167 static struct xdr_discrim reply_dscrm[3] = {
168 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
169 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
170 { __dontcare__, NULL_xdrproc_t } };
171
172 /*
173 * XDR a reply message
174 */
175 LIBINFO_EXPORT
176 bool_t
177 xdr_replymsg(xdrs, rmsg)
178 register XDR *xdrs;
179 register struct rpc_msg *rmsg;
180 {
181 if (
182 xdr_u_long(xdrs, &(rmsg->rm_xid)) &&
183 xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
184 (rmsg->rm_direction == REPLY) )
185 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
186 (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));
187 return (FALSE);
188 }
189
190
191 /*
192 * Serializes the "static part" of a call message header.
193 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
194 * The rm_xid is not really static, but the user can easily munge on the fly.
195 */
196 LIBINFO_EXPORT
197 bool_t
198 xdr_callhdr(xdrs, cmsg)
199 register XDR *xdrs;
200 register struct rpc_msg *cmsg;
201 {
202
203 cmsg->rm_direction = CALL;
204 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
205 if (
206 (xdrs->x_op == XDR_ENCODE) &&
207 xdr_u_long(xdrs, &(cmsg->rm_xid)) &&
208 xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
209 xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
210 xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)) )
211 return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)));
212 return (FALSE);
213 }
214
215 /* ************************** Client utility routine ************* */
216
217 static void
218 accepted(acpt_stat, error)
219 register enum accept_stat acpt_stat;
220 register struct rpc_err *error;
221 {
222
223 switch (acpt_stat) {
224
225 case PROG_UNAVAIL:
226 error->re_status = RPC_PROGUNAVAIL;
227 return;
228
229 case PROG_MISMATCH:
230 error->re_status = RPC_PROGVERSMISMATCH;
231 return;
232
233 case PROC_UNAVAIL:
234 error->re_status = RPC_PROCUNAVAIL;
235 return;
236
237 case GARBAGE_ARGS:
238 error->re_status = RPC_CANTDECODEARGS;
239 return;
240
241 case SYSTEM_ERR:
242 error->re_status = RPC_SYSTEMERROR;
243 return;
244
245 case SUCCESS:
246 error->re_status = RPC_SUCCESS;
247 return;
248 }
249 /* something's wrong, but we don't know what ... */
250 error->re_status = RPC_FAILED;
251 #ifdef __LP64__
252 error->re_lb.s1 = (int)MSG_ACCEPTED;
253 error->re_lb.s2 = (int)acpt_stat;
254 #else
255 error->re_lb.s1 = (long)MSG_ACCEPTED;
256 error->re_lb.s2 = (long)acpt_stat;
257 #endif
258 }
259
260 static void
261 rejected(rjct_stat, error)
262 register enum reject_stat rjct_stat;
263 register struct rpc_err *error;
264 {
265
266 switch (rjct_stat) {
267
268 case RPC_MISMATCH:
269 error->re_status = RPC_VERSMISMATCH;
270 return;
271
272 case AUTH_ERROR:
273 error->re_status = RPC_AUTHERROR;
274 return;
275 default: break;
276 }
277 /* something's wrong, but we don't know what ... */
278 error->re_status = RPC_FAILED;
279 #ifdef __LP64__
280 error->re_lb.s1 = (int)MSG_DENIED;
281 error->re_lb.s2 = (int)rjct_stat;
282 #else
283 error->re_lb.s1 = (long)MSG_DENIED;
284 error->re_lb.s2 = (long)rjct_stat;
285 #endif
286 }
287
288 /*
289 * given a reply message, fills in the error
290 */
291 LIBINFO_EXPORT
292 void
293 _seterr_reply(msg, error)
294 register struct rpc_msg *msg;
295 register struct rpc_err *error;
296 {
297
298 /* optimized for normal, SUCCESSful case */
299 switch (msg->rm_reply.rp_stat) {
300
301 case MSG_ACCEPTED:
302 if (msg->acpted_rply.ar_stat == SUCCESS) {
303 error->re_status = RPC_SUCCESS;
304 return;
305 };
306 accepted(msg->acpted_rply.ar_stat, error);
307 break;
308
309 case MSG_DENIED:
310 rejected(msg->rjcted_rply.rj_stat, error);
311 break;
312
313 default:
314 error->re_status = RPC_FAILED;
315 #ifdef __LP64__
316 error->re_lb.s1 = (int)(msg->rm_reply.rp_stat);
317 #else
318 error->re_lb.s1 = (long)(msg->rm_reply.rp_stat);
319 #endif
320 break;
321 }
322 switch (error->re_status) {
323
324 case RPC_VERSMISMATCH:
325 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
326 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
327 break;
328
329 case RPC_AUTHERROR:
330 error->re_why = msg->rjcted_rply.rj_why;
331 break;
332
333 case RPC_PROGVERSMISMATCH:
334 error->re_vers.low = msg->acpted_rply.ar_vers.low;
335 error->re_vers.high = msg->acpted_rply.ar_vers.high;
336 break;
337
338 default: break;
339 }
340 }