Home | History | Annotate | Line # | Download | only in yp
xdryp.c revision 1.14
      1 /*	$NetBSD: xdryp.c,v 1.14 1996/08/09 10:06:09 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Jason R. Thorpe <thorpej (at) NetBSD.ORG>.
      5  * All rights reserved.
      6  *
      7  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      8  * All rights reserved.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by Theo de Raadt.
     21  *	This product includes software developed for the NetBSD Project
     22  *	by Jason R. Thorpe.
     23  * 4. The name of the author may not be used to endorse or promote products
     24  *    derived from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     27  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #if defined(LIBC_SCCS) && !defined(lint)
     40 static char *rcsid = "$NetBSD: xdryp.c,v 1.14 1996/08/09 10:06:09 thorpej Exp $";
     41 #endif
     42 
     43 /*
     44  * XDR routines used by the YP protocol.  Note that these routines do
     45  * not strictly conform to the RPC definition in yp.x.  This file
     46  * replicates the functions exported by the Sun YP API; reality is
     47  * often inaccurate.
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/types.h>
     52 #include <sys/socket.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <ctype.h>
     57 #include <rpc/rpc.h>
     58 #include <rpc/xdr.h>
     59 #include <rpcsvc/yp_prot.h>
     60 #include <rpcsvc/ypclnt.h>
     61 
     62 /*
     63  * Functions used only within this file.
     64  */
     65 static	bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *));
     66 static	bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *));
     67 static	bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *));
     68 static	bool_t xdr_ypmaplist_str __P((XDR *, char *));
     69 
     70 __warn_references(xdr_domainname,
     71     "warning: this program uses xdr_domainname(), which is deprecated and buggy.");
     72 
     73 bool_t
     74 xdr_domainname(xdrs, objp)
     75 	XDR *xdrs;
     76 	char *objp;
     77 {
     78 	return xdr_string(xdrs, &objp, YPMAXDOMAIN);
     79 }
     80 
     81 __warn_references(xdr_peername,
     82     "warning: this program uses xdr_peername(), which is deprecated and buggy.");
     83 
     84 bool_t
     85 xdr_peername(xdrs, objp)
     86 	XDR *xdrs;
     87 	char *objp;
     88 {
     89 	return xdr_string(xdrs, &objp, YPMAXPEER);
     90 }
     91 
     92 __warn_references(xdr_mapname,
     93     "warning: this program uses xdr_mapname(), which is deprecated and buggy.");
     94 
     95 bool_t
     96 xdr_mapname(xdrs, objp)
     97 	XDR *xdrs;
     98 	char *objp;
     99 {
    100 	return xdr_string(xdrs, &objp, YPMAXMAP);
    101 }
    102 
    103 bool_t
    104 xdr_ypdomain_wrap_string(xdrs, objp)
    105 	XDR *xdrs;
    106 	char **objp;
    107 {
    108 	return xdr_string(xdrs, objp, YPMAXDOMAIN);
    109 }
    110 
    111 bool_t
    112 xdr_ypmap_wrap_string(xdrs, objp)
    113 	XDR *xdrs;
    114 	char **objp;
    115 {
    116 	return xdr_string(xdrs, objp, YPMAXMAP);
    117 }
    118 
    119 bool_t
    120 xdr_ypowner_wrap_string(xdrs, objp)
    121 	XDR *xdrs;
    122 	char **objp;
    123 {
    124 	return xdr_string(xdrs, objp, YPMAXPEER);
    125 }
    126 
    127 bool_t
    128 xdr_datum(xdrs, objp)
    129 	XDR *xdrs;
    130 	datum *objp;
    131 {
    132 	return xdr_bytes(xdrs, (char **)&objp->dptr,
    133 	    (u_int *)&objp->dsize, YPMAXRECORD);
    134 }
    135 
    136 bool_t
    137 xdr_ypreq_key(xdrs, objp)
    138 	XDR *xdrs;
    139 	struct ypreq_key *objp;
    140 {
    141 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    142 		return FALSE;
    143 
    144 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    145 		return FALSE;
    146 
    147 	if (!xdr_datum(xdrs, &objp->keydat))
    148 		return FALSE;
    149 
    150 	return TRUE;
    151 }
    152 
    153 bool_t
    154 xdr_ypreq_nokey(xdrs, objp)
    155 	XDR *xdrs;
    156 	struct ypreq_nokey *objp;
    157 {
    158 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    159 		return FALSE;
    160 
    161 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    162 		return FALSE;
    163 
    164 	return TRUE;
    165 }
    166 
    167 bool_t
    168 xdr_yp_inaddr(xdrs, objp)
    169 	XDR *xdrs;
    170 	struct in_addr *objp;
    171 {
    172 	return xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr);
    173 }
    174 
    175 static bool_t
    176 xdr_ypbind_binding(xdrs, objp)
    177 	XDR *xdrs;
    178 	struct ypbind_binding *objp;
    179 {
    180 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
    181 		return FALSE;
    182 
    183 	if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
    184 	    sizeof objp->ypbind_binding_port))
    185 		return FALSE;
    186 
    187 	return TRUE;
    188 }
    189 
    190 static bool_t
    191 xdr_ypbind_resptype(xdrs, objp)
    192 	XDR *xdrs;
    193 	enum ypbind_resptype *objp;
    194 {
    195 	return xdr_enum(xdrs, (enum_t *)objp);
    196 }
    197 
    198 static bool_t
    199 xdr_ypstat(xdrs, objp)
    200 	XDR *xdrs;
    201 	enum ypbind_resptype *objp;
    202 {
    203 	return xdr_enum(xdrs, (enum_t *)objp);
    204 }
    205 
    206 bool_t
    207 xdr_ypbind_resp(xdrs, objp)
    208 	XDR *xdrs;
    209 	struct ypbind_resp *objp;
    210 {
    211 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
    212 		return FALSE;
    213 
    214 	switch (objp->ypbind_status) {
    215 	case YPBIND_FAIL_VAL:
    216 		return xdr_u_int(xdrs,
    217 		    (u_int *)&objp->ypbind_respbody.ypbind_error);
    218 
    219 	case YPBIND_SUCC_VAL:
    220 		return xdr_ypbind_binding(xdrs,
    221 		    &objp->ypbind_respbody.ypbind_bindinfo);
    222 
    223 	default:
    224 		return FALSE;
    225 	}
    226 	/* NOTREACHED */
    227 }
    228 
    229 bool_t
    230 xdr_ypresp_val(xdrs, objp)
    231 	XDR *xdrs;
    232 	struct ypresp_val *objp;
    233 {
    234 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    235 		return FALSE;
    236 
    237 	if (!xdr_datum(xdrs, &objp->valdat))
    238 		return FALSE;
    239 
    240 	return TRUE;
    241 }
    242 
    243 bool_t
    244 xdr_ypbind_setdom(xdrs, objp)
    245 	XDR *xdrs;
    246 	struct ypbind_setdom *objp;
    247 {
    248 	char *cp = objp->ypsetdom_domain;
    249 
    250 	if (!xdr_ypdomain_wrap_string(xdrs, &cp))
    251 		return FALSE;
    252 
    253 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
    254 		return FALSE;
    255 
    256 	if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
    257 		return FALSE;
    258 
    259 	return TRUE;
    260 }
    261 
    262 bool_t
    263 xdr_ypresp_key_val(xdrs, objp)
    264 	XDR *xdrs;
    265 	struct ypresp_key_val *objp;
    266 {
    267 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    268 		return FALSE;
    269 
    270 	if (!xdr_datum(xdrs, &objp->valdat))
    271 		return FALSE;
    272 
    273 	if (!xdr_datum(xdrs, &objp->keydat))
    274 		return FALSE;
    275 
    276 	return TRUE;
    277 }
    278 
    279 bool_t
    280 xdr_ypall(xdrs, incallback)
    281 	XDR *xdrs;
    282 	struct ypall_callback *incallback;
    283 {
    284 	struct ypresp_key_val out;
    285 	char key[YPMAXRECORD], val[YPMAXRECORD];
    286 	bool_t more, status;
    287 	int size;
    288 	int r;
    289 
    290 	/*
    291 	 * Set up key/val struct to be used during the transaction.
    292 	 */
    293 	memset(&out, 0, sizeof out);
    294 	out.keydat.dptr = key;
    295 	out.keydat.dsize = sizeof(key);
    296 	out.valdat.dptr = val;
    297 	out.valdat.dsize = sizeof(val);
    298 
    299 	for (;;) {
    300 		/* Values pending? */
    301 		if (!xdr_bool(xdrs, &more))
    302 			return FALSE;		/* can't tell! */
    303 		if (!more)
    304 			return TRUE;		/* no more */
    305 
    306 		/* Transfer key/value pair. */
    307 		status = xdr_ypresp_key_val(xdrs, &out);
    308 
    309 		/*
    310 		 * If we succeeded, call the callback function.
    311 		 * The callback will return TRUE when it wants
    312 		 * no more values.  If we fail, indicate the
    313 		 * error.
    314 		 */
    315 		if (status) {
    316 			if ((*incallback->foreach)(out.status,
    317 			    (char *)out.keydat.dptr, out.keydat.dsize,
    318 			    (char *)out.valdat.dptr, out.valdat.dsize,
    319 			    incallback->data))
    320 				return TRUE;
    321 		} else
    322 			return FALSE;
    323 	}
    324 }
    325 
    326 bool_t
    327 xdr_ypresp_master(xdrs, objp)
    328 	XDR *xdrs;
    329 	struct ypresp_master *objp;
    330 {
    331 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    332 		return FALSE;
    333 
    334 	if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
    335 		return FALSE;
    336 
    337 	return TRUE;
    338 }
    339 
    340 static bool_t
    341 xdr_ypmaplist_str(xdrs, objp)
    342 	XDR *xdrs;
    343 	char *objp;
    344 {
    345 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
    346 }
    347 
    348 bool_t
    349 xdr_ypmaplist(xdrs, objp)
    350 	XDR *xdrs;
    351 	struct ypmaplist *objp;
    352 {
    353 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
    354 		return FALSE;
    355 
    356 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
    357 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    358 		return FALSE;
    359 
    360 	return TRUE;
    361 }
    362 
    363 bool_t
    364 xdr_ypresp_maplist(xdrs, objp)
    365 	XDR *xdrs;
    366 	struct ypresp_maplist *objp;
    367 {
    368 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    369 		return FALSE;
    370 
    371 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
    372 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    373 		return FALSE;
    374 
    375 	return TRUE;
    376 }
    377 
    378 bool_t
    379 xdr_ypresp_order(xdrs, objp)
    380 	XDR *xdrs;
    381 	struct ypresp_order *objp;
    382 {
    383 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    384 		return FALSE;
    385 
    386 	if (!xdr_u_int(xdrs, &objp->ordernum))
    387 		return FALSE;
    388 
    389 	return TRUE;
    390 }
    391 
    392 bool_t
    393 xdr_ypreq_xfr(xdrs, objp)
    394 	XDR *xdrs;
    395 	struct ypreq_xfr *objp;
    396 {
    397 	if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
    398 		return FALSE;
    399 
    400 	if (!xdr_u_int(xdrs, &objp->transid))
    401 		return FALSE;
    402 
    403 	if (!xdr_u_int(xdrs, &objp->proto))
    404 		return FALSE;
    405 
    406 	if (!xdr_u_int(xdrs, &objp->port))
    407 		return FALSE;
    408 
    409 	return TRUE;
    410 }
    411 
    412 bool_t
    413 xdr_ypmap_parms(xdrs, objp)
    414 	XDR *xdrs;
    415 	struct ypmap_parms *objp;
    416 {
    417 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)objp->domain))
    418 		return FALSE;
    419 
    420 	if (!xdr_ypmap_wrap_string(xdrs, (char **)objp->map))
    421 		return FALSE;
    422 
    423 	if (!xdr_u_int(xdrs, &objp->ordernum))
    424 		return FALSE;
    425 
    426 	if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
    427 		return FALSE;
    428 
    429 	return TRUE;
    430 }
    431 
    432 bool_t
    433 xdr_yppushresp_xfr(xdrs, objp)
    434 	XDR *xdrs;
    435 	struct yppushresp_xfr *objp;
    436 {
    437 	if (!xdr_u_int(xdrs, &objp->transid))
    438 		return FALSE;
    439 
    440 	if (!xdr_enum(xdrs, (enum_t *)&objp->status))
    441 		return FALSE;
    442 
    443 	return TRUE;
    444 }
    445