Remove tm.h and xm.h handling, as it wasn't used. Use nm.h only when needed.
[dragonfly.git] / usr.bin / ypwhich / ypwhich.c
blobc3d2c2b3d8bb004942847b52159e559f65d4d1c9
1 /*
2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca>
3 * All rights reserved.
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.
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.
29 * $FreeBSD: src/usr.bin/ypwhich/ypwhich.c,v 1.11.2.1 2002/02/15 00:46:56 des Exp $
30 * $DragonFly: src/usr.bin/ypwhich/ypwhich.c,v 1.3 2003/10/04 20:36:55 hmp Exp $
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <netdb.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <rpc/rpc.h>
46 #include <rpc/xdr.h>
47 #include <rpcsvc/yp.h>
48 struct dom_binding{};
49 #include <rpcsvc/ypclnt.h>
51 #define ERR_USAGE 1 /* bad arguments - display 'usage' message */
52 #define ERR_NOSUCHHOST 2 /* no such host */
53 #define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */
54 #define ERR_NOYPBIND 4 /* ypbind not running */
55 #define ERR_NOMASTER 5 /* could not find master server */
57 extern bool_t xdr_domainname();
59 struct ypalias {
60 char *alias, *name;
61 } ypaliases[] = {
62 { "passwd", "passwd.byname" },
63 { "master.passwd", "master.passwd.byname" },
64 { "group", "group.byname" },
65 { "networks", "networks.byaddr" },
66 { "hosts", "hosts.byaddr" },
67 { "protocols", "protocols.bynumber" },
68 { "services", "services.byname" },
69 { "aliases", "mail.aliases" },
70 { "ethers", "ethers.byname" },
73 static void
74 usage(void)
76 fprintf(stderr, "%s\n%s\n",
77 "usage: ypwhich [-d domain] [[-t] -m [mname] | host]",
78 " ypwhich -x");
79 exit(ERR_USAGE);
84 * Like yp_bind except can query a specific host
86 int
87 bind_host(char *dom, struct sockaddr_in *sin)
89 struct hostent *hent = NULL;
90 struct ypbind_resp ypbr;
91 struct timeval tv;
92 CLIENT *client;
93 int sock, r;
94 struct in_addr ss_addr;
96 sock = RPC_ANYSOCK;
97 tv.tv_sec = 15;
98 tv.tv_usec = 0;
99 client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
100 if (client == NULL) {
101 warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND));
102 return (YPERR_YPBIND);
105 tv.tv_sec = 5;
106 tv.tv_usec = 0;
107 r = clnt_call(client, YPBINDPROC_DOMAIN,
108 xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
109 if (r != RPC_SUCCESS) {
110 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
111 clnt_destroy(client);
112 return (YPERR_YPBIND);
113 } else {
114 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
115 warnx("can't yp_bind: reason: %s",
116 ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error));
117 clnt_destroy(client);
118 return (r);
121 clnt_destroy(client);
123 ss_addr = *(struct in_addr *)ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr;
124 /*printf("%08x\n", ss_addr);*/
125 hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
126 if (hent)
127 printf("%s\n", hent->h_name);
128 else
129 printf("%s\n", inet_ntoa(ss_addr));
130 return (0);
134 main(int argc, char **argv)
136 char *domainname = NULL, *master, *map = NULL;
137 struct ypmaplist *ypml, *y;
138 struct hostent *hent;
139 struct sockaddr_in sin;
140 int notrans, mode, getmap;
141 int c, r, i;
143 getmap = notrans = mode = 0;
144 while ((c = getopt(argc, argv, "xd:mt")) != -1)
145 switch (c) {
146 case 'x':
147 for (i = 0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
148 printf("\"%s\" is an alias for \"%s\"\n",
149 ypaliases[i].alias,
150 ypaliases[i].name);
151 exit(0);
152 case 'd':
153 domainname = optarg;
154 break;
155 case 't':
156 notrans++;
157 break;
158 case 'm':
159 mode++;
160 break;
161 default:
162 usage();
165 if (!domainname)
166 yp_get_default_domain(&domainname);
168 if (mode == 0) {
169 switch (argc-optind) {
170 case 0:
171 bzero(&sin, sizeof sin);
172 sin.sin_family = AF_INET;
173 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
175 if (bind_host(domainname, &sin))
176 exit(ERR_NOBINDING);
177 break;
178 case 1:
179 bzero(&sin, sizeof sin);
180 sin.sin_family = AF_INET;
181 if ((sin.sin_addr.s_addr = inet_addr(argv[optind])) == -1) {
182 hent = gethostbyname(argv[optind]);
183 if (!hent)
184 errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]);
185 bcopy((char *)hent->h_addr_list[0],
186 (char *)&sin.sin_addr, sizeof sin.sin_addr);
188 if (bind_host(domainname, &sin))
189 exit(ERR_NOBINDING);
190 break;
191 default:
192 usage();
194 exit(0);
197 if (argc-optind > 1)
198 usage();
200 if (argv[optind]) {
201 map = argv[optind];
202 for (i = 0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
203 if (strcmp(map, ypaliases[i].alias) == 0)
204 map = ypaliases[i].name;
205 r = yp_master(domainname, map, &master);
206 switch (r) {
207 case 0:
208 printf("%s\n", master);
209 free(master);
210 break;
211 case YPERR_YPBIND:
212 errx(ERR_NOYPBIND, "not running ypbind");
213 default:
214 errx(ERR_NOMASTER, "can't find master for map %s. reason: %s",
215 map, yperr_string(r));
217 exit(0);
220 ypml = NULL;
221 r = yp_maplist(domainname, &ypml);
222 switch (r) {
223 case 0:
224 for (y = ypml; y;) {
225 ypml = y;
226 r = yp_master(domainname, ypml->map, &master);
227 switch (r) {
228 case 0:
229 printf("%s %s\n", ypml->map, master);
230 free(master);
231 break;
232 default:
233 warnx("can't find the master of %s: reason: %s",
234 ypml->map, yperr_string(r));
235 break;
237 y = ypml->next;
238 free(ypml);
240 break;
241 case YPERR_YPBIND:
242 errx(ERR_NOYPBIND, "not running ypbind");
243 default:
244 errx(ERR_NOMASTER, "can't get map list for domain %s. reason: %s",
245 domainname, yperr_string(r));
247 exit(0);