14415cd19SGarrett Wollman /* 24415cd19SGarrett Wollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 34e6ace08SBill Paul * Copyright (c) 1998 Bill Paul <wpaul@ctr.columbia.edu> 44415cd19SGarrett Wollman * All rights reserved. 54415cd19SGarrett Wollman * 64415cd19SGarrett Wollman * Redistribution and use in source and binary forms, with or without 74415cd19SGarrett Wollman * modification, are permitted provided that the following conditions 84415cd19SGarrett Wollman * are met: 94415cd19SGarrett Wollman * 1. Redistributions of source code must retain the above copyright 104415cd19SGarrett Wollman * notice, this list of conditions and the following disclaimer. 114415cd19SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 124415cd19SGarrett Wollman * notice, this list of conditions and the following disclaimer in the 134415cd19SGarrett Wollman * documentation and/or other materials provided with the distribution. 144415cd19SGarrett Wollman * 3. The name of the author may not be used to endorse or promote 154415cd19SGarrett Wollman * products derived from this software without specific prior written 164415cd19SGarrett Wollman * permission. 174415cd19SGarrett Wollman * 184415cd19SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 194415cd19SGarrett Wollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 204415cd19SGarrett Wollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214415cd19SGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 224415cd19SGarrett Wollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 234415cd19SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 244415cd19SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 254415cd19SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 264415cd19SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 274415cd19SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 284415cd19SGarrett Wollman * SUCH DAMAGE. 294415cd19SGarrett Wollman */ 304415cd19SGarrett Wollman 31333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 32333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 334415cd19SGarrett Wollman 34d201fe46SDaniel Eischen #include "namespace.h" 354415cd19SGarrett Wollman #include <sys/param.h> 364415cd19SGarrett Wollman #include <sys/types.h> 374415cd19SGarrett Wollman #include <sys/socket.h> 384415cd19SGarrett Wollman #include <sys/file.h> 394415cd19SGarrett Wollman #include <sys/uio.h> 40fd8e4ebcSMike Barcroft #include <arpa/inet.h> 414415cd19SGarrett Wollman #include <errno.h> 424415cd19SGarrett Wollman #include <stdio.h> 434415cd19SGarrett Wollman #include <string.h> 44b9729ac2SBill Paul #include <stdlib.h> 45b9729ac2SBill Paul #include <unistd.h> 464415cd19SGarrett Wollman #include <rpc/rpc.h> 474415cd19SGarrett Wollman #include <rpc/xdr.h> 480e276383SBill Paul #include <rpcsvc/yp.h> 49d201fe46SDaniel Eischen #include "un-namespace.h" 50d201fe46SDaniel Eischen #include "libc_private.h" 510e276383SBill Paul 520e276383SBill Paul /* 530e276383SBill Paul * We have to define these here due to clashes between yp_prot.h and 540e276383SBill Paul * yp.h. 550e276383SBill Paul */ 560e276383SBill Paul 574e6ace08SBill Paul #define YPMATCHCACHE 584e6ace08SBill Paul 594e6ace08SBill Paul #ifdef YPMATCHCACHE 604e6ace08SBill Paul struct ypmatch_ent { 614e6ace08SBill Paul char *ypc_map; 624e6ace08SBill Paul keydat ypc_key; 634e6ace08SBill Paul valdat ypc_val; 644e6ace08SBill Paul time_t ypc_expire_t; 654e6ace08SBill Paul struct ypmatch_ent *ypc_next; 664e6ace08SBill Paul }; 674e6ace08SBill Paul #define YPLIB_MAXCACHE 5 /* At most 5 entries */ 684e6ace08SBill Paul #define YPLIB_EXPIRE 5 /* Expire after 5 seconds */ 694e6ace08SBill Paul #endif 704e6ace08SBill Paul 710e276383SBill Paul struct dom_binding { 720e276383SBill Paul struct dom_binding *dom_pnext; 730e276383SBill Paul char dom_domain[YPMAXDOMAIN + 1]; 740e276383SBill Paul struct sockaddr_in dom_server_addr; 750e276383SBill Paul u_short dom_server_port; 760e276383SBill Paul int dom_socket; 770e276383SBill Paul CLIENT *dom_client; 78a2308772SBill Paul u_short dom_local_port; /* now I finally know what this is for. */ 790e276383SBill Paul long dom_vers; 804e6ace08SBill Paul #ifdef YPMATCHCACHE 814e6ace08SBill Paul struct ypmatch_ent *cache; 824e6ace08SBill Paul int ypmatch_cachecnt; 834e6ace08SBill Paul #endif 840e276383SBill Paul }; 850e276383SBill Paul 864415cd19SGarrett Wollman #include <rpcsvc/ypclnt.h> 874415cd19SGarrett Wollman 884415cd19SGarrett Wollman #ifndef BINDINGDIR 894415cd19SGarrett Wollman #define BINDINGDIR "/var/yp/binding" 904415cd19SGarrett Wollman #endif 91456ebbf8SBill Paul #define MAX_RETRIES 20 924415cd19SGarrett Wollman 934415cd19SGarrett Wollman extern bool_t xdr_domainname(), xdr_ypbind_resp(); 944415cd19SGarrett Wollman extern bool_t xdr_ypreq_key(), xdr_ypresp_val(); 954415cd19SGarrett Wollman extern bool_t xdr_ypreq_nokey(), xdr_ypresp_key_val(); 964415cd19SGarrett Wollman extern bool_t xdr_ypresp_all(), xdr_ypresp_all_seq(); 974415cd19SGarrett Wollman extern bool_t xdr_ypresp_master(); 984415cd19SGarrett Wollman 994415cd19SGarrett Wollman int (*ypresp_allfn)(); 1004415cd19SGarrett Wollman void *ypresp_data; 1014415cd19SGarrett Wollman 102ed4d1c46SDag-Erling Smørgrav static void _yp_unbind(struct dom_binding *); 1034415cd19SGarrett Wollman struct dom_binding *_ypbindlist; 1044415cd19SGarrett Wollman static char _yp_domain[MAXHOSTNAMELEN]; 1054415cd19SGarrett Wollman int _yplib_timeout = 10; 1064415cd19SGarrett Wollman 1074415cd19SGarrett Wollman #ifdef YPMATCHCACHE 108dc584ddbSDag-Erling Smørgrav static void 109dc584ddbSDag-Erling Smørgrav ypmatch_cache_delete(struct dom_binding *ypdb, struct ypmatch_ent *prev, 110dc584ddbSDag-Erling Smørgrav struct ypmatch_ent *cur) 1114415cd19SGarrett Wollman { 1124e6ace08SBill Paul if (prev == NULL) 1134e6ace08SBill Paul ypdb->cache = cur->ypc_next; 1144e6ace08SBill Paul else 1154e6ace08SBill Paul prev->ypc_next = cur->ypc_next; 1164e6ace08SBill Paul 1174e6ace08SBill Paul free(cur->ypc_map); 1184e6ace08SBill Paul free(cur->ypc_key.keydat_val); 1194e6ace08SBill Paul free(cur->ypc_val.valdat_val); 1204e6ace08SBill Paul free(cur); 1214e6ace08SBill Paul 1224e6ace08SBill Paul ypdb->ypmatch_cachecnt--; 1234e6ace08SBill Paul 1244e6ace08SBill Paul return; 1254e6ace08SBill Paul } 1264e6ace08SBill Paul 127dc584ddbSDag-Erling Smørgrav static void 128dc584ddbSDag-Erling Smørgrav ypmatch_cache_flush(struct dom_binding *ypdb) 1294e6ace08SBill Paul { 1304e6ace08SBill Paul struct ypmatch_ent *n, *c = ypdb->cache; 1314e6ace08SBill Paul 1324e6ace08SBill Paul while (c != NULL) { 1334e6ace08SBill Paul n = c->ypc_next; 1344e6ace08SBill Paul ypmatch_cache_delete(ypdb, NULL, c); 1354e6ace08SBill Paul c = n; 1364e6ace08SBill Paul } 1374e6ace08SBill Paul 1384e6ace08SBill Paul return; 1394e6ace08SBill Paul } 1404e6ace08SBill Paul 141dc584ddbSDag-Erling Smørgrav static void 142dc584ddbSDag-Erling Smørgrav ypmatch_cache_expire(struct dom_binding *ypdb) 1434e6ace08SBill Paul { 1444e6ace08SBill Paul struct ypmatch_ent *c = ypdb->cache; 1454e6ace08SBill Paul struct ypmatch_ent *n, *p = NULL; 1464415cd19SGarrett Wollman time_t t; 1474415cd19SGarrett Wollman 1484415cd19SGarrett Wollman time(&t); 1494415cd19SGarrett Wollman 1504e6ace08SBill Paul while (c != NULL) { 1514e6ace08SBill Paul if (t >= c->ypc_expire_t) { 1524e6ace08SBill Paul n = c->ypc_next; 1534e6ace08SBill Paul ypmatch_cache_delete(ypdb, p, c); 1544e6ace08SBill Paul c = n; 1554415cd19SGarrett Wollman } else { 1564e6ace08SBill Paul p = c; 1574e6ace08SBill Paul c = c->ypc_next; 1584e6ace08SBill Paul } 1594415cd19SGarrett Wollman } 1604415cd19SGarrett Wollman 1614e6ace08SBill Paul return; 1624415cd19SGarrett Wollman } 1634415cd19SGarrett Wollman 164dc584ddbSDag-Erling Smørgrav static void 165dc584ddbSDag-Erling Smørgrav ypmatch_cache_insert(struct dom_binding *ypdb, char *map, keydat *key, 166dc584ddbSDag-Erling Smørgrav valdat *val) 1674415cd19SGarrett Wollman { 1684e6ace08SBill Paul struct ypmatch_ent *new; 1694415cd19SGarrett Wollman 1704e6ace08SBill Paul /* Do an expire run to maybe open up a slot. */ 1714e6ace08SBill Paul if (ypdb->ypmatch_cachecnt) 1724e6ace08SBill Paul ypmatch_cache_expire(ypdb); 1734415cd19SGarrett Wollman 1744e6ace08SBill Paul /* 1754e6ace08SBill Paul * If there are no slots free, then force an expire of 1764e6ace08SBill Paul * the least recently used entry. 1774e6ace08SBill Paul */ 1784e6ace08SBill Paul if (ypdb->ypmatch_cachecnt >= YPLIB_MAXCACHE) { 1794e6ace08SBill Paul struct ypmatch_ent *o = NULL, *c = ypdb->cache; 1804e6ace08SBill Paul time_t oldest = 0; 1814415cd19SGarrett Wollman 1824e6ace08SBill Paul oldest = ~oldest; 1834415cd19SGarrett Wollman 1844e6ace08SBill Paul while (c != NULL) { 1854e6ace08SBill Paul if (c->ypc_expire_t < oldest) { 1864e6ace08SBill Paul oldest = c->ypc_expire_t; 1874e6ace08SBill Paul o = c; 1884415cd19SGarrett Wollman } 1894e6ace08SBill Paul c = c->ypc_next; 1904e6ace08SBill Paul } 1914e6ace08SBill Paul 1924e6ace08SBill Paul if (o == NULL) 1934e6ace08SBill Paul return; 1944e6ace08SBill Paul o->ypc_expire_t = 0; 1954e6ace08SBill Paul ypmatch_cache_expire(ypdb); 1964e6ace08SBill Paul } 1974e6ace08SBill Paul 1984e6ace08SBill Paul new = malloc(sizeof(struct ypmatch_ent)); 1994e6ace08SBill Paul if (new == NULL) 2004e6ace08SBill Paul return; 2014e6ace08SBill Paul 2024e6ace08SBill Paul new->ypc_map = strdup(map); 2034e6ace08SBill Paul if (new->ypc_map == NULL) { 2044e6ace08SBill Paul free(new); 2054e6ace08SBill Paul return; 2064e6ace08SBill Paul } 2074e6ace08SBill Paul new->ypc_key.keydat_val = malloc(key->keydat_len); 2084e6ace08SBill Paul if (new->ypc_key.keydat_val == NULL) { 2094e6ace08SBill Paul free(new->ypc_map); 2104e6ace08SBill Paul free(new); 2114e6ace08SBill Paul return; 2124e6ace08SBill Paul } 2134e6ace08SBill Paul new->ypc_val.valdat_val = malloc(val->valdat_len); 2144e6ace08SBill Paul if (new->ypc_val.valdat_val == NULL) { 2154e6ace08SBill Paul free(new->ypc_val.valdat_val); 2164e6ace08SBill Paul free(new->ypc_map); 2174e6ace08SBill Paul free(new); 2184e6ace08SBill Paul return; 2194e6ace08SBill Paul } 2204e6ace08SBill Paul 2214e6ace08SBill Paul new->ypc_expire_t = time(NULL) + YPLIB_EXPIRE; 2224e6ace08SBill Paul new->ypc_key.keydat_len = key->keydat_len; 2234e6ace08SBill Paul new->ypc_val.valdat_len = val->valdat_len; 2244e6ace08SBill Paul bcopy(key->keydat_val, new->ypc_key.keydat_val, key->keydat_len); 2254e6ace08SBill Paul bcopy(val->valdat_val, new->ypc_val.valdat_val, val->valdat_len); 2264e6ace08SBill Paul 2274e6ace08SBill Paul new->ypc_next = ypdb->cache; 2284e6ace08SBill Paul ypdb->cache = new; 2294e6ace08SBill Paul 2304e6ace08SBill Paul ypdb->ypmatch_cachecnt++; 2314e6ace08SBill Paul 2324e6ace08SBill Paul return; 2334e6ace08SBill Paul } 2344e6ace08SBill Paul 235dc584ddbSDag-Erling Smørgrav static bool_t 236dc584ddbSDag-Erling Smørgrav ypmatch_cache_lookup(struct dom_binding *ypdb, char *map, keydat *key, 237dc584ddbSDag-Erling Smørgrav valdat *val) 2384e6ace08SBill Paul { 2394e6ace08SBill Paul struct ypmatch_ent *c = ypdb->cache; 2404e6ace08SBill Paul 2414e6ace08SBill Paul ypmatch_cache_expire(ypdb); 2424e6ace08SBill Paul 2434e6ace08SBill Paul for (c = ypdb->cache; c != NULL; c = c->ypc_next) { 2444e6ace08SBill Paul if (strcmp(map, c->ypc_map)) 2454e6ace08SBill Paul continue; 2464e6ace08SBill Paul if (key->keydat_len != c->ypc_key.keydat_len) 2474e6ace08SBill Paul continue; 2484e6ace08SBill Paul if (bcmp(key->keydat_val, c->ypc_key.keydat_val, 2494e6ace08SBill Paul key->keydat_len)) 2504e6ace08SBill Paul continue; 2514e6ace08SBill Paul } 2524e6ace08SBill Paul 2534e6ace08SBill Paul if (c == NULL) 2544e6ace08SBill Paul return(FALSE); 2554e6ace08SBill Paul 2564e6ace08SBill Paul val->valdat_len = c->ypc_val.valdat_len; 2574e6ace08SBill Paul val->valdat_val = c->ypc_val.valdat_val; 2584e6ace08SBill Paul 2594e6ace08SBill Paul return(TRUE); 2604415cd19SGarrett Wollman } 2614415cd19SGarrett Wollman #endif 2624415cd19SGarrett Wollman 263f249dbccSDag-Erling Smørgrav const char * 264dc584ddbSDag-Erling Smørgrav ypbinderr_string(int incode) 26564416168SBill Paul { 26664416168SBill Paul static char err[80]; 26764416168SBill Paul switch (incode) { 26864416168SBill Paul case 0: 269ed4d1c46SDag-Erling Smørgrav return ("Success"); 27062967ca2SBill Paul case YPBIND_ERR_ERR: 271ed4d1c46SDag-Erling Smørgrav return ("Internal ypbind error"); 27262967ca2SBill Paul case YPBIND_ERR_NOSERV: 273ed4d1c46SDag-Erling Smørgrav return ("Domain not bound"); 27462967ca2SBill Paul case YPBIND_ERR_RESC: 275ed4d1c46SDag-Erling Smørgrav return ("System resource allocation failure"); 27664416168SBill Paul } 27762967ca2SBill Paul sprintf(err, "Unknown ypbind error: #%d\n", incode); 278ed4d1c46SDag-Erling Smørgrav return (err); 27964416168SBill Paul } 28064416168SBill Paul 2814415cd19SGarrett Wollman int 282dc584ddbSDag-Erling Smørgrav _yp_dobind(char *dom, struct dom_binding **ypdb) 2834415cd19SGarrett Wollman { 284e75ad74aSJames Raynard static pid_t pid = -1; 2854415cd19SGarrett Wollman char path[MAXPATHLEN]; 2864415cd19SGarrett Wollman struct dom_binding *ysd, *ysd2; 2879d80b1ddSBill Paul struct ypbind_resp ypbr; 2884415cd19SGarrett Wollman struct timeval tv; 2894415cd19SGarrett Wollman struct sockaddr_in clnt_sin; 29051295a4dSJordan K. Hubbard int clnt_sock, fd; 291ccbcef60SJames Raynard pid_t gpid; 2924415cd19SGarrett Wollman CLIENT *client; 2934415cd19SGarrett Wollman int new = 0, r; 294456ebbf8SBill Paul int retries = 0; 295a2308772SBill Paul struct sockaddr_in check; 296a2308772SBill Paul int checklen = sizeof(struct sockaddr_in); 2974415cd19SGarrett Wollman 2986e8caff7SBill Paul /* Not allowed; bad doggie. Bad. */ 2996e8caff7SBill Paul if (strchr(dom, '/') != NULL) 3006e8caff7SBill Paul return(YPERR_BADARGS); 3016e8caff7SBill Paul 3024415cd19SGarrett Wollman gpid = getpid(); 3034415cd19SGarrett Wollman if (!(pid == -1 || pid == gpid)) { 3044415cd19SGarrett Wollman ysd = _ypbindlist; 3054415cd19SGarrett Wollman while (ysd) { 3069fa75c15SBill Paul if (ysd->dom_client != NULL) 307cc64a2bfSBill Paul _yp_unbind(ysd); 3084415cd19SGarrett Wollman ysd2 = ysd->dom_pnext; 3094415cd19SGarrett Wollman free(ysd); 3104415cd19SGarrett Wollman ysd = ysd2; 3114415cd19SGarrett Wollman } 3124415cd19SGarrett Wollman _ypbindlist = NULL; 3134415cd19SGarrett Wollman } 3144415cd19SGarrett Wollman pid = gpid; 3154415cd19SGarrett Wollman 3164415cd19SGarrett Wollman if (ypdb != NULL) 3174415cd19SGarrett Wollman *ypdb = NULL; 3184415cd19SGarrett Wollman 3194415cd19SGarrett Wollman if (dom == NULL || strlen(dom) == 0) 320ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 3214415cd19SGarrett Wollman 3224415cd19SGarrett Wollman for (ysd = _ypbindlist; ysd; ysd = ysd->dom_pnext) 3234415cd19SGarrett Wollman if (strcmp(dom, ysd->dom_domain) == 0) 3244415cd19SGarrett Wollman break; 325a169c9b1SBill Paul 326a2308772SBill Paul 3274415cd19SGarrett Wollman if (ysd == NULL) { 3284415cd19SGarrett Wollman ysd = (struct dom_binding *)malloc(sizeof *ysd); 3294415cd19SGarrett Wollman bzero((char *)ysd, sizeof *ysd); 3304415cd19SGarrett Wollman ysd->dom_socket = -1; 3314415cd19SGarrett Wollman ysd->dom_vers = 0; 3324415cd19SGarrett Wollman new = 1; 333a2308772SBill Paul } else { 334a2308772SBill Paul /* Check the socket -- may have been hosed by the caller. */ 335d201fe46SDaniel Eischen if (_getsockname(ysd->dom_socket, (struct sockaddr *)&check, 336a2308772SBill Paul &checklen) == -1 || check.sin_family != AF_INET || 337a2308772SBill Paul check.sin_port != ysd->dom_local_port) { 338a2308772SBill Paul /* Socket became bogus somehow... need to rebind. */ 339a2308772SBill Paul int save, sock; 340a2308772SBill Paul 341a2308772SBill Paul sock = ysd->dom_socket; 342d201fe46SDaniel Eischen save = _dup(ysd->dom_socket); 3439fa75c15SBill Paul if (ysd->dom_client != NULL) 344a2308772SBill Paul clnt_destroy(ysd->dom_client); 345a2308772SBill Paul ysd->dom_vers = 0; 346a2308772SBill Paul ysd->dom_client = NULL; 347d201fe46SDaniel Eischen sock = _dup2(save, sock); 3489233c4d9SJason Evans _close(save); 349a2308772SBill Paul } 3504415cd19SGarrett Wollman } 3510f98415eSBill Paul 3524415cd19SGarrett Wollman again: 353456ebbf8SBill Paul retries++; 354456ebbf8SBill Paul if (retries > MAX_RETRIES) { 355456ebbf8SBill Paul if (new) 356456ebbf8SBill Paul free(ysd); 357456ebbf8SBill Paul return(YPERR_YPBIND); 358456ebbf8SBill Paul } 3594415cd19SGarrett Wollman #ifdef BINDINGDIR 3604415cd19SGarrett Wollman if (ysd->dom_vers == 0) { 361a169c9b1SBill Paul /* 362a169c9b1SBill Paul * We're trying to make a new binding: zorch the 363a169c9b1SBill Paul * existing handle now (if any). 364a169c9b1SBill Paul */ 3659fa75c15SBill Paul if (ysd->dom_client != NULL) { 366a169c9b1SBill Paul clnt_destroy(ysd->dom_client); 367a169c9b1SBill Paul ysd->dom_client = NULL; 368a2308772SBill Paul ysd->dom_socket = -1; 369a169c9b1SBill Paul } 37083ac6cdcSKris Kennaway snprintf(path, sizeof(path), "%s/%s.%d", BINDINGDIR, dom, 2); 3719233c4d9SJason Evans if ((fd = _open(path, O_RDONLY)) == -1) { 3724415cd19SGarrett Wollman /* no binding file, YP is dead. */ 373456ebbf8SBill Paul /* Try to bring it back to life. */ 3749233c4d9SJason Evans _close(fd); 375b9729ac2SBill Paul goto skipit; 3764415cd19SGarrett Wollman } 377d201fe46SDaniel Eischen if (_flock(fd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) { 3784415cd19SGarrett Wollman struct iovec iov[2]; 3799d80b1ddSBill Paul struct ypbind_resp ybr; 3804415cd19SGarrett Wollman u_short ypb_port; 3814415cd19SGarrett Wollman 3824415cd19SGarrett Wollman iov[0].iov_base = (caddr_t)&ypb_port; 3834415cd19SGarrett Wollman iov[0].iov_len = sizeof ypb_port; 3844415cd19SGarrett Wollman iov[1].iov_base = (caddr_t)&ybr; 3854415cd19SGarrett Wollman iov[1].iov_len = sizeof ybr; 3864415cd19SGarrett Wollman 387d201fe46SDaniel Eischen r = _readv(fd, iov, 2); 3884415cd19SGarrett Wollman if (r != iov[0].iov_len + iov[1].iov_len) { 3899233c4d9SJason Evans _close(fd); 3904415cd19SGarrett Wollman ysd->dom_vers = -1; 3914415cd19SGarrett Wollman goto again; 3924415cd19SGarrett Wollman } 3934415cd19SGarrett Wollman 3944415cd19SGarrett Wollman bzero(&ysd->dom_server_addr, sizeof ysd->dom_server_addr); 3954415cd19SGarrett Wollman ysd->dom_server_addr.sin_family = AF_INET; 3964415cd19SGarrett Wollman ysd->dom_server_addr.sin_len = sizeof(struct sockaddr_in); 3977b5f5922SJohn Baldwin bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 3987b5f5922SJohn Baldwin &ysd->dom_server_addr.sin_addr.s_addr, 3997b5f5922SJohn Baldwin sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 4007b5f5922SJohn Baldwin bcopy(&ybr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port, 4017b5f5922SJohn Baldwin &ysd->dom_server_addr.sin_port, 4027b5f5922SJohn Baldwin sizeof(ysd->dom_server_addr.sin_port)); 4034415cd19SGarrett Wollman 4044415cd19SGarrett Wollman ysd->dom_server_port = ysd->dom_server_addr.sin_port; 4059233c4d9SJason Evans _close(fd); 4064415cd19SGarrett Wollman goto gotit; 4074415cd19SGarrett Wollman } else { 4084415cd19SGarrett Wollman /* no lock on binding file, YP is dead. */ 409456ebbf8SBill Paul /* Try to bring it back to life. */ 4109233c4d9SJason Evans _close(fd); 411456ebbf8SBill Paul goto skipit; 412f067dfeaSBill Paul } 413f067dfeaSBill Paul } 414b9729ac2SBill Paul skipit: 4154415cd19SGarrett Wollman #endif 4164415cd19SGarrett Wollman if (ysd->dom_vers == -1 || ysd->dom_vers == 0) { 417a169c9b1SBill Paul /* 418a169c9b1SBill Paul * We're trying to make a new binding: zorch the 419a169c9b1SBill Paul * existing handle now (if any). 420a169c9b1SBill Paul */ 4219fa75c15SBill Paul if (ysd->dom_client != NULL) { 422a169c9b1SBill Paul clnt_destroy(ysd->dom_client); 423a169c9b1SBill Paul ysd->dom_client = NULL; 424a2308772SBill Paul ysd->dom_socket = -1; 425a169c9b1SBill Paul } 4264415cd19SGarrett Wollman bzero((char *)&clnt_sin, sizeof clnt_sin); 4274415cd19SGarrett Wollman clnt_sin.sin_family = AF_INET; 4284415cd19SGarrett Wollman clnt_sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 4294415cd19SGarrett Wollman 4304415cd19SGarrett Wollman clnt_sock = RPC_ANYSOCK; 4314415cd19SGarrett Wollman client = clnttcp_create(&clnt_sin, YPBINDPROG, YPBINDVERS, &clnt_sock, 4324415cd19SGarrett Wollman 0, 0); 4334415cd19SGarrett Wollman if (client == NULL) { 43420e4b00aSBill Paul /* 43520e4b00aSBill Paul * These conditions indicate ypbind just isn't 43620e4b00aSBill Paul * alive -- we probably don't want to shoot our 437a169c9b1SBill Paul * mouth off in this case; instead generate error 43820e4b00aSBill Paul * messages only for really exotic problems. 43920e4b00aSBill Paul */ 44020e4b00aSBill Paul if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED && 44120e4b00aSBill Paul (rpc_createerr.cf_stat != RPC_SYSTEMERROR && 44220e4b00aSBill Paul rpc_createerr.cf_error.re_errno == ECONNREFUSED)) 4434415cd19SGarrett Wollman clnt_pcreateerror("clnttcp_create"); 4444415cd19SGarrett Wollman if (new) 4454415cd19SGarrett Wollman free(ysd); 446456ebbf8SBill Paul return (YPERR_YPBIND); 4474415cd19SGarrett Wollman } 4484415cd19SGarrett Wollman 44956d18edaSBill Paul /* 45056d18edaSBill Paul * Check the port number -- should be < IPPORT_RESERVED. 45156d18edaSBill Paul * If not, it's possible someone has registered a bogus 45256d18edaSBill Paul * ypbind with the portmapper and is trying to trick us. 45356d18edaSBill Paul */ 45456d18edaSBill Paul if (ntohs(clnt_sin.sin_port) >= IPPORT_RESERVED) { 4559fa75c15SBill Paul if (client != NULL) 45656d18edaSBill Paul clnt_destroy(client); 45756d18edaSBill Paul if (new) 45856d18edaSBill Paul free(ysd); 45956d18edaSBill Paul return(YPERR_YPBIND); 46056d18edaSBill Paul } 461456ebbf8SBill Paul tv.tv_sec = _yplib_timeout/2; 4624415cd19SGarrett Wollman tv.tv_usec = 0; 4634415cd19SGarrett Wollman r = clnt_call(client, YPBINDPROC_DOMAIN, 464f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_domainname, &dom, 465f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypbind_resp, &ypbr, tv); 4664415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 4674415cd19SGarrett Wollman clnt_destroy(client); 4684415cd19SGarrett Wollman ysd->dom_vers = -1; 469a2308772SBill Paul if (r == RPC_PROGUNAVAIL || r == RPC_PROCUNAVAIL) { 470a2308772SBill Paul if (new) 471a2308772SBill Paul free(ysd); 47220e4b00aSBill Paul return(YPERR_YPBIND); 473a2308772SBill Paul } 47420e4b00aSBill Paul fprintf(stderr, 47520e4b00aSBill Paul "YP: server for domain %s not responding, retrying\n", dom); 4764415cd19SGarrett Wollman goto again; 47764416168SBill Paul } else { 47864416168SBill Paul if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 4799233c4d9SJason Evans struct timespec time_to_sleep, time_remaining; 4809233c4d9SJason Evans 481456ebbf8SBill Paul clnt_destroy(client); 482456ebbf8SBill Paul ysd->dom_vers = -1; 4839233c4d9SJason Evans 4849233c4d9SJason Evans time_to_sleep.tv_sec = _yplib_timeout/2; 4859233c4d9SJason Evans time_to_sleep.tv_nsec = 0; 4869233c4d9SJason Evans _nanosleep(&time_to_sleep, 4879233c4d9SJason Evans &time_remaining); 488456ebbf8SBill Paul goto again; 48964416168SBill Paul } 4904415cd19SGarrett Wollman } 4914415cd19SGarrett Wollman clnt_destroy(client); 4924415cd19SGarrett Wollman 4934415cd19SGarrett Wollman bzero((char *)&ysd->dom_server_addr, sizeof ysd->dom_server_addr); 4944415cd19SGarrett Wollman ysd->dom_server_addr.sin_family = AF_INET; 4957b5f5922SJohn Baldwin bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port, 4967b5f5922SJohn Baldwin &ysd->dom_server_addr.sin_port, 4977b5f5922SJohn Baldwin sizeof(ysd->dom_server_addr.sin_port)); 4987b5f5922SJohn Baldwin bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 4997b5f5922SJohn Baldwin &ysd->dom_server_addr.sin_addr.s_addr, 5007b5f5922SJohn Baldwin sizeof(ysd->dom_server_addr.sin_addr.s_addr)); 50156d18edaSBill Paul 50256d18edaSBill Paul /* 50356d18edaSBill Paul * We could do a reserved port check here too, but this 50456d18edaSBill Paul * could pose compatibility problems. The local ypbind is 50556d18edaSBill Paul * supposed to decide whether or not to trust yp servers 50656d18edaSBill Paul * on insecure ports. For now, we trust its judgement. 50756d18edaSBill Paul */ 5084415cd19SGarrett Wollman ysd->dom_server_port = 5099d80b1ddSBill Paul *(u_short *)&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_port; 5104415cd19SGarrett Wollman gotit: 5114415cd19SGarrett Wollman ysd->dom_vers = YPVERS; 512d0509082SJacques Vidrine strlcpy(ysd->dom_domain, dom, sizeof(ysd->dom_domain)); 5134415cd19SGarrett Wollman } 5144415cd19SGarrett Wollman 515a169c9b1SBill Paul /* Don't rebuild the connection to the server unless we have to. */ 516a169c9b1SBill Paul if (ysd->dom_client == NULL) { 5174415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout/2; 5184415cd19SGarrett Wollman tv.tv_usec = 0; 5194415cd19SGarrett Wollman ysd->dom_socket = RPC_ANYSOCK; 5204e6ace08SBill Paul ysd->dom_client = clntudp_bufcreate(&ysd->dom_server_addr, 5214e6ace08SBill Paul YPPROG, YPVERS, tv, &ysd->dom_socket, 1280, 2304); 5224415cd19SGarrett Wollman if (ysd->dom_client == NULL) { 5234415cd19SGarrett Wollman clnt_pcreateerror("clntudp_create"); 5244415cd19SGarrett Wollman ysd->dom_vers = -1; 5254415cd19SGarrett Wollman goto again; 5264415cd19SGarrett Wollman } 5279233c4d9SJason Evans if (_fcntl(ysd->dom_socket, F_SETFD, 1) == -1) 5284415cd19SGarrett Wollman perror("fcntl: F_SETFD"); 529a2308772SBill Paul /* 530a2308772SBill Paul * We want a port number associated with this socket 531a2308772SBill Paul * so that we can check its authenticity later. 532a2308772SBill Paul */ 533a2308772SBill Paul checklen = sizeof(struct sockaddr_in); 534a2308772SBill Paul bzero((char *)&check, checklen); 535d201fe46SDaniel Eischen _bind(ysd->dom_socket, (struct sockaddr *)&check, checklen); 536a2308772SBill Paul check.sin_family = AF_INET; 537d201fe46SDaniel Eischen if (!_getsockname(ysd->dom_socket, 538a2308772SBill Paul (struct sockaddr *)&check, &checklen)) { 539a2308772SBill Paul ysd->dom_local_port = check.sin_port; 540a2308772SBill Paul } else { 541a2308772SBill Paul clnt_destroy(ysd->dom_client); 542a2308772SBill Paul if (new) 543a2308772SBill Paul free(ysd); 544a2308772SBill Paul return(YPERR_YPBIND); 545a2308772SBill Paul } 546a169c9b1SBill Paul } 5474415cd19SGarrett Wollman 5484415cd19SGarrett Wollman if (new) { 5494415cd19SGarrett Wollman ysd->dom_pnext = _ypbindlist; 5504415cd19SGarrett Wollman _ypbindlist = ysd; 5514415cd19SGarrett Wollman } 5524415cd19SGarrett Wollman 5534415cd19SGarrett Wollman if (ypdb != NULL) 5544415cd19SGarrett Wollman *ypdb = ysd; 555ed4d1c46SDag-Erling Smørgrav return (0); 5564415cd19SGarrett Wollman } 5574415cd19SGarrett Wollman 5584415cd19SGarrett Wollman static void 559dc584ddbSDag-Erling Smørgrav _yp_unbind(struct dom_binding *ypb) 5604415cd19SGarrett Wollman { 5619fa75c15SBill Paul struct sockaddr_in check; 5629fa75c15SBill Paul int checklen = sizeof(struct sockaddr_in); 5639fa75c15SBill Paul 5649fa75c15SBill Paul if (ypb->dom_client != NULL) { 5659fa75c15SBill Paul /* Check the socket -- may have been hosed by the caller. */ 566d201fe46SDaniel Eischen if (_getsockname(ypb->dom_socket, (struct sockaddr *)&check, 5679fa75c15SBill Paul &checklen) == -1 || check.sin_family != AF_INET || 5689fa75c15SBill Paul check.sin_port != ypb->dom_local_port) { 5699fa75c15SBill Paul int save, sock; 5709fa75c15SBill Paul 5719fa75c15SBill Paul sock = ypb->dom_socket; 572d201fe46SDaniel Eischen save = _dup(ypb->dom_socket); 5734415cd19SGarrett Wollman clnt_destroy(ypb->dom_client); 574d201fe46SDaniel Eischen sock = _dup2(save, sock); 5759233c4d9SJason Evans _close(save); 5769fa75c15SBill Paul } else 5779fa75c15SBill Paul clnt_destroy(ypb->dom_client); 5789fa75c15SBill Paul } 5799fa75c15SBill Paul 5804415cd19SGarrett Wollman ypb->dom_client = NULL; 5814415cd19SGarrett Wollman ypb->dom_socket = -1; 5822694f9b9SBill Paul ypb->dom_vers = -1; 5834e6ace08SBill Paul #ifdef YPMATCHCACHE 5844e6ace08SBill Paul ypmatch_cache_flush(ypb); 5854e6ace08SBill Paul #endif 5864415cd19SGarrett Wollman } 5874415cd19SGarrett Wollman 5884415cd19SGarrett Wollman int 589dc584ddbSDag-Erling Smørgrav yp_bind(char *dom) 5904415cd19SGarrett Wollman { 591ed4d1c46SDag-Erling Smørgrav return (_yp_dobind(dom, NULL)); 5924415cd19SGarrett Wollman } 5934415cd19SGarrett Wollman 5944415cd19SGarrett Wollman void 595dc584ddbSDag-Erling Smørgrav yp_unbind(char *dom) 5964415cd19SGarrett Wollman { 5974415cd19SGarrett Wollman struct dom_binding *ypb, *ypbp; 5984415cd19SGarrett Wollman 5994415cd19SGarrett Wollman ypbp = NULL; 6004415cd19SGarrett Wollman for (ypb = _ypbindlist; ypb; ypb = ypb->dom_pnext) { 6014415cd19SGarrett Wollman if (strcmp(dom, ypb->dom_domain) == 0) { 6022694f9b9SBill Paul _yp_unbind(ypb); 6034415cd19SGarrett Wollman if (ypbp) 6044415cd19SGarrett Wollman ypbp->dom_pnext = ypb->dom_pnext; 6054415cd19SGarrett Wollman else 6064415cd19SGarrett Wollman _ypbindlist = ypb->dom_pnext; 6074415cd19SGarrett Wollman free(ypb); 6084415cd19SGarrett Wollman return; 6094415cd19SGarrett Wollman } 6104415cd19SGarrett Wollman ypbp = ypb; 6114415cd19SGarrett Wollman } 6124415cd19SGarrett Wollman return; 6134415cd19SGarrett Wollman } 6144415cd19SGarrett Wollman 6154415cd19SGarrett Wollman int 616dc584ddbSDag-Erling Smørgrav yp_match(char *indomain, char *inmap, const char *inkey, int inkeylen, 617dc584ddbSDag-Erling Smørgrav char **outval, int *outvallen) 6184415cd19SGarrett Wollman { 6194415cd19SGarrett Wollman struct dom_binding *ysd; 6204415cd19SGarrett Wollman struct ypresp_val yprv; 6214415cd19SGarrett Wollman struct timeval tv; 6224415cd19SGarrett Wollman struct ypreq_key yprk; 6234415cd19SGarrett Wollman int r; 6244415cd19SGarrett Wollman 6254415cd19SGarrett Wollman *outval = NULL; 6264415cd19SGarrett Wollman *outvallen = 0; 6274415cd19SGarrett Wollman 62806643071SBill Paul /* Sanity check */ 629e17334c3SBill Paul 63006643071SBill Paul if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 63106643071SBill Paul inmap == NULL || !strlen(inmap) || 63206643071SBill Paul indomain == NULL || !strlen(indomain)) 633ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 634e17334c3SBill Paul 6354e6ace08SBill Paul if (_yp_dobind(indomain, &ysd) != 0) 6364e6ace08SBill Paul return(YPERR_DOMAIN); 6374e6ace08SBill Paul 6384e6ace08SBill Paul yprk.domain = indomain; 6394e6ace08SBill Paul yprk.map = inmap; 6404e6ace08SBill Paul yprk.key.keydat_val = (char *)inkey; 6414e6ace08SBill Paul yprk.key.keydat_len = inkeylen; 6424e6ace08SBill Paul 6434415cd19SGarrett Wollman #ifdef YPMATCHCACHE 6444e6ace08SBill Paul if (ypmatch_cache_lookup(ysd, yprk.map, &yprk.key, &yprv.val) == TRUE) { 6454e6ace08SBill Paul /* 6464415cd19SGarrett Wollman if (!strcmp(_yp_domain, indomain) && ypmatch_find(inmap, inkey, 6470e276383SBill Paul inkeylen, &yprv.val.valdat_val, &yprv.val.valdat_len)) { 6484e6ace08SBill Paul */ 6490e276383SBill Paul *outvallen = yprv.val.valdat_len; 6504415cd19SGarrett Wollman *outval = (char *)malloc(*outvallen+1); 6510e276383SBill Paul bcopy(yprv.val.valdat_val, *outval, *outvallen); 6524415cd19SGarrett Wollman (*outval)[*outvallen] = '\0'; 653ed4d1c46SDag-Erling Smørgrav return (0); 6544415cd19SGarrett Wollman } 6554415cd19SGarrett Wollman #endif 6564415cd19SGarrett Wollman 6574cc738f7SBill Paul again: 6584cc738f7SBill Paul if (_yp_dobind(indomain, &ysd) != 0) 659ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 6604cc738f7SBill Paul 6614415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 6624415cd19SGarrett Wollman tv.tv_usec = 0; 6634415cd19SGarrett Wollman 6644415cd19SGarrett Wollman bzero((char *)&yprv, sizeof yprv); 6654415cd19SGarrett Wollman 6664415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_MATCH, 667f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_key, &yprk, 668f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_val, &yprv, tv); 6694415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 6704415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_match: clnt_call"); 6712694f9b9SBill Paul _yp_unbind(ysd); 6724415cd19SGarrett Wollman goto again; 6734415cd19SGarrett Wollman } 674a169c9b1SBill Paul 6750e276383SBill Paul if (!(r = ypprot_err(yprv.stat))) { 6760e276383SBill Paul *outvallen = yprv.val.valdat_len; 6774415cd19SGarrett Wollman *outval = (char *)malloc(*outvallen+1); 6780e276383SBill Paul bcopy(yprv.val.valdat_val, *outval, *outvallen); 6794415cd19SGarrett Wollman (*outval)[*outvallen] = '\0'; 6804415cd19SGarrett Wollman #ifdef YPMATCHCACHE 6814e6ace08SBill Paul ypmatch_cache_insert(ysd, yprk.map, &yprk.key, &yprv.val); 6824415cd19SGarrett Wollman #endif 6832694f9b9SBill Paul } 684a169c9b1SBill Paul 685f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_val, &yprv); 686ed4d1c46SDag-Erling Smørgrav return (r); 6874415cd19SGarrett Wollman } 6884415cd19SGarrett Wollman 6894415cd19SGarrett Wollman int 690dc584ddbSDag-Erling Smørgrav yp_get_default_domain(char **domp) 6914415cd19SGarrett Wollman { 6924415cd19SGarrett Wollman *domp = NULL; 6934415cd19SGarrett Wollman if (_yp_domain[0] == '\0') 6944415cd19SGarrett Wollman if (getdomainname(_yp_domain, sizeof _yp_domain)) 695ed4d1c46SDag-Erling Smørgrav return (YPERR_NODOM); 6964415cd19SGarrett Wollman *domp = _yp_domain; 697ed4d1c46SDag-Erling Smørgrav return (0); 6984415cd19SGarrett Wollman } 6994415cd19SGarrett Wollman 7004415cd19SGarrett Wollman int 701dc584ddbSDag-Erling Smørgrav yp_first(char *indomain, char *inmap, char **outkey, int *outkeylen, 702dc584ddbSDag-Erling Smørgrav char **outval, int *outvallen) 7034415cd19SGarrett Wollman { 7044415cd19SGarrett Wollman struct ypresp_key_val yprkv; 7054415cd19SGarrett Wollman struct ypreq_nokey yprnk; 7064415cd19SGarrett Wollman struct dom_binding *ysd; 7074415cd19SGarrett Wollman struct timeval tv; 7084415cd19SGarrett Wollman int r; 7094415cd19SGarrett Wollman 71006643071SBill Paul /* Sanity check */ 71106643071SBill Paul 71206643071SBill Paul if (indomain == NULL || !strlen(indomain) || 71306643071SBill Paul inmap == NULL || !strlen(inmap)) 714ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 71506643071SBill Paul 7164415cd19SGarrett Wollman *outkey = *outval = NULL; 7174415cd19SGarrett Wollman *outkeylen = *outvallen = 0; 7184415cd19SGarrett Wollman 7194415cd19SGarrett Wollman again: 7204415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 721ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 7224415cd19SGarrett Wollman 7234415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 7244415cd19SGarrett Wollman tv.tv_usec = 0; 7254415cd19SGarrett Wollman 7264415cd19SGarrett Wollman yprnk.domain = indomain; 7274415cd19SGarrett Wollman yprnk.map = inmap; 7284415cd19SGarrett Wollman bzero((char *)&yprkv, sizeof yprkv); 7294415cd19SGarrett Wollman 7304415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_FIRST, 731f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_nokey, &yprnk, 732f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv); 7334415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 7344415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_first: clnt_call"); 7352694f9b9SBill Paul _yp_unbind(ysd); 7364415cd19SGarrett Wollman goto again; 7374415cd19SGarrett Wollman } 7380e276383SBill Paul if (!(r = ypprot_err(yprkv.stat))) { 7390e276383SBill Paul *outkeylen = yprkv.key.keydat_len; 7404415cd19SGarrett Wollman *outkey = (char *)malloc(*outkeylen+1); 7410e276383SBill Paul bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 7424415cd19SGarrett Wollman (*outkey)[*outkeylen] = '\0'; 7430e276383SBill Paul *outvallen = yprkv.val.valdat_len; 7444415cd19SGarrett Wollman *outval = (char *)malloc(*outvallen+1); 7450e276383SBill Paul bcopy(yprkv.val.valdat_val, *outval, *outvallen); 7464415cd19SGarrett Wollman (*outval)[*outvallen] = '\0'; 7472694f9b9SBill Paul } 748a169c9b1SBill Paul 749f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv); 750ed4d1c46SDag-Erling Smørgrav return (r); 7514415cd19SGarrett Wollman } 7524415cd19SGarrett Wollman 7534415cd19SGarrett Wollman int 754dc584ddbSDag-Erling Smørgrav yp_next(char *indomain, char *inmap, char *inkey, int inkeylen, 755dc584ddbSDag-Erling Smørgrav char **outkey, int *outkeylen, char **outval, int *outvallen) 7564415cd19SGarrett Wollman { 7574415cd19SGarrett Wollman struct ypresp_key_val yprkv; 7584415cd19SGarrett Wollman struct ypreq_key yprk; 7594415cd19SGarrett Wollman struct dom_binding *ysd; 7604415cd19SGarrett Wollman struct timeval tv; 7614415cd19SGarrett Wollman int r; 7624415cd19SGarrett Wollman 76306643071SBill Paul /* Sanity check */ 76406643071SBill Paul 76506643071SBill Paul if (inkey == NULL || !strlen(inkey) || inkeylen <= 0 || 76606643071SBill Paul inmap == NULL || !strlen(inmap) || 76706643071SBill Paul indomain == NULL || !strlen(indomain)) 768ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 76906643071SBill Paul 7704415cd19SGarrett Wollman *outkey = *outval = NULL; 7714415cd19SGarrett Wollman *outkeylen = *outvallen = 0; 7724415cd19SGarrett Wollman 7734415cd19SGarrett Wollman again: 7744415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 775ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 7764415cd19SGarrett Wollman 7774415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 7784415cd19SGarrett Wollman tv.tv_usec = 0; 7794415cd19SGarrett Wollman 7804415cd19SGarrett Wollman yprk.domain = indomain; 7814415cd19SGarrett Wollman yprk.map = inmap; 7820e276383SBill Paul yprk.key.keydat_val = inkey; 7830e276383SBill Paul yprk.key.keydat_len = inkeylen; 7844415cd19SGarrett Wollman bzero((char *)&yprkv, sizeof yprkv); 7854415cd19SGarrett Wollman 7864415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_NEXT, 787f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_key, &yprk, 788f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_key_val, &yprkv, tv); 7894415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 7904415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_next: clnt_call"); 7912694f9b9SBill Paul _yp_unbind(ysd); 7924415cd19SGarrett Wollman goto again; 7934415cd19SGarrett Wollman } 7940e276383SBill Paul if (!(r = ypprot_err(yprkv.stat))) { 7950e276383SBill Paul *outkeylen = yprkv.key.keydat_len; 7964415cd19SGarrett Wollman *outkey = (char *)malloc(*outkeylen+1); 7970e276383SBill Paul bcopy(yprkv.key.keydat_val, *outkey, *outkeylen); 7984415cd19SGarrett Wollman (*outkey)[*outkeylen] = '\0'; 7990e276383SBill Paul *outvallen = yprkv.val.valdat_len; 8004415cd19SGarrett Wollman *outval = (char *)malloc(*outvallen+1); 8010e276383SBill Paul bcopy(yprkv.val.valdat_val, *outval, *outvallen); 8024415cd19SGarrett Wollman (*outval)[*outvallen] = '\0'; 8032694f9b9SBill Paul } 804a169c9b1SBill Paul 805f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_key_val, &yprkv); 806ed4d1c46SDag-Erling Smørgrav return (r); 8074415cd19SGarrett Wollman } 8084415cd19SGarrett Wollman 8094415cd19SGarrett Wollman int 810dc584ddbSDag-Erling Smørgrav yp_all(char *indomain, char *inmap, struct ypall_callback *incallback) 8114415cd19SGarrett Wollman { 8124415cd19SGarrett Wollman struct ypreq_nokey yprnk; 8134415cd19SGarrett Wollman struct dom_binding *ysd; 8144415cd19SGarrett Wollman struct timeval tv; 8154415cd19SGarrett Wollman struct sockaddr_in clnt_sin; 8164415cd19SGarrett Wollman CLIENT *clnt; 81708aff01bSBill Paul u_long status, savstat; 8184415cd19SGarrett Wollman int clnt_sock; 8194415cd19SGarrett Wollman 82006643071SBill Paul /* Sanity check */ 82106643071SBill Paul 82206643071SBill Paul if (indomain == NULL || !strlen(indomain) || 82306643071SBill Paul inmap == NULL || !strlen(inmap)) 824ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 82506643071SBill Paul 8262694f9b9SBill Paul again: 8272694f9b9SBill Paul 8284415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 829ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 8304415cd19SGarrett Wollman 8314415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 8324415cd19SGarrett Wollman tv.tv_usec = 0; 8332694f9b9SBill Paul 8342694f9b9SBill Paul /* YPPROC_ALL manufactures its own channel to ypserv using TCP */ 8352694f9b9SBill Paul 8364415cd19SGarrett Wollman clnt_sock = RPC_ANYSOCK; 8374415cd19SGarrett Wollman clnt_sin = ysd->dom_server_addr; 8384415cd19SGarrett Wollman clnt_sin.sin_port = 0; 8394415cd19SGarrett Wollman clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0); 8404415cd19SGarrett Wollman if (clnt == NULL) { 8414415cd19SGarrett Wollman printf("clnttcp_create failed\n"); 842ed4d1c46SDag-Erling Smørgrav return (YPERR_PMAP); 8434415cd19SGarrett Wollman } 8444415cd19SGarrett Wollman 8454415cd19SGarrett Wollman yprnk.domain = indomain; 8464415cd19SGarrett Wollman yprnk.map = inmap; 8474415cd19SGarrett Wollman ypresp_allfn = incallback->foreach; 8484415cd19SGarrett Wollman ypresp_data = (void *)incallback->data; 8494415cd19SGarrett Wollman 8502694f9b9SBill Paul if (clnt_call(clnt, YPPROC_ALL, 851f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_nokey, &yprnk, 852f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_all_seq, &status, tv) != RPC_SUCCESS) { 853d825ca42SJim Pirzyk clnt_perror(ysd->dom_client, "yp_all: clnt_call"); 8542694f9b9SBill Paul clnt_destroy(clnt); 8552694f9b9SBill Paul _yp_unbind(ysd); 8562694f9b9SBill Paul goto again; 8572694f9b9SBill Paul } 8582694f9b9SBill Paul 8594415cd19SGarrett Wollman clnt_destroy(clnt); 86008aff01bSBill Paul savstat = status; 861f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_all_seq, &status); /* not really needed... */ 86208aff01bSBill Paul if (savstat != YP_NOMORE) 863ed4d1c46SDag-Erling Smørgrav return (ypprot_err(savstat)); 864ed4d1c46SDag-Erling Smørgrav return (0); 8654415cd19SGarrett Wollman } 8664415cd19SGarrett Wollman 8674415cd19SGarrett Wollman int 868dc584ddbSDag-Erling Smørgrav yp_order(char *indomain, char *inmap, int *outorder) 8694415cd19SGarrett Wollman { 8704415cd19SGarrett Wollman struct dom_binding *ysd; 8714415cd19SGarrett Wollman struct ypresp_order ypro; 8724415cd19SGarrett Wollman struct ypreq_nokey yprnk; 8734415cd19SGarrett Wollman struct timeval tv; 8744415cd19SGarrett Wollman int r; 8754415cd19SGarrett Wollman 87606643071SBill Paul /* Sanity check */ 87706643071SBill Paul 87806643071SBill Paul if (indomain == NULL || !strlen(indomain) || 87906643071SBill Paul inmap == NULL || !strlen(inmap)) 880ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 88106643071SBill Paul 8824415cd19SGarrett Wollman again: 8834415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 884ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 8854415cd19SGarrett Wollman 8864415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 8874415cd19SGarrett Wollman tv.tv_usec = 0; 8884415cd19SGarrett Wollman 8894415cd19SGarrett Wollman yprnk.domain = indomain; 8904415cd19SGarrett Wollman yprnk.map = inmap; 8914415cd19SGarrett Wollman 8924415cd19SGarrett Wollman bzero((char *)(char *)&ypro, sizeof ypro); 8934415cd19SGarrett Wollman 8944415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_ORDER, 895f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_nokey, &yprnk, 896f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_order, &ypro, tv); 8979fa75c15SBill Paul 8989fa75c15SBill Paul /* 8999fa75c15SBill Paul * NIS+ in YP compat mode doesn't support the YPPROC_ORDER 9009fa75c15SBill Paul * procedure. 9019fa75c15SBill Paul */ 9029fa75c15SBill Paul if (r == RPC_PROCUNAVAIL) { 9039fa75c15SBill Paul return(YPERR_YPERR); 9049fa75c15SBill Paul } 9059fa75c15SBill Paul 9064415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 9074415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_order: clnt_call"); 9082694f9b9SBill Paul _yp_unbind(ysd); 9094415cd19SGarrett Wollman goto again; 9104415cd19SGarrett Wollman } 9114415cd19SGarrett Wollman 912a169c9b1SBill Paul if (!(r = ypprot_err(ypro.stat))) { 9134415cd19SGarrett Wollman *outorder = ypro.ordernum; 9142694f9b9SBill Paul } 915a169c9b1SBill Paul 916f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_order, &ypro); 9172694f9b9SBill Paul return (r); 9184415cd19SGarrett Wollman } 9194415cd19SGarrett Wollman 9204415cd19SGarrett Wollman int 921dc584ddbSDag-Erling Smørgrav yp_master(char *indomain, char *inmap, char **outname) 9224415cd19SGarrett Wollman { 9234415cd19SGarrett Wollman struct dom_binding *ysd; 9244415cd19SGarrett Wollman struct ypresp_master yprm; 9254415cd19SGarrett Wollman struct ypreq_nokey yprnk; 9264415cd19SGarrett Wollman struct timeval tv; 9274415cd19SGarrett Wollman int r; 9284415cd19SGarrett Wollman 92906643071SBill Paul /* Sanity check */ 93006643071SBill Paul 93106643071SBill Paul if (indomain == NULL || !strlen(indomain) || 93206643071SBill Paul inmap == NULL || !strlen(inmap)) 933ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 9344415cd19SGarrett Wollman again: 9354415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 936ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 9374415cd19SGarrett Wollman 9384415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 9394415cd19SGarrett Wollman tv.tv_usec = 0; 9404415cd19SGarrett Wollman 9414415cd19SGarrett Wollman yprnk.domain = indomain; 9424415cd19SGarrett Wollman yprnk.map = inmap; 9434415cd19SGarrett Wollman 9444415cd19SGarrett Wollman bzero((char *)&yprm, sizeof yprm); 9454415cd19SGarrett Wollman 9464415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_MASTER, 947f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypreq_nokey, &yprnk, 948f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_master, &yprm, tv); 9494415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 9504415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_master: clnt_call"); 9512694f9b9SBill Paul _yp_unbind(ysd); 9524415cd19SGarrett Wollman goto again; 9534415cd19SGarrett Wollman } 9542694f9b9SBill Paul 9550e276383SBill Paul if (!(r = ypprot_err(yprm.stat))) { 9560e276383SBill Paul *outname = (char *)strdup(yprm.peer); 9572694f9b9SBill Paul } 958a169c9b1SBill Paul 959f249dbccSDag-Erling Smørgrav xdr_free((xdrproc_t)xdr_ypresp_master, &yprm); 9602694f9b9SBill Paul return (r); 9614415cd19SGarrett Wollman } 962dc584ddbSDag-Erling Smørgrav 963b9729ac2SBill Paul int 964dc584ddbSDag-Erling Smørgrav yp_maplist(char *indomain, struct ypmaplist **outmaplist) 9654415cd19SGarrett Wollman { 9664415cd19SGarrett Wollman struct dom_binding *ysd; 9674415cd19SGarrett Wollman struct ypresp_maplist ypml; 9684415cd19SGarrett Wollman struct timeval tv; 9694415cd19SGarrett Wollman int r; 9704415cd19SGarrett Wollman 97106643071SBill Paul /* Sanity check */ 97206643071SBill Paul 97306643071SBill Paul if (indomain == NULL || !strlen(indomain)) 974ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 97506643071SBill Paul 9764415cd19SGarrett Wollman again: 9774415cd19SGarrett Wollman if (_yp_dobind(indomain, &ysd) != 0) 978ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 9794415cd19SGarrett Wollman 9804415cd19SGarrett Wollman tv.tv_sec = _yplib_timeout; 9814415cd19SGarrett Wollman tv.tv_usec = 0; 9824415cd19SGarrett Wollman 9834415cd19SGarrett Wollman bzero((char *)&ypml, sizeof ypml); 9844415cd19SGarrett Wollman 9854415cd19SGarrett Wollman r = clnt_call(ysd->dom_client, YPPROC_MAPLIST, 986f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_domainname, &indomain, 987f249dbccSDag-Erling Smørgrav (xdrproc_t)xdr_ypresp_maplist, &ypml,tv); 9884415cd19SGarrett Wollman if (r != RPC_SUCCESS) { 9894415cd19SGarrett Wollman clnt_perror(ysd->dom_client, "yp_maplist: clnt_call"); 9902694f9b9SBill Paul _yp_unbind(ysd); 9914415cd19SGarrett Wollman goto again; 9924415cd19SGarrett Wollman } 993a169c9b1SBill Paul if (!(r = ypprot_err(ypml.stat))) { 9940e276383SBill Paul *outmaplist = ypml.maps; 9952694f9b9SBill Paul } 9962694f9b9SBill Paul 997f249dbccSDag-Erling Smørgrav /* NO: xdr_free((xdrproc_t)xdr_ypresp_maplist, &ypml);*/ 9982694f9b9SBill Paul return (r); 9994415cd19SGarrett Wollman } 10004415cd19SGarrett Wollman 1001f249dbccSDag-Erling Smørgrav const char * 1002dc584ddbSDag-Erling Smørgrav yperr_string(int incode) 10034415cd19SGarrett Wollman { 10044415cd19SGarrett Wollman static char err[80]; 10054415cd19SGarrett Wollman 10064415cd19SGarrett Wollman switch (incode) { 10074415cd19SGarrett Wollman case 0: 1008ed4d1c46SDag-Erling Smørgrav return ("Success"); 10094415cd19SGarrett Wollman case YPERR_BADARGS: 1010ed4d1c46SDag-Erling Smørgrav return ("Request arguments bad"); 10114415cd19SGarrett Wollman case YPERR_RPC: 1012ed4d1c46SDag-Erling Smørgrav return ("RPC failure"); 10134415cd19SGarrett Wollman case YPERR_DOMAIN: 1014ed4d1c46SDag-Erling Smørgrav return ("Can't bind to server which serves this domain"); 10154415cd19SGarrett Wollman case YPERR_MAP: 1016ed4d1c46SDag-Erling Smørgrav return ("No such map in server's domain"); 10174415cd19SGarrett Wollman case YPERR_KEY: 1018ed4d1c46SDag-Erling Smørgrav return ("No such key in map"); 10194415cd19SGarrett Wollman case YPERR_YPERR: 1020ed4d1c46SDag-Erling Smørgrav return ("YP server error"); 10214415cd19SGarrett Wollman case YPERR_RESRC: 1022ed4d1c46SDag-Erling Smørgrav return ("Local resource allocation failure"); 10234415cd19SGarrett Wollman case YPERR_NOMORE: 1024ed4d1c46SDag-Erling Smørgrav return ("No more records in map database"); 10254415cd19SGarrett Wollman case YPERR_PMAP: 1026ed4d1c46SDag-Erling Smørgrav return ("Can't communicate with portmapper"); 10274415cd19SGarrett Wollman case YPERR_YPBIND: 1028ed4d1c46SDag-Erling Smørgrav return ("Can't communicate with ypbind"); 10294415cd19SGarrett Wollman case YPERR_YPSERV: 1030ed4d1c46SDag-Erling Smørgrav return ("Can't communicate with ypserv"); 10314415cd19SGarrett Wollman case YPERR_NODOM: 1032ed4d1c46SDag-Erling Smørgrav return ("Local domain name not set"); 10334415cd19SGarrett Wollman case YPERR_BADDB: 1034ed4d1c46SDag-Erling Smørgrav return ("Server data base is bad"); 10354415cd19SGarrett Wollman case YPERR_VERS: 1036ed4d1c46SDag-Erling Smørgrav return ("YP server version mismatch - server can't supply service."); 10374415cd19SGarrett Wollman case YPERR_ACCESS: 1038ed4d1c46SDag-Erling Smørgrav return ("Access violation"); 10394415cd19SGarrett Wollman case YPERR_BUSY: 1040ed4d1c46SDag-Erling Smørgrav return ("Database is busy"); 10414415cd19SGarrett Wollman } 10424415cd19SGarrett Wollman sprintf(err, "YP unknown error %d\n", incode); 1043ed4d1c46SDag-Erling Smørgrav return (err); 10444415cd19SGarrett Wollman } 10454415cd19SGarrett Wollman 10464415cd19SGarrett Wollman int 1047dc584ddbSDag-Erling Smørgrav ypprot_err(unsigned int incode) 10484415cd19SGarrett Wollman { 10494415cd19SGarrett Wollman switch (incode) { 10504415cd19SGarrett Wollman case YP_TRUE: 1051ed4d1c46SDag-Erling Smørgrav return (0); 10524415cd19SGarrett Wollman case YP_FALSE: 1053ed4d1c46SDag-Erling Smørgrav return (YPERR_YPBIND); 10544415cd19SGarrett Wollman case YP_NOMORE: 1055ed4d1c46SDag-Erling Smørgrav return (YPERR_NOMORE); 10564415cd19SGarrett Wollman case YP_NOMAP: 1057ed4d1c46SDag-Erling Smørgrav return (YPERR_MAP); 10584415cd19SGarrett Wollman case YP_NODOM: 1059ed4d1c46SDag-Erling Smørgrav return (YPERR_DOMAIN); 10604415cd19SGarrett Wollman case YP_NOKEY: 1061ed4d1c46SDag-Erling Smørgrav return (YPERR_KEY); 10624415cd19SGarrett Wollman case YP_BADOP: 1063ed4d1c46SDag-Erling Smørgrav return (YPERR_YPERR); 10644415cd19SGarrett Wollman case YP_BADDB: 1065ed4d1c46SDag-Erling Smørgrav return (YPERR_BADDB); 10664415cd19SGarrett Wollman case YP_YPERR: 1067ed4d1c46SDag-Erling Smørgrav return (YPERR_YPERR); 10684415cd19SGarrett Wollman case YP_BADARGS: 1069ed4d1c46SDag-Erling Smørgrav return (YPERR_BADARGS); 10704415cd19SGarrett Wollman case YP_VERS: 1071ed4d1c46SDag-Erling Smørgrav return (YPERR_VERS); 10724415cd19SGarrett Wollman } 1073ed4d1c46SDag-Erling Smørgrav return (YPERR_YPERR); 10744415cd19SGarrett Wollman } 10754415cd19SGarrett Wollman 10764415cd19SGarrett Wollman int 1077dc584ddbSDag-Erling Smørgrav _yp_check(char **dom) 10784415cd19SGarrett Wollman { 10794415cd19SGarrett Wollman char *unused; 10804415cd19SGarrett Wollman 10814415cd19SGarrett Wollman if (_yp_domain[0]=='\0') 10824415cd19SGarrett Wollman if (yp_get_default_domain(&unused)) 1083ed4d1c46SDag-Erling Smørgrav return (0); 10844415cd19SGarrett Wollman 10854415cd19SGarrett Wollman if (dom) 10864415cd19SGarrett Wollman *dom = _yp_domain; 10874415cd19SGarrett Wollman 10880f98415eSBill Paul if (yp_bind(_yp_domain) == 0) { 10890f98415eSBill Paul yp_unbind(_yp_domain); 1090ed4d1c46SDag-Erling Smørgrav return (1); 10910f98415eSBill Paul } 1092ed4d1c46SDag-Erling Smørgrav return (0); 10934415cd19SGarrett Wollman } 1094