Path: blob/main/crypto/krb5/src/lib/rpc/rpc_prot.c
39536 views
/* @(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* rpc_prot.c39*40* This set of routines implements the rpc message definition,41* its serializer and some common rpc utility routines.42* The routines are meant for various implementations of rpc -43* they are NOT for the rpc client or rpc service implementations!44* Because authentication stuff is easy and is part of rpc, the opaque45* routines are also in this program.46*/4748#include <sys/param.h>4950#include <gssrpc/rpc.h>5152/* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */5354/*55* XDR an opaque authentication struct56* (see auth.h)57*/58bool_t59xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)60{6162if (xdr_enum(xdrs, &(ap->oa_flavor)))63return (xdr_bytes(xdrs, &ap->oa_base,64&ap->oa_length, MAX_AUTH_BYTES));65return (FALSE);66}6768/*69* XDR a DES block70*/71bool_t72xdr_des_block(XDR *xdrs, des_block *blkp)73{74return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block)));75}7677/* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */7879/*80* XDR the MSG_ACCEPTED part of a reply message union81*/82bool_t83xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)84{8586/* personalized union, rather than calling xdr_union */87if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))88return (FALSE);89if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))90return (FALSE);91switch (ar->ar_stat) {9293case SUCCESS:94return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));9596case PROG_MISMATCH:97if (! xdr_rpcvers(xdrs, &(ar->ar_vers.low)))98return (FALSE);99return (xdr_rpcvers(xdrs, &(ar->ar_vers.high)));100101case GARBAGE_ARGS:102case SYSTEM_ERR:103case PROC_UNAVAIL:104case PROG_UNAVAIL:105break;106}107return (TRUE); /* TRUE => open ended set of problems */108}109110/*111* XDR the MSG_DENIED part of a reply message union112*/113bool_t114xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)115{116117/* personalized union, rather than calling xdr_union */118if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))119return (FALSE);120switch (rr->rj_stat) {121122case RPC_MISMATCH:123if (! xdr_rpcvers(xdrs, &(rr->rj_vers.low)))124return (FALSE);125return (xdr_rpcvers(xdrs, &(rr->rj_vers.high)));126127case AUTH_ERROR:128return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));129}130return (FALSE);131}132133static struct xdr_discrim reply_dscrm[3] = {134{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },135{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },136{ __dontcare__, NULL_xdrproc_t } };137138/*139* XDR a reply message140*/141bool_t142xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)143{144if (145xdr_u_int32(xdrs, &(rmsg->rm_xid)) &&146xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&147(rmsg->rm_direction == REPLY) )148return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),149(caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t));150return (FALSE);151}152153154/*155* Serializes the "static part" of a call message header.156* The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.157* The rm_xid is not really static, but the user can easily munge on the fly.158*/159bool_t160xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)161{162163cmsg->rm_direction = CALL;164cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;165if (166(xdrs->x_op == XDR_ENCODE) &&167xdr_u_int32(xdrs, &(cmsg->rm_xid)) &&168xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&169xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&170xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) )171return (xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)));172return (FALSE);173}174175/* ************************** Client utility routine ************* */176177static void178accepted(enum accept_stat acpt_stat, struct rpc_err *error)179{180181switch (acpt_stat) {182183case PROG_UNAVAIL:184error->re_status = RPC_PROGUNAVAIL;185return;186187case PROG_MISMATCH:188error->re_status = RPC_PROGVERSMISMATCH;189return;190191case PROC_UNAVAIL:192error->re_status = RPC_PROCUNAVAIL;193return;194195case GARBAGE_ARGS:196error->re_status = RPC_CANTDECODEARGS;197return;198199case SYSTEM_ERR:200error->re_status = RPC_SYSTEMERROR;201return;202203case SUCCESS:204error->re_status = RPC_SUCCESS;205return;206}207/* something's wrong, but we don't know what ... */208error->re_status = RPC_FAILED;209error->re_lb.s1 = (int32_t)MSG_ACCEPTED;210error->re_lb.s2 = (int32_t)acpt_stat;211}212213static void214rejected(enum reject_stat rjct_stat, struct rpc_err *error)215{216217switch (rjct_stat) {218219case RPC_MISMATCH:220error->re_status = RPC_VERSMISMATCH;221return;222223case AUTH_ERROR:224error->re_status = RPC_AUTHERROR;225return;226}227/* something's wrong, but we don't know what ... */228error->re_status = RPC_FAILED;229error->re_lb.s1 = (int32_t)MSG_DENIED;230error->re_lb.s2 = (int32_t)rjct_stat;231}232233/*234* given a reply message, fills in the error235*/236void237gssrpc__seterr_reply(struct rpc_msg *msg, struct rpc_err *error)238{239240/* optimized for normal, SUCCESSful case */241switch (msg->rm_reply.rp_stat) {242243case MSG_ACCEPTED:244if (msg->acpted_rply.ar_stat == SUCCESS) {245error->re_status = RPC_SUCCESS;246return;247};248accepted(msg->acpted_rply.ar_stat, error);249break;250251case MSG_DENIED:252rejected(msg->rjcted_rply.rj_stat, error);253break;254255default:256error->re_status = RPC_FAILED;257error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);258break;259}260switch (error->re_status) {261262case RPC_VERSMISMATCH:263error->re_vers.low = msg->rjcted_rply.rj_vers.low;264error->re_vers.high = msg->rjcted_rply.rj_vers.high;265break;266267case RPC_AUTHERROR:268error->re_why = msg->rjcted_rply.rj_why;269break;270271case RPC_PROGVERSMISMATCH:272error->re_vers.low = msg->acpted_rply.ar_vers.low;273error->re_vers.high = msg->acpted_rply.ar_vers.high;274break;275276case RPC_FAILED:277case RPC_SUCCESS:278case RPC_PROGNOTREGISTERED:279case RPC_PMAPFAILURE:280case RPC_UNKNOWNPROTO:281case RPC_UNKNOWNHOST:282case RPC_SYSTEMERROR:283case RPC_CANTDECODEARGS:284case RPC_PROCUNAVAIL:285case RPC_PROGUNAVAIL:286case RPC_TIMEDOUT:287case RPC_CANTRECV:288case RPC_CANTSEND:289case RPC_CANTDECODERES:290case RPC_CANTENCODEARGS:291default:292break;293}294}295296297