smbd: Add conn_using_smb2()
[samba.git] / source3 / smbd / conn.c
index 3baf0cfaabb8ccaa545688eae2bc9cc24a4d707d..b7a745a951e85f2731ba25373ae4b915714b5df8 100644 (file)
-/* 
+/*
    Unix SMB/CIFS implementation.
    Manage connections_struct structures
    Copyright (C) Andrew Tridgell 1998
    Copyright (C) Alexander Bokovoy 2002
-   
+   Copyright (C) Jeremy Allison 2010
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
+#include "lib/util/bitmap.h"
 
-/* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
- * maximum size of the bitmap is the largest positive integer, but you will hit
- * the "max connections" limit, looong before that.
- */
-#define BITMAP_BLOCK_SZ 128
-
-static connection_struct *Connections;
-
-/* number of open connections */
-static struct bitmap *bmap;
-static int num_open;
+static void conn_free_internal(connection_struct *conn);
 
 /****************************************************************************
-init the conn structures
+ * Remove a conn struct from conn->sconn->connections
+ * if not already done.
 ****************************************************************************/
-void conn_init(void)
+
+static int conn_struct_destructor(connection_struct *conn)
 {
-       bmap = bitmap_allocate(BITMAP_BLOCK_SZ);
+        if (conn->sconn != NULL) {
+               DLIST_REMOVE(conn->sconn->connections, conn);
+               SMB_ASSERT(conn->sconn->num_connections > 0);
+               conn->sconn->num_connections--;
+               conn->sconn = NULL;
+       }
+       conn_free_internal(conn);
+       return 0;
 }
 
 /****************************************************************************
-return the number of open connections
+ Return the number of open connections.
 ****************************************************************************/
-int conn_num_open(void)
+
+int conn_num_open(struct smbd_server_connection *sconn)
 {
-       return num_open;
+       return sconn->num_connections;
 }
 
-
 /****************************************************************************
-check if a snum is in use
+ Check if a snum is in use.
 ****************************************************************************/
-bool conn_snum_used(int snum)
+
+bool conn_snum_used(struct smbd_server_connection *sconn,
+                   int snum)
 {
-       connection_struct *conn;
-       for (conn=Connections;conn;conn=conn->next) {
+       struct connection_struct *conn;
+
+       for (conn=sconn->connections; conn; conn=conn->next) {
                if (conn->params->service == snum) {
-                       return(True);
+                       return true;
                }
        }
-       return(False);
-}
 
+       return false;
+}
 
