More formatting changes. Mostly converted some DEBUG() calls to DEBUGADD()
[samba.git] / source3 / nmbd / nmbd.c
index 86f01d8e795e0197b4c0d2b4080c2f5519fdf6be..567f7bbca767aab69b92135d46b3315497708c45 100644 (file)
@@ -2,7 +2,7 @@
    Unix SMB/Netbios implementation.
    Version 1.9.
    NBT netbios routines and daemon - version 2
-   Copyright (C) Andrew Tridgell 1994-1997
+   Copyright (C) Andrew Tridgell 1994-1998
    
    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
@@ -40,10 +40,12 @@ int global_nmb_port = -1;
 
 extern pstring myhostname;
 static pstring host_file;
-extern pstring myname;
-extern fstring myworkgroup;
+extern pstring global_myname;
+extern fstring global_myworkgroup;
 extern char **my_netbios_names;
 
+extern BOOL global_in_nmbd;
+
 /* are we running as a daemon ? */
 static BOOL is_daemon = False;
 
@@ -59,7 +61,7 @@ extern struct in_addr ipzero;
 /**************************************************************************** **
   catch a sigterm
  **************************************************************************** */
-static int sig_term()
+static int sig_term(void)
 {
   BlockSignals(True,SIGTERM);
   
@@ -74,12 +76,15 @@ static int sig_term()
   /* Announce all server entries as 0 time-to-live, 0 type. */
   announce_my_servers_removed();
 
+  /* If there was an async dns child - kill it. */
+  kill_async_dns_child();
+
   exit(0);
+
   /* Keep compiler happy.. */
   return 0;
 } /* sig_term */
 
-
 /**************************************************************************** **
  catch a sighup
  **************************************************************************** */
@@ -97,9 +102,7 @@ static int sig_hup(void)
   set_samba_nb_type();
 
   BlockSignals(False,SIGHUP);
-#ifndef DONT_REINSTALL_SIG
-  signal(SIGHUP,SIGNAL_CAST sig_hup);
-#endif
+
   return(0);
 } /* sig_hup */
 
@@ -128,7 +131,7 @@ static BOOL dump_core(void)
   pstrcpy( dname, debugf );
   if ((p=strrchr(dname,'/')))
     *p=0;
-  strcat( dname, "/corefiles" );
+  pstrcat( dname, "/corefiles" );
   mkdir( dname, 0700 );
   sys_chown( dname, getuid(), getgid() );
   chmod( dname, 0700 );
@@ -136,7 +139,7 @@ static BOOL dump_core(void)
     return( False );
   umask( ~(0700) );
 
