updated the 3.0 branch from the head branch - ready for alpha18
[nivanova/samba-autobuild/.git] / source3 / smbd / server.c
index 6e9b9a48284813f40f91ce2ddb56f1997cc471d6..6f0d0238b0d907c73d2ae7410dcdb40aa4aca0f4 100644 (file)
@@ -1,8 +1,8 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Main SMB server routines
-   Copyright (C) Andrew Tridgell 1992-1998
+   Copyright (C) Andrew Tridgell               1992-1998
+   Copyright (C) Martin Pool                   2002
    
    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
@@ -21,8 +21,6 @@
 
 #include "includes.h"
 
-pstring servicesf = CONFIGFILE;
-extern pstring debugf;
 extern fstring global_myworkgroup;
 extern pstring global_myname;
 
@@ -34,8 +32,6 @@ int last_message = -1;
 /* a useful macro to debug the last message processed */
 #define LAST_MESSAGE() smb_fn_name(last_message)
 
-extern int DEBUGLEVEL;
-
 extern pstring user_socket_options;
 
 #ifdef WITH_DFS
@@ -57,33 +53,78 @@ int smbd_server_fd(void)
        return server_fd;
 }
 
-void smbd_set_server_fd(int fd)
+static void smbd_set_server_fd(int fd)
 {
        server_fd = fd;
        client_setfd(fd);
 }
 
 /****************************************************************************
-  when exiting, take the whole family
+ Terminate signal.
+****************************************************************************/
+
+SIG_ATOMIC_T got_sig_term = 0;
+
+static void sig_term(void)
+{
+       got_sig_term = 1;
+       sys_select_signal();
+}
+
+/****************************************************************************
+ Catch a sighup.
 ****************************************************************************/
-static void *dflt_sig(void)
+
+SIG_ATOMIC_T reload_after_sighup = 0;
+
+static void sig_hup(int sig)
 {
-       exit_server("caught signal");
-       return NULL;
+       reload_after_sighup = 1;
+       sys_select_signal();
 }
 
 /****************************************************************************
   Send a SIGTERM to our process group.
 *****************************************************************************/
+
 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"));
+}
 
 /****************************************************************************
-  open the socket communication
+ 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 - inetd.
+****************************************************************************/
+
 static BOOL open_sockets_inetd(void)
 {
        /* Started from inetd. fd 0 is the socket. */
@@ -95,15 +136,21 @@ static BOOL open_sockets_inetd(void)
        close_low_fds();
        
        set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
-       set_socket_options(smbd_server_fd(),user_socket_options);
+       set_socket_options(smbd_server_fd(), user_socket_options);
 
        return True;
 }
 
+static void msg_exit_server(int msg_type, pid_t src, void *buf, size_t len)
+{
+       exit_server("Got a SHUTDOWN message");
+}
+
 
 /****************************************************************************
-  open the socket communication
+ Open the socket communication.
 ****************************************************************************/
+
 static BOOL open_sockets(BOOL is_daemon,int port)
 {
        int num_interfaces = iface_count();
@@ -158,7 +205,11 @@ max can be %d\n",
                        s = fd_listenset[i] = open_socket_in(SOCK_STREAM, port, 0, ifip->s_addr, True);
                        if(s == -1)
                                return False;
-                               /* ready to listen */
+
+                       /* 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);
@@ -178,6 +229,9 @@ max can be %d\n",
                        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)));
@@ -189,6 +243,12 @@ max can be %d\n",
                FD_SET(s,&listen_set);
        } 
 
+        /* Listen to messages */
+
+        message_register(MSG_SMB_SAM_SYNC, msg_sam_sync);
+        message_register(MSG_SMB_SAM_REPL, msg_sam_repl);
+        message_register(MSG_SHUTDOWN, msg_exit_server);
+
        /* now accept incoming connections - forking a new process
           for each incoming connection */
        DEBUG(2,("waiting for a connection\n"));
@@ -205,17 +265,19 @@ max can be %d\n",
                memcpy((char *)&lfds, (char *)&listen_set, 
                       sizeof(listen_set));
                
-               num = sys_select(FD_SETSIZE,&lfds,NULL);
+               num = sys_select(FD_SETSIZE,&lfds,NULL,NULL,NULL);
                
                if (num == -1 && errno == EINTR) {
-                       extern VOLATILE sig_atomic_t reload_after_sighup;
+                       if (got_sig_term) {
+                               exit_server("Caught TERM signal");
+                       }
 
                        /* check for sighup processing */
                        if (reload_after_sighup) {
-                               unbecome_user();
+                               change_to_root_user();
                                DEBUG(1,("Reloading services after SIGHUP\n"));
                                reload_services(False);
-                               reload_after_sighup = False;
+                               reload_after_sighup = 0;
                        }
 
                        continue;
@@ -272,6 +334,9 @@ max can be %d\n",
                                   done correctly in the process.  */
                                reset_globals_after_fork();
 
+                               /* tdb needs special fork handling */
+                               tdb_reopen_all();
+
                                return True; 
                        }
                        /* The parent doesn't need this socket */
@@ -310,17 +375,21 @@ max can be %d\n",
 }
 
 /****************************************************************************
-  reload the services file
-  **************************************************************************/
+ Reload the services file.
+**************************************************************************/
+
 BOOL reload_services(BOOL test)
 {
        BOOL ret;
        
+       set_register_printer_fn();
+
        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;
                }
        }
