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