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