updated the 3.0 branch from the head branch - ready for alpha18
[samba.git] / source3 / nmbd / nmbd_incomingrequests.c
index ae163c601459e74ad2787647c80903cc63692672..834f237a2c24ae991be569a5866526ddca99008a 100644 (file)
@@ -1,6 +1,5 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    NBT netbios routines and daemon - version 2
    Copyright (C) Andrew Tridgell 1994-1998
    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
@@ -28,8 +27,7 @@
 
 #include "includes.h"
 
-extern int DEBUGLEVEL;
-extern fstring myworkgroup;
+extern fstring global_myworkgroup;
 
 /****************************************************************************
 Send a name release response.
@@ -81,7 +79,7 @@ void process_name_release_request(struct subnet_record *subrec,
      */
     DEBUG(0,("process_name_release_request: unicast name release request \
 received for name %s from IP %s on subnet %s. Error - should be sent to WINS server\n",
-          namestr(question), inet_ntoa(owner_ip), subrec->subnet_name));      
+          nmb_namestr(question), inet_ntoa(owner_ip), subrec->subnet_name));      
 
     send_name_release_response(FMT_ERR, p);
     return;
@@ -89,7 +87,7 @@ received for name %s from IP %s on subnet %s. Error - should be sent to WINS ser
 
   DEBUG(3,("process_name_release_request: Name release on name %s, \
 subnet %s from owner IP %s\n",
-           namestr(&nmb->question.question_name),
+           nmb_namestr(&nmb->question.question_name),
            subrec->subnet_name, inet_ntoa(owner_ip)));
   
   /* If someone is releasing a broadcast group name, just ignore it. */
@@ -102,24 +100,26 @@ subnet %s from owner IP %s\n",
    * names and *don't set the group bit* !!!!!
    */
 
-  if( !group && !ismyip(owner_ip) && strequal(question->name, myworkgroup) && 
+  if( !group && !ismyip(owner_ip) && strequal(question->name, global_myworkgroup) && 
       ((question->name_type == 0x0) || (question->name_type == 0x1e)))
   {
     DEBUG(6,("process_name_release_request: FTP OnNet bug workaround. Ignoring \
 group release name %s from IP %s on subnet %s with no group bit set.\n",
-        namestr(question), inet_ntoa(owner_ip), subrec->subnet_name ));
+        nmb_namestr(question), inet_ntoa(owner_ip), subrec->subnet_name ));
     return;
   }
 
   namerec = find_name_on_subnet(subrec, &nmb->question.question_name, FIND_ANY_NAME);
 
   /* We only care about someone trying to release one of our names. */
-  if (namerec && ((namerec->source == SELF_NAME) || (namerec->source == PERMANENT_NAME)))
+  if( namerec
+   && ( (namerec->data.source == SELF_NAME)
+     || (namerec->data.source == PERMANENT_NAME) ) )
   {
     rcode = ACT_ERR;
     DEBUG(0, ("process_name_release_request: Attempt to release name %s from IP %s \
 on subnet %s being rejected as it is one of our names.\n", 
-          namestr(&nmb->question.question_name), inet_ntoa(owner_ip), subrec->subnet_name));
+          nmb_namestr(&nmb->question.question_name), inet_ntoa(owner_ip), subrec->subnet_name));
   }
 
   if(rcode == 0)
