s3-talloc Change TALLOC_ZERO_P() to talloc_zero()
[nivanova/samba-autobuild/.git] / source3 / smbd / conn.c
index 38fa2e023769d9db2fbc0c6a01576bee8d86cc43..a3f66b36be7f5b50e614354edcf3aca544c9e57a 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 2 of the License, or
+   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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "smbd/globals.h"
 
-/* set these to define the limits of the server. NOTE These are on a
-   per-client basis. Thus any one machine can't connect to more than
-   MAX_CONNECTIONS services, but any number of machines may connect at
-   one time. */
-#define MAX_CONNECTIONS 128
+/* 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.
+ */
 
-static connection_struct *Connections;
-
-/* number of open connections */
-static struct bitmap *bmap;
-static int num_open;
+#define BITMAP_BLOCK_SZ 128
 
 /****************************************************************************
-init the conn structures
+ Init the conn structures.
 ****************************************************************************/
-void conn_init(void)
+
+void conn_init(struct smbd_server_connection *sconn)
 {
-       bmap = bitmap_allocate(MAX_CONNECTIONS);
+       sconn->smb1.tcons.Connections = NULL;
+       sconn->smb1.tcons.bmap = bitmap_talloc(sconn, BITMAP_BLOCK_SZ);
 }
 
 /****************************************************************************
-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_tcons_open;
 }
 
-
 /****************************************************************************
-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) {
-               if (conn->service == snum) {
-                       return(True);
+       if (sconn->using_smb2) {
+               /* SMB2 */
+               struct smbd_smb2_session *sess;
+               for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
+                       struct smbd_smb2_tcon *ptcon;
+
+                       for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
+                               if (ptcon->compat_conn &&
+                                               ptcon->compat_conn->params &&
+                                               (ptcon->compat_conn->params->service = snum)) {
+                                       return true;
+                               }
+                       }
+               }
+       } else {
+               /* SMB1 */
+               connection_struct *conn;
+               for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
+                       if (conn->params->service == snum) {
+                               return true;
+                       }
                }
        }
-       return(False);
+       return false;
 }
 
-
 /****************************************************************************
-find a conn given a cnum
+ Find a conn given a cnum.
 ****************************************************************************/
-connection_struct *conn_find(unsigned cnum)
-{
-       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);
+connection_struct *conn_find(struct smbd_server_connection *sconn,unsigned cnum)
+{
+       if (sconn->using_smb2) {
+               /* SMB2 */
+               struct smbd_smb2_session *sess;
+               for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
+                       struct smbd_smb2_tcon *ptcon;
+
+                       for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
+                               if (ptcon->compat_conn &&
+                                               ptcon->compat_conn->cnum == cnum) {
+                                       return ptcon->compat_conn;
+                               }
+                       }
+               }
+       } else {
+               /* SMB1 */
+               int count=0;
+               connection_struct *conn;
+               for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next,count++) {
+                       if (conn->cnum == cnum) {
+                               if (count > 10) {
+                                       DLIST_PROMOTE(sconn->smb1.tcons.Connections,
+                                               conn);
+                               }
+                               return conn;
                        }
-                       return conn;
                }
        }
 
        return NULL;
 }
 
-
 /****************************************************************************
 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 dieing and clients
+ thinking the server is still available.
 ****************************************************************************/
-connection_struct *conn_new(void)
+
+connection_struct *conn_new(struct smbd_server_connection *sconn)
 {
        connection_struct *conn;
        int i;
+        int find_offset = 1;
+
+       if (sconn->using_smb2) {
+               /* SMB2 */
+               if (!(conn=talloc_zero(NULL, connection_struct)) ||
+                   !(conn->params = talloc(conn, struct share_params))) {
+                       DEBUG(0,("TALLOC_ZERO() failed!\n"));
+                       TALLOC_FREE(conn);
+                       return NULL;
+               }
+               conn->sconn = sconn;
+               return conn;
+       }
+
+       /* SMB1 */
+find_again:
+       i = bitmap_find(sconn->smb1.tcons.bmap, find_offset);
 
-       i = bitmap_find(bmap, 1);
-       
        if (i == -1) {
-               DEBUG(1,("ERROR! Out of connection structures\n"));            
-               return NULL;
+                /* Expand the connections bitmap. */
+                int             oldsz = sconn->smb1.tcons.bmap->n;
+                int             newsz = sconn->smb1.tcons.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_talloc(sconn, newsz);
+               if (!nbmap) {
+                       DEBUG(0,("ERROR! malloc fail.\n"));
+                       return NULL;
+               }
+
+                bitmap_copy(nbmap, sconn->smb1.tcons.bmap);
+               TALLOC_FREE(sconn->smb1.tcons.bmap);
+
+                sconn->smb1.tcons.bmap = nbmap;
+                find_offset = oldsz; /* Start next search in the new portion. */
+
+                goto find_again;
        }
 
-       conn = (connection_struct *)malloc(sizeof(*conn));
-       if (!conn) 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"));
+               return NULL;
+       }
 
