yp_dnslookup.c revision 90297
1173412Skevlo/* 2173412Skevlo * Copyright (c) 1995, 1996 32311Sjkh * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 42311Sjkh * 52311Sjkh * Redistribution and use in source and binary forms, with or without 62311Sjkh * modification, are permitted provided that the following conditions 72311Sjkh * are met: 82311Sjkh * 1. Redistributions of source code must retain the above copyright 92311Sjkh * notice, this list of conditions and the following disclaimer. 102311Sjkh * 2. Redistributions in binary form must reproduce the above copyright 112311Sjkh * notice, this list of conditions and the following disclaimer in the 122311Sjkh * documentation and/or other materials provided with the distribution. 132311Sjkh * 3. All advertising materials mentioning features or use of this software 142311Sjkh * must display the following acknowledgement: 152311Sjkh * This product includes software developed by Bill Paul. 162311Sjkh * 4. Neither the name of the University nor the names of its contributors 172311Sjkh * may be used to endorse or promote products derived from this software 182311Sjkh * without specific prior written permission. 192311Sjkh * 202311Sjkh * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 212311Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 222311Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 232311Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 242311Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 252311Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 262311Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 272311Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 282311Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 292311Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 302311Sjkh * SUCH DAMAGE. 312311Sjkh */ 322311Sjkh 332311Sjkh#ifndef lint 342311Sjkhstatic const char rcsid[] = 352311Sjkh "$FreeBSD: head/usr.sbin/ypserv/yp_dnslookup.c 90297 2002-02-06 13:30:31Z des $"; 362311Sjkh#endif /* not lint */ 372311Sjkh 382311Sjkh/* 392311Sjkh * Do standard and reverse DNS lookups using the resolver library. 402311Sjkh * Take care of all the dirty work here so the main program only has to 412311Sjkh * pass us a pointer to an array of characters. 422311Sjkh * 432311Sjkh * We have to use direct resolver calls here otherwise the YP server 442311Sjkh * could end up looping by calling itself over and over again until 452311Sjkh * it disappeared up its own belly button. 462311Sjkh */ 472311Sjkh 482311Sjkh#include <sys/param.h> 492311Sjkh#include <sys/socket.h> 502311Sjkh#include <sys/time.h> 512311Sjkh#include <sys/fcntl.h> 522311Sjkh#include <sys/queue.h> 532311Sjkh#include <netinet/in.h> 542311Sjkh#include <arpa/inet.h> 552311Sjkh#include <arpa/nameser.h> 562311Sjkh 572311Sjkh#include <ctype.h> 582311Sjkh#include <errno.h> 592311Sjkh#include <netdb.h> 602311Sjkh#include <stdio.h> 612311Sjkh#include <stdlib.h> 622311Sjkh#include <string.h> 632311Sjkh#include <resolv.h> 642311Sjkh#include <unistd.h> 652311Sjkh 662311Sjkh#include <rpcsvc/yp.h> 672311Sjkh#include "yp_extern.h" 682311Sjkh 692311Sjkhstatic char *parse(hp) 702311Sjkh struct hostent *hp; 712311Sjkh{ 722311Sjkh static char result[MAXHOSTNAMELEN * 2]; 732311Sjkh int len,i; 742311Sjkh struct in_addr addr; 752311Sjkh 76173412Skevlo if (hp == NULL) 772311Sjkh return(NULL); 782311Sjkh 792311Sjkh len = 16 + strlen(hp->h_name); 802311Sjkh for (i = 0; hp->h_aliases[i]; i++) 812311Sjkh len += strlen(hp->h_aliases[i]) + 1; 822311Sjkh len++; 832311Sjkh 842311Sjkh if (len > sizeof(result)) 852311Sjkh return(NULL); 862311Sjkh 872311Sjkh bzero(result, sizeof(result)); 882311Sjkh 892311Sjkh bcopy(hp->h_addr, &addr, sizeof(struct in_addr)); 902311Sjkh snprintf(result, sizeof(result), "%s %s", inet_ntoa(addr), hp->h_name); 912311Sjkh 922311Sjkh for (i = 0; hp->h_aliases[i]; i++) { 932311Sjkh strcat(result, " "); 942311Sjkh strcat(result, hp->h_aliases[i]); 952311Sjkh } 962311Sjkh 972311Sjkh return ((char *)&result); 982311Sjkh} 992311Sjkh 1002311Sjkh#define MAXPACKET 1024 1012311Sjkh#define DEF_TTL 50 1022311Sjkh 1032311Sjkh#define BY_DNS_ID 1 1042311Sjkh#define BY_RPC_XID 2 1052311Sjkh 1062311Sjkhextern struct hostent *__dns_getanswer(char *, int, char *, int); 1072311Sjkh 1082311Sjkhstatic TAILQ_HEAD(dns_qhead, circleq_dnsentry) qhead; 1092311Sjkh 1102311Sjkhstruct circleq_dnsentry { 1112311Sjkh SVCXPRT *xprt; 1122311Sjkh unsigned long xid; 1132311Sjkh struct sockaddr_in client_addr; 114173412Skevlo unsigned long ypvers; 1152311Sjkh unsigned long id; 1162311Sjkh unsigned long ttl; 1172311Sjkh unsigned long type; 118173412Skevlo unsigned short prot_type; 1192311Sjkh char **domain; 1202311Sjkh char *name; 1212311Sjkh struct in_addr addr; 122173412Skevlo TAILQ_ENTRY(circleq_dnsentry) links; 1232311Sjkh}; 1242311Sjkh 1252311Sjkhstatic int pending = 0; 126173412Skevlo 1272311Sjkhint yp_init_resolver() 1282311Sjkh{ 1292311Sjkh TAILQ_INIT(&qhead); 1302311Sjkh if (!(_res.options & RES_INIT) && res_init() == -1) { 1312311Sjkh yp_error("res_init failed"); 1322311Sjkh return(1); 1332311Sjkh } 134173412Skevlo if ((resfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 1352311Sjkh yp_error("couldn't create socket"); 1362311Sjkh return(1); 1372311Sjkh } 138173412Skevlo if (fcntl(resfd, F_SETFL, O_NONBLOCK) == -1) { 1392311Sjkh yp_error("couldn't make resolver socket non-blocking"); 1402311Sjkh return(1); 1412311Sjkh } 142173412Skevlo return(0); 1432311Sjkh} 1442311Sjkh 1452311Sjkhstatic struct circleq_dnsentry *yp_malloc_dnsent() 146173412Skevlo{ 1472311Sjkh register struct circleq_dnsentry *q; 148 149 q = (struct circleq_dnsentry *)malloc(sizeof(struct circleq_dnsentry)); 150 151 if (q == NULL) { 152 yp_error("failed to malloc() circleq dns entry"); 153 return(NULL); 154 } 155 156 return(q); 157} 158 159/* 160 * Transmit a query. 161 */ 162static unsigned long yp_send_dns_query(name, type) 163 char *name; 164 int type; 165{ 166 char buf[MAXPACKET]; 167 int n; 168 HEADER *hptr; 169 int ns; 170 int rval; 171 unsigned long id; 172 173 bzero(buf, sizeof(buf)); 174 175 n = res_mkquery(QUERY,name,C_IN,type,NULL,0,NULL,buf,sizeof(buf)); 176 177 if (n <= 0) { 178 yp_error("res_mkquery failed"); 179 return(0); 180 } 181 182 hptr = (HEADER *)&buf; 183 id = ntohs(hptr->id); 184 185 for (ns = 0; ns < _res.nscount; ns++) { 186 rval = sendto(resfd, buf, n, 0, 187 (struct sockaddr *)&_res.nsaddr_list[ns], 188 sizeof(struct sockaddr)); 189 if (rval == -1) { 190 yp_error("sendto failed"); 191 return(0); 192 } 193 } 194 195 return(id); 196} 197 198static struct circleq_dnsentry *yp_find_dnsqent(id, type) 199 unsigned long id; 200 int type; 201{ 202 register struct circleq_dnsentry *q; 203 204 TAILQ_FOREACH(q, &qhead, links) { 205 switch (type) { 206 case BY_RPC_XID: 207 if (id == q->xid) 208 return(q); 209 break; 210 case BY_DNS_ID: 211 default: 212 if (id == q->id) 213 return(q); 214 break; 215 } 216 } 217 return (NULL); 218} 219 220static void yp_send_dns_reply(q, buf) 221 struct circleq_dnsentry *q; 222 char *buf; 223{ 224 ypresponse result_v1; 225 ypresp_val result_v2; 226 unsigned long xid; 227 struct sockaddr_in client_addr; 228 xdrproc_t xdrfunc; 229 char *result; 230 231 /* 232 * Set up correct reply struct and 233 * XDR filter depending on ypvers. 234 */ 235 switch (q->ypvers) { 236 case YPVERS: 237 bzero((char *)&result_v2, sizeof(result_v2)); 238 239 if (buf == NULL) 240 result_v2.stat = YP_NOKEY; 241 else { 242 result_v2.val.valdat_len = strlen(buf); 243 result_v2.val.valdat_val = buf; 244 result_v2.stat = YP_TRUE; 245 } 246 result = (char *)&result_v2; 247 xdrfunc = (xdrproc_t)xdr_ypresp_val; 248 break; 249 case YPOLDVERS: 250 /* 251 * The odds are we will _never_ execute this 252 * particular code, but we include it anyway 253 * for the sake of completeness. 254 */ 255 bzero((char *)&result_v1, sizeof(result_v1)); 256 result_v1.yp_resptype = YPRESP_VAL; 257# define YPVAL ypresponse_u.yp_resp_valtype 258 259 if (buf == NULL) 260 result_v1.YPVAL.stat = YP_NOKEY; 261 else { 262 result_v1.YPVAL.val.valdat_len = strlen(buf); 263 result_v1.YPVAL.val.valdat_val = buf; 264 result_v1.YPVAL.stat = YP_TRUE; 265 } 266 result = (char *)&result_v1; 267 xdrfunc = (xdrproc_t)xdr_ypresponse; 268 break; 269 default: 270 yp_error("bad YP program version (%lu)!", q->ypvers); 271 return; 272 break; 273 } 274 275 if (debug) 276 yp_error("sending dns reply to %s (%lu)", 277 inet_ntoa(q->client_addr.sin_addr), q->id); 278 /* 279 * XXX This is disgusting. There's basically one transport 280 * handle for UDP, but we're holding off on replying to a 281 * client until we're ready, by which time we may have received 282 * several other queries from other clients with different 283 * transaction IDs. So to make the delayed response thing work, 284 * we have to save the transaction ID and client address of 285 * each request, then jam them into the transport handle when 286 * we're ready to send a reply. Then after we've send the reply, 287 * we put the old transaction ID and remote address back the 288 * way we found 'em. This is _INCREDIBLY_ non-portable; it's 289 * not even supported by the RPC library. 290 */ 291 /* 292 * XXX Don't frob the transaction ID for TCP handles. 293 */ 294 if (q->prot_type == SOCK_DGRAM) 295 xid = svcudp_set_xid(q->xprt, q->xid); 296 client_addr = q->xprt->xp_raddr; 297 q->xprt->xp_raddr = q->client_addr; 298 299 if (!svc_sendreply(q->xprt, xdrfunc, result)) 300 yp_error("svc_sendreply failed"); 301 302 /* 303 * Now that we sent the reply, 304 * put the handle back the way it was. 305 */ 306 if (q->prot_type == SOCK_DGRAM) 307 svcudp_set_xid(q->xprt, xid); 308 q->xprt->xp_raddr = client_addr; 309 310 return; 311} 312 313/* 314 * Decrement TTL on all queue entries, possibly nuking 315 * any that have been around too long without being serviced. 316 */ 317void yp_prune_dnsq() 318{ 319 register struct circleq_dnsentry *q, *n; 320 321 q = TAILQ_FIRST(&qhead); 322 while (q != NULL) { 323 q->ttl--; 324 n = TAILQ_NEXT(q, links); 325 if (!q->ttl) { 326 TAILQ_REMOVE(&qhead, q, links); 327 free(q->name); 328 free(q); 329 pending--; 330 } 331 q = n; 332 } 333 334 if (pending < 0) 335 pending = 0; 336 337 return; 338} 339 340/* 341 * Data is pending on the DNS socket; check for valid replies 342 * to our queries and dispatch them to waiting clients. 343 */ 344void yp_run_dnsq() 345{ 346 register struct circleq_dnsentry *q; 347 char buf[sizeof(HEADER) + MAXPACKET]; 348 char retrybuf[MAXHOSTNAMELEN]; 349 struct sockaddr_in sin; 350 int rval; 351 int len; 352 HEADER *hptr; 353 struct hostent *hent; 354 355 if (debug) 356 yp_error("running dns queue"); 357 358 bzero(buf, sizeof(buf)); 359 360 len = sizeof(struct sockaddr_in); 361 rval = recvfrom(resfd, buf, sizeof(buf), 0, 362 (struct sockaddr *)&sin, &len); 363 364 if (rval == -1) { 365 yp_error("recvfrom failed: %s", strerror(errno)); 366 return; 367 } 368 369 /* 370 * We may have data left in the socket that represents 371 * replies to earlier queries that we don't care about 372 * anymore. If there are no lookups pending or the packet 373 * ID doesn't match any of the queue IDs, just drop it 374 * on the floor. 375 */ 376 hptr = (HEADER *)&buf; 377 if (!pending || 378 (q = yp_find_dnsqent(ntohs(hptr->id), BY_DNS_ID)) == NULL) { 379 /* ignore */ 380 return; 381 } 382 383 if (debug) 384 yp_error("got dns reply from %s", inet_ntoa(sin.sin_addr)); 385 386 hent = __dns_getanswer(buf, rval, q->name, q->type); 387 388 /* 389 * If the lookup failed, try appending one of the domains 390 * from resolv.conf. If we have no domains to test, the 391 * query has failed. 392 */ 393 if (hent == NULL) { 394 if ((h_errno == TRY_AGAIN || h_errno == NO_RECOVERY) 395 && q->domain && *q->domain) { 396 snprintf(retrybuf, sizeof(retrybuf), "%s.%s", 397 q->name, *q->domain); 398 if (debug) 399 yp_error("retrying with: %s", retrybuf); 400 q->id = yp_send_dns_query(retrybuf, q->type); 401 q->ttl = DEF_TTL; 402 q->domain++; 403 return; 404 } 405 } else { 406 if (q->type == T_PTR) { 407 hent->h_addr = (char *)&q->addr.s_addr; 408 hent->h_length = sizeof(struct in_addr); 409 } 410 } 411 412 /* Got an answer ready for a client -- send it off. */ 413 yp_send_dns_reply(q, parse(hent)); 414 pending--; 415 TAILQ_REMOVE(&qhead, q, links); 416 free(q->name); 417 free(q); 418 419 /* Decrement TTLs on other entries while we're here. */ 420 yp_prune_dnsq(); 421 422 return; 423} 424 425/* 426 * Queue and transmit an asynchronous DNS hostname lookup. 427 */ 428ypstat yp_async_lookup_name(rqstp, name) 429 struct svc_req *rqstp; 430 char *name; 431{ 432 register struct circleq_dnsentry *q; 433 int type, len; 434 435 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */ 436 type = -1; len = sizeof(type); 437 if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET, 438 SO_TYPE, &type, &len) == -1) { 439 yp_error("getsockopt failed: %s", strerror(errno)); 440 return(YP_YPERR); 441 } 442 443 /* Avoid transmitting dupe requests. */ 444 if (type == SOCK_DGRAM && 445 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL) 446 return(YP_TRUE); 447 448 if ((q = yp_malloc_dnsent()) == NULL) 449 return(YP_YPERR); 450 451 q->type = T_A; 452 q->ttl = DEF_TTL; 453 q->xprt = rqstp->rq_xprt; 454 q->ypvers = rqstp->rq_vers; 455 q->prot_type = type; 456 if (q->prot_type == SOCK_DGRAM) 457 q->xid = svcudp_get_xid(q->xprt); 458 q->client_addr = q->xprt->xp_raddr; 459 q->domain = _res.dnsrch; 460 q->id = yp_send_dns_query(name, q->type); 461 462 if (q->id == 0) { 463 yp_error("DNS query failed"); 464 free(q); 465 return(YP_YPERR); 466 } 467 468 q->name = strdup(name); 469 TAILQ_INSERT_HEAD(&qhead, q, links); 470 pending++; 471 472 if (debug) 473 yp_error("queueing async DNS name lookup (%d)", q->id); 474 475 yp_prune_dnsq(); 476 return(YP_TRUE); 477} 478 479/* 480 * Queue and transmit an asynchronous DNS IP address lookup. 481 */ 482ypstat yp_async_lookup_addr(rqstp, addr) 483 struct svc_req *rqstp; 484 char *addr; 485{ 486 register struct circleq_dnsentry *q; 487 char buf[MAXHOSTNAMELEN]; 488 int a, b, c, d; 489 int type, len; 490 491 /* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */ 492 type = -1; len = sizeof(type); 493 if (getsockopt(rqstp->rq_xprt->xp_fd, SOL_SOCKET, 494 SO_TYPE, &type, &len) == -1) { 495 yp_error("getsockopt failed: %s", strerror(errno)); 496 return(YP_YPERR); 497 } 498 499 /* Avoid transmitting dupe requests. */ 500 if (type == SOCK_DGRAM && 501 yp_find_dnsqent(svcudp_get_xid(rqstp->rq_xprt),BY_RPC_XID) != NULL) 502 return(YP_TRUE); 503 504 if ((q = yp_malloc_dnsent()) == NULL) 505 return(YP_YPERR); 506 507 if (sscanf(addr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) 508 return(YP_NOKEY); 509 510 snprintf(buf, sizeof(buf), "%d.%d.%d.%d.in-addr.arpa", d, c, b, a); 511 512 if (debug) 513 yp_error("DNS address is: %s", buf); 514 515 q->type = T_PTR; 516 q->ttl = DEF_TTL; 517 q->xprt = rqstp->rq_xprt; 518 q->ypvers = rqstp->rq_vers; 519 q->domain = NULL; 520 q->prot_type = type; 521 if (q->prot_type == SOCK_DGRAM) 522 q->xid = svcudp_get_xid(q->xprt); 523 q->client_addr = q->xprt->xp_raddr; 524 q->id = yp_send_dns_query(buf, q->type); 525 526 if (q->id == 0) { 527 yp_error("DNS query failed"); 528 free(q); 529 return(YP_YPERR); 530 } 531 532 inet_aton(addr, &q->addr); 533 q->name = strdup(buf); 534 TAILQ_INSERT_HEAD(&qhead, q, links); 535 pending++; 536 537 if (debug) 538 yp_error("queueing async DNS address lookup (%d)", q->id); 539 540 yp_prune_dnsq(); 541 return(YP_TRUE); 542} 543