smbd: Remove source3/smbd/statcache.c
[samba.git] / source3 / smbd / server.c
index b9fb7e855b83f19ad603a318271d5324fdd4b047..6bdaca7a0a410286be5ac3cc9aa335c1eee9b77c 100644 (file)
 #include "locking/share_mode_lock.h"
 #include "smbd/smbd.h"
 #include "smbd/globals.h"
+#include "smbd/smbXsrv_open.h"
 #include "registry/reg_init_full.h"
 #include "libcli/auth/schannel.h"
 #include "secrets.h"
 #include "../lib/util/memcache.h"
 #include "ctdbd_conn.h"
+#include "lib/util/util_process.h"
 #include "util_cluster.h"
 #include "printing/queue_process.h"
-#include "rpc_server/rpc_service_setup.h"
 #include "rpc_server/rpc_config.h"
 #include "passdb.h"
 #include "auth.h"
 #include "lib/util/sys_rw.h"
 #include "cleanupdb.h"
 #include "g_lock.h"
-#include "rpc_server/epmd.h"
-#include "rpc_server/lsasd.h"
-#include "rpc_server/fssd.h"
-#include "rpc_server/mdssd.h"
 #include "lib/global_contexts.h"
+#include "source3/lib/substitute.h"
 
 #ifdef CLUSTER_SUPPORT
 #include "ctdb_protocol.h"
