2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * This set of routines implements the rpc message definition,
2N/A * its serializer and some common rpc utility routines.
2N/A * The routines are meant for various implementations of rpc -
2N/A * they are NOT for the rpc client or rpc service implementations!
2N/A * Because authentication stuff is easy and is part of rpc, the opaque
2N/A * routines are also in this program.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include <sys/param.h>
2N/A#include <syslog.h>
2N/A#include <rpc/rpc.h>
2N/A#include <malloc.h>
2N/A
2N/A/* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
2N/A
2N/Astruct opaque_auth _null_auth;
2N/A
2N/A/*
2N/A * XDR an opaque authentication struct
2N/A * (see auth.h)
2N/A */
2N/Abool_t
2N/Axdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
2N/A{
2N/A if (xdr_enum(xdrs, &(ap->oa_flavor)))
2N/A return (xdr_bytes(xdrs, &ap->oa_base,
2N/A &ap->oa_length, MAX_AUTH_BYTES));
2N/A return (FALSE);
2N/A}
2N/A
2N/A/*
2N/A * XDR a DES block
2N/A */
2N/Abool_t
2N/Axdr_des_block(XDR *xdrs, des_block *blkp)
2N/A{
2N/A return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof (des_block)));
2N/A}
2N/A
2N/A/* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
2N/A
2N/A/*
2N/A * XDR the MSG_ACCEPTED part of a reply message union
2N/A */
2N/Abool_t
2N/Axdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
2N/A{
2N/A /* personalized union, rather than calling xdr_union */
2N/A if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
2N/A return (FALSE);
2N/A if (!xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
2N/A return (FALSE);
2N/A
2N/A switch (ar->ar_stat) {
2N/A case SUCCESS:
2N/A return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
2N/A case PROG_MISMATCH:
2N/A if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
2N/A return (FALSE);
2N/A return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
2N/A }
2N/A return (TRUE); /* TRUE => open ended set of problems */
2N/A}
2N/A
2N/A/*
2N/A * XDR the MSG_DENIED part of a reply message union
2N/A */
2N/Abool_t
2N/Axdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
2N/A{
2N/A /* personalized union, rather than calling xdr_union */
2N/A if (!xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
2N/A return (FALSE);
2N/A switch (rr->rj_stat) {
2N/A case RPC_MISMATCH:
2N/A if (!xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.low)))
2N/A return (FALSE);
2N/A return (xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.high)));
2N/A case AUTH_ERROR:
2N/A return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
2N/A }
2N/A return (FALSE);
2N/A}
2N/A
2N/A/*
2N/A * XDR a reply message
2N/A */
2N/Abool_t
2N/Axdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
2N/A{
2N/A struct xdr_discrim reply_dscrm[3];
2N/A rpc_inline_t *buf;
2N/A struct accepted_reply *ar;
2N/A struct opaque_auth *oa;
2N/A uint_t rndup;
2N/A
2N/A if (xdrs->x_op == XDR_ENCODE &&
2N/A rmsg->rm_reply.rp_stat == MSG_ACCEPTED &&
2N/A rmsg->rm_direction == REPLY &&
2N/A (buf = XDR_INLINE(xdrs, 6 * BYTES_PER_XDR_UNIT + (rndup =
2N/A RNDUP(rmsg->rm_reply.rp_acpt.ar_verf.oa_length)))) != NULL) {
2N/A IXDR_PUT_INT32(buf, rmsg->rm_xid);
2N/A IXDR_PUT_ENUM(buf, rmsg->rm_direction);
2N/A IXDR_PUT_ENUM(buf, rmsg->rm_reply.rp_stat);
2N/A ar = &rmsg->rm_reply.rp_acpt;
2N/A oa = &ar->ar_verf;
2N/A IXDR_PUT_ENUM(buf, oa->oa_flavor);
2N/A IXDR_PUT_INT32(buf, oa->oa_length);
2N/A if (oa->oa_length) {
2N/A (void) memcpy(buf, oa->oa_base, oa->oa_length);
2N/A/* LINTED pointer alignment */
2N/A buf = (rpc_inline_t *)(((caddr_t)buf) + oa->oa_length);
2N/A }
2N/A if ((rndup = (rndup - oa->oa_length)) > 0) {
2N/A (void) memset((caddr_t)buf, 0, rndup);
2N/A/* LINTED pointer alignment */
2N/A buf = (rpc_inline_t *)(((caddr_t)buf) + rndup);
2N/A }
2N/A /*
2N/A * stat and rest of reply, copied from xdr_accepted_reply
2N/A */
2N/A IXDR_PUT_ENUM(buf, ar->ar_stat);
2N/A switch (ar->ar_stat) {
2N/A case SUCCESS:
2N/A return ((*(ar->ar_results.proc))
2N/A (xdrs, ar->ar_results.where));
2N/A case PROG_MISMATCH:
2N/A if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
2N/A return (FALSE);
2N/A return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
2N/A }
2N/A return (TRUE);
2N/A }
2N/A if (xdrs->x_op == XDR_DECODE &&
2N/A (buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT)) != NULL) {
2N/A rmsg->rm_xid = IXDR_GET_INT32(buf);
2N/A rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
2N/A if (rmsg->rm_direction != REPLY)
2N/A return (FALSE);
2N/A rmsg->rm_reply.rp_stat = IXDR_GET_ENUM(buf, enum reply_stat);
2N/A if (rmsg->rm_reply.rp_stat != MSG_ACCEPTED) {
2N/A if (rmsg->rm_reply.rp_stat == MSG_DENIED)
2N/A return (xdr_rejected_reply(xdrs,
2N/A &rmsg->rm_reply.rp_rjct));
2N/A return (FALSE);
2N/A }
2N/A ar = &rmsg->rm_reply.rp_acpt;
2N/A oa = &ar->ar_verf;
2N/A buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
2N/A if (buf != NULL) {
2N/A oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
2N/A oa->oa_length = IXDR_GET_INT32(buf);
2N/A } else {
2N/A if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
2N/A xdr_u_int(xdrs, &oa->oa_length) == FALSE)
2N/A return (FALSE);
2N/A }
2N/A if (oa->oa_length) {
2N/A if (oa->oa_length > MAX_AUTH_BYTES)
2N/A return (FALSE);
2N/A if (oa->oa_base == NULL) {
2N/A oa->oa_base = malloc(oa->oa_length);
2N/A if (oa->oa_base == NULL) {
2N/A syslog(LOG_ERR,
2N/A "xdr_replymsg : "
2N/A "out of memory.");
2N/A rpc_callerr.re_status = RPC_SYSTEMERROR;
2N/A return (FALSE);
2N/A }
2N/A }
2N/A buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
2N/A if (buf == NULL) {
2N/A if (xdr_opaque(xdrs, oa->oa_base,
2N/A oa->oa_length) == FALSE)
2N/A return (FALSE);
2N/A } else {
2N/A (void) memcpy(oa->oa_base, buf, oa->oa_length);
2N/A }
2N/A }
2N/A /*
2N/A * stat and rest of reply, copied from
2N/A * xdr_accepted_reply
2N/A */
2N/A if (!xdr_enum(xdrs, (enum_t *)&ar->ar_stat))
2N/A return (FALSE);
2N/A switch (ar->ar_stat) {
2N/A case SUCCESS:
2N/A return ((*(ar->ar_results.proc))
2N/A (xdrs, ar->ar_results.where));
2N/A case PROG_MISMATCH:
2N/A if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
2N/A return (FALSE);
2N/A return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
2N/A }
2N/A return (TRUE);
2N/A }
2N/A
2N/A reply_dscrm[0].value = (int)MSG_ACCEPTED;
2N/A reply_dscrm[0].proc = (xdrproc_t)xdr_accepted_reply;
2N/A reply_dscrm[1].value = (int)MSG_DENIED;
2N/A reply_dscrm[1].proc = (xdrproc_t)xdr_rejected_reply;
2N/A reply_dscrm[2].value = __dontcare__;
2N/A reply_dscrm[2].proc = NULL_xdrproc_t;
2N/A if (xdr_u_int(xdrs, &(rmsg->rm_xid)) &&
2N/A xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
2N/A (rmsg->rm_direction == REPLY))
2N/A return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
2N/A (caddr_t)&(rmsg->rm_reply.ru),
2N/A reply_dscrm, NULL_xdrproc_t));
2N/A return (FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Serializes the "static part" of a call message header.
2N/A * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
2N/A * The rm_xid is not really static, but the user can easily munge on the fly.
2N/A */
2N/Abool_t
2N/Axdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
2N/A{
2N/A cmsg->rm_direction = CALL;
2N/A cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
2N/A if (xdrs->x_op == XDR_ENCODE &&
2N/A xdr_u_int(xdrs, &(cmsg->rm_xid)) &&
2N/A xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
2N/A xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_rpcvers)) &&
2N/A xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_prog))) {
2N/A return (xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_vers)));
2N/A }
2N/A return (FALSE);
2N/A}
2N/A
2N/A/* ************************** Client utility routine ************* */
2N/A
2N/Astatic void
2N/Aaccepted(enum accept_stat acpt_stat, struct rpc_err *error)
2N/A{
2N/A switch (acpt_stat) {
2N/A
2N/A case PROG_UNAVAIL:
2N/A error->re_status = RPC_PROGUNAVAIL;
2N/A return;
2N/A
2N/A case PROG_MISMATCH:
2N/A error->re_status = RPC_PROGVERSMISMATCH;
2N/A return;
2N/A
2N/A case PROC_UNAVAIL:
2N/A error->re_status = RPC_PROCUNAVAIL;
2N/A return;
2N/A
2N/A case GARBAGE_ARGS:
2N/A error->re_status = RPC_CANTDECODEARGS;
2N/A return;
2N/A
2N/A case SYSTEM_ERR:
2N/A error->re_status = RPC_SYSTEMERROR;
2N/A return;
2N/A
2N/A case SUCCESS:
2N/A error->re_status = RPC_SUCCESS;
2N/A return;
2N/A }
2N/A /* something's wrong, but we don't know what ... */
2N/A error->re_status = RPC_FAILED;
2N/A error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
2N/A error->re_lb.s2 = (int32_t)acpt_stat;
2N/A}
2N/A
2N/Astatic void
2N/Arejected(enum reject_stat rjct_stat, struct rpc_err *error)
2N/A{
2N/A switch (rjct_stat) {
2N/A case RPC_MISMATCH:
2N/A error->re_status = RPC_VERSMISMATCH;
2N/A return;
2N/A
2N/A case AUTH_ERROR:
2N/A error->re_status = RPC_AUTHERROR;
2N/A return;
2N/A }
2N/A /* something's wrong, but we don't know what ... */
2N/A error->re_status = RPC_FAILED;
2N/A error->re_lb.s1 = (int32_t)MSG_DENIED;
2N/A error->re_lb.s2 = (int32_t)rjct_stat;
2N/A}
2N/A
2N/A/*
2N/A * given a reply message, fills in the error
2N/A */
2N/Avoid
2N/A__seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
2N/A{
2N/A /* optimized for normal, SUCCESSful case */
2N/A switch (msg->rm_reply.rp_stat) {
2N/A case MSG_ACCEPTED:
2N/A if (msg->acpted_rply.ar_stat == SUCCESS) {
2N/A error->re_status = RPC_SUCCESS;
2N/A return;
2N/A };
2N/A accepted(msg->acpted_rply.ar_stat, error);
2N/A break;
2N/A
2N/A case MSG_DENIED:
2N/A rejected(msg->rjcted_rply.rj_stat, error);
2N/A break;
2N/A
2N/A default:
2N/A error->re_status = RPC_FAILED;
2N/A error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
2N/A break;
2N/A }
2N/A
2N/A switch (error->re_status) {
2N/A case RPC_VERSMISMATCH:
2N/A error->re_vers.low = msg->rjcted_rply.rj_vers.low;
2N/A error->re_vers.high = msg->rjcted_rply.rj_vers.high;
2N/A break;
2N/A
2N/A case RPC_AUTHERROR:
2N/A error->re_why = msg->rjcted_rply.rj_why;
2N/A break;
2N/A
2N/A case RPC_PROGVERSMISMATCH:
2N/A error->re_vers.low = msg->acpted_rply.ar_vers.low;
2N/A error->re_vers.high = msg->acpted_rply.ar_vers.high;
2N/A break;
2N/A }
2N/A}