1 /* $NetBSD: xdryp.c,v 1.25 2000/01/22 22:19:22 mycroft Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Jason R. Thorpe <thorpej@NetBSD.ORG>. 5 * All rights reserved. 6 * 7 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@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.25 2000/01/22 22:19:22 mycroft 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 53 #include <sys/param.h> 54 #include <sys/socket.h> 55 56 #include <assert.h> 57 #include <ctype.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 62 #include <rpc/rpc.h> 63 #include <rpc/xdr.h> 64 #include <rpcsvc/yp_prot.h> 65 #include <rpcsvc/ypclnt.h> 66 67 #ifdef __weak_alias 68 __weak_alias(xdr_datum,_xdr_datum) 69 __weak_alias(xdr_domainname,_xdr_domainname) 70 __weak_alias(xdr_mapname,_xdr_mapname) 71 __weak_alias(xdr_peername,_xdr_peername) 72 __weak_alias(xdr_yp_inaddr,_xdr_yp_inaddr) 73 __weak_alias(xdr_ypall,_xdr_ypall) 74 __weak_alias(xdr_ypbind_resp,_xdr_ypbind_resp) 75 __weak_alias(xdr_ypbind_setdom,_xdr_ypbind_setdom) 76 __weak_alias(xdr_ypdomain_wrap_string,_xdr_ypdomain_wrap_string) 77 __weak_alias(xdr_ypmap_parms,_xdr_ypmap_parms) 78 __weak_alias(xdr_ypmap_wrap_string,_xdr_ypmap_wrap_string) 79 __weak_alias(xdr_ypmaplist,_xdr_ypmaplist) 80 __weak_alias(xdr_ypowner_wrap_string,_xdr_ypowner_wrap_string) 81 __weak_alias(xdr_yppushresp_xfr,_xdr_yppushresp_xfr) 82 __weak_alias(xdr_ypreq_key,_xdr_ypreq_key) 83 __weak_alias(xdr_ypreq_nokey,_xdr_ypreq_nokey) 84 __weak_alias(xdr_ypreq_xfr,_xdr_ypreq_xfr) 85 __weak_alias(xdr_ypresp_key_val,_xdr_ypresp_key_val) 86 __weak_alias(xdr_ypresp_maplist,_xdr_ypresp_maplist) 87 __weak_alias(xdr_ypresp_master,_xdr_ypresp_master) 88 __weak_alias(xdr_ypresp_order,_xdr_ypresp_order) 89 __weak_alias(xdr_ypresp_val,_xdr_ypresp_val) 90 #endif 91 92 /* 93 * Functions used only within this file. 94 */ 95 static bool_t xdr_ypbind_binding __P((XDR *, struct ypbind_binding *)); 96 static bool_t xdr_ypbind_resptype __P((XDR *, enum ypbind_resptype *)); 97 static bool_t xdr_ypstat __P((XDR *, enum ypbind_resptype *)); 98 static bool_t xdr_ypmaplist_str __P((XDR *, char *)); 99 100 __warn_references(xdr_domainname, 101 "warning: this program uses xdr_domainname(), which is deprecated and buggy.") 102 103 bool_t 104 xdr_domainname(xdrs, objp) 105 XDR *xdrs; 106 char *objp; 107 { 108 109 _DIAGASSERT(xdrs != NULL); 110 _DIAGASSERT(objp != NULL); 111 112 return xdr_string(xdrs, &objp, YPMAXDOMAIN); 113 } 114 115 __warn_references(xdr_peername, 116 "warning: this program uses xdr_peername(), which is deprecated and buggy.") 117 118 bool_t 119 xdr_peername(xdrs, objp) 120 XDR *xdrs; 121 char *objp; 122 { 123 124 _DIAGASSERT(xdrs != NULL); 125 _DIAGASSERT(objp != NULL); 126 127 return xdr_string(xdrs, &objp, YPMAXPEER); 128 } 129 130 __warn_references(xdr_mapname, 131 "warning: this program uses xdr_mapname(), which is deprecated and buggy.") 132 133 bool_t 134 xdr_mapname(xdrs, objp) 135 XDR *xdrs; 136 char *objp; 137 { 138 139 _DIAGASSERT(xdrs != NULL); 140 _DIAGASSERT(objp != NULL); 141 142 return xdr_string(xdrs, &objp, YPMAXMAP); 143 } 144 145 bool_t 146 xdr_ypdomain_wrap_string(xdrs, objp) 147 XDR *xdrs; 148 char **objp; 149 { 150 151 _DIAGASSERT(xdrs != NULL); 152 _DIAGASSERT(objp != NULL); 153 154 return xdr_string(xdrs, objp, YPMAXDOMAIN); 155 } 156 157 bool_t 158 xdr_ypmap_wrap_string(xdrs, objp) 159 XDR *xdrs; 160 char **objp; 161 { 162 163 _DIAGASSERT(xdrs != NULL); 164 _DIAGASSERT(objp != NULL); 165 166 return xdr_string(xdrs, objp, YPMAXMAP); 167 } 168 169 bool_t 170 xdr_ypowner_wrap_string(xdrs, objp) 171 XDR *xdrs; 172 char **objp; 173 { 174 175 _DIAGASSERT(xdrs != NULL); 176 _DIAGASSERT(objp != NULL); 177 178 return xdr_string(xdrs, objp, YPMAXPEER); 179 } 180 181 bool_t 182 xdr_datum(xdrs, objp) 183 XDR *xdrs; 184 datum *objp; 185 { 186 187 _DIAGASSERT(xdrs != NULL); 188 _DIAGASSERT(objp != NULL); 189 190 return xdr_bytes(xdrs, (char **)&objp->dptr, 191 (u_int *)&objp->dsize, YPMAXRECORD); 192 } 193 194 bool_t 195 xdr_ypreq_key(xdrs, objp) 196 XDR *xdrs; 197 struct ypreq_key *objp; 198 { 199 200 _DIAGASSERT(xdrs != NULL); 201 _DIAGASSERT(objp != NULL); 202 203 if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain)) 204 return FALSE; 205 206 if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map)) 207 return FALSE; 208 209 if (!xdr_datum(xdrs, &objp->keydat)) 210 return FALSE; 211 212 return TRUE; 213 } 214 215 bool_t 216 xdr_ypreq_nokey(xdrs, objp) 217 XDR *xdrs; 218 struct ypreq_nokey *objp; 219 { 220 221 _DIAGASSERT(xdrs != NULL); 222 _DIAGASSERT(objp != NULL); 223 224 if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain)) 225 return FALSE; 226 227 if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map)) 228 return FALSE; 229 230 return TRUE; 231 } 232 233 bool_t 234 xdr_yp_inaddr(xdrs, objp) 235 XDR *xdrs; 236 struct in_addr *objp; 237 { 238 239 _DIAGASSERT(xdrs != NULL); 240 _DIAGASSERT(objp != NULL); 241 242 return xdr_opaque(xdrs, (caddr_t)(void *)&objp->s_addr, 243 sizeof objp->s_addr); 244 } 245 246 static bool_t 247 xdr_ypbind_binding(xdrs, objp) 248 XDR *xdrs; 249 struct ypbind_binding *objp; 250 { 251 252 _DIAGASSERT(xdrs != NULL); 253 _DIAGASSERT(objp != NULL); 254 255 if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr)) 256 return FALSE; 257 258 if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port, 259 sizeof objp->ypbind_binding_port)) 260 return FALSE; 261 262 return TRUE; 263 } 264 265 static bool_t 266 xdr_ypbind_resptype(xdrs, objp) 267 XDR *xdrs; 268 enum ypbind_resptype *objp; 269 { 270 271 _DIAGASSERT(xdrs != NULL); 272 _DIAGASSERT(objp != NULL); 273 274 return xdr_enum(xdrs, (enum_t *)objp); 275 } 276 277 static bool_t 278 xdr_ypstat(xdrs, objp) 279 XDR *xdrs; 280 enum ypbind_resptype *objp; 281 { 282 283 _DIAGASSERT(xdrs != NULL); 284 _DIAGASSERT(objp != NULL); 285 286 return xdr_enum(xdrs, (enum_t *)objp); 287 } 288 289 bool_t 290 xdr_ypbind_resp(xdrs, objp) 291 XDR *xdrs; 292 struct ypbind_resp *objp; 293 { 294 295 _DIAGASSERT(xdrs != NULL); 296 _DIAGASSERT(objp != NULL); 297 298 if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status)) 299 return FALSE; 300 301 switch (objp->ypbind_status) { 302 case YPBIND_FAIL_VAL: 303 return xdr_u_int(xdrs, 304 (u_int *)&objp->ypbind_respbody.ypbind_error); 305 306 case YPBIND_SUCC_VAL: 307 return xdr_ypbind_binding(xdrs, 308 &objp->ypbind_respbody.ypbind_bindinfo); 309 310 default: 311 return FALSE; 312 } 313 /* NOTREACHED */ 314 } 315 316 bool_t 317 xdr_ypresp_val(xdrs, objp) 318 XDR *xdrs; 319 struct ypresp_val *objp; 320 { 321 322 _DIAGASSERT(xdrs != NULL); 323 _DIAGASSERT(objp != NULL); 324 325 if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) 326 return FALSE; 327 328 if (!xdr_datum(xdrs, &objp->valdat)) 329 return FALSE; 330 331 return TRUE; 332 } 333 334 bool_t 335 xdr_ypbind_setdom(xdrs, objp) 336 XDR *xdrs; 337 struct ypbind_setdom *objp; 338 { 339 char *cp; 340 341 _DIAGASSERT(xdrs != NULL); 342 _DIAGASSERT(objp != NULL); 343 344 cp = objp->ypsetdom_domain; 345 346 if (!xdr_ypdomain_wrap_string(xdrs, &cp)) 347 return FALSE; 348 349 if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding)) 350 return FALSE; 351 352 if (!xdr_u_int(xdrs, &objp->ypsetdom_vers)) 353 return FALSE; 354 355 return TRUE; 356 } 357 358 bool_t 359 xdr_ypresp_key_val(xdrs, objp) 360 XDR *xdrs; 361 struct ypresp_key_val *objp; 362 { 363 364 _DIAGASSERT(xdrs != NULL); 365 _DIAGASSERT(objp != NULL); 366 367 if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) 368 return FALSE; 369 370 if (!xdr_datum(xdrs, &objp->valdat)) 371 return FALSE; 372 373 if (!xdr_datum(xdrs, &objp->keydat)) 374 return FALSE; 375 376 return TRUE; 377 } 378 379 bool_t 380 xdr_ypall(xdrs, incallback) 381 XDR *xdrs; 382 struct ypall_callback *incallback; 383 { 384 struct ypresp_key_val out; 385 char key[YPMAXRECORD], val[YPMAXRECORD]; 386 bool_t more, status; 387 388 _DIAGASSERT(xdrs != NULL); 389 _DIAGASSERT(incallback != NULL); 390 391 /* 392 * Set up key/val struct to be used during the transaction. 393 */ 394 memset(&out, 0, sizeof out); 395 out.keydat.dptr = key; 396 out.keydat.dsize = sizeof(key); 397 out.valdat.dptr = val; 398 out.valdat.dsize = sizeof(val); 399 400 for (;;) { 401 /* Values pending? */ 402 if (!xdr_bool(xdrs, &more)) 403 return FALSE; /* can't tell! */ 404 if (!more) 405 return TRUE; /* no more */ 406 407 /* Transfer key/value pair. */ 408 status = xdr_ypresp_key_val(xdrs, &out); 409 410 /* 411 * If we succeeded, call the callback function. 412 * The callback will return TRUE when it wants 413 * no more values. If we fail, indicate the 414 * error. 415 */ 416 if (status) { 417 /* LINTED const dropouts */ 418 if ((*incallback->foreach)((int)out.status, 419 (char *)out.keydat.dptr, out.keydat.dsize, 420 (char *)out.valdat.dptr, out.valdat.dsize, 421 incallback->data)) 422 return TRUE; 423 } else 424 return FALSE; 425 } 426 } 427 428 bool_t 429 xdr_ypresp_master(xdrs, objp) 430 XDR *xdrs; 431 struct ypresp_master *objp; 432 { 433 434 _DIAGASSERT(xdrs != NULL); 435 _DIAGASSERT(objp != NULL); 436 437 if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) 438 return FALSE; 439 440 if (!xdr_string(xdrs, &objp->master, YPMAXPEER)) 441 return FALSE; 442 443 return TRUE; 444 } 445 446 static bool_t 447 xdr_ypmaplist_str(xdrs, objp) 448 XDR *xdrs; 449 char *objp; 450 { 451 452 _DIAGASSERT(xdrs != NULL); 453 _DIAGASSERT(objp != NULL); 454 455 return xdr_string(xdrs, &objp, YPMAXMAP+1); 456 } 457 458 bool_t 459 xdr_ypmaplist(xdrs, objp) 460 XDR *xdrs; 461 struct ypmaplist *objp; 462 { 463 464 _DIAGASSERT(xdrs != NULL); 465 _DIAGASSERT(objp != NULL); 466 467 if (!xdr_ypmaplist_str(xdrs, objp->ypml_name)) 468 return FALSE; 469 470 if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next, 471 sizeof(struct ypmaplist), (xdrproc_t)xdr_ypmaplist)) 472 return FALSE; 473 474 return TRUE; 475 } 476 477 bool_t 478 xdr_ypresp_maplist(xdrs, objp) 479 XDR *xdrs; 480 struct ypresp_maplist *objp; 481 { 482 483 _DIAGASSERT(xdrs != NULL); 484 _DIAGASSERT(objp != NULL); 485 486 if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) 487 return FALSE; 488 489 if (!xdr_pointer(xdrs, (caddr_t *)&objp->list, 490 sizeof(struct ypmaplist), (xdrproc_t)xdr_ypmaplist)) 491 return FALSE; 492 493 return TRUE; 494 } 495 496 bool_t 497 xdr_ypresp_order(xdrs, objp) 498 XDR *xdrs; 499 struct ypresp_order *objp; 500 { 501 502 _DIAGASSERT(xdrs != NULL); 503 _DIAGASSERT(objp != NULL); 504 505 if (!xdr_ypstat(xdrs, (enum ypbind_resptype *)&objp->status)) 506 return FALSE; 507 508 if (!xdr_u_int(xdrs, &objp->ordernum)) 509 return FALSE; 510 511 return TRUE; 512 } 513 514 bool_t 515 xdr_ypreq_xfr(xdrs, objp) 516 XDR *xdrs; 517 struct ypreq_xfr *objp; 518 { 519 520 _DIAGASSERT(xdrs != NULL); 521 _DIAGASSERT(objp != NULL); 522 523 if (!xdr_ypmap_parms(xdrs, &objp->map_parms)) 524 return FALSE; 525 526 if (!xdr_u_int(xdrs, &objp->transid)) 527 return FALSE; 528 529 if (!xdr_u_int(xdrs, &objp->proto)) 530 return FALSE; 531 532 if (!xdr_u_int(xdrs, &objp->port)) 533 return FALSE; 534 535 return TRUE; 536 } 537 538 bool_t 539 xdr_ypmap_parms(xdrs, objp) 540 XDR *xdrs; 541 struct ypmap_parms *objp; 542 { 543 544 _DIAGASSERT(xdrs != NULL); 545 _DIAGASSERT(objp != NULL); 546 547 if (!xdr_ypdomain_wrap_string(xdrs, (char **)&objp->domain)) 548 return FALSE; 549 550 if (!xdr_ypmap_wrap_string(xdrs, (char **)&objp->map)) 551 return FALSE; 552 553 if (!xdr_u_int(xdrs, &objp->ordernum)) 554 return FALSE; 555 556 if (!xdr_ypowner_wrap_string(xdrs, &objp->owner)) 557 return FALSE; 558 559 return TRUE; 560 } 561 562 bool_t 563 xdr_yppushresp_xfr(xdrs, objp) 564 XDR *xdrs; 565 struct yppushresp_xfr *objp; 566 { 567 568 _DIAGASSERT(xdrs != NULL); 569 _DIAGASSERT(objp != NULL); 570 571 if (!xdr_u_int(xdrs, &objp->transid)) 572 return FALSE; 573 574 if (!xdr_enum(xdrs, (enum_t *)&objp->status)) 575 return FALSE; 576 577 return TRUE; 578 } 579