]> git.saurik.com Git - apple/network_cmds.git/blob - ypwhich.tproj/ypwhich.c
network_cmds-245.19.tar.gz
[apple/network_cmds.git] / ypwhich.tproj / ypwhich.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
26 * All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by Theo de Raadt.
39 * 4. The name of the author may not be used to endorse or promote
40 * products derived from this software without specific prior written
41 * permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 */
55
56 #ifndef LINT
57 static char rcsid[] = "$Id: ypwhich.c,v 1.1 1999/05/02 03:59:06 wsanchez Exp $";
58 #endif
59
60 #include <sys/param.h>
61 #include <sys/types.h>
62 #include <sys/socket.h>
63 #include <stdio.h>
64 #include <ctype.h>
65 #include <netdb.h>
66 #include <rpc/rpc.h>
67 #include <rpc/xdr.h>
68 #include <rpcsvc/yp.h>
69 //#include <rpcsvc/yp_prot.h>
70 #include <rpcsvc/ypclnt.h>
71 #include <netinet/in.h>
72 #include <arpa/inet.h>
73 #include <string.h>
74 #include <stdlib.h>
75 #include <unistd.h>
76
77 extern bool_t xdr_domainname();
78
79 struct ypalias {
80 char *alias, *name;
81 } ypaliases[] = {
82 { "passwd", "passwd.byname" },
83 { "group", "group.byname" },
84 { "networks", "networks.byaddr" },
85 { "hosts", "hosts.byaddr" },
86 { "protocols", "protocols.bynumber" },
87 { "services", "services.byname" },
88 { "aliases", "mail.aliases" },
89 { "ethers", "ethers.byname" },
90 };
91 static int n_aliases = 8;
92
93 void
94 usage()
95 {
96 fprintf(stderr, "Usage:\n");
97 fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
98 fprintf(stderr, "\typwhich -x\n");
99 exit(1);
100 }
101
102
103 /*
104 * Like yp_bind except can query a specific host
105 */
106 int
107 bind_host(char *dom, struct sockaddr_in *sin)
108 {
109 struct hostent *hent = NULL;
110 struct ypbind_resp ypbr;
111 struct timeval tv;
112 CLIENT *client;
113 int sock, r;
114 struct in_addr addr;
115
116 sock = RPC_ANYSOCK;
117 tv.tv_sec = 15;
118 tv.tv_usec = 0;
119 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
120 if (client==NULL)
121 {
122 fprintf(stderr, "can't clntudp_create: %s\n",
123 yperr_string(YPERR_YPBIND));
124 return YPERR_YPBIND;
125 }
126
127 tv.tv_sec = 5;
128 tv.tv_usec = 0;
129 r = clnt_call(client, YPBINDPROC_DOMAIN,
130 xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
131
132 if (r != RPC_SUCCESS)
133 {
134 fprintf(stderr, "can't clnt_call: %s\n", yperr_string(YPERR_YPBIND));
135 clnt_destroy(client);
136 return YPERR_YPBIND;
137 }
138 else
139 {
140 if (ypbr.ypbind_status != YPBIND_SUCC_VAL)
141 {
142 fprintf(stderr, "can't yp_bind: Reason: %s\n",
143 yperr_string(ypbr.ypbind_status));
144 clnt_destroy(client);
145 return r;
146 }
147 }
148
149 clnt_destroy(client);
150
151 memmove(&addr, ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr, 4);
152
153 hent = gethostbyaddr((char *)&(addr.s_addr), sizeof(u_long), AF_INET);
154 if (hent != NULL) printf("%s\n", hent->h_name);
155 else printf("%s\n", inet_ntoa(addr));
156 return 0;
157 }
158
159 int
160 main(int argc, char *argv[])
161 {
162 char *domainname, *master, *map;
163 ypmaplist *ypml, *y;
164 extern char *optarg;
165 extern int optind;
166 struct hostent *hent;
167 struct sockaddr_in sin;
168 int notrans, mode, getmap;
169 int c, r, i;
170
171 yp_get_default_domain(&domainname);
172
173 map = NULL;
174 getmap = notrans = mode = 0;
175 while ((c = getopt(argc, argv, "xd:mt")) != -1)
176 {
177 switch(c)
178 {
179 case 'x':
180 for (i = 0; i < n_aliases; i++)
181 {
182 printf("Use \"%s\" for \"%s\"\n",
183 ypaliases[i].alias,
184 ypaliases[i].name);
185 }
186 exit(0);
187
188 case 'd':
189 domainname = optarg;
190 break;
191
192 case 't':
193 notrans++;
194 break;
195
196 case 'm':
197 mode++;
198 break;
199
200 default:
201 usage();
202 }
203 }
204
205 if (mode == 0)
206 {
207 switch(argc-optind)
208 {
209 case 0:
210 bzero(&sin, sizeof sin);
211 sin.sin_family = AF_INET;
212 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
213
214 if (bind_host(domainname, &sin)) exit(1);
215 break;
216
217 case 1:
218 bzero(&sin, sizeof sin);
219 sin.sin_family = AF_INET;
220 sin.sin_addr.s_addr = inet_addr(argv[optind]);
221 if (sin.sin_addr.s_addr == -1)
222 {
223 hent = gethostbyname(argv[optind]);
224 if (hent == NULL)
225 {
226 fprintf(stderr, "ypwhich: host %s unknown\n",
227 argv[optind]);
228 exit(1);
229 }
230
231 bcopy((char *)hent->h_addr_list[0],
232 (char *)&sin.sin_addr, sizeof sin.sin_addr);
233 }
234
235 if (bind_host(domainname, &sin)) exit(1);
236 break;
237
238 default:
239 usage();
240 }
241
242 exit(0);
243 }
244
245 if (argc-optind > 1) usage();
246
247 if (argv[optind])
248 {
249 map = argv[optind];
250 for (i = 0; (!notrans) && (i < n_aliases); i++)
251 {
252 if (!strcmp(map, ypaliases[i].alias)) map = ypaliases[i].name;
253 }
254
255 r = yp_master(domainname, map, &master);
256 switch(r)
257 {
258 case 0:
259 printf("%s\n", master);
260 free(master);
261 break;
262 case YPERR_YPBIND:
263 fprintf(stderr, "ypwhich: not running ypbind\n");
264 exit(1);
265 default:
266 fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
267 map, yperr_string(r));
268 exit(1);
269 }
270 exit(0);
271 }
272
273 ypml = NULL;
274 r = yp_maplist(domainname, &ypml);
275 switch(r)
276 {
277 case 0:
278 while (ypml != NULL)
279 {
280 r = yp_master(domainname, ypml->map, &master);
281 switch(r)
282 {
283 case 0:
284 printf("%s %s\n", ypml->map, master);
285 free(master);
286 break;
287 default:
288 fprintf(stderr,
289 "YP: can't find the master of %s: Reason: %s\n",
290 ypml->map, yperr_string(r));
291 break;
292 }
293
294 y = ypml;
295 ypml = ypml->next;
296 free(y);
297 }
298 break;
299
300 case YPERR_YPBIND:
301 fprintf(stderr, "ypwhich: not running ypbind\n");
302 exit(1);
303
304 default:
305 fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
306 domainname, yperr_string(r));
307 exit(1);
308 }
309
310 exit(0);
311 }