Home | History | Annotate | Line # | Download | only in yp
xdryp.c revision 1.21
      1 /*	$NetBSD: xdryp.c,v 1.21 1998/11/15 17:10:30 christos 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 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 __RCSID("$NetBSD: xdryp.c,v 1.21 1998/11/15 17:10:30 christos Exp $");
     42 #endif
     43 
     44 /*
     45  * XDR routines used by the YP protocol.  Note that these routines do
     46  * not strictly conform to the RPC definition in yp.x.  This file
     47  * replicates the functions exported by the Sun YP API; reality is
     48  * often inaccurate.
     49  */
     50 
     51 #include "namespace.h"
     52 #include <sys/param.h>
     53 #include <sys/types.h>
     54 #include <sys/socket.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <ctype.h>
     59 #include <rpc/rpc.h>
     60 #include <rpc/xdr.h>
     61 #include <rpcsvc/yp_prot.h>
     62 #include <rpcsvc/ypclnt.h>
     63 
     64 #ifdef __weak_alias
     65 __weak_alias(xdr_datum,_xdr_datum);
     66 __weak_alias(xdr_domainname,_xdr_domainname);
     67 __weak_alias(xdr_mapname,_xdr_mapname);
     68 __weak_alias(xdr_peername,_xdr_peername);
     69 __weak_alias(xdr_yp_inaddr,_xdr_yp_inaddr);
     70 __weak_alias(xdr_ypall,_xdr_ypall);
     71 __weak_alias(xdr_ypbind_resp,_xdr_ypbind_resp);
     72 __weak_alias(xdr_ypbind_setdom,_xdr_ypbind_setdom);
     73 __weak_alias(xdr_ypdomain_wrap_string,_xdr_ypdomain_wrap_string);
     74 __weak_alias(xdr_ypmap_parms,_xdr_ypmap_parms);
     75 __weak_alias(xdr_ypmap_wrap_string,_xdr_ypmap_wrap_string);
     76 __weak_alias(xdr_ypmaplist,_xdr_ypmaplist);
     77 __weak_alias(xdr_ypowner_wrap_string,_xdr_ypowner_wrap_string);
     78 __weak_alias(xdr_yppushresp_xfr,_xdr_yppushresp_xfr);
     79 __weak_alias(xdr_ypreq_key,_xdr_ypreq_key);
     80 __weak_alias(xdr_ypreq_nokey,_xdr_ypreq_nokey);
     81 __weak_alias(xdr_ypreq_xfr,_xdr_ypreq_xfr);
     82 __weak_alias(xdr_ypresp_key_val,_xdr_ypresp_key_val);
     83 __weak_alias(xdr_ypresp_maplist,_xdr_ypresp_maplist);
     84 __weak_alias(xdr_ypresp_master,_xdr_ypresp_master);
     85 __weak_alias(xdr_ypresp_order,_xdr_ypresp_order);
     86 __weak_alias(xdr_ypresp_val,_xdr_ypresp_val);
     87 #endif
     88 
     89 /*
     90  * Functions used only within this file.
     91  */
     92 static	bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *));
     93 static	bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *));
     94 static	bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *));
     95 static	bool_t xdr_ypmaplist_str __P((XDR *, char *));
     96 
     97 __warn_references(xdr_domainname,
     98     "warning: this program uses xdr_domainname(), which is deprecated and buggy.")
     99 
    100 bool_t
    101 xdr_domainname(xdrs, objp)
    102 	XDR *xdrs;
    103 	char *objp;
    104 {
    105 	return xdr_string(xdrs, &objp, YPMAXDOMAIN);
    106 }
    107 
    108 __warn_references(xdr_peername,
    109     "warning: this program uses xdr_peername(), which is deprecated and buggy.")
    110 
    111 bool_t
    112 xdr_peername(xdrs, objp)
    113 	XDR *xdrs;
    114 	char *objp;
    115 {
    116 	return xdr_string(xdrs, &objp, YPMAXPEER);
    117 }
    118 
    119 __warn_references(xdr_mapname,
    120     "warning: this program uses xdr_mapname(), which is deprecated and buggy.")
    121 
    122 bool_t
    123 xdr_mapname(xdrs, objp)
    124 	XDR *xdrs;
    125 	char *objp;
    126 {
    127 	return xdr_string(xdrs, &objp, YPMAXMAP);
    128 }
    129 
    130 bool_t
    131 xdr_ypdomain_wrap_string(xdrs, objp)
    132 	XDR *xdrs;
    133 	char **objp;
    134 {
    135 	return xdr_string(xdrs, objp, YPMAXDOMAIN);
    136 }
    137 
    138 bool_t
    139 xdr_ypmap_wrap_string(xdrs, objp)
    140 	XDR *xdrs;
    141 	char **objp;
    142 {
    143 	return xdr_string(xdrs, objp, YPMAXMAP);
    144 }
    145 
    146 bool_t
    147 xdr_ypowner_wrap_string(xdrs, objp)
    148 	XDR *xdrs;
    149 	char **objp;
    150 {
    151 	return xdr_string(xdrs, objp, YPMAXPEER);
    152 }
    153 
    154 bool_t
    155 xdr_datum(xdrs, objp)
    156 	XDR *xdrs;
    157 	datum *objp;
    158 {
    159 	return xdr_bytes(xdrs, (char **)&objp->dptr,
    160 	    (u_int *)&objp->dsize, YPMAXRECORD);
    161 }
    162 
    163 bool_t
    164 xdr_ypreq_key(xdrs, objp)
    165 	XDR *xdrs;
    166 	struct ypreq_key *objp;
    167 {
    168 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    169 		return FALSE;
    170 
    171 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    172 		return FALSE;
    173 
    174 	if (!xdr_datum(xdrs, &objp->keydat))
    175 		return FALSE;
    176 
    177 	return TRUE;
    178 }
    179 
    180 bool_t
    181 xdr_ypreq_nokey(xdrs, objp)
    182 	XDR *xdrs;
    183 	struct ypreq_nokey *objp;
    184 {
    185 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    186 		return FALSE;
    187 
    188 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    189 		return FALSE;
    190 
    191 	return TRUE;
    192 }
    193 
    194 bool_t
    195 xdr_yp_inaddr(xdrs, objp)
    196 	XDR *xdrs;
    197 	struct in_addr *objp;
    198 {
    199 	return xdr_opaque(xdrs, (caddr_t)(void *)&objp->s_addr,
    200 	    sizeof objp->s_addr);
    201 }
    202 
    203 static bool_t
    204 xdr_ypbind_binding(xdrs, objp)
    205 	XDR *xdrs;
    206 	struct ypbind_binding *objp;
    207 {
    208 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
    209 		return FALSE;
    210 
    211 	if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
    212 	    sizeof objp->ypbind_binding_port))
    213 		return FALSE;
    214 
    215 	return TRUE;
    216 }
    217 
    218 static bool_t
    219 xdr_ypbind_resptype(xdrs, objp)
    220 	XDR *xdrs;
    221 	enum ypbind_resptype *objp;
    222 {
    223 	return xdr_enum(xdrs, (enum_t *)objp);
    224 }
    225 
    226 static bool_t
    227 xdr_ypstat(xdrs, objp)
    228 	XDR *xdrs;
    229 	enum ypbind_resptype *objp;
    230 {
    231 	return xdr_enum(xdrs, (enum_t *)objp);
    232 }
    233 
    234 bool_t
    235 xdr_ypbind_resp(xdrs, objp)
    236 	XDR *xdrs;
    237 	struct ypbind_resp *objp;
    238 {
    239 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
    240 		return FALSE;
    241 
    242 	switch (objp->ypbind_status) {
    243 	case YPBIND_FAIL_VAL:
    244 		return xdr_u_int(xdrs,
    245 		    (u_int *)&objp->ypbind_respbody.ypbind_error);
    246 
    247 	case YPBIND_SUCC_VAL:
    248 		return xdr_ypbind_binding(xdrs,
    249 		    &objp->ypbind_respbody.ypbind_bindinfo);
    250 
    251 	default:
    252 		return FALSE;
    253 	}
    254 	/* NOTREACHED */
    255 }
    256 
    257 bool_t
    258 xdr_ypresp_val(xdrs, objp)
    259 	XDR *xdrs;
    260 	struct ypresp_val *objp;
    261 {
    262 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    263 		return FALSE;
    264 
    265 	if (!xdr_datum(xdrs, &objp->valdat))
    266 		return FALSE;
    267 
    268 	return TRUE;
    269 }
    270 
    271 bool_t
    272 xdr_ypbind_setdom(xdrs, objp)
    273 	XDR *xdrs;
    274 	struct ypbind_setdom *objp;
    275 {
    276 	char *cp = objp->ypsetdom_domain;
    277 
    278 	if (!xdr_ypdomain_wrap_string(xdrs, &cp))
    279 		return FALSE;
    280 
    281 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
    282 		return FALSE;
    283 
    284 	if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
    285 		return FALSE;
    286 
    287 	return TRUE;
    288 }
    289 
    290 bool_t
    291 xdr_ypresp_key_val(xdrs, objp)
    292 	XDR *xdrs;
    293 	struct ypresp_key_val *objp;
    294 {
    295 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    296 		return FALSE;
    297 
    298 	if (!xdr_datum(xdrs, &objp->valdat))
    299 		return FALSE;
    300 
    301 	if (!xdr_datum(xdrs, &objp->keydat))
    302 		return FALSE;
    303 
    304 	return TRUE;
    305 }
    306 
    307 bool_t
    308 xdr_ypall(xdrs, incallback)
    309 	XDR *xdrs;
    310 	struct ypall_callback *incallback;
    311 {
    312 	struct ypresp_key_val out;
    313 	char key[YPMAXRECORD], val[YPMAXRECORD];
    314 	bool_t more, status;
    315 
    316 	/*
    317 	 * Set up key/val struct to be used during the transaction.
    318 	 */
    319 	memset(&out, 0, sizeof out);
    320 	out.keydat.dptr = key;
    321 	out.keydat.dsize = sizeof(key);
    322 	out.valdat.dptr = val;
    323 	out.valdat.dsize = sizeof(val);
    324 
    325 	for (;;) {
    326 		/* Values pending? */
    327 		if (!xdr_bool(xdrs, &more))
    328 			return FALSE;		/* can't tell! */
    329 		if (!more)
    330 			return TRUE;		/* no more */
    331 
    332 		/* Transfer key/value pair. */
    333 		status = xdr_ypresp_key_val(xdrs, &out);
    334 
    335 		/*
    336 		 * If we succeeded, call the callback function.
    337 		 * The callback will return TRUE when it wants
    338 		 * no more values.  If we fail, indicate the
    339 		 * error.
    340 		 */
    341 		if (status) {
    342 			/* LINTED const dropouts */
    343 			if ((*incallback->foreach)((int)out.status,
    344 			    (char *)out.keydat.dptr, out.keydat.dsize,
    345 			    (char *)out.valdat.dptr, out.valdat.dsize,
    346 			    incallback->data))
    347 				return TRUE;
    348 		} else
    349 			return FALSE;
    350 	}
    351 }
    352 
    353 bool_t
    354 xdr_ypresp_master(xdrs, objp)
    355 	XDR *xdrs;
    356 	struct ypresp_master *objp;
    357 {
    358 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    359 		return FALSE;
    360 
    361 	if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
    362 		return FALSE;
    363 
    364 	return TRUE;
    365 }
    366 
    367 static bool_t
    368 xdr_ypmaplist_str(xdrs, objp)
    369 	XDR *xdrs;
    370 	char *objp;
    371 {
    372 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
    373 }
    374 
    375 bool_t
    376 xdr_ypmaplist(xdrs, objp)
    377 	XDR *xdrs;
    378 	struct ypmaplist *objp;
    379 {
    380 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
    381 		return FALSE;
    382 
    383 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
    384 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    385 		return FALSE;
    386 
    387 	return TRUE;
    388 }
    389 
    390 bool_t
    391 xdr_ypresp_maplist(xdrs, objp)
    392 	XDR *xdrs;
    393 	struct ypresp_maplist *objp;
    394 {
    395 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    396 		return FALSE;
    397 
    398 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
    399 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    400 		return FALSE;
    401 
    402 	return TRUE;
    403 }
    404 
    405 bool_t
    406 xdr_ypresp_order(xdrs, objp)
    407 	XDR *xdrs;
    408 	struct ypresp_order *objp;
    409 {
    410 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    411 		return FALSE;
    412 
    413 	if (!xdr_u_int(xdrs, &objp->ordernum))
    414 		return FALSE;
    415 
    416 	return TRUE;
    417 }
    418 
    419 bool_t
    420 xdr_ypreq_xfr(xdrs, objp)
    421 	XDR *xdrs;
    422 	struct ypreq_xfr *objp;
    423 {
    424 	if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
    425 		return FALSE;
    426 
    427 	if (!xdr_u_int(xdrs, &objp->transid))
    428 		return FALSE;
    429 
    430 	if (!xdr_u_int(xdrs, &objp->proto))
    431 		return FALSE;
    432 
    433 	if (!xdr_u_int(xdrs, &objp->port))
    434 		return FALSE;
    435 
    436 	return TRUE;
    437 }
    438 
    439 bool_t
    440 xdr_ypmap_parms(xdrs, objp)
    441 	XDR *xdrs;
    442 	struct ypmap_parms *objp;
    443 {
    444 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    445 		return FALSE;
    446 
    447 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    448 		return FALSE;
    449 
    450 	if (!xdr_u_int(xdrs, &objp->ordernum))
    451 		return FALSE;
    452 
    453 	if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
    454 		return FALSE;
    455 
    456 	return TRUE;
    457 }
    458 
    459 bool_t
    460 xdr_yppushresp_xfr(xdrs, objp)
    461 	XDR *xdrs;
    462 	struct yppushresp_xfr *objp;
    463 {
    464 	if (!xdr_u_int(xdrs, &objp->transid))
    465 		return FALSE;
    466 
    467 	if (!xdr_enum(xdrs, (enum_t *)&objp->status))
    468 		return FALSE;
    469 
    470 	return TRUE;
    471 }
    472