Fix the last reported debian problem with nmbd not waiting
[ira/wip.git] / source / nmbd / nmbd_subnetdb.c
index 40ae1db1b3580590cabc0a95ab8a2c573186f692..a4422d27d573541490c721c8f97e4ca044ceb776 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
@@ -8,7 +7,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    Revision History:
 
 */
 
 #include "includes.h"
-#include "smb.h"
 
-extern int ClientNMB;
-extern int ClientDGRAM;
 extern int global_nmb_port;
 
-extern int DEBUGLEVEL;
-
-extern fstring myworkgroup;
-extern char **my_netbios_names;
-extern struct in_addr ipzero;
-
 /* This is the broadcast subnets database. */
 struct subnet_record *subnetlist = NULL;
 
@@ -55,253 +44,308 @@ extern uint16 samba_nb_type; /* Samba's NetBIOS name type. */
 
 static void add_subnet(struct subnet_record *subrec)
 {
-  struct subnet_record *subrec2;
-
-  if (!subnetlist)
-  {
-    subnetlist = subrec;
-    subrec->prev = NULL;
-    subrec->next = NULL;
-    return;
-  }
-
-  for (subrec2 = subnetlist; subrec2->next; subrec2 = subrec2->next)
-    ;
-
-  subrec2->next = subrec;
-  subrec->next = NULL;
-  subrec->prev = subrec2;
+       DLIST_ADD(subnetlist, subrec);
 }
 
-/* CRH!!! */
-/* ************************************************************************** ** * This will go away when we move to a "real" database back-end.
-  Note that we cannot use memcmp here as we have no control
-  over how the struct nmb_name structures are packed in memory. JRA.
- * ************************************************************************** ** */
-int namelist_entry_compare( ubi_trItemPtr Item, ubi_trNodePtr Node )
-  {
-  struct name_record *NR = (struct name_record *)Node;
-  struct nmb_name *nmbname = (struct nmb_name *)Item;
-  int ret;
-
-  if(nmb_name_equal( &NR->name, nmbname))
-    return 0;
-
-  ret = strcmp( nmbname->name, NR->name.name);
-  if(ret)
-    return ret;
-
-  ret = strcmp( nmbname->scope, NR->name.scope);
-  if(ret)
-    return ret;
+/****************************************************************************
+stop listening on a subnet
+we don't free the record as we don't have proper reference counting for it
+yet and it may be in use by a response record
+  ****************************************************************************/
 
-  return nmbname->name_type - NR->name.name_type;
-  } /* namelist_entry_compare */
-/* CRH!!! */   
+void close_subnet(struct subnet_record *subrec)
+{
+       if (subrec->dgram_sock != -1) {
+               close(subrec->dgram_sock);
+               subrec->dgram_sock = -1;
+       }
+       if (subrec->nmb_sock != -1) {
+               close(subrec->nmb_sock);
+               subrec->nmb_sock = -1;
+       }
+
+       DLIST_REMOVE(subnetlist, subrec);
+}
 
 /****************************************************************************
   Create a subnet entry.
   ****************************************************************************/
 
