| 1 | /* 
 | 
|---|
| 2 |    Unix SMB/CIFS implementation.
 | 
|---|
| 3 |    Manage smbsrv_tcon structures
 | 
|---|
| 4 |    Copyright (C) Andrew Tridgell 1998
 | 
|---|
| 5 |    Copyright (C) Alexander Bokovoy 2002
 | 
|---|
| 6 |    Copyright (C) Stefan Metzmacher 2005-2006
 | 
|---|
| 7 |    
 | 
|---|
| 8 |    This program is free software; you can redistribute it and/or modify
 | 
|---|
| 9 |    it under the terms of the GNU General Public License as published by
 | 
|---|
| 10 |    the Free Software Foundation; either version 3 of the License, or
 | 
|---|
| 11 |    (at your option) any later version.
 | 
|---|
| 12 |    
 | 
|---|
| 13 |    This program is distributed in the hope that it will be useful,
 | 
|---|
| 14 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 15 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 16 |    GNU General Public License for more details.
 | 
|---|
| 17 |    
 | 
|---|
| 18 |    You should have received a copy of the GNU General Public License
 | 
|---|
| 19 |    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 20 | */
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "includes.h"
 | 
|---|
| 23 | #include "smb_server/smb_server.h"
 | 
|---|
| 24 | #include "smbd/service_stream.h"
 | 
|---|
| 25 | #include "ntvfs/ntvfs.h"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | struct socket_address *smbsrv_get_my_addr(void *p, TALLOC_CTX *mem_ctx)
 | 
|---|
| 28 | {
 | 
|---|
| 29 |         struct smbsrv_connection *smb_conn = talloc_get_type(p,
 | 
|---|
| 30 |                                              struct smbsrv_connection);
 | 
|---|
| 31 | 
 | 
|---|
| 32 |         return socket_get_my_addr(smb_conn->connection->socket, mem_ctx);
 | 
|---|
| 33 | }
 | 
|---|
| 34 | 
 | 
|---|
| 35 | struct socket_address *smbsrv_get_peer_addr(void *p, TALLOC_CTX *mem_ctx)
 | 
|---|
| 36 | {
 | 
|---|
| 37 |         struct smbsrv_connection *smb_conn = talloc_get_type(p,
 | 
|---|
| 38 |                                              struct smbsrv_connection);
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         return socket_get_peer_addr(smb_conn->connection->socket, mem_ctx);
 | 
|---|
| 41 | }
 | 
|---|
| 42 | 
 | 
|---|
| 43 | /****************************************************************************
 | 
|---|
| 44 | init the tcon structures
 | 
|---|
| 45 | ****************************************************************************/
 | 
|---|
| 46 | static NTSTATUS smbsrv_init_tcons(struct smbsrv_tcons_context *tcons_ctx, TALLOC_CTX *mem_ctx, uint32_t limit)
 | 
