xref: /openbsd/usr.bin/ypwhich/ypwhich.c (revision db3296cf)
1 /*	$OpenBSD: ypwhich.c,v 1.16 2003/07/06 23:26:18 deraadt Exp $	*/
2 /*	$NetBSD: ypwhich.c,v 1.6 1996/05/13 02:43:48 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
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
31 static char rcsid[] = "$Id: ypwhich.c,v 1.16 2003/07/06 23:26:18 deraadt Exp $";
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <netdb.h>
47 #include <err.h>
48 
49 #include <rpc/rpc.h>
50 #include <rpc/xdr.h>
51 #include <rpcsvc/yp.h>
52 #include <rpcsvc/ypclnt.h>
53 
54 #include "yplib_host.h"
55 
56 struct ypalias {
57 	char *alias, *name;
58 } ypaliases[] = {
59 	{ "passwd", "passwd.byname" },
60 	{ "group", "group.byname" },
61 	{ "networks", "networks.byaddr" },
62 	{ "hosts", "hosts.byaddr" },
63 	{ "protocols", "protocols.bynumber" },
64 	{ "services", "services.byname" },
65 	{ "aliases", "mail.aliases" },
66 	{ "ethers", "ethers.byname" },
67 };
68 
69 int	bind_host(char *dom, struct sockaddr_in *sin);
70 
71 static void
72 usage(void)
73 {
74 	fprintf(stderr,
75 	    "usage: ypwhich [-d domain] [[-h host] [-t] -m [mname] | host]\n");
76 	fprintf(stderr, "       ypwhich -x\n");
77 	exit(1);
78 }
79 
80 
81 /*
82  * Like yp_bind except can query a specific host
83  */
84 int
85 bind_host(char *dom, struct sockaddr_in *sin)
86 {
87 	struct hostent *hent = NULL;
88 	struct ypbind_resp ypbr;
89 	struct in_addr ss_addr;
90 	struct timeval tv;
91 	CLIENT *client;
92 	int sock, r;
93 
94 	sock = RPC_ANYSOCK;
95 	tv.tv_sec = 15;
96 	tv.tv_usec = 0;
97 	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
98 
99 	if (client == NULL) {
100 		fprintf(stderr, "ypwhich: host is not bound to a ypmaster\n");
101 		return YPERR_YPBIND;
102 	}
103 
104 	tv.tv_sec = 5;
105 	tv.tv_usec = 0;
106 
107 	r = clnt_call(client, YPBINDPROC_DOMAIN,
108 	    xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
109 	if (r != RPC_SUCCESS) {
110 		fprintf(stderr, "can't clnt_call: %s\n",
111 		    yperr_string(YPERR_YPBIND));
112 		clnt_destroy(client);
113 		return YPERR_YPBIND;
114 	} else {
115 		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
116 			fprintf(stderr, "can't yp_bind: Reason: %s\n",
117 			    yperr_string(ypbr.ypbind_status));
118 			clnt_destroy(client);
119 			return r;
120 		}
121 	}
122 	clnt_destroy(client);
123 
124 	memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
125 	    sizeof (ss_addr));
126 
127 	hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
128 	    AF_INET);
129 	if (hent != NULL)
130 		printf("%s\n", hent->h_name);
131 	else
132 		printf("%s\n", inet_ntoa(ss_addr));
133 
134 	return 0;
135 }
136 
137 int
138 main(int argc, char *argv[])
139 {
140 	char *domain, *master, *map = NULL, *host = NULL;
141 	int notrans, mode, getmap, c, r, i;
142 	struct ypmaplist *ypml, *y;
143 	struct sockaddr_in sin;
144 	struct hostent *hent;
145 	CLIENT *client = NULL;
146 
147 	getmap = notrans = mode = 0;
148 
149 	yp_get_default_domain(&domain);
150 	if (domain == NULL)
151 		errx(1, "YP domain name not set");
152 
153 	while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
154 		switch (c) {
155 		case 'x':
156 			for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
157 				printf("Use \"%s\" for \"%s\"\n",
158 				    ypaliases[i].alias, ypaliases[i].name);
159 			exit(0);
160 		case 'h':
161 			host = optarg;
162 			break;
163 		case 'd':
164 			domain = optarg;
165 			break;
166 		case 't':
167 			notrans++;
168 			break;
169 		case 'm':
170 			mode++;
171 			break;
172 		default:
173 			usage();
174 		}
175 	argc -= optind;
176 	argv += optind;
177 
178 	if (mode == 0) {
179 		switch (argc) {
180 		case 0:
181 			memset(&sin, 0, sizeof sin);
182 			sin.sin_family = AF_INET;
183 			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
184 
185 			if (bind_host(domain, &sin))
186 				exit(1);
187 			break;
188 		case 1:
189 			bzero(&sin, sizeof sin);
190 			sin.sin_family = AF_INET;
191 			if (inet_aton(argv[0], &sin.sin_addr) == 0) {
192 				hent = gethostbyname(argv[0]);
193 				if (!hent) {
194 					fprintf(stderr, "ypwhich: host %s unknown\n",
195 					    argv[0]);
196 					exit(1);
197 				}
198 				bcopy((char *)hent->h_addr,
199 				    (char *)&sin.sin_addr, sizeof sin.sin_addr);
200 			}
201 			if (bind_host(domain, &sin))
202 				exit(1);
203 			break;
204 		default:
205 			usage();
206 		}
207 		exit(0);
208 	}
209 
210 	if (argc > 1)
211 		usage();
212 
213 	if (host != NULL)
214 		client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
215 
216 	if (argv[0]) {
217 		map = argv[0];
218 		for (i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
219 			if (strcmp(map, ypaliases[i].alias) == 0)
220 				map = ypaliases[i].name;
221 
222 		if (host != NULL)
223 			r = yp_master_host(client, domain, map, &master);
224 		else
225 			r = yp_master(domain, map, &master);
226 
227 		switch (r) {
228 		case 0:
229 			printf("%s\n", master);
230 			free(master);
231 			break;
232 		case YPERR_YPBIND:
233 			fprintf(stderr, "ypwhich: not running ypbind\n");
234 			exit(1);
235 		default:
236 			fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
237 			    map, yperr_string(r));
238 			exit(1);
239 		}
240 		exit(0);
241 	}
242 
243 	ypml = NULL;
244 	if (host != NULL)
245 		r = yp_maplist_host(client, domain, &ypml);
246 	else
247 		r = yp_maplist(domain, &ypml);
248 
249 	r = 0;
250 	switch (r) {
251 	case 0:
252 		for (y = ypml; y; ) {
253 			ypml = y;
254 			if (host != NULL) {
255 				r = yp_master_host(client,
256 						   domain, ypml->map, &master);
257 			} else {
258 				r = yp_master(domain, ypml->map, &master);
259 			}
260 			switch (r) {
261 			case 0:
262 				printf("%s %s\n", ypml->map, master);
263 				free(master);
264 				break;
265 			default:
266 				fprintf(stderr,
267 				    "YP: can't find the master of %s: Reason: %s\n",
268 				    ypml->map, yperr_string(r));
269 				break;
270 			}
271 			y = ypml->next;
272 			free(ypml);
273 		}
274 		break;
275 	case YPERR_YPBIND:
276 		fprintf(stderr, "ypwhich: not running ypbind\n");
277 		exit(1);
278 	default:
279 		fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
280 		    domain, yperr_string(r));
281 		exit(1);
282 	}
283 	exit(0);
284 }
285