@@ -174,8 +174,9 @@ void process_name_refresh_request(struct subnet_record *subrec,
        and send an error reply back.
      */
     DEBUG(0,("process_name_refresh_request: unicast name registration request \
-received for name %s from IP %s on subnet %s. Error - should be sent to WINS server\n",
-          namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
+received for name %s from IP %s on subnet %s.\n",
+          nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
+    DEBUG(0,("Error - should be sent to WINS server\n"));
     
     send_name_registration_response(FMT_ERR, 0, p);
     return;
@@ -185,7 +186,7 @@ received for name %s from IP %s on subnet %s. Error - should be sent to WINS ser
      refreshes. */
      
   DEBUG(3,("process_name_refresh_request: Name refresh for name %s \
-IP %s on subnet %s\n", namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
+IP %s on subnet %s\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
      
 }
     
@@ -218,14 +219,14 @@ void process_name_registration_request(struct subnet_record *subrec,
      */
     DEBUG(0,("process_name_registration_request: unicast name registration request \
 received for name %s from IP %s on subnet %s. Error - should be sent to WINS server\n",
-          namestr(question), inet_ntoa(from_ip), subrec->subnet_name));      
+          nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));      
 
     send_name_registration_response(FMT_ERR, 0, p);
     return;
   }
 
   DEBUG(3,("process_name_registration_request: Name registration for name %s \
-IP %s on subnet %s\n", namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
+IP %s on subnet %s\n", nmb_namestr(question), inet_ntoa(from_ip), subrec->subnet_name));
   
   /* See if the name already exists. */
   namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
@@ -236,7 +237,7 @@ IP %s on subnet %s\n", namestr(question), inet_ntoa(from_ip), subrec->subnet_nam
    * later to queries.
    */
 
-  if((namerec != NULL) && (namerec->source == WINS_PROXY_NAME))
+  if((namerec != NULL) && (namerec->data.source == WINS_PROXY_NAME))
   {
     remove_name_from_namelist( subrec, namerec );
     namerec = NULL;
@@ -246,10 +247,10 @@ IP %s on subnet %s\n", namestr(question), inet_ntoa(from_ip), subrec->subnet_nam
   {
     /* Unique name. */
 
-    if ((namerec != NULL) && 
-        ((namerec->source == SELF_NAME) || (namerec->source == PERMANENT_NAME) ||
-          NAME_GROUP(namerec))
-       )
+    if( (namerec != NULL)
+     && ( (namerec->data.source == SELF_NAME)
+       || (namerec->data.source == PERMANENT_NAME)
+       || NAME_GROUP(namerec) ) )
     {
       /* No-one can register one of Samba's names, nor can they
          register a name that's a group name as a unique name */
@@ -260,11 +261,11 @@ IP %s on subnet %s\n", namestr(question), inet_ntoa(from_ip), subrec->subnet_nam
     else if(namerec != NULL)
     {
       /* Update the namelist record with the new information. */
-      namerec->ip[0] = from_ip;
+      namerec->data.ip[0] = from_ip;
       update_name_ttl(namerec, ttl);
 
       DEBUG(3,("process_name_registration_request: Updated name record %s \
-with IP %s on subnet %s\n",namestr(&namerec->name),inet_ntoa(from_ip), subrec->subnet_name));
+with IP %s on subnet %s\n",nmb_namestr(&namerec->name),inet_ntoa(from_ip), subrec->subnet_name));
       return;
     }
   }
@@ -272,9 +273,10 @@ with IP %s on subnet %s\n",namestr(&namerec->name),inet_ntoa(from_ip), subrec->s
   {
     /* Group name. */
 
-    if((namerec != NULL) && !NAME_GROUP(namerec) &&
-        ((namerec->source == SELF_NAME) || (namerec->source == PERMANENT_NAME))
-      )
+    if( (namerec != NULL)
+     && !NAME_GROUP(namerec)
+     && ( (namerec->data.source == SELF_NAME)
+       || (namerec->data.source == PERMANENT_NAME) ) )
     {
       /* Disallow group names when we have a unique name. */
       send_name_registration_response(ACT_ERR, 0, p);  
@@ -290,20 +292,20 @@ We put our own names first, then in alphabetical order.
 
 static int status_compare(char *n1,char *n2)
 {
-  extern pstring myname;
+  extern pstring global_myname;
   int l1,l2,l3;
 
   /* It's a bit tricky because the names are space padded */
   for (l1=0;l1<15 && n1[l1] && n1[l1] != ' ';l1++) ;
   for (l2=0;l2<15 && n2[l2] && n2[l2] != ' ';l2++) ;
-  l3 = strlen(myname);
+  l3 = strlen(global_myname);
 
-  if ((l1==l3) && strncmp(n1,myname,l3) == 0 && 
-      (l2!=l3 || strncmp(n2,myname,l3) != 0))
+  if ((l1==l3) && strncmp(n1,global_myname,l3) == 0 && 
+      (l2!=l3 || strncmp(n2,global_myname,l3) != 0))
     return -1;
 
-  if ((l2==l3) && strncmp(n2,myname,l3) == 0 && 
-      (l1!=l3 || strncmp(n1,myname,l3) != 0))
+  if ((l2==l3) && strncmp(n2,global_myname,l3) == 0 && 
+      (l1!=l3 || strncmp(n1,global_myname,l3) != 0))
     return 1;
 
   return memcmp(n1,n2,18);
@@ -325,32 +327,34 @@ void process_node_status_request(struct subnet_record *subrec, struct packet_str
   struct name_record *namerec;
 
   DEBUG(3,("process_node_status_request: status request for name %s from IP %s on \
-subnet %s.\n", namestr(&nmb->question.question_name), inet_ntoa(p->ip),
+subnet %s.\n", nmb_namestr(&nmb->question.question_name), inet_ntoa(p->ip),
           subrec->subnet_name));
 
   if((namerec = find_name_on_subnet(subrec, &nmb->question.question_name,
                                     FIND_SELF_NAME)) == 0)
   {
     DEBUG(1,("process_node_status_request: status request for name %s from IP %s on \
-subnet %s - name not found.\n", namestr(&nmb->question.question_name),
+subnet %s - name not found.\n", nmb_namestr(&nmb->question.question_name),
           inet_ntoa(p->ip), subrec->subnet_name));
 
     return;
   }
  
-  /* XXXX hack, we should calculate exactly how many will fit. */
-  bufend = &rdata[MAX_DGRAM_SIZE] - 18;
+  /* this is not an exact calculation. the 46 is for the stats buffer
+     and the 60 is to leave room for the header etc */
+  bufend = &rdata[MAX_DGRAM_SIZE] - (18 + 46 + 60);
   countptr = buf = rdata;
   buf += 1;
   buf0 = buf;
 
   names_added = 0;
 
-  namerec = subrec->namelist;
+  namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
 
   while (buf < bufend) 
   {
-    if ((namerec->source == SELF_NAME) || (namerec->source == PERMANENT_NAME))
+    if( (namerec->data.source == SELF_NAME)
+     || (namerec->data.source == PERMANENT_NAME) )
     {
       int name_type = namerec->name.name_type;
       
@@ -361,25 +365,27 @@ subnet %s - name not found.\n", namestr(&nmb->question.question_name),
            strequal(qname, namerec->name.name)))
       {
         /* Start with the name. */
-        bzero(buf,18);
-        sprintf(buf,"%-15.15s",namerec->name.name);
+        memset(buf,'\0',18);
+        slprintf(buf, 17, "%-15.15s",namerec->name.name);
         strupper(buf);
         
         /* Put the name type and netbios flags in the buffer. */
         buf[15] = name_type;
-        set_nb_flags(&buf[16],namerec->nb_flags);
+        set_nb_flags( &buf[16],namerec->data.nb_flags );
         buf[16] |= NB_ACTIVE; /* all our names are active */
 
         buf += 18;
-      
+
         names_added++;
       }
     }
 
     /* Remove duplicate names. */
-    qsort(buf0,names_added,18,QSORT_CAST status_compare);
+    if (names_added > 1) {
+           qsort( buf0, names_added, 18, QSORT_CAST status_compare );
+    }
 
-    for (i=1;i<names_added;i++)
+    for( i=1; i < names_added ; i++ )
     {
       if (memcmp(buf0 + 18*i,buf0 + 18*(i-1),16) == 0) 
       {
@@ -393,7 +399,7 @@ subnet %s - name not found.\n", namestr(&nmb->question.question_name),
 
     buf = buf0 + 18*names_added;
 
-    namerec = namerec->next;
+    namerec = (struct name_record *)ubi_trNext( namerec );
 
     if (!namerec)
     {
@@ -404,7 +410,7 @@ subnet %s - name not found.\n", namestr(&nmb->question.question_name),
       if (uni_subrec != subrec)
       {
         subrec = uni_subrec;
-        namerec = subrec->namelist;
+        namerec = (struct name_record *)ubi_trFirst( subrec->namelist );
       }
     }
     if (!namerec)
@@ -416,7 +422,7 @@ subnet %s - name not found.\n", namestr(&nmb->question.question_name),
   
   /* We don't send any stats as they could be used to attack
      the protocol. */
-  bzero(buf,64);
+  memset(buf,'\0',46);
   
   buf += 46;
   
@@ -443,164 +449,151 @@ For broadcast name queries:
 
 void process_name_query_request(struct subnet_record *subrec, struct packet_struct *p)
 {
-  struct nmb_packet *nmb = &p->packet.nmb;
-  struct nmb_name *question = &nmb->question.question_name;
-  int name_type = question->name_type;
-  BOOL bcast = nmb->header.nm_flags.bcast;
-  int ttl=0;
-  int rcode = 0;
-  char *prdata = NULL;
-  char rdata[6];
-  BOOL success = False;
-  struct name_record *namerec = NULL;
-  int reply_data_len = 0;
-  int i;
-  DEBUG(3,("process_name_query_request: Name query from %s on subnet %s for name %s\n", 
-            inet_ntoa(p->ip), subrec->subnet_name, namestr(question)));
+       struct nmb_packet *nmb = &p->packet.nmb;
+       struct nmb_name *question = &nmb->question.question_name;
+       int name_type = question->name_type;
+       BOOL bcast = nmb->header.nm_flags.bcast;
+       int ttl=0;
+       int rcode = 0;
+       char *prdata = NULL;
+       char rdata[6];
+       BOOL success = False;
+       struct name_record *namerec = NULL;
+       int reply_data_len = 0;
+       int i;
+       
+       DEBUG(3,("process_name_query_request: Name query from %s on subnet %s for name %s\n", 
+                inet_ntoa(p->ip), subrec->subnet_name, nmb_namestr(question)));
   
-  /* Look up the name in the cache - if the request is a broadcast request that
-     came from a subnet we don't know about then search all the broadcast subnets
-     for a match (as we don't know what interface the request came in on). */
-
-  if(subrec == remote_broadcast_subnet)
-    namerec = find_name_for_remote_broadcast_subnet( question, FIND_ANY_NAME);
-  else
-    namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
-
-
-  /* Check if it is a name that expired */
-  if (namerec && ((namerec->death_time != PERMANENT_TTL) && (namerec->death_time < p->timestamp)))
-  {
-    DEBUG(5,("process_name_query_request: expired name %s\n", namestr(&namerec->name)));
-    namerec = NULL;
-  }
-
-  if (namerec)
-  {
-
-    /* 
-     * Always respond to unicast queries.
-     * Don't respond to broadcast queries unless the query is for
-     * a name we own, a Primary Domain Controller name, or a WINS_PROXY 
-     * name with type 0 or 0x20. WINS_PROXY names are only ever added
-     * into the namelist if we were configured as a WINS proxy.
-     */
-
-    if (!bcast || 
-        (bcast && ((name_type == 0x1b) || (namerec->source == SELF_NAME) || 
-        (namerec->source == PERMANENT_NAME) || 
-        ((namerec->source == WINS_PROXY_NAME) && ((name_type == 0) || (name_type == 0x20)))))
-       )
-    {
-      
-      /* The requested name is a directed query, or it's SELF or PERMANENT or WINS_PROXY, 
-         or it's a Domain Master type. */
-
-      /*
-       * If this is a WINS_PROXY_NAME, then ceck that none of the IP 
-       * addresses we are returning is on the same broadcast subnet 
-       * as the requesting packet. If it is then don't reply as the 
-       * actual machine will be replying also and we don't want two 
-       * replies to a broadcast query.
-       */
-
-      if(namerec->source == WINS_PROXY_NAME)
-      {
-        for( i = 0; i < namerec->num_ips; i++)
-        {
-          if(same_net( namerec->ip[i], subrec->myip, subrec->mask_ip ))    
-          {
-            DEBUG(5,("process_name_query_request: name %s is a WINS proxy name and is also \
-on the same subnet (%s) as the requestor. Not replying.\n", 
-                   namestr(&namerec->name), subrec->subnet_name ));
-            return;
-          }
-        }   
-      }     
-
-      ttl = (namerec->death_time != PERMANENT_TTL) ?
-                     namerec->death_time - p->timestamp : lp_max_ttl();
-
-      /* Copy all known ip addresses into the return data. */
-      /* Optimise for the common case of one IP address so 
-         we don't need a malloc. */
-
-      if(namerec->num_ips == 1 )
-        prdata = rdata;
-      else
-      {
-        if((prdata = (char *)malloc( namerec->num_ips * 6 )) == NULL)
-        {
-          DEBUG(0,("process_name_query_request: malloc fail !\n"));
-          return;
-        }
-      }
-
-      for( i = 0; i < namerec->num_ips; i++)
-      {
-        set_nb_flags(&prdata[i*6],namerec->nb_flags);
-        putip((char *)&prdata[2+(i*6)], &namerec->ip[i]);
-      }
-      reply_data_len = namerec->num_ips * 6;
-      success = True;
-    }
-  }
-
-  /*
-   * If a machine is broadcasting a name lookup request and we have lp_wins_proxy()
-   * set we should initiate a WINS query here. On success we add the resolved name 
-   * into our namelist with a type of WINS_PROXY_NAME and then reply to the query.
-   */
-
-  if(!success && (namerec == NULL) && we_are_a_wins_client() && lp_wins_proxy() && 
-     bcast && (subrec != remote_broadcast_subnet))
-  {
-    make_wins_proxy_name_query_request( subrec, p, question );
-    return;
-  }
-
-  if (!success && bcast)
-  {
-    if((prdata != rdata) && (prdata != NULL))
-      free(rdata);
-    return; /* Never reply with a negative response to broadcasts. */
-  }
-
-  /* 
-   * Final check. From observation, if a unicast packet is sent
-   * to a non-WINS server with the recursion desired bit set
-   * then never send a negative response.
-   */
-
-  if(!success && !bcast && nmb->header.nm_flags.recursion_desired)
-  {
-    if((prdata != rdata) && (prdata != NULL))
-      free(rdata);
-    return;
-  }
-
-  if (success)
-  {
-      rcode = 0;
-      DEBUG(3,("OK\n"));
-  }
-  else
-  {
-      rcode = NAM_ERR;
-      DEBUG(3,("UNKNOWN\n"));      
-  }
-
-  /* See rfc1002.txt 4.2.13. */
-
-  reply_netbios_packet(p,                              /* Packet to reply to. */
-                       rcode,                          /* Result code. */
-                       NMB_QUERY,                      /* nmbd type code. */
-                       NMB_NAME_QUERY_OPCODE,          /* opcode. */
-                       ttl,                            /* ttl. */
-                       prdata,                         /* data to send. */
-                       reply_data_len);                /* data length. */
-
-  if((prdata != rdata) && (prdata != NULL))
-    free(prdata);
+       /* Look up the name in the cache - if the request is a broadcast request that
+          came from a subnet we don't know about then search all the broadcast subnets
+          for a match (as we don't know what interface the request came in on). */
+
+       if(subrec == remote_broadcast_subnet)
+               namerec = find_name_for_remote_broadcast_subnet( question, FIND_ANY_NAME);
+       else
+               namerec = find_name_on_subnet(subrec, question, FIND_ANY_NAME);
+
+       /* Check if it is a name that expired */
+       if (namerec && 
+           ((namerec->data.death_time != PERMANENT_TTL) && 
+            (namerec->data.death_time < p->timestamp))) {
+               DEBUG(5,("process_name_query_request: expired name %s\n", nmb_namestr(&namerec->name)));
+               namerec = NULL;
+       }
+
+       if (namerec) {
+               /* 
+                * Always respond to unicast queries.
+                * Don't respond to broadcast queries unless the query is for
+                * a name we own, a Primary Domain Controller name, or a WINS_PROXY 
+                * name with type 0 or 0x20. WINS_PROXY names are only ever added
+                * into the namelist if we were configured as a WINS proxy.
+                */
+               
+               if (!bcast || 
+                   (bcast && ((name_type == 0x1b) ||
+                              (namerec->data.source == SELF_NAME) ||
+                              (namerec->data.source == PERMANENT_NAME) ||
+                              ((namerec->data.source == WINS_PROXY_NAME) &&
+                               ((name_type == 0) || (name_type == 0x20)))))) {
+                       /* The requested name is a directed query, or it's SELF or PERMANENT or WINS_PROXY, 
+                          or it's a Domain Master type. */
+
+                       /*
+                        * If this is a WINS_PROXY_NAME, then ceck that none of the IP 
+                        * addresses we are returning is on the same broadcast subnet 
+                        * as the requesting packet. If it is then don't reply as the 
+                        * actual machine will be replying also and we don't want two 
+                        * replies to a broadcast query.
+                        */
+                       
+                       if (namerec->data.source == WINS_PROXY_NAME) {
+                               for( i = 0; i < namerec->data.num_ips; i++) {
+                                       if (same_net(namerec->data.ip[i], subrec->myip, subrec->mask_ip)) {
+                                               DEBUG(5,("process_name_query_request: name %s is a WINS proxy name and is also on the same subnet (%s) as the requestor. Not replying.\n", 
+                                                        nmb_namestr(&namerec->name), subrec->subnet_name ));
+                                               return;
+                                       }
+                               }
+                       }
+
+                       ttl = (namerec->data.death_time != PERMANENT_TTL) ?
+                               namerec->data.death_time - p->timestamp : lp_max_ttl();
+
+                       /* Copy all known ip addresses into the return data. */
+                       /* Optimise for the common case of one IP address so 
+                          we don't need a malloc. */
+
+                       if (namerec->data.num_ips == 1) {
+                               prdata = rdata;
+                       } else {
+                               if ((prdata = (char *)malloc( namerec->data.num_ips * 6 )) == NULL) {
+                                       DEBUG(0,("process_name_query_request: malloc fail !\n"));
+                                       return;
+                               }
+                       }
+
+                       for (i = 0; i < namerec->data.num_ips; i++) {
+                               set_nb_flags(&prdata[i*6],namerec->data.nb_flags);
+                               putip((char *)&prdata[2+(i*6)], &namerec->data.ip[i]);
+                       }
+
+                       sort_query_replies(prdata, i, p->ip);
+                       
+                       reply_data_len = namerec->data.num_ips * 6;
+                       success = True;
+               }
+       }
+
+       /*
+        * If a machine is broadcasting a name lookup request and we have lp_wins_proxy()
+        * set we should initiate a WINS query here. On success we add the resolved name 
+        * into our namelist with a type of WINS_PROXY_NAME and then reply to the query.
+        */
+       
+       if(!success && (namerec == NULL) && we_are_a_wins_client() && lp_wins_proxy() && 
+          bcast && (subrec != remote_broadcast_subnet)) {
+               make_wins_proxy_name_query_request( subrec, p, question );
+               return;
+       }
+
+       if (!success && bcast) {
+               if(prdata != rdata)
+                       SAFE_FREE(prdata);
+               return; /* Never reply with a negative response to broadcasts. */
+       }
+
+       /* 
+        * Final check. From observation, if a unicast packet is sent
+        * to a non-WINS server with the recursion desired bit set
+        * then never send a negative response.
+        */
+       
+       if(!success && !bcast && nmb->header.nm_flags.recursion_desired) {
+               if(prdata != rdata)
+                       SAFE_FREE(prdata);
+               return;
+       }
+
+       if (success) {
+               rcode = 0;
+               DEBUG(3,("OK\n"));
+       } else {
+               rcode = NAM_ERR;
+               DEBUG(3,("UNKNOWN\n"));      
+       }
+
+       /* See rfc1002.txt 4.2.13. */
+
+       reply_netbios_packet(p,                              /* Packet to reply to. */
+                            rcode,                          /* Result code. */
+                            NMB_QUERY,                      /* nmbd type code. */
+                            NMB_NAME_QUERY_OPCODE,          /* opcode. */
+                            ttl,                            /* ttl. */
+                            prdata,                         /* data to send. */
+                            reply_data_len);                /* data length. */
+       
+       if(prdata != rdata)
+               SAFE_FREE(prdata);
 }