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