Home | History | Annotate | Line # | Download | only in yp
xdryp.c revision 1.20
      1 /*	$NetBSD: xdryp.c,v 1.20 1997/11/04 23:53:10 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 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 __RCSID("$NetBSD: xdryp.c,v 1.20 1997/11/04 23:53:10 thorpej 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)&objp->s_addr, sizeof objp->s_addr);
    200 }
    201 
    202 static bool_t
    203 xdr_ypbind_binding(xdrs, objp)
    204 	XDR *xdrs;
    205 	struct ypbind_binding *objp;
    206 {
    207 	if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr))
    208 		return FALSE;
    209 
    210 	if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port,
    211 	    sizeof objp->ypbind_binding_port))
    212 		return FALSE;
    213 
    214 	return TRUE;
    215 }
    216 
    217 static bool_t
    218 xdr_ypbind_resptype(xdrs, objp)
    219 	XDR *xdrs;
    220 	enum ypbind_resptype *objp;
    221 {
    222 	return xdr_enum(xdrs, (enum_t *)objp);
    223 }
    224 
    225 static bool_t
    226 xdr_ypstat(xdrs, objp)
    227 	XDR *xdrs;
    228 	enum ypbind_resptype *objp;
    229 {
    230 	return xdr_enum(xdrs, (enum_t *)objp);
    231 }
    232 
    233 bool_t
    234 xdr_ypbind_resp(xdrs, objp)
    235 	XDR *xdrs;
    236 	struct ypbind_resp *objp;
    237 {
    238 	if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status))
    239 		return FALSE;
    240 
    241 	switch (objp->ypbind_status) {
    242 	case YPBIND_FAIL_VAL:
    243 		return xdr_u_int(xdrs,
    244 		    (u_int *)&objp->ypbind_respbody.ypbind_error);
    245 
    246 	case YPBIND_SUCC_VAL:
    247 		return xdr_ypbind_binding(xdrs,
    248 		    &objp->ypbind_respbody.ypbind_bindinfo);
    249 
    250 	default:
    251 		return FALSE;
    252 	}
    253 	/* NOTREACHED */
    254 }
    255 
    256 bool_t
    257 xdr_ypresp_val(xdrs, objp)
    258 	XDR *xdrs;
    259 	struct ypresp_val *objp;
    260 {
    261 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    262 		return FALSE;
    263 
    264 	if (!xdr_datum(xdrs, &objp->valdat))
    265 		return FALSE;
    266 
    267 	return TRUE;
    268 }
    269 
    270 bool_t
    271 xdr_ypbind_setdom(xdrs, objp)
    272 	XDR *xdrs;
    273 	struct ypbind_setdom *objp;
    274 {
    275 	char *cp = objp->ypsetdom_domain;
    276 
    277 	if (!xdr_ypdomain_wrap_string(xdrs, &cp))
    278 		return FALSE;
    279 
    280 	if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding))
    281 		return FALSE;
    282 
    283 	if (!xdr_u_int(xdrs, &objp->ypsetdom_vers))
    284 		return FALSE;
    285 
    286 	return TRUE;
    287 }
    288 
    289 bool_t
    290 xdr_ypresp_key_val(xdrs, objp)
    291 	XDR *xdrs;
    292 	struct ypresp_key_val *objp;
    293 {
    294 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    295 		return FALSE;
    296 
    297 	if (!xdr_datum(xdrs, &objp->valdat))
    298 		return FALSE;
    299 
    300 	if (!xdr_datum(xdrs, &objp->keydat))
    301 		return FALSE;
    302 
    303 	return TRUE;
    304 }
    305 
    306 bool_t
    307 xdr_ypall(xdrs, incallback)
    308 	XDR *xdrs;
    309 	struct ypall_callback *incallback;
    310 {
    311 	struct ypresp_key_val out;
    312 	char key[YPMAXRECORD], val[YPMAXRECORD];
    313 	bool_t more, status;
    314 
    315 	/*
    316 	 * Set up key/val struct to be used during the transaction.
    317 	 */
    318 	memset(&out, 0, sizeof out);
    319 	out.keydat.dptr = key;
    320 	out.keydat.dsize = sizeof(key);
    321 	out.valdat.dptr = val;
    322 	out.valdat.dsize = sizeof(val);
    323 
    324 	for (;;) {
    325 		/* Values pending? */
    326 		if (!xdr_bool(xdrs, &more))
    327 			return FALSE;		/* can't tell! */
    328 		if (!more)
    329 			return TRUE;		/* no more */
    330 
    331 		/* Transfer key/value pair. */
    332 		status = xdr_ypresp_key_val(xdrs, &out);
    333 
    334 		/*
    335 		 * If we succeeded, call the callback function.
    336 		 * The callback will return TRUE when it wants
    337 		 * no more values.  If we fail, indicate the
    338 		 * error.
    339 		 */
    340 		if (status) {
    341 			if ((*incallback->foreach)(out.status,
    342 			    (char *)out.keydat.dptr, out.keydat.dsize,
    343 			    (char *)out.valdat.dptr, out.valdat.dsize,
    344 			    incallback->data))
    345 				return TRUE;
    346 		} else
    347 			return FALSE;
    348 	}
    349 }
    350 
    351 bool_t
    352 xdr_ypresp_master(xdrs, objp)
    353 	XDR *xdrs;
    354 	struct ypresp_master *objp;
    355 {
    356 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    357 		return FALSE;
    358 
    359 	if (!xdr_string(xdrs, &objp->master, YPMAXPEER))
    360 		return FALSE;
    361 
    362 	return TRUE;
    363 }
    364 
    365 static bool_t
    366 xdr_ypmaplist_str(xdrs, objp)
    367 	XDR *xdrs;
    368 	char *objp;
    369 {
    370 	return xdr_string(xdrs, &objp, YPMAXMAP+1);
    371 }
    372 
    373 bool_t
    374 xdr_ypmaplist(xdrs, objp)
    375 	XDR *xdrs;
    376 	struct ypmaplist *objp;
    377 {
    378 	if (!xdr_ypmaplist_str(xdrs, objp->ypml_name))
    379 		return FALSE;
    380 
    381 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next,
    382 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    383 		return FALSE;
    384 
    385 	return TRUE;
    386 }
    387 
    388 bool_t
    389 xdr_ypresp_maplist(xdrs, objp)
    390 	XDR *xdrs;
    391 	struct ypresp_maplist *objp;
    392 {
    393 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    394 		return FALSE;
    395 
    396 	if (!xdr_pointer(xdrs, (caddr_t *)&objp->list,
    397 	    sizeof(struct ypmaplist), xdr_ypmaplist))
    398 		return FALSE;
    399 
    400 	return TRUE;
    401 }
    402 
    403 bool_t
    404 xdr_ypresp_order(xdrs, objp)
    405 	XDR *xdrs;
    406 	struct ypresp_order *objp;
    407 {
    408 	if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status))
    409 		return FALSE;
    410 
    411 	if (!xdr_u_int(xdrs, &objp->ordernum))
    412 		return FALSE;
    413 
    414 	return TRUE;
    415 }
    416 
    417 bool_t
    418 xdr_ypreq_xfr(xdrs, objp)
    419 	XDR *xdrs;
    420 	struct ypreq_xfr *objp;
    421 {
    422 	if (!xdr_ypmap_parms(xdrs, &objp->map_parms))
    423 		return FALSE;
    424 
    425 	if (!xdr_u_int(xdrs, &objp->transid))
    426 		return FALSE;
    427 
    428 	if (!xdr_u_int(xdrs, &objp->proto))
    429 		return FALSE;
    430 
    431 	if (!xdr_u_int(xdrs, &objp->port))
    432 		return FALSE;
    433 
    434 	return TRUE;
    435 }
    436 
    437 bool_t
    438 xdr_ypmap_parms(xdrs, objp)
    439 	XDR *xdrs;
    440 	struct ypmap_parms *objp;
    441 {
    442 	if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain))
    443 		return FALSE;
    444 
    445 	if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map))
    446 		return FALSE;
    447 
    448 	if (!xdr_u_int(xdrs, &objp->ordernum))
    449 		return FALSE;
    450 
    451 	if (!xdr_ypowner_wrap_string(xdrs, &objp->owner))
    452 		return FALSE;
    453 
    454 	return TRUE;
    455 }
    456 
    457 bool_t
    458 xdr_yppushresp_xfr(xdrs, objp)
    459 	XDR *xdrs;
    460 	struct yppushresp_xfr *objp;
    461 {
    462 	if (!xdr_u_int(xdrs, &objp->transid))
    463 		return FALSE;
    464 
    465 	if (!xdr_enum(xdrs, (enum_t *)&objp->status))
    466 		return FALSE;
    467 
    468 	return TRUE;
    469 }
    470