2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static char rcsid[] = "ypwhich.c,v 1.2 1993/05/16 02:49:10 deraadt Exp";
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
42 #include <rpcsvc/yp_prot.h>
43 #include <rpcsvc/ypclnt.h>
45 extern bool_t xdr_domainname();
50 { "passwd", "passwd.byname" },
51 { "group", "group.byname" },
52 { "networks", "networks.byaddr" },
53 { "hosts", "hosts.byaddr" },
54 { "protocols", "protocols.bynumber" },
55 { "services", "services.byname" },
56 { "aliases", "mail.aliases" },
57 { "ethers", "ethers.byname" },
62 fprintf(stderr, "Usage:\n");
63 fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
64 fprintf(stderr, "\typwhich -x\n");
70 * Like yp_bind except can query a specific host
74 struct sockaddr_in *sin;
76 struct hostent *hent = NULL;
77 struct ypbind_resp ypbr;
78 struct dom_binding *ysd;
87 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
89 fprintf(stderr, "can't clntudp_create: %s\n",
90 yperr_string(YPERR_YPBIND));
96 r = clnt_call(client, YPBINDPROC_DOMAIN,
97 xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
98 if( r != RPC_SUCCESS) {
99 fprintf(stderr, "can't clnt_call: %s\n",
100 yperr_string(YPERR_YPBIND));
101 clnt_destroy(client);
104 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
105 fprintf(stderr, "can't yp_bind: Reason: %s\n",
106 yperr_string(ypbr.ypbind_status));
107 clnt_destroy(client);
111 clnt_destroy(client);
113 ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
114 /*printf("%08x\n", ss_addr);*/
115 hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
117 printf("%s\n", hent->h_name);
119 printf("%s\n", inet_ntoa(ss_addr));
127 char *domainname, *master, *map;
128 struct ypmaplist *ypml, *y;
131 struct hostent *hent;
132 struct sockaddr_in sin;
133 int notrans, mode, getmap;
136 yp_get_default_domain(&domainname);
139 getmap = notrans = mode = 0;
140 while( (c=getopt(argc, argv, "xd:mt")) != -1)
143 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
144 printf("Use \"%s\" for \"%s\"\n",
162 switch(argc-optind) {
164 bzero(&sin, sizeof sin);
165 sin.sin_family = AF_INET;
166 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
168 if(bind_host(domainname, &sin))
172 bzero(&sin, sizeof sin);
173 sin.sin_family = AF_INET;
174 if( (sin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) {
175 hent = gethostbyname(argv[optind]);
177 fprintf(stderr, "ypwhich: host %s unknown\n",
181 bcopy((char *)hent->h_addr_list[0],
182 (char *)&sin.sin_addr, sizeof sin.sin_addr);
184 if(bind_host(domainname, &sin))
198 for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
199 if( strcmp(map, ypaliases[i].alias) == 0)
200 map = ypaliases[i].name;
201 r = yp_master(domainname, map, &master);
204 printf("%s\n", master);
208 fprintf(stderr, "ypwhich: not running ypbind\n");
211 fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
212 map, yperr_string(r));
219 r = yp_maplist(domainname, &ypml);
224 r = yp_master(domainname, ypml->ypml_name, &master);
227 printf("%s %s\n", ypml->ypml_name, master);
232 "YP: can't find the master of %s: Reason: %s\n",
233 ypml->ypml_name, yperr_string(r));
241 fprintf(stderr, "ypwhich: not running ypbind\n");
244 fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
245 domainname, yperr_string(r));