s3: nmbd: Fix strict overflow checking compiler warning.
[samba.git] / source3 / nmbd / nmbd.c
index bb264d2274009f8dd46fe069435707683d5b7f9f..9be708a9c0293203f75e0f4199635ef0cee0ceeb 100644 (file)
@@ -25,6 +25,8 @@
 #include "nmbd/nmbd.h"
 #include "serverid.h"
 #include "messages.h"
+#include "../lib/util/pidfile.h"
+#include "util_cluster.h"
 
 int ClientNMB       = -1;
 int ClientDGRAM     = -1;
@@ -42,7 +44,7 @@ bool found_lm_clients = False;
 
 time_t StartupTime = 0;
 
-struct event_context *nmbd_event_context(void)
+struct tevent_context *nmbd_event_context(void)
 {
        return server_event_context();
 }
@@ -68,9 +70,8 @@ static void terminate(struct messaging_context *msg)
        kill_async_dns_child();
 
        gencache_stabilize();
-       serverid_deregister(messaging_server_id(msg));
 
-       pidfile_unlink();
+       pidfile_unlink(lp_pid_directory(), "nmbd");
 
        exit(0);
 }
@@ -129,8 +130,20 @@ static bool nmbd_setup_stdin_handler(struct messaging_context *msg, bool foregro
                /* if we are running in the foreground then look for
                   EOF on stdin, and exit if it happens. This allows
                   us to die if the parent process dies
+                  Only do this on a pipe or socket, no other device.
                */
-               tevent_add_fd(nmbd_event_context(), nmbd_event_context(), 0, TEVENT_FD_READ, nmbd_stdin_handler, msg);
+               struct stat st;
+               if (fstat(0, &st) != 0) {
+                       return false;
+               }
+               if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
+                       tevent_add_fd(nmbd_event_context(),
+                               nmbd_event_context(),
+                               0,
+                               TEVENT_FD_READ,
+                               nmbd_stdin_handler,
+                               msg);
+               }
        }
 
        return true;
@@ -382,11 +395,12 @@ static bool reload_nmbd_services(bool test)
        set_remote_machine_name("nmbd", False);
 
        if ( lp_loaded() ) {
-               const char *fname = lp_configfile();
+               char *fname = lp_next_configfile(talloc_tos());
                if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
                        set_dyn_CONFIGFILE(fname);
                        test = False;
                }
+               TALLOC_FREE(fname);
        }
 
        if ( test && !lp_file_list_changed() )
@@ -400,6 +414,8 @@ static bool reload_nmbd_services(bool test)
                reload_nmbd_services( True );
        }
 
+       reopen_logs();
+
        return(ret);
 }
 
@@ -690,7 +706,7 @@ static void process(struct messaging_context *msg)
 static bool open_sockets(bool isdaemon, int port)
 {
        struct sockaddr_storage ss;
-       const char *sock_addr = lp_socket_address();
+       const char *sock_addr = lp_nbt_client_socket_address();
 
        /*
         * The sockets opened here will be used to receive broadcast
@@ -786,6 +802,7 @@ static bool open_sockets(bool isdaemon, int port)
        };
        TALLOC_CTX *frame;
        NTSTATUS status;
+       bool ok;
 
        /*
         * Do this before any other talloc operation
@@ -793,9 +810,15 @@ static bool open_sockets(bool isdaemon, int port)
        talloc_enable_null_tracking();
        frame = talloc_stackframe();
 
+       /*
+        * We want total control over the permissions on created files,
+        * so set our umask to 0.
+        */
+       umask(0);
+
        setup_logging(argv[0], DEBUG_DEFAULT_STDOUT);
 
-       load_case_tables();
+       smb_init_locale();
 
        global_nmb_port = NMB_PORT;
 
@@ -842,7 +865,7 @@ static bool open_sockets(bool isdaemon, int port)
        }
 
        fault_setup();
-       dump_core_setup("nmbd", lp_logfile());
+       dump_core_setup("nmbd", lp_logfile(talloc_tos()));
 
        /* POSIX demands that signals are inherited. If the invoking process has
         * these signals masked, we will have problems, as we won't receive them. */
@@ -860,6 +883,9 @@ static bool open_sockets(bool isdaemon, int port)
        BlockSignals(True, SIGUSR2);
 #endif
 
+       /* Ignore children - no zombies. */
+       CatchChild();
+
        if ( opt_interactive ) {
                Fork = False;
                log_stdout = True;
@@ -886,6 +912,22 @@ static bool open_sockets(bool isdaemon, int port)
                exit(1);
        }
 
