1*17003265Schristos /* $NetBSD: rpcb_prot.c,v 1.12 2017/05/03 21:39:27 christos Exp $ */
27df0ccbaSfvdl
37df0ccbaSfvdl /*
447c0e0c3Stron * Copyright (c) 2010, Oracle America, Inc.
57df0ccbaSfvdl *
647c0e0c3Stron * Redistribution and use in source and binary forms, with or without
747c0e0c3Stron * modification, are permitted provided that the following conditions are
847c0e0c3Stron * met:
97df0ccbaSfvdl *
1047c0e0c3Stron * * Redistributions of source code must retain the above copyright
1147c0e0c3Stron * notice, this list of conditions and the following disclaimer.
1247c0e0c3Stron * * Redistributions in binary form must reproduce the above
1347c0e0c3Stron * copyright notice, this list of conditions and the following
1447c0e0c3Stron * disclaimer in the documentation and/or other materials
1547c0e0c3Stron * provided with the distribution.
1647c0e0c3Stron * * Neither the name of the "Oracle America, Inc." nor the names of its
1747c0e0c3Stron * contributors may be used to endorse or promote products derived
1847c0e0c3Stron * from this software without specific prior written permission.
197df0ccbaSfvdl *
2047c0e0c3Stron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2147c0e0c3Stron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2247c0e0c3Stron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2347c0e0c3Stron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2447c0e0c3Stron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2547c0e0c3Stron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2647c0e0c3Stron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2747c0e0c3Stron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2847c0e0c3Stron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2947c0e0c3Stron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3047c0e0c3Stron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3147c0e0c3Stron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
327df0ccbaSfvdl */
337df0ccbaSfvdl /*
347df0ccbaSfvdl * Copyright (c) 1986-1991 by Sun Microsystems Inc.
357df0ccbaSfvdl */
367df0ccbaSfvdl
377df0ccbaSfvdl /* #ident "@(#)rpcb_prot.c 1.13 94/04/24 SMI" */
387df0ccbaSfvdl
395c945215Sitojun #include <sys/cdefs.h>
405c945215Sitojun #if defined(LIBC_SCCS) && !defined(lint)
417df0ccbaSfvdl #if 0
427df0ccbaSfvdl static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
435c945215Sitojun #else
44*17003265Schristos __RCSID("$NetBSD: rpcb_prot.c,v 1.12 2017/05/03 21:39:27 christos Exp $");
457df0ccbaSfvdl #endif
467df0ccbaSfvdl #endif
477df0ccbaSfvdl
487df0ccbaSfvdl /*
497df0ccbaSfvdl * rpcb_prot.c
507df0ccbaSfvdl * XDR routines for the rpcbinder version 3.
517df0ccbaSfvdl *
527df0ccbaSfvdl * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
537df0ccbaSfvdl */
547df0ccbaSfvdl
557df0ccbaSfvdl #include "namespace.h"
567df0ccbaSfvdl
577df0ccbaSfvdl #include <rpc/rpc.h>
587df0ccbaSfvdl #include <rpc/types.h>
597df0ccbaSfvdl #include <rpc/xdr.h>
607df0ccbaSfvdl #include <rpc/rpcb_prot.h>
61*17003265Schristos #include <rpc/rpc_com.h>
627df0ccbaSfvdl
630e8cfd8fSlukem #include <assert.h>
640e8cfd8fSlukem
657df0ccbaSfvdl #ifdef __weak_alias
__weak_alias(xdr_rpcb,_xdr_rpcb)667df0ccbaSfvdl __weak_alias(xdr_rpcb,_xdr_rpcb)
677df0ccbaSfvdl __weak_alias(xdr_rpcblist_ptr,_xdr_rpcblist_ptr)
687df0ccbaSfvdl __weak_alias(xdr_rpcblist,_xdr_rpcblist)
697df0ccbaSfvdl __weak_alias(xdr_rpcb_entry,_xdr_rpcb_entry)
707df0ccbaSfvdl __weak_alias(xdr_rpcb_entry_list_ptr,_xdr_rpcb_entry_list_ptr)
717df0ccbaSfvdl __weak_alias(xdr_rpcb_rmtcallargs,_xdr_rpcb_rmtcallargs)
727df0ccbaSfvdl __weak_alias(xdr_rpcb_rmtcallres,_xdr_rpcb_rmtcallres)
737df0ccbaSfvdl __weak_alias(xdr_netbuf,_xdr_netbuf)
747df0ccbaSfvdl #endif
757df0ccbaSfvdl
767df0ccbaSfvdl
777df0ccbaSfvdl bool_t
789e66e6d7Sabs xdr_rpcb(XDR *xdrs, RPCB *objp)
797df0ccbaSfvdl {
800e8cfd8fSlukem
810e8cfd8fSlukem _DIAGASSERT(objp != NULL);
820e8cfd8fSlukem
837df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
847df0ccbaSfvdl return (FALSE);
857df0ccbaSfvdl }
867df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
877df0ccbaSfvdl return (FALSE);
887df0ccbaSfvdl }
89*17003265Schristos if (!xdr_string(xdrs, &objp->r_netid, RPC_MAXDATASIZE)) {
907df0ccbaSfvdl return (FALSE);
917df0ccbaSfvdl }
92*17003265Schristos if (!xdr_string(xdrs, &objp->r_addr, RPC_MAXDATASIZE)) {
937df0ccbaSfvdl return (FALSE);
947df0ccbaSfvdl }
95*17003265Schristos if (!xdr_string(xdrs, &objp->r_owner, RPC_MAXDATASIZE)) {
967df0ccbaSfvdl return (FALSE);
977df0ccbaSfvdl }
987df0ccbaSfvdl return (TRUE);
997df0ccbaSfvdl }
1007df0ccbaSfvdl
1017df0ccbaSfvdl /*
1027df0ccbaSfvdl * rpcblist_ptr implements a linked list. The RPCL definition from
1037df0ccbaSfvdl * rpcb_prot.x is:
1047df0ccbaSfvdl *
1057df0ccbaSfvdl * struct rpcblist {
1067df0ccbaSfvdl * rpcb rpcb_map;
1077df0ccbaSfvdl * struct rpcblist *rpcb_next;
1087df0ccbaSfvdl * };
1097df0ccbaSfvdl * typedef rpcblist *rpcblist_ptr;
1107df0ccbaSfvdl *
1117df0ccbaSfvdl * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
1127df0ccbaSfvdl * there's any data behind the pointer, followed by the data (if any exists).
1137df0ccbaSfvdl * The boolean can be interpreted as ``more data follows me''; if FALSE then
1147df0ccbaSfvdl * nothing follows the boolean; if TRUE then the boolean is followed by an
1157df0ccbaSfvdl * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
1167df0ccbaSfvdl * rpcblist *").
1177df0ccbaSfvdl *
1187df0ccbaSfvdl * This could be implemented via the xdr_pointer type, though this would
1197df0ccbaSfvdl * result in one recursive call per element in the list. Rather than do that
1207df0ccbaSfvdl * we can ``unwind'' the recursion into a while loop and use xdr_reference to
1217df0ccbaSfvdl * serialize the rpcb elements.
1227df0ccbaSfvdl */
1237df0ccbaSfvdl
1247df0ccbaSfvdl bool_t
xdr_rpcblist_ptr(XDR * xdrs,rpcblist_ptr * rp)1259e66e6d7Sabs xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
1267df0ccbaSfvdl {
1277df0ccbaSfvdl /*
1287df0ccbaSfvdl * more_elements is pre-computed in case the direction is
1297df0ccbaSfvdl * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
1307df0ccbaSfvdl * xdr_bool when the direction is XDR_DECODE.
1317df0ccbaSfvdl */
1327df0ccbaSfvdl bool_t more_elements;
1330e8cfd8fSlukem int freeing;
1347df0ccbaSfvdl rpcblist_ptr next;
1357df0ccbaSfvdl rpcblist_ptr next_copy;
1367df0ccbaSfvdl
1370e8cfd8fSlukem _DIAGASSERT(xdrs != NULL);
1380e8cfd8fSlukem /* XXX: rp may be NULL ??? */
1390e8cfd8fSlukem
1400e8cfd8fSlukem freeing = (xdrs->x_op == XDR_FREE);
1413af3e6a4Slukem next = NULL;
1420e8cfd8fSlukem
143deb154d2Schristos for (;;) {
1447df0ccbaSfvdl more_elements = (bool_t)(*rp != NULL);
1457df0ccbaSfvdl if (! xdr_bool(xdrs, &more_elements)) {
1467df0ccbaSfvdl return (FALSE);
1477df0ccbaSfvdl }
1487df0ccbaSfvdl if (! more_elements) {
1497df0ccbaSfvdl return (TRUE); /* we are done */
1507df0ccbaSfvdl }
1517df0ccbaSfvdl /*
1527df0ccbaSfvdl * the unfortunate side effect of non-recursion is that in
1537df0ccbaSfvdl * the case of freeing we must remember the next object
1547df0ccbaSfvdl * before we free the current object ...
1557df0ccbaSfvdl */
156d06c1d3eSchristos if (freeing && *rp)
1577df0ccbaSfvdl next = (*rp)->rpcb_next;
1587df0ccbaSfvdl if (! xdr_reference(xdrs, (caddr_t *)rp,
1597df0ccbaSfvdl (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
1607df0ccbaSfvdl return (FALSE);
1617df0ccbaSfvdl }
1627df0ccbaSfvdl if (freeing) {
1637df0ccbaSfvdl next_copy = next;
1647df0ccbaSfvdl rp = &next_copy;
1657df0ccbaSfvdl /*
1667df0ccbaSfvdl * Note that in the subsequent iteration, next_copy
1677df0ccbaSfvdl * gets nulled out by the xdr_reference
1687df0ccbaSfvdl * but next itself survives.
1697df0ccbaSfvdl */
170d06c1d3eSchristos } else if (*rp) {
1717df0ccbaSfvdl rp = &((*rp)->rpcb_next);
1727df0ccbaSfvdl }
1737df0ccbaSfvdl }
1747df0ccbaSfvdl /*NOTREACHED*/
1757df0ccbaSfvdl }
1767df0ccbaSfvdl
1777df0ccbaSfvdl /*
1787df0ccbaSfvdl * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
1797df0ccbaSfvdl * functionality to xdr_rpcblist_ptr().
1807df0ccbaSfvdl */
1817df0ccbaSfvdl bool_t
xdr_rpcblist(XDR * xdrs,RPCBLIST ** rp)1829e66e6d7Sabs xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
1837df0ccbaSfvdl {
1847df0ccbaSfvdl bool_t dummy;
1857df0ccbaSfvdl
1867df0ccbaSfvdl dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
1877df0ccbaSfvdl return (dummy);
1887df0ccbaSfvdl }
1897df0ccbaSfvdl
1907df0ccbaSfvdl
1917df0ccbaSfvdl bool_t
xdr_rpcb_entry(XDR * xdrs,rpcb_entry * objp)1929e66e6d7Sabs xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
1937df0ccbaSfvdl {
1940e8cfd8fSlukem
1950e8cfd8fSlukem _DIAGASSERT(objp != NULL);
1960e8cfd8fSlukem
197*17003265Schristos if (!xdr_string(xdrs, &objp->r_maddr, RPC_MAXDATASIZE)) {
1987df0ccbaSfvdl return (FALSE);
1997df0ccbaSfvdl }
200*17003265Schristos if (!xdr_string(xdrs, &objp->r_nc_netid, RPC_MAXDATASIZE)) {
2017df0ccbaSfvdl return (FALSE);
2027df0ccbaSfvdl }
2037df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
2047df0ccbaSfvdl return (FALSE);
2057df0ccbaSfvdl }
206*17003265Schristos if (!xdr_string(xdrs, &objp->r_nc_protofmly, RPC_MAXDATASIZE)) {
2077df0ccbaSfvdl return (FALSE);
2087df0ccbaSfvdl }
209*17003265Schristos if (!xdr_string(xdrs, &objp->r_nc_proto, RPC_MAXDATASIZE)) {
2107df0ccbaSfvdl return (FALSE);
2117df0ccbaSfvdl }
2127df0ccbaSfvdl return (TRUE);
2137df0ccbaSfvdl }
2147df0ccbaSfvdl
2157df0ccbaSfvdl bool_t
xdr_rpcb_entry_list_ptr(XDR * xdrs,rpcb_entry_list_ptr * rp)2169e66e6d7Sabs xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
2177df0ccbaSfvdl {
2187df0ccbaSfvdl /*
2197df0ccbaSfvdl * more_elements is pre-computed in case the direction is
2207df0ccbaSfvdl * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
2217df0ccbaSfvdl * xdr_bool when the direction is XDR_DECODE.
2227df0ccbaSfvdl */
2237df0ccbaSfvdl bool_t more_elements;
2240e8cfd8fSlukem int freeing;
2257df0ccbaSfvdl rpcb_entry_list_ptr next;
2267df0ccbaSfvdl rpcb_entry_list_ptr next_copy;
2277df0ccbaSfvdl
2280e8cfd8fSlukem _DIAGASSERT(xdrs != NULL);
2290e8cfd8fSlukem /* XXX: rp is allowed to be NULL ??? */
2300e8cfd8fSlukem
2310e8cfd8fSlukem freeing = (xdrs->x_op == XDR_FREE);
2323af3e6a4Slukem next = NULL;
2330e8cfd8fSlukem
234deb154d2Schristos for (;;) {
2357df0ccbaSfvdl more_elements = (bool_t)(*rp != NULL);
2367df0ccbaSfvdl if (! xdr_bool(xdrs, &more_elements)) {
2377df0ccbaSfvdl return (FALSE);
2387df0ccbaSfvdl }
2397df0ccbaSfvdl if (! more_elements) {
2407df0ccbaSfvdl return (TRUE); /* we are done */
2417df0ccbaSfvdl }
2427df0ccbaSfvdl /*
2437df0ccbaSfvdl * the unfortunate side effect of non-recursion is that in
2447df0ccbaSfvdl * the case of freeing we must remember the next object
2457df0ccbaSfvdl * before we free the current object ...
2467df0ccbaSfvdl */
2474435b154Schristos if (freeing && *rp)
2487df0ccbaSfvdl next = (*rp)->rpcb_entry_next;
2497df0ccbaSfvdl if (! xdr_reference(xdrs, (caddr_t *)rp,
2507df0ccbaSfvdl (u_int)sizeof (rpcb_entry_list),
2517df0ccbaSfvdl (xdrproc_t)xdr_rpcb_entry)) {
2527df0ccbaSfvdl return (FALSE);
2537df0ccbaSfvdl }
2547df0ccbaSfvdl if (freeing) {
2557df0ccbaSfvdl next_copy = next;
2567df0ccbaSfvdl rp = &next_copy;
2577df0ccbaSfvdl /*
2587df0ccbaSfvdl * Note that in the subsequent iteration, next_copy
2597df0ccbaSfvdl * gets nulled out by the xdr_reference
2607df0ccbaSfvdl * but next itself survives.
2617df0ccbaSfvdl */
2624435b154Schristos } else if (*rp) {
2637df0ccbaSfvdl rp = &((*rp)->rpcb_entry_next);
2647df0ccbaSfvdl }
2657df0ccbaSfvdl }
2667df0ccbaSfvdl /*NOTREACHED*/
2677df0ccbaSfvdl }
2687df0ccbaSfvdl
2697df0ccbaSfvdl /*
2707df0ccbaSfvdl * XDR remote call arguments
2717df0ccbaSfvdl * written for XDR_ENCODE direction only
2727df0ccbaSfvdl */
2737df0ccbaSfvdl bool_t
xdr_rpcb_rmtcallargs(XDR * xdrs,struct rpcb_rmtcallargs * p)2749e66e6d7Sabs xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
2757df0ccbaSfvdl {
276deb154d2Schristos struct r_rpcb_rmtcallargs *objp =
277deb154d2Schristos (struct r_rpcb_rmtcallargs *)(void *)p;
2787df0ccbaSfvdl u_int lenposition, argposition, position;
2797df0ccbaSfvdl int32_t *buf;
2807df0ccbaSfvdl
2810e8cfd8fSlukem _DIAGASSERT(p != NULL);
2820e8cfd8fSlukem
2837df0ccbaSfvdl buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
2847df0ccbaSfvdl if (buf == NULL) {
2857df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->prog)) {
2867df0ccbaSfvdl return (FALSE);
2877df0ccbaSfvdl }
2887df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->vers)) {
2897df0ccbaSfvdl return (FALSE);
2907df0ccbaSfvdl }
2917df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, &objp->proc)) {
2927df0ccbaSfvdl return (FALSE);
2937df0ccbaSfvdl }
2947df0ccbaSfvdl } else {
2957c5c8962Sfvdl IXDR_PUT_U_INT32(buf, objp->prog);
2967c5c8962Sfvdl IXDR_PUT_U_INT32(buf, objp->vers);
2977c5c8962Sfvdl IXDR_PUT_U_INT32(buf, objp->proc);
2987df0ccbaSfvdl }
2997df0ccbaSfvdl
3007df0ccbaSfvdl /*
3017df0ccbaSfvdl * All the jugglery for just getting the size of the arguments
3027df0ccbaSfvdl */
3037df0ccbaSfvdl lenposition = XDR_GETPOS(xdrs);
3047df0ccbaSfvdl if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
3057df0ccbaSfvdl return (FALSE);
3067df0ccbaSfvdl }
3077df0ccbaSfvdl argposition = XDR_GETPOS(xdrs);
3087df0ccbaSfvdl if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
3097df0ccbaSfvdl return (FALSE);
3107df0ccbaSfvdl }
3117df0ccbaSfvdl position = XDR_GETPOS(xdrs);
312deb154d2Schristos objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
3137df0ccbaSfvdl XDR_SETPOS(xdrs, lenposition);
3147df0ccbaSfvdl if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
3157df0ccbaSfvdl return (FALSE);
3167df0ccbaSfvdl }
3177df0ccbaSfvdl XDR_SETPOS(xdrs, position);
3187df0ccbaSfvdl return (TRUE);
3197df0ccbaSfvdl }
3207df0ccbaSfvdl
3217df0ccbaSfvdl /*
3227df0ccbaSfvdl * XDR remote call results
3237df0ccbaSfvdl * written for XDR_DECODE direction only
3247df0ccbaSfvdl */
3257df0ccbaSfvdl bool_t
xdr_rpcb_rmtcallres(XDR * xdrs,struct rpcb_rmtcallres * p)3269e66e6d7Sabs xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
3277df0ccbaSfvdl {
3287df0ccbaSfvdl bool_t dummy;
329deb154d2Schristos struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
3307df0ccbaSfvdl
3310e8cfd8fSlukem _DIAGASSERT(p != NULL);
3320e8cfd8fSlukem
333*17003265Schristos if (!xdr_string(xdrs, &objp->addr, RPC_MAXDATASIZE)) {
3347df0ccbaSfvdl return (FALSE);
3357df0ccbaSfvdl }
3367df0ccbaSfvdl if (!xdr_u_int(xdrs, &objp->results.results_len)) {
3377df0ccbaSfvdl return (FALSE);
3387df0ccbaSfvdl }
3397df0ccbaSfvdl dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
3407df0ccbaSfvdl return (dummy);
3417df0ccbaSfvdl }
3427df0ccbaSfvdl
3437df0ccbaSfvdl bool_t
xdr_netbuf(XDR * xdrs,struct netbuf * objp)3449e66e6d7Sabs xdr_netbuf(XDR *xdrs, struct netbuf *objp)
3457df0ccbaSfvdl {
3467df0ccbaSfvdl bool_t dummy;
3477df0ccbaSfvdl
3480e8cfd8fSlukem _DIAGASSERT(objp != NULL);
3490e8cfd8fSlukem
3507df0ccbaSfvdl if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
3517df0ccbaSfvdl return (FALSE);
3527df0ccbaSfvdl }
353*17003265Schristos
354*17003265Schristos if (objp->maxlen > RPC_MAXDATASIZE) {
355*17003265Schristos return (FALSE);
356*17003265Schristos }
357*17003265Schristos
3586f4493cdSmrg dummy = xdr_bytes(xdrs, (char **)(void *)&(objp->buf),
3597df0ccbaSfvdl (u_int *)&(objp->len), objp->maxlen);
3607df0ccbaSfvdl return (dummy);
3617df0ccbaSfvdl }
362