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 const char rcsid[] =
32 "$FreeBSD: src/usr.bin/ypwhich/ypwhich.c,v 1.11.2.1 2002/02/15 00:46:56 des Exp $";
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
49 #include <rpcsvc/yp.h>
51 #include <rpcsvc/ypclnt.h>
53 #define ERR_USAGE 1 /* bad arguments - display 'usage' message */
54 #define ERR_NOSUCHHOST 2 /* no such host */
55 #define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */
56 #define ERR_NOYPBIND 4 /* ypbind not running */
57 #define ERR_NOMASTER 5 /* could not find master server */
59 extern bool_t xdr_domainname();
64 { "passwd", "passwd.byname" },
65 { "master.passwd", "master.passwd.byname" },
66 { "group", "group.byname" },
67 { "networks", "networks.byaddr" },
68 { "hosts", "hosts.byaddr" },
69 { "protocols", "protocols.bynumber" },
70 { "services", "services.byname" },
71 { "aliases", "mail.aliases" },
72 { "ethers", "ethers.byname" },
78 fprintf(stderr, "%s\n%s\n",
79 "usage: ypwhich [-d domain] [[-t] -m [mname] | host]",
86 * Like yp_bind except can query a specific host
91 struct sockaddr_in *sin;
93 struct hostent *hent = NULL;
94 struct ypbind_resp ypbr;
98 struct in_addr ss_addr;
103 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
104 if (client == NULL) {
105 warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND));
106 return (YPERR_YPBIND);
111 r = clnt_call(client, YPBINDPROC_DOMAIN,
112 xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
113 if (r != RPC_SUCCESS) {
114 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
115 clnt_destroy(client);
116 return (YPERR_YPBIND);
118 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
119 warnx("can't yp_bind: reason: %s",
120 ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error));
121 clnt_destroy(client);
125 clnt_destroy(client);
127 ss_addr = *(struct in_addr *)ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
128 /*printf("%08x\n", ss_addr);*/
129 hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
131 printf("%s\n", hent->h_name);
133 printf("%s\n", inet_ntoa(ss_addr));
141 char *domainname = NULL, *master, *map = NULL;
142 struct ypmaplist *ypml, *y;
143 struct hostent *hent;
144 struct sockaddr_in sin;
145 int notrans, mode, getmap;
148 getmap = notrans = mode = 0;
149 while ((c = getopt(argc, argv, "xd:mt")) != -1)
152 for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
153 printf("\"%s\" is an alias for \"%s\"\n",
171 yp_get_default_domain(&domainname);
174 switch (argc-optind) {
176 bzero(&sin, sizeof sin);
177 sin.sin_family = AF_INET;
178 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
180 if (bind_host(domainname, &sin))
184 bzero(&sin, sizeof sin);
185 sin.sin_family = AF_INET;
186 if ((sin.sin_addr.s_addr = inet_addr(argv[optind])) == -1) {
187 hent = gethostbyname(argv[optind]);
189 errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]);
190 bcopy((char *)hent->h_addr_list[0],
191 (char *)&sin.sin_addr, sizeof sin.sin_addr);
193 if (bind_host(domainname, &sin))
207 for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
208 if (strcmp(map, ypaliases[i].alias) == 0)
209 map = ypaliases[i].name;
210 r = yp_master(domainname, map, &master);
213 printf("%s\n", master);
217 errx(ERR_NOYPBIND, "not running ypbind");
219 errx(ERR_NOMASTER, "can't find master for map %s. reason: %s",
220 map, yperr_string(r));
226 r = yp_maplist(domainname, &ypml);
231 r = yp_master(domainname, ypml->map, &master);
234 printf("%s %s\n", ypml->map, master);
238 warnx("can't find the master of %s: reason: %s",
239 ypml->map, yperr_string(r));
247 errx(ERR_NOYPBIND, "not running ypbind");
249 errx(ERR_NOMASTER, "can't get map list for domain %s. reason: %s",
250 domainname, yperr_string(r));