1 /* $OpenBSD: ypwhich.c,v 1.23 2015/02/08 23:40:35 deraadt Exp $ */
2 /* $NetBSD: ypwhich.c,v 1.6 1996/05/13 02:43:48 thorpej Exp $ */
5 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
50 #include <rpcsvc/yp.h>
51 #include <rpcsvc/ypclnt.h>
53 #include "yplib_host.h"
55 static const struct ypalias {
58 { "passwd", "passwd.byname" },
59 { "master.passwd", "master.passwd.byname" },
60 { "shadow", "shadow.byname" },
61 { "group", "group.byname" },
62 { "networks", "networks.byaddr" },
63 { "hosts", "hosts.byaddr" },
64 { "protocols", "protocols.bynumber" },
65 { "services", "services.byname" },
66 { "aliases", "mail.aliases" },
67 { "ethers", "ethers.byname" },
74 "usage: ypwhich [-t] [-d domain] [[-h] host]\n"
75 " ypwhich [-t] [-d domain] [-h host] -m [mname]\n"
82 * Like yp_bind except can query a specific host
85 bind_host(char *dom, struct sockaddr_in *sin)
87 struct hostent *hent = NULL;
88 struct ypbind_resp ypbr;
89 struct in_addr ss_addr;
97 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
100 warnx("host is not bound to a ypmaster");
101 return (YPERR_YPBIND);
107 r = clnt_call(client, YPBINDPROC_DOMAIN,
108 (xdrproc_t)xdr_domainname, &dom,
109 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
110 if (r != RPC_SUCCESS) {
111 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
112 clnt_destroy(client);
113 return (YPERR_YPBIND);
115 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
116 warnx("can't yp_bind: reason: %s",
117 yperr_string(ypbr.ypbind_status));
118 clnt_destroy(client);
122 clnt_destroy(client);
124 memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
127 hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
130 printf("%s\n", hent->h_name);
132 printf("%s\n", inet_ntoa(ss_addr));
138 main(int argc, char *argv[])
140 char *domain, *master, *map = NULL, *host = NULL;
141 int notrans = 0, mode = 0, c, r, i;
142 struct ypmaplist *ypml, *y;
143 struct sockaddr_in sin;
144 struct hostent *hent;
145 CLIENT *client = NULL;
147 yp_get_default_domain(&domain);
149 errx(1, "YP domain name not set");
151 while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
154 for (i = 0; i < nitems(ypaliases); i++)
155 printf("\"%s\" is an alias for \"%s\"\n",
180 memset(&sin, 0, sizeof sin);
181 sin.sin_family = AF_INET;
182 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
184 if (bind_host(domain, &sin))
188 bzero(&sin, sizeof sin);
189 sin.sin_family = AF_INET;
190 if (inet_aton(argv[0], &sin.sin_addr) == 0) {
191 hent = gethostbyname(argv[0]);
193 errx(1, "host %s unknown",
197 if (bind_host(domain, &sin))
210 client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
215 for (i = 0; i < nitems(ypaliases); i++)
216 if (strcmp(map, ypaliases[i].alias) == 0)
217 map = ypaliases[i].name;
221 r = yp_master_host(client, domain, map, &master);
223 r = yp_master(domain, map, &master);
227 printf("%s\n", master);
231 errx(1, "not running ypbind");
233 errx(1, "can't find master for map %s: reason: %s",
234 map, yperr_string(r));
241 r = yp_maplist_host(client, domain, &ypml);
243 r = yp_maplist(domain, &ypml);
248 for (y = ypml; y; ) {
251 r = yp_master_host(client,
252 domain, ypml->map, &master);
254 r = yp_master(domain, ypml->map, &master);
258 printf("%s %s\n", ypml->map, master);
262 warnx("can't find the master of %s: reason: %s",
263 ypml->map, yperr_string(r));
271 errx(1, "not running ypbind");
273 errx(1, "can't get map list for domain %s: reason: %s",
274 domain, yperr_string(r));