1*84d9c625SLionel Sambuc /* $NetBSD: rpcb_prot.c,v 1.11 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras */
362fe8fb19SBen Gras
372fe8fb19SBen Gras /* #ident "@(#)rpcb_prot.c 1.13 94/04/24 SMI" */
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
432fe8fb19SBen Gras #else
44*84d9c625SLionel Sambuc __RCSID("$NetBSD: rpcb_prot.c,v 1.11 2013/03/11 20:19:29 tron Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras
482fe8fb19SBen Gras /*
492fe8fb19SBen Gras * rpcb_prot.c
502fe8fb19SBen Gras * XDR routines for the rpcbinder version 3.
512fe8fb19SBen Gras *
522fe8fb19SBen Gras * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
532fe8fb19SBen Gras */
542fe8fb19SBen Gras
552fe8fb19SBen Gras #include "namespace.h"
562fe8fb19SBen Gras
572fe8fb19SBen Gras #include <rpc/rpc.h>
582fe8fb19SBen Gras #include <rpc/types.h>
592fe8fb19SBen Gras #include <rpc/xdr.h>
602fe8fb19SBen Gras #include <rpc/rpcb_prot.h>
612fe8fb19SBen Gras
622fe8fb19SBen Gras #include <assert.h>
632fe8fb19SBen Gras
642fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(xdr_rpcb,_xdr_rpcb)652fe8fb19SBen Gras __weak_alias(xdr_rpcb,_xdr_rpcb)
662fe8fb19SBen Gras __weak_alias(xdr_rpcblist_ptr,_xdr_rpcblist_ptr)
672fe8fb19SBen Gras __weak_alias(xdr_rpcblist,_xdr_rpcblist)
682fe8fb19SBen Gras __weak_alias(xdr_rpcb_entry,_xdr_rpcb_entry)
692fe8fb19SBen Gras __weak_alias(xdr_rpcb_entry_list_ptr,_xdr_rpcb_entry_list_ptr)
702fe8fb19SBen Gras __weak_alias(xdr_rpcb_rmtcallargs,_xdr_rpcb_rmtcallargs)
712fe8fb19SBen Gras __weak_alias(xdr_rpcb_rmtcallres,_xdr_rpcb_rmtcallres)
722fe8fb19SBen Gras __weak_alias(xdr_netbuf,_xdr_netbuf)
732fe8fb19SBen Gras #endif
742fe8fb19SBen Gras
752fe8fb19SBen Gras
762fe8fb19SBen Gras bool_t
77f14fb602SLionel Sambuc xdr_rpcb(XDR *xdrs, RPCB *objp)
782fe8fb19SBen Gras {
792fe8fb19SBen Gras
802fe8fb19SBen Gras _DIAGASSERT(objp != NULL);
812fe8fb19SBen Gras
822fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
832fe8fb19SBen Gras return (FALSE);
842fe8fb19SBen Gras }
852fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
862fe8fb19SBen Gras return (FALSE);
872fe8fb19SBen Gras }
882fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
892fe8fb19SBen Gras return (FALSE);
902fe8fb19SBen Gras }
912fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
922fe8fb19SBen Gras return (FALSE);
932fe8fb19SBen Gras }
942fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
952fe8fb19SBen Gras return (FALSE);
962fe8fb19SBen Gras }
972fe8fb19SBen Gras return (TRUE);
982fe8fb19SBen Gras }
992fe8fb19SBen Gras
1002fe8fb19SBen Gras /*
1012fe8fb19SBen Gras * rpcblist_ptr implements a linked list. The RPCL definition from
1022fe8fb19SBen Gras * rpcb_prot.x is:
1032fe8fb19SBen Gras *
1042fe8fb19SBen Gras * struct rpcblist {
1052fe8fb19SBen Gras * rpcb rpcb_map;
1062fe8fb19SBen Gras * struct rpcblist *rpcb_next;
1072fe8fb19SBen Gras * };
1082fe8fb19SBen Gras * typedef rpcblist *rpcblist_ptr;
1092fe8fb19SBen Gras *
1102fe8fb19SBen Gras * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
1112fe8fb19SBen Gras * there's any data behind the pointer, followed by the data (if any exists).
1122fe8fb19SBen Gras * The boolean can be interpreted as ``more data follows me''; if FALSE then
1132fe8fb19SBen Gras * nothing follows the boolean; if TRUE then the boolean is followed by an
1142fe8fb19SBen Gras * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
1152fe8fb19SBen Gras * rpcblist *").
1162fe8fb19SBen Gras *
1172fe8fb19SBen Gras * This could be implemented via the xdr_pointer type, though this would
1182fe8fb19SBen Gras * result in one recursive call per element in the list. Rather than do that
1192fe8fb19SBen Gras * we can ``unwind'' the recursion into a while loop and use xdr_reference to
1202fe8fb19SBen Gras * serialize the rpcb elements.
1212fe8fb19SBen Gras */
1222fe8fb19SBen Gras
1232fe8fb19SBen Gras bool_t
xdr_rpcblist_ptr(XDR * xdrs,rpcblist_ptr * rp)124f14fb602SLionel Sambuc xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
1252fe8fb19SBen Gras {
1262fe8fb19SBen Gras /*
1272fe8fb19SBen Gras * more_elements is pre-computed in case the direction is
1282fe8fb19SBen Gras * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
1292fe8fb19SBen Gras * xdr_bool when the direction is XDR_DECODE.
1302fe8fb19SBen Gras */
1312fe8fb19SBen Gras bool_t more_elements;
1322fe8fb19SBen Gras int freeing;
1332fe8fb19SBen Gras rpcblist_ptr next;
1342fe8fb19SBen Gras rpcblist_ptr next_copy;
1352fe8fb19SBen Gras
1362fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
1372fe8fb19SBen Gras /* XXX: rp may be NULL ??? */
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras freeing = (xdrs->x_op == XDR_FREE);
1402fe8fb19SBen Gras next = NULL;
1412fe8fb19SBen Gras
1422fe8fb19SBen Gras for (;;) {
1432fe8fb19SBen Gras more_elements = (bool_t)(*rp != NULL);
1442fe8fb19SBen Gras if (! xdr_bool(xdrs, &more_elements)) {
1452fe8fb19SBen Gras return (FALSE);
1462fe8fb19SBen Gras }
1472fe8fb19SBen Gras if (! more_elements) {
1482fe8fb19SBen Gras return (TRUE); /* we are done */
1492fe8fb19SBen Gras }
1502fe8fb19SBen Gras /*
1512fe8fb19SBen Gras * the unfortunate side effect of non-recursion is that in
1522fe8fb19SBen Gras * the case of freeing we must remember the next object
1532fe8fb19SBen Gras * before we free the current object ...
1542fe8fb19SBen Gras */
1552fe8fb19SBen Gras if (freeing && *rp)
1562fe8fb19SBen Gras next = (*rp)->rpcb_next;
1572fe8fb19SBen Gras if (! xdr_reference(xdrs, (caddr_t *)rp,
1582fe8fb19SBen Gras (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
1592fe8fb19SBen Gras return (FALSE);
1602fe8fb19SBen Gras }
1612fe8fb19SBen Gras if (freeing) {
1622fe8fb19SBen Gras next_copy = next;
1632fe8fb19SBen Gras rp = &next_copy;
1642fe8fb19SBen Gras /*
1652fe8fb19SBen Gras * Note that in the subsequent iteration, next_copy
1662fe8fb19SBen Gras * gets nulled out by the xdr_reference
1672fe8fb19SBen Gras * but next itself survives.
1682fe8fb19SBen Gras */
1692fe8fb19SBen Gras } else if (*rp) {
1702fe8fb19SBen Gras rp = &((*rp)->rpcb_next);
1712fe8fb19SBen Gras }
1722fe8fb19SBen Gras }
1732fe8fb19SBen Gras /*NOTREACHED*/
1742fe8fb19SBen Gras }
1752fe8fb19SBen Gras
1762fe8fb19SBen Gras /*
1772fe8fb19SBen Gras * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
1782fe8fb19SBen Gras * functionality to xdr_rpcblist_ptr().
1792fe8fb19SBen Gras */
1802fe8fb19SBen Gras bool_t
xdr_rpcblist(XDR * xdrs,RPCBLIST ** rp)181f14fb602SLionel Sambuc xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
1822fe8fb19SBen Gras {
1832fe8fb19SBen Gras bool_t dummy;
1842fe8fb19SBen Gras
1852fe8fb19SBen Gras dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
1862fe8fb19SBen Gras return (dummy);
1872fe8fb19SBen Gras }
1882fe8fb19SBen Gras
1892fe8fb19SBen Gras
1902fe8fb19SBen Gras bool_t
xdr_rpcb_entry(XDR * xdrs,rpcb_entry * objp)191f14fb602SLionel Sambuc xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
1922fe8fb19SBen Gras {
1932fe8fb19SBen Gras
1942fe8fb19SBen Gras _DIAGASSERT(objp != NULL);
1952fe8fb19SBen Gras
1962fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
1972fe8fb19SBen Gras return (FALSE);
1982fe8fb19SBen Gras }
1992fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
2002fe8fb19SBen Gras return (FALSE);
2012fe8fb19SBen Gras }
2022fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
2032fe8fb19SBen Gras return (FALSE);
2042fe8fb19SBen Gras }
2052fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
2062fe8fb19SBen Gras return (FALSE);
2072fe8fb19SBen Gras }
2082fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
2092fe8fb19SBen Gras return (FALSE);
2102fe8fb19SBen Gras }
2112fe8fb19SBen Gras return (TRUE);
2122fe8fb19SBen Gras }
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras bool_t
xdr_rpcb_entry_list_ptr(XDR * xdrs,rpcb_entry_list_ptr * rp)215f14fb602SLionel Sambuc xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
2162fe8fb19SBen Gras {
2172fe8fb19SBen Gras /*
2182fe8fb19SBen Gras * more_elements is pre-computed in case the direction is
2192fe8fb19SBen Gras * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
2202fe8fb19SBen Gras * xdr_bool when the direction is XDR_DECODE.
2212fe8fb19SBen Gras */
2222fe8fb19SBen Gras bool_t more_elements;
2232fe8fb19SBen Gras int freeing;
2242fe8fb19SBen Gras rpcb_entry_list_ptr next;
2252fe8fb19SBen Gras rpcb_entry_list_ptr next_copy;
2262fe8fb19SBen Gras
2272fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
2282fe8fb19SBen Gras /* XXX: rp is allowed to be NULL ??? */
2292fe8fb19SBen Gras
2302fe8fb19SBen Gras freeing = (xdrs->x_op == XDR_FREE);
2312fe8fb19SBen Gras next = NULL;
2322fe8fb19SBen Gras
2332fe8fb19SBen Gras for (;;) {
2342fe8fb19SBen Gras more_elements = (bool_t)(*rp != NULL);
2352fe8fb19SBen Gras if (! xdr_bool(xdrs, &more_elements)) {
2362fe8fb19SBen Gras return (FALSE);
2372fe8fb19SBen Gras }
2382fe8fb19SBen Gras if (! more_elements) {
2392fe8fb19SBen Gras return (TRUE); /* we are done */
2402fe8fb19SBen Gras }
2412fe8fb19SBen Gras /*
2422fe8fb19SBen Gras * the unfortunate side effect of non-recursion is that in
2432fe8fb19SBen Gras * the case of freeing we must remember the next object
2442fe8fb19SBen Gras * before we free the current object ...
2452fe8fb19SBen Gras */
2462fe8fb19SBen Gras if (freeing && *rp)
2472fe8fb19SBen Gras next = (*rp)->rpcb_entry_next;
2482fe8fb19SBen Gras if (! xdr_reference(xdrs, (caddr_t *)rp,
2492fe8fb19SBen Gras (u_int)sizeof (rpcb_entry_list),
2502fe8fb19SBen Gras (xdrproc_t)xdr_rpcb_entry)) {
2512fe8fb19SBen Gras return (FALSE);
2522fe8fb19SBen Gras }
2532fe8fb19SBen Gras if (freeing) {
2542fe8fb19SBen Gras next_copy = next;
2552fe8fb19SBen Gras rp = &next_copy;
2562fe8fb19SBen Gras /*
2572fe8fb19SBen Gras * Note that in the subsequent iteration, next_copy
2582fe8fb19SBen Gras * gets nulled out by the xdr_reference
2592fe8fb19SBen Gras * but next itself survives.
2602fe8fb19SBen Gras */
2612fe8fb19SBen Gras } else if (*rp) {
2622fe8fb19SBen Gras rp = &((*rp)->rpcb_entry_next);
2632fe8fb19SBen Gras }
2642fe8fb19SBen Gras }
2652fe8fb19SBen Gras /*NOTREACHED*/
2662fe8fb19SBen Gras }
2672fe8fb19SBen Gras
2682fe8fb19SBen Gras /*
2692fe8fb19SBen Gras * XDR remote call arguments
2702fe8fb19SBen Gras * written for XDR_ENCODE direction only
2712fe8fb19SBen Gras */
2722fe8fb19SBen Gras bool_t
xdr_rpcb_rmtcallargs(XDR * xdrs,struct rpcb_rmtcallargs * p)273f14fb602SLionel Sambuc xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
2742fe8fb19SBen Gras {
2752fe8fb19SBen Gras struct r_rpcb_rmtcallargs *objp =
2762fe8fb19SBen Gras (struct r_rpcb_rmtcallargs *)(void *)p;
2772fe8fb19SBen Gras u_int lenposition, argposition, position;
2782fe8fb19SBen Gras int32_t *buf;
2792fe8fb19SBen Gras
2802fe8fb19SBen Gras _DIAGASSERT(p != NULL);
2812fe8fb19SBen Gras
2822fe8fb19SBen Gras buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
2832fe8fb19SBen Gras if (buf == NULL) {
2842fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->prog)) {
2852fe8fb19SBen Gras return (FALSE);
2862fe8fb19SBen Gras }
2872fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->vers)) {
2882fe8fb19SBen Gras return (FALSE);
2892fe8fb19SBen Gras }
2902fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, &objp->proc)) {
2912fe8fb19SBen Gras return (FALSE);
2922fe8fb19SBen Gras }
2932fe8fb19SBen Gras } else {
2942fe8fb19SBen Gras IXDR_PUT_U_INT32(buf, objp->prog);
2952fe8fb19SBen Gras IXDR_PUT_U_INT32(buf, objp->vers);
2962fe8fb19SBen Gras IXDR_PUT_U_INT32(buf, objp->proc);
2972fe8fb19SBen Gras }
2982fe8fb19SBen Gras
2992fe8fb19SBen Gras /*
3002fe8fb19SBen Gras * All the jugglery for just getting the size of the arguments
3012fe8fb19SBen Gras */
3022fe8fb19SBen Gras lenposition = XDR_GETPOS(xdrs);
3032fe8fb19SBen Gras if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
3042fe8fb19SBen Gras return (FALSE);
3052fe8fb19SBen Gras }
3062fe8fb19SBen Gras argposition = XDR_GETPOS(xdrs);
3072fe8fb19SBen Gras if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
3082fe8fb19SBen Gras return (FALSE);
3092fe8fb19SBen Gras }
3102fe8fb19SBen Gras position = XDR_GETPOS(xdrs);
3112fe8fb19SBen Gras objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
3122fe8fb19SBen Gras XDR_SETPOS(xdrs, lenposition);
3132fe8fb19SBen Gras if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
3142fe8fb19SBen Gras return (FALSE);
3152fe8fb19SBen Gras }
3162fe8fb19SBen Gras XDR_SETPOS(xdrs, position);
3172fe8fb19SBen Gras return (TRUE);
3182fe8fb19SBen Gras }
3192fe8fb19SBen Gras
3202fe8fb19SBen Gras /*
3212fe8fb19SBen Gras * XDR remote call results
3222fe8fb19SBen Gras * written for XDR_DECODE direction only
3232fe8fb19SBen Gras */
3242fe8fb19SBen Gras bool_t
xdr_rpcb_rmtcallres(XDR * xdrs,struct rpcb_rmtcallres * p)325f14fb602SLionel Sambuc xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
3262fe8fb19SBen Gras {
3272fe8fb19SBen Gras bool_t dummy;
3282fe8fb19SBen Gras struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
3292fe8fb19SBen Gras
3302fe8fb19SBen Gras _DIAGASSERT(p != NULL);
3312fe8fb19SBen Gras
3322fe8fb19SBen Gras if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
3332fe8fb19SBen Gras return (FALSE);
3342fe8fb19SBen Gras }
3352fe8fb19SBen Gras if (!xdr_u_int(xdrs, &objp->results.results_len)) {
3362fe8fb19SBen Gras return (FALSE);
3372fe8fb19SBen Gras }
3382fe8fb19SBen Gras dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
3392fe8fb19SBen Gras return (dummy);
3402fe8fb19SBen Gras }
3412fe8fb19SBen Gras
3422fe8fb19SBen Gras bool_t
xdr_netbuf(XDR * xdrs,struct netbuf * objp)343f14fb602SLionel Sambuc xdr_netbuf(XDR *xdrs, struct netbuf *objp)
3442fe8fb19SBen Gras {
3452fe8fb19SBen Gras bool_t dummy;
3462fe8fb19SBen Gras
3472fe8fb19SBen Gras _DIAGASSERT(objp != NULL);
3482fe8fb19SBen Gras
3492fe8fb19SBen Gras if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
3502fe8fb19SBen Gras return (FALSE);
3512fe8fb19SBen Gras }
3522fe8fb19SBen Gras dummy = xdr_bytes(xdrs, (char **)(void *)&(objp->buf),
3532fe8fb19SBen Gras (u_int *)&(objp->len), objp->maxlen);
3542fe8fb19SBen Gras return (dummy);
3552fe8fb19SBen Gras }
356