Annotation of src/usr.bin/ypwhich/ypwhich.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: ypwhich.c,v 1.3 1996/05/21 21:32:44 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.4 ! deraadt 37: static char rcsid[] = "$Id: ypwhich.c,v 1.3 1996/05/21 21:32:44 deraadt 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;
87: u_long ss_addr;
88:
89: sock = RPC_ANYSOCK;
90: tv.tv_sec = 15;
91: tv.tv_usec = 0;
92: client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
93: if(client==NULL) {
94: fprintf(stderr, "can't clntudp_create: %s\n",
95: yperr_string(YPERR_YPBIND));
96: return YPERR_YPBIND;
97: }
98:
99: tv.tv_sec = 5;
100: tv.tv_usec = 0;
101: r = clnt_call(client, YPBINDPROC_DOMAIN,
1.2 deraadt 102: xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv);
1.1 deraadt 103: if( r != RPC_SUCCESS) {
104: fprintf(stderr, "can't clnt_call: %s\n",
105: yperr_string(YPERR_YPBIND));
106: clnt_destroy(client);
107: return YPERR_YPBIND;
108: } else {
109: if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
110: fprintf(stderr, "can't yp_bind: Reason: %s\n",
111: yperr_string(ypbr.ypbind_status));
112: clnt_destroy(client);
113: return r;
114: }
115: }
116: clnt_destroy(client);
117:
1.2 deraadt 118: bcopy(&ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
119: &ss_addr, sizeof (ss_addr));
1.1 deraadt 120: /*printf("%08x\n", ss_addr);*/
121: hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET);
122: if (hent)
123: printf("%s\n", hent->h_name);
124: else
125: printf("%s\n", inet_ntoa(ss_addr));
126: return 0;
127: }
128:
129: int
130: main(argc, argv)
131: char **argv;
132: {
1.2 deraadt 133: char *domain, *master, *map;
1.1 deraadt 134: struct ypmaplist *ypml, *y;
135: struct hostent *hent;
136: struct sockaddr_in sin;
137: int notrans, mode, getmap;
138: int c, r, i;
139:
1.2 deraadt 140: yp_get_default_domain(&domain);
1.1 deraadt 141:
142: map = NULL;
143: getmap = notrans = mode = 0;
144: while( (c=getopt(argc, argv, "xd:mt")) != -1)
145: switch(c) {
146: case 'x':
147: for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++)
148: printf("Use \"%s\" for \"%s\"\n",
149: ypaliases[i].alias,
150: ypaliases[i].name);
151: exit(0);
152: case 'd':
1.2 deraadt 153: domain = optarg;
1.1 deraadt 154: break;
155: case 't':
156: notrans++;
157: break;
158: case 'm':
159: mode++;
160: break;
161: default:
162: usage();
163: }
164: argc -= optind;
165: argv += optind;
166:
167: if(mode==0) {
168: switch(argc) {
169: case 0:
170: bzero(&sin, sizeof sin);
171: sin.sin_family = AF_INET;
172: sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
173:
1.2 deraadt 174: if(bind_host(domain, &sin))
1.1 deraadt 175: exit(1);
176: break;
177: case 1:
178: bzero(&sin, sizeof sin);
179: sin.sin_family = AF_INET;
180: if (inet_aton(argv[0], &sin.sin_addr) == 0) {
181: hent = gethostbyname(argv[0]);
182: if(!hent) {
183: fprintf(stderr, "ypwhich: host %s unknown\n",
184: argv[0]);
185: exit(1);
186: }
187: bcopy((char *)hent->h_addr,
188: (char *)&sin.sin_addr, sizeof sin.sin_addr);
189: }
1.2 deraadt 190: if(bind_host(domain, &sin))
1.1 deraadt 191: exit(1);
192: break;
193: default:
194: usage();
195: }
196: exit(0);
197: }
198:
199: if( argc > 1)
200: usage();
201:
202: if(argv[0]) {
203: map = argv[0];
204: for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++)
205: if( strcmp(map, ypaliases[i].alias) == 0)
206: map = ypaliases[i].name;
1.2 deraadt 207: r = yp_master(domain, map, &master);
1.1 deraadt 208: switch(r) {
209: case 0:
210: printf("%s\n", master);
211: free(master);
212: break;
213: case YPERR_YPBIND:
214: fprintf(stderr, "ypwhich: not running ypbind\n");
215: exit(1);
216: default:
217: fprintf(stderr, "Can't find master for map %s. Reason: %s\n",
218: map, yperr_string(r));
219: exit(1);
220: }
221: exit(0);
222: }
223:
224: ypml = NULL;
1.2 deraadt 225: r = yp_maplist(domain, &ypml);
226: r = 0;
1.1 deraadt 227: switch(r) {
228: case 0:
1.2 deraadt 229: for(y = ypml; y; ) {
1.1 deraadt 230: ypml = y;
1.2 deraadt 231: r = yp_master(domain, ypml->map, &master);
1.1 deraadt 232: switch(r) {
233: case 0:
1.2 deraadt 234: printf("%s %s\n", ypml->map, master);
1.1 deraadt 235: free(master);
236: break;
237: default:
238: fprintf(stderr,
239: "YP: can't find the master of %s: Reason: %s\n",
1.2 deraadt 240: ypml->map, yperr_string(r));
1.1 deraadt 241: break;
242: }
1.2 deraadt 243: y = ypml->next;
1.1 deraadt 244: free(ypml);
245: }
246: break;
247: case YPERR_YPBIND:
248: fprintf(stderr, "ypwhich: not running ypbind\n");
249: exit(1);
250: default:
251: fprintf(stderr, "Can't get map list for domain %s. Reason: %s\n",
1.2 deraadt 252: domain, yperr_string(r));
1.1 deraadt 253: exit(1);
254: }
255: exit(0);
256: }