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 3087672Smarkm#include <sys/cdefs.h> 3187672Smarkm__FBSDID("$FreeBSD: releng/10.2/usr.bin/ypwhich/ypwhich.c 209342 2010-06-19 16:24:55Z gavin $"); 3287672Smarkm 331930Swollman#include <sys/param.h> 341930Swollman#include <sys/types.h> 351930Swollman#include <sys/socket.h> 3687672Smarkm 37121551Speter#include <rpc/rpc.h> 38121551Speter#include <rpc/xdr.h> 39121551Speter#include <rpcsvc/yp_prot.h> 4087672Smarkm#include <rpcsvc/ypclnt.h> 4187672Smarkm 4236914Speter#include <netinet/in.h> 4387672Smarkm 4436914Speter#include <arpa/inet.h> 4587672Smarkm 46200462Sdelphij#include <ctype.h> 4728895Scharnier#include <err.h> 481930Swollman#include <netdb.h> 4928895Scharnier#include <stdio.h> 5028895Scharnier#include <stdlib.h> 5128895Scharnier#include <string.h> 5228895Scharnier#include <unistd.h> 531930Swollman 547990Swpaul#define ERR_USAGE 1 /* bad arguments - display 'usage' message */ 557990Swpaul#define ERR_NOSUCHHOST 2 /* no such host */ 567990Swpaul#define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */ 577990Swpaul#define ERR_NOYPBIND 4 /* ypbind not running */ 587990Swpaul#define ERR_NOMASTER 5 /* could not find master server */ 597990Swpaul 601930Swollmanextern bool_t xdr_domainname(); 611930Swollman 621930Swollmanstruct ypalias { 63121551Speter char *alias, *name; 641930Swollman} ypaliases[] = { 651930Swollman { "passwd", "passwd.byname" }, 6627345Speter { "master.passwd", "master.passwd.byname" }, 67194968Sbrian { "shadow", "shadow.byname" }, 681930Swollman { "group", "group.byname" }, 691930Swollman { "networks", "networks.byaddr" }, 701930Swollman { "hosts", "hosts.byaddr" }, 711930Swollman { "protocols", "protocols.bynumber" }, 721930Swollman { "services", "services.byname" }, 731930Swollman { "aliases", "mail.aliases" }, 741930Swollman { "ethers", "ethers.byname" }, 751930Swollman}; 761930Swollman 7728895Scharnierstatic void 7887672Smarkmusage(void) 791930Swollman{ 8028895Scharnier fprintf(stderr, "%s\n%s\n", 8128895Scharnier "usage: ypwhich [-d domain] [[-t] -m [mname] | host]", 8228895Scharnier " ypwhich -x"); 837990Swpaul exit(ERR_USAGE); 841930Swollman} 851930Swollman 861930Swollman 871930Swollman/* 881930Swollman * Like yp_bind except can query a specific host 891930Swollman */ 9087672Smarkmstatic int 9187672Smarkmbind_host(char *dom, struct sockaddr_in *lsin) 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; 10387672Smarkm client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock); 10490297Sdes if (client == NULL) { 10528895Scharnier warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND)); 10690297Sdes return (YPERR_YPBIND); 1071930Swollman } 1081930Swollman 1091930Swollman tv.tv_sec = 5; 1101930Swollman tv.tv_usec = 0; 1111930Swollman r = clnt_call(client, YPBINDPROC_DOMAIN, 112121551Speter (xdrproc_t)xdr_domainname, &dom, 113121551Speter (xdrproc_t)xdr_ypbind_resp, &ypbr, tv); 11490297Sdes if (r != RPC_SUCCESS) { 11528895Scharnier warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 1161930Swollman clnt_destroy(client); 11790297Sdes return (YPERR_YPBIND); 1181930Swollman } else { 1191930Swollman if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 12028895Scharnier warnx("can't yp_bind: reason: %s", 121121551Speter ypbinderr_string(ypbr.ypbind_respbody.ypbind_error)); 1221930Swollman clnt_destroy(client); 12390297Sdes return (r); 1241930Swollman } 1251930Swollman } 1261930Swollman clnt_destroy(client); 1271930Swollman 128121551Speter ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr; 1291930Swollman /*printf("%08x\n", ss_addr);*/ 1301930Swollman hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET); 1311930Swollman if (hent) 1321930Swollman printf("%s\n", hent->h_name); 1331930Swollman else 1341930Swollman printf("%s\n", inet_ntoa(ss_addr)); 13590297Sdes return (0); 1361930Swollman} 1378874Srgrimes 1381930Swollmanint 13987672Smarkmmain(int argc, char *argv[]) 1401930Swollman{ 14187672Smarkm char *domnam = NULL, *master; 142121551Speter char *map = NULL; 1431930Swollman struct ypmaplist *ypml, *y; 1441930Swollman struct hostent *hent; 14587672Smarkm struct sockaddr_in lsin; 146209342Sgavin int notrans, mode; 14787672Smarkm int c, r; 14887672Smarkm u_int i; 1491930Swollman 150209342Sgavin notrans = mode = 0; 15190297Sdes while ((c = getopt(argc, argv, "xd:mt")) != -1) 15290297Sdes switch (c) { 1531930Swollman case 'x': 15490297Sdes for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 1559594Swollman printf("\"%s\" is an alias for \"%s\"\n", 1561930Swollman ypaliases[i].alias, 1571930Swollman ypaliases[i].name); 1581930Swollman exit(0); 1591930Swollman case 'd': 16087672Smarkm domnam = optarg; 1611930Swollman break; 1621930Swollman case 't': 1631930Swollman notrans++; 1641930Swollman break; 1651930Swollman case 'm': 1661930Swollman mode++; 1671930Swollman break; 1681930Swollman default: 1691930Swollman usage(); 1701930Swollman } 1711930Swollman 17290297Sdes if (!domnam) 17387672Smarkm yp_get_default_domain(&domnam); 17428895Scharnier 17590297Sdes if (mode == 0) { 17690297Sdes switch (argc-optind) { 1771930Swollman case 0: 17887672Smarkm bzero(&lsin, sizeof lsin); 17987672Smarkm lsin.sin_family = AF_INET; 18087672Smarkm lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1811930Swollman 18290297Sdes if (bind_host(domnam, &lsin)) 1837990Swpaul exit(ERR_NOBINDING); 1841930Swollman break; 1851930Swollman case 1: 18687672Smarkm bzero(&lsin, sizeof lsin); 18787672Smarkm lsin.sin_family = AF_INET; 18891377Sdwmalone if ((lsin.sin_addr.s_addr = inet_addr(argv[optind])) == INADDR_NONE) { 1891930Swollman hent = gethostbyname(argv[optind]); 19090297Sdes if (!hent) 19128895Scharnier errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]); 1921930Swollman bcopy((char *)hent->h_addr_list[0], 19387672Smarkm (char *)&lsin.sin_addr, sizeof lsin.sin_addr); 1941930Swollman } 19590297Sdes if (bind_host(domnam, &lsin)) 1967990Swpaul exit(ERR_NOBINDING); 1971930Swollman break; 1981930Swollman default: 1991930Swollman usage(); 2001930Swollman } 2011930Swollman exit(0); 2021930Swollman } 2031930Swollman 20490297Sdes if (argc-optind > 1) 2051930Swollman usage(); 2061930Swollman 20790297Sdes if (argv[optind]) { 2081930Swollman map = argv[optind]; 20990297Sdes for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 21090297Sdes if (strcmp(map, ypaliases[i].alias) == 0) 2111930Swollman map = ypaliases[i].name; 21287672Smarkm r = yp_master(domnam, map, &master); 21390297Sdes switch (r) { 2141930Swollman case 0: 2151930Swollman printf("%s\n", master); 2161930Swollman free(master); 2171930Swollman break; 2181930Swollman case YPERR_YPBIND: 21928895Scharnier errx(ERR_NOYPBIND, "not running ypbind"); 2201930Swollman default: 221127856Scharnier errx(ERR_NOMASTER, "can't find master for map %s: reason: %s", 2221930Swollman map, yperr_string(r)); 2231930Swollman } 2241930Swollman exit(0); 2251930Swollman } 2261930Swollman 2271930Swollman ypml = NULL; 22887672Smarkm r = yp_maplist(domnam, &ypml); 22990297Sdes switch (r) { 2301930Swollman case 0: 23190297Sdes for (y = ypml; y;) { 2321930Swollman ypml = y; 233121551Speter r = yp_master(domnam, ypml->ypml_name, &master); 23490297Sdes switch (r) { 2351930Swollman case 0: 236121551Speter printf("%s %s\n", ypml->ypml_name, master); 2371930Swollman free(master); 2381930Swollman break; 2391930Swollman default: 24028895Scharnier warnx("can't find the master of %s: reason: %s", 241121551Speter ypml->ypml_name, yperr_string(r)); 2421930Swollman break; 2431930Swollman } 244121551Speter y = ypml->ypml_next; 2451930Swollman free(ypml); 2461930Swollman } 2471930Swollman break; 2481930Swollman case YPERR_YPBIND: 24928895Scharnier errx(ERR_NOYPBIND, "not running ypbind"); 2501930Swollman default: 251127856Scharnier errx(ERR_NOMASTER, "can't get map list for domain %s: reason: %s", 25287672Smarkm domnam, yperr_string(r)); 2531930Swollman } 2541930Swollman exit(0); 2551930Swollman} 256