@@ -73,7 +71,6 @@ struct smbd_parent_context {
 
        struct tevent_context *ev_ctx;
        struct messaging_context *msg_ctx;
-       struct dcesrv_context *dce_ctx;
 
        /* the list of listening sockets */
        struct smbd_open_socket *sockets;
@@ -113,15 +110,12 @@ static void smbd_parent_conf_updated(struct messaging_context *msg,
                                     struct server_id server_id,
                                     DATA_BLOB *data)
 {
-       struct tevent_context *ev_ctx =
-               talloc_get_type_abort(private_data, struct tevent_context);
        bool ok;
 
        DEBUG(10,("smbd_parent_conf_updated: Got message saying smb.conf was "
                  "updated. Reloading.\n"));
        change_to_root_user();
        reload_services(NULL, NULL, false);
-       printing_subsystem_update(ev_ctx, msg, false);
 
        ok = reinit_guest_session_info(NULL);
        if (!ok) {
@@ -130,21 +124,6 @@ static void smbd_parent_conf_updated(struct messaging_context *msg,
        messaging_send_to_children(msg, MSG_SMB_CONF_UPDATED, NULL);
 }
 
-/*******************************************************************
- Delete a statcache entry.
- ********************************************************************/
-
-static void smb_stat_cache_delete(struct messaging_context *msg,
-                                 void *private_data,
-                                 uint32_t msg_tnype,
-                                 struct server_id server_id,
-                                 DATA_BLOB *data)
-{
-       const char *name = (const char *)data->data;
-       DEBUG(10,("smb_stat_cache_delete: delete name %s\n", name));
-       stat_cache_delete(name);
-}
-
 /****************************************************************************
   Send a SIGTERM to our process group.
 *****************************************************************************/
@@ -402,6 +381,7 @@ static void notifyd_sig_hup_handler(struct tevent_context *ev,
 {
        DBG_NOTICE("notifyd: Reloading services after SIGHUP\n");
        reload_services(NULL, NULL, false);
+       reopen_logs();
 }
 
 static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive,
@@ -409,10 +389,10 @@ static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive,
 {
        struct tevent_context *ev = messaging_tevent_context(msg);
        struct tevent_req *req;
+       struct tevent_signal *se = NULL;
        pid_t pid;
        NTSTATUS status;
        bool ok;
-       struct tevent_signal *se;
 
        if (interactive) {
                req = notifyd_req(msg, ev);
@@ -434,13 +414,15 @@ static bool smbd_notifyd_init(struct messaging_context *msg, bool interactive,
                return true;
        }
 
-       status = smbd_reinit_after_fork(msg, ev, true, "smbd-notifyd");
+       status = smbd_reinit_after_fork(msg, ev, true);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(1, ("%s: reinit_after_fork failed: %s\n",
                          __func__, nt_errstr(status)));
                exit(1);
        }
 
+       process_set_title("smbd-notifyd", "notifyd");
+
        reopen_logs();
 
        /* Set up sighup handler for notifyd */
@@ -569,6 +551,17 @@ static void notifyd_started(struct tevent_req *req)
        }
 }
 
+static void cleanupd_sig_hup_handler(struct tevent_context *ev,
+                                    struct tevent_signal *se,
+                                    int signum,
+                                    int count,
+                                    void *siginfo,
+                                    void *pvt)
+{
+       DBG_NOTICE("cleanupd: Reloading services after SIGHUP\n");
+       reopen_logs();
+}
+
 static void cleanupd_stopped(struct tevent_req *req);
 
 static bool cleanupd_init(struct messaging_context *msg, bool interactive,
@@ -576,6 +569,7 @@ static bool cleanupd_init(struct messaging_context *msg, bool interactive,
 {
        struct tevent_context *ev = messaging_tevent_context(msg);
        struct server_id parent_id = messaging_server_id(msg);
+       struct tevent_signal *se = NULL;
        struct tevent_req *req;
        pid_t pid;
        NTSTATUS status;
@@ -636,7 +630,7 @@ static bool cleanupd_init(struct messaging_context *msg, bool interactive,
 
        close(up_pipe[0]);
 
-       status = smbd_reinit_after_fork(msg, ev, true, "cleanupd");
+       status = smbd_reinit_after_fork(msg, ev, true);
        if (!NT_STATUS_IS_OK(status)) {
                DBG_WARNING("reinit_after_fork failed: %s\n",
                            nt_errstr(status));
@@ -646,6 +640,19 @@ static bool cleanupd_init(struct messaging_context *msg, bool interactive,
                exit(1);
        }
 
+       process_set_title("smbd-cleanupd", "cleanupd");
+
+       se = tevent_add_signal(ev,
+                              ev,
+                              SIGHUP,
+                              0,
+                              cleanupd_sig_hup_handler,
+                              NULL);
+       if (se == NULL) {
+               DBG_ERR("Could not add SIGHUP handler\n");
+               exit(1);
+       }
+
        req = smbd_cleanupd_send(msg, ev, msg, parent_id.pid);
        if (req == NULL) {
                DBG_WARNING("smbd_cleanupd_send failed\n");
@@ -950,7 +957,6 @@ static void smbd_accept_connection(struct tevent_context *ev,
        struct smbd_open_socket *s = talloc_get_type_abort(private_data,
                                     struct smbd_open_socket);
        struct messaging_context *msg_ctx = s->parent->msg_ctx;
-       struct dcesrv_context *dce_ctx = s->parent->dce_ctx;
        struct sockaddr_storage addr;
        socklen_t in_addrlen = sizeof(addr);
        int fd;
@@ -968,8 +974,8 @@ static void smbd_accept_connection(struct tevent_context *ev,
        smb_set_close_on_exec(fd);
 
        if (s->parent->interactive) {
-               reinit_after_fork(msg_ctx, ev, true, NULL);
-               smbd_process(ev, msg_ctx, dce_ctx, fd, true);
+               reinit_after_fork(msg_ctx, ev, true);
+               smbd_process(ev, msg_ctx, fd, true);
                exit_server_cleanly("end of interactive mode");
                return;
        }
@@ -981,6 +987,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
 
        pid = fork();
        if (pid == 0) {
+               char addrstr[INET6_ADDRSTRLEN];
                NTSTATUS status = NT_STATUS_OK;
 
                /*
@@ -994,7 +1001,7 @@ static void smbd_accept_connection(struct tevent_context *ev,
                 * them, counting worker smbds. */
                CatchChild();
 
-               status = smbd_reinit_after_fork(msg_ctx, ev, true, NULL);
+               status = smbd_reinit_after_fork(msg_ctx, ev, true);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,
                                            NT_STATUS_TOO_MANY_OPENED_FILES)) {
@@ -1018,7 +1025,10 @@ static void smbd_accept_connection(struct tevent_context *ev,
                        smb_panic("reinit_after_fork() failed");
                }
 
-               smbd_process(ev, msg_ctx, dce_ctx, fd, false);
+               print_sockaddr(addrstr, sizeof(addrstr), &addr);
+               process_set_title("smbd[%s]", "client [%s]", addrstr);
+
+               smbd_process(ev, msg_ctx, fd, false);
         exit:
                exit_server_cleanly("end of child");
                return;
@@ -1255,8 +1265,6 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
        messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server);
        messaging_register(msg_ctx, ev_ctx, MSG_SMB_CONF_UPDATED,
                           smbd_parent_conf_updated);
-       messaging_register(msg_ctx, NULL, MSG_SMB_STAT_CACHE_DELETE,
-                          smb_stat_cache_delete);
        messaging_register(msg_ctx, NULL, MSG_DEBUG, smbd_msg_debug);
        messaging_register(msg_ctx, NULL, MSG_SMB_FORCE_TDIS,
                           smb_parent_send_to_children);
@@ -1408,15 +1416,9 @@ static void smbd_parent_sig_hup_handler(struct tevent_context *ev,
                                        void *siginfo,
                                        void *private_data)
 {
-       struct smbd_parent_context *parent =
-               talloc_get_type_abort(private_data,
-               struct smbd_parent_context);
-
        change_to_root_user();
        DEBUG(1,("parent: Reloading services after SIGHUP\n"));
        reload_services(NULL, NULL, false);
-
-       printing_subsystem_update(parent->ev_ctx, parent->msg_ctx, true);
 }
 
 struct smbd_claim_version_state {
@@ -1426,7 +1428,7 @@ struct smbd_claim_version_state {
 
 static void smbd_claim_version_parser(struct server_id exclusive,
                                      size_t num_shared,
-                                     struct server_id *shared,
+                                     const struct server_id *shared,
                                      const uint8_t *data,
                                      size_t datalen,
                                      void *private_data)
@@ -1461,8 +1463,12 @@ static NTSTATUS smbd_claim_version(struct messaging_context *msg,
                return NT_STATUS_UNSUCCESSFUL;
        }
 
-       status = g_lock_lock(
-               ctx, key, G_LOCK_READ, (struct timeval) { .tv_sec = 60 });
+       status = g_lock_lock(ctx,
+                            key,
+                            G_LOCK_READ,
+                            (struct timeval) { .tv_sec = 60 },
+                            NULL,
+                            NULL);
        if (!NT_STATUS_IS_OK(status)) {
                DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
                            nt_errstr(status));
@@ -1490,8 +1496,12 @@ static NTSTATUS smbd_claim_version(struct messaging_context *msg,
                return NT_STATUS_OK;
        }
 
-       status = g_lock_lock(
-               ctx, key, G_LOCK_UPGRADE, (struct timeval) { .tv_sec = 60 });
+       status = g_lock_lock(ctx,
+                            key,
+                            G_LOCK_UPGRADE,
+                            (struct timeval) { .tv_sec = 60 },
+                            NULL,
+                            NULL);
        if (!NT_STATUS_IS_OK(status)) {
                DBG_WARNING("g_lock_lock(G_LOCK_WRITE) failed: %s\n",
                            nt_errstr(status));
@@ -1510,8 +1520,12 @@ static NTSTATUS smbd_claim_version(struct messaging_context *msg,
                return status;
        }
 
-       status = g_lock_lock(
-               ctx, key, G_LOCK_DOWNGRADE, (struct timeval) { .tv_sec = 60 });
+       status = g_lock_lock(ctx,
+                            key,
+                            G_LOCK_DOWNGRADE,
+                            (struct timeval) { .tv_sec = 60 },
+                            NULL,
+                            NULL);
        if (!NT_STATUS_IS_OK(status)) {
                DBG_WARNING("g_lock_lock(G_LOCK_READ) failed: %s\n",
                            nt_errstr(status));
@@ -1544,8 +1558,6 @@ extern void build_options(bool screen);
        char *profile_level = NULL;
        int opt;
        poptContext pc;
-       bool print_build_options = False;
-       bool serving_printers = false;
        struct server_id main_server_id = {0};
        struct poptOption long_options[] = {
                POPT_AUTOHELP
@@ -1583,7 +1595,6 @@ extern void build_options(bool screen);
        NTSTATUS status;
        struct tevent_context *ev_ctx;
        struct messaging_context *msg_ctx;
-       struct dcesrv_context *dce_ctx = NULL;
        struct server_id server_id;
        struct tevent_signal *se;
        int profiling_level;
@@ -1592,7 +1603,6 @@ extern void build_options(bool screen);
                loadparm_s3_global_substitution();
        static const struct smbd_shim smbd_shim_fns =
        {
-               .send_stat_cache_delete_message = smbd_send_stat_cache_delete_message,
                .change_to_root_user = smbd_change_to_root_user,
                .become_authenticated_pipe_user = smbd_become_authenticated_pipe_user,
                .unbecome_authenticated_pipe_user = smbd_unbecome_authenticated_pipe_user,
@@ -1608,6 +1618,8 @@ extern void build_options(bool screen);
        };
        bool ok;
 
+       setproctitle_init(argc, discard_const(argv), environ);
+
        /*
         * Do this before any other talloc operation
         */
@@ -1649,7 +1661,8 @@ extern void build_options(bool screen);
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt)  {
                case 'b':
-                       print_build_options = True;
+                       build_options(true); /* Display output to screen as well as debug */
+                       exit(0);
                        break;
                default:
                        d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
@@ -1666,11 +1679,6 @@ extern void build_options(bool screen);
                log_stdout = True;
        }
 
-       if (print_build_options) {
-               build_options(True); /* Display output to screen as well as debug */
-               exit(0);
-       }
-
 #ifdef HAVE_SETLUID
        /* needed for SecureWare on SCO */
        setluid(0);
@@ -1741,7 +1749,7 @@ extern void build_options(bool screen);
        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 */ 
+       /* Output the build options to the debug log */
        build_options(False);
 
        if (sizeof(uint16_t) < 2 || sizeof(uint32_t) < 4) {
@@ -1782,11 +1790,6 @@ extern void build_options(bool screen);
                exit(1);
        }
 
-       dce_ctx = global_dcesrv_context();
-       if (dce_ctx == NULL) {
-               exit(1);
-       }
-
        /*
         * Reloading of the printers will not work here as we don't have a
         * server info and rpc services set up. It will be called later.
@@ -1868,7 +1871,7 @@ extern void build_options(bool screen);
        if (cmdline_daemon_cfg->daemon)
                pidfile_create(lp_pid_directory(), "smbd");
 
-       status = reinit_after_fork(msg_ctx, ev_ctx, false, NULL);
+       status = reinit_after_fork(msg_ctx, ev_ctx, false);
        if (!NT_STATUS_IS_OK(status)) {
                exit_daemon("reinit_after_fork() failed", map_errno_from_nt_status(status));
        }
@@ -1892,7 +1895,6 @@ extern void build_options(bool screen);
        parent->interactive = cmdline_daemon_cfg->interactive;
        parent->ev_ctx = ev_ctx;
        parent->msg_ctx = msg_ctx;
-       parent->dce_ctx = dce_ctx;
        am_parent = parent;
 
        se = tevent_add_signal(parent->ev_ctx,
@@ -1930,7 +1932,7 @@ extern void build_options(bool screen);
                exit_daemon("smbd can not open secrets.tdb", EACCES);
        }
 
-       if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC) {
+       if (lp_server_role() == ROLE_DOMAIN_BDC || lp_server_role() == ROLE_DOMAIN_PDC || lp_server_role() == ROLE_IPA_DC) {
                struct loadparm_context *lp_ctx = loadparm_init_s3(NULL, loadparm_s3_helpers());
                if (!open_schannel_session_store(NULL, lp_ctx)) {
                        exit_daemon("ERROR: Samba cannot open schannel store for secured NETLOGON operations.", EACCES);
@@ -2055,65 +2057,10 @@ extern void build_options(bool screen);
                return -1;
        }
 
-       status = dcesrv_init(ev_ctx, ev_ctx, msg_ctx, dce_ctx);
-       if (!NT_STATUS_IS_OK(status)) {
-               DBG_ERR("Failed to setup RPC server: %s\n", nt_errstr(status));
-               exit_daemon("Samba cannot setup ep pipe", EACCES);
-       }
-
        if (!cmdline_daemon_cfg->interactive) {
                daemon_ready("smbd");
        }
 
-       serving_printers = (!lp__disable_spoolss() &&
-                           (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED));
-
-       /* only start other daemons if we are running as a daemon
-        * -- bad things will happen if smbd is launched via inetd
-        *  and we fork a copy of ourselves here */
-       if (cmdline_daemon_cfg->daemon && !cmdline_daemon_cfg->interactive) {
-
-               if (rpc_epmapper_daemon() == RPC_DAEMON_FORK) {
-                       start_epmd(ev_ctx, msg_ctx, dce_ctx);
-               }
-
-               if (rpc_lsasd_daemon() == RPC_DAEMON_FORK) {
-                       start_lsasd(ev_ctx, msg_ctx, dce_ctx);
-               }
-
-               if (rpc_fss_daemon() == RPC_DAEMON_FORK) {
-                       start_fssd(ev_ctx, msg_ctx, dce_ctx);
-               }
-
-               if (serving_printers) {
-                       bool bgq = lp_parm_bool(-1, "smbd", "backgroundqueue", true);
-                       ok = printing_subsystem_init(ev_ctx,
-                                                    msg_ctx,
-                                                    dce_ctx,
-                                                    true,
-                                                    bgq);
-                       if (!ok) {
-                               exit_daemon("Samba failed to init printing subsystem", EACCES);
-                       }
-               }
-
-#ifdef WITH_SPOTLIGHT
-               if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) &&
-                   (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) {
-                       start_mdssd(ev_ctx, msg_ctx, dce_ctx);
-               }
-#endif
-       } else if (serving_printers) {
-               ok = printing_subsystem_init(ev_ctx,
-                                            msg_ctx,
-                                            dce_ctx,
-                                            false,
-                                            false);
-               if (!ok) {
-                       exit(1);
-               }
-       }
-
        if (!cmdline_daemon_cfg->daemon) {
                int ret, sock;
 
@@ -2147,7 +2094,7 @@ extern void build_options(bool screen);
                /* Stop zombies */
                smbd_setup_sig_chld_handler(parent);
 
-               smbd_process(ev_ctx, msg_ctx, dce_ctx, sock, true);
+               smbd_process(ev_ctx, msg_ctx, sock, true);
 
                exit_server_cleanly(NULL);
                return(0);
@@ -2156,13 +2103,6 @@ extern void build_options(bool screen);
        if (!open_sockets_smbd(parent, ev_ctx, msg_ctx, ports))
                exit_server("open_sockets_smbd() failed");
 
-       /* do a printer update now that all messaging has been set up,
-        * before we allow clients to start connecting */
-       if (!lp__disable_spoolss() &&
-           (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
-               printing_subsystem_update(ev_ctx, msg_ctx, false);
-       }
-
        TALLOC_FREE(frame);
        /* make sure we always have a valid stackframe */
        frame = talloc_stackframe();
@@ -2175,7 +2115,7 @@ extern void build_options(bool screen);
                */
                struct stat st;
                if (fstat(0, &st) != 0) {
-                       return false;
+                       return 1;
                }
                if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) {
                        tevent_add_fd(ev_ctx,