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 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
7 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
52 #include <rpcsvc/yp.h>
53 #include <rpcsvc/ypclnt.h>
55 #include "yplib_host.h"
57 static const struct ypalias {
60 { "passwd", "passwd.byname" },
61 { "master.passwd", "master.passwd.byname" },
62 { "shadow", "shadow.byname" },
63 { "group", "group.byname" },
64 { "networks", "networks.byaddr" },
65 { "hosts", "hosts.byaddr" },
66 { "protocols", "protocols.bynumber" },
67 { "services", "services.byname" },
68 { "aliases", "mail.aliases" },
69 { "ethers", "ethers.byname" },
76 "usage: ypwhich [-t] [-d domain] [[-h] host]\n"
77 " ypwhich [-t] [-d domain] [-h host] -m [mname]\n"
84 * Like yp_bind except can query a specific host
87 bind_host(char *dom, struct sockaddr_in *sin)
89 struct hostent *hent = NULL;
90 struct ypbind_resp ypbr;
91 struct in_addr ss_addr;
99 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
101 if (client == NULL) {
102 warnx("host is not bound to a ypmaster");
103 return (YPERR_YPBIND);
109 r = clnt_call(client, YPBINDPROC_DOMAIN,
110 (xdrproc_t)xdr_domainname, &dom,
111 (xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
112 if (r != RPC_SUCCESS) {
113 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
114 clnt_destroy(client);
115 return (YPERR_YPBIND);
117 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
118 warnx("can't yp_bind: reason: %s",
119 yperr_string(ypbr.ypbind_status));
120 clnt_destroy(client);
124 clnt_destroy(client);
126 memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
129 hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
132 printf("%s\n", hent->h_name);
134 printf("%s\n", inet_ntoa(ss_addr));
140 main(int argc, char *argv[])
142 char *domain, *master, *map = NULL, *host = NULL;
143 int notrans = 0, mode = 0, c, r, i;
144 struct ypmaplist *ypml, *y;
145 struct sockaddr_in sin;
146 struct hostent *hent;
147 CLIENT *client = NULL;
149 yp_get_default_domain(&domain);
151 errx(1, "YP domain name not set");
153 while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
156 for (i = 0; i < nitems(ypaliases); i++)
157 printf("\"%s\" is an alias for \"%s\"\n",
182 memset(&sin, 0, sizeof sin);
183 sin.sin_family = AF_INET;
184 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
186 if (bind_host(domain, &sin))
190 bzero(&sin, sizeof sin);
191 sin.sin_family = AF_INET;
192 if (inet_aton(argv[0], &sin.sin_addr) == 0) {
193 hent = gethostbyname(argv[0]);
195 errx(1, "host %s unknown",
199 if (bind_host(domain, &sin))
212 client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
217 for (i = 0; i < nitems(ypaliases); i++)
218 if (strcmp(map, ypaliases[i].alias) == 0)
219 map = ypaliases[i].name;
223 r = yp_master_host(client, domain, map, &master);
225 r = yp_master(domain, map, &master);
229 printf("%s\n", master);
233 errx(1, "not running ypbind");
235 errx(1, "can't find master for map %s: reason: %s",
236 map, yperr_string(r));
243 r = yp_maplist_host(client, domain, &ypml);
245 r = yp_maplist(domain, &ypml);
250 for (y = ypml; y; ) {
253 r = yp_master_host(client,
254 domain, ypml->map, &master);
256 r = yp_master(domain, ypml->map, &master);
260 printf("%s %s\n", ypml->map, master);
264 warnx("can't find the master of %s: reason: %s",
265 ypml->map, yperr_string(r));
273 errx(1, "not running ypbind");
275 errx(1, "can't get map list for domain %s: reason: %s",
276 domain, yperr_string(r));