@@ -332,7 +401,7 @@ BOOL reload_services(BOOL test)
 
        lp_killunused(conn_snum_used);
        
-       ret = lp_load(servicesf,False,False,True);
+       ret = lp_load(dyn_CONFIGFILE, False, False, True);
 
        load_printers();
 
@@ -347,39 +416,19 @@ BOOL reload_services(BOOL test)
        {
                if (smbd_server_fd() != -1) {      
                        set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
-                       set_socket_options(smbd_server_fd(),user_socket_options);
+                       set_socket_options(smbd_server_fd(), user_socket_options);
                }
        }
 
-       reset_mangled_cache();
+       mangle_reset_cache();
        reset_stat_cache();
 
        /* this forces service parameters to be flushed */
-       become_service(NULL,True);
+       set_current_service(NULL,True);
 
        return(ret);
 }
 
-
-
-/****************************************************************************
- Catch a sighup.
-****************************************************************************/
-
-VOLATILE sig_atomic_t reload_after_sighup = False;
-
-static void sig_hup(int sig)
-{
-       BlockSignals(True,SIGHUP);
-       DEBUG(0,("Got SIGHUP\n"));
-
-       sys_select_signal();
-       reload_after_sighup = True;
-       BlockSignals(False,SIGHUP);
-}
-
-
-
 #if DUMP_CORE
 /*******************************************************************
 prepare to dump a core file - carefully!
@@ -388,7 +437,8 @@ static BOOL dump_core(void)
 {
        char *p;
        pstring dname;
-       pstrcpy(dname,debugf);
+       
+       pstrcpy(dname,lp_logfile());
        if ((p=strrchr_m(dname,'/'))) *p=0;
        pstrcat(dname,"/corefiles");
        mkdir(dname,0700);
@@ -412,7 +462,7 @@ static BOOL dump_core(void)
 #endif
 
 
-       DEBUG(0,("Dumping core in %s\n",dname));
+       DEBUG(0,("Dumping core in %s\n", dname));
        abort();
        return(True);
 }
@@ -424,37 +474,41 @@ update the current smbd process count
 
 static void decrement_smbd_process_count(void)
 {
-       int total_smbds;
+       int32 total_smbds;
 
        if (lp_max_smbd_processes()) {
                total_smbds = 0;
-               tdb_change_int_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
+               tdb_change_int32_atomic(conn_tdb_ctx(), "INFO/total_smbds", &total_smbds, -1);
        }
 }
 
 /****************************************************************************
-exit the server
+ Exit the server.
 ****************************************************************************/
+
 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. */
-       if (lp_status(-1)) {
-               yield_connection(NULL,"",MAXSTATUS);
-       }
+       yield_connection(NULL,"");
 
        respond_to_all_remaining_local_messages();
        decrement_smbd_process_count();
@@ -479,14 +533,16 @@ void exit_server(char *reason)
        }    
 
        locking_end();
+       printing_end();
 
        DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
        exit(0);
 }
 
 /****************************************************************************
-  initialise connect, service and file structs
+ Initialise connect, service and file structs.
 ****************************************************************************/
