Annotation of src/usr.bin/ypwhich/ypwhich.c, Revision 1.18
1.18 ! deraadt 1: /* $OpenBSD: ypwhich.c,v 1.17 2006/04/02 01:49:19 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:
1.17 deraadt 30: #ifndef lint
1.18 ! deraadt 31: static char rcsid[] = "$Id: ypwhich.c,v 1.17 2006/04/02 01:49:19 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.16 deraadt 69: int bind_host(char *dom, struct sockaddr_in *sin);
70:
71: static void
1.13 deraadt 72: usage(void)
1.1 deraadt 73: {
1.13 deraadt 74: fprintf(stderr,
75: "usage: ypwhich [-d domain] [[-h host] [-t] -m [mname] | host]\n");
76: fprintf(stderr, " ypwhich -x\n");
1.1 deraadt 77: exit(1);
78: }
79:
80:
81: /*
82: * Like yp_bind except can query a specific host
83: */
1.8 deraadt 84: int
1.13 deraadt 85: bind_host(char *dom, struct sockaddr_in *sin)
1.1 deraadt 86: {
87: struct hostent *hent = NULL;
88: struct ypbind_resp ypbr;
1.13 deraadt 89: struct in_addr ss_addr;
1.1 deraadt 90: struct timeval tv;
91: CLIENT *client;
92: int sock, r;
93:
94: sock = RPC_ANYSOCK;
95: tv.tv_sec = 15;
96: tv.tv_usec = 0;
97: client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
1.6 gene 98:
99: if (client == NULL) {
100: fprintf(stderr, "ypwhich: host is not bound to a ypmaster\n");
1.1 deraadt 101: return YPERR_YPBIND;
102: }
103:
104: tv.tv_sec = 5;
105: tv.tv_usec = 0;
1.6 gene 106:
1.1 deraadt 107: r = clnt_call(client, YPBINDPROC_DOMAIN,
1.5 deraadt 108: xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
109: if (r != RPC_SUCCESS) {
1.1 deraadt 110: fprintf(stderr, "can't clnt_call: %s\n",
1.5 deraadt 111: yperr_string(YPERR_YPBIND));
1.1 deraadt 112: clnt_destroy(client);
113: return YPERR_YPBIND;
114: } else {
115: if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
116: fprintf(stderr, "can't yp_bind: Reason: %s\n",
1.5 deraadt 117: yperr_string(ypbr.ypbind_status));
1.1 deraadt 118: clnt_destroy(client);
119: return r;
120: }
121: }
122: clnt_destroy(client);
123:
1.8 deraadt 124: memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
125: sizeof (ss_addr));
1.6 gene 126:
1.8 deraadt 127: hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
128: AF_INET);
1.6 gene 129: if (hent != NULL)
1.1 deraadt 130: printf("%s\n", hent->h_name);
131: else
132: printf("%s\n", inet_ntoa(ss_addr));
1.6 gene 133:
1.1 deraadt 134: return 0;
135: }
1.13 deraadt 136:
1.1 deraadt 137: int
1.13 deraadt 138: main(int argc, char *argv[])
1.1 deraadt 139: {
1.13 deraadt 140: char *domain, *master, *map = NULL, *host = NULL;
1.18 ! deraadt 141: int notrans = 0, mode = 0, c, r, i;
1.1 deraadt 142: struct ypmaplist *ypml, *y;
1.13 deraadt 143: struct sockaddr_in sin;
1.1 deraadt 144: struct hostent *hent;
1.13 deraadt 145: CLIENT *client = NULL;
1.1 deraadt 146:
1.6 gene 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: }
1.18 ! deraadt 196: bcopy(hent->h_addr, &sin.sin_addr,
! 197: 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: }