s3-smbd provide struct smbd_server_connection * to conn_snum_used
[nivanova/samba-autobuild/.git] / source3 / smbd / server.c
index ebcc8ab6d7bedcd7c8bea3dc50c18820c009cdb9..10edb5ef4521e7593daae4f5799b26a222107a23 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "popt_common.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
-#include "librpc/gen_ndr/messaging.h"
-#include "registry.h"
 #include "registry/reg_init_full.h"
 #include "libcli/auth/schannel.h"
 #include "secrets.h"
 #include "memcache.h"
 #include "ctdbd_conn.h"
 #include "printing/printer_list.h"
+#include "rpc_server/rpc_ep_setup.h"
+#include "printing/pcap.h"
+#include "printing.h"
+#include "serverid.h"
+#include "passdb.h"
+#include "auth.h"
+#include "messages.h"
+#include "smbprofile.h"
 
-#include "../librpc/gen_ndr/srv_dfs.h"
-#include "../librpc/gen_ndr/srv_dssetup.h"
-#include "../librpc/gen_ndr/srv_echo.h"
-#include "../librpc/gen_ndr/srv_eventlog.h"
-#include "../librpc/gen_ndr/srv_initshutdown.h"
-#include "../librpc/gen_ndr/srv_lsa.h"
-#include "../librpc/gen_ndr/srv_netlogon.h"
-#include "../librpc/gen_ndr/srv_ntsvcs.h"
-#include "../librpc/gen_ndr/srv_samr.h"
-#include "../librpc/gen_ndr/srv_spoolss.h"
-#include "../librpc/gen_ndr/srv_srvsvc.h"
-#include "../librpc/gen_ndr/srv_svcctl.h"
-#include "../librpc/gen_ndr/srv_winreg.h"
-#include "../librpc/gen_ndr/srv_wkssvc.h"
-
-#include "printing/nt_printing_migrate.h"
-
-static_decl_rpc;
+extern void start_epmd(struct tevent_context *ev_ctx,
+                      struct messaging_context *msg_ctx);
+
+extern void start_spoolssd(struct event_context *ev_ctx,
+                          struct messaging_context *msg_ctx);
 
 #ifdef WITH_DFS
 extern int dcelogin_atmost_once;
 #endif /* WITH_DFS */
 
-static void smbd_set_server_fd(int fd)
-{
-       struct smbd_server_connection *sconn = smbd_server_conn;
-       char addr[INET6_ADDRSTRLEN];
-       const char *name;
-
-       sconn->sock = fd;
-
-       /*
-        * Initialize sconn->client_id: If we can't find the client's
-        * name, default to its address.
-        */
-
-       client_addr(fd, sconn->client_id.addr, sizeof(sconn->client_id.addr));
-
-       name = client_name(sconn->sock);
-       if (strcmp(name, "UNKNOWN") != 0) {
-               name = talloc_strdup(sconn, name);
-       } else {
-               name = NULL;
-       }
-       sconn->client_id.name =
-               (name != NULL) ? name : sconn->client_id.addr;
-
-       sub_set_socket_ids(sconn->client_id.addr, sconn->client_id.name,
-                          client_socket_addr(sconn->sock, addr,
-                                             sizeof(addr)));
-}
-
 struct event_context *smbd_event_context(void)
 {
        return server_event_context();
@@ -100,12 +66,36 @@ static void smb_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);
+
        DEBUG(10,("smb_conf_updated: Got message saying smb.conf was "
                  "updated. Reloading.\n"));
        change_to_root_user();
        reload_services(msg, smbd_server_conn->sock, False);
+       if (am_parent) {
+               pcap_cache_reload(ev_ctx, msg,
+                                 &reload_pcap_change_notify);
+       }
 }
 
