1 /* 2 * Copyright (c) 1992/3 Theo de Raadt <deraadt@fsa.ca> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 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/cdefs.h> 31 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 37 38 #include <rpcsvc/ypclnt.h> 39 40 #include <netinet/in.h> 41 42 #include <arpa/inet.h> 43 44 #include <ctype.h> 45 #include <err.h> 46 #include <netdb.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <rpc/rpc.h> 52 #include <rpc/xdr.h> 53 #include <rpcsvc/yp.h> 54 struct dom_binding{}; 55 56 #define ERR_USAGE 1 /* bad arguments - display 'usage' message */ 57 #define ERR_NOSUCHHOST 2 /* no such host */ 58 #define ERR_NOBINDING 3 /* error from ypbind -- domain not bound */ 59 #define ERR_NOYPBIND 4 /* ypbind not running */ 60 #define ERR_NOMASTER 5 /* could not find master server */ 61 62 extern bool_t xdr_domainname(); 63 64 struct ypalias { 65 const char *alias, *name; 66 } ypaliases[] = { 67 { "passwd", "passwd.byname" }, 68 { "master.passwd", "master.passwd.byname" }, 69 { "group", "group.byname" }, 70 { "networks", "networks.byaddr" }, 71 { "hosts", "hosts.byaddr" }, 72 { "protocols", "protocols.bynumber" }, 73 { "services", "services.byname" }, 74 { "aliases", "mail.aliases" }, 75 { "ethers", "ethers.byname" }, 76 }; 77 78 static void 79 usage(void) 80 { 81 fprintf(stderr, "%s\n%s\n", 82 "usage: ypwhich [-d domain] [[-t] -m [mname] | host]", 83 " ypwhich -x"); 84 exit(ERR_USAGE); 85 } 86 87 88 /* 89 * Like yp_bind except can query a specific host 90 */ 91 static int 92 bind_host(char *dom, struct sockaddr_in *lsin) 93 { 94 struct hostent *hent = NULL; 95 struct ypbind_resp ypbr; 96 struct timeval tv; 97 CLIENT *client; 98 int sock, r; 99 struct in_addr ss_addr; 100 101 sock = RPC_ANYSOCK; 102 tv.tv_sec = 15; 103 tv.tv_usec = 0; 104 client = clntudp_create(lsin, YPBINDPROG, YPBINDVERS, tv, &sock); 105 if(client==NULL) { 106 warnx("can't clntudp_create: %s", yperr_string(YPERR_YPBIND)); 107 return YPERR_YPBIND; 108 } 109 110 tv.tv_sec = 5; 111 tv.tv_usec = 0; 112 r = clnt_call(client, YPBINDPROC_DOMAIN, 113 xdr_domainname, &dom, xdr_ypbind_resp, &ypbr, tv); 114 if( r != RPC_SUCCESS) { 115 warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND)); 116 clnt_destroy(client); 117 return YPERR_YPBIND; 118 } else { 119 if (ypbr.ypbind_status != YPBIND_SUCC_VAL) { 120 warnx("can't yp_bind: reason: %s", 121 ypbinderr_string(ypbr.ypbind_resp_u.ypbind_error)); 122 clnt_destroy(client); 123 return r; 124 } 125 } 126 clnt_destroy(client); 127 128 ss_addr = *(struct in_addr *)ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr; 129 /*printf("%08x\n", ss_addr);*/ 130 hent = gethostbyaddr((char *)&ss_addr, sizeof(ss_addr), AF_INET); 131 if (hent) 132 printf("%s\n", hent->h_name); 133 else 134 printf("%s\n", inet_ntoa(ss_addr)); 135 return 0; 136 } 137 138 int 139 main(int argc, char *argv[]) 140 { 141 char *domnam = NULL, *master; 142 const char *map = NULL; 143 struct ypmaplist *ypml, *y; 144 struct hostent *hent; 145 struct sockaddr_in lsin; 146 int notrans, mode, getmap; 147 int c, r; 148 u_int i; 149 150 getmap = notrans = mode = 0; 151 while( (c=getopt(argc, argv, "xd:mt")) != -1) 152 switch(c) { 153 case 'x': 154 for(i=0; i<sizeof ypaliases/sizeof ypaliases[0]; i++) 155 printf("\"%s\" is an alias for \"%s\"\n", 156 ypaliases[i].alias, 157 ypaliases[i].name); 158 exit(0); 159 case 'd': 160 domnam = optarg; 161 break; 162 case 't': 163 notrans++; 164 break; 165 case 'm': 166 mode++; 167 break; 168 default: 169 usage(); 170 } 171 172 if(!domnam) 173 yp_get_default_domain(&domnam); 174 175 if(mode==0) { 176 switch(argc-optind) { 177 case 0: 178 bzero(&lsin, sizeof lsin); 179 lsin.sin_family = AF_INET; 180 lsin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 181 182 if(bind_host(domnam, &lsin)) 183 exit(ERR_NOBINDING); 184 break; 185 case 1: 186 bzero(&lsin, sizeof lsin); 187 lsin.sin_family = AF_INET; 188 if( (lsin.sin_addr.s_addr=inet_addr(argv[optind]))==-1) { 189 hent = gethostbyname(argv[optind]); 190 if(!hent) 191 errx(ERR_NOSUCHHOST, "host %s unknown", argv[optind]); 192 bcopy((char *)hent->h_addr_list[0], 193 (char *)&lsin.sin_addr, sizeof lsin.sin_addr); 194 } 195 if(bind_host(domnam, &lsin)) 196 exit(ERR_NOBINDING); 197 break; 198 default: 199 usage(); 200 } 201 exit(0); 202 } 203 204 if( argc-optind > 1) 205 usage(); 206 207 if(argv[optind]) { 208 map = argv[optind]; 209 for(i=0; (!notrans) && i<sizeof ypaliases/sizeof ypaliases[0]; i++) 210 if( strcmp(map, ypaliases[i].alias) == 0) 211 map = ypaliases[i].name; 212 r = yp_master(domnam, map, &master); 213 switch(r) { 214 case 0: 215 printf("%s\n", master); 216 free(master); 217 break; 218 case YPERR_YPBIND: 219 errx(ERR_NOYPBIND, "not running ypbind"); 220 default: 221 errx(ERR_NOMASTER, "can't find master for map %s. reason: %s", 222 map, yperr_string(r)); 223 } 224 exit(0); 225 } 226 227 ypml = NULL; 228 r = yp_maplist(domnam, &ypml); 229 switch(r) { 230 case 0: 231 for(y=ypml; y; ) { 232 ypml = y; 233 r = yp_master(domnam, ypml->map, &master); 234 switch(r) { 235 case 0: 236 printf("%s %s\n", ypml->map, master); 237 free(master); 238 break; 239 default: 240 warnx("can't find the master of %s: reason: %s", 241 ypml->map, yperr_string(r)); 242 break; 243 } 244 y = ypml->next; 245 free(ypml); 246 } 247 break; 248 case YPERR_YPBIND: 249 errx(ERR_NOYPBIND, "not running ypbind"); 250 default: 251 errx(ERR_NOMASTER, "can't get map list for domain %s. reason: %s", 252 domnam, yperr_string(r)); 253 } 254 exit(0); 255 } 256