-#ifndef NO_GETRLIMIT
+#ifdef HAVE_GETRLIMIT
 #ifdef RLIMIT_CORE
   {
     struct rlimit rlp;
@@ -204,7 +207,7 @@ BOOL reload_services(BOOL test)
   BOOL ret;
   extern fstring remote_machine;
 
-  strcpy( remote_machine, "nmbd" );
+  fstrcpy( remote_machine, "nmb" );
 
   if ( lp_loaded() )
   {
@@ -220,7 +223,7 @@ BOOL reload_services(BOOL test)
   if ( test && !lp_file_list_changed() )
     return(True);
 
-  ret = lp_load( servicesf, True );
+  ret = lp_load( servicesf, True , False, False);
 
   /* perhaps the config filename is now set */
   if ( !test )
@@ -384,7 +387,15 @@ static void process(void)
     initiate_wins_processing(t);
 
     /*
-     * Go through the repsonse record queue and time out or re-transmit
+     * If we are a domain master browser, attempt to contact the
+     * WINS server to get a list of all known WORKGROUPS/DOMAINS.
+     * This will only work to a Samba WINS server.
+     * (nmbd_browsesync.c)
+     */
+    collect_all_workgroup_names_from_wins_server(t);
+
+    /*
+     * Go through the response record queue and time out or re-transmit
      * and expired entries.
      * (nmbd_packets.c)
      */
@@ -415,7 +426,7 @@ static BOOL open_sockets(BOOL isdaemon, int port)
   if ( ClientNMB == -1 )
     return( False );
 
-  signal( SIGPIPE, SIGNAL_CAST sig_pipe );
+  CatchSignal( SIGPIPE, SIGNAL_CAST sig_pipe );
 
   set_socket_options( ClientNMB,   "SO_BROADCAST" );
   set_socket_options( ClientDGRAM, "SO_BROADCAST" );
@@ -428,7 +439,7 @@ static BOOL open_sockets(BOOL isdaemon, int port)
 /**************************************************************************** **
  initialise connect, service and file structs
  **************************************************************************** */
-static BOOL init_structs()
+static BOOL init_structs(void)
 {
   extern fstring local_machine;
   char *p, *ptr;
@@ -437,23 +448,23 @@ static BOOL init_structs()
   int nodup;
   pstring nbname;
 
-  if (! *myname)
+  if (! *global_myname)
   {
-    fstrcpy( myname, myhostname );
-    p = strchr( myname, '.' );
+    fstrcpy( global_myname, myhostname );
+    p = strchr( global_myname, '.' );
     if (p)
       *p = 0;
   }
-  strupper( myname );
+  strupper( global_myname );
 
   /* Add any NETBIOS name aliases. Ensure that the first entry
-     is equal to myname.
+     is equal to global_myname.
    */
   /* Work out the max number of netbios aliases that we have */
   ptr = lp_netbios_aliases();
   for( namecount=0; next_token(&ptr,nbname,NULL); namecount++ )
     ;
-  if ( *myname )
+  if ( *global_myname )
     namecount++;
 
   /* Allocate space for the netbios aliases */
@@ -464,10 +475,10 @@ static BOOL init_structs()
      return( False );
   }
  
-  /* Use the myname string first */
+  /* Use the global_myname string first */
   namecount=0;
-  if ( *myname )
-    my_netbios_names[namecount++] = myname;
+  if ( *global_myname )
+    my_netbios_names[namecount++] = global_myname;
   
   ptr = lp_netbios_aliases();
   while ( next_token( &ptr, nbname, NULL ) )
@@ -495,7 +506,7 @@ static BOOL init_structs()
   /* Terminate name list */
   my_netbios_names[namecount++] = NULL;
   
-  fstrcpy( local_machine, myname );
+  fstrcpy( local_machine, global_myname );
   trim_string( local_machine, " ", " " );
   p = strchr( local_machine, ' ' );
   if (p)
@@ -504,7 +515,7 @@ static BOOL init_structs()
 
   DEBUG( 5, ("Netbios name list:-\n") );
   for( n=0; my_netbios_names[n]; n++ )
-    DEBUG( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
+    DEBUGADD( 5, ( "my_netbios_names[%d]=\"%s\"\n", n, my_netbios_names[n] ) );
 
   return( True );
 } /* init_structs */
@@ -538,23 +549,26 @@ int main(int argc,char *argv[])
   int opt;
   extern FILE *dbf;
   extern char *optarg;
-  char pidFile[100] = { 0 };
 
   global_nmb_port = NMB_PORT;
   *host_file = 0;
+  global_in_nmbd = True;
 
   StartupTime = time(NULL);
 
   TimeInit();
 
-  strcpy( debugf, NMBLOGFILE );
+  pstrcpy( debugf, NMBLOGFILE );
 
   setup_logging( argv[0], False );
 
   charset_initialise();
 
+  if(!initialize_password_db())
+    exit(1);
+
 #ifdef LMHOSTSFILE
-  strcpy( host_file, LMHOSTSFILE );
+  pstrcpy( host_file, LMHOSTSFILE );
 #endif
 
   /* this is for people who can't start the program correctly */
@@ -564,18 +578,30 @@ int main(int argc,char *argv[])
     argc--;
   }
 
-  fault_setup( fault_continue );
+  fault_setup((void (*)(void *))fault_continue );
+
+  CatchSignal( SIGHUP,  SIGNAL_CAST sig_hup );
+  CatchSignal( SIGTERM, SIGNAL_CAST sig_term );
+
+  /* Setup the signals that allow the debug log level
+     to by dynamically changed. */
+
+  /* If we are using the malloc debug code we can't use
+     SIGUSR1 and SIGUSR2 to do debug level changes. */
+#ifndef MEM_MAN
+#if defined(SIGUSR1)
+  CatchSignal( SIGUSR1, SIGNAL_CAST sig_usr1 );
+#endif /* SIGUSR1 */
 
-  signal( SIGHUP,  SIGNAL_CAST sig_hup );
-  signal( SIGTERM, SIGNAL_CAST sig_term );
+#if defined(SIGUSR2)
+  CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
+#endif /* SIGUSR2 */
+#endif /* MEM_MAN */
 
   while((opt = getopt(argc, argv, "as:T:I:C:bAi:B:N:Rn:l:d:Dp:hSH:G:f:")) != EOF)
     {
       switch (opt)
         {
-        case 'f':
-          strncpy(pidFile, optarg, sizeof(pidFile));
-          break;
         case 's':
           pstrcpy(servicesf,optarg);
           break;          
@@ -590,11 +616,11 @@ int main(int argc,char *argv[])
           pstrcpy(host_file,optarg);
           break;
         case 'n':
-          pstrcpy(myname,optarg);
-          strupper(myname);
+          pstrcpy(global_myname,optarg);
+          strupper(global_myname);
           break;
         case 'l':
-          sprintf(debugf,"%s.nmb",optarg);
+          slprintf(debugf,sizeof(debugf)-1, "%s.nmb",optarg);
           break;
         case 'i':
           pstrcpy(scope,optarg);
@@ -628,12 +654,14 @@ int main(int argc,char *argv[])
         }
     }
 
-  DEBUG(1,("%s netbios nameserver version %s started\n",timestring(),VERSION));
-  DEBUG(1,("Copyright Andrew Tridgell 1994-1997\n"));
+  reopen_logs();
+
+  DEBUG( 1, ( "Netbios nameserver version %s started.\n", VERSION ) );
+  DEBUGADD( 1, ( "Copyright Andrew Tridgell 1994-1997\n" ) );
 
   if( !get_myname( myhostname, NULL) )
   {
-    DEBUG(0,("Unable to get my hostname - exiting.\n"));
+    DEBUG( 0, ( "Unable to get my hostname - exiting.\n" ) );
     return -1;
   }
 
@@ -647,9 +675,9 @@ int main(int argc,char *argv[])
 
   reload_services( True );
 
-  fstrcpy( myworkgroup, lp_workgroup() );
+  fstrcpy( global_myworkgroup, lp_workgroup() );
 
-  if (strequal(myworkgroup,"*"))
+  if (strequal(global_myworkgroup,"*"))
   {
     DEBUG(0,("ERROR: a workgroup name of * is no longer supported\n"));
     exit(1);
@@ -665,44 +693,15 @@ int main(int argc,char *argv[])
   
   if (is_daemon)
   {
-    DEBUG(2,("%s becoming a daemon\n",timestring()));
+    DEBUG( 2, ( "Becoming a daemon.\n" ) );
     become_daemon();
   }
 
-  if (!directory_exist(lp_lockdir(), NULL))
-  {
-    mkdir(lp_lockdir(), 0755);
-  }
-
-  if (*pidFile)
-  {
-    int     fd;
-    char    buf[20];
-
-#ifdef O_NONBLOCK
-    fd = open( pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_TRUNC, 0644 );
-#else
-    fd = open( pidFile, O_CREAT | O_WRONLY | O_TRUNC, 0644 );
-#endif
-    if ( fd < 0 )
-    {
-      DEBUG(0,("ERROR: can't open %s: %s\n", pidFile, strerror(errno)));
-      exit(1);
-    }
-    if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False)
-    {
-      DEBUG(0,("ERROR: nmbd is already running\n"));
-      exit(1);
-    }
-    sprintf(buf, "%u\n", (unsigned int) getpid());
-    if (write(fd, buf, strlen(buf)) < 0)
-    {
-      DEBUG(0,("ERROR: can't write to %s: %s\n", pidFile, strerror(errno)));
-      exit(1);
-    }
-      /* Leave pid file open & locked for the duration... */
+  if (!directory_exist(lp_lockdir(), NULL)) {
+         mkdir(lp_lockdir(), 0755);
   }
 
+  pidfile_create("nmbd");
 
   DEBUG( 3, ( "Opening sockets %d\n", global_nmb_port ) );
 
@@ -747,8 +746,14 @@ int main(int argc,char *argv[])
     exit(1);
   }
 
-  /* We can only take sigterm signals in the select. */
+  /* We can only take signals in the select. */
   BlockSignals( True, SIGTERM );
+#if defined(SIGUSR1)
+  BlockSignals( True, SIGUSR1);
+#endif /* SIGUSR1 */
+#if defined(SIGUSR2)
+  BlockSignals( True, SIGUSR2);
+#endif /* SIGUSR2 */
 
   process();
   close_sockets();