+/*******************************************************************
+ What to do when printcap is updated.
+ ********************************************************************/
+
+static void smb_pcap_updated(struct messaging_context *msg,
+                            void *private_data,
+                            uint32_t msg_type,
+                            struct server_id server_id,
+                            DATA_BLOB *data)
+{
+       struct tevent_context *ev_ctx =
+               talloc_get_type_abort(private_data, struct tevent_context);
+
+       DEBUG(10,("Got message saying pcap was updated. Reloading.\n"));
+       change_to_root_user();
+       reload_printers(ev_ctx, msg);
+}
 
 /*******************************************************************
  Delete a statcache entry.
@@ -294,7 +284,8 @@ static void remove_child_pid(pid_t pid, bool unclean_shutdown)
                }
        }
 
-       DEBUG(0, ("Could not find child %d -- ignoring\n", (int)pid));
+       /* not all forked child processes are added to the children list */
+       DEBUG(1, ("Could not find child %d -- ignoring\n", (int)pid));
 }
 
 /****************************************************************************
@@ -581,6 +572,26 @@ static bool smbd_open_one_socket(struct smbd_parent_context *parent,
        return true;
 }
 
+static bool smbd_parent_housekeeping(const struct timeval *now, void *private_data)
+{
+       time_t printcap_cache_time = (time_t)lp_printcap_cache_time();
+       time_t t = time_mono(NULL);
+
+       DEBUG(5, ("parent housekeeping\n"));
+
+       /* if periodic printcap rescan is enabled, see if it's time to reload */
+       if ((printcap_cache_time != 0)
+        && (t >= (last_printer_reload_time + printcap_cache_time))) {
+               DEBUG( 3,( "Printcap cache time expired.\n"));
+               pcap_cache_reload(server_event_context(),
+                                 smbd_messaging_context(),
+                                 &reload_pcap_change_notify);
+               last_printer_reload_time = t;
+       }
+
+       return true;
+}
+
 /****************************************************************************
  Open the socket communication.
 ****************************************************************************/
@@ -641,6 +652,13 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                                        continue;
                                }
 
+                               /* Keep the first port for mDNS service
+                                * registration.
+                                */
+                               if (dns_port == 0) {
+                                       dns_port = port;
+                               }
+
                                if (!smbd_open_one_socket(parent, ifss, port)) {
                                        return false;
                                }
@@ -715,19 +733,31 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                return false;
        }
 
+       if (!(event_add_idle(smbd_event_context(), NULL,
+                            timeval_set(SMBD_HOUSEKEEPING_INTERVAL, 0),
+                            "parent_housekeeping", smbd_parent_housekeeping,
+                            NULL))) {
+               DEBUG(0, ("Could not add parent_housekeeping event\n"));
+               return false;
+       }
+
         /* Listen to messages */
 
        messaging_register(msg_ctx, NULL, MSG_SMB_SAM_SYNC, msg_sam_sync);
        messaging_register(msg_ctx, NULL, MSG_SHUTDOWN, msg_exit_server);
        messaging_register(msg_ctx, NULL, MSG_SMB_FILE_RENAME,
                           msg_file_was_renamed);
-       messaging_register(msg_ctx, NULL, MSG_SMB_CONF_UPDATED,
+       messaging_register(msg_ctx, server_event_context(), MSG_SMB_CONF_UPDATED,
                           smb_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, server_event_context(), MSG_PRINTER_PCAP,
+                          smb_pcap_updated);
        brl_register_msgs(msg_ctx);
 
+       msg_idmap_register_msgs(msg_ctx);
+
 #ifdef CLUSTER_SUPPORT
        if (lp_clustering()) {
                ctdbd_register_reconfigure(messaging_ctdbd_connection());
@@ -739,7 +769,7 @@ static bool open_sockets_smbd(struct smbd_parent_context *parent,
                           msg_inject_fault);
 #endif
 
-       if (dns_port != 0) {
+       if (lp_multicast_dns_register() && (dns_port != 0)) {
 #ifdef WITH_DNSSD_SUPPORT
                smbd_setup_mdns_registration(smbd_event_context(),
                                             parent, dns_port);
@@ -799,20 +829,6 @@ static bool init_structs(void )
        return True;
 }
 
-static bool spoolss_init_cb(void *ptr)
-{
-       struct messaging_context *msg_ctx = talloc_get_type_abort(
-               ptr, struct messaging_context);
-       return nt_printing_tdb_migrate(msg_ctx);
-}
-
-static bool spoolss_shutdown_cb(void *ptr)
-{
-       srv_spoolss_cleanup();
-
-       return true;
-}
-
 /****************************************************************************
  main program.
 ****************************************************************************/
@@ -861,7 +877,6 @@ extern void build_options(bool screen);
        TALLOC_CTX *frame;
        NTSTATUS status;
        uint64_t unique_id;
-       struct rpc_srv_callbacks spoolss_cb;
 
        /*
         * Do this before any other talloc operation
@@ -869,6 +884,8 @@ extern void build_options(bool screen);
        talloc_enable_null_tracking();
        frame = talloc_stackframe();
 
+       load_case_tables();
+
        /* Initialize the event context, it will panic on error */
        smbd_event_context();
 
@@ -926,8 +943,6 @@ extern void build_options(bool screen);
                exit(0);
        }
 
-       load_case_tables();
-
 #ifdef HAVE_SETLUID
        /* needed for SecureWare on SCO */
        setluid(0);
@@ -956,8 +971,8 @@ extern void build_options(bool screen);
        gain_root_privilege();
        gain_root_group_privilege();
 
-       fault_setup((void (*)(void *))exit_server_fault);
-       dump_core_setup("smbd");
+       fault_setup();
+       dump_core_setup("smbd", lp_logfile());
 
        /* we are never interested in SIGPIPE */
        BlockSignals(True,SIGPIPE);
@@ -1004,7 +1019,7 @@ extern void build_options(bool screen);
        }
 
        if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
-               DEBUG(0, ("error opening config file\n"));
+               DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
                exit(1);
        }
 
