1/*
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
15 *    permission.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: src/usr.bin/ypwhich/ypwhich.c,v 1.19 2009/12/13 03:14:06 delphij Exp $");
32
33#include <sys/param.h>
34#include <sys/types.h>
35#include <sys/socket.h>
36
37#include <rpc/rpc.h>
38#include <rpc/xdr.h>
39#include <rpcsvc/yp_prot.h>
40#include <rpcsvc/ypclnt.h>
41
42#include <netinet/in.h>
43
44#include <arpa/inet.h>
45
46#include <ctype.h>
47#include <err.h>
48#include <netdb.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#define ERR_USAGE	1	/* bad arguments - display 'usage' message */
55#define ERR_NOSUCHHOST	2	/* no such host */
56#define ERR_NOBINDING	3	/* error from ypbind -- domain not bound */
57#define ERR_NOYPBIND	4	/* ypbind not running */
58#define ERR_NOMASTER	5	/* could not find master server */
59
60extern bool_t xdr_domainname();
61
62struct ypalias {
63	char *alias, *name;
64} ypaliases[] = {
65	{ "passwd", "passwd.byname" },
66	{ "master.passwd", "master.passwd.byname" },
67	{ "shadow", "shadow.byname" },
68	{ "group", "group.byname" },
69	{ "networks", "networks.byaddr" },
70	{ "hosts", "hosts.byaddr" },
71	{ "protocols", "protocols.bynumber" },
72	{ "services", "services.byname" },
73	{ "aliases", "mail.aliases" },
74	{ "ethers", "ethers.byname" },
75};
76
77static void
78usage(void)
79{
80	fprintf(stderr, "%s\n%s\n",
81		"usage: ypwhich [-d domain] [[-t] -m [mname] | host]",
82		"       ypwhich -x");
83	exit(ERR_USAGE);
84}
85
86
87/*
88 * Like yp_bind except can query a specific host
89 */
90static int
91bind_host(char *dom, struct sockaddr_in *lsin)
92{
93	struct hostent *hent = NULL;
94	struct ypbind_resp ypbr;
95	struct timeval tv;
96	CLIENT *client;
97	int sock, r;
98	struct in_addr ss_addr;
99
100	sock = RPC_ANYSOCK;
101	tv.tv_sec = 15;
102	tv.tv_usec = 0;
103	client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock);
104	if (client == NULL) {
105		warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND));
106		return (YPERR_YPBIND);
107	}
108
109	tv.tv_sec = 5;
110	tv.tv_usec = 0;
111	r = clnt_call(client, YPBINDPROC_DOMAIN,
112		(xdrproc_t)xdr_domainname, &dom,
113		(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
114	if (r != RPC_SUCCESS) {
115		warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
116		clnt_destroy(client);
117		return (YPERR_YPBIND);
118	} else {
119		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
120			warnx("can't yp_bind: reason: %s",
121#ifdef __APPLE__
122				yperr_string(ypbr.ypbind_status));
123#else
124				ypbinderr_string(ypbr.ypbind_respbody.ypbind_error));
125#endif
126			clnt_destroy(client);
127			return (r);
128		}
129	}
130	clnt_destroy(client);
131
132	ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
133	/*printf("%08x\n", ss_addr);*/
134	hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
135	if (hent)
136		printf("%s\n", hent->h_name);
137	else
138		printf("%s\n", inet_ntoa(ss_addr));
139	return (0);
140}
141
142int
143main(int argc, char *argv[])
144{
145	char *domnam = NULL, *master;
146	char *map = NULL;
147	struct ypmaplist *ypml, *y;
148	struct hostent *hent;
149	struct sockaddr_in lsin;
150	int notrans, mode, getmap;
151	int c, r;
152	u_int i;
153
154	getmap = notrans = mode = 0;
155	while ((c = getopt(argc, argv, "xd:mt")) != -1)
156		switch (c) {
157		case 'x':
158			for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
159				printf("\"%s\" is an alias for \"%s\"\n",
160					ypaliases[i].alias,
161					ypaliases[i].name);
162			exit(0);
163		case 'd':
164			domnam = optarg;
165			break;
166		case 't':
167			notrans++;
168			break;
169		case 'm':
170			mode++;
171			break;
172		default:
173			usage();
174		}
175
176	if (!domnam)
177		yp_get_default_domain(&domnam);
178
179	if (mode == 0) {
180		switch (argc-optind) {
181		case 0:
182			bzero(&lsin, sizeof lsin);
183			lsin.sin_family = AF_INET;
184			lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
185
186			if (bind_host(domnam, &lsin))
187				exit(ERR_NOBINDING);
188			break;
189		case 1:
190			bzero(&lsin, sizeof lsin);
191			lsin.sin_family = AF_INET;
192			if ((lsin.sin_addr.s_addr = inet_addr(argv[optind])) == INADDR_NONE) {
193				hent = gethostbyname(argv[optind]);
194				if (!hent)
195					errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]);
196				bcopy((char *)hent->h_addr_list[0],
197					(char *)&lsin.sin_addr, sizeof lsin.sin_addr);
198			}
199			if (bind_host(domnam, &lsin))
200				exit(ERR_NOBINDING);
201			break;
202		default:
203			usage();
204		}
205		exit(0);
206	}
207
208	if (argc-optind > 1)
209		usage();
210
211	if (argv[optind]) {
212		map = argv[optind];
213		for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
214			if (strcmp(map, ypaliases[i].alias) == 0)
215				map = ypaliases[i].name;
216		r = yp_master(domnam, map, &master);
217		switch (r) {
218		case 0:
219			printf("%s\n", master);
220			free(master);
221			break;
222		case YPERR_YPBIND:
223			errx(ERR_NOYPBIND, "not running ypbind");
224		default:
225			errx(ERR_NOMASTER, "can't find master for map %s: reason: %s",
226				map, yperr_string(r));
227		}
228		exit(0);
229	}
230
231	ypml = NULL;
232	r = yp_maplist(domnam, &ypml);
233	switch (r) {
234	case 0:
235		for (y = ypml; y;) {
236			ypml = y;
237			r = yp_master(domnam, ypml->ypml_name, &master);
238			switch (r) {
239			case 0:
240				printf("%s %s\n", ypml->ypml_name, master);
241				free(master);
242				break;
243			default:
244				warnx("can't find the master of %s: reason: %s",
245					ypml->ypml_name, yperr_string(r));
246				break;
247			}
248			y = ypml->ypml_next;
249			free(ypml);
250		}
251		break;
252	case YPERR_YPBIND:
253		errx(ERR_NOYPBIND, "not running ypbind");
254	default:
255		errx(ERR_NOMASTER, "can't get map list for domain %s: reason: %s",
256			domnam, yperr_string(r));
257	}
258	exit(0);
259}
260