+       reopen_logs();
+
+       if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
+           && !lp_parm_bool(-1, "server role check", "inhibit", false)) {
+               /* TODO: when we have a merged set of defaults for
+                * loadparm, we could possibly check if the internal
+                * nbt server is in the list, and allow a startup if disabled */
+               DEBUG(0, ("server role = 'active directory domain controller' not compatible with running nmbd standalone. \n"));
+               DEBUGADD(0, ("You should start 'samba' instead, and it will control starting the internal nbt server\n"));
+               exit(1);
+       }
+
+       if (!cluster_probe_ok()) {
+               exit(1);
+       }
+
        msg = messaging_init(NULL, server_event_context());
        if (msg == NULL) {
                return 1;
@@ -907,12 +949,12 @@ static bool open_sockets(bool isdaemon, int port)
        set_samba_nb_type();
 
        if (!is_daemon && !is_a_socket(0)) {
-               DEBUG(0,("standard input is not a socket, assuming -D option\n"));
+               DEBUG(3, ("standard input is not a socket, assuming -D option\n"));
                is_daemon = True;
        }
 
        if (is_daemon && !opt_interactive) {
-               DEBUG( 2, ( "Becoming a daemon.\n" ) );
+               DEBUG(3, ("Becoming a daemon.\n"));
                become_daemon(Fork, no_process_group, log_stdout);
        }
 
@@ -928,27 +970,27 @@ static bool open_sockets(bool isdaemon, int port)
 #ifndef SYNC_DNS
        /* Setup the async dns. We do it here so it doesn't have all the other
                stuff initialised and thus chewing memory and sockets */
-       if(lp_we_are_a_wins_server() && lp_dns_proxy()) {
+       if(lp_we_are_a_wins_server() && lp_wins_dns_proxy()) {
                start_async_dns(msg);
        }
 #endif
 
-       if (!directory_exist(lp_lockdir())) {
-               mkdir(lp_lockdir(), 0755);
+       ok = directory_create_or_exist(lp_lock_directory(), 0755);
+       if (!ok) {
+               exit_daemon("Failed to create directory for lock files, check 'lock directory'", errno);
        }
 
-       if (!directory_exist(lp_piddir())) {
-               mkdir(lp_piddir(), 0755);
+       ok = directory_create_or_exist(lp_pid_directory(), 0755);
+       if (!ok) {
+               exit_daemon("Failed to create directory for pid files, check 'pid directory'", errno);
        }
 
-       pidfile_create("nmbd");
+       pidfile_create(lp_pid_directory(), "nmbd");
 
-       status = reinit_after_fork(msg, nmbd_event_context(),
-                                  false);
+       status = reinit_after_fork(msg, nmbd_event_context(), false, NULL);
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0,("reinit_after_fork() failed\n"));
-               exit(1);
+               exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
        }
 
        /*
@@ -958,24 +1000,17 @@ static bool open_sockets(bool isdaemon, int port)
         */
        status = init_before_fork();
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status)));
-               exit(1);
+               exit_daemon(nt_errstr(status), map_errno_from_nt_status(status));
        }
 
        if (!nmbd_setup_sig_term_handler(msg))
-               exit(1);
+               exit_daemon("NMBD failed to setup signal handler", EINVAL);
        if (!nmbd_setup_stdin_handler(msg, !Fork))
-               exit(1);
+               exit_daemon("NMBD failed to setup stdin handler", EINVAL);
        if (!nmbd_setup_sig_hup_handler(msg))
-               exit(1);
+               exit_daemon("NMBD failed to setup SIGHUP handler", EINVAL);
 
-       /* get broadcast messages */
-
-       if (!serverid_register(messaging_server_id(msg),
-                               FLAG_MSG_GENERAL |
-                               FLAG_MSG_NMBD |
-                               FLAG_MSG_DBWRAP)) {
-               DEBUG(1, ("Could not register myself in serverid.tdb\n"));
+       if (!messaging_parent_dgm_cleanup_init(msg)) {
                exit(1);
        }
 
@@ -1007,9 +1042,8 @@ static bool open_sockets(bool isdaemon, int port)
 
        /* Create an nmbd subnet record for each of the above. */
        if( False == create_subnets() ) {
-               DEBUG(0,("ERROR: Failed when creating subnet lists. Exiting.\n"));
                kill_async_dns_child();
-               exit(1);
+               exit_daemon("NMBD failed when creating subnet lists", EACCES);
        }
 
        /* Load in any static local names. */ 
@@ -1021,9 +1055,8 @@ static bool open_sockets(bool isdaemon, int port)
 
        /* If we are acting as a WINS server, initialise data structures. */
        if( !initialise_wins() ) {
-               DEBUG( 0, ( "nmbd: Failed when initialising WINS server.\n" ) );
                kill_async_dns_child();
-               exit(1);
+               exit_daemon( "NMBD failed when initialising WINS server.", EACCES);
        }
 
        /* 
@@ -1035,21 +1068,23 @@ static bool open_sockets(bool isdaemon, int port)
         */
 
        if( False == register_my_workgroup_and_names() ) {
-               DEBUG(0,("ERROR: Failed when creating my my workgroup. Exiting.\n"));
                kill_async_dns_child();
-               exit(1);
+               exit_daemon( "NMBD failed when creating my workgroup.", EACCES);
        }
 
        if (!initialize_nmbd_proxy_logon()) {
-               DEBUG(0,("ERROR: Failed setup nmbd_proxy_logon.\n"));
                kill_async_dns_child();
-               exit(1);
+               exit_daemon( "NMBD failed to setup nmbd_proxy_logon.", EACCES);
        }
 
        if (!nmbd_init_packet_server()) {
                kill_async_dns_child();
-                exit(1);
-        }
+               exit_daemon( "NMBD failed to setup packet server.", EACCES);
+       }
+
+       if (is_daemon && !opt_interactive) {
+               daemon_ready("nmbd");
+       }
 
        TALLOC_FREE(frame);
        process(msg);