17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 2161961e0fSrobinson */ 2261961e0fSrobinson 2361961e0fSrobinson /* 24*e8031f0aSraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 337c478bd9Sstevel@tonic-gate * under license from the Regents of the University of 347c478bd9Sstevel@tonic-gate * California. 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 387c478bd9Sstevel@tonic-gate 39*e8031f0aSraf #include "mt.h" 4061961e0fSrobinson #include <stdlib.h> 4161961e0fSrobinson #include <unistd.h> 427c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 437c478bd9Sstevel@tonic-gate #include <syslog.h> 447c478bd9Sstevel@tonic-gate #include "yp_b.h" 457c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h> 467c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h> 477c478bd9Sstevel@tonic-gate #include <netdir.h> 487c478bd9Sstevel@tonic-gate #include <string.h> 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate extern int __yp_dobind_cflookup(char *, struct dom_binding **, int); 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate static struct timeval tp_timout = { 120, 0}; 537c478bd9Sstevel@tonic-gate static char nullstring[] = "\000"; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate /* 567c478bd9Sstevel@tonic-gate * __yp_all_cflookup() is a variant of the yp_all() code, 577c478bd9Sstevel@tonic-gate * which adds a 'hardlookup' parameter. This parameter is passed 587c478bd9Sstevel@tonic-gate * to __yp_dobind_cflookup(), and determines whether the server 597c478bd9Sstevel@tonic-gate * binding attempt is hard (try forever) of soft (retry a compiled- 607c478bd9Sstevel@tonic-gate * in number of times). 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate int 6361961e0fSrobinson __yp_all_cflookup(char *domain, char *map, struct ypall_callback *callback, 6461961e0fSrobinson int hardlookup) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate size_t domlen; 677c478bd9Sstevel@tonic-gate size_t maplen; 687c478bd9Sstevel@tonic-gate struct ypreq_nokey req; 697c478bd9Sstevel@tonic-gate int reason; 707c478bd9Sstevel@tonic-gate struct dom_binding *pdomb; 717c478bd9Sstevel@tonic-gate enum clnt_stat s; 727c478bd9Sstevel@tonic-gate CLIENT *allc; 737c478bd9Sstevel@tonic-gate char server_name[MAXHOSTNAMELEN]; 747c478bd9Sstevel@tonic-gate char errbuf[BUFSIZ]; 757c478bd9Sstevel@tonic-gate 7661961e0fSrobinson if ((map == NULL) || (domain == NULL)) 777c478bd9Sstevel@tonic-gate return (YPERR_BADARGS); 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate domlen = strlen(domain); 807c478bd9Sstevel@tonic-gate maplen = strlen(map); 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate if ((domlen == 0) || (domlen > YPMAXDOMAIN) || 837c478bd9Sstevel@tonic-gate (maplen == 0) || (maplen > YPMAXMAP) || 8461961e0fSrobinson (callback == NULL)) 857c478bd9Sstevel@tonic-gate return (YPERR_BADARGS); 867c478bd9Sstevel@tonic-gate 8761961e0fSrobinson if (reason = __yp_dobind_cflookup(domain, &pdomb, hardlookup)) 887c478bd9Sstevel@tonic-gate return (reason); 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate if (pdomb->dom_binding->ypbind_hi_vers < YPVERS) { 917c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 927c478bd9Sstevel@tonic-gate return (YPERR_VERS); 937c478bd9Sstevel@tonic-gate } 9461961e0fSrobinson (void) mutex_lock(&pdomb->server_name_lock); 957c478bd9Sstevel@tonic-gate if (!pdomb->dom_binding->ypbind_servername) { 9661961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 977c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 987c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "yp_all: failed to get server's name\n"); 997c478bd9Sstevel@tonic-gate return (YPERR_RPC); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate (void) strcpy(server_name, pdomb->dom_binding->ypbind_servername); 10261961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 1037c478bd9Sstevel@tonic-gate if (strcmp(server_name, nullstring) == 0) { 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * This is the case where ypbind is running in broadcast mode, 1067c478bd9Sstevel@tonic-gate * we have to do the jugglery to get the 1077c478bd9Sstevel@tonic-gate * ypserv's address on COTS transport based 1087c478bd9Sstevel@tonic-gate * on the CLTS address ypbind gave us ! 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate struct nd_hostservlist *nhs; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (netdir_getbyaddr(pdomb->dom_binding->ypbind_nconf, 1147c478bd9Sstevel@tonic-gate &nhs, pdomb->dom_binding->ypbind_svcaddr) != ND_OK) { 1157c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 1167c478bd9Sstevel@tonic-gate "yp_all: failed to get server's name\n"); 1177c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 1187c478bd9Sstevel@tonic-gate return (YPERR_RPC); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate /* check server name again, some other thread may have set it */ 12161961e0fSrobinson (void) mutex_lock(&pdomb->server_name_lock); 1227c478bd9Sstevel@tonic-gate if (strcmp(pdomb->dom_binding->ypbind_servername, 1237c478bd9Sstevel@tonic-gate nullstring) == 0) { 1247c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_servername = 1257c478bd9Sstevel@tonic-gate (char *)strdup(nhs->h_hostservs->h_host); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate (void) strcpy(server_name, 1287c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_servername); 12961961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 1307c478bd9Sstevel@tonic-gate netdir_free((char *)nhs, ND_HOSTSERVLIST); 1317c478bd9Sstevel@tonic-gate } 1327c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 1337c478bd9Sstevel@tonic-gate if ((allc = clnt_create(server_name, YPPROG, 13461961e0fSrobinson YPVERS, "circuit_n")) == NULL) { 13561961e0fSrobinson (void) snprintf(errbuf, BUFSIZ, "yp_all \ 1367c478bd9Sstevel@tonic-gate - transport level create failure for domain %s / map %s", domain, map); 13761961e0fSrobinson syslog(LOG_ERR, "%s", clnt_spcreateerror(errbuf)); 1387c478bd9Sstevel@tonic-gate return (YPERR_RPC); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate req.domain = domain; 1427c478bd9Sstevel@tonic-gate req.map = map; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate s = clnt_call(allc, YPPROC_ALL, 1467c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_ypreq_nokey, (char *)&req, 1477c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_ypall, (char *)callback, tp_timout); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (s != RPC_SUCCESS && s != RPC_TIMEDOUT) { 15061961e0fSrobinson syslog(LOG_ERR, "%s", clnt_sperror(allc, 1517c478bd9Sstevel@tonic-gate "yp_all - RPC clnt_call (transport level) failure")); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate clnt_destroy(allc); 1557c478bd9Sstevel@tonic-gate switch (s) { 1567c478bd9Sstevel@tonic-gate case RPC_SUCCESS: 1577c478bd9Sstevel@tonic-gate return (0); 1587c478bd9Sstevel@tonic-gate case RPC_TIMEDOUT: 1597c478bd9Sstevel@tonic-gate return (YPERR_YPSERV); 1607c478bd9Sstevel@tonic-gate default: 1617c478bd9Sstevel@tonic-gate return (YPERR_RPC); 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * This does the "glommed enumeration" stuff. callback->foreach is the name 1687c478bd9Sstevel@tonic-gate * of a function which gets called per decoded key-value pair: 1697c478bd9Sstevel@tonic-gate * 1707c478bd9Sstevel@tonic-gate * (*callback->foreach)(status, key, keylen, val, vallen, callback->data); 1717c478bd9Sstevel@tonic-gate * 1727c478bd9Sstevel@tonic-gate * If the server we get back from __yp_dobind speaks the old protocol, this 1737c478bd9Sstevel@tonic-gate * returns YPERR_VERS, and does not attempt to emulate the new functionality 1747c478bd9Sstevel@tonic-gate * by using the old protocol. 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate int 17761961e0fSrobinson yp_all(char *domain, char *map, struct ypall_callback *callback) 1787c478bd9Sstevel@tonic-gate { 1797c478bd9Sstevel@tonic-gate return (__yp_all_cflookup(domain, map, callback, 1)); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate /* 1847c478bd9Sstevel@tonic-gate * This function is identical to 'yp_all' with the exception that it 1857c478bd9Sstevel@tonic-gate * attempts to use reserve ports. 1867c478bd9Sstevel@tonic-gate */ 1877c478bd9Sstevel@tonic-gate int 18861961e0fSrobinson __yp_all_rsvdport(char *domain, char *map, struct ypall_callback *callback) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate size_t domlen; 1917c478bd9Sstevel@tonic-gate size_t maplen; 1927c478bd9Sstevel@tonic-gate struct ypreq_nokey req; 1937c478bd9Sstevel@tonic-gate int reason; 1947c478bd9Sstevel@tonic-gate struct dom_binding *pdomb; 1957c478bd9Sstevel@tonic-gate enum clnt_stat s; 1967c478bd9Sstevel@tonic-gate CLIENT *allc; 1977c478bd9Sstevel@tonic-gate char server_name[MAXHOSTNAMELEN]; 1987c478bd9Sstevel@tonic-gate char errbuf[BUFSIZ]; 1997c478bd9Sstevel@tonic-gate 20061961e0fSrobinson if ((map == NULL) || (domain == NULL)) 2017c478bd9Sstevel@tonic-gate return (YPERR_BADARGS); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate domlen = strlen(domain); 2047c478bd9Sstevel@tonic-gate maplen = strlen(map); 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate if ((domlen == 0) || (domlen > YPMAXDOMAIN) || 2077c478bd9Sstevel@tonic-gate (maplen == 0) || (maplen > YPMAXMAP) || 20861961e0fSrobinson (callback == NULL)) 2097c478bd9Sstevel@tonic-gate return (YPERR_BADARGS); 2107c478bd9Sstevel@tonic-gate 21161961e0fSrobinson if (reason = __yp_dobind_rsvdport(domain, &pdomb)) 2127c478bd9Sstevel@tonic-gate return (reason); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (pdomb->dom_binding->ypbind_hi_vers < YPVERS) { 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * Have to free the binding since the reserved 2177c478bd9Sstevel@tonic-gate * port bindings are not cached. 2187c478bd9Sstevel@tonic-gate */ 2197c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 2207c478bd9Sstevel@tonic-gate free_dom_binding(pdomb); 2217c478bd9Sstevel@tonic-gate return (YPERR_VERS); 2227c478bd9Sstevel@tonic-gate } 22361961e0fSrobinson (void) mutex_lock(&pdomb->server_name_lock); 2247c478bd9Sstevel@tonic-gate if (!pdomb->dom_binding->ypbind_servername) { 22561961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 2267c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "yp_all: failed to get server's name\n"); 2277c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 2287c478bd9Sstevel@tonic-gate free_dom_binding(pdomb); 2297c478bd9Sstevel@tonic-gate return (YPERR_RPC); 2307c478bd9Sstevel@tonic-gate } 2317c478bd9Sstevel@tonic-gate (void) strcpy(server_name, pdomb->dom_binding->ypbind_servername); 23261961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 2337c478bd9Sstevel@tonic-gate if (strcmp(server_name, nullstring) == 0) { 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * This is the case where ypbind is running in broadcast mode, 2367c478bd9Sstevel@tonic-gate * we have to do the jugglery to get the 2377c478bd9Sstevel@tonic-gate * ypserv's address on COTS transport based 2387c478bd9Sstevel@tonic-gate * on the CLTS address ypbind gave us ! 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate struct nd_hostservlist *nhs; 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate if (netdir_getbyaddr(pdomb->dom_binding->ypbind_nconf, 2447c478bd9Sstevel@tonic-gate &nhs, pdomb->dom_binding->ypbind_svcaddr) != ND_OK) { 2457c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 2467c478bd9Sstevel@tonic-gate "yp_all: failed to get server's name\n"); 2477c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 2487c478bd9Sstevel@tonic-gate free_dom_binding(pdomb); 2497c478bd9Sstevel@tonic-gate return (YPERR_RPC); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate /* check server name again, some other thread may have set it */ 25261961e0fSrobinson (void) mutex_lock(&pdomb->server_name_lock); 2537c478bd9Sstevel@tonic-gate if (strcmp(pdomb->dom_binding->ypbind_servername, 2547c478bd9Sstevel@tonic-gate nullstring) == 0) { 2557c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_servername = 2567c478bd9Sstevel@tonic-gate (char *)strdup(nhs->h_hostservs->h_host); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate (void) strcpy(server_name, 2597c478bd9Sstevel@tonic-gate pdomb->dom_binding->ypbind_servername); 26061961e0fSrobinson (void) mutex_unlock(&pdomb->server_name_lock); 2617c478bd9Sstevel@tonic-gate netdir_free((char *)nhs, ND_HOSTSERVLIST); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate __yp_rel_binding(pdomb); 2657c478bd9Sstevel@tonic-gate if ((allc = __yp_clnt_create_rsvdport(server_name, YPPROG, YPVERS, 26661961e0fSrobinson "tcp6", 0, 0)) == NULL && 2677c478bd9Sstevel@tonic-gate (allc = __yp_clnt_create_rsvdport(server_name, YPPROG, YPVERS, 26861961e0fSrobinson "tcp", 0, 0)) == NULL) { 26961961e0fSrobinson (void) snprintf(errbuf, BUFSIZ, "yp_all \ 2707c478bd9Sstevel@tonic-gate - transport level create failure for domain %s / map %s", domain, map); 27161961e0fSrobinson syslog(LOG_ERR, "%s", clnt_spcreateerror(errbuf)); 2727c478bd9Sstevel@tonic-gate free_dom_binding(pdomb); 2737c478bd9Sstevel@tonic-gate return (YPERR_RPC); 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate req.domain = domain; 2777c478bd9Sstevel@tonic-gate req.map = map; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate s = clnt_call(allc, YPPROC_ALL, 2807c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_ypreq_nokey, (char *)&req, 2817c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_ypall, (char *)callback, tp_timout); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate if (s != RPC_SUCCESS && s != RPC_TIMEDOUT) { 28461961e0fSrobinson syslog(LOG_ERR, "%s", clnt_sperror(allc, 2857c478bd9Sstevel@tonic-gate "yp_all - RPC clnt_call (transport level) failure")); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate clnt_destroy(allc); 2897c478bd9Sstevel@tonic-gate free_dom_binding(pdomb); 2907c478bd9Sstevel@tonic-gate switch (s) { 2917c478bd9Sstevel@tonic-gate case RPC_SUCCESS: 2927c478bd9Sstevel@tonic-gate return (0); 2937c478bd9Sstevel@tonic-gate case RPC_TIMEDOUT: 2947c478bd9Sstevel@tonic-gate return (YPERR_YPSERV); 2957c478bd9Sstevel@tonic-gate default: 2967c478bd9Sstevel@tonic-gate return (YPERR_RPC); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate } 299