1 /* 2 * Copyright (c) 1995, 1996 3 * Bill Paul <wpaul@ctr.columbia.edu>. 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id: yp_dnslookup.c,v 1.11 1997/07/27 03:41:53 wpaul Exp $ 33 */ 34 35 /* 36 * Do standard and reverse DNS lookups using the resolver library. 37 * Take care of all the dirty work here so the main program only has to 38 * pass us a pointer to an array of characters. 39 * 40 * We have to use direct resolver calls here otherwise the YP server 41 * could end up looping by calling itself over and over again until 42 * it disappeared up its own belly button. 43 */ 44 45 #include <sys/param.h> 46 #include <sys/socket.h> 47 #include <sys/time.h> 48 #include <sys/fcntl.h> 49 #include <sys/queue.h> 50 #include <netinet/in.h> 51 #include <arpa/inet.h> 52 #include <arpa/nameser.h> 53 54 #include <stdio.h> 55 #include <ctype.h> 56 #include <resolv.h> 57 #include <netdb.h> 58 #include <unistd.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <errno.h> 62 #include <err.h> 63 64 #include <rpcsvc/yp.h> 65 #include "yp_extern.h" 66 67 #ifndef lint 68 static const char rcsid[] = "$Id: yp_dnslookup.c,v 1.11 1997/07/27 03:41:53 wpaul Exp $"; 69 #endif 70 71 static char *parse(hp) 72 struct hostent *hp; 73 { 74 static char result[MAXHOSTNAMELEN * 2]; 75 int len,i; 76 struct in_addr addr; 77 78 if (hp == NULL) 79 return(NULL); 80 81 len = 16 + strlen(hp->h_name); 82 for (i = 0; hp->h_aliases[i]; i++) 83 len += strlen(hp->h_aliases[i]) + 1; 84 85 bzero(result, sizeof(result)); 86 87 bcopy(hp->h_addr, &addr, sizeof(struct in_addr)); 88 snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name); 89 90 for (i = 0; hp->h_aliases[i]; i++) { 91 strcat(result, " "); 92 strcat(result, hp->h_aliases[i]); 93 } 94 95 return ((char *)&result); 96 } 97 98 #define MAXPACKET 1024 99 #define DEF_TTL 50 100 101 #define BY_DNS_ID 1 102 #define BY_RPC_XID 2 103 104 extern struct hostent *__dns_getanswer __P((char *, int, char *, int)); 105 106 static CIRCLEQ_HEAD(dns_qhead, circleq_dnsentry) qhead; 107 108 struct circleq_dnsentry { 109 SVCXPRT *xprt; 110 unsigned long xid; 111 struct sockaddr_in client_addr; 112 unsigned long ypvers; 113 unsigned long id; 114 unsigned long ttl; 115 unsigned long type; 116 unsigned short prot_type; 117 char **domain; 118 char *name; 119 struct in_addr addr; 120 CIRCLEQ_ENTRY(circleq_dnsentry) links; 121 }; 122 123 static int pending = 0; 124 125 int yp_init_resolver() 126 { 127 CIRCLEQ_INIT(&qhead); 128 if (!(_res.options & RES_INIT) && res_init() == -1) { 129 yp_error("res_init failed"); 130 return(1); 131 } 132 if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 133 yp_error("couldn't create socket"); 134 return(1); 135 } 136 if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) { 137 yp_error("couldn't make resolver socket non-blocking"); 138 return(1); 139 } 140 return(0); 141 } 142 143 static struct circleq_dnsentry *yp_malloc_dnsent() 144 { 145 register struct circleq_dnsentry *q; 146 147 q = (struct circleq_dnsentry *)malloc(sizeof(struct circleq_dnsentry)); 148 149 if (q == NULL) { 150 yp_error("failed to malloc() circleq dns entry: %s", 151 strerror(errno)); 152 return(NULL); 153 } 154 155 return(q); 156 } 157 158 /* 159 * Transmit a query. 160 */ 161 static unsigned long yp_send_dns_query(name, type) 162 char *name; 163 int type; 164 { 165 char buf[MAXPACKET]; 166 int n; 167 HEADER *hptr; 168 int ns; 169 int rval; 170 unsigned long id; 171 172 bzero(buf, sizeof(buf)); 173 174 n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf)); 175 176 if (n <= 0) { 177 yp_error("res_mkquery failed"); 178 return(0); 179 } 180 181 hptr = (HEADER *)&buf; 182 id = ntohs(hptr->id); 183 184 for (ns = 0; ns < _res.nscount; ns++) { 185 rval = sendto(resfd, buf, n, 0, 186 (struct sockaddr *)&_res.nsaddr_list[ns], 187 sizeof(struct sockaddr)); 188 if (rval == -1) { 189 yp_error("sendto failed"); 190 return(0); 191 } 192 } 193 194 return(id); 195 } 196 197 static struct circleq_dnsentry *yp_find_dnsqent(id, type) 198 unsigned long id; 199 int type; 200 { 201 register struct circleq_dnsentry *q; 202 203 for (q = qhead.cqh_first; q != (void *)&qhead; q = q->links.cqe_next) { 204 switch(type) { 205 case BY_RPC_XID: 206 if (id == q->xid) 207 return(q); 208 break; 209 case BY_DNS_ID: 210 default: 211 if (id == q->id) 212 return(q); 213 break; 214 } 215 } 216 return (NULL); 217 } 218 219 static void yp_send_dns_reply(q, buf) 220 struct circleq_dnsentry *q; 221 char *buf; 222 { 223 ypresponse result_v1; 224 ypresp_val result_v2; 225 unsigned long xid; 226 struct sockaddr_in client_addr; 227 xdrproc_t xdrfunc; 228 char *result; 229 230 /* 231 * Set up correct reply struct and 232 * XDR filter depending on ypvers. 233 */ 234 switch(q->ypvers) { 235 case YPVERS: 236 bzero((char *)&result_v2, sizeof(result_v2)); 237 238 if (buf == NULL) 239 result_v2.stat = YP_NOKEY; 240 else { 241 result_v2.val.valdat_len = strlen(buf); 242 result_v2.val.valdat_val = buf; 243 result_v2.stat = YP_TRUE; 244 } 245 result = (char *)&result_v2; 246 xdrfunc = (xdrproc_t)xdr_ypresp_val; 247 break; 248 case YPOLDVERS: 249 /* 250 * The odds are we will _never_ execute this 251 * particular code, but we include it anyway 252 * for the sake of completeness. 253 */ 254 bzero((char *)&result_v1, sizeof(result_v1)); 255 result_v1.yp_resptype = YPRESP_VAL; 256 # define YPVAL ypresponse_u.yp_resp_valtype 257 258 if (buf == NULL) 259 result_v1.YPVAL.stat = YP_NOKEY; 260 else { 261 result_v1.YPVAL.val.valdat_len = strlen(buf); 262 result_v1.YPVAL.val.valdat_val = buf; 263 result_v1.YPVAL.stat = YP_TRUE; 264 } 265 result = (char *)&result_v1; 266 xdrfunc = (xdrproc_t)xdr_ypresponse; 267 break; 268 default: 269 yp_error("Bad YP program version (%lu)!",q->ypvers); 270 return; 271 break; 272 } 273 274 if (debug) 275 yp_error("Sending dns reply to %s (%lu)", 276 inet_ntoa(q->client_addr.sin_addr), q->id); 277 /* 278 * XXX This is disgusting. There's basically one transport 279 * handle for UDP, but we're holding off on replying to a 280 * client until we're ready, by which time we may have received 281 * several other queries from other clients with different 282 * transaction IDs. So to make the delayed response thing work, 283 * we have to save the transaction ID and client address of 284 * each request, then jam them into the transport handle when 285 * we're ready to send a reply. Then after we've send the reply, 286 * we put the old transaction ID and remote address back the 287 * way we found 'em. This is _INCREDIBLY_ non-portable; it's 288 * not even supported by the RPC library. 289 */ 290 /* 291 * XXX Don't frob the transaction ID for TCP handles. 292 */ 293 if (q->prot_type == SOCK_DGRAM) 294 xid = svcudp_set_xid(q->xprt, q->xid); 295 client_addr = q->xprt->xp_raddr; 296 q->xprt->xp_raddr = q->client_addr; 297 298 if (!svc_sendreply(q->xprt, xdrfunc, result)) 299 yp_error("svc_sendreply failed"); 300 301 /* 302 * Now that we sent the reply, 303 * put the handle back the way it was. 304 */ 305 if (q->prot_type == SOCK_DGRAM) 306 svcudp_set_xid(q->xprt, xid); 307 q->xprt->xp_raddr = client_addr; 308 309 return; 310 } 311 312 /* 313 * Decrement TTL on all queue entries, possibly nuking 314 * any that have been around too long without being serviced. 315 */ 316 void yp_prune_dnsq() 317 { 318 register struct circleq_dnsentry *q, *n; 319 320 q = qhead.cqh_first; 321 while(q != (void *)&qhead) { 322 q->ttl--; 323 n = q->links.cqe_next; 324 if (!q->ttl) { 325 CIRCLEQ_REMOVE(&qhead, q, links); 326 free(q->name); 327 free(q); 328 pending--; 329 } 330 q = n; 331 } 332 333 if (pending < 0) 334 pending = 0; 335 336 return; 337 } 338 339 /* 340 * Data is pending on the DNS socket; check for valid replies 341 * to our queries and dispatch them to waiting clients. 342 */ 343 void yp_run_dnsq() 344 { 345 register struct circleq_dnsentry *q; 346 char buf[sizeof(HEADER) + MAXPACKET]; 347 char retrybuf[MAXHOSTNAMELEN]; 348 struct sockaddr_in sin; 349 int rval; 350 int len; 351 HEADER *hptr; 352 struct hostent *hent; 353 354 if (debug) 355 yp_error("Running dns queue"); 356 357 bzero(buf, sizeof(buf)); 358 359 len = sizeof(struct sockaddr_in); 360 rval = recvfrom(resfd, buf, sizeof(buf), 0, 361 (struct sockaddr *)&sin, &len); 362 363 if (rval == -1) { 364 yp_error("recvfrom failed: %s", strerror(errno)); 365 return; 366 } 367 368 /* 369 * We may have data left in the socket that represents 370 * replies to earlier queries that we don't care about 371 * anymore. If there are no lookups pending or the packet 372 * ID doesn't match any of the queue IDs, just drop it 373 * on the floor. 374 */ 375 hptr = (HEADER *)&buf; 376 if (!pending || 377 (q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) { 378 /* ignore */ 379 return; 380 } 381 382 if (debug) 383 yp_error("Got dns reply from %s", inet_ntoa(sin.sin_addr)); 384 385 hent = __dns_getanswer(buf, rval, q->name, q->type); 386 387 /* 388 * If the lookup failed, try appending one of the domains 389 * from resolv.conf. If we have no domains to test, the 390 * query has failed. 391 */ 392 if (hent == NULL) { 393 if ((h_errno == TRY_AGAIN || h_errno == NO_RECOVERY) 394 && q->domain && *q->domain) { 395 snprintf(retrybuf, sizeof(retrybuf), "%s.%s", 396 q->name, *q->domain); 397 if (debug) 398 yp_error("Retrying with: %s", retrybuf); 399 q->id = yp_send_dns_query(retrybuf, q->type); 400 q->ttl = DEF_TTL; 401 q->domain++; 402 return; 403 } 404 } else { 405 if (q->type == T_PTR) { 406 hent->h_addr = (char *)&q->addr.s_addr; 407 hent->h_length = sizeof(struct in_addr); 408 } 409 } 410 411 /* Got an answer ready for a client -- send it off. */ 412 yp_send_dns_reply(q, parse(hent)); 413 pending--; 414 CIRCLEQ_REMOVE(&qhead, q, links); 415 free(q->name); 416 free(q); 417 418 /* Decrement TTLs on other entries while we're here. */ 419 yp_prune_dnsq(); 420 421 return; 422 } 423 424 /* 425 * Queue and transmit an asynchronous DNS hostname lookup. 426 */ 427 ypstat yp_async_lookup_name(rqstp, name) 428 struct svc_req *rqstp; 429 char *name; 430 { 431 register struct circleq_dnsentry *q; 432 int type, len; 433 434 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */ 435 type = -1; len = sizeof(type); 436 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET, 437 SO_TYPE, &type, &len) == -1) { 438 yp_error("getsockopt failed: %s", strerror(errno)); 439 return(YP_YPERR); 440 } 441 442 /* Avoid transmitting dupe requests. */ 443 if (type == SOCK_DGRAM && 444 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL) 445 return(YP_TRUE); 446 447 if ((q = yp_malloc_dnsent()) == NULL) 448 return(YP_YPERR); 449 450 q->type = T_A; 451 q->ttl = DEF_TTL; 452 q->xprt = rqstp->rq_xprt; 453 q->ypvers = rqstp->rq_vers; 454 q->prot_type = type; 455 if (q->prot_type == SOCK_DGRAM) 456 q->xid = svcudp_get_xid(q->xprt); 457 q->client_addr = q->xprt->xp_raddr; 458 if (!strchr(name, '.')) 459 q->domain = _res.dnsrch; 460 else 461 q->domain = NULL; 462 q->id = yp_send_dns_query(name, q->type); 463 464 if (q->id == 0) { 465 yp_error("DNS query failed"); 466 free(q); 467 return(YP_YPERR); 468 } 469 470 q->name = strdup(name); 471 CIRCLEQ_INSERT_HEAD(&qhead, q, links); 472 pending++; 473 474 if (debug) 475 yp_error("Queueing async DNS name lookup (%d)", q->id); 476 477 yp_prune_dnsq(); 478 return(YP_TRUE); 479 } 480 481 /* 482 * Queue and transmit an asynchronous DNS IP address lookup. 483 */ 484 ypstat yp_async_lookup_addr(rqstp, addr) 485 struct svc_req *rqstp; 486 char *addr; 487 { 488 register struct circleq_dnsentry *q; 489 char buf[MAXHOSTNAMELEN]; 490 int a, b, c, d; 491 int type, len; 492 493 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */ 494 type = -1; len = sizeof(type); 495 if (getsockopt(rqstp->rq_xprt->xp_sock, SOL_SOCKET, 496 SO_TYPE, &type, &len) == -1) { 497 yp_error("getsockopt failed: %s", strerror(errno)); 498 return(YP_YPERR); 499 } 500 501 /* Avoid transmitting dupe requests. */ 502 if (type == SOCK_DGRAM && 503 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL) 504 return(YP_TRUE); 505 506 if ((q = yp_malloc_dnsent()) == NULL) 507 return(YP_YPERR); 508 509 if (sscanf(addr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) 510 return(YP_NOKEY); 511 512 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa", d, c, b, a); 513 514 if (debug) 515 yp_error("DNS address is: %s", buf); 516 517 q->type = T_PTR; 518 q->ttl = DEF_TTL; 519 q->xprt = rqstp->rq_xprt; 520 q->ypvers = rqstp->rq_vers; 521 q->domain = NULL; 522 q->prot_type = type; 523 if (q->prot_type == SOCK_DGRAM) 524 q->xid = svcudp_get_xid(q->xprt); 525 q->client_addr = q->xprt->xp_raddr; 526 q->id = yp_send_dns_query(buf, q->type); 527 528 if (q->id == 0) { 529 yp_error("DNS query failed"); 530 free(q); 531 return(YP_YPERR); 532 } 533 534 inet_aton(addr, &q->addr); 535 q->name = strdup(buf); 536 CIRCLEQ_INSERT_HEAD(&qhead, q, links); 537 pending++; 538 539 if (debug) 540 yp_error("Queueing async DNS address lookup (%d)", q->id); 541 542 yp_prune_dnsq(); 543 return(YP_TRUE); 544 } 545