@@ -1018,7 +1033,7 @@ extern void build_options(bool screen);
         * 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.
         */
-       if (!reload_services(smbd_messaging_context(), -1, False)) {
+       if (!reload_services(NULL, -1, False)) {
                exit(1);
        }
 
@@ -1155,11 +1170,6 @@ extern void build_options(bool screen);
        if (!print_backend_init(smbd_messaging_context()))
                exit(1);
 
-#if 0
-       if (!init_svcctl_db())
-                exit(1);
-#endif
-
        /* Open the share_info.tdb here, so we don't have to open
           after the fork on every single connection.  This is a small
           performance improvment and reduces the total number of system
@@ -1169,8 +1179,10 @@ extern void build_options(bool screen);
                exit(1);
        }
 
-       if (!init_system_info()) {
-               DEBUG(0,("ERROR: failed to setup system user info.\n"));
+       status = init_system_info();
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
+                         nt_errstr(status)));
                return -1;
        }
 
@@ -1184,28 +1196,25 @@ extern void build_options(bool screen);
                return -1;
        }
 
-       /*
-        * Initialize spoolss with an init function to convert printers first.
-        * static_init_rpc will try to initialize the spoolss server too but you
-        * can't register it twice.
-        */
-       spoolss_cb.init = spoolss_init_cb;
-       spoolss_cb.shutdown = spoolss_shutdown_cb;
-       spoolss_cb.private_data = smbd_server_conn->msg_ctx;
+       if (is_daemon && !interactive) {
+               const char *rpcsrv_type;
 
-       /* Spoolss depends on a winreg pipe, so start it first. */
-       if (!NT_STATUS_IS_OK(rpc_winreg_init(NULL))) {
-               exit(1);
+               rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
+                                                  "rpc_server", "epmapper",
+                                                  "none");
+               if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
+                       start_epmd(smbd_event_context(),
+                                  smbd_server_conn->msg_ctx);
+               }
        }
 
-       if (!NT_STATUS_IS_OK(rpc_spoolss_init(&spoolss_cb))) {
+       if (!dcesrv_ep_setup(smbd_event_context(), smbd_server_conn->msg_ctx)) {
                exit(1);
        }
 
-       static_init_rpc;
-
        /* Publish nt printers, this requires a working winreg pipe */
-       reload_printers(smbd_messaging_context());
+       pcap_cache_reload(server_event_context(), smbd_messaging_context(),
+                         &reload_printers);
 
        /* only start the background queue daemon if we are 
           running as a daemon -- bad things will happen if
@@ -1218,6 +1227,20 @@ extern void build_options(bool screen);
                                       smbd_messaging_context());
        }
 
+       if (is_daemon && !_lp_disable_spoolss()) {
+               const char *rpcsrv_type;
+
+               /* start spoolss daemon */
+               /* start as a separate daemon only if enabled */
+               rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
+                                                  "rpc_server", "spoolss",
+                                                  "embedded");
+               if (strcasecmp_m(rpcsrv_type, "daemon") == 0) {
+                       start_spoolssd(smbd_event_context(),
+                                      smbd_messaging_context());
+               }
+       }
+
        if (!is_daemon) {
                /* inetd mode */
                TALLOC_FREE(frame);