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