From sparky@tccn.cs.kun.nl  Tue Jan  6 02:52:41 1998
Received: from tccn.cs.kun.nl (tccn.cs.kun.nl [131.174.32.38])
          by hub.freebsd.org (8.8.7/8.8.7) with ESMTP id CAA27865
          for <FreeBSD-gnats-submit@freebsd.org>; Tue, 6 Jan 1998 02:52:40 -0800 (PST)
          (envelope-from sparky@tccn.cs.kun.nl)
Received: (from sparky@localhost)
	by tccn.cs.kun.nl (8.8.5/8.8.5) id MAA26525;
	Tue, 6 Jan 1998 12:16:12 +0100 (CET)
Message-Id: <199801061116.MAA26525@tccn.cs.kun.nl>
Date: Tue, 6 Jan 1998 12:16:12 +0100 (CET)
From: sparky@tccn.cs.kun.nl
Reply-To: sparky@tccn.cs.kun.nl
To: FreeBSD-gnats-submit@freebsd.org
Subject: ypserv uses wrong dns lookup order
X-Send-Pr-Version: 3.2

>Number:         5444
>Category:       bin
>Synopsis:       [PATCH] ypserv uses wrong dns lookup order
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jan  8 14:31:55 PST 1998
>Closed-Date:    Thu Jan 17 08:12:05 PST 2002
>Last-Modified:  Thu Jan 17 08:12:05 PST 2002
>Originator:     Franc Grootjen and Kees Jan Koster
>Release:        FreeBSD 2.2.2-RELEASE i386
>Organization:
University of Nijmegen, The Netherlands
>Environment:
A local network (at home) with a few arcane architectures (PDP11, Sun3,
VAX), a up to date Pentium FreeBSD server (DNS, NIS) and a masquerading
gateway to the internet.
>Description:
A ping to a (short named) local host on a NIS client leads to an
unreasonable long timeout. A fully qualified hostname (that is including 
the local domainname) works without problems.
>How-To-Repeat:
Set up a FreeBSD NIS server (use ypserv -n). Configure a NIS client so that
it will _not_ perform DNS queries on its own (a Sun3 always uses NIS or YP
to resolve hosts). Ping a local (short named) host on your local net
(for example 'charon'). Watch the debug output from 'named' running on
your FreeBSD machine. You'll see ypserv querying 'charon' and 
'charon.my.domain' in that order. If you configured your local name server
to resolve '.my.domain' hosts, and forward all other queries to a name server
on the internet, this particular search order can lead to long timeouts
(especially if the gateway is not connected to the internet).
Ping a local (short named) host on your FreeBSD box. You'll notice that
the libc resolver routines will first try to resolv 'charon.my.domain' and
if that fails 'charon'.
>Fix:
Inspection of the ypserv code reveals that ypserv does _not_ use libc to
resolve dns queries (to prevent recursion), but calls 'named' on its own.
The following patch to yp_dnslookup.c will change the query order (first 
the specified search domains from /etc/resolv.conf, followed by
the plain name).

