Add two more memory-debug smbcontrol messages: these ones should
[ira/wip.git] / source3 / smbd / server.c
index a0c9ab7c5656dd2fc85365732c94491fd13c26fb..2f831cdd97a8e856b71097b91b6fa43b8a9e92be 100644 (file)
 */
 
 #include "includes.h"
-#include "trans2.h"
 
-pstring servicesf = CONFIGFILE;
-extern pstring debugf;
 extern fstring global_myworkgroup;
-extern fstring global_sam_name;
 extern pstring global_myname;
-extern dfs_internal dfs_struct;
 
 int am_parent = 1;
 
@@ -37,20 +32,32 @@ int last_message = -1;
 /* a useful macro to debug the last message processed */
 #define LAST_MESSAGE() smb_fn_name(last_message)
 
-extern pstring scope;
-extern int DEBUGLEVEL;
-
 extern pstring user_socket_options;
 
 #ifdef WITH_DFS
 extern int dcelogin_atmost_once;
 #endif /* WITH_DFS */
 
-
 extern fstring remote_machine;
-extern pstring OriginalDir;
-extern pstring myhostname;
 
+/* really we should have a top level context structure that has the
+   client file descriptor as an element. That would require a major rewrite :(
+
+   the following 2 functions are an alternative - they make the file
+   descriptor private to smbd
+ */
+static int server_fd = -1;
+
+int smbd_server_fd(void)
+{
+       return server_fd;
+}
+
+void smbd_set_server_fd(int fd)
+{
+       server_fd = fd;
+       client_setfd(fd);
+}
 
 /****************************************************************************
   when exiting, take the whole family
@@ -69,64 +76,64 @@ static void  killkids(void)
        if(am_parent) kill(0,SIGTERM);
 }
 
+/****************************************************************************
+  process a sam sync message - not sure whether to do this here or
+  somewhere else
+****************************************************************************/
+static void msg_sam_sync(int UNUSED(msg_type), pid_t UNUSED(pid),
+                        void *UNUSED(buf), size_t UNUSED(len))
+{
+        DEBUG(10, ("** sam sync message received, ignoring\n"));
+}
+
+/****************************************************************************
+  process a sam sync replicate message - not sure whether to do this here or
+  somewhere else
+****************************************************************************/
+static void msg_sam_repl(int msg_type, pid_t pid, void *buf, size_t len)
+{
+        uint32 low_serial;
+
+        if (len != sizeof(uint32))
+                return;
+
+        low_serial = *((uint32 *)buf);
+
+        DEBUG(3, ("received sam replication message, serial = 0x%04x\n",
+                  low_serial));
+}
 
 /****************************************************************************
   open the socket communication
 ****************************************************************************/
 static BOOL open_sockets_inetd(void)
 {
-       extern int Client;
-       extern int ClientPort;
-
        /* Started from inetd. fd 0 is the socket. */
        /* We will abort gracefully when the client or remote system 
           goes away */
-       Client = dup(0);
-       ClientPort = SMB_PORT;
+       smbd_set_server_fd(dup(0));
        
        /* close our standard file descriptors */
        close_low_fds();
        
-       set_socket_options(Client,"SO_KEEPALIVE");
-       set_socket_options(Client,user_socket_options);
+       set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
+       set_socket_options(smbd_server_fd(),user_socket_options);
 
        return True;
 }
 
-/****************************************************************************
-  open and listen to a socket
-****************************************************************************/
-static int open_server_socket(int port, uint32 ipaddr)
-{
-       int s;
-
-       s = open_socket_in(SOCK_STREAM, port, 0, ipaddr);
-       if(s == -1)
-               return -1;
-               /* ready to listen */
-       if (listen(s, 5) == -1) {
-               DEBUG(0,("listen: %s\n", strerror(errno)));
-               close(s);
-               return -1;
-       }
-       return s;
-}
 
 /****************************************************************************
   open the socket communication
 ****************************************************************************/
