xref: /netbsd-src/lib/libc/rpc/rpc_prot.c (revision 47c0e0c312cd964546204bd0febab9a56fa11df9)
1*47c0e0c3Stron /*	$NetBSD: rpc_prot.c,v 1.21 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*47c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*47c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron  * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron  * met:
963d7b677Scgd  *
10*47c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron  *       copyright notice, this list of conditions and the following
14*47c0e0c3Stron  *       disclaimer in the documentation and/or other materials
15*47c0e0c3Stron  *       provided with the distribution.
16*47c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron  *       contributors may be used to endorse or promote products derived
18*47c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*47c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34ee71b4ceSchristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36ee71b4ceSchristos #if 0
37ee71b4ceSchristos static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro";
38ee71b4ceSchristos static char *sccsid = "@(#)rpc_prot.c	2.3 88/08/07 4.0 RPCSRC";
39ee71b4ceSchristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: rpc_prot.c,v 1.21 2013/03/11 20:19:29 tron Exp $");
41ee71b4ceSchristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * rpc_prot.c
4663d7b677Scgd  *
4763d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
4863d7b677Scgd  *
4963d7b677Scgd  * This set of routines implements the rpc message definition,
5063d7b677Scgd  * its serializer and some common rpc utility routines.
5163d7b677Scgd  * The routines are meant for various implementations of rpc -
5263d7b677Scgd  * they are NOT for the rpc client or rpc service implementations!
5363d7b677Scgd  * Because authentication stuff is easy and is part of rpc, the opaque
5463d7b677Scgd  * routines are also in this program.
5563d7b677Scgd  */
5663d7b677Scgd 
5743fa6fe3Sjtc #include "namespace.h"
5846e6c5e8Slukem 
5963d7b677Scgd #include <sys/param.h>
6046e6c5e8Slukem 
61b48252f3Slukem #include <assert.h>
62b48252f3Slukem 
6363d7b677Scgd #include <rpc/rpc.h>
6463d7b677Scgd 
6543fa6fe3Sjtc #ifdef __weak_alias
6660549036Smycroft __weak_alias(xdr_accepted_reply,_xdr_accepted_reply)
6760549036Smycroft __weak_alias(xdr_callhdr,_xdr_callhdr)
6860549036Smycroft __weak_alias(xdr_des_block,_xdr_des_block)
6960549036Smycroft __weak_alias(xdr_opaque_auth,_xdr_opaque_auth)
7060549036Smycroft __weak_alias(xdr_rejected_reply,_xdr_rejected_reply)
7160549036Smycroft __weak_alias(xdr_replymsg,_xdr_replymsg)
7243fa6fe3Sjtc #endif
7343fa6fe3Sjtc 
74adb74221Smatt static void accepted(enum accept_stat, struct rpc_err *);
75adb74221Smatt static void rejected(enum reject_stat, struct rpc_err *);
76ee71b4ceSchristos 
7763d7b677Scgd /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
7863d7b677Scgd 
7963d7b677Scgd /*
8063d7b677Scgd  * XDR an opaque authentication struct
8163d7b677Scgd  * (see auth.h)
8263d7b677Scgd  */
8363d7b677Scgd bool_t
xdr_opaque_auth(XDR * xdrs,struct opaque_auth * ap)84adb74221Smatt xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
8563d7b677Scgd {
8663d7b677Scgd 
87b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
88b48252f3Slukem 	_DIAGASSERT(ap != NULL);
89b48252f3Slukem 
9063d7b677Scgd 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
9163d7b677Scgd 		return (xdr_bytes(xdrs, &ap->oa_base,
9263d7b677Scgd 			&ap->oa_length, MAX_AUTH_BYTES));
9363d7b677Scgd 	return (FALSE);
9463d7b677Scgd }
9563d7b677Scgd 
9663d7b677Scgd /*
9763d7b677Scgd  * XDR a DES block
9863d7b677Scgd  */
9963d7b677Scgd bool_t
xdr_des_block(XDR * xdrs,des_block * blkp)100adb74221Smatt xdr_des_block(XDR *xdrs, des_block *blkp)
10163d7b677Scgd {
102b48252f3Slukem 
103b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
104b48252f3Slukem 	_DIAGASSERT(blkp != NULL);
105b48252f3Slukem 
106c5e820caSchristos 	return (xdr_opaque(xdrs, (caddr_t)(void *)blkp,
107c5e820caSchristos 	    (u_int)sizeof(des_block)));
10863d7b677Scgd }
10963d7b677Scgd 
11063d7b677Scgd /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
11163d7b677Scgd 
11263d7b677Scgd /*
11363d7b677Scgd  * XDR the MSG_ACCEPTED part of a reply message union
11463d7b677Scgd  */
11563d7b677Scgd bool_t
xdr_accepted_reply(XDR * xdrs,struct accepted_reply * ar)116adb74221Smatt xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
11763d7b677Scgd {
11863d7b677Scgd 
119b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
120b48252f3Slukem 	_DIAGASSERT(ar != NULL);
121b48252f3Slukem 
12263d7b677Scgd 	/* personalized union, rather than calling xdr_union */
12363d7b677Scgd 	if (! xdr_opaque_auth(xdrs, &(ar->ar_verf)))
12463d7b677Scgd 		return (FALSE);
12525cb0b1eSchristos 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(ar->ar_stat)))
12663d7b677Scgd 		return (FALSE);
12763d7b677Scgd 	switch (ar->ar_stat) {
12863d7b677Scgd 
12963d7b677Scgd 	case SUCCESS:
13063d7b677Scgd 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
13163d7b677Scgd 
13263d7b677Scgd 	case PROG_MISMATCH:
1332583d406Scgd 		if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low)))
13463d7b677Scgd 			return (FALSE);
1352583d406Scgd 		return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high)));
136ee71b4ceSchristos 
137ee71b4ceSchristos 	case GARBAGE_ARGS:
138ee71b4ceSchristos 	case SYSTEM_ERR:
139ee71b4ceSchristos 	case PROC_UNAVAIL:
140ee71b4ceSchristos 	case PROG_UNAVAIL:
141ee71b4ceSchristos 		break;
14263d7b677Scgd 	}
14363d7b677Scgd 	return (TRUE);  /* TRUE => open ended set of problems */
14463d7b677Scgd }
14563d7b677Scgd 
14663d7b677Scgd /*
14763d7b677Scgd  * XDR the MSG_DENIED part of a reply message union
14863d7b677Scgd  */
14963d7b677Scgd bool_t
xdr_rejected_reply(XDR * xdrs,struct rejected_reply * rr)150adb74221Smatt xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
15163d7b677Scgd {
15263d7b677Scgd 
153b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
154b48252f3Slukem 	_DIAGASSERT(rr != NULL);
155b48252f3Slukem 
15663d7b677Scgd 	/* personalized union, rather than calling xdr_union */
15725cb0b1eSchristos 	if (! xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_stat)))
15863d7b677Scgd 		return (FALSE);
15963d7b677Scgd 	switch (rr->rj_stat) {
16063d7b677Scgd 
16163d7b677Scgd 	case RPC_MISMATCH:
1622583d406Scgd 		if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low)))
16363d7b677Scgd 			return (FALSE);
1642583d406Scgd 		return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high)));
16563d7b677Scgd 
16663d7b677Scgd 	case AUTH_ERROR:
16725cb0b1eSchristos 		return (xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_why)));
16863d7b677Scgd 	}
1691325a26dSchristos 	/* NOTREACHED */
17063d7b677Scgd 	return (FALSE);
17163d7b677Scgd }
17263d7b677Scgd 
173dae9fd96Smycroft static const struct xdr_discrim reply_dscrm[3] = {
174caaf1528Schristos 	{ (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply },
175caaf1528Schristos 	{ (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply },
17663d7b677Scgd 	{ __dontcare__, NULL_xdrproc_t } };
17763d7b677Scgd 
17863d7b677Scgd /*
17963d7b677Scgd  * XDR a reply message
18063d7b677Scgd  */
18163d7b677Scgd bool_t
xdr_replymsg(XDR * xdrs,struct rpc_msg * rmsg)182adb74221Smatt xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
18363d7b677Scgd {
184b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
185b48252f3Slukem 	_DIAGASSERT(rmsg != NULL);
186b48252f3Slukem 
18763d7b677Scgd 	if (
1882583d406Scgd 	    xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) &&
18925cb0b1eSchristos 	    xdr_enum(xdrs, (enum_t *)(void *)&(rmsg->rm_direction)) &&
19063d7b677Scgd 	    (rmsg->rm_direction == REPLY) )
19125cb0b1eSchristos 		return (xdr_union(xdrs, (enum_t *)(void *)&(rmsg->rm_reply.rp_stat),
1921325a26dSchristos 		   (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm,
1931325a26dSchristos 		   NULL_xdrproc_t));
19463d7b677Scgd 	return (FALSE);
19563d7b677Scgd }
19663d7b677Scgd 
19763d7b677Scgd 
19863d7b677Scgd /*
19963d7b677Scgd  * Serializes the "static part" of a call message header.
20063d7b677Scgd  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
20163d7b677Scgd  * The rm_xid is not really static, but the user can easily munge on the fly.
20263d7b677Scgd  */
20363d7b677Scgd bool_t
xdr_callhdr(XDR * xdrs,struct rpc_msg * cmsg)204adb74221Smatt xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
20563d7b677Scgd {
20663d7b677Scgd 
207b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
208b48252f3Slukem 	_DIAGASSERT(cmsg != NULL);
209b48252f3Slukem 
21063d7b677Scgd 	cmsg->rm_direction = CALL;
21163d7b677Scgd 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
21263d7b677Scgd 	if (
21363d7b677Scgd 	    (xdrs->x_op == XDR_ENCODE) &&
2142583d406Scgd 	    xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
21525cb0b1eSchristos 	    xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) &&
2162583d406Scgd 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
2172583d406Scgd 	    xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) )
2182583d406Scgd 		return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers)));
21963d7b677Scgd 	return (FALSE);
22063d7b677Scgd }
22163d7b677Scgd 
22263d7b677Scgd /* ************************** Client utility routine ************* */
22363d7b677Scgd 
22463d7b677Scgd static void
accepted(enum accept_stat acpt_stat,struct rpc_err * error)225adb74221Smatt accepted(enum accept_stat acpt_stat, struct rpc_err *error)
22663d7b677Scgd {
22763d7b677Scgd 
228b48252f3Slukem 	_DIAGASSERT(error != NULL);
229b48252f3Slukem 
23063d7b677Scgd 	switch (acpt_stat) {
23163d7b677Scgd 
23263d7b677Scgd 	case PROG_UNAVAIL:
23363d7b677Scgd 		error->re_status = RPC_PROGUNAVAIL;
23463d7b677Scgd 		return;
23563d7b677Scgd 
23663d7b677Scgd 	case PROG_MISMATCH:
23763d7b677Scgd 		error->re_status = RPC_PROGVERSMISMATCH;
23863d7b677Scgd 		return;
23963d7b677Scgd 
24063d7b677Scgd 	case PROC_UNAVAIL:
24163d7b677Scgd 		error->re_status = RPC_PROCUNAVAIL;
24263d7b677Scgd 		return;
24363d7b677Scgd 
24463d7b677Scgd 	case GARBAGE_ARGS:
24563d7b677Scgd 		error->re_status = RPC_CANTDECODEARGS;
24663d7b677Scgd 		return;
24763d7b677Scgd 
24863d7b677Scgd 	case SYSTEM_ERR:
24963d7b677Scgd 		error->re_status = RPC_SYSTEMERROR;
25063d7b677Scgd 		return;
25163d7b677Scgd 
25263d7b677Scgd 	case SUCCESS:
25363d7b677Scgd 		error->re_status = RPC_SUCCESS;
25463d7b677Scgd 		return;
25563d7b677Scgd 	}
2561325a26dSchristos 	/* NOTREACHED */
25763d7b677Scgd 	/* something's wrong, but we don't know what ... */
25863d7b677Scgd 	error->re_status = RPC_FAILED;
2591325a26dSchristos 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
2601325a26dSchristos 	error->re_lb.s2 = (int32_t)acpt_stat;
26163d7b677Scgd }
26263d7b677Scgd 
26363d7b677Scgd static void
rejected(enum reject_stat rjct_stat,struct rpc_err * error)264adb74221Smatt rejected(enum reject_stat rjct_stat, struct rpc_err *error)
26563d7b677Scgd {
26663d7b677Scgd 
267b48252f3Slukem 	_DIAGASSERT(error != NULL);
268b48252f3Slukem 
26963d7b677Scgd 	switch (rjct_stat) {
270ee71b4ceSchristos 	case RPC_MISMATCH:
27163d7b677Scgd 		error->re_status = RPC_VERSMISMATCH;
27263d7b677Scgd 		return;
27363d7b677Scgd 
27463d7b677Scgd 	case AUTH_ERROR:
27563d7b677Scgd 		error->re_status = RPC_AUTHERROR;
27663d7b677Scgd 		return;
27763d7b677Scgd 	}
27863d7b677Scgd 	/* something's wrong, but we don't know what ... */
2791325a26dSchristos 	/* NOTREACHED */
28063d7b677Scgd 	error->re_status = RPC_FAILED;
2811325a26dSchristos 	error->re_lb.s1 = (int32_t)MSG_DENIED;
2821325a26dSchristos 	error->re_lb.s2 = (int32_t)rjct_stat;
28363d7b677Scgd }
28463d7b677Scgd 
28563d7b677Scgd /*
28663d7b677Scgd  * given a reply message, fills in the error
28763d7b677Scgd  */
28863d7b677Scgd void
_seterr_reply(struct rpc_msg * msg,struct rpc_err * error)289adb74221Smatt _seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
29063d7b677Scgd {
29163d7b677Scgd 
292b48252f3Slukem 	_DIAGASSERT(msg != NULL);
293b48252f3Slukem 	_DIAGASSERT(error != NULL);
294b48252f3Slukem 
29563d7b677Scgd 	/* optimized for normal, SUCCESSful case */
29663d7b677Scgd 	switch (msg->rm_reply.rp_stat) {
29763d7b677Scgd 
29863d7b677Scgd 	case MSG_ACCEPTED:
29963d7b677Scgd 		if (msg->acpted_rply.ar_stat == SUCCESS) {
30063d7b677Scgd 			error->re_status = RPC_SUCCESS;
30163d7b677Scgd 			return;
3026c13a3b8Slukem 		}
30363d7b677Scgd 		accepted(msg->acpted_rply.ar_stat, error);
30463d7b677Scgd 		break;
30563d7b677Scgd 
30663d7b677Scgd 	case MSG_DENIED:
30763d7b677Scgd 		rejected(msg->rjcted_rply.rj_stat, error);
30863d7b677Scgd 		break;
30963d7b677Scgd 
31063d7b677Scgd 	default:
31163d7b677Scgd 		error->re_status = RPC_FAILED;
3121325a26dSchristos 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
31363d7b677Scgd 		break;
31463d7b677Scgd 	}
31563d7b677Scgd 	switch (error->re_status) {
31663d7b677Scgd 
31763d7b677Scgd 	case RPC_VERSMISMATCH:
31863d7b677Scgd 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
31963d7b677Scgd 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
32063d7b677Scgd 		break;
32163d7b677Scgd 
32263d7b677Scgd 	case RPC_AUTHERROR:
32363d7b677Scgd 		error->re_why = msg->rjcted_rply.rj_why;
32463d7b677Scgd 		break;
32563d7b677Scgd 
32663d7b677Scgd 	case RPC_PROGVERSMISMATCH:
32763d7b677Scgd 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
32863d7b677Scgd 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
32963d7b677Scgd 		break;
330ee71b4ceSchristos 
331ee71b4ceSchristos 	case RPC_FAILED:
332ee71b4ceSchristos 	case RPC_SUCCESS:
333ee71b4ceSchristos 	case RPC_PROGNOTREGISTERED:
334ee71b4ceSchristos 	case RPC_PMAPFAILURE:
335ee71b4ceSchristos 	case RPC_UNKNOWNPROTO:
336ee71b4ceSchristos 	case RPC_UNKNOWNHOST:
337ee71b4ceSchristos 	case RPC_SYSTEMERROR:
338ee71b4ceSchristos 	case RPC_CANTDECODEARGS:
339ee71b4ceSchristos 	case RPC_PROCUNAVAIL:
340ee71b4ceSchristos 	case RPC_PROGUNAVAIL:
341ee71b4ceSchristos 	case RPC_TIMEDOUT:
342ee71b4ceSchristos 	case RPC_CANTRECV:
343ee71b4ceSchristos 	case RPC_CANTSEND:
344ee71b4ceSchristos 	case RPC_CANTDECODERES:
345ee71b4ceSchristos 	case RPC_CANTENCODEARGS:
3467df0ccbaSfvdl 	default:
347ee71b4ceSchristos 		break;
34863d7b677Scgd 	}
34963d7b677Scgd }
350