-static struct subnet_record *make_subnet(char *name, enum subnet_type type,
-                                         struct in_addr myip, struct in_addr bcast_ip, 
-                                         struct in_addr mask_ip)
+static struct subnet_record *make_subnet(const char *name, enum subnet_type type,
+                                        struct in_addr myip, struct in_addr bcast_ip, 
+                                        struct in_addr mask_ip)
 {
-  struct subnet_record *subrec = NULL;
-  int nmb_sock, dgram_sock;
-
-  /* Check if we are creating a non broadcast subnet - if so don't create
-     sockets.
-   */
-
-  if(type != NORMAL_SUBNET)
-  {
-    nmb_sock = -1;
-    dgram_sock = -1;
-  }
-  else
-  {
-    /*
-     * Attempt to open the sockets on port 137/138 for this interface
-     * and bind them.
-     * Fail the subnet creation if this fails.
-     */
-
-    if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, myip.s_addr)) == -1)
-    {
-      DEBUG(0,("make_subnet: Failed to open nmb socket on interface %s \
-for port %d. Error was %s\n", inet_ntoa(myip), global_nmb_port, strerror(errno)));
-      return NULL;
-    }
-
-    if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, myip.s_addr)) == -1)
-    {
-      DEBUG(0,("make_subnet: Failed to open dgram socket on interface %s \
-for port %d. Error was %s\n", inet_ntoa(myip), DGRAM_PORT, strerror(errno)));
-      return NULL;
-    }
-
-    /* Make sure we can broadcast from these sockets. */
-    set_socket_options(nmb_sock,"SO_BROADCAST");
-    set_socket_options(dgram_sock,"SO_BROADCAST");
-
-  }
-
-  subrec = (struct subnet_record *)malloc(sizeof(*subrec));
-  
-  if (!subrec) 
-  {
-    DEBUG(0,("make_subnet: malloc fail !\n"));
-    close(nmb_sock);
-    close(dgram_sock);
-    return(NULL);
-  }
+       struct subnet_record *subrec = NULL;
+       int nmb_sock, dgram_sock;
+
+       /* Check if we are creating a non broadcast subnet - if so don't create
+               sockets.  */
+
+       if(type != NORMAL_SUBNET) {
+               nmb_sock = -1;
+               dgram_sock = -1;
+       } else {
+               struct sockaddr_storage ss;
+
+               in_addr_to_sockaddr_storage(&ss, myip);
+
+               /*
+                * Attempt to open the sockets on port 137/138 for this interface
+                * and bind them.
+                * Fail the subnet creation if this fails.
+                */
+
+               if((nmb_sock = open_socket_in(SOCK_DGRAM, global_nmb_port,0, &ss,true)) == -1) {
+                       if( DEBUGLVL( 0 ) ) {
+                               Debug1( "nmbd_subnetdb:make_subnet()\n" );
+                               Debug1( "  Failed to open nmb socket on interface %s ", inet_ntoa(myip) );
+                               Debug1( "for port %d.  ", global_nmb_port );
+                               Debug1( "Error was %s\n", strerror(errno) );
+                       }
+                       return NULL;
+               }
+
+               if((dgram_sock = open_socket_in(SOCK_DGRAM,DGRAM_PORT,3, &ss, true)) == -1) {
+                       if( DEBUGLVL( 0 ) ) {
+                               Debug1( "nmbd_subnetdb:make_subnet()\n" );
+                               Debug1( "  Failed to open dgram socket on interface %s ", inet_ntoa(myip) );
+                               Debug1( "for port %d.  ", DGRAM_PORT );
+                               Debug1( "Error was %s\n", strerror(errno) );
+                       }
+                       return NULL;
+               }
+
+               /* Make sure we can broadcast from these sockets. */
+               set_socket_options(nmb_sock,"SO_BROADCAST");
+               set_socket_options(dgram_sock,"SO_BROADCAST");
+
+               /* Set them non-blocking. */
+               set_blocking(nmb_sock, False);
+               set_blocking(dgram_sock, False);
+       }
+
+       subrec = SMB_MALLOC_P(struct subnet_record);
+       if (!subrec) {
+               DEBUG(0,("make_subnet: malloc fail !\n"));
+               if (nmb_sock != -1) {
+                       close(nmb_sock);
+               }
+               if (dgram_sock != -1) {
+                       close(dgram_sock);
+               }
+               return(NULL);
+       }
   
