Annotation of src/usr.bin/ypwhich/ypwhich.c, Revision 1.11
1.11 ! maja 1: /* $OpenBSD: ypwhich.c,v 1.10 1999/03/19 22:21:27 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.11 ! maja 37: static char rcsid[] = "$Id: ypwhich.c,v 1.10 1999/03/19 22:21:27 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:
1.11 ! maja 60: #include "yplib_host.h"
! 61:
1.1 deraadt 62: struct ypalias {
63: char *alias, *name;
64: } ypaliases[] = {
65: { "passwd", "passwd.byname" },
66: { "group", "group.byname" },
67: { "networks", "networks.byaddr" },
68: { "hosts", "hosts.byaddr" },
69: { "protocols", "protocols.bynumber" },
70: { "services", "services.byname" },
71: { "aliases", "mail.aliases" },
72: { "ethers", "ethers.byname" },
73: };
74:
1.8 deraadt 75: void
1.1 deraadt 76: usage()
77: {
78: fprintf(stderr, "Usage:\n");
1.11 ! maja 79: fprintf(stderr, "\typwhich [-d domain] [[-h host] [-t] -m [mname] | host]\n");
1.1 deraadt 80: fprintf(stderr, "\typwhich -x\n");
81: exit(1);
82: }
83:
84:
85: /*
86: * Like yp_bind except can query a specific host
87: */
1.8 deraadt 88: int
1.1 deraadt 89: bind_host(dom, sin)
90: char *dom;
91: struct sockaddr_in *sin;
92: {
93: struct hostent *hent = NULL;
94: struct ypbind_resp ypbr;
95: struct timeval tv;
96: CLIENT *client;
97: int sock, r;
1.8 deraadt 98: struct in_addr ss_addr;
1.1 deraadt 99:
100: sock = RPC_ANYSOCK;
101: tv.tv_sec = 15;
102: tv.tv_usec = 0;
103: client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
1.6 gene 104:
105: if (client == NULL) {
106: fprintf(stderr, "ypwhich: host is not bound to a ypmaster\n");
1.1 deraadt 107: return YPERR_YPBIND;
108: }
109:
110: tv.tv_sec = 5;
111: tv.tv_usec = 0;
1.6 gene 112:
1.1 deraadt 113: r = clnt_call(client, YPBINDPROC_DOMAIN,
1.5 deraadt 114: xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
115: if (r != RPC_SUCCESS) {
1.1 deraadt 116: fprintf(stderr, "can't clnt_call: %s\n",
1.5 deraadt 117: yperr_string(YPERR_YPBIND));
1.1 deraadt 118: clnt_destroy(client);
119: return YPERR_YPBIND;
120: } else {
121: if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
122: fprintf(stderr, "can't yp_bind: Reason: %s\n",
1.5 deraadt 123: yperr_string(ypbr.ypbind_status));
1.1 deraadt 124: clnt_destroy(client);
125: return r;
126: }
127: }
128: clnt_destroy(client);
129:
1.8 deraadt 130: memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
131: sizeof (ss_addr));
1.6 gene 132:
1.8 deraadt 133: hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
134: AF_INET);
1.6 gene 135: if (hent != NULL)
1.1 deraadt 136: printf("%s\n", hent->h_name);
137: else
138: printf("%s\n", inet_ntoa(ss_addr));
1.6 gene 139:
1.1 deraadt 140: return 0;
141: }
142:
143: int
144: main(argc, argv)
1.8 deraadt 145: int argc;
146: char **argv;
1.1 deraadt 147: {
1.2 deraadt 148: char *domain, *master, *map;
1.1 deraadt 149: struct ypmaplist *ypml, *y;
150: struct hostent *hent;
151: struct sockaddr_in sin;
152: int notrans, mode, getmap;
153: int c, r, i;
1.11 ! maja 154: CLIENT *client;
! 155: char *host = NULL;
1.1 deraadt 156:
157: map = NULL;
158: getmap = notrans = mode = 0;
1.6 gene 159:
160: yp_get_default_domain(&domain);
1.8 deraadt 161: if (domain == NULL)
162: errx(1, "YP domain name not set");
163:
1.11 ! maja 164: while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
1.6 gene 165: switch(c) {
1.1 deraadt 166: case 'x':
1.5 deraadt 167: for (i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
1.1 deraadt 168: printf("Use \"%s\" for \"%s\"\n",
1.5 deraadt 169: ypaliases[i].alias, ypaliases[i].name);
1.1 deraadt 170: exit(0);
1.11 ! maja 171: case 'h':
! 172: host = optarg;
! 173: break;
1.1 deraadt 174: case 'd':
1.2 deraadt 175: domain = optarg;
1.1 deraadt 176: break;
177: case 't':
178: notrans++;
179: break;
180: case 'm':
181: mode++;
182: break;
183: default:
184: usage();
185: }
186: argc -= optind;
187: argv += optind;
188:
1.6 gene 189: if (mode == 0) {
190: switch(argc) {
1.1 deraadt 191: case 0:
1.8 deraadt 192: memset(&sin, 0, sizeof sin);
1.1 deraadt 193: sin.sin_family = AF_INET;
194: sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
195:
1.5 deraadt 196: if (bind_host(domain, &sin))
1.1 deraadt 197: exit(1);
198: break;
199: case 1:
200: bzero(&sin, sizeof sin);
201: sin.sin_family = AF_INET;
202: if (inet_aton(argv[0], &sin.sin_addr) == 0) {
203: hent = gethostbyname(argv[0]);
1.5 deraadt 204: if (!hent) {
1.1 deraadt 205: fprintf(stderr, "ypwhich: host %s unknown\n",
206: argv[0]);
207: exit(1);
208: }
209: bcopy((char *)hent->h_addr,
1.5 deraadt 210: (char *)&sin.sin_addr, sizeof sin.sin_addr);
1.1 deraadt 211: }
1.5 deraadt 212: if (bind_host(domain, &sin))
1.1 deraadt 213: exit(1);
214: break;
215: default:
216: usage();
217: }
218: exit(0);
219: }
220:
1.5 deraadt 221: if (argc > 1)
1.1 deraadt 222: usage();
223:
1.11 ! maja 224: if (host != NULL) {
! 225: client = yp_bind_host(host,YPPROG,YPVERS,0,1);
! 226: }
! 227:
1.5 deraadt 228: if (argv[0]) {
1.1 deraadt 229: map = argv[0];
1.5 deraadt 230: for (i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
231: if (strcmp(map, ypaliases[i].alias) == 0)
1.1 deraadt 232: map = ypaliases[i].name;
1.6 gene 233:
1.11 ! maja 234: if (host != NULL) {
! 235: r = yp_master_host(client, domain, map, &master);
! 236: } else {
! 237: r = yp_master(domain, map, &master);
! 238: }
1.5 deraadt 239: switch (r) {
1.1 deraadt 240: case 0:
241: printf("%s\n", master);
242: free(master);
243: break;
244: case YPERR_YPBIND:
245: fprintf(stderr, "ypwhich: not running ypbind\n");
246: exit(1);
247: default:
248: fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
1.5 deraadt 249: map, yperr_string(r));
1.1 deraadt 250: exit(1);
251: }
252: exit(0);
253: }
254:
255: ypml = NULL;
1.11 ! maja 256: if (host != NULL) {
! 257: r = yp_maplist_host(client, domain, &ypml);
! 258: } else {
! 259: r = yp_maplist(domain, &ypml);
! 260: }
1.2 deraadt 261: r = 0;
1.1 deraadt 262: switch(r) {
263: case 0:
1.5 deraadt 264: for (y = ypml; y; ) {
1.1 deraadt 265: ypml = y;
1.11 ! maja 266: if (host != NULL) {
! 267: r = yp_master_host(client,
! 268: domain, ypml->map, &master);
! 269: } else {
! 270: r = yp_master(domain, ypml->map, &master);
! 271: }
1.1 deraadt 272: switch(r) {
273: case 0:
1.2 deraadt 274: printf("%s %s\n", ypml->map, master);
1.1 deraadt 275: free(master);
276: break;
277: default:
278: fprintf(stderr,
1.5 deraadt 279: "YP: can't find the master of %s: Reason: %s\n",
280: ypml->map, yperr_string(r));
1.1 deraadt 281: break;
282: }
1.2 deraadt 283: y = ypml->next;
1.1 deraadt 284: free(ypml);
285: }
286: break;
287: case YPERR_YPBIND:
288: fprintf(stderr, "ypwhich: not running ypbind\n");
289: exit(1);
290: default:
291: fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
1.5 deraadt 292: domain, yperr_string(r));
1.1 deraadt 293: exit(1);
294: }
295: exit(0);
296: }