Add two flags to allow for handling of Extended Signatures (Session Key Protection...
[samba.git] / source3 / smbd / process.c
index f955959fd411cee0ab49eabbf7894b40c8c0039f..dd26a27ec391eae7ecff4fa57bcfd81c7184242c 100644 (file)
@@ -41,8 +41,6 @@
 #include "lib/id_cache.h"
 #include "serverid.h"
 
-extern bool global_machine_password_needs_changing;
-
 /* Internal message queue for deferred opens. */
 struct pending_message_list {
        struct pending_message_list *next, *prev;
@@ -516,13 +514,21 @@ static bool init_smb_request(struct smb_request *req,
                             size_t unread_bytes, bool encrypted,
                             uint32_t seqnum)
 {
+       struct smbXsrv_tcon *tcon;
+       NTSTATUS status;
+       NTTIME now;
        size_t req_size = smb_len(inbuf) + 4;
+
        /* Ensure we have at least smb_size bytes. */
        if (req_size < smb_size) {
                DEBUG(0,("init_smb_request: invalid request size %u\n",
                        (unsigned int)req_size ));
                return false;
        }
+
+       req->request_time = timeval_current();
+       now = timeval_to_nttime(&req->request_time);
+
        req->cmd    = CVAL(inbuf, smb_com);
        req->flags2 = SVAL(inbuf, smb_flg2);
        req->smbpid = SVAL(inbuf, smb_pid);
@@ -537,7 +543,12 @@ static bool init_smb_request(struct smb_request *req,
        req->unread_bytes = unread_bytes;
        req->encrypted = encrypted;
        req->sconn = sconn;
-       req->conn = conn_find(sconn,req->tid);
+       status = smb1srv_tcon_lookup(sconn->conn, req->tid, now, &tcon);
+       if (NT_STATUS_IS_OK(status)) {
+               req->conn = tcon->compat;
+       } else {
+               req->conn = NULL;
+       }
        req->chain_fsp = NULL;
        req->smb2req = NULL;
        req->priv_paths = NULL;
@@ -705,15 +716,14 @@ void remove_deferred_open_message_smb(struct smbd_server_connection *sconn,
  schedule it for immediate processing.
 ****************************************************************************/
 
-void schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
+bool schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
                                        uint64_t mid)
 {
        struct pending_message_list *pml;
        int i = 0;
 
        if (sconn->using_smb2) {
-               schedule_deferred_open_message_smb2(sconn, mid);
-               return;
+               return schedule_deferred_open_message_smb2(sconn, mid);
        }
 
        for (pml = sconn->deferred_open_queue; pml; pml = pml->next) {
@@ -755,13 +765,15 @@ void schedule_deferred_open_message_smb(struct smbd_server_connection *sconn,
                        TALLOC_FREE(pml->te);
                        pml->te = te;
                        DLIST_PROMOTE(sconn->deferred_open_queue, pml);
-                       return;
+                       return true;
                }
        }
 
        DEBUG(10,("schedule_deferred_open_message_smb: failed to "
                "find message mid %llu\n",
                (unsigned long long)mid ));
+
+       return false;
 }
 
 /****************************************************************************
@@ -1354,9 +1366,12 @@ static void smb_dump(const char *name, int type, const char *data)
 static connection_struct *switch_message(uint8 type, struct smb_request *req)
 {
        int flags;
-       uint16 session_tag;
+       uint64_t session_tag;
        connection_struct *conn = NULL;
        struct smbd_server_connection *sconn = req->sconn;
+       NTTIME now = timeval_to_nttime(&req->request_time);
+       struct smbXsrv_session *session = NULL;
+       NTSTATUS status;
 
        errno = 0;
 
@@ -1390,18 +1405,42 @@ static connection_struct *switch_message(uint8 type, struct smb_request *req)
         * JRA.
         */
 
+       /*
+        * lookup an existing session
+        *
+        * Note: for now we only check for NT_STATUS_NETWORK_SESSION_EXPIRED
+        * here, the main check is still in change_to_user()
+        */
+       status = smb1srv_session_lookup(sconn->conn,
+                                       session_tag,
+                                       now,
+                                       &session);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
+               switch (type) {
+               case SMBsesssetupX:
+                       status = NT_STATUS_OK;
+                       break;
+               default:
+                       DEBUG(1,("Error: session %llu is expired, mid=%llu.\n",
+                                (unsigned long long)session_tag,
+                                (unsigned long long)req->mid));
+                       reply_nterror(req, NT_STATUS_NETWORK_SESSION_EXPIRED);
+                       return conn;
+               }
+       }
+
        if (session_tag != sconn->smb1.sessions.last_session_tag) {
-               user_struct *vuser = NULL;
+               struct user_struct *vuser = NULL;
 
                sconn->smb1.sessions.last_session_tag = session_tag;
-               if(session_tag != UID_FIELD_INVALID) {
-                       vuser = get_valid_user_struct(sconn, session_tag);
-                       if (vuser) {
-                               set_current_user_info(
-                                       vuser->session_info->unix_info->sanitized_username,
-                                       vuser->session_info->unix_info->unix_name,
-                                       vuser->session_info->info->domain_name);
-                       }
+               if (session) {
+                       vuser = session->compat;
+               }
+               if (vuser) {
+                       set_current_user_info(
+                               vuser->session_info->unix_info->sanitized_username,
+                               vuser->session_info->unix_info->unix_name,
+                               vuser->session_info->info->domain_name);
                }
        }
 
@@ -1639,10 +1678,18 @@ void smb_request_done(struct smb_request *req)
 
        while ((next_index < num_reqs) && (IVAL(req->outbuf, smb_rcls) == 0)) {
                struct smb_request *next = reqs[next_index];
+               struct smbXsrv_tcon *tcon;
+               NTTIME now = timeval_to_nttime(&req->request_time);
 
                next->vuid = SVAL(req->outbuf, smb_uid);
                next->tid  = SVAL(req->outbuf, smb_tid);
-               next->conn = conn_find(req->sconn, req->tid);
+               status = smb1srv_tcon_lookup(req->sconn->conn, req->tid,
+                                            now, &tcon);
+               if (NT_STATUS_IS_OK(status)) {
+                       req->conn = tcon->compat;
+               } else {
+                       req->conn = NULL;
+               }
                next->chain_fsp = req->chain_fsp;
                next->inbuf = (uint8_t *)req->inbuf;
 
@@ -2415,12 +2462,21 @@ static void smbd_server_echo_handler(struct event_context *ev,
 }
 
 #ifdef CLUSTER_SUPPORT
+
+struct smbd_release_ip_state {
+       struct smbd_server_connection *sconn;
+       char addr[INET6_ADDRSTRLEN];
+};
+
 /****************************************************************************
 received when we should release a specific IP
 ****************************************************************************/
 static void release_ip(const char *ip, void *priv)
 {
-       const char *addr = (const char *)priv;
+       struct smbd_release_ip_state *state =
+               talloc_get_type_abort(priv,
+               struct smbd_release_ip_state);
+       const char *addr = state->addr;
        const char *p = addr;
 
        if (strncmp("::ffff:", addr, 7) == 0) {
@@ -2431,19 +2487,55 @@ static void release_ip(const char *ip, void *priv)
                   "our address is %s\n", ip, p));
 
        if ((strcmp(p, ip) == 0) || ((p != addr) && strcmp(addr, ip) == 0)) {
-               /* we can't afford to do a clean exit - that involves
-                  database writes, which would potentially mean we
-                  are still running after the failover has finished -
-                  we have to get rid of this process ID straight
-                  away */
                DEBUG(0,("Got release IP message for our IP %s - exiting immediately\n",
                        ip));
-               /* note we must exit with non-zero status so the unclean handler gets
-                  called in the parent, so that the brl database is tickled */
-               _exit(1);
+               /*
+                * With SMB2 we should do a clean disconnect,
+                * the previous_session_id in the session setup
+                * will cleanup the old session, tcons and opens.
+                *
+                * A clean disconnect is needed in order to support
+                * durable handles.
+                *
+                * Note: typically this is never triggered
+                *       as we got a TCP RST (triggered by ctdb event scripts)
+                *       before we get CTDB_SRVID_RELEASE_IP.
+                *
+                * We used to call _exit(1) here, but as this was mostly never
+                * triggered and has implication on our process model,
+                * we can just use smbd_server_connection_terminate()
+                * (also for SMB1).
+                */
+               smbd_server_connection_terminate(state->sconn,
+                                                "CTDB_SRVID_RELEASE_IP");
+               return;
        }
 }
 
+static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn,
+                                 struct sockaddr_storage *srv,
+                                 struct sockaddr_storage *clnt)
+{
+       struct smbd_release_ip_state *state;
+       struct ctdbd_connection *cconn;
+
+       cconn = messaging_ctdbd_connection();
+       if (cconn == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       state = talloc_zero(sconn, struct smbd_release_ip_state);
+       if (state == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       state->sconn = sconn;
+       if (print_sockaddr(state->addr, sizeof(state->addr), srv) == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       return ctdbd_register_ips(cconn, srv, clnt, release_ip, state);
+}
+
 static int client_get_tcp_info(int sock, struct sockaddr_storage *server,
                               struct sockaddr_storage *client)
 {
@@ -2532,9 +2624,6 @@ static bool housekeeping_fn(const struct timeval *now, void *private_data)
        /* check if we need to reload services */
        check_reload(sconn, time_mono(NULL));
 
-       /* Change machine password if neccessary. */
-       attempt_machine_password_change();
-
         /*
         * Force a log file check.
         */
@@ -3023,33 +3112,6 @@ fail:
        return false;
 }
 
-#if CLUSTER_SUPPORT
-
-static NTSTATUS smbd_register_ips(struct smbd_server_connection *sconn,
-                                 struct sockaddr_storage *srv,
-                                 struct sockaddr_storage *clnt)
-{
-       struct ctdbd_connection *cconn;
-       char tmp_addr[INET6_ADDRSTRLEN];
-       char *addr;
-
-       cconn = messaging_ctdbd_connection();
-       if (cconn == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (client_socket_addr(sconn->sock, tmp_addr, sizeof(tmp_addr)) == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       addr = talloc_strdup(cconn, tmp_addr);
-       if (addr == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-       return ctdbd_register_ips(cconn, srv, clnt, release_ip, addr);
-}
-
-#endif
-
 static bool uid_in_use(const struct user_struct *user, uid_t uid)
 {
        while (user) {
@@ -3153,12 +3215,63 @@ static void smbd_id_cache_kill(struct messaging_context *msg_ctx,
 NTSTATUS smbXsrv_connection_init_tables(struct smbXsrv_connection *conn,
                                        enum protocol_types protocol)
 {
+       NTSTATUS status;
+
        set_Protocol(protocol);
        conn->protocol = protocol;
 
+       if (protocol >= PROTOCOL_SMB2_02) {
+               status = smb2srv_session_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb2srv_open_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       } else {
+               status = smb1srv_session_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb1srv_tcon_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+
+               status = smb1srv_open_table_init(conn);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
        return NT_STATUS_OK;
 }
 
+static void smbd_tevent_trace_callback(enum tevent_trace_point point,
+                                      void *private_data)
+{
+       struct smbXsrv_connection *conn =
+               talloc_get_type_abort(private_data,
+               struct smbXsrv_connection);
+
+       switch (point) {
+       case TEVENT_TRACE_BEFORE_WAIT:
+               /*
+                * This just removes compiler warning
+                * without profile support
+                */
+               conn->smbd_idle_profstamp = 0;
+               START_PROFILE_STAMP(smbd_idle, conn->smbd_idle_profstamp);
+               break;
+       case TEVENT_TRACE_AFTER_WAIT:
+               END_PROFILE_STAMP(smbd_idle, conn->smbd_idle_profstamp);
+               break;
+       }
+}
+
 /****************************************************************************
  Process commands from the client
 ****************************************************************************/
@@ -3361,16 +3474,17 @@ void smbd_process(struct tevent_context *ev_ctx,
                exit_server("Could not open account policy tdb.\n");
        }
 
-       if (*lp_rootdir()) {
-               if (chroot(lp_rootdir()) != 0) {
-                       DEBUG(0,("Failed to change root to %s\n", lp_rootdir()));
+       if (*lp_rootdir(talloc_tos())) {
+               if (chroot(lp_rootdir(talloc_tos())) != 0) {
+                       DEBUG(0,("Failed to change root to %s\n",
+                                lp_rootdir(talloc_tos())));
                        exit_server("Failed to chroot()");
                }
                if (chdir("/") == -1) {
-                       DEBUG(0,("Failed to chdir to / on chroot to %s\n", lp_rootdir()));
+                       DEBUG(0,("Failed to chdir to / on chroot to %s\n", lp_rootdir(talloc_tos())));
                        exit_server("Failed to chroot()");
                }
-               DEBUG(0,("Changed root to %s\n", lp_rootdir()));
+               DEBUG(0,("Changed root to %s\n", lp_rootdir(talloc_tos())));
        }
 
        if (!srv_init_signing(sconn)) {
@@ -3466,15 +3580,12 @@ void smbd_process(struct tevent_context *ev_ctx,
 
        sconn->nbt.got_session = false;
 
-       sconn->smb1.negprot.max_recv = MIN(lp_maxxmit(),BUFFER_SIZE);
+       sconn->smb1.negprot.max_recv = MIN(lp_max_xmit(),BUFFER_SIZE);
 
        sconn->smb1.sessions.done_sesssetup = false;
        sconn->smb1.sessions.max_send = BUFFER_SIZE;
        sconn->smb1.sessions.last_session_tag = UID_FIELD_INVALID;
-       /* this holds info on user ids that are already validated for this VC */
-       sconn->smb1.sessions.next_vuid = VUID_OFFSET;
 
-       conn_init(sconn);
        if (!init_dptrs(sconn)) {
                exit_server("init_dptrs() failed");
        }
@@ -3496,6 +3607,8 @@ void smbd_process(struct tevent_context *ev_ctx,
 
        TALLOC_FREE(frame);
 
+       tevent_set_trace_callback(ev_ctx, smbd_tevent_trace_callback, conn);
+
        while (True) {
                frame = talloc_stackframe_pool(8192);