1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef LINT 31 static char *rcsid = "$Id: xdryp.c,v 1.2 1995/04/02 01:02:17 wpaul Exp $"; 32 #endif 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <stdlib.h> 40 #include <ctype.h> 41 #include <rpc/rpc.h> 42 #include <rpc/xdr.h> 43 #include <rpcsvc/yp_prot.h> 44 #include <rpcsvc/ypclnt.h> 45 46 extern int (*ypresp_allfn)(); 47 extern void *ypresp_data; 48 49 struct ypresp_all { 50 bool_t more; 51 union { 52 struct ypresp_key_val val; 53 } ypresp_all_u; 54 }; 55 56 enum ypxfrstat { 57 YPXFR_SUCC = 1, 58 YPXFR_AGE = 2, 59 YPXFR_NOMAP = -1, 60 YPXFR_NODOM = -2, 61 YPXFR_RSRC = -3, 62 YPXFR_RPC = -4, 63 YPXFR_MADDR = -5, 64 YPXFR_YPERR = -6, 65 YPXFR_BADARGS = -7, 66 YPXFR_DBM = -8, 67 YPXFR_FILE = -9, 68 YPXFR_SKEW = -10, 69 YPXFR_CLEAR = -11, 70 YPXFR_FORCE = -12, 71 YPXFR_XFRERR = -13, 72 YPXFR_REFUSED = -14, 73 }; 74 75 struct ypresp_xfr { 76 u_int transid; 77 enum ypxfrstat xfrstat; 78 }; 79 80 bool_t 81 xdr_domainname(xdrs, objp) 82 XDR *xdrs; 83 char *objp; 84 { 85 if (!xdr_string(xdrs, &objp, YPMAXDOMAIN)) { 86 return (FALSE); 87 } 88 return (TRUE); 89 } 90 91 bool_t 92 xdr_peername(xdrs, objp) 93 XDR *xdrs; 94 char *objp; 95 { 96 if (!xdr_string(xdrs, &objp, YPMAXPEER)) { 97 return (FALSE); 98 } 99 return (TRUE); 100 } 101 102 bool_t 103 xdr_datum(xdrs, objp) 104 XDR *xdrs; 105 datum *objp; 106 { 107 if (!xdr_bytes(xdrs, (char **)&objp->dptr, (u_int *)&objp->dsize, YPMAXRECORD)) { 108 return (FALSE); 109 } 110 return (TRUE); 111 } 112 113 bool_t 114 xdr_mapname(xdrs, objp) 115 XDR *xdrs; 116 char *objp; 117 { 118 if (!xdr_string(xdrs, &objp, YPMAXMAP)) { 119 return (FALSE); 120 } 121 return (TRUE); 122 } 123 124 bool_t 125 xdr_ypreq_key(xdrs, objp) 126 XDR *xdrs; 127 struct ypreq_key *objp; 128 { 129 if (!xdr_domainname(xdrs, objp->domain)) { 130 return (FALSE); 131 } 132 if (!xdr_mapname(xdrs, objp->map)) { 133 return (FALSE); 134 } 135 if (!xdr_datum(xdrs, &objp->keydat)) { 136 return (FALSE); 137 } 138 return (TRUE); 139 } 140 141 bool_t 142 xdr_ypreq_nokey(xdrs, objp) 143 XDR *xdrs; 144 struct ypreq_nokey *objp; 145 { 146 if (!xdr_domainname(xdrs, objp->domain)) { 147 return (FALSE); 148 } 149 if (!xdr_mapname(xdrs, objp->map)) { 150 return (FALSE); 151 } 152 return (TRUE); 153 } 154 155 bool_t 156 xdr_yp_inaddr(xdrs, objp) 157 XDR *xdrs; 158 struct in_addr *objp; 159 { 160 if (!xdr_opaque(xdrs, (caddr_t)&objp->s_addr, sizeof objp->s_addr)) { 161 return (FALSE); 162 } 163 return (TRUE); 164 } 165 166 bool_t 167 xdr_ypbind_binding(xdrs, objp) 168 XDR *xdrs; 169 struct ypbind_binding *objp; 170 { 171 if (!xdr_yp_inaddr(xdrs, &objp->ypbind_binding_addr)) { 172 return (FALSE); 173 } 174 if (!xdr_opaque(xdrs, (void *)&objp->ypbind_binding_port, 175 sizeof objp->ypbind_binding_port)) { 176 return (FALSE); 177 } 178 return (TRUE); 179 } 180 181 bool_t 182 xdr_ypbind_resptype(xdrs, objp) 183 XDR *xdrs; 184 enum ypbind_resptype *objp; 185 { 186 if (!xdr_enum(xdrs, (enum_t *)objp)) { 187 return (FALSE); 188 } 189 return (TRUE); 190 } 191 192 bool_t 193 xdr_ypstat(xdrs, objp) 194 XDR *xdrs; 195 enum ypbind_resptype *objp; 196 { 197 if (!xdr_enum(xdrs, (enum_t *)objp)) { 198 return (FALSE); 199 } 200 return (TRUE); 201 } 202 203 bool_t 204 xdr_ypbind_resp(xdrs, objp) 205 XDR *xdrs; 206 struct ypbind_resp *objp; 207 { 208 if (!xdr_ypbind_resptype(xdrs, &objp->ypbind_status)) { 209 return (FALSE); 210 } 211 switch (objp->ypbind_status) { 212 case YPBIND_FAIL_VAL: 213 if (!xdr_u_int(xdrs, (u_int *)&objp->ypbind_respbody.ypbind_error)) { 214 return (FALSE); 215 } 216 break; 217 case YPBIND_SUCC_VAL: 218 if (!xdr_ypbind_binding(xdrs, &objp->ypbind_respbody.ypbind_bindinfo)) { 219 return (FALSE); 220 } 221 break; 222 default: 223 return (FALSE); 224 } 225 return (TRUE); 226 } 227 228 bool_t 229 xdr_ypresp_val(xdrs, objp) 230 XDR *xdrs; 231 struct ypresp_val *objp; 232 { 233 if (!xdr_ypstat(xdrs, &objp->status)) { 234 return (FALSE); 235 } 236 if (!xdr_datum(xdrs, &objp->valdat)) { 237 return (FALSE); 238 } 239 return (TRUE); 240 } 241 242 bool_t 243 xdr_ypbind_setdom(xdrs, objp) 244 XDR *xdrs; 245 struct ypbind_setdom *objp; 246 { 247 if (!xdr_domainname(xdrs, objp->ypsetdom_domain)) { 248 return (FALSE); 249 } 250 if (!xdr_ypbind_binding(xdrs, &objp->ypsetdom_binding)) { 251 return (FALSE); 252 } 253 if (!xdr_u_short(xdrs, &objp->ypsetdom_vers)) { 254 return (FALSE); 255 } 256 return (TRUE); 257 } 258 259 bool_t 260 xdr_ypresp_key_val(xdrs, objp) 261 XDR *xdrs; 262 struct ypresp_key_val *objp; 263 { 264 if (!xdr_ypstat(xdrs, &objp->status)) { 265 return (FALSE); 266 } 267 if (!xdr_datum(xdrs, &objp->valdat)) { 268 return (FALSE); 269 } 270 if (!xdr_datum(xdrs, &objp->keydat)) { 271 return (FALSE); 272 } 273 return (TRUE); 274 } 275 276 bool_t 277 xdr_ypresp_all(xdrs, objp) 278 XDR *xdrs; 279 struct ypresp_all *objp; 280 { 281 if (!xdr_bool(xdrs, &objp->more)) { 282 return (FALSE); 283 } 284 switch (objp->more) { 285 case TRUE: 286 if (!xdr_ypresp_key_val(xdrs, &objp->ypresp_all_u.val)) { 287 return (FALSE); 288 } 289 break; 290 case FALSE: 291 break; 292 default: 293 return (FALSE); 294 } 295 return (TRUE); 296 } 297 298 bool_t 299 xdr_ypresp_all_seq(xdrs, objp) 300 XDR *xdrs; 301 u_long *objp; 302 { 303 struct ypresp_all out; 304 u_long status; 305 char *key, *val; 306 int r; 307 308 bzero(&out, sizeof out); 309 while(1) { 310 if( !xdr_ypresp_all(xdrs, &out)) { 311 xdr_free(xdr_ypresp_all, (char *)&out); 312 *objp = YP_YPERR; 313 return FALSE; 314 } 315 if(out.more == 0) { 316 xdr_free(xdr_ypresp_all, (char *)&out); 317 return FALSE; 318 } 319 status = out.ypresp_all_u.val.status; 320 switch(status) { 321 case YP_TRUE: 322 key = (char *)malloc(out.ypresp_all_u.val.keydat.dsize + 1); 323 bcopy(out.ypresp_all_u.val.keydat.dptr, key, 324 out.ypresp_all_u.val.keydat.dsize); 325 key[out.ypresp_all_u.val.keydat.dsize] = '\0'; 326 val = (char *)malloc(out.ypresp_all_u.val.valdat.dsize + 1); 327 bcopy(out.ypresp_all_u.val.valdat.dptr, val, 328 out.ypresp_all_u.val.valdat.dsize); 329 val[out.ypresp_all_u.val.valdat.dsize] = '\0'; 330 xdr_free(xdr_ypresp_all, (char *)&out); 331 332 r = (*ypresp_allfn)(status, 333 key, out.ypresp_all_u.val.keydat.dsize, 334 val, out.ypresp_all_u.val.valdat.dsize, 335 ypresp_data); 336 *objp = status; 337 free(key); 338 free(val); 339 if(r) 340 return TRUE; 341 break; 342 case YP_NOMORE: 343 xdr_free(xdr_ypresp_all, (char *)&out); 344 return TRUE; 345 default: 346 xdr_free(xdr_ypresp_all, (char *)&out); 347 *objp = status; 348 return TRUE; 349 } 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, &objp->status)) { 359 return (FALSE); 360 } 361 if (!xdr_string(xdrs, &objp->master, YPMAXPEER)) { 362 return (FALSE); 363 } 364 return (TRUE); 365 } 366 367 bool_t 368 xdr_ypmaplist_str(xdrs, objp) 369 XDR *xdrs; 370 char *objp; 371 { 372 if (!xdr_string(xdrs, &objp, YPMAXMAP+1)) { 373 return (FALSE); 374 } 375 return (TRUE); 376 } 377 378 bool_t 379 xdr_ypmaplist(xdrs, objp) 380 XDR *xdrs; 381 struct ypmaplist *objp; 382 { 383 if (!xdr_ypmaplist_str(xdrs, objp->ypml_name)) { 384 return (FALSE); 385 } 386 if (!xdr_pointer(xdrs, (caddr_t *)&objp->ypml_next, 387 sizeof(struct ypmaplist), xdr_ypmaplist)) { 388 return (FALSE); 389 } 390 return (TRUE); 391 } 392 393 bool_t 394 xdr_ypresp_maplist(xdrs, objp) 395 XDR *xdrs; 396 struct ypresp_maplist *objp; 397 { 398 if (!xdr_ypstat(xdrs, &objp->status)) { 399 return (FALSE); 400 } 401 if (!xdr_pointer(xdrs, (caddr_t *)&objp->list, 402 sizeof(struct ypmaplist), xdr_ypmaplist)) { 403 return (FALSE); 404 } 405 return (TRUE); 406 } 407 408 bool_t 409 xdr_ypresp_order(xdrs, objp) 410 XDR *xdrs; 411 struct ypresp_order *objp; 412 { 413 if (!xdr_ypstat(xdrs, &objp->status)) { 414 return (FALSE); 415 } 416 if (!xdr_u_long(xdrs, &objp->ordernum)) { 417 return (FALSE); 418 } 419 return (TRUE); 420 } 421 422 bool_t 423 xdr_ypxfrstat(xdrs, objp) 424 XDR *xdrs; 425 enum ypxfrstat *objp; 426 { 427 if (!xdr_enum(xdrs, (enum_t *)objp)) { 428 return (FALSE); 429 } 430 return (TRUE); 431 } 432 433 bool_t 434 xdr_ypresp_xfr(xdrs, objp) 435 XDR *xdrs; 436 struct ypresp_xfr *objp; 437 { 438 if (!xdr_u_int(xdrs, &objp->transid)) { 439 return (FALSE); 440 } 441 if (!xdr_ypxfrstat(xdrs, &objp->xfrstat)) { 442 return (FALSE); 443 } 444 return (TRUE); 445 } 446 447 bool_t 448 xdr_ypmap_parms(xdrs, objp) 449 XDR *xdrs; 450 struct ypmap_parms *objp; 451 { 452 if (!xdr_domainname(xdrs, objp->domain)) { 453 return (FALSE); 454 } 455 if (!xdr_mapname(xdrs, objp->map)) { 456 return (FALSE); 457 } 458 if (!xdr_u_long(xdrs, &objp->ordernum)) { 459 return (FALSE); 460 } 461 if (!xdr_peername(xdrs, objp->owner)) { 462 return (FALSE); 463 } 464 } 465 466 bool_t 467 xdr_ypreq_xfr(xdrs, objp) 468 XDR *xdrs; 469 struct ypreq_xfr *objp; 470 { 471 if (!xdr_ypmap_parms(xdrs, &objp->map_parms)) { 472 return (FALSE); 473 } 474 if (!xdr_u_long(xdrs, &objp->transid)) { 475 return (FALSE); 476 } 477 if (!xdr_u_long(xdrs, &objp->proto)) { 478 return (FALSE); 479 } 480 if (!xdr_u_short(xdrs, &objp->port)) { 481 return (FALSE); 482 } 483 return (TRUE); 484 } 485 486 bool_t 487 xdr_yppush_status(xdrs, objp) 488 XDR *xdrs; 489 enum yppush_status *objp; 490 { 491 if (!xdr_enum(xdrs, (enum_t *)objp)) { 492 return (FALSE); 493 } 494 return (TRUE); 495 } 496 497 bool_t 498 xdr_yppushresp_xfr(xdrs, objp) 499 XDR *xdrs; 500 struct yppushresp_xfr *objp; 501 { 502 if (!xdr_u_long(xdrs, &objp->transid)) { 503 return (FALSE); 504 } 505 if (!xdr_yppush_status(xdrs, &objp->status)) { 506 return (FALSE); 507 } 508 return (TRUE); 509 } 510