-       ZERO_STRUCTP(conn);
+       if (!(conn=talloc_zero(NULL, connection_struct)) ||
+           !(conn->params = talloc(conn, struct share_params))) {
+               DEBUG(0,("TALLOC_ZERO() failed!\n"));
+               TALLOC_FREE(conn);
+               return NULL;
+       }
+       conn->sconn = sconn;
        conn->cnum = i;
+       conn->force_group_gid = (gid_t)-1;
 
-       bitmap_set(bmap, i);
+       bitmap_set(sconn->smb1.tcons.bmap, i);
 
-       num_open++;
+       sconn->num_tcons_open++;
 
-       string_set(&conn->user,"");
-       string_set(&conn->dirpath,"");
        string_set(&conn->connectpath,"");
        string_set(&conn->origpath,"");
-       
-       DLIST_ADD(Connections, conn);
 
-       return conn;
-}
+       DLIST_ADD(sconn->smb1.tcons.Connections, conn);
 
-/****************************************************************************
-close all conn structures
-****************************************************************************/
-void conn_close_all(void)
-{
-       connection_struct *conn, *next;
-       for (conn=Connections;conn;conn=next) {
-               next=conn->next;
-               close_cnum(conn, conn->vuid);
-       }
+       return conn;
 }
 
 /****************************************************************************
- Idle inactive connections.
+ Clear a vuid out of the connection's vuid cache
 ****************************************************************************/
 
-BOOL conn_idle_all(time_t t, int deadtime)
+static void conn_clear_vuid_cache(connection_struct *conn, uint16_t vuid)
 {
-       pipes_struct *plist = NULL;
-       BOOL allidle = True;
-       connection_struct *conn, *next;
-
-       for (conn=Connections;conn;conn=next) {
-               next=conn->next;
-               /* close dirptrs on connections that are idle */
-               if ((t-conn->lastused) > DPTR_IDLE_TIMEOUT)
-                       dptr_idlecnum(conn);
-
-               if (conn->num_files_open > 0 || 
-                   (t-conn->lastused)<deadtime)
-                       allidle = 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)
-                       allidle = False;
-       
-       return allidle;
+       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;
+               }
+       }
 }
 
 /****************************************************************************
-clear a vuid out of the validity cache, and as the 'owner' of a connection.
+ 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,uint16_t vuid)
 {
        connection_struct *conn;
-       unsigned int i;
 
-       for (conn=Connections;conn;conn=conn->next) {
-               if (conn->vuid == vuid) {
-                       conn->vuid = UID_FIELD_INVALID;
+       if (sconn->using_smb2) {
+               /* SMB2 */
+               struct smbd_smb2_session *sess;
+               for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
+                       struct smbd_smb2_tcon *ptcon;
+
+                       for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
+                               if (ptcon->compat_conn) {
+                                       if (ptcon->compat_conn->vuid == vuid) {
+                                               ptcon->compat_conn->vuid = UID_FIELD_INVALID;
+                                       }
+                                       conn_clear_vuid_cache(ptcon->compat_conn, vuid);
+                               }
+                       }
                }