-  bzero( (char *)subrec, sizeof(*subrec) );
-  (void)ubi_trInitTree( subrec->namelist,
-                        namelist_entry_compare,
-                        ubi_trOVERWRITE );
-
-  if((subrec->subnet_name = strdup(name)) == NULL)
-  {
-    DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
-    close(nmb_sock);
-    close(dgram_sock);
-    free((char *)subrec);
-    return(NULL);
-  }
-
-  DEBUG(2, ("making subnet name:%s ", name ));
-  DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
-  DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
+       ZERO_STRUCTP(subrec);
+
+       if((subrec->subnet_name = SMB_STRDUP(name)) == NULL) {
+               DEBUG(0,("make_subnet: malloc fail for subnet name !\n"));
+               if (nmb_sock != -1) {
+                       close(nmb_sock);
+               }
+               if (dgram_sock != -1) {
+                       close(dgram_sock);
+               }
+               ZERO_STRUCTP(subrec);
+               SAFE_FREE(subrec);
+               return(NULL);
+       }
+
+       DEBUG(2, ("making subnet name:%s ", name ));
+       DEBUG(2, ("Broadcast address:%s ", inet_ntoa(bcast_ip)));
+       DEBUG(2, ("Subnet mask:%s\n", inet_ntoa(mask_ip)));
  
-  subrec->namelist_changed = False;
-  subrec->work_changed = False;
+       subrec->namelist_changed = False;
+       subrec->work_changed = False;
  
-  subrec->bcast_ip = bcast_ip;
-  subrec->mask_ip  = mask_ip;
-  subrec->myip = myip;
-  subrec->type = type;
-  subrec->nmb_sock = nmb_sock;
-  subrec->dgram_sock = dgram_sock;
+       subrec->bcast_ip = bcast_ip;
+       subrec->mask_ip  = mask_ip;
+       subrec->myip = myip;
+       subrec->type = type;
+       subrec->nmb_sock = nmb_sock;
+       subrec->dgram_sock = dgram_sock;
   
-  return subrec;
+       return subrec;
+}
+
+/****************************************************************************
+  Create a normal subnet
+**************************************************************************/
+
+struct subnet_record *make_normal_subnet(const struct interface *iface)
+{
+
+       struct subnet_record *subrec;
+       const struct in_addr *pip = &((const struct sockaddr_in *)&iface->ip)->sin_addr;
+       const struct in_addr *pbcast = &((const struct sockaddr_in *)&iface->bcast)->sin_addr;
+       const struct in_addr *pnmask = &((const struct sockaddr_in *)&iface->netmask)->sin_addr;
+
+       subrec = make_subnet(inet_ntoa(*pip), NORMAL_SUBNET,
+                            *pip, *pbcast, *pnmask);
+       if (subrec) {
+               add_subnet(subrec);
+       }
+       return subrec;
 }
 
 /****************************************************************************
   Create subnet entries.
 **************************************************************************/
 
