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