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