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