+
 static void init_structs(void )
 {
        /*
@@ -496,13 +552,13 @@ static void init_structs(void )
 
        if (!*global_myname) {
                char *p;
-               fstrcpy( global_myname, myhostname() );
-               p = strchr_m( global_myname, '.' );
+               pstrcpy( global_myname, myhostname() );
+               p = strchr_m(global_myname, '.' );
                if (p) 
                        *p = 0;
        }
 
-       strupper( global_myname );
+       strupper(global_myname);
 
        conn_init();
 
@@ -514,44 +570,51 @@ static void init_structs(void )
        init_dptrs();
 
        secrets_init();
+
 }
 
 /****************************************************************************
-usage on the program
+ Usage on the program.
 ****************************************************************************/
+
 static void usage(char *pname)
 {
 
-       printf("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
-       printf("       [-O socket options] [-s services file]\n");
-       printf("\t-D                    Become a daemon\n");
-       printf("\t-a                    Append to log file (default)\n");
-       printf("\t-o                    Overwrite log file, don't append\n");
-       printf("\t-h                    Print usage\n");
-       printf("\t-?                    Print usage\n");
-       printf("\t-V                    Print version\n");
-       printf("\t-d debuglevel         Set the debuglevel\n");
-       printf("\t-l log basename.      Basename for log/debug files\n");
-       printf("\t-p port               Listen on the specified port\n");
-       printf("\t-O socket options     Socket options\n");
-       printf("\t-s services file.     Filename of services file\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
+ main program.
 ****************************************************************************/
+
  int main(int argc,char *argv[])
 {
        extern BOOL append_log;
+       extern BOOL AllowDebugChange;
+       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 opt;
-       extern char *optarg;
-       
+       pstring logfile;
+
 #ifdef HAVE_SET_AUTH_PARAMETERS
        set_auth_parameters(argc,argv);
 #endif
@@ -562,25 +625,30 @@ static void usage(char *pname)
                argc--;
        }
 
-       while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaof:")) )
+       while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?bVaiof:")) )
                switch (opt)  {
                case 'O':
                        pstrcpy(user_socket_options,optarg);
                        break;
 
                case 's':
-                       pstrcpy(servicesf,optarg);
+                       pstrcpy(dyn_CONFIGFILE,optarg);
                        break;
 
                case 'l':
                        specified_logfile = True;
-                       slprintf(debugf, sizeof(debugf)-1, "%s/log.smbd", optarg);
+                       pstr_sprintf(logfile, "%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;
@@ -594,6 +662,7 @@ static void usage(char *pname)
                                DEBUGLEVEL = 10000;
                        else
                                DEBUGLEVEL = atoi(optarg);
+                       AllowDebugChange = False;
                        break;
 
                case 'p':
@@ -607,7 +676,11 @@ static void usage(char *pname)
                        break;
 
                case 'V':
-                       printf("Version %s\n",VERSION);
+                       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:
@@ -623,17 +696,18 @@ static void usage(char *pname)
 
        sec_init();
 
-       append_log = True;
+       load_case_tables();
 
-       TimeInit();
+       append_log = True;
 
        if(!specified_logfile) {
-               slprintf(debugf, sizeof(debugf)-1, "%s/log.smbd", LOGFILEBASE);
+               pstr_sprintf(logfile, "%s/log.smbd", dyn_LOGFILEBASE);
+               lp_set_logfile(logfile);
        }
 
-       pstrcpy(remote_machine, "smbd");
+       fstrcpy(remote_machine, "smbd");
 
-       setup_logging(argv[0],False);
+       setup_logging(argv[0],interactive);
 
        /* we want to re-seed early to prevent time delays causing
            client problems at a later date. (tridge) */
@@ -646,8 +720,9 @@ static void usage(char *pname)
        gain_root_group_privilege();
 
        fault_setup((void (*)(void *))exit_server);
-       CatchSignal(SIGTERM , SIGNAL_CAST dflt_sig);
-
+       CatchSignal(SIGTERM , SIGNAL_CAST sig_term);
+       CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
+       
        /* we are never interested in SIGPIPE */
        BlockSignals(True,SIGPIPE);
 
@@ -665,6 +740,7 @@ static void usage(char *pname)
         * these signals masked, we will have problems, as we won't recieve them. */
        BlockSignals(False, SIGHUP);
        BlockSignals(False, SIGUSR1);
+       BlockSignals(False, SIGTERM);
 
        /* we want total control over the permissions on created files,
           so set our umask to 0 */
@@ -674,12 +750,15 @@ static void usage(char *pname)
 
        reopen_logs();
 
-       DEBUG(1,( "smbd version %s started.\n", VERSION));
-       DEBUGADD(1,( "Copyright Andrew Tridgell 1992-1998\n"));
+       DEBUG(0,( "smbd version %s started.\n", VERSION));
+       DEBUGADD(0,( "Copyright Andrew Tridgell and the Samba Team 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);
@@ -706,31 +785,36 @@ static void usage(char *pname)
        }
 #endif
 
-#ifdef WITH_SSL
-       {
-               extern BOOL sslEnabled;
-               sslEnabled = lp_ssl_enabled();
-               if(sslEnabled)
-                       sslutil_init(True);
-       }
-#endif        /* WITH_SSL */
-
        fstrcpy(global_myworkgroup, lp_workgroup());
 
-       CatchSignal(SIGHUP,SIGNAL_CAST sig_hup);
-       
        DEBUG(3,( "loaded services\n"));
 
        if (!is_daemon && !is_a_socket(0)) {
-               DEBUG(0,("standard input is not a socket, assuming -D option\n"));
+               if (!interactive)
+                       DEBUG(0,("standard input is not a socket, assuming -D option\n"));
+
+               /*
+                * Setting is_daemon here prevents us from eventually calling
+                * the open_sockets_inetd()
+                */
+
                is_daemon = True;
        }
 
-       if (is_daemon) {
+       if (is_daemon && !interactive) {
                DEBUG( 3, ( "Becoming a daemon.\n" ) );
                become_daemon();
        }
 
+#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);
        }
@@ -742,16 +826,18 @@ static void usage(char *pname)
        if (!message_init()) {
                exit(1);
        }
+       register_msg_pool_usage();
+       register_dmalloc_msgs();
 
        /* Setup the main smbd so that we can get messages. */
-       if (lp_status(-1)) {
-               claim_connection(NULL,"",MAXSTATUS,True);
-       }
+       claim_connection(NULL,"",0,True);
 
-       /* Attempt to migrate from an old 2.0.x machine account file. */
-       if (!migrate_from_old_password_file(global_myworkgroup)) {
-               DEBUG(0,("Failed to migrate from old MAC file.\n"));
-       }
+       /* 
+          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);
@@ -760,32 +846,33 @@ static void usage(char *pname)
         * everything after this point is run after the fork()
         */ 
 
-       if (!locking_init(0)) {
+       if (!locking_init(0))
                exit(1);
-       }
 
-       if (!print_backend_init()) {
+       if (!print_backend_init())
                exit(1);
-       }
 
-       if (!share_info_db_init()) {
+       if (!share_info_db_init())
                exit(1);
-       }
 
-       if(!initialize_password_db(False)) {
+       if (!init_registry())
                exit(1);
-       }
 
+       if(!initialize_password_db(False))
+               exit(1);
+
+       uni_group_cache_init(); /* Non-critical */
+       
        /* possibly reload the services file. */
        reload_services(True);
 
-       if (init_group_mapping()==False) {
-               printf("Could not open tdb mapping file.\n");
-               return 0;
+       if(!get_global_sam_sid()) {
+               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
+               exit(1);
        }
 
-       if(!pdb_generate_sam_sid()) {
-               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
+       if (!init_account_policy()) {
+               DEBUG(0,("Could not open account policy tdb.\n"));
                exit(1);
        }
 
@@ -795,17 +882,16 @@ static void usage(char *pname)
        }
 
        /* Setup oplocks */
-       if (!init_oplocks()) {
+       if (!init_oplocks())
                exit(1);
-       }
-
+       
        /* Setup change notify */
-       if (!init_change_notify()) {
+       if (!init_change_notify())
                exit(1);
-       }
 
        smbd_process();
        
+       uni_group_cache_shutdown();
        exit_server("normal exit");
        return(0);
 }