Annotation of src/usr.bin/ypwhich/ypwhich.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: ypwhich.c,v 1.6 1997/04/22 02:55:42 gene 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.7 ! deraadt 37: static char rcsid[] = "$Id: ypwhich.c,v 1.6 1997/04/22 02:55:42 gene Exp $";
1.1 deraadt 38: #endif
39:
40: #include <sys/param.h>
41: #include <sys/types.h>
42: #include <sys/socket.h>
43: #include <stdio.h>
44: #include <stdlib.h>
45: #include <ctype.h>
46: #include <netdb.h>
47: #include <rpc/rpc.h>
48: #include <rpc/xdr.h>
1.2 deraadt 49: #include <rpcsvc/yp.h>
1.1 deraadt 50: #include <rpcsvc/ypclnt.h>
51:
52: struct ypalias {
53: char *alias, *name;
54: } ypaliases[] = {
55: { "passwd", "passwd.byname" },
56: { "group", "group.byname" },
57: { "networks", "networks.byaddr" },
58: { "hosts", "hosts.byaddr" },
59: { "protocols", "protocols.bynumber" },
60: { "services", "services.byname" },
61: { "aliases", "mail.aliases" },
62: { "ethers", "ethers.byname" },
63: };
64:
65: usage()
66: {
67: fprintf(stderr, "Usage:\n");
68: fprintf(stderr, "\typwhich [-d domain] [[-t] -m [mname] | host]\n");
69: fprintf(stderr, "\typwhich -x\n");
70: exit(1);
71: }
72:
73:
74: /*
75: * Like yp_bind except can query a specific host
76: */
77: bind_host(dom, sin)
78: char *dom;
79: struct sockaddr_in *sin;
80: {
81: struct hostent *hent = NULL;
82: struct ypbind_resp ypbr;
83: struct dom_binding *ysd;
84: struct timeval tv;
85: CLIENT *client;
86: int sock, r;
1.7 ! deraadt 87: u_int32_t ss_addr;
1.1 deraadt 88:
89: sock = RPC_ANYSOCK;
90: tv.tv_sec = 15;
91: tv.tv_usec = 0;
92: client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
1.6 gene 93:
94: if (client == NULL) {
95: fprintf(stderr, "ypwhich: host is not bound to a ypmaster\n");
1.1 deraadt 96: return YPERR_YPBIND;
97: }
98:
99: tv.tv_sec = 5;
100: tv.tv_usec = 0;
1.6 gene 101:
1.1 deraadt 102: r = clnt_call(client, YPBINDPROC_DOMAIN,
1.5 deraadt 103: xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
104: if (r != RPC_SUCCESS) {
1.1 deraadt 105: fprintf(stderr, "can't clnt_call: %s\n",
1.5 deraadt 106: yperr_string(YPERR_YPBIND));
1.1 deraadt 107: clnt_destroy(client);
108: return YPERR_YPBIND;
109: } else {
110: if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
111: fprintf(stderr, "can't yp_bind: Reason: %s\n",
1.5 deraadt 112: yperr_string(ypbr.ypbind_status));
1.1 deraadt 113: clnt_destroy(client);
114: return r;
115: }
116: }
117: clnt_destroy(client);
118:
1.2 deraadt 119: bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
120: &ss_addr, sizeof (ss_addr));
1.6 gene 121:
1.1 deraadt 122: hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
1.6 gene 123: if (hent != NULL)
1.1 deraadt 124: printf("%s\n", hent->h_name);
125: else
126: printf("%s\n", inet_ntoa(ss_addr));
1.6 gene 127:
1.1 deraadt 128: return 0;
129: }
130:
131: int
132: main(argc, argv)
133: char **argv;
134: {
1.2 deraadt 135: char *domain, *master, *map;
1.1 deraadt 136: struct ypmaplist *ypml, *y;
137: struct hostent *hent;
138: struct sockaddr_in sin;
139: int notrans, mode, getmap;
140: int c, r, i;
141:
142: map = NULL;
143: getmap = notrans = mode = 0;
1.6 gene 144:
145: yp_get_default_domain(&domain);
146: while ((c = getopt(argc, argv, "xd:mt")) != -1)
147: switch(c) {
1.1 deraadt 148: case 'x':
1.5 deraadt 149: for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1 deraadt 150: printf("Use \"%s\" for \"%s\"\n",
1.5 deraadt 151: ypaliases[i].alias, ypaliases[i].name);
1.1 deraadt 152: exit(0);
153: case 'd':
1.2 deraadt 154: domain = optarg;
1.1 deraadt 155: break;
156: case 't':
157: notrans++;
158: break;
159: case 'm':
160: mode++;
161: break;
162: default:
163: usage();
164: }
165: argc -= optind;
166: argv += optind;
167:
1.6 gene 168: if (mode == 0) {
169: switch(argc) {
1.1 deraadt 170: case 0:
171: bzero(&sin, sizeof sin);
172: sin.sin_family = AF_INET;
173: sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
174:
1.5 deraadt 175: if (bind_host(domain, &sin))
1.1 deraadt 176: exit(1);
177: break;
178: case 1:
179: bzero(&sin, sizeof sin);
180: sin.sin_family = AF_INET;
181: if (inet_aton(argv[0], &sin.sin_addr) == 0) {
182: hent = gethostbyname(argv[0]);
1.5 deraadt 183: if (!hent) {
1.1 deraadt 184: fprintf(stderr, "ypwhich: host %s unknown\n",
185: argv[0]);
186: exit(1);
187: }
188: bcopy((char *)hent->h_addr,
1.5 deraadt 189: (char *)&sin.sin_addr, sizeof sin.sin_addr);
1.1 deraadt 190: }
1.5 deraadt 191: if (bind_host(domain, &sin))
1.1 deraadt 192: exit(1);
193: break;
194: default:
195: usage();
196: }
197: exit(0);
198: }
199:
1.5 deraadt 200: if (argc > 1)
1.1 deraadt 201: usage();
202:
1.5 deraadt 203: if (argv[0]) {
1.1 deraadt 204: map = argv[0];
1.5 deraadt 205: for (i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
206: if (strcmp(map, ypaliases[i].alias) == 0)
1.1 deraadt 207: map = ypaliases[i].name;
1.6 gene 208:
1.2 deraadt 209: r = yp_master(domain, map, &master);
1.5 deraadt 210: switch (r) {
1.1 deraadt 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(1);
218: default:
219: fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
1.5 deraadt 220: map, yperr_string(r));
1.1 deraadt 221: exit(1);
222: }
223: exit(0);
224: }
225:
226: ypml = NULL;
1.2 deraadt 227: r = yp_maplist(domain, &ypml);
228: r = 0;
1.1 deraadt 229: switch(r) {
230: case 0:
1.5 deraadt 231: for (y = ypml; y; ) {
1.1 deraadt 232: ypml = y;
1.2 deraadt 233: r = yp_master(domain, ypml->map, &master);
1.1 deraadt 234: switch(r) {
235: case 0:
1.2 deraadt 236: printf("%s %s\n", ypml->map, master);
1.1 deraadt 237: free(master);
238: break;
239: default:
240: fprintf(stderr,
1.5 deraadt 241: "YP: can't find the master of %s: Reason: %s\n",
242: ypml->map, yperr_string(r));
1.1 deraadt 243: break;
244: }
1.2 deraadt 245: y = ypml->next;
1.1 deraadt 246: free(ypml);
247: }
248: break;
249: case YPERR_YPBIND:
250: fprintf(stderr, "ypwhich: not running ypbind\n");
251: exit(1);
252: default:
253: fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
1.5 deraadt 254: domain, yperr_string(r));
1.1 deraadt 255: exit(1);
256: }
257: exit(0);
258: }