1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "namespace.h" 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/socket.h> 38 #include <sys/file.h> 39 #include <sys/uio.h> 40 #include <arpa/inet.h> 41 #include <errno.h> 42 #include <stdio.h> 43 #include <string.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 #include <rpc/rpc.h> 47 #include <rpc/xdr.h> 48 #include <rpcsvc/yp.h> 49 #include "un-namespace.h" 50 #include "libc_private.h" 51 52 /* 53 * We have to define these here due to clashes between yp_prot.h and 54 * yp.h. 55 */ 56 57 #define YPMATCHCACHE 58 59 #ifdef YPMATCHCACHE 60 struct ypmatch_ent { 61 char *ypc_map; 62 keydat ypc_key; 63 valdat ypc_val; 64 time_t ypc_expire_t; 65 struct ypmatch_ent *ypc_next; 66 }; 67 #define YPLIB_MAXCACHE 5 /* At most 5 entries */ 68 #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */ 69 #endif 70 71 struct dom_binding { 72 struct dom_binding *dom_pnext; 73 char dom_domain[YPMAXDOMAIN + 1]; 74 struct sockaddr_in dom_server_addr; 75 u_short dom_server_port; 76 int dom_socket; 77 CLIENT *dom_client; 78 u_short dom_local_port; /* now I finally know what this is for. */ 79 long dom_vers; 80 #ifdef YPMATCHCACHE 81 struct ypmatch_ent *cache; 82 int ypmatch_cachecnt; 83 #endif 84 }; 85 86 #include <rpcsvc/ypclnt.h> 87 88 #ifndef BINDINGDIR 89 #define BINDINGDIR "/var/yp/binding" 90 #endif 91 #define MAX_RETRIES 20 92 93 extern bool_t xdr_domainname(), xdr_ypbind_resp(); 94 extern bool_t xdr_ypreq_key(), xdr_ypresp_val(); 95 extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val(); 96 extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq(); 97 extern bool_t xdr_ypresp_master(); 98 99 int (*ypresp_allfn)(); 100 void *ypresp_data; 101 102 static void _yp_unbind(struct dom_binding *); 103 struct dom_binding *_ypbindlist; 104 static char _yp_domain[MAXHOSTNAMELEN]; 105 int _yplib_timeout = 10; 106 107 #ifdef YPMATCHCACHE 108 static void 109 ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev, 110 struct ypmatch_ent *cur) 111 { 112 if (prev == NULL) 113 ypdb->cache = cur->ypc_next; 114 else 115 prev->ypc_next = cur->ypc_next; 116 117 free(cur->ypc_map); 118 free(cur->ypc_key.keydat_val); 119 free(cur->ypc_val.valdat_val); 120 free(cur); 121 122 ypdb->ypmatch_cachecnt--; 123 124 return; 125 } 126 127 static void 128 ypmatch_cache_flush(struct dom_binding *ypdb) 129 { 130 struct ypmatch_ent *n, *c = ypdb->cache; 131 132 while (c != NULL) { 133 n = c->ypc_next; 134 ypmatch_cache_delete(ypdb, NULL, c); 135 c = n; 136 } 137 138 return; 139 } 140 141 static void 142 ypmatch_cache_expire(struct dom_binding *ypdb) 143 { 144 struct ypmatch_ent *c = ypdb->cache; 145 struct ypmatch_ent *n, *p = NULL; 146 time_t t; 147 148 time(&t); 149 150 while (c != NULL) { 151 if (t >= c->ypc_expire_t) { 152 n = c->ypc_next; 153 ypmatch_cache_delete(ypdb, p, c); 154 c = n; 155 } else { 156 p = c; 157 c = c->ypc_next; 158 } 159 } 160 161 return; 162 } 163 164 static void 165 ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key, 166 valdat *val) 167 { 168 struct ypmatch_ent *new; 169 170 /* Do an expire run to maybe open up a slot. */ 171 if (ypdb->ypmatch_cachecnt) 172 ypmatch_cache_expire(ypdb); 173 174 /* 175 * If there are no slots free, then force an expire of 176 * the least recently used entry. 177 */ 178 if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) { 179 struct ypmatch_ent *o = NULL, *c = ypdb->cache; 180 time_t oldest = 0; 181 182 oldest = ~oldest; 183 184 while (c != NULL) { 185 if (c->ypc_expire_t < oldest) { 186 oldest = c->ypc_expire_t; 187 o = c; 188 } 189 c = c->ypc_next; 190 } 191 192 if (o == NULL) 193 return; 194 o->ypc_expire_t = 0; 195 ypmatch_cache_expire(ypdb); 196 } 197 198 new = malloc(sizeof(struct ypmatch_ent)); 199 if (new == NULL) 200 return; 201 202 new->ypc_map = strdup(map); 203 if (new->ypc_map == NULL) { 204 free(new); 205 return; 206 } 207 new->ypc_key.keydat_val = malloc(key->keydat_len); 208 if (new->ypc_key.keydat_val == NULL) { 209 free(new->ypc_map); 210 free(new); 211 return; 212 } 213 new->ypc_val.valdat_val = malloc(val->valdat_len); 214 if (new->ypc_val.valdat_val == NULL) { 215 free(new->ypc_val.valdat_val); 216 free(new->ypc_map); 217 free(new); 218 return; 219 } 220 221 new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE; 222 new->ypc_key.keydat_len = key->keydat_len; 223 new->ypc_val.valdat_len = val->valdat_len; 224 bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len); 225 bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len); 226 227 new->ypc_next = ypdb->cache; 228 ypdb->cache = new; 229 230 ypdb->ypmatch_cachecnt++; 231 232 return; 233 } 234 235 static bool_t 236 ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key, 237 valdat *val) 238 { 239 struct ypmatch_ent *c = ypdb->cache; 240 241 ypmatch_cache_expire(ypdb); 242 243 for (c = ypdb->cache; c != NULL; c = c->ypc_next) { 244 if (strcmp(map, c->ypc_map)) 245 continue; 246 if (key->keydat_len != c->ypc_key.keydat_len) 247 continue; 248 if (bcmp(key->keydat_val, c->ypc_key.keydat_val, 249 key->keydat_len)) 250 continue; 251 } 252 253 if (c == NULL) 254 return(FALSE); 255 256 val->valdat_len = c->ypc_val.valdat_len; 257 val->valdat_val = c->ypc_val.valdat_val; 258 259 return(TRUE); 260 } 261 #endif 262 263 char * 264 ypbinderr_string(int incode) 265 { 266 static char err[80]; 267 switch (incode) { 268 case 0: 269 return ("Success"); 270 case YPBIND_ERR_ERR: 271 return ("Internal ypbind error"); 272 case YPBIND_ERR_NOSERV: 273 return ("Domain not bound"); 274 case YPBIND_ERR_RESC: 275 return ("System resource allocation failure"); 276 } 277 sprintf(err, "Unknown ypbind error: #%d\n", incode); 278 return (err); 279 } 280 281 int 282 _yp_dobind(char *dom, struct dom_binding **ypdb) 283 { 284 static pid_t pid = -1; 285 char path[MAXPATHLEN]; 286 struct dom_binding *ysd, *ysd2; 287 struct ypbind_resp ypbr; 288 struct timeval tv; 289 struct sockaddr_in clnt_sin; 290 int clnt_sock, fd; 291 pid_t gpid; 292 CLIENT *client; 293 int new = 0, r; 294 int retries = 0; 295 struct sockaddr_in check; 296 int checklen = sizeof(struct sockaddr_in); 297 298 /* Not allowed; bad doggie. Bad. */ 299 if (strchr(dom, '/') != NULL) 300 return(YPERR_BADARGS); 301 302 gpid = getpid(); 303 if (!(pid == -1 || pid == gpid)) { 304 ysd = _ypbindlist; 305 while (ysd) { 306 if (ysd->dom_client != NULL) 307 _yp_unbind(ysd); 308 ysd2 = ysd->dom_pnext; 309 free(ysd); 310 ysd = ysd2; 311 } 312 _ypbindlist = NULL; 313 } 314 pid = gpid; 315 316 if (ypdb != NULL) 317 *ypdb = NULL; 318 319 if (dom == NULL || strlen(dom) == 0) 320 return (YPERR_BADARGS); 321 322 for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext) 323 if (strcmp(dom, ysd->dom_domain) == 0) 324 break; 325 326 327 if (ysd == NULL) { 328 ysd = (struct dom_binding *)malloc(sizeof *ysd); 329 bzero((char *)ysd, sizeof *ysd); 330 ysd->dom_socket = -1; 331 ysd->dom_vers = 0; 332 new = 1; 333 } else { 334 /* Check the socket -- may have been hosed by the caller. */ 335 if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check, 336 &checklen) == -1 || check.sin_family != AF_INET || 337 check.sin_port != ysd->dom_local_port) { 338 /* Socket became bogus somehow... need to rebind. */ 339 int save, sock; 340 341 sock = ysd->dom_socket; 342 save = _dup(ysd->dom_socket); 343 if (ysd->dom_client != NULL) 344 clnt_destroy(ysd->dom_client); 345 ysd->dom_vers = 0; 346 ysd->dom_client = NULL; 347 sock = _dup2(save, sock); 348 _close(save); 349 } 350 } 351 352 again: 353 retries++; 354 if (retries > MAX_RETRIES) { 355 if (new) 356 free(ysd); 357 return(YPERR_YPBIND); 358 } 359 #ifdef BINDINGDIR 360 if (ysd->dom_vers == 0) { 361 /* 362 * We're trying to make a new binding: zorch the 363 * existing handle now (if any). 364 */ 365 if (ysd->dom_client != NULL) { 366 clnt_destroy(ysd->dom_client); 367 ysd->dom_client = NULL; 368 ysd->dom_socket = -1; 369 } 370 snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2); 371 if ((fd = _open(path, O_RDONLY)) == -1) { 372 /* no binding file, YP is dead. */ 373 /* Try to bring it back to life. */ 374 _close(fd); 375 goto skipit; 376 } 377 if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) { 378 struct iovec iov[2]; 379 struct ypbind_resp ybr; 380 u_short ypb_port; 381 382 iov[0].iov_base = (caddr_t)&ypb_port; 383 iov[0].iov_len = sizeof ypb_port; 384 iov[1].iov_base = (caddr_t)&ybr; 385 iov[1].iov_len = sizeof ybr; 386 387 r = _readv(fd, iov, 2); 388 if (r != iov[0].iov_len + iov[1].iov_len) { 389 _close(fd); 390 ysd->dom_vers = -1; 391 goto again; 392 } 393 394 bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr); 395 ysd->dom_server_addr.sin_family = AF_INET; 396 ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in); 397 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 398 &ysd->dom_server_addr.sin_addr.s_addr, 399 sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 400 bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port, 401 &ysd->dom_server_addr.sin_port, 402 sizeof(ysd->dom_server_addr.sin_port)); 403 404 ysd->dom_server_port = ysd->dom_server_addr.sin_port; 405 _close(fd); 406 goto gotit; 407 } else { 408 /* no lock on binding file, YP is dead. */ 409 /* Try to bring it back to life. */ 410 _close(fd); 411 goto skipit; 412 } 413 } 414 skipit: 415 #endif 416 if (ysd->dom_vers == -1 || ysd->dom_vers == 0) { 417 /* 418 * We're trying to make a new binding: zorch the 419 * existing handle now (if any). 420 */ 421 if (ysd->dom_client != NULL) { 422 clnt_destroy(ysd->dom_client); 423 ysd->dom_client = NULL; 424 ysd->dom_socket = -1; 425 } 426 bzero((char *)&clnt_sin, sizeof clnt_sin); 427 clnt_sin.sin_family = AF_INET; 428 clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 429 430 clnt_sock = RPC_ANYSOCK; 431 client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock, 432 0, 0); 433 if (client == NULL) { 434 /* 435 * These conditions indicate ypbind just isn't 436 * alive -- we probably don't want to shoot our 437 * mouth off in this case; instead generate error 438 * messages only for really exotic problems. 439 */ 440 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED && 441 (rpc_createerr.cf_stat != RPC_SYSTEMERROR && 442 rpc_createerr.cf_error.re_errno == ECONNREFUSED)) 443 clnt_pcreateerror("clnttcp_create"); 444 if (new) 445 free(ysd); 446 return (YPERR_YPBIND); 447 } 448 449 /* 450 * Check the port number -- should be < IPPORT_RESERVED. 451 * If not, it's possible someone has registered a bogus 452 * ypbind with the portmapper and is trying to trick us. 453 */ 454 if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) { 455 if (client != NULL) 456 clnt_destroy(client); 457 if (new) 458 free(ysd); 459 return(YPERR_YPBIND); 460 } 461 tv.tv_sec = _yplib_timeout/2; 462 tv.tv_usec = 0; 463 r = clnt_call(client, YPBINDPROC_DOMAIN, 464 xdr_domainname, (char *)&dom, xdr_ypbind_resp, &ypbr, tv); 465 if (r != RPC_SUCCESS) { 466 clnt_destroy(client); 467 ysd->dom_vers = -1; 468 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) { 469 if (new) 470 free(ysd); 471 return(YPERR_YPBIND); 472 } 473 fprintf(stderr, 474 "YP: server for domain %s not responding, retrying\n", dom); 475 goto again; 476 } else { 477 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 478 struct timespec time_to_sleep, time_remaining; 479 480 clnt_destroy(client); 481 ysd->dom_vers = -1; 482 483 time_to_sleep.tv_sec = _yplib_timeout/2; 484 time_to_sleep.tv_nsec = 0; 485 _nanosleep(&time_to_sleep, 486 &time_remaining); 487 goto again; 488 } 489 } 490 clnt_destroy(client); 491 492 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr); 493 ysd->dom_server_addr.sin_family = AF_INET; 494 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port, 495 &ysd->dom_server_addr.sin_port, 496 sizeof(ysd->dom_server_addr.sin_port)); 497 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 498 &ysd->dom_server_addr.sin_addr.s_addr, 499 sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 500 501 /* 502 * We could do a reserved port check here too, but this 503 * could pose compatibility problems. The local ypbind is 504 * supposed to decide whether or not to trust yp servers 505 * on insecure ports. For now, we trust its judgement. 506 */ 507 ysd->dom_server_port = 508 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port; 509 gotit: 510 ysd->dom_vers = YPVERS; 511 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); 512 } 513 514 /* Don't rebuild the connection to the server unless we have to. */ 515 if (ysd->dom_client == NULL) { 516 tv.tv_sec = _yplib_timeout/2; 517 tv.tv_usec = 0; 518 ysd->dom_socket = RPC_ANYSOCK; 519 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr, 520 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304); 521 if (ysd->dom_client == NULL) { 522 clnt_pcreateerror("clntudp_create"); 523 ysd->dom_vers = -1; 524 goto again; 525 } 526 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1) 527 perror("fcntl: F_SETFD"); 528 /* 529 * We want a port number associated with this socket 530 * so that we can check its authenticity later. 531 */ 532 checklen = sizeof(struct sockaddr_in); 533 bzero((char *)&check, checklen); 534 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen); 535 check.sin_family = AF_INET; 536 if (!_getsockname(ysd->dom_socket, 537 (struct sockaddr *)&check, &checklen)) { 538 ysd->dom_local_port = check.sin_port; 539 } else { 540 clnt_destroy(ysd->dom_client); 541 if (new) 542 free(ysd); 543 return(YPERR_YPBIND); 544 } 545 } 546 547 if (new) { 548 ysd->dom_pnext = _ypbindlist; 549 _ypbindlist = ysd; 550 } 551 552 if (ypdb != NULL) 553 *ypdb = ysd; 554 return (0); 555 } 556 557 static void 558 _yp_unbind(struct dom_binding *ypb) 559 { 560 struct sockaddr_in check; 561 int checklen = sizeof(struct sockaddr_in); 562 563 if (ypb->dom_client != NULL) { 564 /* Check the socket -- may have been hosed by the caller. */ 565 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check, 566 &checklen) == -1 || check.sin_family != AF_INET || 567 check.sin_port != ypb->dom_local_port) { 568 int save, sock; 569 570 sock = ypb->dom_socket; 571 save = _dup(ypb->dom_socket); 572 clnt_destroy(ypb->dom_client); 573 sock = _dup2(save, sock); 574 _close(save); 575 } else 576 clnt_destroy(ypb->dom_client); 577 } 578 579 ypb->dom_client = NULL; 580 ypb->dom_socket = -1; 581 ypb->dom_vers = -1; 582 #ifdef YPMATCHCACHE 583 ypmatch_cache_flush(ypb); 584 #endif 585 } 586 587 int 588 yp_bind(char *dom) 589 { 590 return (_yp_dobind(dom, NULL)); 591 } 592 593 void 594 yp_unbind(char *dom) 595 { 596 struct dom_binding *ypb, *ypbp; 597 598 ypbp = NULL; 599 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) { 600 if (strcmp(dom, ypb->dom_domain) == 0) { 601 _yp_unbind(ypb); 602 if (ypbp) 603 ypbp->dom_pnext = ypb->dom_pnext; 604 else 605 _ypbindlist = ypb->dom_pnext; 606 free(ypb); 607 return; 608 } 609 ypbp = ypb; 610 } 611 return; 612 } 613 614 int 615 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen, 616 char **outval, int *outvallen) 617 { 618 struct dom_binding *ysd; 619 struct ypresp_val yprv; 620 struct timeval tv; 621 struct ypreq_key yprk; 622 int r; 623 624 *outval = NULL; 625 *outvallen = 0; 626 627 /* Sanity check */ 628 629 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 630 inmap == NULL || !strlen(inmap) || 631 indomain == NULL || !strlen(indomain)) 632 return (YPERR_BADARGS); 633 634 if (_yp_dobind(indomain, &ysd) != 0) 635 return(YPERR_DOMAIN); 636 637 yprk.domain = indomain; 638 yprk.map = inmap; 639 yprk.key.keydat_val = (char *)inkey; 640 yprk.key.keydat_len = inkeylen; 641 642 #ifdef YPMATCHCACHE 643 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) { 644 /* 645 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey, 646 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) { 647 */ 648 *outvallen = yprv.val.valdat_len; 649 *outval = (char *)malloc(*outvallen+1); 650 bcopy(yprv.val.valdat_val, *outval, *outvallen); 651 (*outval)[*outvallen] = '\0'; 652 return (0); 653 } 654 #endif 655 656 again: 657 if (_yp_dobind(indomain, &ysd) != 0) 658 return (YPERR_DOMAIN); 659 660 tv.tv_sec = _yplib_timeout; 661 tv.tv_usec = 0; 662 663 bzero((char *)&yprv, sizeof yprv); 664 665 r = clnt_call(ysd->dom_client, YPPROC_MATCH, 666 xdr_ypreq_key, &yprk, xdr_ypresp_val, &yprv, tv); 667 if (r != RPC_SUCCESS) { 668 clnt_perror(ysd->dom_client, "yp_match: clnt_call"); 669 _yp_unbind(ysd); 670 goto again; 671 } 672 673 if (!(r = ypprot_err(yprv.stat))) { 674 *outvallen = yprv.val.valdat_len; 675 *outval = (char *)malloc(*outvallen+1); 676 bcopy(yprv.val.valdat_val, *outval, *outvallen); 677 (*outval)[*outvallen] = '\0'; 678 #ifdef YPMATCHCACHE 679 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val); 680 #endif 681 } 682 683 xdr_free(xdr_ypresp_val, (char *)&yprv); 684 return (r); 685 } 686 687 int 688 yp_get_default_domain(char **domp) 689 { 690 *domp = NULL; 691 if (_yp_domain[0] == '\0') 692 if (getdomainname(_yp_domain, sizeof _yp_domain)) 693 return (YPERR_NODOM); 694 *domp = _yp_domain; 695 return (0); 696 } 697 698 int 699 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen, 700 char **outval, int *outvallen) 701 { 702 struct ypresp_key_val yprkv; 703 struct ypreq_nokey yprnk; 704 struct dom_binding *ysd; 705 struct timeval tv; 706 int r; 707 708 /* Sanity check */ 709 710 if (indomain == NULL || !strlen(indomain) || 711 inmap == NULL || !strlen(inmap)) 712 return (YPERR_BADARGS); 713 714 *outkey = *outval = NULL; 715 *outkeylen = *outvallen = 0; 716 717 again: 718 if (_yp_dobind(indomain, &ysd) != 0) 719 return (YPERR_DOMAIN); 720 721 tv.tv_sec = _yplib_timeout; 722 tv.tv_usec = 0; 723 724 yprnk.domain = indomain; 725 yprnk.map = inmap; 726 bzero((char *)&yprkv, sizeof yprkv); 727 728 r = clnt_call(ysd->dom_client, YPPROC_FIRST, 729 xdr_ypreq_nokey, &yprnk, xdr_ypresp_key_val, &yprkv, tv); 730 if (r != RPC_SUCCESS) { 731 clnt_perror(ysd->dom_client, "yp_first: clnt_call"); 732 _yp_unbind(ysd); 733 goto again; 734 } 735 if (!(r = ypprot_err(yprkv.stat))) { 736 *outkeylen = yprkv.key.keydat_len; 737 *outkey = (char *)malloc(*outkeylen+1); 738 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 739 (*outkey)[*outkeylen] = '\0'; 740 *outvallen = yprkv.val.valdat_len; 741 *outval = (char *)malloc(*outvallen+1); 742 bcopy(yprkv.val.valdat_val, *outval, *outvallen); 743 (*outval)[*outvallen] = '\0'; 744 } 745 746 xdr_free(xdr_ypresp_key_val, (char *)&yprkv); 747 return (r); 748 } 749 750 int 751 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen, 752 char **outkey, int *outkeylen, char **outval, int *outvallen) 753 { 754 struct ypresp_key_val yprkv; 755 struct ypreq_key yprk; 756 struct dom_binding *ysd; 757 struct timeval tv; 758 int r; 759 760 /* Sanity check */ 761 762 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 763 inmap == NULL || !strlen(inmap) || 764 indomain == NULL || !strlen(indomain)) 765 return (YPERR_BADARGS); 766 767 *outkey = *outval = NULL; 768 *outkeylen = *outvallen = 0; 769 770 again: 771 if (_yp_dobind(indomain, &ysd) != 0) 772 return (YPERR_DOMAIN); 773 774 tv.tv_sec = _yplib_timeout; 775 tv.tv_usec = 0; 776 777 yprk.domain = indomain; 778 yprk.map = inmap; 779 yprk.key.keydat_val = inkey; 780 yprk.key.keydat_len = inkeylen; 781 bzero((char *)&yprkv, sizeof yprkv); 782 783 r = clnt_call(ysd->dom_client, YPPROC_NEXT, 784 xdr_ypreq_key, &yprk, xdr_ypresp_key_val, &yprkv, tv); 785 if (r != RPC_SUCCESS) { 786 clnt_perror(ysd->dom_client, "yp_next: clnt_call"); 787 _yp_unbind(ysd); 788 goto again; 789 } 790 if (!(r = ypprot_err(yprkv.stat))) { 791 *outkeylen = yprkv.key.keydat_len; 792 *outkey = (char *)malloc(*outkeylen+1); 793 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 794 (*outkey)[*outkeylen] = '\0'; 795 *outvallen = yprkv.val.valdat_len; 796 *outval = (char *)malloc(*outvallen+1); 797 bcopy(yprkv.val.valdat_val, *outval, *outvallen); 798 (*outval)[*outvallen] = '\0'; 799 } 800 801 xdr_free(xdr_ypresp_key_val, (char *)&yprkv); 802 return (r); 803 } 804 805 int 806 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback) 807 { 808 struct ypreq_nokey yprnk; 809 struct dom_binding *ysd; 810 struct timeval tv; 811 struct sockaddr_in clnt_sin; 812 CLIENT *clnt; 813 u_long status, savstat; 814 int clnt_sock; 815 816 /* Sanity check */ 817 818 if (indomain == NULL || !strlen(indomain) || 819 inmap == NULL || !strlen(inmap)) 820 return (YPERR_BADARGS); 821 822 again: 823 824 if (_yp_dobind(indomain, &ysd) != 0) 825 return (YPERR_DOMAIN); 826 827 tv.tv_sec = _yplib_timeout; 828 tv.tv_usec = 0; 829 830 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */ 831 832 clnt_sock = RPC_ANYSOCK; 833 clnt_sin = ysd->dom_server_addr; 834 clnt_sin.sin_port = 0; 835 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0); 836 if (clnt == NULL) { 837 printf("clnttcp_create failed\n"); 838 return (YPERR_PMAP); 839 } 840 841 yprnk.domain = indomain; 842 yprnk.map = inmap; 843 ypresp_allfn = incallback->foreach; 844 ypresp_data = (void *)incallback->data; 845 846 if (clnt_call(clnt, YPPROC_ALL, 847 xdr_ypreq_nokey, &yprnk, 848 xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) { 849 clnt_perror(ysd->dom_client, "yp_all: clnt_call"); 850 clnt_destroy(clnt); 851 _yp_unbind(ysd); 852 goto again; 853 } 854 855 clnt_destroy(clnt); 856 savstat = status; 857 xdr_free(xdr_ypresp_all_seq, (char *)&status); /* not really needed... */ 858 if (savstat != YP_NOMORE) 859 return (ypprot_err(savstat)); 860 return (0); 861 } 862 863 int 864 yp_order(char *indomain, char *inmap, int *outorder) 865 { 866 struct dom_binding *ysd; 867 struct ypresp_order ypro; 868 struct ypreq_nokey yprnk; 869 struct timeval tv; 870 int r; 871 872 /* Sanity check */ 873 874 if (indomain == NULL || !strlen(indomain) || 875 inmap == NULL || !strlen(inmap)) 876 return (YPERR_BADARGS); 877 878 again: 879 if (_yp_dobind(indomain, &ysd) != 0) 880 return (YPERR_DOMAIN); 881 882 tv.tv_sec = _yplib_timeout; 883 tv.tv_usec = 0; 884 885 yprnk.domain = indomain; 886 yprnk.map = inmap; 887 888 bzero((char *)(char *)&ypro, sizeof ypro); 889 890 r = clnt_call(ysd->dom_client, YPPROC_ORDER, 891 xdr_ypreq_nokey, &yprnk, xdr_ypresp_order, &ypro, tv); 892 893 /* 894 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER 895 * procedure. 896 */ 897 if (r == RPC_PROCUNAVAIL) { 898 return(YPERR_YPERR); 899 } 900 901 if (r != RPC_SUCCESS) { 902 clnt_perror(ysd->dom_client, "yp_order: clnt_call"); 903 _yp_unbind(ysd); 904 goto again; 905 } 906 907 if (!(r = ypprot_err(ypro.stat))) { 908 *outorder = ypro.ordernum; 909 } 910 911 xdr_free(xdr_ypresp_order, (char *)&ypro); 912 return (r); 913 } 914 915 int 916 yp_master(char *indomain, char *inmap, char **outname) 917 { 918 struct dom_binding *ysd; 919 struct ypresp_master yprm; 920 struct ypreq_nokey yprnk; 921 struct timeval tv; 922 int r; 923 924 /* Sanity check */ 925 926 if (indomain == NULL || !strlen(indomain) || 927 inmap == NULL || !strlen(inmap)) 928 return (YPERR_BADARGS); 929 again: 930 if (_yp_dobind(indomain, &ysd) != 0) 931 return (YPERR_DOMAIN); 932 933 tv.tv_sec = _yplib_timeout; 934 tv.tv_usec = 0; 935 936 yprnk.domain = indomain; 937 yprnk.map = inmap; 938 939 bzero((char *)&yprm, sizeof yprm); 940 941 r = clnt_call(ysd->dom_client, YPPROC_MASTER, 942 xdr_ypreq_nokey, &yprnk, xdr_ypresp_master, &yprm, tv); 943 if (r != RPC_SUCCESS) { 944 clnt_perror(ysd->dom_client, "yp_master: clnt_call"); 945 _yp_unbind(ysd); 946 goto again; 947 } 948 949 if (!(r = ypprot_err(yprm.stat))) { 950 *outname = (char *)strdup(yprm.peer); 951 } 952 953 xdr_free(xdr_ypresp_master, (char *)&yprm); 954 return (r); 955 } 956 957 int 958 yp_maplist(char *indomain, struct ypmaplist **outmaplist) 959 { 960 struct dom_binding *ysd; 961 struct ypresp_maplist ypml; 962 struct timeval tv; 963 int r; 964 965 /* Sanity check */ 966 967 if (indomain == NULL || !strlen(indomain)) 968 return (YPERR_BADARGS); 969 970 again: 971 if (_yp_dobind(indomain, &ysd) != 0) 972 return (YPERR_DOMAIN); 973 974 tv.tv_sec = _yplib_timeout; 975 tv.tv_usec = 0; 976 977 bzero((char *)&ypml, sizeof ypml); 978 979 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST, 980 xdr_domainname,(char *)&indomain,xdr_ypresp_maplist,&ypml,tv); 981 if (r != RPC_SUCCESS) { 982 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call"); 983 _yp_unbind(ysd); 984 goto again; 985 } 986 if (!(r = ypprot_err(ypml.stat))) { 987 *outmaplist = ypml.maps; 988 } 989 990 /* NO: xdr_free(xdr_ypresp_maplist, &ypml);*/ 991 return (r); 992 } 993 994 char * 995 yperr_string(int incode) 996 { 997 static char err[80]; 998 999 switch (incode) { 1000 case 0: 1001 return ("Success"); 1002 case YPERR_BADARGS: 1003 return ("Request arguments bad"); 1004 case YPERR_RPC: 1005 return ("RPC failure"); 1006 case YPERR_DOMAIN: 1007 return ("Can't bind to server which serves this domain"); 1008 case YPERR_MAP: 1009 return ("No such map in server's domain"); 1010 case YPERR_KEY: 1011 return ("No such key in map"); 1012 case YPERR_YPERR: 1013 return ("YP server error"); 1014 case YPERR_RESRC: 1015 return ("Local resource allocation failure"); 1016 case YPERR_NOMORE: 1017 return ("No more records in map database"); 1018 case YPERR_PMAP: 1019 return ("Can't communicate with portmapper"); 1020 case YPERR_YPBIND: 1021 return ("Can't communicate with ypbind"); 1022 case YPERR_YPSERV: 1023 return ("Can't communicate with ypserv"); 1024 case YPERR_NODOM: 1025 return ("Local domain name not set"); 1026 case YPERR_BADDB: 1027 return ("Server data base is bad"); 1028 case YPERR_VERS: 1029 return ("YP server version mismatch - server can't supply service."); 1030 case YPERR_ACCESS: 1031 return ("Access violation"); 1032 case YPERR_BUSY: 1033 return ("Database is busy"); 1034 } 1035 sprintf(err, "YP unknown error %d\n", incode); 1036 return (err); 1037 } 1038 1039 int 1040 ypprot_err(unsigned int incode) 1041 { 1042 switch (incode) { 1043 case YP_TRUE: 1044 return (0); 1045 case YP_FALSE: 1046 return (YPERR_YPBIND); 1047 case YP_NOMORE: 1048 return (YPERR_NOMORE); 1049 case YP_NOMAP: 1050 return (YPERR_MAP); 1051 case YP_NODOM: 1052 return (YPERR_DOMAIN); 1053 case YP_NOKEY: 1054 return (YPERR_KEY); 1055 case YP_BADOP: 1056 return (YPERR_YPERR); 1057 case YP_BADDB: 1058 return (YPERR_BADDB); 1059 case YP_YPERR: 1060 return (YPERR_YPERR); 1061 case YP_BADARGS: 1062 return (YPERR_BADARGS); 1063 case YP_VERS: 1064 return (YPERR_VERS); 1065 } 1066 return (YPERR_YPERR); 1067 } 1068 1069 int 1070 _yp_check(char **dom) 1071 { 1072 char *unused; 1073 1074 if (_yp_domain[0]=='\0') 1075 if (yp_get_default_domain(&unused)) 1076 return (0); 1077 1078 if (dom) 1079 *dom = _yp_domain; 1080 1081 if (yp_bind(_yp_domain) == 0) { 1082 yp_unbind(_yp_domain); 1083 return (1); 1084 } 1085 return (0); 1086 } 1087