-
-               for (i=0;i<conn->vuid_cache.entries && i< VUID_CACHE_SIZE;i++) {
-                       if (conn->vuid_cache.list[i] == vuid) {
-                               conn->vuid_cache.list[i] = UID_FIELD_INVALID;
+       } else {
+               /* SMB1 */
+               for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
+                       if (conn->vuid == vuid) {
+                               conn->vuid = UID_FIELD_INVALID;
                        }
+                       conn_clear_vuid_cache(conn, vuid);
                }
        }
 }
 
 /****************************************************************************
- Free a conn structure.
+ Free a conn structure - internal part.
 ****************************************************************************/
 
-void conn_free(connection_struct *conn)
+static void conn_free_internal(connection_struct *conn)
 {
-       smb_vfs_handle_struct *handle, *thandle;
-       void (*done_fptr)(connection_struct *the_conn);
+       vfs_handle_struct *handle = NULL, *thandle = NULL;
+       struct trans_state *state = NULL;
 
        /* Free vfs_connection_struct */
-       handle = conn->vfs_private;
+       handle = conn->vfs_handles;
        while(handle) {
-               /* Close dlopen() handle */
-               done_fptr = (void (*)(connection_struct *))sys_dlsym(handle->handle, "vfs_done");
-               if (done_fptr == NULL) {
-                       DEBUG(3, ("No vfs_done() symbol found in module with handle %p, ignoring\n", handle->handle));
-               } else {
-                       done_fptr(conn);
-               }
-               sys_dlclose(handle->handle);
-               DLIST_REMOVE(conn->vfs_private, handle);
                thandle = handle->next;
-               SAFE_FREE(handle);
+               DLIST_REMOVE(conn->vfs_handles, handle);
+               if (handle->free_data)
+                       handle->free_data(&handle->data);
                handle = thandle;
        }
 
-       DLIST_REMOVE(Connections, conn);
-
-       if (conn->ngroups && conn->groups) {
-               SAFE_FREE(conn->groups);
-               conn->ngroups = 0;
+       /* Free any pending transactions stored on this conn. */
+       for (state = conn->pending_trans; state; state = state->next) {
+               /* state->setup is a talloc child of state. */
+               SAFE_FREE(state->param);
+               SAFE_FREE(state->data);
        }
 
        free_namearray(conn->veto_list);
        free_namearray(conn->hide_list);
        free_namearray(conn->veto_oplock_list);
-       
-       string_free(&conn->user);
-       string_free(&conn->dirpath);
+       free_namearray(conn->aio_write_behind_list);
+
        string_free(&conn->connectpath);
        string_free(&conn->origpath);
 
-       bitmap_clear(bmap, conn->cnum);
-       num_open--;
-
        ZERO_STRUCTP(conn);
-       SAFE_FREE(conn);
+       talloc_destroy(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
+ Free a conn structure.
 ****************************************************************************/
-void msg_force_tdis(int msg_type, pid_t pid, void *buf, size_t len)
-{
-       connection_struct *conn, *next;
-       fstring sharename;
 
-       fstrcpy(sharename, buf);
+void conn_free(connection_struct *conn)
+{
+       if (conn->sconn == NULL) {
+               conn_free_internal(conn);
+               return;
+       }
 
-       if (strcmp(sharename, "*") == 0) {
-               DEBUG(1,("Forcing close of all shares\n"));
-               conn_close_all();
+       if (conn->sconn->using_smb2) {
+               /* SMB2 */
+               conn_free_internal(conn);
                return;
        }
 
-       for (conn=Connections;conn;conn=next) {
-               next=conn->next;
-               if (strequal(lp_servicename(conn->service), sharename)) {
-                       DEBUG(1,("Forcing close of share %s cnum=%d\n",
-                                sharename, conn->cnum));
-                       close_cnum(conn, (uint16)-1);
-               }
+       /* SMB1 */
+       DLIST_REMOVE(conn->sconn->smb1.tcons.Connections, conn);
+
+       if (conn->sconn->smb1.tcons.bmap != NULL) {
+               /*
+                * Can be NULL for fake connections created by
+                * create_conn_struct()
+                */
+               bitmap_clear(conn->sconn->smb1.tcons.bmap, conn->cnum);
        }
+
+       SMB_ASSERT(conn->sconn->num_tcons_open > 0);
+       conn->sconn->num_tcons_open--;
+
+       conn_free_internal(conn);
 }