ypwhich.c revision 91377
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#include <sys/cdefs.h> 3128895Scharnier__FBSDID("$FreeBSD: head/usr.bin/ypwhich/ypwhich.c 91377 2002-02-27 14:32:43Z dwmalone $"); 3228895Scharnier 3328895Scharnier#include <sys/param.h> 341930Swollman#include <sys/types.h> 351930Swollman#include <sys/socket.h> 361930Swollman 371930Swollman#include <rpcsvc/ypclnt.h> 381930Swollman 3928895Scharnier#include <netinet/in.h> 401930Swollman 4128895Scharnier#include <arpa/inet.h> 4228895Scharnier 4328895Scharnier#include <ctype.h> 4428895Scharnier#include <err.h> 451930Swollman#include <netdb.h> 461930Swollman#include <stdio.h> 4712862Swpaul#include <stdlib.h> 4812862Swpaul#include <string.h> 491930Swollman#include <unistd.h> 501930Swollman#include <rpc/rpc.h> 517990Swpaul#include <rpc/xdr.h> 527990Swpaul#include <rpcsvc/yp.h> 537990Swpaulstruct dom_binding{}; 547990Swpaul 557990Swpaul#define ERR_USAGE 1 /* bad arguments - display 'usage' message */ 567990Swpaul#define ERR_NOSUCHHOST 2 /* no such host */ 571930Swollman#define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */ 581930Swollman#define ERR_NOYPBIND 4 /* ypbind not running */ 591930Swollman#define ERR_NOMASTER 5 /* could not find master server */ 601930Swollman 611930Swollmanextern bool_t xdr_domainname(); 621930Swollman 6327345Speterstruct ypalias { 641930Swollman const char *alias, *name; 651930Swollman} ypaliases[] = { 661930Swollman { "passwd", "passwd.byname" }, 671930Swollman { "master.passwd", "master.passwd.byname" }, 681930Swollman { "group", "group.byname" }, 691930Swollman { "networks", "networks.byaddr" }, 701930Swollman { "hosts", "hosts.byaddr" }, 711930Swollman { "protocols", "protocols.bynumber" }, 721930Swollman { "services", "services.byname" }, 7328895Scharnier { "aliases", "mail.aliases" }, 741930Swollman { "ethers", "ethers.byname" }, 751930Swollman}; 7628895Scharnier 7728895Scharnierstatic void 7828895Scharnierusage(void) 797990Swpaul{ 801930Swollman fprintf(stderr, "%s\n%s\n", 811930Swollman "usage: ypwhich [-d domain] [[-t] -m [mname] | host]", 821930Swollman " ypwhich -x"); 831930Swollman exit(ERR_USAGE); 841930Swollman} 851930Swollman 8628895Scharnier 871930Swollman/* 881930Swollman * Like yp_bind except can query a specific host 891930Swollman */ 901930Swollmanstatic int 911930Swollmanbind_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; 981930Swollman struct in_addr ss_addr; 991930Swollman 1001930Swollman sock = RPC_ANYSOCK; 1011930Swollman tv.tv_sec = 15; 1021930Swollman tv.tv_usec = 0; 10328895Scharnier client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock); 1041930Swollman if (client == NULL) { 1051930Swollman warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND)); 1061930Swollman return (YPERR_YPBIND); 1071930Swollman } 1081930Swollman 1091930Swollman tv.tv_sec = 5; 11012862Swpaul tv.tv_usec = 0; 1111930Swollman r = clnt_call(client, YPBINDPROC_DOMAIN, 11228895Scharnier xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv); 1131930Swollman if (r != RPC_SUCCESS) { 1141930Swollman warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 1151930Swollman clnt_destroy(client); 1161930Swollman return (YPERR_YPBIND); 11728895Scharnier } else { 11812862Swpaul if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 1191930Swollman warnx("can't yp_bind: reason: %s", 1201930Swollman ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error)); 1211930Swollman clnt_destroy(client); 1221930Swollman return (r); 1231930Swollman } 1241930Swollman } 12512862Swpaul clnt_destroy(client); 1261930Swollman 1271930Swollman 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)); 1348874Srgrimes return (0); 1351930Swollman} 1361930Swollman 1371930Swollmanint 1381930Swollmanmain(int argc, char *argv[]) 13928895Scharnier{ 1401930Swollman char *domnam = NULL, *master; 1411930Swollman const char *map = NULL; 1421930Swollman struct ypmaplist *ypml, *y; 1431930Swollman struct hostent *hent; 1441930Swollman struct sockaddr_in lsin; 1451930Swollman int notrans, mode, getmap; 1461930Swollman int c, r; 1471930Swollman u_int i; 1481930Swollman 1491930Swollman getmap = notrans = mode = 0; 1501930Swollman while ((c = getopt(argc, argv, "xd:mt")) != -1) 1519594Swollman switch (c) { 1521930Swollman case 'x': 1531930Swollman for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 1541930Swollman printf("\"%s\" is an alias for \"%s\"\n", 1551930Swollman ypaliases[i].alias, 1561930Swollman ypaliases[i].name); 1571930Swollman exit(0); 1581930Swollman case 'd': 1591930Swollman domnam = optarg; 1601930Swollman break; 1611930Swollman case 't': 1621930Swollman notrans++; 1631930Swollman break; 1641930Swollman case 'm': 1651930Swollman mode++; 1661930Swollman break; 1671930Swollman default: 16828895Scharnier usage(); 16928895Scharnier } 17028895Scharnier 1711930Swollman if (!domnam) 1721930Swollman yp_get_default_domain(&domnam); 1731930Swollman 1741930Swollman if (mode == 0) { 1751930Swollman switch (argc-optind) { 1761930Swollman case 0: 1771930Swollman bzero(&lsin, sizeof lsin); 1781930Swollman lsin.sin_family = AF_INET; 1797990Swpaul lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 1801930Swollman 1811930Swollman if (bind_host(domnam, &lsin)) 1821930Swollman exit(ERR_NOBINDING); 1831930Swollman break; 1841930Swollman case 1: 1851930Swollman bzero(&lsin, sizeof lsin); 18628895Scharnier lsin.sin_family = AF_INET; 18728895Scharnier if ((lsin.sin_addr.s_addr = inet_addr(argv[optind])) == INADDR_NONE) { 1881930Swollman hent = gethostbyname(argv[optind]); 1891930Swollman if (!hent) 1901930Swollman errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]); 1911930Swollman bcopy((char *)hent->h_addr_list[0], 1927990Swpaul (char *)&lsin.sin_addr, sizeof lsin.sin_addr); 1931930Swollman } 1941930Swollman if (bind_host(domnam, &lsin)) 1951930Swollman exit(ERR_NOBINDING); 1961930Swollman break; 1971930Swollman default: 1981930Swollman usage(); 1991930Swollman } 2001930Swollman exit(0); 2011930Swollman } 2021930Swollman 2031930Swollman if (argc-optind > 1) 2041930Swollman usage(); 2051930Swollman 2061930Swollman if (argv[optind]) { 2071930Swollman map = argv[optind]; 2081930Swollman for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 2091930Swollman if (strcmp(map, ypaliases[i].alias) == 0) 2101930Swollman map = ypaliases[i].name; 2111930Swollman r = yp_master(domnam, map, &master); 2121930Swollman switch (r) { 2131930Swollman case 0: 2141930Swollman printf("%s\n", master); 21528895Scharnier free(master); 2161930Swollman break; 21728895Scharnier case YPERR_YPBIND: 2181930Swollman errx(ERR_NOYPBIND, "not running ypbind"); 2191930Swollman default: 2201930Swollman errx(ERR_NOMASTER, "can't find master for map %s. reason: %s", 2211930Swollman map, yperr_string(r)); 2221930Swollman } 2231930Swollman exit(0); 2241930Swollman } 2251930Swollman 2261930Swollman ypml = NULL; 2271930Swollman r = yp_maplist(domnam, &ypml); 2281930Swollman switch (r) { 22912862Swpaul case 0: 2301930Swollman for (y = ypml; y;) { 2311930Swollman ypml = y; 23212862Swpaul r = yp_master(domnam, ypml->map, &master); 2331930Swollman switch (r) { 2341930Swollman case 0: 2351930Swollman printf("%s %s\n", ypml->map, master); 23628895Scharnier free(master); 23712862Swpaul break; 2381930Swollman default: 2391930Swollman warnx("can't find the master of %s: reason: %s", 24012862Swpaul ypml->map, yperr_string(r)); 2411930Swollman break; 2421930Swollman } 2431930Swollman y = ypml->next; 2441930Swollman free(ypml); 24528895Scharnier } 2461930Swollman break; 24728895Scharnier case YPERR_YPBIND: 2481930Swollman errx(ERR_NOYPBIND, "not running ypbind"); 2491930Swollman default: 2501930Swollman errx(ERR_NOMASTER, "can't get map list for domain %s. reason: %s", 2511930Swollman domnam, yperr_string(r)); 252 } 253 exit(0); 254} 255