dealing with name queries and dns lookups etc.
[samba.git] / source3 / namedbname.c
index 3e6b8428cf270d3f3ecd716450ceabe2eb9e6bac..8bf65117374913ed28079da4cd4b036e11ad0a3e 100644 (file)
@@ -150,7 +150,8 @@ struct name_record *find_name(struct name_record *n,
                if (name_equal(&ret->name,name))
                {
                        /* self search: self names only */
-                       if ((search&FIND_SELF) == FIND_SELF && ret->source != SELF)
+                       if ((search&FIND_SELF) == FIND_SELF && 
+                           ret->source != SELF)
                                continue;
          
                        return ret;
@@ -173,27 +174,25 @@ struct name_record *find_name_search(struct subnet_record **d,
 {
        if (d == NULL) return NULL; /* bad error! */
        
-    if ((search & FIND_LOCAL) == FIND_LOCAL)
-       {
-               if (*d != NULL)
-        {
-                       struct name_record *n = find_name((*d)->namelist, name, search);
-                       DEBUG(4,("find_name on local: %s %s search %x\n",
-                                               namestr(name),inet_ntoa(ip), search));
-                       if (n) return n;
-               }
-       }
-
-       if ((search & FIND_WINS) != FIND_WINS) return NULL;
-
-       /* find WINS subnet record. */
-       *d = find_subnet(ipgrp);
+    if (search & FIND_LOCAL) {
+      if (*d != NULL) {
+       struct name_record *n = find_name((*d)->namelist, name, search);
+       DEBUG(4,("find_name on local: %s %s search %x\n",
+                namestr(name),inet_ntoa(ip), search));
+       if (n) return n;
+      }
+    }
 
-       if (*d == NULL) return NULL;
+    if (!(search & FIND_WINS)) return NULL;
 
-       DEBUG(4,("find_name on WINS: %s %s search %x\n",
-                                               namestr(name),inet_ntoa(ip), search));
-       return find_name((*d)->namelist, name, search);
+    /* find WINS subnet record. */
+    *d = find_subnet(ipgrp);
+    
+    if (*d == NULL) return NULL;
+    
+    DEBUG(4,("find_name on WINS: %s %s search %x\n",
+            namestr(name),inet_ntoa(ip), search));
+    return find_name((*d)->namelist, name, search);
 }
 
 
@@ -238,37 +237,28 @@ void dump_names(void)
 
         for (i = 0; i < n->num_ips; i++)
         {
-           DEBUG(3,("%15s NB=%2x ",
-                                               inet_ntoa(n->ip_flgs[i].ip),
-                                               n->ip_flgs[i].nb_flags));
+           DEBUG(3,("%15s NB=%2x source=%d",
+                   inet_ntoa(n->ip_flgs[i].ip),
+                   n->ip_flgs[i].nb_flags,n->source));
 
         }
                DEBUG(3,("\n"));
 
       if (f && ip_equal(d->bcast_ip, ipgrp) && n->source == REGISTER)
       {
-        fstring data;
-
       /* XXXX i have little imagination as to how to output nb_flags as
          anything other than as a hexadecimal number :-) */
 
-        sprintf(data, "%s#%02x %ld",
+        fprintf(f, "%s#%02x %ld ",
               n->name.name,n->name.name_type, /* XXXX ignore scope for now */
               n->death_time);
-           fprintf(f, "%s", data);
+
         for (i = 0; i < n->num_ips; i++)
         {
-           DEBUG(3,("%15s NB=%2x ",
-                                               inet_ntoa(n->ip_flgs[i].ip),
-                                               n->ip_flgs[i].nb_flags));
-
-           sprintf(data, "%s %2x ",
+           fprintf(f, "%s %2x ",
                                                inet_ntoa(n->ip_flgs[i].ip),
                                                n->ip_flgs[i].nb_flags);
-   
-                  fprintf(f, "%s", data);
         }
-               DEBUG(3,("\n"));
                fprintf(f, "\n");
       }
 
@@ -284,8 +274,10 @@ void dump_names(void)
 
 
 /****************************************************************************
-load a netbios name database file
-****************************************************************************/
+  load a netbios name database file
+
+  XXXX we cannot cope with loading Internet Group names, yet
+  ****************************************************************************/
 void load_netbios_names(void)
 {
   struct subnet_record *d = find_subnet(ipgrp);
@@ -314,7 +306,7 @@ void load_netbios_names(void)
 
       pstring name;
       int type = 0;
-      int nb_flags;
+      unsigned int nb_flags;
       time_t ttd;
          struct in_addr ipaddr;
 
@@ -332,15 +324,15 @@ void load_netbios_names(void)
        ptr = line;
 
        if (next_token(&ptr,name_str    ,NULL)) ++count;
-       if (next_token(&ptr,ip_str      ,NULL)) ++count;
        if (next_token(&ptr,ttd_str     ,NULL)) ++count;
+       if (next_token(&ptr,ip_str      ,NULL)) ++count;
        if (next_token(&ptr,nb_flags_str,NULL)) ++count;
 
        if (count <= 0) continue;
 
        if (count != 4) {
          DEBUG(0,("Ill formed wins line"));
-         DEBUG(0,("[%s]: name#type ip nb_flags abs_time\n",line));
+         DEBUG(0,("[%s]: name#type abs_time ip nb_flags\n",line));
          continue;
        }
 
@@ -368,11 +360,11 @@ void load_netbios_names(void)
          source = REGISTER;
       }
 
-      DEBUG(4, ("add WINS line: %s#%02x %s %ld %2x\n",
-              name,type, inet_ntoa(ipaddr), ttd, nb_flags));
+      DEBUG(4, ("add WINS line: %s#%02x %ld %s %2x\n",
+              name,type, ttd, inet_ntoa(ipaddr), nb_flags));
 
       /* add all entries that have 60 seconds or more to live */
-      if (ttd - 60 < time(NULL) || ttd == 0)
+      if (ttd - 60 > time(NULL) || ttd == 0)
       {
         time_t t = (ttd?ttd-time(NULL):0) / 3;
 
@@ -493,13 +485,17 @@ void expire_names(time_t t)
        for (d = subnetlist; d; d = d->next)
        {
          for (n = d->namelist; n; n = next)
+           {
+             next = n->next;
+             if (n->death_time && n->death_time < t)
                {
-                 if (n->death_time && n->death_time < t)
-               {
+                 if (n->source == SELF) {
+                   DEBUG(3,("not expiring SELF name %s\n", namestr(&n->name)));
+                   n->death_time += 300;
+                   continue;
+                 }
                  DEBUG(3,("Removing dead name %s\n", namestr(&n->name)));
                  
-                 next = n->next;
-                 
                  if (n->prev) n->prev->next = n->next;
                  if (n->next) n->next->prev = n->prev;
                  
@@ -508,11 +504,7 @@ void expire_names(time_t t)
                  free(n->ip_flgs);
                  free(n);
                }
-                 else
-               {
-                 next = n->next;
-               }
-               }
+           }
        }
 }
 
@@ -520,30 +512,32 @@ void expire_names(time_t t)
 /***************************************************************************
   reply to a name query
   ****************************************************************************/
-struct name_record *search_for_name(struct subnet_record **d,
-                                       struct nmb_name *question,
-                                   struct in_addr ip, int Time, int search)
+struct name_record *dns_name_search(struct nmb_name *question,
+                                   int Time, int search)
 {
+  struct subnet_record *d = find_subnet(ipgrp);
   int name_type = question->name_type;
   char *qname = question->name;
-  BOOL dns_type = name_type == 0x20 || name_type == 0;
-  
+  BOOL dns_type = (name_type == 0x20 || name_type == 0);
   struct name_record *n;
   
+  if (d == NULL) return NULL;
+
   DEBUG(3,("Search for %s from %s - ", namestr(question), inet_ntoa(ip)));
   
-  /* first look up name in cache */
-  n = find_name_search(d,question,search,ip);
-  
-  if (*d == NULL) return NULL;
-
-  DEBUG(4,("subnet %s ", inet_ntoa((*d)->bcast_ip)));
+  if (!n && (search & FIND_SELF))
+  {
+    if (!lp_wins_proxy())
+      DEBUG(3,("wins proxy not enabled - failing lookup\n"));
+    else
+      DEBUG(3,("FIND_SELF set - failing lookup\n"));
+    return NULL;
+  }
 
   /* now try DNS lookup. */
   if (!n)
     {
       struct in_addr dns_ip;
-      unsigned long a;
       
       /* only do DNS lookups if the query is for type 0x20 or type 0x0 */
       if (!dns_type && name_type != 0x1b)
@@ -553,11 +547,9 @@ struct name_record *search_for_name(struct subnet_record **d,
        }
       
       /* look it up with DNS */      
-      a = interpret_addr(qname);
-      
-      putip((char *)&dns_ip,(char *)&a);
+      dns_ip.s_addr = interpret_addr(qname);
       
-      if (!a)
+      if (dns_ip.s_addr)
        {
          /* no luck with DNS. We could possibly recurse here XXXX */
          DEBUG(3,("no recursion.\n"));
@@ -575,21 +567,6 @@ struct name_record *search_for_name(struct subnet_record **d,
       if (!n) return NULL;
     }
   
-  /* is our entry already dead? */
-  if (n->death_time)
-    {
-      if (n->death_time < Time) return False;
-    }
-  
-  /* it may have been an earlier failure */
-  if (n->source == DNSFAIL)
-    {
-      DEBUG(3,("DNSFAIL\n"));
-      return NULL;
-    }
-  
-  DEBUG(3,("OK %s\n",inet_ntoa(n->ip_flgs[0].ip)));      
-  
   return n;
 }