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