ypwhich.c revision 12862
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 char rcsid[] = "ypwhich.c,v 1.2 1993/05/16 02:49:10 deraadt Exp";
32#endif
33
34#include <sys/param.h>
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <stdio.h>
38#include <ctype.h>
39#include <netdb.h>
40#include <rpc/rpc.h>
41#include <rpc/xdr.h>
42#include <rpcsvc/yp.h>
43struct dom_binding{};
44#include <rpcsvc/ypclnt.h>
45
46#define ERR_USAGE	1	/* bad arguments - display 'usage' message */
47#define ERR_NOSUCHHOST	2	/* no such host */
48#define ERR_NOBINDING	3	/* error from ypbind -- domain not bound */
49#define ERR_NOYPBIND	4	/* ypbind not running */
50#define ERR_NOMASTER	5	/* could not find master server */
51
52extern bool_t xdr_domainname();
53
54struct ypalias {
55	char *alias, *name;
56} ypaliases[] = {
57	{ "passwd", "passwd.byname" },
58	{ "group", "group.byname" },
59	{ "networks", "networks.byaddr" },
60	{ "hosts", "hosts.byaddr" },
61	{ "protocols", "protocols.bynumber" },
62	{ "services", "services.byname" },
63	{ "aliases", "mail.aliases" },
64	{ "ethers", "ethers.byname" },
65};
66
67usage()
68{
69	fprintf(stderr, "Usage:\n");
70	fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
71	fprintf(stderr, "\typwhich -x\n");
72	exit(ERR_USAGE);
73}
74
75
76/*
77 * Like yp_bind except can query a specific host
78 */
79bind_host(dom, sin)
80char *dom;
81struct sockaddr_in *sin;
82{
83	struct hostent *hent = NULL;
84	struct ypbind_resp ypbr;
85	struct dom_binding *ysd;
86	struct timeval tv;
87	CLIENT *client;
88	int sock, r;
89	u_long ss_addr;
90
91	sock = RPC_ANYSOCK;
92	tv.tv_sec = 15;
93	tv.tv_usec = 0;
94	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
95	if(client==NULL) {
96		fprintf(stderr, "can't clntudp_create: %s\n",
97			yperr_string(YPERR_YPBIND));
98		return YPERR_YPBIND;
99	}
100
101	tv.tv_sec = 5;
102	tv.tv_usec = 0;
103	r = clnt_call(client, YPBINDPROC_DOMAIN,
104		xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
105	if( r != RPC_SUCCESS) {
106		fprintf(stderr, "can't clnt_call: %s\n",
107			yperr_string(YPERR_YPBIND));
108		clnt_destroy(client);
109		return YPERR_YPBIND;
110	} else {
111		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
112			fprintf(stderr, "can't yp_bind: Reason: %s\n",
113				ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error));
114			clnt_destroy(client);
115			return r;
116		}
117	}
118	clnt_destroy(client);
119
120	ss_addr = *(u_long *)ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
121	/*printf("%08x\n", ss_addr);*/
122	hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
123	if (hent)
124		printf("%s\n", hent->h_name);
125	else
126		printf("%s\n", inet_ntoa(ss_addr));
127	return 0;
128}
129
130int
131main(argc, argv)
132char **argv;
133{
134	char *domainname, *master, *map;
135	struct ypmaplist *ypml, *y;
136	extern char *optarg;
137	extern int optind;
138	struct hostent *hent;
139	struct sockaddr_in sin;
140	int notrans, mode, getmap;
141	int c, r, i;
142
143	yp_get_default_domain(&domainname);
144
145	map = NULL;
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(mode==0) {
169		switch(argc-optind) {
170		case 0:
171			bzero(&sin, sizeof sin);
172			sin.sin_family = AF_INET;
173			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
174
175			if(bind_host(domainname, &sin))
176				exit(ERR_NOBINDING);
177			break;
178		case 1:
179			bzero(&sin, sizeof sin);
180			sin.sin_family = AF_INET;
181			if( (sin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) {
182				hent = gethostbyname(argv[optind]);
183				if(!hent) {
184					fprintf(stderr, "ypwhich: host %s unknown\n",
185						argv[optind]);
186					exit(ERR_NOSUCHHOST);
187				}
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			fprintf(stderr, "ypwhich: not running ypbind\n");
216			exit(ERR_NOYPBIND);
217		default:
218			fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
219				map, yperr_string(r));
220			exit(ERR_NOMASTER);
221		}
222		exit(0);
223	}
224
225	ypml = NULL;
226	r = yp_maplist(domainname, &ypml);
227	switch(r) {
228	case 0:
229		for(y=ypml; y; ) {
230			ypml = y;
231			r = yp_master(domainname, ypml->map, &master);
232			switch(r) {
233			case 0:
234				printf("%s %s\n", ypml->map, master);
235				free(master);
236				break;
237			default:
238				fprintf(stderr,
239					"YP: can't find the master of %s: Reason: %s\n",
240					ypml->map, yperr_string(r));
241				break;
242			}
243			y = ypml->next;
244			free(ypml);
245		}
246		break;
247	case YPERR_YPBIND:
248		fprintf(stderr, "ypwhich: not running ypbind\n");
249		exit(ERR_NOYPBIND);
250	default:
251		fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
252			domainname, yperr_string(r));
253		exit(ERR_NOMASTER);
254	}
255	exit(0);
256}
257