first pass at updating head branch to be to be the same as the SAMBA_2_0 branch
[samba.git] / source3 / smbd / server.c
index bdb2827483eb50735cc56d93bb6a363af9c04ac8..1c6d6536ade0ed5cc4c71ee4438ba64051c3d46e 100644 (file)
@@ -25,9 +25,7 @@
 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;
 
@@ -49,8 +47,6 @@ extern int dcelogin_atmost_once;
 
 extern fstring remote_machine;
 extern pstring OriginalDir;
-extern pstring myhostname;
-
 
 /****************************************************************************
   when exiting, take the whole family
@@ -76,13 +72,11 @@ static void  killkids(void)
 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;
        
        /* close our standard file descriptors */
        close_low_fds();
@@ -93,40 +87,19 @@ static BOOL open_sockets_inetd(void)
        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 +127,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 +143,15 @@ 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 */
+                       if (listen(s, 5) == -1) {
+                               DEBUG(0,("listen: %s\n",strerror(errno)));
+                               close(s);
+                               return False;
+                       }
                        FD_SET(s,&listen_set);
                }
        } else {
@@ -183,18 +160,21 @@ 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 */
+               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
        } 
 
        /* now accept incoming connections - forking a new process
@@ -207,11 +187,14 @@ max can be %d\n",
                memcpy((char *)&lfds, (char *)&listen_set, 
                       sizeof(listen_set));
                
-               num = sys_select(256,&lfds,NULL, NULL);
+               num = sys_select(FD_SETSIZE,&lfds,NULL);
                
                if (num == -1 && errno == EINTR)
                        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--) {
@@ -220,24 +203,15 @@ max can be %d\n",
                        
                        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;
+                               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;
                                }
-#if 0
-                               if(FD_ISSET(fd_listenset[i * 2 + 1],&lfds)) {
-                                       s = fd_listenset[i * 2 + 1];
-                                       ClientPort = SMB_PORT2;
-                                       break;
-                               }
-#endif
                        }
 
-                       /* Clear this so we don't look
-                          at it again. */
-                       FD_CLR(s,&lfds);
-
                        Client = accept(s,&addr,&in_addrlen);
                        
                        if (Client == -1 && errno == EINTR)
@@ -357,9 +331,10 @@ 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)
 {
@@ -434,6 +409,8 @@ void exit_server(char *reason)
 
        conn_close_all();
 
+    respond_to_all_remaining_local_messages();
+
 #ifdef WITH_DFS
        if (dcelogin_atmost_once) {
                dfs_unlogin();
@@ -456,13 +433,6 @@ 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);
 }
 
@@ -471,17 +441,34 @@ void exit_server(char *reason)
 /****************************************************************************
   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( 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");
-       }
+
+       /* for RPC pipes */
+       init_rpc_pipe_hnd();
+
+       /* for LSA handles */
+       init_lsa_policy_hnd();
+
        init_dptrs();
-       init_dfs_table();
 }
 
 /****************************************************************************
@@ -489,19 +476,21 @@ 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("Usage: %s [-DaoPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
+       printf("       [-O socket options] [-s services file] [-i scope]\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-P                    Passive only\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("\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");
 }
@@ -516,7 +505,6 @@ static void usage(char *pname)
        /* shall I run as a daemon */
        BOOL is_daemon = False;
        int port = SMB_PORT;
-       int port445 = SMB_PORT2;
        int opt;
        extern char *optarg;
        
@@ -524,55 +512,13 @@ static void usage(char *pname)
        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:i:l:s:d:Dp:h?VPaof:")) )
                switch (opt)  {
                case 'O':
                        pstrcpy(user_socket_options,optarg);
@@ -626,11 +572,72 @@ static void usage(char *pname)
                        exit(0);
                        break;
 
+               case 'V':
+                       printf("Version %s\n",VERSION);
+                       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
+
+       /*
+        * gain_root_privilege uses an assert than will cause a core
+        * dump if euid != 0. Ensure this is the case.
+        */
+
+       if(geteuid() != (uid_t)0) {
+               fprintf(stderr, "%s: Version %s : Must have effective user id of zero to run.\n", argv[0], VERSION);
+               exit(1);
+       }
+
+       append_log = True;
+
+       TimeInit();
+
+       pstrcpy(debugf,SMBLOGFILE);  
+
+       pstrcpy(remote_machine, "smb");
+
+       setup_logging(argv[0],False);
+
+       charset_initialise();
+
+       /* 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
+
+       /* we want total control over the permissions on created files,
+          so set our umask to 0 */
+       umask(0);
+
+       dos_GetWd(OriginalDir);
+
+       init_uid();
+
        reopen_logs();
 
        DEBUG(1,( "smbd version %s started.\n", VERSION));
@@ -644,13 +651,15 @@ static void usage(char *pname)
                exit(1);
        }
 
-       get_myname(myhostname,NULL);
+       /*
+        * Do this before reload_services.
+        */
 
        if (!reload_services(False))
                return(-1);     
 
        init_structs();
-
+       
 #ifdef WITH_PROFILE
        if (!profile_setup(False)) {
                DEBUG(0,("ERROR: failed to setup profiling\n"));
@@ -658,16 +667,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;
@@ -679,40 +678,10 @@ static void usage(char *pname)
 
        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);
-       }
+       fstrcpy(global_myworkgroup, lp_workgroup());
 
-       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()));
+       if(!pdb_generate_sam_sid()) {
+               DEBUG(0,("ERROR: Samba cannot create a SAM SID.\n"));
                exit(1);
        }
 
@@ -746,7 +715,7 @@ static void usage(char *pname)
                become_daemon();
        }
 
-       check_kernel_oplocks();
+    check_kernel_oplocks();
 
        if (!directory_exist(lp_lockdir(), NULL)) {
                mkdir(lp_lockdir(), 0755);
@@ -756,10 +725,13 @@ static void usage(char *pname)
                pidfile_create("smbd");
        }
 
-       if (!open_sockets(is_daemon,port,port445))
+       if (!locking_init(0))
                exit(1);
 
-       if (!locking_init(0))
+       if (!open_sockets(is_daemon,port))
+               exit(1);
+
+       if(!initialize_password_db())
                exit(1);
 
        /* possibly reload the services file. */