-BOOL create_subnets(void)
-{    
-  int num_interfaces = iface_count();
-  int i;
-  struct in_addr unicast_ip;
-
-  if(num_interfaces == 0)
-  {
-    DEBUG(0,("create_subnets: No local interfaces !\n"));
-    return False;
-  }
-
-  /* 
-   * Create subnets from all the local interfaces and thread them onto
-   * the linked list. 
-   */
-
-  for (i = 0 ; i < num_interfaces; i++)
-  {
-    struct subnet_record *subrec;
-    struct interface *iface = get_interface(i);
-
-    if((subrec = make_subnet(inet_ntoa(iface->ip), NORMAL_SUBNET,
-                 iface->ip, iface->bcast,iface->nmask)) == NULL)
-      return False;
-    add_subnet(subrec);
-  }
-
-  /* 
-   * If we have been configured to use a WINS server, then try and
-   * get the ip address of it here. If we are the WINS server then
-   * set the unicast subnet address to be the first of our own real
-   * addresses.
-   */
-
-  if(*lp_wins_server())
-  {
-    struct in_addr real_wins_ip;
-    real_wins_ip = *interpret_addr2(lp_wins_server());
-
-    if (!zero_ip(real_wins_ip))
-    {
-      unicast_ip = real_wins_ip;
-    }
-    else
-    {
-      /* The smb.conf's wins server parameter MUST be a host_name
-         or an ip_address. */
-      DEBUG(0,("invalid smb.conf parameter 'wins server'\n"));
-      return False;
-    }
-  } 
-  else if(lp_we_are_a_wins_server())
-  {
-    /* Pick the first interface ip address as the WINS server ip. */
-    unicast_ip = *iface_n_ip(0);
-  }
-  else
-  {
-    /* We should not be using a WINS server at all. Set the
-      ip address of the subnet to be zero. */
-    unicast_ip = ipzero;
-  }
-
-  /*
-   * Create the unicast and remote broadcast subnets.
-   * Don't put these onto the linked list.
-   * The ip address of the unicast subnet is set to be
-   * the WINS server address, if it exists, or ipzero if not.
-   */
-
-  unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
-                                 unicast_ip, unicast_ip, unicast_ip);
-
-  remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
-                                         REMOTE_BROADCAST_SUBNET,
-                                         ipzero, ipzero, ipzero);
-
-  if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
-    return False;
-
-  /* 
-   * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
-   * the linked list.
-   */
-
-  if (lp_we_are_a_wins_server())
-  {
-    if((wins_server_subnet = make_subnet("WINS_SERVER_SUBNET",
-                                       WINS_SERVER_SUBNET, 
-                                       ipzero, ipzero, ipzero)) == NULL)
-      return False;
-  }
-
-  return True;
+bool create_subnets(void)
+{
+       /* We only count IPv4 interfaces whilst we're waiting. */
+       int num_interfaces = iface_count_v4();
+       int i;
+       struct in_addr unicast_ip, ipzero;
+
+  try_interfaces_again:
+
+       if (iface_count_v4() == 0) {
+               DEBUG(0,("create_subnets: No local interfaces !\n"));
+               DEBUG(0,("create_subnets: Waiting for an interface to appear ...\n"));
+       }
+
+       /* We only count IPv4 interfaces here. */
+       while (iface_count_v4() == 0) {
+               void (*saved_handler)(int);
+
+               /*
+                * Whilst we're waiting for an interface, allow SIGTERM to
+                * cause us to exit.
+                */
+
+               saved_handler = CatchSignal( SIGTERM, SIGNAL_CAST SIG_DFL );
+
+               sleep(5);
+               load_interfaces();
+
+               /*
+                * We got an interface, restore our normal term handler.
+                */
+
+               CatchSignal( SIGTERM, SIGNAL_CAST saved_handler );
+       }
+
+       /*
+        * Here we count v4 and v6 - we know there's at least one
+        * IPv4 interface and we filter on it below.
+        */
+       num_interfaces = iface_count();
+
+       /*
+        * Create subnets from all the local interfaces and thread them onto
+        * the linked list.
+        */
+
+       for (i = 0 ; i < num_interfaces; i++) {
+               const struct interface *iface = get_interface(i);
+
+               if (!iface) {
+                       DEBUG(2,("create_subnets: can't get interface %d.\n", i ));
+                       continue;
+               }
+
+               /* Ensure we're only dealing with IPv4 here. */
+               if (iface->ip.ss_family != AF_INET) {
+                       DEBUG(2,("create_subnets: "
+                               "ignoring non IPv4 interface.\n"));
+                       continue;
+               }
+
+               /*
+                * We don't want to add a loopback interface, in case
+                * someone has added 127.0.0.1 for smbd, nmbd needs to
+                * ignore it here. JRA.
+                */
+
+               if (is_loopback_addr(&iface->ip)) {
+                       DEBUG(2,("create_subnets: Ignoring loopback interface.\n" ));
+                       continue;
+               }
+
+               if (!make_normal_subnet(iface))
+                       return False;
+       }
+
+        /* We must have at least one subnet. */
+       if (subnetlist == NULL) {
+               void (*saved_handler)(int);
+
+               DEBUG(0,("create_subnets: Unable to create any subnet from "
+                               "given interfaces. Is your interface line in "
+                               "smb.conf correct ?\n"));
+
+               saved_handler = CatchSignal( SIGTERM, SIGNAL_CAST SIG_DFL );
+
+               sleep(5);
+               load_interfaces();
+
+               CatchSignal( SIGTERM, SIGNAL_CAST saved_handler );
+               goto try_interfaces_again;
+       }
+
+       if (lp_we_are_a_wins_server()) {
+               /* Pick the first interface IPv4 address as the WINS server ip. */
+               const struct in_addr *nip = first_ipv4_iface();
+
+               if (!nip) {
+                       return False;
+               }
+
+               unicast_ip = *nip;
+       } else {
+               /* note that we do not set the wins server IP here. We just
+                       set it at zero and let the wins registration code cope
+                       with getting the IPs right for each packet */
+               zero_ip_v4(&unicast_ip);
+       }
+
+       /*
+        * Create the unicast and remote broadcast subnets.
+        * Don't put these onto the linked list.
+        * The ip address of the unicast subnet is set to be
+        * the WINS server address, if it exists, or ipzero if not.
+        */
+
+       unicast_subnet = make_subnet( "UNICAST_SUBNET", UNICAST_SUBNET, 
+                               unicast_ip, unicast_ip, unicast_ip);
+
+       zero_ip_v4(&ipzero);
+
+       remote_broadcast_subnet = make_subnet( "REMOTE_BROADCAST_SUBNET",
+                               REMOTE_BROADCAST_SUBNET,
+                               ipzero, ipzero, ipzero);
+
+       if((unicast_subnet == NULL) || (remote_broadcast_subnet == NULL))
+               return False;
+
+       /* 
+        * If we are WINS server, create the WINS_SERVER_SUBNET - don't put on
+        * the linked list.
+        */
+
+       if (lp_we_are_a_wins_server()) {
+               if( (wins_server_subnet = make_subnet( "WINS_SERVER_SUBNET",
+                                               WINS_SERVER_SUBNET, 
+                                               ipzero, ipzero, ipzero )) == NULL )
+                       return False;
+       }
+
+       return True;
 }
 
 /*******************************************************************
 Function to tell us if we can use the unicast subnet.
 ******************************************************************/
 