-static BOOL open_sockets(BOOL is_daemon,int port,int port445)
+static BOOL open_sockets(BOOL is_daemon,int port)
 {
-       extern int Client;
-       extern int ClientPort;
        int num_interfaces = iface_count();
        int fd_listenset[FD_SETSIZE];
        fd_set listen_set;
        int s;
        int i;
 
-       memset(&fd_listenset, 0, sizeof(fd_listenset));
-
        if (!is_daemon) {
                return open_sockets_inetd();
        }
@@ -154,7 +161,7 @@ static BOOL open_sockets(BOOL is_daemon,int port,int port445)
                   socket per interface and bind to only these.
                */
                
-               if(num_interfaces * 2 > FD_SETSIZE) {
+               if(num_interfaces > FD_SETSIZE) {
                        DEBUG(0,("open_sockets: Too many interfaces specified to bind to. Number was %d \
 max can be %d\n", 
                                 num_interfaces, FD_SETSIZE));
@@ -170,11 +177,19 @@ max can be %d\n",
                                DEBUG(0,("open_sockets: interface %d has NULL IP address !\n", i));
                                continue;
                        }
-                       s = fd_listenset[i * 2] = open_server_socket(port, ifip->s_addr);
-                       if(s == -1) return False;
-                       FD_SET(s,&listen_set);
-                       s = fd_listenset[i * 2 + 1] = open_server_socket(port445, ifip->s_addr);
-                       if(s == -1) return False;
+                       s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
+                       if(s == -1)
+                               return False;
+
+                       /* ready to listen */
+                       set_socket_options(s,"SO_KEEPALIVE"); 
+                       set_socket_options(s,user_socket_options);
+      
+                       if (listen(s, 5) == -1) {
+                               DEBUG(0,("listen: %s\n",strerror(errno)));
+                               close(s);
+                               return False;
+                       }
                        FD_SET(s,&listen_set);
                }
        } else {
@@ -183,20 +198,31 @@ max can be %d\n",
                num_interfaces = 1;
                
                /* open an incoming socket */
-               s = open_server_socket(port, interpret_addr(lp_socket_address()));
+               s = open_socket_in(SOCK_STREAM, port, 0,
+                                  interpret_addr(lp_socket_address()),True);
                if (s == -1)
                        return(False);
+               
+               /* ready to listen */
+               set_socket_options(s,"SO_KEEPALIVE"); 
+               set_socket_options(s,user_socket_options);
+
+               if (listen(s, 5) == -1) {
+                       DEBUG(0,("open_sockets: listen: %s\n",
+                                strerror(errno)));
+                       close(s);
+                       return False;
+               }
+               
                fd_listenset[0] = s;
                FD_SET(s,&listen_set);
-#if 0
-               s = open_server_socket(port445, interpret_addr(lp_socket_address()));
-               if (s == -1)
-                       return(False);
-               fd_listenset[1] = s;
-               FD_SET(s,&listen_set);
-#endif
        } 
 
+        /* Listen to messages */
+
+        message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
+        message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
+
        /* now accept incoming connections - forking a new process
           for each incoming connection */
        DEBUG(2,("waiting for a connection\n"));
@@ -204,52 +230,63 @@ max can be %d\n",
                fd_set lfds;
                int num;
                
+               /* Free up temporary memory from the main smbd. */
+               lp_talloc_free();
+
+               /* Ensure we respond to PING and DEBUG messages from the main smbd. */
+               message_dispatch();
+
                memcpy((char *)&lfds, (char *)&listen_set, 
                       sizeof(listen_set));
                
-               num = sys_select(256,&lfds,NULL);
+               num = sys_select(FD_SETSIZE,&lfds,NULL);
                
-               if (num == -1 && errno == EINTR)
+               if (num == -1 && errno == EINTR) {
+                       extern VOLATILE sig_atomic_t reload_after_sighup;
+
+                       /* check for sighup processing */
+                       if (reload_after_sighup) {
+                               change_to_root_user();
+                               DEBUG(1,("Reloading services after SIGHUP\n"));
+                               reload_services(False);
+                               reload_after_sighup = False;
+                       }
+
                        continue;
+               }
                
