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