s4:schannel merge code with s3
[ira/wip.git] / source4 / smbd / server.c
index d576782ab1f0f189ec0a25500aecfb73c6886782..83f6e7d8063c660f68297abfdba024c39101b002 100644 (file)
 #include "lib/cmdline/popt_common.h"
 #include "system/dir.h"
 #include "system/filesys.h"
-#include "ldb/include/ldb.h"
-#include "registry/registry.h"
 #include "ntvfs/ntvfs.h"
 #include "ntptr/ntptr.h"
 #include "auth/gensec/gensec.h"
 #include "smbd/process_model.h"
-#include "smbd/service.h"
 #include "param/secrets.h"
 #include "smbd/pidfile.h"
 #include "param/param.h"
+#include "dsdb/samdb/samdb.h"
+#include "auth/session.h"
+#include "lib/messaging/irpc.h"
+#include "librpc/gen_ndr/ndr_irpc.h"
+#include "cluster/cluster.h"
 
 /*
   recursively delete a directory tree
@@ -112,6 +114,7 @@ static void sig_term(int sig)
                kill(-getpgrp(), SIGTERM);
        }
 #endif
+       DEBUG(0,("Exiting pid %d on SIGTERM\n", (int)getpid()));
        exit(0);
 }
 
@@ -137,7 +140,7 @@ static void setup_signals(void)
 #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. */
+        * these signals masked, we will have problems, as we won't receive them. */
        BlockSignals(false, SIGHUP);
        BlockSignals(false, SIGTERM);
 
@@ -157,6 +160,7 @@ static void server_stdin_handler(struct tevent_context *event_ctx, struct tevent
                DEBUG(0,("%s: EOF on stdin - terminating\n", binary_name));
 #if HAVE_GETPGRP
                if (getpgrp() == getpid()) {
+                       DEBUG(0,("Sending SIGTERM from pid %d\n", (int)getpid()));
                        kill(-getpgrp(), SIGTERM);
                }
 #endif
@@ -176,6 +180,59 @@ _NORETURN_ static void max_runtime_handler(struct tevent_context *ev,
        exit(0);
 }
 
+/*
+  pre-open the key databases. This saves a lot of time in child
+  processes
+ */
+static void prime_ldb_databases(struct tevent_context *event_ctx)
+{
+       TALLOC_CTX *db_context;
+       db_context = talloc_new(event_ctx);
+
+       samdb_connect(db_context, event_ctx, cmdline_lp_ctx, system_session(cmdline_lp_ctx));
+       privilege_connect(db_context, event_ctx, cmdline_lp_ctx);
+
+       /* we deliberately leave these open, which allows them to be
+        * re-used in ldb_wrap_connect() */
+}
+
+
+/*
+  called when a fatal condition occurs in a child task
+ */
+static NTSTATUS samba_terminate(struct irpc_message *msg, 
+                               struct samba_terminate *r)
+{
+       DEBUG(0,("samba_terminate: %s\n", r->in.reason));
+       exit(1);
+}
+
+/*
+  setup messaging for the top level samba (parent) task
+ */
+static NTSTATUS setup_parent_messaging(struct tevent_context *event_ctx, 
+                                      struct loadparm_context *lp_ctx)
+{
+       struct messaging_context *msg;
+       NTSTATUS status;
+
+       msg = messaging_init(talloc_autofree_context(), 
+                            lp_messaging_path(event_ctx, lp_ctx),
+                            cluster_id(0, SAMBA_PARENT_TASKID),
+                            lp_iconv_convenience(lp_ctx),
+                            event_ctx);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       irpc_add_name(msg, "samba");
+
+       status = IRPC_REGISTER(msg, irpc, SAMBA_TERMINATE,
+                              samba_terminate, NULL);
+
+       return status;
+}
+
+
+
 /*
  main server.
 */
@@ -196,6 +253,8 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        extern NTSTATUS server_service_cldapd_init(void);
        extern NTSTATUS server_service_smb_init(void);
        extern NTSTATUS server_service_drepl_init(void);
+       extern NTSTATUS server_service_kcc_init(void);
+       extern NTSTATUS server_service_dnsupdate_init(void);
        extern NTSTATUS server_service_rpc_init(void);
        extern NTSTATUS server_service_ntp_signd_init(void);
        extern NTSTATUS server_service_samba3_smb_init(void);
@@ -242,7 +301,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
                        fprintf(stderr, "\nInvalid option %s: %s\n\n",
                                  poptBadOption(pc, 0), poptStrerror(opt));
                        poptPrintUsage(pc, stderr, 0);
-                       exit(1);
+                       return 1;
                }
        }
 
@@ -250,7 +309,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
                fprintf(stderr,"\nERROR: "
                          "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
                poptPrintUsage(pc, stderr, 0);
-               exit(1);
+               return 1;
        } else if (!opt_interactive) {
                /* default is --daemon */
                opt_daemon = true;
@@ -266,13 +325,13 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        umask(0);
 
        DEBUG(0,("%s version %s started.\n", binary_name, SAMBA_VERSION_STRING));
-       DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2009\n"));
+       DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2010\n"));
 
        if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4 || sizeof(uint64_t) < 8) {
                DEBUG(0,("ERROR: Samba is not configured correctly for the word size on your machine\n"));
                DEBUGADD(0,("sizeof(uint16_t) = %u, sizeof(uint32_t) %u, sizeof(uint64_t) = %u\n",
                            (unsigned int)sizeof(uint16_t), (unsigned int)sizeof(uint32_t), (unsigned int)sizeof(uint64_t)));
-               exit(1);
+               return 1;
        }
 
        if (opt_daemon) {
@@ -292,7 +351,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
         * passdb/secrets.c, and proved that Samba still builds... */
        /* Setup the SECRETS subsystem */
        if (secrets_init(talloc_autofree_context(), cmdline_lp_ctx) == NULL) {
-               exit(1);
+               return 1;
        }
 
        gensec_init(cmdline_lp_ctx); /* FIXME: */
@@ -316,6 +375,9 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
           should hang off that */
        event_ctx = s4_event_context_init(talloc_autofree_context());
 
+       /* setup this as the default context */
+       s4_event_context_set_default(event_ctx);
+
        if (event_ctx == NULL) {
                DEBUG(0,("Initializing event context failed\n"));
                return 1;
@@ -344,7 +406,16 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
                                 discard_const(binary_name));
        }
 
+       prime_ldb_databases(event_ctx);
+
+       status = setup_parent_messaging(event_ctx, cmdline_lp_ctx);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("Failed to setup parent messaging - %s\n", nt_errstr(status)));
+               return 1;
+       }
+
        DEBUG(0,("%s: using '%s' process model\n", binary_name, model));
+
        status = server_service_startup(event_ctx, cmdline_lp_ctx, model, 
                                        lp_server_services(cmdline_lp_ctx));
        if (!NT_STATUS_IS_OK(status)) {
@@ -363,7 +434,7 @@ static int binary_smbd_main(const char *binary_name, int argc, const char *argv[
        return 0;
 }
 
- int main(int argc, const char *argv[])
+int main(int argc, const char *argv[])
 {
-       return binary_smbd_main("smbd", argc, argv);
+       return binary_smbd_main("samba", argc, argv);
 }