import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / rpc_prot.c
blob27488d7b09d2088fbff0186f0cec6994532696b4
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley
32 * 4.3 BSD under license from the Regents of the University of
33 * California.
36 #pragma ident "%Z%%M% %I% %E% SMI"
39 * This set of routines implements the rpc message definition,
40 * its serializer and some common rpc utility routines.
41 * The routines are meant for various implementations of rpc -
42 * they are NOT for the rpc client or rpc service implementations!
43 * Because authentication stuff is easy and is part of rpc, the opaque
44 * routines are also in this program.
47 #include "mt.h"
48 #include <sys/param.h>
49 #include <syslog.h>
50 #include <rpc/rpc.h>
51 #include <malloc.h>
53 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
55 struct opaque_auth _null_auth;
58 * XDR an opaque authentication struct
59 * (see auth.h)
61 bool_t
62 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
64 if (xdr_enum(xdrs, &(ap->oa_flavor)))
65 return (xdr_bytes(xdrs, &ap->oa_base,
66 &ap->oa_length, MAX_AUTH_BYTES));
67 return (FALSE);
71 * XDR a DES block
73 bool_t
74 xdr_des_block(XDR *xdrs, des_block *blkp)
76 return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof (des_block)));
79 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
82 * XDR the MSG_ACCEPTED part of a reply message union
84 bool_t
85 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
87 /* personalized union, rather than calling xdr_union */
88 if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
89 return (FALSE);
90 if (!xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
91 return (FALSE);
93 switch (ar->ar_stat) {
94 case SUCCESS:
95 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
96 case PROG_MISMATCH:
97 if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
98 return (FALSE);
99 return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
101 return (TRUE); /* TRUE => open ended set of problems */
105 * XDR the MSG_DENIED part of a reply message union
107 bool_t
108 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
110 /* personalized union, rather than calling xdr_union */
111 if (!xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
112 return (FALSE);
113 switch (rr->rj_stat) {
114 case RPC_MISMATCH:
115 if (!xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.low)))
116 return (FALSE);
117 return (xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.high)));
118 case AUTH_ERROR:
119 return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
121 return (FALSE);
125 * XDR a reply message
127 bool_t
128 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
130 struct xdr_discrim reply_dscrm[3];
131 rpc_inline_t *buf;
132 struct accepted_reply *ar;
133 struct opaque_auth *oa;
134 uint_t rndup;
136 if (xdrs->x_op == XDR_ENCODE &&
137 rmsg->rm_reply.rp_stat == MSG_ACCEPTED &&
138 rmsg->rm_direction == REPLY &&
139 (buf = XDR_INLINE(xdrs, 6 * BYTES_PER_XDR_UNIT + (rndup =
140 RNDUP(rmsg->rm_reply.rp_acpt.ar_verf.oa_length)))) != NULL) {
141 IXDR_PUT_INT32(buf, rmsg->rm_xid);
142 IXDR_PUT_ENUM(buf, rmsg->rm_direction);
143 IXDR_PUT_ENUM(buf, rmsg->rm_reply.rp_stat);
144 ar = &rmsg->rm_reply.rp_acpt;
145 oa = &ar->ar_verf;
146 IXDR_PUT_ENUM(buf, oa->oa_flavor);
147 IXDR_PUT_INT32(buf, oa->oa_length);
148 if (oa->oa_length) {
149 (void) memcpy(buf, oa->oa_base, oa->oa_length);
150 /* LINTED pointer alignment */
151 buf = (rpc_inline_t *)(((caddr_t)buf) + oa->oa_length);
153 if ((rndup = (rndup - oa->oa_length)) > 0) {
154 (void) memset((caddr_t)buf, 0, rndup);
155 /* LINTED pointer alignment */
156 buf = (rpc_inline_t *)(((caddr_t)buf) + rndup);
159 * stat and rest of reply, copied from xdr_accepted_reply
161 IXDR_PUT_ENUM(buf, ar->ar_stat);
162 switch (ar->ar_stat) {
163 case SUCCESS:
164 return ((*(ar->ar_results.proc))
165 (xdrs, ar->ar_results.where));
166 case PROG_MISMATCH:
167 if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
168 return (FALSE);
169 return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
171 return (TRUE);
173 if (xdrs->x_op == XDR_DECODE &&
174 (buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT)) != NULL) {
175 rmsg->rm_xid = IXDR_GET_INT32(buf);
176 rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
177 if (rmsg->rm_direction != REPLY)
178 return (FALSE);
179 rmsg->rm_reply.rp_stat = IXDR_GET_ENUM(buf, enum reply_stat);
180 if (rmsg->rm_reply.rp_stat != MSG_ACCEPTED) {
181 if (rmsg->rm_reply.rp_stat == MSG_DENIED)
182 return (xdr_rejected_reply(xdrs,
183 &rmsg->rm_reply.rp_rjct));
184 return (FALSE);
186 ar = &rmsg->rm_reply.rp_acpt;
187 oa = &ar->ar_verf;
188 buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
189 if (buf != NULL) {
190 oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
191 oa->oa_length = IXDR_GET_INT32(buf);
192 } else {
193 if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
194 xdr_u_int(xdrs, &oa->oa_length) == FALSE)
195 return (FALSE);
197 if (oa->oa_length) {
198 if (oa->oa_length > MAX_AUTH_BYTES)
199 return (FALSE);
200 if (oa->oa_base == NULL) {
201 oa->oa_base = malloc(oa->oa_length);
202 if (oa->oa_base == NULL) {
203 syslog(LOG_ERR,
204 "xdr_replymsg : "
205 "out of memory.");
206 rpc_callerr.re_status = RPC_SYSTEMERROR;
207 return (FALSE);
210 buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
211 if (buf == NULL) {
212 if (xdr_opaque(xdrs, oa->oa_base,
213 oa->oa_length) == FALSE)
214 return (FALSE);
215 } else {
216 (void) memcpy(oa->oa_base, buf, oa->oa_length);
220 * stat and rest of reply, copied from
221 * xdr_accepted_reply
223 if (!xdr_enum(xdrs, (enum_t *)&ar->ar_stat))
224 return (FALSE);
225 switch (ar->ar_stat) {
226 case SUCCESS:
227 return ((*(ar->ar_results.proc))
228 (xdrs, ar->ar_results.where));
229 case PROG_MISMATCH:
230 if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
231 return (FALSE);
232 return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
234 return (TRUE);
237 reply_dscrm[0].value = (int)MSG_ACCEPTED;
238 reply_dscrm[0].proc = (xdrproc_t)xdr_accepted_reply;
239 reply_dscrm[1].value = (int)MSG_DENIED;
240 reply_dscrm[1].proc = (xdrproc_t)xdr_rejected_reply;
241 reply_dscrm[2].value = __dontcare__;
242 reply_dscrm[2].proc = NULL_xdrproc_t;
243 if (xdr_u_int(xdrs, &(rmsg->rm_xid)) &&
244 xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
245 (rmsg->rm_direction == REPLY))
246 return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
247 (caddr_t)&(rmsg->rm_reply.ru),
248 reply_dscrm, NULL_xdrproc_t));
249 return (FALSE);
253 * Serializes the "static part" of a call message header.
254 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
255 * The rm_xid is not really static, but the user can easily munge on the fly.
257 bool_t
258 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
260 cmsg->rm_direction = CALL;
261 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
262 if (xdrs->x_op == XDR_ENCODE &&
263 xdr_u_int(xdrs, &(cmsg->rm_xid)) &&
264 xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
265 xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_rpcvers)) &&
266 xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_prog))) {
267 return (xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_vers)));
269 return (FALSE);
272 /* ************************** Client utility routine ************* */
274 static void
275 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
277 switch (acpt_stat) {
279 case PROG_UNAVAIL:
280 error->re_status = RPC_PROGUNAVAIL;
281 return;
283 case PROG_MISMATCH:
284 error->re_status = RPC_PROGVERSMISMATCH;
285 return;
287 case PROC_UNAVAIL:
288 error->re_status = RPC_PROCUNAVAIL;
289 return;
291 case GARBAGE_ARGS:
292 error->re_status = RPC_CANTDECODEARGS;
293 return;
295 case SYSTEM_ERR:
296 error->re_status = RPC_SYSTEMERROR;
297 return;
299 case SUCCESS:
300 error->re_status = RPC_SUCCESS;
301 return;
303 /* something's wrong, but we don't know what ... */
304 error->re_status = RPC_FAILED;
305 error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
306 error->re_lb.s2 = (int32_t)acpt_stat;
309 static void
310 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
312 switch (rjct_stat) {
313 case RPC_MISMATCH:
314 error->re_status = RPC_VERSMISMATCH;
315 return;
317 case AUTH_ERROR:
318 error->re_status = RPC_AUTHERROR;
319 return;
321 /* something's wrong, but we don't know what ... */
322 error->re_status = RPC_FAILED;
323 error->re_lb.s1 = (int32_t)MSG_DENIED;
324 error->re_lb.s2 = (int32_t)rjct_stat;
328 * given a reply message, fills in the error
330 void
331 __seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
333 /* optimized for normal, SUCCESSful case */
334 switch (msg->rm_reply.rp_stat) {
335 case MSG_ACCEPTED:
336 if (msg->acpted_rply.ar_stat == SUCCESS) {
337 error->re_status = RPC_SUCCESS;
338 return;
340 accepted(msg->acpted_rply.ar_stat, error);
341 break;
343 case MSG_DENIED:
344 rejected(msg->rjcted_rply.rj_stat, error);
345 break;
347 default:
348 error->re_status = RPC_FAILED;
349 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
350 break;
353 switch (error->re_status) {
354 case RPC_VERSMISMATCH:
355 error->re_vers.low = msg->rjcted_rply.rj_vers.low;
356 error->re_vers.high = msg->rjcted_rply.rj_vers.high;
357 break;
359 case RPC_AUTHERROR:
360 error->re_why = msg->rjcted_rply.rj_why;
361 break;
363 case RPC_PROGVERSMISMATCH:
364 error->re_vers.low = msg->acpted_rply.ar_vers.low;
365 error->re_vers.high = msg->acpted_rply.ar_vers.high;
366 break;