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