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