r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.git] / source4 / smb_server / handle.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Manage smbsrv_handle structures
4    Copyright (C) Stefan Metzmacher 2006
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/util/dlinklist.h"
22 #include "smb_server/smb_server.h"
23 #include "ntvfs/ntvfs.h"
24
25
26 /****************************************************************************
27 init the handle structures
28 ****************************************************************************/
29 NTSTATUS smbsrv_init_handles(struct smbsrv_tcon *tcon, uint32_t limit)
30 {
31         /* 
32          * the idr_* functions take 'int' as limit,
33          * and only work with a max limit 0x00FFFFFF
34          */
35         limit &= 0x00FFFFFF;
36
37         tcon->handles.idtree_hid        = idr_init(tcon);
38         NT_STATUS_HAVE_NO_MEMORY(tcon->handles.idtree_hid);
39         tcon->handles.idtree_limit      = limit;
40         tcon->handles.list              = NULL;
41
42         return NT_STATUS_OK;
43 }
44
45 /****************************************************************************
46 find a handle given a handle id
47 ****************************************************************************/
48 static struct smbsrv_handle *smbsrv_handle_find(struct smbsrv_handles_context *handles_ctx,
49                                                 uint32_t hid, struct timeval request_time)
50 {
51         void *p;
52         struct smbsrv_handle *handle;
53
54         if (hid == 0) return NULL;
55
56         if (hid > handles_ctx->idtree_limit) return NULL;
57
58         p = idr_find(handles_ctx->idtree_hid, hid);
59         if (!p) return NULL;
60
61         handle = talloc_get_type(p, struct smbsrv_handle);
62         if (!handle) return NULL;
63
64         /* only give it away when the ntvfs subsystem has made the handle valid */
65         if (!handle->ntvfs) return NULL;
66
67         handle->statistics.last_use_time = request_time;
68
69         return handle;
70 }
71
72 struct smbsrv_handle *smbsrv_smb_handle_find(struct smbsrv_tcon *smb_tcon,
73                                              uint16_t fnum, struct timeval request_time)
74 {
75         return smbsrv_handle_find(&smb_tcon->handles, fnum, request_time);
76 }
77
78 struct smbsrv_handle *smbsrv_smb2_handle_find(struct smbsrv_tcon *smb_tcon,
79                                               uint32_t hid, struct timeval request_time)
80 {
81         return smbsrv_handle_find(&smb_tcon->handles, hid, request_time);
82 }
83
84 /*
85   destroy a connection structure
86 */
87 static int smbsrv_handle_destructor(struct smbsrv_handle *handle)
88 {
89         struct smbsrv_handles_context *handles_ctx;
90
91         handles_ctx = &handle->tcon->handles;
92
93         idr_remove(handles_ctx->idtree_hid, handle->hid);
94         DLIST_REMOVE(handles_ctx->list, handle);
95         DLIST_REMOVE(handle->session->handles, &handle->session_item);
96
97         /* tell the ntvfs backend that we are disconnecting */
98         if (handle->ntvfs) {
99                 talloc_free(handle->ntvfs);
100                 handle->ntvfs = NULL;
101         }
102
103         return 0;
104 }
105
106 /*
107   find first available handle slot
108 */
109 struct smbsrv_handle *smbsrv_handle_new(struct smbsrv_session *session,
110                                         struct smbsrv_tcon *tcon,
111                                         TALLOC_CTX *mem_ctx,
112                                         struct timeval request_time)
113 {
114         struct smbsrv_handles_context *handles_ctx = &tcon->handles;
115         struct smbsrv_handle *handle;
116         int i;
117
118         handle = talloc_zero(mem_ctx, struct smbsrv_handle);
119         if (!handle) return NULL;
120         handle->tcon    = tcon;
121         handle->session = session;
122         
123         i = idr_get_new_above(handles_ctx->idtree_hid, handle, 1, handles_ctx->idtree_limit);
124         if (i == -1) {
125                 DEBUG(1,("ERROR! Out of handle structures\n"));
126                 goto failed;
127         }
128         handle->hid = i;
129         handle->session_item.handle = handle;
130
131         DLIST_ADD(handles_ctx->list, handle);
132         DLIST_ADD(session->handles, &handle->session_item);
133         talloc_set_destructor(handle, smbsrv_handle_destructor);
134
135         /* now fill in some statistics */
136         handle->statistics.open_time            = request_time;
137         handle->statistics.last_use_time        = request_time;
138
139         return handle;
140
141 failed:
142         talloc_free(handle);
143         return NULL;
144 }