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