Home | History | Annotate | Line # | Download | only in rpc
rpc_prot.c revision 1.13
      1 /*	$NetBSD: rpc_prot.c,v 1.13 1999/09/16 11:45:24 lukem 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.13 1999/09/16 11:45:24 lukem 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 struct opaque_auth _null_auth;
     78 
     79 /*
     80  * XDR an opaque authentication struct
     81  * (see auth.h)
     82  */
     83 bool_t
     84 xdr_opaque_auth(xdrs, ap)
     85 	XDR *xdrs;
     86 	struct opaque_auth *ap;
     87 {
     88 
     89 	_DIAGASSERT(xdrs != NULL);
     90 	_DIAGASSERT(ap != NULL);
     91 #ifdef _DIAGNOSTIC
     92 	if (xdrs == NULL || ap == NULL)
     93 		return (FALSE);
     94 #endif
     95 
     96 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
     97 		return (xdr_bytes(xdrs, &ap->oa_base,
     98 			&ap->oa_length, MAX_AUTH_BYTES));
     99 	return (FALSE);
    100 }
    101 
    102 /*
    103  * XDR a DES block
    104  */
    105 bool_t
    106 xdr_des_block(xdrs, blkp)
    107 	XDR *xdrs;
    108 	des_block *blkp;
    109 {
    110 
    111 	_DIAGASSERT(xdrs != NULL);
    112 	_DIAGASSERT(blkp != NULL);
    113 #ifdef _DIAGNOSTIC
    114 	if (xdrs == NULL || blkp == NULL)
    115 		return (FALSE);
    116 #endif
    117 
    118 	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block)));
    119 }
    120 
    121 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
    122 
    123 /*
    124  * XDR the MSG_ACCEPTED part of a reply message union
    125  */
    126 bool_t
    127 xdr_accepted_reply(xdrs, ar)
    128 	XDR *xdrs;
    129 	struct accepted_reply *ar;
    130 {
    131 
    132 	_DIAGASSERT(xdrs != NULL);
    133 	_DIAGASSERT(ar != NULL);
    134 #ifdef _DIAGNOSTIC
    135 	if (xdrs == NULL || ar == NULL)
    136 		return (FALSE);
    137 #endif
    138 
    139 	/* personalized union, rather than calling xdr_union */
    140 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
    141 		return (FALSE);
    142 	if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
    143 		return (FALSE);
    144 	switch (ar->ar_stat) {
    145 
    146 	case SUCCESS:
    147 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
    148 
    149 	case PROG_MISMATCH:
    150 		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
    151 			return (FALSE);
    152 		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
    153 
    154 	case GARBAGE_ARGS:
    155 	case SYSTEM_ERR:
    156 	case PROC_UNAVAIL:
    157 	case PROG_UNAVAIL:
    158 		break;
    159 	}
    160 	return (TRUE);  /* TRUE => open ended set of problems */
    161 }
    162 
    163 /*
    164  * XDR the MSG_DENIED part of a reply message union
    165  */
    166 bool_t
    167 xdr_rejected_reply(xdrs, rr)
    168 	XDR *xdrs;
    169 	struct rejected_reply *rr;
    170 {
    171 
    172 	_DIAGASSERT(xdrs != NULL);
    173 	_DIAGASSERT(rr != NULL);
    174 #ifdef _DIAGNOSTIC
    175 	if (xdrs == NULL || rr == NULL)
    176 		return (FALSE);
    177 #endif
    178 
    179 	/* personalized union, rather than calling xdr_union */
    180 	if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
    181 		return (FALSE);
    182 	switch (rr->rj_stat) {
    183 
    184 	case RPC_MISMATCH:
    185 		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
    186 			return (FALSE);
    187 		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
    188 
    189 	case AUTH_ERROR:
    190 		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
    191 	}
    192 	/* NOTREACHED */
    193 	return (FALSE);
    194 }
    195 
    196 static const struct xdr_discrim reply_dscrm[3] = {
    197 	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
    198 	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
    199 	{ __dontcare__, NULL_xdrproc_t } };
    200 
    201 /*
    202  * XDR a reply message
    203  */
    204 bool_t
    205 xdr_replymsg(xdrs, rmsg)
    206 	XDR *xdrs;
    207 	struct rpc_msg *rmsg;
    208 {
    209 	_DIAGASSERT(xdrs != NULL);
    210 	_DIAGASSERT(rmsg != NULL);
    211 #ifdef _DIAGNOSTIC
    212 	if (xdrs == NULL || rmsg == NULL)
    213 		return (FALSE);
    214 #endif
    215 
    216 	if (
    217 	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
    218 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
    219 	    (rmsg->rm_direction == REPLY) )
    220 		return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
    221 		   (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
    222 		   NULL_xdrproc_t));
    223 	return (FALSE);
    224 }
    225 
    226 
    227 /*
    228  * Serializes the "static part" of a call message header.
    229  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
    230  * The rm_xid is not really static, but the user can easily munge on the fly.
    231  */
    232 bool_t
    233 xdr_callhdr(xdrs, cmsg)
    234 	XDR *xdrs;
    235 	struct rpc_msg *cmsg;
    236 {
    237 
    238 	_DIAGASSERT(xdrs != NULL);
    239 	_DIAGASSERT(cmsg != NULL);
    240 #ifdef _DIAGNOSTIC
    241 	if (xdrs == NULL || cmsg == NULL)
    242 		return (FALSE);
    243 #endif
    244 
    245 	cmsg->rm_direction = CALL;
    246 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
    247 	if (
    248 	    (xdrs->x_op == XDR_ENCODE) &&
    249 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
    250 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
    251 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
    252 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
    253 		return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
    254 	return (FALSE);
    255 }
    256 
    257 /* ************************** Client utility routine ************* */
    258 
    259 static void
    260 accepted(acpt_stat, error)
    261 	enum accept_stat acpt_stat;
    262 	struct rpc_err *error;
    263 {
    264 
    265 	_DIAGASSERT(error != NULL);
    266 
    267 	switch (acpt_stat) {
    268 
    269 	case PROG_UNAVAIL:
    270 		error->re_status = RPC_PROGUNAVAIL;
    271 		return;
    272 
    273 	case PROG_MISMATCH:
    274 		error->re_status = RPC_PROGVERSMISMATCH;
    275 		return;
    276 
    277 	case PROC_UNAVAIL:
    278 		error->re_status = RPC_PROCUNAVAIL;
    279 		return;
    280 
    281 	case GARBAGE_ARGS:
    282 		error->re_status = RPC_CANTDECODEARGS;
    283 		return;
    284 
    285 	case SYSTEM_ERR:
    286 		error->re_status = RPC_SYSTEMERROR;
    287 		return;
    288 
    289 	case SUCCESS:
    290 		error->re_status = RPC_SUCCESS;
    291 		return;
    292 	}
    293 	/* NOTREACHED */
    294 	/* something's wrong, but we don't know what ... */
    295 	error->re_status = RPC_FAILED;
    296 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
    297 	error->re_lb.s2 = (int32_t)acpt_stat;
    298 }
    299 
    300 static void
    301 rejected(rjct_stat, error)
    302 	enum reject_stat rjct_stat;
    303 	struct rpc_err *error;
    304 {
    305 
    306 	_DIAGASSERT(error != NULL);
    307 
    308 	switch (rjct_stat) {
    309 	case RPC_MISMATCH:
    310 		error->re_status = RPC_VERSMISMATCH;
    311 		return;
    312 
    313 	case AUTH_ERROR:
    314 		error->re_status = RPC_AUTHERROR;
    315 		return;
    316 	}
    317 	/* something's wrong, but we don't know what ... */
    318 	/* NOTREACHED */
    319 	error->re_status = RPC_FAILED;
    320 	error->re_lb.s1 = (int32_t)MSG_DENIED;
    321 	error->re_lb.s2 = (int32_t)rjct_stat;
    322 }
    323 
    324 /*
    325  * given a reply message, fills in the error
    326  */
    327 void
    328 _seterr_reply(msg, error)
    329 	struct rpc_msg *msg;
    330 	struct rpc_err *error;
    331 {
    332 
    333 	_DIAGASSERT(msg != NULL);
    334 	_DIAGASSERT(error != NULL);
    335 #ifdef _DIAGNOSTIC
    336 	if (msg == NULL || error == NULL)
    337 		return;
    338 #endif
    339 
    340 	/* optimized for normal, SUCCESSful case */
    341 	switch (msg->rm_reply.rp_stat) {
    342 
    343 	case MSG_ACCEPTED:
    344 		if (msg->acpted_rply.ar_stat == SUCCESS) {
    345 			error->re_status = RPC_SUCCESS;
    346 			return;
    347 		}
    348 		accepted(msg->acpted_rply.ar_stat, error);
    349 		break;
    350 
    351 	case MSG_DENIED:
    352 		rejected(msg->rjcted_rply.rj_stat, error);
    353 		break;
    354 
    355 	default:
    356 		error->re_status = RPC_FAILED;
    357 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
    358 		break;
    359 	}
    360 	switch (error->re_status) {
    361 
    362 	case RPC_VERSMISMATCH:
    363 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
    364 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
    365 		break;
    366 
    367 	case RPC_AUTHERROR:
    368 		error->re_why = msg->rjcted_rply.rj_why;
    369 		break;
    370 
    371 	case RPC_PROGVERSMISMATCH:
    372 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
    373 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
    374 		break;
    375 
    376 	case RPC_FAILED:
    377 	case RPC_SUCCESS:
    378 	case RPC_PROGNOTREGISTERED:
    379 	case RPC_PMAPFAILURE:
    380 	case RPC_UNKNOWNPROTO:
    381 	case RPC_UNKNOWNHOST:
    382 	case RPC_SYSTEMERROR:
    383 	case RPC_CANTDECODEARGS:
    384 	case RPC_PROCUNAVAIL:
    385 	case RPC_PROGUNAVAIL:
    386 	case RPC_TIMEDOUT:
    387 	case RPC_CANTRECV:
    388 	case RPC_CANTSEND:
    389 	case RPC_CANTDECODERES:
    390 	case RPC_CANTENCODEARGS:
    391 		break;
    392 	}
    393 }
    394