Merge branch 'master' of ssh://git.samba.org/data/git/samba
[tprouty/samba.git] / source3 / smbd / conn.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Manage connections_struct structures
4    Copyright (C) Andrew Tridgell 1998
5    Copyright (C) Alexander Bokovoy 2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/globals.h"
23
24 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
25  * maximum size of the bitmap is the largest positive integer, but you will hit
26  * the "max connections" limit, looong before that.
27  */
28 #define BITMAP_BLOCK_SZ 128
29
30 /****************************************************************************
31 init the conn structures
32 ****************************************************************************/
33 void conn_init(void)
34 {
35         bmap = bitmap_allocate(BITMAP_BLOCK_SZ);
36 }
37
38 /****************************************************************************
39 return the number of open connections
40 ****************************************************************************/
41 int conn_num_open(void)
42 {
43         return num_open;
44 }
45
46
47 /****************************************************************************
48 check if a snum is in use
49 ****************************************************************************/
50 bool conn_snum_used(int snum)
51 {
52         connection_struct *conn;
53         for (conn=Connections;conn;conn=conn->next) {
54                 if (conn->params->service == snum) {
55                         return(True);
56                 }
57         }
58         return(False);
59 }
60
61 /****************************************************************************
62  Find a conn given a cnum.
63 ****************************************************************************/
64
65 connection_struct *conn_find(unsigned cnum)
66 {
67         int count=0;
68         connection_struct *conn;
69
70         for (conn=Connections;conn;conn=conn->next,count++) {
71                 if (conn->cnum == cnum) {
72                         if (count > 10) {
73                                 DLIST_PROMOTE(Connections, conn);
74                         }
75                         return conn;
76                 }
77         }
78
79         return NULL;
80 }
81
82 /****************************************************************************
83   find first available connection slot, starting from a random position.
84 The randomisation stops problems with the server dieing and clients
85 thinking the server is still available.
86 ****************************************************************************/
87 connection_struct *conn_new(void)
88 {
89         connection_struct *conn;
90         int i;
91         int find_offset = 1;
92
93 find_again:
94         i = bitmap_find(bmap, find_offset);
95         
96         if (i == -1) {
97                 /* Expand the connections bitmap. */
98                 int             oldsz = bmap->n;
99                 int             newsz = bmap->n + BITMAP_BLOCK_SZ;
100                 struct bitmap * nbmap;
101
102                 if (newsz <= oldsz) {
103                         /* Integer wrap. */
104                         DEBUG(0,("ERROR! Out of connection structures\n"));
105                         return NULL;
106                 }
107
108                 DEBUG(4,("resizing connections bitmap from %d to %d\n",
109                         oldsz, newsz));
110
111                 nbmap = bitmap_allocate(newsz);
112                 if (!nbmap) {
113                         DEBUG(0,("ERROR! malloc fail.\n"));
114                         return NULL;
115                 }
116
117                 bitmap_copy(nbmap, bmap);
118                 bitmap_free(bmap);
119
120                 bmap = nbmap;
121                 find_offset = oldsz; /* Start next search in the new portion. */
122
123                 goto find_again;
124         }
125
126         /* The bitmap position is used below as the connection number
127          * conn->cnum). This ends up as the TID field in the SMB header,
128          * which is limited to 16 bits (we skip 0xffff which is the
129          * NULL TID).
130          */
131         if (i > 65534) {
132                 DEBUG(0, ("Maximum connection limit reached\n"));
133                 return NULL;
134         }
135
136         if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
137             !(conn->params = TALLOC_P(conn, struct share_params))) {
138                 DEBUG(0,("TALLOC_ZERO() failed!\n"));
139                 TALLOC_FREE(conn);
140                 return NULL;
141         }
142         conn->cnum = i;
143
144         bitmap_set(bmap, i);
145
146         num_open++;
147
148         string_set(&conn->dirpath,"");
149         string_set(&conn->connectpath,"");
150         string_set(&conn->origpath,"");
151         
152         DLIST_ADD(Connections, conn);
153
154         return conn;
155 }
156
157 /****************************************************************************
158  Close all conn structures.
159 return true if any were closed
160 ****************************************************************************/
161 bool conn_close_all(void)
162 {
163         connection_struct *conn, *next;
164         bool ret = false;
165         for (conn=Connections;conn;conn=next) {
166                 next=conn->next;
167                 set_current_service(conn, 0, True);
168                 close_cnum(conn, conn->vuid);
169                 ret = true;
170         }
171         return ret;
172 }
173
174 /****************************************************************************
175  Idle inactive connections.
176 ****************************************************************************/
177
178 bool conn_idle_all(time_t t)
179 {
180         int deadtime = lp_deadtime()*60;
181         pipes_struct *plist = NULL;
182         connection_struct *conn;
183
184         if (deadtime <= 0)
185                 deadtime = DEFAULT_SMBD_TIMEOUT;
186
187         for (conn=Connections;conn;conn=conn->next) {
188
189                 time_t age = t - conn->lastused;
190
191                 /* Update if connection wasn't idle. */
192                 if (conn->lastused != conn->lastused_count) {
193                         conn->lastused = t;
194                         conn->lastused_count = t;
195                 }
196
197                 /* close dirptrs on connections that are idle */
198                 if (age > DPTR_IDLE_TIMEOUT) {
199                         dptr_idlecnum(conn);
200                 }
201
202                 if (conn->num_files_open > 0 || age < deadtime) {
203                         return False;
204                 }
205         }
206
207         /*
208          * Check all pipes for any open handles. We cannot
209          * idle with a handle open.
210          */
211
212         for (plist = get_first_internal_pipe(); plist;
213              plist = get_next_internal_pipe(plist)) {
214                 if (plist->pipe_handles && plist->pipe_handles->count) {
215                         return False;
216                 }
217         }
218         
219         return True;
220 }
221
222 /****************************************************************************
223  Clear a vuid out of the validity cache, and as the 'owner' of a connection.
224 ****************************************************************************/
225
226 void conn_clear_vuid_caches(uint16_t vuid)
227 {
228         connection_struct *conn;
229
230         for (conn=Connections;conn;conn=conn->next) {
231                 if (conn->vuid == vuid) {
232                         conn->vuid = UID_FIELD_INVALID;
233                 }
234                 conn_clear_vuid_cache(conn, vuid);
235         }
236 }
237
238 /****************************************************************************
239  Free a conn structure - internal part.
240 ****************************************************************************/
241
242 void conn_free_internal(connection_struct *conn)
243 {
244         vfs_handle_struct *handle = NULL, *thandle = NULL;
245         struct trans_state *state = NULL;
246
247         /* Free vfs_connection_struct */
248         handle = conn->vfs_handles;
249         while(handle) {
250                 thandle = handle->next;
251                 DLIST_REMOVE(conn->vfs_handles, handle);
252                 if (handle->free_data)
253                         handle->free_data(&handle->data);
254                 handle = thandle;
255         }
256
257         /* Free any pending transactions stored on this conn. */
258         for (state = conn->pending_trans; state; state = state->next) {
259                 /* state->setup is a talloc child of state. */
260                 SAFE_FREE(state->param);
261                 SAFE_FREE(state->data);
262         }
263
264         free_namearray(conn->veto_list);
265         free_namearray(conn->hide_list);
266         free_namearray(conn->veto_oplock_list);
267         free_namearray(conn->aio_write_behind_list);
268         
269         string_free(&conn->dirpath);
270         string_free(&conn->connectpath);
271         string_free(&conn->origpath);
272
273         ZERO_STRUCTP(conn);
274         talloc_destroy(conn);
275 }
276
277 /****************************************************************************
278  Free a conn structure.
279 ****************************************************************************/
280
281 void conn_free(connection_struct *conn)
282 {
283         DLIST_REMOVE(Connections, conn);
284
285         bitmap_clear(bmap, conn->cnum);
286
287         SMB_ASSERT(num_open > 0);
288         num_open--;
289
290         conn_free_internal(conn);
291 }
292  
293 /****************************************************************************
294 receive a smbcontrol message to forcibly unmount a share
295 the message contains just a share name and all instances of that
296 share are unmounted
297 the special sharename '*' forces unmount of all shares
298 ****************************************************************************/
299 void msg_force_tdis(struct messaging_context *msg,
300                     void *private_data,
301                     uint32_t msg_type,
302                     struct server_id server_id,
303                     DATA_BLOB *data)
304 {
305         connection_struct *conn, *next;
306         fstring sharename;
307
308         fstrcpy(sharename, (const char *)data->data);
309
310         if (strcmp(sharename, "*") == 0) {
311                 DEBUG(1,("Forcing close of all shares\n"));
312                 conn_close_all();
313                 return;
314         }
315
316         for (conn=Connections;conn;conn=next) {
317                 next=conn->next;
318                 if (strequal(lp_servicename(SNUM(conn)), sharename)) {
319                         DEBUG(1,("Forcing close of share %s cnum=%d\n",
320                                  sharename, conn->cnum));
321                         close_cnum(conn, (uint16)-1);
322                 }
323         }
324 }