ypwhich.c revision 36914
11930Swollman/* 21930Swollman * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 31930Swollman * All rights reserved. 41930Swollman * 51930Swollman * Redistribution and use in source and binary forms, with or without 61930Swollman * modification, are permitted provided that the following conditions 71930Swollman * are met: 81930Swollman * 1. Redistributions of source code must retain the above copyright 91930Swollman * notice, this list of conditions and the following disclaimer. 101930Swollman * 2. Redistributions in binary form must reproduce the above copyright 111930Swollman * notice, this list of conditions and the following disclaimer in the 121930Swollman * documentation and/or other materials provided with the distribution. 131930Swollman * 3. The name of the author may not be used to endorse or promote 141930Swollman * products derived from this software without specific prior written 151930Swollman * permission. 161930Swollman * 171930Swollman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 181930Swollman * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 191930Swollman * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201930Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 211930Swollman * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221930Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231930Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241930Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251930Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261930Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271930Swollman * SUCH DAMAGE. 281930Swollman */ 291930Swollman 3028895Scharnier#ifndef lint 3128895Scharnierstatic const char rcsid[] = 3236914Speter "$Id: ypwhich.c,v 1.9 1997/08/29 11:56:51 charnier Exp $"; 3328895Scharnier#endif /* not lint */ 341930Swollman 351930Swollman#include <sys/param.h> 361930Swollman#include <sys/types.h> 371930Swollman#include <sys/socket.h> 3836914Speter#include <netinet/in.h> 3936914Speter#include <arpa/inet.h> 401930Swollman#include <ctype.h> 4128895Scharnier#include <err.h> 421930Swollman#include <netdb.h> 4328895Scharnier#include <stdio.h> 4428895Scharnier#include <stdlib.h> 4528895Scharnier#include <string.h> 4628895Scharnier#include <unistd.h> 471930Swollman#include <rpc/rpc.h> 481930Swollman#include <rpc/xdr.h> 4912862Swpaul#include <rpcsvc/yp.h> 5012862Swpaulstruct dom_binding{}; 511930Swollman#include <rpcsvc/ypclnt.h> 521930Swollman 537990Swpaul#define ERR_USAGE 1 /* bad arguments - display 'usage' message */ 547990Swpaul#define ERR_NOSUCHHOST 2 /* no such host */ 557990Swpaul#define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */ 567990Swpaul#define ERR_NOYPBIND 4 /* ypbind not running */ 577990Swpaul#define ERR_NOMASTER 5 /* could not find master server */ 587990Swpaul 591930Swollmanextern bool_t xdr_domainname(); 601930Swollman 611930Swollmanstruct ypalias { 621930Swollman char *alias, *name; 631930Swollman} ypaliases[] = { 641930Swollman { "passwd", "passwd.byname" }, 6527345Speter { "master.passwd", "master.passwd.byname" }, 661930Swollman { "group", "group.byname" }, 671930Swollman { "networks", "networks.byaddr" }, 681930Swollman { "hosts", "hosts.byaddr" }, 691930Swollman { "protocols", "protocols.bynumber" }, 701930Swollman { "services", "services.byname" }, 711930Swollman { "aliases", "mail.aliases" }, 721930Swollman { "ethers", "ethers.byname" }, 731930Swollman}; 741930Swollman 7528895Scharnierstatic void 761930Swollmanusage() 771930Swollman{ 7828895Scharnier fprintf(stderr, "%s\n%s\n", 7928895Scharnier "usage: ypwhich [-d domain] [[-t] -m [mname] | host]", 8028895Scharnier " ypwhich -x"); 817990Swpaul exit(ERR_USAGE); 821930Swollman} 831930Swollman 841930Swollman 851930Swollman/* 861930Swollman * Like yp_bind except can query a specific host 871930Swollman */ 8828895Scharnierint 891930Swollmanbind_host(dom, sin) 901930Swollmanchar *dom; 911930Swollmanstruct sockaddr_in *sin; 921930Swollman{ 931930Swollman struct hostent *hent = NULL; 941930Swollman struct ypbind_resp ypbr; 951930Swollman struct timeval tv; 961930Swollman CLIENT *client; 971930Swollman int sock, r; 9836914Speter struct in_addr ss_addr; 991930Swollman 1001930Swollman sock = RPC_ANYSOCK; 1011930Swollman tv.tv_sec = 15; 1021930Swollman tv.tv_usec = 0; 1031930Swollman client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock); 1041930Swollman if(client==NULL) { 10528895Scharnier warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND)); 1061930Swollman return YPERR_YPBIND; 1071930Swollman } 1081930Swollman 1091930Swollman tv.tv_sec = 5; 1101930Swollman tv.tv_usec = 0; 1111930Swollman r = clnt_call(client, YPBINDPROC_DOMAIN, 11212862Swpaul xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv); 1131930Swollman if( r != RPC_SUCCESS) { 11428895Scharnier warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 1151930Swollman clnt_destroy(client); 1161930Swollman return YPERR_YPBIND; 1171930Swollman } else { 1181930Swollman if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 11928895Scharnier warnx("can't yp_bind: reason: %s", 12012862Swpaul ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error)); 1211930Swollman clnt_destroy(client); 1221930Swollman return r; 1231930Swollman } 1241930Swollman } 1251930Swollman clnt_destroy(client); 1261930Swollman 12736914Speter ss_addr = *(struct in_addr *)ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr; 1281930Swollman /*printf("%08x\n", ss_addr);*/ 1291930Swollman hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET); 1301930Swollman if (hent) 1311930Swollman printf("%s\n", hent->h_name); 1321930Swollman else 1331930Swollman printf("%s\n", inet_ntoa(ss_addr)); 1341930Swollman return 0; 1351930Swollman} 1368874Srgrimes 1371930Swollmanint 1381930Swollmanmain(argc, argv) 1391930Swollmanchar **argv; 1401930Swollman{ 14128895Scharnier char *domainname = NULL, *master, *map = NULL; 1421930Swollman struct ypmaplist *ypml, *y; 1431930Swollman struct hostent *hent; 1441930Swollman struct sockaddr_in sin; 1451930Swollman int notrans, mode, getmap; 1461930Swollman int c, r, i; 1471930Swollman 1481930Swollman getmap = notrans = mode = 0; 1491930Swollman while( (c=getopt(argc, argv, "xd:mt")) != -1) 1501930Swollman switch(c) { 1511930Swollman case 'x': 1521930Swollman for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 1539594Swollman printf("\"%s\" is an alias for \"%s\"\n", 1541930Swollman ypaliases[i].alias, 1551930Swollman ypaliases[i].name); 1561930Swollman exit(0); 1571930Swollman case 'd': 1581930Swollman domainname = optarg; 1591930Swollman break; 1601930Swollman case 't': 1611930Swollman notrans++; 1621930Swollman break; 1631930Swollman case 'm': 1641930Swollman mode++; 1651930Swollman break; 1661930Swollman default: 1671930Swollman usage(); 1681930Swollman } 1691930Swollman 17028895Scharnier if(!domainname) 17128895Scharnier yp_get_default_domain(&domainname); 17228895Scharnier 1731930Swollman if(mode==0) { 1741930Swollman switch(argc-optind) { 1751930Swollman case 0: 1761930Swollman bzero(&sin, sizeof sin); 1771930Swollman sin.sin_family = AF_INET; 1781930Swollman sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1791930Swollman 1801930Swollman if(bind_host(domainname, &sin)) 1817990Swpaul exit(ERR_NOBINDING); 1821930Swollman break; 1831930Swollman case 1: 1841930Swollman bzero(&sin, sizeof sin); 1851930Swollman sin.sin_family = AF_INET; 1861930Swollman if( (sin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) { 1871930Swollman hent = gethostbyname(argv[optind]); 18828895Scharnier if(!hent) 18928895Scharnier errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]); 1901930Swollman bcopy((char *)hent->h_addr_list[0], 1911930Swollman (char *)&sin.sin_addr, sizeof sin.sin_addr); 1921930Swollman } 1931930Swollman if(bind_host(domainname, &sin)) 1947990Swpaul exit(ERR_NOBINDING); 1951930Swollman break; 1961930Swollman default: 1971930Swollman usage(); 1981930Swollman } 1991930Swollman exit(0); 2001930Swollman } 2011930Swollman 2021930Swollman if( argc-optind > 1) 2031930Swollman usage(); 2041930Swollman 2051930Swollman if(argv[optind]) { 2061930Swollman map = argv[optind]; 2071930Swollman for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 2081930Swollman if( strcmp(map, ypaliases[i].alias) == 0) 2091930Swollman map = ypaliases[i].name; 2101930Swollman r = yp_master(domainname, map, &master); 2111930Swollman switch(r) { 2121930Swollman case 0: 2131930Swollman printf("%s\n", master); 2141930Swollman free(master); 2151930Swollman break; 2161930Swollman case YPERR_YPBIND: 21728895Scharnier errx(ERR_NOYPBIND, "not running ypbind"); 2181930Swollman default: 21928895Scharnier errx(ERR_NOMASTER, "can't find master for map %s. reason: %s", 2201930Swollman map, yperr_string(r)); 2211930Swollman } 2221930Swollman exit(0); 2231930Swollman } 2241930Swollman 2251930Swollman ypml = NULL; 2261930Swollman r = yp_maplist(domainname, &ypml); 2271930Swollman switch(r) { 2281930Swollman case 0: 2291930Swollman for(y=ypml; y; ) { 2301930Swollman ypml = y; 23112862Swpaul r = yp_master(domainname, ypml->map, &master); 2321930Swollman switch(r) { 2331930Swollman case 0: 23412862Swpaul printf("%s %s\n", ypml->map, master); 2351930Swollman free(master); 2361930Swollman break; 2371930Swollman default: 23828895Scharnier warnx("can't find the master of %s: reason: %s", 23912862Swpaul ypml->map, yperr_string(r)); 2401930Swollman break; 2411930Swollman } 24212862Swpaul y = ypml->next; 2431930Swollman free(ypml); 2441930Swollman } 2451930Swollman break; 2461930Swollman case YPERR_YPBIND: 24728895Scharnier errx(ERR_NOYPBIND, "not running ypbind"); 2481930Swollman default: 24928895Scharnier errx(ERR_NOMASTER, "can't get map list for domain %s. reason: %s", 2501930Swollman domainname, yperr_string(r)); 2511930Swollman } 2521930Swollman exit(0); 2531930Swollman} 254