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 const 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 (xdrproc_t)xdr_domainname, &dom, 465 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv); 466 if (r != RPC_SUCCESS) { 467 clnt_destroy(client); 468 ysd->dom_vers = -1; 469 if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) { 470 if (new) 471 free(ysd); 472 return(YPERR_YPBIND); 473 } 474 fprintf(stderr, 475 "YP: server for domain %s not responding, retrying\n", dom); 476 goto again; 477 } else { 478 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 479 struct timespec time_to_sleep, time_remaining; 480 481 clnt_destroy(client); 482 ysd->dom_vers = -1; 483 484 time_to_sleep.tv_sec = _yplib_timeout/2; 485 time_to_sleep.tv_nsec = 0; 486 _nanosleep(&time_to_sleep, 487 &time_remaining); 488 goto again; 489 } 490 } 491 clnt_destroy(client); 492 493 bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr); 494 ysd->dom_server_addr.sin_family = AF_INET; 495 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port, 496 &ysd->dom_server_addr.sin_port, 497 sizeof(ysd->dom_server_addr.sin_port)); 498 bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 499 &ysd->dom_server_addr.sin_addr.s_addr, 500 sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 501 502 /* 503 * We could do a reserved port check here too, but this 504 * could pose compatibility problems. The local ypbind is 505 * supposed to decide whether or not to trust yp servers 506 * on insecure ports. For now, we trust its judgement. 507 */ 508 ysd->dom_server_port = 509 *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port; 510 gotit: 511 ysd->dom_vers = YPVERS; 512 strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); 513 } 514 515 /* Don't rebuild the connection to the server unless we have to. */ 516 if (ysd->dom_client == NULL) { 517 tv.tv_sec = _yplib_timeout/2; 518 tv.tv_usec = 0; 519 ysd->dom_socket = RPC_ANYSOCK; 520 ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr, 521 YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304); 522 if (ysd->dom_client == NULL) { 523 clnt_pcreateerror("clntudp_create"); 524 ysd->dom_vers = -1; 525 goto again; 526 } 527 if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1) 528 perror("fcntl: F_SETFD"); 529 /* 530 * We want a port number associated with this socket 531 * so that we can check its authenticity later. 532 */ 533 checklen = sizeof(struct sockaddr_in); 534 bzero((char *)&check, checklen); 535 _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen); 536 check.sin_family = AF_INET; 537 if (!_getsockname(ysd->dom_socket, 538 (struct sockaddr *)&check, &checklen)) { 539 ysd->dom_local_port = check.sin_port; 540 } else { 541 clnt_destroy(ysd->dom_client); 542 if (new) 543 free(ysd); 544 return(YPERR_YPBIND); 545 } 546 } 547 548 if (new) { 549 ysd->dom_pnext = _ypbindlist; 550 _ypbindlist = ysd; 551 } 552 553 if (ypdb != NULL) 554 *ypdb = ysd; 555 return (0); 556 } 557 558 static void 559 _yp_unbind(struct dom_binding *ypb) 560 { 561 struct sockaddr_in check; 562 int checklen = sizeof(struct sockaddr_in); 563 564 if (ypb->dom_client != NULL) { 565 /* Check the socket -- may have been hosed by the caller. */ 566 if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check, 567 &checklen) == -1 || check.sin_family != AF_INET || 568 check.sin_port != ypb->dom_local_port) { 569 int save, sock; 570 571 sock = ypb->dom_socket; 572 save = _dup(ypb->dom_socket); 573 clnt_destroy(ypb->dom_client); 574 sock = _dup2(save, sock); 575 _close(save); 576 } else 577 clnt_destroy(ypb->dom_client); 578 } 579 580 ypb->dom_client = NULL; 581 ypb->dom_socket = -1; 582 ypb->dom_vers = -1; 583 #ifdef YPMATCHCACHE 584 ypmatch_cache_flush(ypb); 585 #endif 586 } 587 588 int 589 yp_bind(char *dom) 590 { 591 return (_yp_dobind(dom, NULL)); 592 } 593 594 void 595 yp_unbind(char *dom) 596 { 597 struct dom_binding *ypb, *ypbp; 598 599 ypbp = NULL; 600 for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) { 601 if (strcmp(dom, ypb->dom_domain) == 0) { 602 _yp_unbind(ypb); 603 if (ypbp) 604 ypbp->dom_pnext = ypb->dom_pnext; 605 else 606 _ypbindlist = ypb->dom_pnext; 607 free(ypb); 608 return; 609 } 610 ypbp = ypb; 611 } 612 return; 613 } 614 615 int 616 yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen, 617 char **outval, int *outvallen) 618 { 619 struct dom_binding *ysd; 620 struct ypresp_val yprv; 621 struct timeval tv; 622 struct ypreq_key yprk; 623 int r; 624 625 *outval = NULL; 626 *outvallen = 0; 627 628 /* Sanity check */ 629 630 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 631 inmap == NULL || !strlen(inmap) || 632 indomain == NULL || !strlen(indomain)) 633 return (YPERR_BADARGS); 634 635 if (_yp_dobind(indomain, &ysd) != 0) 636 return(YPERR_DOMAIN); 637 638 yprk.domain = indomain; 639 yprk.map = inmap; 640 yprk.key.keydat_val = (char *)inkey; 641 yprk.key.keydat_len = inkeylen; 642 643 #ifdef YPMATCHCACHE 644 if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) { 645 /* 646 if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey, 647 inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) { 648 */ 649 *outvallen = yprv.val.valdat_len; 650 *outval = (char *)malloc(*outvallen+1); 651 bcopy(yprv.val.valdat_val, *outval, *outvallen); 652 (*outval)[*outvallen] = '\0'; 653 return (0); 654 } 655 #endif 656 657 again: 658 if (_yp_dobind(indomain, &ysd) != 0) 659 return (YPERR_DOMAIN); 660 661 tv.tv_sec = _yplib_timeout; 662 tv.tv_usec = 0; 663 664 bzero((char *)&yprv, sizeof yprv); 665 666 r = clnt_call(ysd->dom_client, YPPROC_MATCH, 667 (xdrproc_t)xdr_ypreq_key, &yprk, 668 (xdrproc_t)xdr_ypresp_val, &yprv, tv); 669 if (r != RPC_SUCCESS) { 670 clnt_perror(ysd->dom_client, "yp_match: clnt_call"); 671 _yp_unbind(ysd); 672 goto again; 673 } 674 675 if (!(r = ypprot_err(yprv.stat))) { 676 *outvallen = yprv.val.valdat_len; 677 *outval = (char *)malloc(*outvallen+1); 678 bcopy(yprv.val.valdat_val, *outval, *outvallen); 679 (*outval)[*outvallen] = '\0'; 680 #ifdef YPMATCHCACHE 681 ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val); 682 #endif 683 } 684 685 xdr_free((xdrproc_t)xdr_ypresp_val, &yprv); 686 return (r); 687 } 688 689 int 690 yp_get_default_domain(char **domp) 691 { 692 *domp = NULL; 693 if (_yp_domain[0] == '\0') 694 if (getdomainname(_yp_domain, sizeof _yp_domain)) 695 return (YPERR_NODOM); 696 *domp = _yp_domain; 697 return (0); 698 } 699 700 int 701 yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen, 702 char **outval, int *outvallen) 703 { 704 struct ypresp_key_val yprkv; 705 struct ypreq_nokey yprnk; 706 struct dom_binding *ysd; 707 struct timeval tv; 708 int r; 709 710 /* Sanity check */ 711 712 if (indomain == NULL || !strlen(indomain) || 713 inmap == NULL || !strlen(inmap)) 714 return (YPERR_BADARGS); 715 716 *outkey = *outval = NULL; 717 *outkeylen = *outvallen = 0; 718 719 again: 720 if (_yp_dobind(indomain, &ysd) != 0) 721 return (YPERR_DOMAIN); 722 723 tv.tv_sec = _yplib_timeout; 724 tv.tv_usec = 0; 725 726 yprnk.domain = indomain; 727 yprnk.map = inmap; 728 bzero((char *)&yprkv, sizeof yprkv); 729 730 r = clnt_call(ysd->dom_client, YPPROC_FIRST, 731 (xdrproc_t)xdr_ypreq_nokey, &yprnk, 732 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv); 733 if (r != RPC_SUCCESS) { 734 clnt_perror(ysd->dom_client, "yp_first: clnt_call"); 735 _yp_unbind(ysd); 736 goto again; 737 } 738 if (!(r = ypprot_err(yprkv.stat))) { 739 *outkeylen = yprkv.key.keydat_len; 740 *outkey = (char *)malloc(*outkeylen+1); 741 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 742 (*outkey)[*outkeylen] = '\0'; 743 *outvallen = yprkv.val.valdat_len; 744 *outval = (char *)malloc(*outvallen+1); 745 bcopy(yprkv.val.valdat_val, *outval, *outvallen); 746 (*outval)[*outvallen] = '\0'; 747 } 748 749 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv); 750 return (r); 751 } 752 753 int 754 yp_next(char *indomain, char *inmap, char *inkey, int inkeylen, 755 char **outkey, int *outkeylen, char **outval, int *outvallen) 756 { 757 struct ypresp_key_val yprkv; 758 struct ypreq_key yprk; 759 struct dom_binding *ysd; 760 struct timeval tv; 761 int r; 762 763 /* Sanity check */ 764 765 if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 766 inmap == NULL || !strlen(inmap) || 767 indomain == NULL || !strlen(indomain)) 768 return (YPERR_BADARGS); 769 770 *outkey = *outval = NULL; 771 *outkeylen = *outvallen = 0; 772 773 again: 774 if (_yp_dobind(indomain, &ysd) != 0) 775 return (YPERR_DOMAIN); 776 777 tv.tv_sec = _yplib_timeout; 778 tv.tv_usec = 0; 779 780 yprk.domain = indomain; 781 yprk.map = inmap; 782 yprk.key.keydat_val = inkey; 783 yprk.key.keydat_len = inkeylen; 784 bzero((char *)&yprkv, sizeof yprkv); 785 786 r = clnt_call(ysd->dom_client, YPPROC_NEXT, 787 (xdrproc_t)xdr_ypreq_key, &yprk, 788 (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv); 789 if (r != RPC_SUCCESS) { 790 clnt_perror(ysd->dom_client, "yp_next: clnt_call"); 791 _yp_unbind(ysd); 792 goto again; 793 } 794 if (!(r = ypprot_err(yprkv.stat))) { 795 *outkeylen = yprkv.key.keydat_len; 796 *outkey = (char *)malloc(*outkeylen+1); 797 bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 798 (*outkey)[*outkeylen] = '\0'; 799 *outvallen = yprkv.val.valdat_len; 800 *outval = (char *)malloc(*outvallen+1); 801 bcopy(yprkv.val.valdat_val, *outval, *outvallen); 802 (*outval)[*outvallen] = '\0'; 803 } 804 805 xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv); 806 return (r); 807 } 808 809 int 810 yp_all(char *indomain, char *inmap, struct ypall_callback *incallback) 811 { 812 struct ypreq_nokey yprnk; 813 struct dom_binding *ysd; 814 struct timeval tv; 815 struct sockaddr_in clnt_sin; 816 CLIENT *clnt; 817 u_long status, savstat; 818 int clnt_sock; 819 820 /* Sanity check */ 821 822 if (indomain == NULL || !strlen(indomain) || 823 inmap == NULL || !strlen(inmap)) 824 return (YPERR_BADARGS); 825 826 again: 827 828 if (_yp_dobind(indomain, &ysd) != 0) 829 return (YPERR_DOMAIN); 830 831 tv.tv_sec = _yplib_timeout; 832 tv.tv_usec = 0; 833 834 /* YPPROC_ALL manufactures its own channel to ypserv using TCP */ 835 836 clnt_sock = RPC_ANYSOCK; 837 clnt_sin = ysd->dom_server_addr; 838 clnt_sin.sin_port = 0; 839 clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0); 840 if (clnt == NULL) { 841 printf("clnttcp_create failed\n"); 842 return (YPERR_PMAP); 843 } 844 845 yprnk.domain = indomain; 846 yprnk.map = inmap; 847 ypresp_allfn = incallback->foreach; 848 ypresp_data = (void *)incallback->data; 849 850 if (clnt_call(clnt, YPPROC_ALL, 851 (xdrproc_t)xdr_ypreq_nokey, &yprnk, 852 (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) { 853 clnt_perror(ysd->dom_client, "yp_all: clnt_call"); 854 clnt_destroy(clnt); 855 _yp_unbind(ysd); 856 goto again; 857 } 858 859 clnt_destroy(clnt); 860 savstat = status; 861 xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */ 862 if (savstat != YP_NOMORE) 863 return (ypprot_err(savstat)); 864 return (0); 865 } 866 867 int 868 yp_order(char *indomain, char *inmap, int *outorder) 869 { 870 struct dom_binding *ysd; 871 struct ypresp_order ypro; 872 struct ypreq_nokey yprnk; 873 struct timeval tv; 874 int r; 875 876 /* Sanity check */ 877 878 if (indomain == NULL || !strlen(indomain) || 879 inmap == NULL || !strlen(inmap)) 880 return (YPERR_BADARGS); 881 882 again: 883 if (_yp_dobind(indomain, &ysd) != 0) 884 return (YPERR_DOMAIN); 885 886 tv.tv_sec = _yplib_timeout; 887 tv.tv_usec = 0; 888 889 yprnk.domain = indomain; 890 yprnk.map = inmap; 891 892 bzero((char *)(char *)&ypro, sizeof ypro); 893 894 r = clnt_call(ysd->dom_client, YPPROC_ORDER, 895 (xdrproc_t)xdr_ypreq_nokey, &yprnk, 896 (xdrproc_t)xdr_ypresp_order, &ypro, tv); 897 898 /* 899 * NIS+ in YP compat mode doesn't support the YPPROC_ORDER 900 * procedure. 901 */ 902 if (r == RPC_PROCUNAVAIL) { 903 return(YPERR_YPERR); 904 } 905 906 if (r != RPC_SUCCESS) { 907 clnt_perror(ysd->dom_client, "yp_order: clnt_call"); 908 _yp_unbind(ysd); 909 goto again; 910 } 911 912 if (!(r = ypprot_err(ypro.stat))) { 913 *outorder = ypro.ordernum; 914 } 915 916 xdr_free((xdrproc_t)xdr_ypresp_order, &ypro); 917 return (r); 918 } 919 920 int 921 yp_master(char *indomain, char *inmap, char **outname) 922 { 923 struct dom_binding *ysd; 924 struct ypresp_master yprm; 925 struct ypreq_nokey yprnk; 926 struct timeval tv; 927 int r; 928 929 /* Sanity check */ 930 931 if (indomain == NULL || !strlen(indomain) || 932 inmap == NULL || !strlen(inmap)) 933 return (YPERR_BADARGS); 934 again: 935 if (_yp_dobind(indomain, &ysd) != 0) 936 return (YPERR_DOMAIN); 937 938 tv.tv_sec = _yplib_timeout; 939 tv.tv_usec = 0; 940 941 yprnk.domain = indomain; 942 yprnk.map = inmap; 943 944 bzero((char *)&yprm, sizeof yprm); 945 946 r = clnt_call(ysd->dom_client, YPPROC_MASTER, 947 (xdrproc_t)xdr_ypreq_nokey, &yprnk, 948 (xdrproc_t)xdr_ypresp_master, &yprm, tv); 949 if (r != RPC_SUCCESS) { 950 clnt_perror(ysd->dom_client, "yp_master: clnt_call"); 951 _yp_unbind(ysd); 952 goto again; 953 } 954 955 if (!(r = ypprot_err(yprm.stat))) { 956 *outname = (char *)strdup(yprm.peer); 957 } 958 959 xdr_free((xdrproc_t)xdr_ypresp_master, &yprm); 960 return (r); 961 } 962 963 int 964 yp_maplist(char *indomain, struct ypmaplist **outmaplist) 965 { 966 struct dom_binding *ysd; 967 struct ypresp_maplist ypml; 968 struct timeval tv; 969 int r; 970 971 /* Sanity check */ 972 973 if (indomain == NULL || !strlen(indomain)) 974 return (YPERR_BADARGS); 975 976 again: 977 if (_yp_dobind(indomain, &ysd) != 0) 978 return (YPERR_DOMAIN); 979 980 tv.tv_sec = _yplib_timeout; 981 tv.tv_usec = 0; 982 983 bzero((char *)&ypml, sizeof ypml); 984 985 r = clnt_call(ysd->dom_client, YPPROC_MAPLIST, 986 (xdrproc_t)xdr_domainname, &indomain, 987 (xdrproc_t)xdr_ypresp_maplist, &ypml,tv); 988 if (r != RPC_SUCCESS) { 989 clnt_perror(ysd->dom_client, "yp_maplist: clnt_call"); 990 _yp_unbind(ysd); 991 goto again; 992 } 993 if (!(r = ypprot_err(ypml.stat))) { 994 *outmaplist = ypml.maps; 995 } 996 997 /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/ 998 return (r); 999 } 1000 1001 const char * 1002 yperr_string(int incode) 1003 { 1004 static char err[80]; 1005 1006 switch (incode) { 1007 case 0: 1008 return ("Success"); 1009 case YPERR_BADARGS: 1010 return ("Request arguments bad"); 1011 case YPERR_RPC: 1012 return ("RPC failure"); 1013 case YPERR_DOMAIN: 1014 return ("Can't bind to server which serves this domain"); 1015 case YPERR_MAP: 1016 return ("No such map in server's domain"); 1017 case YPERR_KEY: 1018 return ("No such key in map"); 1019 case YPERR_YPERR: 1020 return ("YP server error"); 1021 case YPERR_RESRC: 1022 return ("Local resource allocation failure"); 1023 case YPERR_NOMORE: 1024 return ("No more records in map database"); 1025 case YPERR_PMAP: 1026 return ("Can't communicate with portmapper"); 1027 case YPERR_YPBIND: 1028 return ("Can't communicate with ypbind"); 1029 case YPERR_YPSERV: 1030 return ("Can't communicate with ypserv"); 1031 case YPERR_NODOM: 1032 return ("Local domain name not set"); 1033 case YPERR_BADDB: 1034 return ("Server data base is bad"); 1035 case YPERR_VERS: 1036 return ("YP server version mismatch - server can't supply service."); 1037 case YPERR_ACCESS: 1038 return ("Access violation"); 1039 case YPERR_BUSY: 1040 return ("Database is busy"); 1041 } 1042 sprintf(err, "YP unknown error %d\n", incode); 1043 return (err); 1044 } 1045 1046 int 1047 ypprot_err(unsigned int incode) 1048 { 1049 switch (incode) { 1050 case YP_TRUE: 1051 return (0); 1052 case YP_FALSE: 1053 return (YPERR_YPBIND); 1054 case YP_NOMORE: 1055 return (YPERR_NOMORE); 1056 case YP_NOMAP: 1057 return (YPERR_MAP); 1058 case YP_NODOM: 1059 return (YPERR_DOMAIN); 1060 case YP_NOKEY: 1061 return (YPERR_KEY); 1062 case YP_BADOP: 1063 return (YPERR_YPERR); 1064 case YP_BADDB: 1065 return (YPERR_BADDB); 1066 case YP_YPERR: 1067 return (YPERR_YPERR); 1068 case YP_BADARGS: 1069 return (YPERR_BADARGS); 1070 case YP_VERS: 1071 return (YPERR_VERS); 1072 } 1073 return (YPERR_YPERR); 1074 } 1075 1076 int 1077 _yp_check(char **dom) 1078 { 1079 char *unused; 1080 1081 if (_yp_domain[0]=='\0') 1082 if (yp_get_default_domain(&unused)) 1083 return (0); 1084 1085 if (dom) 1086 *dom = _yp_domain; 1087 1088 if (yp_bind(_yp_domain) == 0) { 1089 yp_unbind(_yp_domain); 1090 return (1); 1091 } 1092 return (0); 1093 } 1094