*** yp_dnslookup.c.orig	Sun Jan  4 00:39:27 1998
--- yp_dnslookup.c	Sun Jan  4 00:44:44 1998
***************
*** 382,401 ****
  	hent = __dns_getanswer(buf, rval, q->name, q->type);
  
  	/*
! 	 * If the lookup failed, try appending one of the domains
! 	 * from resolv.conf. If we have no domains to test, the
  	 * query has failed.
  	 */
  	if (hent == NULL) {
! 		if (h_errno == TRY_AGAIN && q->domain && *q->domain) {
! 			snprintf(retrybuf, sizeof(retrybuf), "%s.%s",
! 						q->name, *q->domain);
! 			if (debug)
! 				yp_error("Retrying with: %s", retrybuf);
! 			q->id = yp_send_dns_query(retrybuf, q->type);
! 			q->ttl = DEF_TTL;
! 			q->domain++;
! 			return;
  		}
  	} else {
  		if (q->type == T_PTR) {
--- 382,411 ----
  	hent = __dns_getanswer(buf, rval, q->name, q->type);
  
  	/*
! 	 * If the lookup failed, try appending one of the other domains
! 	 * from resolv.conf. End the search trying the plain name.
!          * If we have no domains to test (q->domain==NULL), the
  	 * query has failed.
  	 */
  	if (hent == NULL) {
! 		if (h_errno == TRY_AGAIN && q->domain)
!                 {
!                   if(*q->domain) /* domains left? */
!                   {
!                     snprintf(retrybuf, sizeof(retrybuf), "%s.%s",q->name, *q->domain);
! 		    q->domain++;
! 		    if (debug)
! 		      yp_error("Retrying with: %s", retrybuf);
!                   } else /* try plain name */
!                   {
!                     strcpy(retrybuf,q->name);
!                     q->domain=NULL;
!                     if(debug)
!                       yp_error("Last resort: %s", retrybuf);
!                   }
! 		  q->id = yp_send_dns_query(retrybuf, q->type);
! 		  q->ttl = DEF_TTL;
! 		  return;
  		}
  	} else {
  		if (q->type == T_PTR) {
***************
*** 426,431 ****
--- 436,442 ----
  {
  	register struct circleq_dnsentry *q;
  	int type, len;
+ 	char buf[MAXHOSTNAMELEN];
  
  	/* Check for SOCK_DGRAM or SOCK_STREAM -- we need to know later */
  	type = -1; len = sizeof(type);
***************
*** 451,461 ****
  	if (q->prot_type == SOCK_DGRAM)
  		q->xid = svcudp_get_xid(q->xprt);
  	q->client_addr = q->xprt->xp_raddr;
  	if (!strchr(name, '.'))
  		q->domain = _res.dnsrch;
! 	else
  		q->domain = NULL;
! 	q->id = yp_send_dns_query(name, q->type);
  
  	if (q->id == 0) {
  		yp_error("DNS query failed");
--- 462,491 ----
  	if (q->prot_type == SOCK_DGRAM)
  		q->xid = svcudp_get_xid(q->xprt);
  	q->client_addr = q->xprt->xp_raddr;
+ 
  	if (!strchr(name, '.'))
+         { /* Dotless hostname */
  		q->domain = _res.dnsrch;
!                 if(*q->domain)
!                 { /* There is a search domain... add first */
! 		  snprintf(buf, sizeof(buf), "%s.%s",name, *q->domain);
!                   q->domain++;
!                 } else /* No search domain, use name */
!                 {
!                   strcpy(buf,name);
!                   q->domain=NULL;
!                 }
!         }
! 	else /* hostname with dot(s), use name */
!         {
!          	strcpy(buf,name);
  		q->domain = NULL;
!         }
! 
! 	if (debug)
! 		yp_error("Trying: %s", buf);
! 
! 	q->id = yp_send_dns_query(buf, q->type);
  
  	if (q->id == 0) {
  		yp_error("DNS query failed");
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->suspended 
State-Changed-By: phk 
State-Changed-When: Thu Apr 30 22:44:07 PDT 1998 
State-Changed-Why:  
Awaiting committer 
State-Changed-From-To: suspended->feedback 
State-Changed-By: mikeh 
State-Changed-When: Thu Jun 14 21:13:29 PDT 2001 
State-Changed-Why:  
I believe this is fixed, is it still a problem with 4-stable? 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=5444 
State-Changed-From-To: feedback->closed 
State-Changed-By: sheldonh 
State-Changed-When: Thu Jan 17 08:12:05 PST 2002 
State-Changed-Why:  
Automatic feedback timeout.  If additional feedback that warrants 
the re-opening of this PR is available but not included in the 
audit trail, please include the feedback in a reply to this message 
(preserving the Subject line) and ask that the PR be re-opened. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=5444 
>Unformatted:
