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