|---|
| 47 | {
 | 
|---|
| 48 |         /* 
 | 
|---|
| 49 |          * the idr_* functions take 'int' as limit,
 | 
|---|
| 50 |          * and only work with a max limit 0x00FFFFFF
 | 
|---|
| 51 |          */
 | 
|---|
| 52 |         limit &= 0x00FFFFFF;
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         tcons_ctx->idtree_tid   = idr_init(mem_ctx);
 | 
|---|
| 55 |         NT_STATUS_HAVE_NO_MEMORY(tcons_ctx->idtree_tid);
 | 
|---|
| 56 |         tcons_ctx->idtree_limit = limit;
 | 
|---|
| 57 |         tcons_ctx->list         = NULL;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         return NT_STATUS_OK;
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | NTSTATUS smbsrv_smb_init_tcons(struct smbsrv_connection *smb_conn)
 | 
|---|
| 63 | {
 | 
|---|
| 64 |         return smbsrv_init_tcons(&smb_conn->smb_tcons, smb_conn, UINT16_MAX);
 | 
|---|
| 65 | }
 | 
|---|
| 66 | 
 | 
|---|
| 67 | NTSTATUS smbsrv_smb2_init_tcons(struct smbsrv_session *smb_sess)
 | 
|---|
| 68 | {
 | 
|---|
| 69 |         return smbsrv_init_tcons(&smb_sess->smb2_tcons, smb_sess, UINT32_MAX);
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | /****************************************************************************
 | 
|---|
| 73 | find a tcon given a tid for SMB
 | 
|---|
| 74 | ****************************************************************************/
 | 
|---|
| 75 | static struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_tcons_context *tcons_ctx,
 | 
|---|
| 76 |                                             uint32_t tid, struct timeval request_time)
 | 
|---|
| 77 | {
 | 
|---|
| 78 |         void *p;
 | 
|---|
| 79 |         struct smbsrv_tcon *tcon;
 | 
|---|
| 80 | 
 | 
|---|
| 81 |         if (tid == 0) return NULL;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         if (tid > tcons_ctx->idtree_limit) return NULL;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         p = idr_find(tcons_ctx->idtree_tid, tid);
 | 
|---|
| 86 |         if (!p) return NULL;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         tcon = talloc_get_type(p, struct smbsrv_tcon);
 | 
|---|
| 89 |         if (!tcon) return NULL;
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         tcon->statistics.last_request_time = request_time;
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         return tcon;
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | struct smbsrv_tcon *smbsrv_smb_tcon_find(struct smbsrv_connection *smb_conn,
 | 
|---|
| 97 |                                          uint32_t tid, struct timeval request_time)
 | 
|---|
| 98 | {
 | 
|---|
| 99 |         return smbsrv_tcon_find(&smb_conn->smb_tcons, tid, request_time);
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | struct smbsrv_tcon *smbsrv_smb2_tcon_find(struct smbsrv_session *smb_sess,
 | 
|---|
| 103 |                                           uint32_t tid, struct timeval request_time)
 | 
|---|
| 104 | {
 | 
|---|
| 105 |         if (!smb_sess) return NULL;
 | 
|---|
| 106 |         return smbsrv_tcon_find(&smb_sess->smb2_tcons, tid, request_time);
 | 
|---|
| 107 | }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | /*
 | 
|---|
| 110 |   destroy a connection structure
 | 
|---|
| 111 | */
 | 
|---|
| 112 | static int smbsrv_tcon_destructor(struct smbsrv_tcon *tcon)
 | 
|---|
| 113 | {
 | 
|---|
| 114 |         struct smbsrv_tcons_context *tcons_ctx;
 | 
|---|
| 115 |         struct socket_address *client_addr;
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         client_addr = socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon);
 | 
|---|
| 118 |         DEBUG(3,("%s closed connection to service %s\n",
 | 
|---|
| 119 |                  client_addr ? client_addr->addr : "(unknown)",
 | 
|---|
| 120 |                  tcon->share_name));
 | 
|---|
| 121 | 
 | 
|---|
| 122 |         /* tell the ntvfs backend that we are disconnecting */
 | 
|---|
| 123 |         if (tcon->ntvfs) {
 | 
|---|
| 124 |                 ntvfs_disconnect(tcon->ntvfs);
 | 
|---|
| 125 |                 tcon->ntvfs = NULL;
 | 
|---|
| 126 |         }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         if (tcon->smb2.session) {
 | 
|---|
| 129 |                 tcons_ctx = &tcon->smb2.session->smb2_tcons;
 | 
|---|
| 130 |         } else {
 | 
|---|
| 131 |                 tcons_ctx = &tcon->smb_conn->smb_tcons;
 | 
|---|
| 132 |         }
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         idr_remove(tcons_ctx->idtree_tid, tcon->tid);
 | 
|---|
| 135 |         DLIST_REMOVE(tcons_ctx->list, tcon);
 | 
|---|
| 136 |         return 0;
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /*
 | 
|---|
| 140 |   find first available connection slot
 | 
|---|
| 141 | */
 | 
|---|
| 142 | static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn,
 | 
|---|
| 143 |                                            struct smbsrv_session *smb_sess,
 | 
|---|
| 144 |                                            const char *share_name)
 | 
|---|
| 145 | {
 | 
|---|
| 146 |         TALLOC_CTX *mem_ctx;
 | 
|---|
| 147 |         struct smbsrv_tcons_context *tcons_ctx;
 | 
|---|
| 148 |         uint32_t handle_uint_max;
 | 
|---|
| 149 |         struct smbsrv_tcon *tcon;
 | 
|---|
| 150 |         NTSTATUS status;
 | 
|---|
| 151 |         int i;
 | 
|---|
| 152 | 
 | 
|---|
| 153 |         if (smb_sess) {
 | 
|---|
| 154 |                 mem_ctx = smb_sess;
 | 
|---|
| 155 |                 tcons_ctx = &smb_sess->smb2_tcons;
 | 
|---|
| 156 |                 handle_uint_max = UINT32_MAX;
 | 
|---|
| 157 |         } else {
 | 
|---|
| 158 |                 mem_ctx = smb_conn;
 | 
|---|
| 159 |                 tcons_ctx = &smb_conn->smb_tcons;
 | 
|---|
| 160 |                 handle_uint_max = UINT16_MAX;
 | 
|---|
| 161 |         }
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         tcon = talloc_zero(mem_ctx, struct smbsrv_tcon);
 | 
|---|
| 164 |         if (!tcon) return NULL;
 | 
|---|
| 165 |         tcon->smb_conn          = smb_conn;
 | 
|---|
| 166 |         tcon->smb2.session      = smb_sess;
 | 
|---|
| 167 |         tcon->share_name        = talloc_strdup(tcon, share_name);
 | 
|---|
| 168 |         if (!tcon->share_name) goto failed;
 | 
|---|
| 169 | 
 | 
|---|
| 170 |         /*
 | 
|---|
| 171 |          * the use -1 here, because we don't want to give away the wildcard
 | 
|---|
| 172 |          * fnum used in SMBflush
 | 
|---|
| 173 |          */
 | 
|---|
| 174 |         status = smbsrv_init_handles(tcon, handle_uint_max - 1);
 | 
|---|
| 175 |         if (!NT_STATUS_IS_OK(status)) {
 | 
|---|
| 176 |                 DEBUG(1,("ERROR! failed to init handles: %s\n", nt_errstr(status)));
 | 
|---|
| 177 |                 goto failed;
 | 
|---|
| 178 |         }
 | 
|---|
| 179 | 
 | 
|---|
| 180 |         i = idr_get_new_random(tcons_ctx->idtree_tid, tcon, tcons_ctx->idtree_limit);
 | 
|---|
| 181 |         if (i == -1) {
 | 
|---|
| 182 |                 DEBUG(1,("ERROR! Out of connection structures\n"));
 | 
|---|
| 183 |                 goto failed;
 | 
|---|
| 184 |         }
 | 
|---|
| 185 |         tcon->tid = i;
 | 
|---|
| 186 | 
 | 
|---|
| 187 |         DLIST_ADD(tcons_ctx->list, tcon);
 | 
|---|
| 188 |         talloc_set_destructor(tcon, smbsrv_tcon_destructor);
 | 
|---|
| 189 | 
 | 
|---|
| 190 |         /* now fill in some statistics */
 | 
|---|
| 191 |         tcon->statistics.connect_time = timeval_current();
 | 
|---|
| 192 | 
 | 
|---|
| 193 |         return tcon;
 | 
|---|
| 194 | 
 | 
|---|
| 195 | failed:
 | 
|---|
| 196 |         talloc_free(tcon);
 | 
|---|
| 197 |         return NULL;
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | struct smbsrv_tcon *smbsrv_smb_tcon_new(struct smbsrv_connection *smb_conn, const char *share_name)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |         return smbsrv_tcon_new(smb_conn, NULL, share_name);
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | struct smbsrv_tcon *smbsrv_smb2_tcon_new(struct smbsrv_session *smb_sess, const char *share_name)
 | 
|---|
| 206 | {
 | 
|---|
| 207 |         return smbsrv_tcon_new(smb_sess->smb_conn, smb_sess, share_name);
 | 
|---|
| 208 | }
 | 
|---|