+               /* check if we need to reload services */
+               check_reload(time(NULL));
+
                /* Find the sockets that are read-ready -
                   accept on these. */
                for( ; num > 0; num--) {
                        struct sockaddr addr;
-                       int in_addrlen = sizeof(addr);
+                       socklen_t in_addrlen = sizeof(addr);
                        
                        s = -1;
                        for(i = 0; i < num_interfaces; i++) {
-                               if(FD_ISSET(fd_listenset[i * 2],&lfds)) {
-                                       s = fd_listenset[i * 2];
-                                       ClientPort = SMB_PORT;
-                                       break;
-                               }
-#if 0
-                               if(FD_ISSET(fd_listenset[i * 2 + 1],&lfds)) {
-                                       s = fd_listenset[i * 2 + 1];
-                                       ClientPort = SMB_PORT2;
+                               if(FD_ISSET(fd_listenset[i],&lfds)) {
+                                       s = fd_listenset[i];
+                                       /* Clear this so we don't look
+                                          at it again. */
+                                       FD_CLR(fd_listenset[i],&lfds);
                                        break;
                                }
-#endif
                        }
 
-                       /* Clear this so we don't look
-                          at it again. */
-                       FD_CLR(s,&lfds);
-
-                       Client = accept(s,&addr,&in_addrlen);
+                       smbd_set_server_fd(accept(s,&addr,&in_addrlen));
                        
-                       if (Client == -1 && errno == EINTR)
+                       if (smbd_server_fd() == -1 && errno == EINTR)
                                continue;
                        
-                       if (Client == -1) {
+                       if (smbd_server_fd() == -1) {
                                DEBUG(0,("open_sockets: accept: %s\n",
                                         strerror(errno)));
                                continue;
                        }
                        
-                       if (Client != -1 && fork()==0) {
+                       if (smbd_server_fd() != -1 && sys_fork()==0) {
                                /* Child code ... */
                                
                                /* close the listening socket(s) */
@@ -261,25 +298,31 @@ max can be %d\n",
                                close_low_fds();
                                am_parent = 0;
                                
-                               set_socket_options(Client,"SO_KEEPALIVE");
-                               set_socket_options(Client,user_socket_options);
+                               set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
+                               set_socket_options(smbd_server_fd(),user_socket_options);
                                
                                /* Reset global variables in util.c so
                                   that client substitutions will be
                                   done correctly in the process.  */
                                reset_globals_after_fork();
 
-                /*
-                 * Ensure this child has kernel oplock
-                 * capabilities, but not it's children.
-                 */
-                set_process_capability(KERNEL_OPLOCK_CAPABILITY, True);
-                set_inherited_process_capability(KERNEL_OPLOCK_CAPABILITY, False);
+                               /* tdb needs special fork handling */
+                               tdb_reopen_all();
 
                                return True; 
                        }
                        /* The parent doesn't need this socket */
-                       close(Client); 
+                       close(smbd_server_fd()); 
+
+                       /* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
+                               Clear the closed fd info out of server_fd --
+                               and more importantly, out of client_fd in
+                               util_sock.c, to avoid a possible
+                               getpeername failure if we reopen the logs
+                               and use %I in the filename.
+                       */
+
+                       smbd_set_server_fd(-1);
 
                        /* Force parent to check log size after
                         * spawning child.  Fix from
@@ -309,12 +352,12 @@ max can be %d\n",
 BOOL reload_services(BOOL test)
 {
        BOOL ret;
-
+       
        if (lp_loaded()) {
                pstring fname;
                pstrcpy(fname,lp_configfile());
-               if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
-                       pstrcpy(servicesf,fname);
+               if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
+                       pstrcpy(dyn_CONFIGFILE,fname);
                        test = False;
                }
        }
@@ -325,8 +368,8 @@ BOOL reload_services(BOOL test)
                return(True);
 
        lp_killunused(conn_snum_used);
-
-       ret = lp_load(servicesf,False,False,True);
+       
+       ret = lp_load(dyn_CONFIGFILE,False,False,True);
 
        load_printers();
 
@@ -339,17 +382,17 @@ BOOL reload_services(BOOL test)
        load_interfaces();
 
        {
-               extern int Client;
-               if (Client != -1) {      
-                       set_socket_options(Client,"SO_KEEPALIVE");
-                       set_socket_options(Client,user_socket_options);
+               if (smbd_server_fd() != -1) {      
+                       set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
+                       set_socket_options(smbd_server_fd(),user_socket_options);
                }
        }
 
        reset_mangled_cache();
+       reset_stat_cache();
 
        /* this forces service parameters to be flushed */
-       become_service(NULL,True);
+       set_current_service(NULL,True);
 
        return(ret);
 }
@@ -357,21 +400,17 @@ BOOL reload_services(BOOL test)
 
 
 /****************************************************************************
-this prevents zombie child processes
+ Catch a sighup.
 ****************************************************************************/
-BOOL reload_after_sighup = False;
+
+VOLATILE sig_atomic_t reload_after_sighup = False;
 
 static void sig_hup(int sig)
 {
        BlockSignals(True,SIGHUP);
        DEBUG(0,("Got SIGHUP\n"));
 
-       /*
-        * Fix from <branko.cibej@hermes.si> here.
-        * We used to reload in the signal handler - this
-        * is a *BIG* no-no.
-        */
-
+       sys_select_signal();
        reload_after_sighup = True;
        BlockSignals(False,SIGHUP);
 }
@@ -386,8 +425,8 @@ static BOOL dump_core(void)
 {
        char *p;
        pstring dname;
-       pstrcpy(dname,debugf);
-       if ((p=strrchr(dname,'/'))) *p=0;
+       pstrcpy(dname,lp_logfile());
+       if ((p=strrchr_m(dname,'/'))) *p=0;
        pstrcat(dname,"/corefiles");
        mkdir(dname,0700);
        sys_chown(dname,getuid(),getgid());
@@ -416,6 +455,19 @@ static BOOL dump_core(void)
 }
 #endif
 
+/****************************************************************************
+update the current smbd process count
+****************************************************************************/
+
+static void decrement_smbd_process_count(void)
+{
+       int32 total_smbds;
+
+       if (lp_max_smbd_processes()) {
+               total_smbds = 0;
+               tdb_change_int32_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
+       }
+}
 
 /****************************************************************************
 exit the server
@@ -424,16 +476,29 @@ void exit_server(char *reason)
 {
        static int firsttime=1;
        extern char *last_inbuf;
+       extern struct auth_context *negprot_global_auth_context;
 
-
-       if (!firsttime) exit(0);
+       if (!firsttime)
+               exit(0);
        firsttime = 0;
 
-       unbecome_user();
+       change_to_root_user();
        DEBUG(2,("Closing connections\n"));
 
+       if (negprot_global_auth_context) {
+               (negprot_global_auth_context->free)(&negprot_global_auth_context);
+       }
+
        conn_close_all();
 
+       invalidate_all_vuids();
+
+       /* delete our entry in the connections database. */
+       yield_connection(NULL,"",MAXSTATUS);
+
+       respond_to_all_remaining_local_messages();
+       decrement_smbd_process_count();
+
 #ifdef WITH_DFS
        if (dcelogin_atmost_once) {
                dfs_unlogin();
@@ -456,33 +521,39 @@ void exit_server(char *reason)
        locking_end();
 
        DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
-#ifdef MEM_MAN
-       {
-               extern FILE *dbf;
-               smb_mem_write_verbose(dbf);
-               dbgflush();
-       }
-#endif
        exit(0);
 }
 
-
-
 /****************************************************************************
   initialise connect, service and file structs
 ****************************************************************************/
-static void init_structs(void)
+static void init_structs(void )
 {
+       /*
+        * Set the machine NETBIOS name if not already
+        * set from the config file.
+        */
+
+       if (!*global_myname) {
+               char *p;
+               fstrcpy( global_myname, myhostname() );
+               p = strchr_m( global_myname, '.' );
+               if (p) 
+                       *p = 0;
+       }
+
+       strupper( global_myname );
+
        conn_init();
+
        file_init();
-       init_rpc_pipe_hnd(); /* for RPC pipes */
-       if (!init_policy_hnd(MAX_SERVER_POLICY_HANDLES)) 
-       {
-               exit_server("could not allocate policy handles\n");
-       }
-       init_printer_hnd(); /* for SPOOLSS handles */
+
+       /* for RPC pipes */
+       init_rpc_pipe_hnd();
+
        init_dptrs();
-       init_dfs_table();
+
+       secrets_init();
 }
 
 /****************************************************************************
@@ -490,23 +561,24 @@ usage on the program
 ****************************************************************************/
 static void usage(char *pname)
 {
-       DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
-
-       printf("Usage: %s [-D] [-p port] [-d debuglevel] ", pname);
-        printf("[-l log basename] [-s services file]\n" );
-       printf("Version %s\n",VERSION);
-       printf("\t-D                    become a daemon\n");
-       printf("\t-p port               listen on the specified port\n");
-       printf("\t-d debuglevel         set the debuglevel\n");
-       printf("\t-l log basename.      Basename for log/debug files\n");
-       printf("\t-s services file.     Filename of services file\n");
-       printf("\t-P                    passive only\n");
-       printf("\t-a                    append to log file (default)\n");
-       printf("\t-o                    overwrite log file, don't append\n");
-       printf("\t-i scope              NetBIOS scope to use (default none)\n");
-       printf("\n");
-}
 
+       d_printf("Usage: %s [-DaioPh?Vb] [-d debuglevel] [-l log basename] [-p port]\n", pname);
+       d_printf("       [-O socket options] [-s services file]\n");
+       d_printf("\t-D                    Become a daemon (default)\n");
+       d_printf("\t-a                    Append to log file (default)\n");
+       d_printf("\t-i                    Run interactive (not a daemon)\n" );
+       d_printf("\t-o                    Overwrite log file, don't append\n");
+       d_printf("\t-h                    Print usage\n");
+       d_printf("\t-?                    Print usage\n");
+       d_printf("\t-V                    Print version\n");
+       d_printf("\t-b                    Print build options\n");
+       d_printf("\t-d debuglevel         Set the debuglevel\n");
+       d_printf("\t-l log basename.      Basename for log/debug files\n");
+       d_printf("\t-p port               Listen on the specified port\n");
+       d_printf("\t-O socket options     Socket options\n");
+       d_printf("\t-s services file.     Filename of services file\n");
+       d_printf("\n");
+}
 
 /****************************************************************************
   main program
@@ -514,94 +586,49 @@ static void usage(char *pname)
  int main(int argc,char *argv[])
 {
        extern BOOL append_log;
+       extern char *optarg;
        /* shall I run as a daemon */
        BOOL is_daemon = False;
+       BOOL interactive = False;
+       BOOL specified_logfile = False;
        int port = SMB_PORT;
-       int port445 = SMB_PORT2;
        int opt;
-       extern char *optarg;
-       
+       pstring logfile;
+
 #ifdef HAVE_SET_AUTH_PARAMETERS
        set_auth_parameters(argc,argv);
 #endif
 
-#ifdef HAVE_SETLUID
-       /* needed for SecureWare on SCO */
-       setluid(0);
-#endif
-
-       append_log = True;
-
-       TimeInit();
-
-       pstrcpy(debugf,SMBLOGFILE);  
-
-       pstrcpy(remote_machine, "smb");
-
-       setup_logging(argv[0],False);
-
-       charset_initialise();
-
-       /* make absolutely sure we run as root - to handle cases where people
-          are crazy enough to have it setuid */
-#ifdef HAVE_SETRESUID
-       setresuid(0,0,0);
-#else
-       setuid(0);
-       seteuid(0);
-       setuid(0);
-       seteuid(0);
-#endif
-
-       fault_setup((void (*)(void *))exit_server);
-       CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
-
-       /* we are never interested in SIGPIPE */
-       BlockSignals(True,SIGPIPE);
-
-       /* we want total control over the permissions on created files,
-          so set our umask to 0 */
-       umask(0);
-
-       dos_GetWd(OriginalDir);
-
-       init_uid();
-
        /* this is for people who can't start the program correctly */
        while (argc > 1 && (*argv[1] != '-')) {
                argv++;
                argc--;
        }
 
-       while ( EOF != (opt = getopt(argc, argv, "O:i:l:s:d:Dp:h?Paof:")) )
+       while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?bVaiof:")) )
                switch (opt)  {
                case 'O':
                        pstrcpy(user_socket_options,optarg);
                        break;
 
-               case 'i':
-                       pstrcpy(scope,optarg);
-                       break;
-
-               case 'P':
-                       {
-                               extern BOOL passive;
-                               passive = True;
-                       }
-                       break;  
-
                case 's':
-                       pstrcpy(servicesf,optarg);
+                       pstrcpy(dyn_CONFIGFILE,optarg);
                        break;
 
                case 'l':
-                       pstrcpy(debugf,optarg);
+                       specified_logfile = True;
+                       slprintf(logfile, sizeof(logfile)-1, "%s/log.smbd", optarg);
+                       lp_set_logfile(logfile);
                        break;
 
                case 'a':
                        append_log = True;
                        break;
 
+               case 'i':
+                       interactive = True;
+                       break;
+
                case 'o':
                        append_log = False;
                        break;
@@ -627,31 +654,108 @@ static void usage(char *pname)
                        exit(0);
                        break;
 
+               case 'V':
+                       d_printf("Version %s\n",VERSION);
+                       exit(0);
+                       break;
+               case 'b':
+                       build_options(True); /* Display output to screen as well as debug */ 
+                       exit(0);
+                       break;
                default:
+                       DEBUG(0,("Incorrect program usage - are you sure the command line is correct?\n"));
                        usage(argv[0]);
                        exit(1);
                }
 
+#ifdef HAVE_SETLUID
+       /* needed for SecureWare on SCO */
+       setluid(0);
+#endif
+
+       sec_init();
+
+       load_case_tables();
+
+       append_log = True;
+
+       if(!specified_logfile) {
+               slprintf(logfile, sizeof(logfile)-1, "%s/log.smbd",
+                        dyn_LOGFILEBASE);
+               lp_set_logfile(logfile);
+       }
+
+       pstrcpy(remote_machine, "smbd");
+
+       setup_logging(argv[0],interactive);
+
+       /* we want to re-seed early to prevent time delays causing
+           client problems at a later date. (tridge) */
+       generate_random_buffer(NULL, 0, False);
+
+       /* make absolutely sure we run as root - to handle cases where people
+          are crazy enough to have it setuid */
+
+       gain_root_privilege();
+       gain_root_group_privilege();
+
+       fault_setup((void (*)(void *))exit_server);
+       CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
+
+       /* we are never interested in SIGPIPE */
+       BlockSignals(True,SIGPIPE);
+
+#if defined(SIGFPE)
+       /* we are never interested in SIGFPE */
+       BlockSignals(True,SIGFPE);
+#endif
+
+#if defined(SIGUSR2)
+       /* We are no longer interested in USR2 */
+       BlockSignals(True,SIGUSR2);
+#endif
+
+       /* POSIX demands that signals are inherited. If the invoking process has
+        * these signals masked, we will have problems, as we won't recieve them. */
+       BlockSignals(False, SIGHUP);
+       BlockSignals(False, SIGUSR1);
+
+       /* we want total control over the permissions on created files,
+          so set our umask to 0 */
+       umask(0);
+
+       init_sec_ctx();
+
        reopen_logs();
 
        DEBUG(1,( "smbd version %s started.\n", VERSION));
-       DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
+       DEBUGADD(1,( "Copyright Andrew Tridgell 1992-2002\n"));
 
        DEBUG(2,("uid=%d gid=%d euid=%d egid=%d\n",
                 (int)getuid(),(int)getgid(),(int)geteuid(),(int)getegid()));
 
+       /* Output the build options to the debug log */ 
+       build_options(False);
+
        if (sizeof(uint16) < 2 || sizeof(uint32) < 4) {
                DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
                exit(1);
        }
 
-       get_myname(myhostname,NULL);
+       /*
+        * Do this before reload_services.
+        */
 
        if (!reload_services(False))
                return(-1);     
 
        init_structs();
 
+       /* don't call winbind for our domain if we are the DC */
+       if (lp_domain_logons()) {
+               winbind_exclude_domain(lp_workgroup());
+       }
+       
 #ifdef WITH_PROFILE
        if (!profile_setup(False)) {
                DEBUG(0,("ERROR: failed to setup profiling\n"));
@@ -659,16 +763,6 @@ static void usage(char *pname)
        }
 #endif
 
-       /*
-        * Set the machine NETBIOS name if not already
-        * set from the config file.
-        */
-       if (!*global_myname)
-       {
-               fstrcpy(global_myname, dns_to_netbios_name(myhostname));
-       }
-       strupper(global_myname);
-
 #ifdef WITH_SSL
        {
                extern BOOL sslEnabled;
@@ -678,63 +772,10 @@ static void usage(char *pname)
        }
 #endif        /* WITH_SSL */
 
-       codepage_initialise(lp_client_code_page());
-
-       if (!pwdb_initialise(True))
-       {
-               exit(1);
-       }
-
-       if(!initialise_sam_password_db())
-       {
-               exit(1);
-       }
-
-       if(!initialise_passgrp_db())
-       {
-               exit(1);
-       }
-
-       if(!initialise_group_db())
-       {
-               exit(1);
-       }
-
-       if(!initialise_alias_db())
-       {
-               exit(1);
-       }
-
-       if(!initialise_builtin_db())
-       {
-               exit(1);
-       }
-
-       if (!get_member_domain_sid())
-       {
-               DEBUG(0,("ERROR: Samba cannot obtain PDC SID from PDC(s) %s.\n",
-                         lp_passwordserver()));
-               exit(1);
-       }
+       fstrcpy(global_myworkgroup, lp_workgroup());
 
        CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
        
-       /* 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 */
-   
-#if defined(SIGUSR2)
-       CatchSignal( SIGUSR2, SIGNAL_CAST sig_usr2 );
-#endif /* SIGUSR2 */
-#endif /* MEM_MAN */
-
        DEBUG(3,( "loaded services\n"));
 
        if (!is_daemon && !is_a_socket(0)) {
@@ -742,12 +783,19 @@ static void usage(char *pname)
                is_daemon = True;
        }
 
-       if (is_daemon) {
+       if (is_daemon && !interactive) {
                DEBUG( 3, ( "Becoming a daemon.\n" ) );
                become_daemon();
        }
 
-       check_kernel_oplocks();
+#if HAVE_SETPGID
+       /*
+        * If we're interactive we want to set our own process group for
+        * signal management.
+        */
+       if (interactive)
+               setpgid( (pid_t)0, (pid_t)0);
+#endif
 
        if (!directory_exist(lp_lockdir(), NULL)) {
                mkdir(lp_lockdir(), 0755);
@@ -757,26 +805,84 @@ static void usage(char *pname)
                pidfile_create("smbd");
        }
 
-       if (!open_sockets(is_daemon,port,port445))
+       if (!message_init()) {
                exit(1);
+       }
+       register_msg_pool_usage();
+       register_dmalloc_msgs();
+
+       /* Setup the main smbd so that we can get messages. */
+       claim_connection(NULL,"",MAXSTATUS,True);
 
-       if (!locking_init(0))
+       /* 
+          DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
+          THIS *killed* LOTS OF BUILD FARM MACHINES. IT CREATED HUNDREDS OF 
+          smbd PROCESSES THAT NEVER DIE
+          start_background_queue(); 
+       */
+
+       if (!open_sockets(is_daemon,port))
+               exit(1);
+
+       /*
+        * everything after this point is run after the fork()
+        */ 
+
+       if (!locking_init(0)) {
+               exit(1);
+       }
+
+       if (!print_backend_init()) {
                exit(1);
+       }
+
+       if (!share_info_db_init()) {
+               exit(1);
+       }
+
+       if(!initialize_password_db(False)) {
+               exit(1);
+       }
 
        /* possibly reload the services file. */
        reload_services(True);
-       
+
+       if(!pdb_generate_sam_sid()) {
+               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
+               exit(1);
+       }
+
+       if (!init_group_mapping()) {
+               DEBUG(0,("Could not open tdb mapping file.\n"));
+               return 0;
+       }
+
+       if (!init_account_policy()) {
+               DEBUG(0,("Could not open account policy tdb.\n"));
+               exit(1);
+       }
+
        if (*lp_rootdir()) {
                if (sys_chroot(lp_rootdir()) == 0)
                        DEBUG(2,("Changed root to %s\n", lp_rootdir()));
        }
 
-       /* Setup the oplock IPC socket. */
-       if( !open_oplock_ipc() )
+       /* Setup oplocks */
+       if (!init_oplocks()) {
+               exit(1);
+       }
+       
+       /* Setup mangle */
+       if (!init_mangle_tdb()) {
+               exit(1);
+       }
+
+       /* Setup change notify */
+       if (!init_change_notify()) {
                exit(1);
+       }
 
        smbd_process();
-       close_sockets();
        
        exit_server("normal exit");
        return(0);