xref: /netbsd-src/usr.bin/ypwhich/ypwhich.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1992, 1993 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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Theo de Raadt.
16  * 4. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior written
18  *    permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
21  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
24  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef LINT
34 static char rcsid[] = "$Id: ypwhich.c,v 1.3 1994/05/25 09:55:02 deraadt Exp $";
35 #endif
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <stdio.h>
41 #include <ctype.h>
42 #include <netdb.h>
43 #include <rpc/rpc.h>
44 #include <rpc/xdr.h>
45 #include <rpcsvc/yp_prot.h>
46 #include <rpcsvc/ypclnt.h>
47 
48 extern bool_t xdr_domainname();
49 
50 struct ypalias {
51 	char *alias, *name;
52 } ypaliases[] = {
53 	{ "passwd", "passwd.byname" },
54 	{ "group", "group.byname" },
55 	{ "networks", "networks.byaddr" },
56 	{ "hosts", "hosts.byaddr" },
57 	{ "protocols", "protocols.bynumber" },
58 	{ "services", "services.byname" },
59 	{ "aliases", "mail.aliases" },
60 	{ "ethers", "ethers.byname" },
61 };
62 
63 usage()
64 {
65 	fprintf(stderr, "Usage:\n");
66 	fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
67 	fprintf(stderr, "\typwhich -x\n");
68 	exit(1);
69 }
70 
71 
72 /*
73  * Like yp_bind except can query a specific host
74  */
75 bind_host(dom, sin)
76 char *dom;
77 struct sockaddr_in *sin;
78 {
79 	struct hostent *hent = NULL;
80 	struct ypbind_resp ypbr;
81 	struct dom_binding *ysd;
82 	struct timeval tv;
83 	CLIENT *client;
84 	int sock, r;
85 	u_long ss_addr;
86 
87 	sock = RPC_ANYSOCK;
88 	tv.tv_sec = 15;
89 	tv.tv_usec = 0;
90 	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
91 	if(client==NULL) {
92 		fprintf(stderr, "can't clntudp_create: %s\n",
93 			yperr_string(YPERR_YPBIND));
94 		return YPERR_YPBIND;
95 	}
96 
97 	tv.tv_sec = 5;
98 	tv.tv_usec = 0;
99 	r = clnt_call(client, YPBINDPROC_DOMAIN,
100 		xdr_domainname, dom, xdr_ypbind_resp, &ypbr, tv);
101 	if( r != RPC_SUCCESS) {
102 		fprintf(stderr, "can't clnt_call: %s\n",
103 			yperr_string(YPERR_YPBIND));
104 		clnt_destroy(client);
105 		return YPERR_YPBIND;
106 	} else {
107 		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
108 			fprintf(stderr, "can't yp_bind: Reason: %s\n",
109 				yperr_string(ypbr.ypbind_status));
110 			clnt_destroy(client);
111 			return r;
112 		}
113 	}
114 	clnt_destroy(client);
115 
116 	ss_addr = ypbr.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr.s_addr;
117 	/*printf("%08x\n", ss_addr);*/
118 	hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
119 	if (hent)
120 		printf("%s\n", hent->h_name);
121 	else
122 		printf("%s\n", inet_ntoa(ss_addr));
123 	return 0;
124 }
125 
126 int
127 main(argc, argv)
128 char **argv;
129 {
130 	char *domainname, *master, *map;
131 	struct ypmaplist *ypml, *y;
132 	extern char *optarg;
133 	extern int optind;
134 	struct hostent *hent;
135 	struct sockaddr_in sin;
136 	int notrans, mode, getmap;
137 	int c, r, i;
138 
139 	yp_get_default_domain(&domainname);
140 
141 	map = NULL;
142 	getmap = notrans = mode = 0;
143 	while( (c=getopt(argc, argv, "xd:mt")) != -1)
144 		switch(c) {
145 		case 'x':
146 			for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
147 				printf("Use \"%s\" for \"%s\"\n",
148 					ypaliases[i].alias,
149 					ypaliases[i].name);
150 			exit(0);
151 		case 'd':
152 			domainname = optarg;
153 			break;
154 		case 't':
155 			notrans++;
156 			break;
157 		case 'm':
158 			mode++;
159 			break;
160 		default:
161 			usage();
162 		}
163 
164 	if(mode==0) {
165 		switch(argc-optind) {
166 		case 0:
167 			bzero(&sin, sizeof sin);
168 			sin.sin_family = AF_INET;
169 			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
170 
171 			if(bind_host(domainname, &sin))
172 				exit(1);
173 			break;
174 		case 1:
175 			bzero(&sin, sizeof sin);
176 			sin.sin_family = AF_INET;
177 			if( (sin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) {
178 				hent = gethostbyname(argv[optind]);
179 				if(!hent) {
180 					fprintf(stderr, "ypwhich: host %s unknown\n",
181 						argv[optind]);
182 					exit(1);
183 				}
184 				bcopy((char *)hent->h_addr_list[0],
185 					(char *)&sin.sin_addr, sizeof sin.sin_addr);
186 			}
187 			if(bind_host(domainname, &sin))
188 				exit(1);
189 			break;
190 		default:
191 			usage();
192 		}
193 		exit(0);
194 	}
195 
196 	if( argc-optind > 1)
197 		usage();
198 
199 	if(argv[optind]) {
200 		map = argv[optind];
201 		for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
202 			if( strcmp(map, ypaliases[i].alias) == 0)
203 				map = ypaliases[i].name;
204 		r = yp_master(domainname, map, &master);
205 		switch(r) {
206 		case 0:
207 			printf("%s\n", master);
208 			free(master);
209 			break;
210 		case YPERR_YPBIND:
211 			fprintf(stderr, "ypwhich: not running ypbind\n");
212 			exit(1);
213 		default:
214 			fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
215 				map, yperr_string(r));
216 			exit(1);
217 		}
218 		exit(0);
219 	}
220 
221 	ypml = NULL;
222 	r = yp_maplist(domainname, &ypml);
223 	switch(r) {
224 	case 0:
225 		for(y=ypml; y; ) {
226 			ypml = y;
227 			r = yp_master(domainname, ypml->ypml_name, &master);
228 			switch(r) {
229 			case 0:
230 				printf("%s %s\n", ypml->ypml_name, master);
231 				free(master);
232 				break;
233 			default:
234 				fprintf(stderr,
235 					"YP: can't find the master of %s: Reason: %s\n",
236 					ypml->ypml_name, yperr_string(r));
237 				break;
238 			}
239 			y = ypml->ypml_next;
240 			free(ypml);
241 		}
242 		break;
243 	case YPERR_YPBIND:
244 		fprintf(stderr, "ypwhich: not running ypbind\n");
245 		exit(1);
246 	default:
247 		fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
248 			domainname, yperr_string(r));
249 		exit(1);
250 	}
251 	exit(0);
252 }
253