-/****************************************************************************
-find a conn given a cnum
-****************************************************************************/
-connection_struct *conn_find(unsigned cnum)
+enum protocol_types conn_protocol(struct smbd_server_connection *sconn)
 {
-       int count=0;
-       connection_struct *conn;
-
-       for (conn=Connections;conn;conn=conn->next,count++) {
-               if (conn->cnum == cnum) {
-                       if (count > 10) {
-                               DLIST_PROMOTE(Connections, conn);
-                       }
-                       return conn;
-               }
+       if ((sconn != NULL) &&
+           (sconn->client != NULL) &&
+           (sconn->client->connections != NULL)) {
+               return sconn->client->connections->protocol;
        }
-
-       return NULL;
+       /*
+        * Default to what source3/lib/util.c has as default for the
+        * static Protocol variable to not change behaviour.
+        */
+       return PROTOCOL_COREPLUS;
 }
 
+bool conn_using_smb2(struct smbd_server_connection *sconn)
+{
+       enum protocol_types proto = conn_protocol(sconn);
+       return (proto >= PROTOCOL_SMB2_02);
+}
 
 /****************************************************************************
 find first available connection slot, starting from a random position.
-The randomisation stops problems with the server dieing and clients
-thinking the server is still available.
Find first available connection slot, starting from a random position.
+ The randomisation stops problems with the server dying and clients
+ thinking the server is still available.
 ****************************************************************************/
-connection_struct *conn_new(void)
-{
-       connection_struct *conn;
-       int i;
-        int find_offset = 1;
-
-find_again:
-       i = bitmap_find(bmap, find_offset);
-       
-       if (i == -1) {
-                /* Expand the connections bitmap. */
-                int             oldsz = bmap->n;
-                int             newsz = bmap->n + BITMAP_BLOCK_SZ;
-                struct bitmap * nbmap;
-
-                if (newsz <= oldsz) {
-                        /* Integer wrap. */
-                       DEBUG(0,("ERROR! Out of connection structures\n"));
-                        return NULL;
-                }
-
-               DEBUG(4,("resizing connections bitmap from %d to %d\n",
-                        oldsz, newsz));
-
-                nbmap = bitmap_allocate(newsz);
-               if (!nbmap) {
-                       DEBUG(0,("ERROR! malloc fail.\n"));
-                       return NULL;
-               }
 
-                bitmap_copy(nbmap, bmap);
-                bitmap_free(bmap);
-
-                bmap = nbmap;
-                find_offset = oldsz; /* Start next search in the new portion. */
+connection_struct *conn_new(struct smbd_server_connection *sconn)
+{
+       connection_struct *conn = NULL;
 
-                goto find_again;
+       conn = talloc_zero(NULL, connection_struct);
+       if (conn == NULL) {
+               DBG_ERR("talloc_zero failed\n");
+               return NULL;
        }
-
-       /* The bitmap position is used below as the connection number
-        * conn->cnum). This ends up as the TID field in the SMB header,
-        * which is limited to 16 bits (we skip 0xffff which is the
-        * NULL TID).
-        */
-       if (i > 65534) {
-               DEBUG(0, ("Maximum connection limit reached\n"));
+       conn->params = talloc(conn, struct share_params);
+       if (conn->params == NULL) {
+               DBG_ERR("talloc_zero failed\n");
+               TALLOC_FREE(conn);
                return NULL;
        }
-
-       if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
-           !(conn->params = TALLOC_P(conn, struct share_params))) {
-               DEBUG(0,("TALLOC_ZERO() failed!\n"));
+       conn->vuid_cache = talloc_zero(conn, struct vuid_cache);
+       if (conn->vuid_cache == NULL) {
+               DBG_ERR("talloc_zero failed\n");
                TALLOC_FREE(conn);
                return NULL;
        }
-       conn->cnum = i;
-
-       bitmap_set(bmap, i);
-
-       num_open++;
+       conn->connectpath = talloc_strdup(conn, "");
+       if (conn->connectpath == NULL) {
+               DBG_ERR("talloc_zero failed\n");
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+       conn->cwd_fsp = talloc_zero(conn, struct files_struct);
+       if (conn->cwd_fsp == NULL) {
+               DBG_ERR("talloc_zero failed\n");
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+       conn->cwd_fsp->fsp_name = synthetic_smb_fname(conn->cwd_fsp,
+                                                     ".",
+                                                     NULL,
+                                                     NULL,
+                                                     0,
+                                                     0);
+       if (conn->cwd_fsp->fsp_name == NULL) {
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+       conn->cwd_fsp->fh = fd_handle_create(conn->cwd_fsp);
+       if (conn->cwd_fsp->fh == NULL) {
+               DBG_ERR("talloc_zero failed\n");
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+       conn->sconn = sconn;
+       conn->force_group_gid = (gid_t)-1;
+       fsp_set_fd(conn->cwd_fsp, -1);
+       conn->cwd_fsp->fnum = FNUM_FIELD_INVALID;
+       conn->cwd_fsp->conn = conn;
 
-       string_set(&conn->user,"");
-       string_set(&conn->dirpath,"");
-       string_set(&conn->connectpath,"");
-       string_set(&conn->origpath,"");
-       
-       DLIST_ADD(Connections, conn);
+       DLIST_ADD(sconn->connections, conn);
+       sconn->num_connections++;
 
+       /*
+        * Catches the case where someone forgets to call
+        * conn_free().
+        */
+       talloc_set_destructor(conn, conn_struct_destructor);
        return conn;
 }
 
 /****************************************************************************
- Close all conn structures.
-****************************************************************************/
-
-void conn_close_all(void)
-{
-       connection_struct *conn, *next;
-       for (conn=Connections;conn;conn=next) {
-               next=conn->next;
-               set_current_service(conn, 0, True);
-               close_cnum(conn, conn->vuid);
-       }
-}
-
-/****************************************************************************
- Idle inactive connections.
+ Clear a vuid out of the connection's vuid cache
 ****************************************************************************/
 
-bool conn_idle_all(time_t t)
+static void conn_clear_vuid_cache(connection_struct *conn, uint64_t vuid)
 {
-       int deadtime = lp_deadtime()*60;
-       pipes_struct *plist = NULL;
-       connection_struct *conn;
-
-       if (deadtime <= 0)
-               deadtime = DEFAULT_SMBD_TIMEOUT;
-
-       for (conn=Connections;conn;conn=conn->next) {
-
-               time_t age = t - conn->lastused;
-
-               /* Update if connection wasn't idle. */
-               if (conn->lastused != conn->lastused_count) {
-                       conn->lastused = t;
-                       conn->lastused_count = t;
-               }
-
-               /* close dirptrs on connections that are idle */
-               if (age > DPTR_IDLE_TIMEOUT) {
-                       dptr_idlecnum(conn);
-               }
-
-               if (conn->num_files_open > 0 || age < deadtime) {
-                       return False;
-               }
-       }
-
-       /*
-        * Check all pipes for any open handles. We cannot
-        * idle with a handle open.
-        */
+       int i;
 
-       for (plist = get_first_internal_pipe(); plist;
-            plist = get_next_internal_pipe(plist)) {
-               if (plist->pipe_handles && plist->pipe_handles->count) {
-                       return False;
+       for (i=0; i<VUID_CACHE_SIZE; i++) {
+               struct vuid_cache_entry *ent;
+
+               ent = &conn->vuid_cache->array[i];
+
+               if (ent->vuid == vuid) {
+                       ent->vuid = UID_FIELD_INVALID;
+                       /*
+                        * We need to keep conn->session_info around
+                        * if it's equal to ent->session_info as a SMBulogoff
+                        * is often followed by a SMBtdis (with an invalid
+                        * vuid). The debug code (or regular code in
+                        * vfs_full_audit) wants to refer to the
+                        * conn->session_info pointer to print debug
+                        * statements. Theoretically this is a bug,
+                        * as once the vuid is gone the session_info
+                        * on the conn struct isn't valid any more,
+                        * but there's enough code that assumes
+                        * conn->session_info is never null that
+                        * it's easier to hold onto the old pointer
+                        * until we get a new sessionsetupX.
+                        * As everything is hung off the
+                        * conn pointer as a talloc context we're not
+                        * leaking memory here. See bug #6315. JRA.
+                        */
+                       if (conn->session_info == ent->session_info) {
+                               ent->session_info = NULL;
+                       } else {
+                               TALLOC_FREE(ent->session_info);
+                       }
+                       ent->read_only = False;
+                       ent->share_access = 0;
                }
        }
-       
-       return True;
 }
 
 /****************************************************************************
  Clear a vuid out of the validity cache, and as the 'owner' of a connection.
+
+ Called from invalidate_vuid()
 ****************************************************************************/
 
-void conn_clear_vuid_cache(uint16 vuid)
+void conn_clear_vuid_caches(struct smbd_server_connection *sconn, uint64_t vuid)
 {
        connection_struct *conn;
-       unsigned int i;
 
-       for (conn=Connections;conn;conn=conn->next) {
+       for (conn=sconn->connections; conn;conn=conn->next) {
                if (conn->vuid == vuid) {
                        conn->vuid = UID_FIELD_INVALID;
                }
-
-               for (i=0; i<VUID_CACHE_SIZE; i++) {
-                       struct vuid_cache_entry *ent;
-
-                       ent = &conn->vuid_cache.array[i];
-
-                       if (ent->vuid == vuid) {
-                               ent->vuid = UID_FIELD_INVALID;
-                               ent->read_only = False;
-                               ent->admin_user = False;
-                       }
-               }
+               conn_clear_vuid_cache(conn, vuid);
        }
 }
 
@@ -255,16 +228,16 @@ void conn_clear_vuid_cache(uint16 vuid)
  Free a conn structure - internal part.
 ****************************************************************************/
 
-void conn_free_internal(connection_struct *conn)
+static void conn_free_internal(connection_struct *conn)
 {
-       vfs_handle_struct *handle = NULL, *thandle = NULL;
+       vfs_handle_struct *handle = NULL, *thandle = NULL;
        struct trans_state *state = NULL;
 
        /* Free vfs_connection_struct */
        handle = conn->vfs_handles;
        while(handle) {
-               DLIST_REMOVE(conn->vfs_handles, handle);
                thandle = handle->next;
+               DLIST_REMOVE(conn->vfs_handles, handle);
                if (handle->free_data)
                        handle->free_data(&handle->data);
                handle = thandle;
@@ -281,14 +254,8 @@ void conn_free_internal(connection_struct *conn)
        free_namearray(conn->hide_list);
        free_namearray(conn->veto_oplock_list);
        free_namearray(conn->aio_write_behind_list);
-       
-       string_free(&conn->user);
-       string_free(&conn->dirpath);
-       string_free(&conn->connectpath);
-       string_free(&conn->origpath);
 
        ZERO_STRUCTP(conn);
-       talloc_destroy(conn);
 }
 
 /****************************************************************************
@@ -297,45 +264,24 @@ void conn_free_internal(connection_struct *conn)
 
 void conn_free(connection_struct *conn)
 {
-       DLIST_REMOVE(Connections, conn);
-
-       bitmap_clear(bmap, conn->cnum);
-
-       SMB_ASSERT(num_open > 0);
-       num_open--;
-
-       conn_free_internal(conn);
+       TALLOC_FREE(conn);
 }
-/****************************************************************************
-receive a smbcontrol message to forcibly unmount a share
-the message contains just a share name and all instances of that
-share are unmounted
-the special sharename '*' forces unmount of all shares
-****************************************************************************/
-void msg_force_tdis(struct messaging_context *msg,
-                   void *private_data,
-                   uint32_t msg_type,
-                   struct server_id server_id,
-                   DATA_BLOB *data)
-{
-       connection_struct *conn, *next;
-       fstring sharename;
-
-       fstrcpy(sharename, (const char *)data->data);
 
-       if (strcmp(sharename, "*") == 0) {
-               DEBUG(1,("Forcing close of all shares\n"));
-               conn_close_all();
-               return;
+/*
+ * Correctly initialize a share with case options.
+ */
+void conn_setup_case_options(connection_struct *conn)
+{
+       int snum = conn->params->service;
+
+       if (lp_case_sensitive(snum) == Auto) {
+               /* We will be setting this per packet. Set to be case
+               * insensitive for now. */
+               conn->case_sensitive = false;
+       } else {
+               conn->case_sensitive = (bool)lp_case_sensitive(snum);
        }
 
-       for (conn=Connections;conn;conn=next) {
-               next=conn->next;
-               if (strequal(lp_servicename(SNUM(conn)), sharename)) {
-                       DEBUG(1,("Forcing close of share %s cnum=%d\n",
-                                sharename, conn->cnum));
-                       close_cnum(conn, (uint16)-1);
-               }
-       }
+       conn->case_preserve = lp_preserve_case(snum);
+       conn->short_case_preserve = lp_short_preserve_case(snum);
 }