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