-BOOL we_are_a_wins_client(void)
+bool we_are_a_wins_client(void)
 {
-  static int cache_we_are_a_wins_client = -1;
-
-  if(cache_we_are_a_wins_client == -1)
-    cache_we_are_a_wins_client = (ip_equal(ipzero, unicast_subnet->myip) ? 
-                                  False : True);
+       if (wins_srv_count() > 0) {
+               return True;
+       }
 
-  return cache_we_are_a_wins_client;
+       return False;
 }
 
 /*******************************************************************
@@ -310,12 +354,12 @@ Access function used by NEXT_SUBNET_INCLUDING_UNICAST
 
 struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec)
 {
-  if(subrec == unicast_subnet)
-    return NULL;
-  else if((subrec->next == NULL) && we_are_a_wins_client())
-    return unicast_subnet;
-  else
-    return subrec->next;
+       if(subrec == unicast_subnet)
+               return NULL;
+       else if((subrec->next == NULL) && we_are_a_wins_client())
+               return unicast_subnet;
+       else
+               return subrec->next;
 }
 
 /*******************************************************************
@@ -327,19 +371,18 @@ struct subnet_record *get_next_subnet_maybe_unicast(struct subnet_record *subrec
 
 struct subnet_record *get_next_subnet_maybe_unicast_or_wins_server(struct subnet_record *subrec)
 {
-  if(subrec == unicast_subnet)
-  {
-    if(wins_server_subnet)
-      return wins_server_subnet;
-    else
-      return NULL;
-  }
-
-  if(wins_server_subnet && subrec == wins_server_subnet)
-    return NULL;
-
-  if((subrec->next == NULL) && we_are_a_wins_client())
-    return unicast_subnet;
-  else
-    return subrec->next;
+       if(subrec == unicast_subnet) {
+               if(wins_server_subnet)
+                       return wins_server_subnet;
+               else
+                       return NULL;
+       }
+
+       if(wins_server_subnet && subrec == wins_server_subnet)
+               return NULL;
+
+       if((subrec->next == NULL) && we_are_a_wins_client())
+               return unicast_subnet;
+       else
+               return subrec->next;
 }