Merge branch 'v3-3-test' of git://git.samba.org/samba into 3.3-test
[abartlet/samba.git/.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
23 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
24  * maximum size of the bitmap is the largest positive integer, but you will hit
25  * the "max connections" limit, looong before that.
26  */
27 #define BITMAP_BLOCK_SZ 128
28
29 static connection_struct *Connections;
30
31 /* number of open connections */
32 static struct bitmap *bmap;
33 static int num_open;
34
35 /****************************************************************************
36 init the conn structures
37 ****************************************************************************/
38 void conn_init(void)
39 {
40         bmap = bitmap_allocate(BITMAP_BLOCK_SZ);
41 }
42
43 /****************************************************************************
44 return the number of open connections
45 ****************************************************************************/
46 int conn_num_open(void)
47 {
48         return num_open;
49 }
50
51
52 /****************************************************************************
53 check if a snum is in use
54 ****************************************************************************/
55 bool conn_snum_used(int snum)
56 {
57         connection_struct *conn;
58         for (conn=Connections;conn;conn=conn->next) {
59                 if (conn->params->service == snum) {
60                         return(True);
61                 }
62         }
63         return(False);
64 }
65
66 /****************************************************************************
67  Find a conn given a cnum.
68 ****************************************************************************/
69
70 connection_struct *conn_find(unsigned cnum)
71 {
72         int count=0;
73         connection_struct *conn;
74
75         for (conn=Connections;conn;conn=conn->next,count++) {
76                 if (conn->cnum == cnum) {
77                         if (count > 10) {
78                                 DLIST_PROMOTE(Connections, conn);
79                         }
80                         return conn;
81                 }
82         }
83
84         return NULL;
85 }
86
87 /****************************************************************************
88  Find a conn given a service name.
89 ****************************************************************************/
90
91 connection_struct *conn_find_byname(const char *service)
92 {
93         int count=0;
94         connection_struct *conn;
95
96         for (conn=Connections;conn;conn=conn->next,count++) {
97                 if (strequal(lp_servicename(SNUM(conn)),service)) {
98                         if (count > 10) {
99                                 DLIST_PROMOTE(Connections, conn);
100                         }
101                         return conn;
102                 }
103         }
104
105         return NULL;
106 }
107
108
109 /****************************************************************************
110   find first available connection slot, starting from a random position.
111 The randomisation stops problems with the server dieing and clients
112 thinking the server is still available.
113 ****************************************************************************/
114 connection_struct *conn_new(void)
115 {
116         connection_struct *conn;
117         int i;
118         int find_offset = 1;
119
120 find_again:
121         i = bitmap_find(bmap, find_offset);
122         
123         if (i == -1) {
124                 /* Expand the connections bitmap. */
125                 int             oldsz = bmap->n;
126                 int             newsz = bmap->n + BITMAP_BLOCK_SZ;
127                 struct bitmap * nbmap;
128
129                 if (newsz <= oldsz) {
130                         /* Integer wrap. */
131                         DEBUG(0,("ERROR! Out of connection structures\n"));
132                         return NULL;
133                 }
134
135                 DEBUG(4,("resizing connections bitmap from %d to %d\n",
136                         oldsz, newsz));
137
138                 nbmap = bitmap_allocate(newsz);
139                 if (!nbmap) {
140                         DEBUG(0,("ERROR! malloc fail.\n"));
141                         return NULL;
142                 }
143
144                 bitmap_copy(nbmap, bmap);
145                 bitmap_free(bmap);
146
147                 bmap = nbmap;
148                 find_offset = oldsz; /* Start next search in the new portion. */
149
150                 goto find_again;
151         }
152
153         /* The bitmap position is used below as the connection number
154          * conn->cnum). This ends up as the TID field in the SMB header,
155          * which is limited to 16 bits (we skip 0xffff which is the
156          * NULL TID).
157          */
158         if (i > 65534) {
159                 DEBUG(0, ("Maximum connection limit reached\n"));
160                 return NULL;
161         }
162
163         if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
164             !(conn->params = TALLOC_P(conn, struct share_params))) {
165                 DEBUG(0,("TALLOC_ZERO() failed!\n"));
166                 TALLOC_FREE(conn);
167                 return NULL;
168         }
169         conn->cnum = i;
170
171         bitmap_set(bmap, i);
172
173         num_open++;
174
175         string_set(&conn->dirpath,"");
176         string_set(&conn->connectpath,"");
177         string_set(&conn->origpath,"");
178         
179         DLIST_ADD(Connections, conn);
180
181         return conn;
182 }
183
184 /****************************************************************************
185  Close all conn structures.
186 ****************************************************************************/
187
188 void conn_close_all(void)
189 {
190         connection_struct *conn, *next;
191         for (conn=Connections;conn;conn=next) {
192                 next=conn->next;
193                 set_current_service(conn, 0, True);
194                 close_cnum(conn, conn->vuid);
195         }
196 }
197
198 /****************************************************************************
199  Idle inactive connections.
200 ****************************************************************************/
201
202 bool conn_idle_all(time_t t)
203 {
204         int deadtime = lp_deadtime()*60;
205         pipes_struct *plist = NULL;
206         connection_struct *conn;
207
208         if (deadtime <= 0)
209                 deadtime = DEFAULT_SMBD_TIMEOUT;
210
211         for (conn=Connections;conn;conn=conn->next) {
212
213                 time_t age = t - conn->lastused;
214
215                 /* Update if connection wasn't idle. */
216                 if (conn->lastused != conn->lastused_count) {
217                         conn->lastused = t;
218                         conn->lastused_count = t;
219                 }
220
221                 /* close dirptrs on connections that are idle */
222                 if (age > DPTR_IDLE_TIMEOUT) {
223                         dptr_idlecnum(conn);
224                 }
225
226                 if (conn->num_files_open > 0 || age < deadtime) {
227                         return False;
228                 }
229         }
230
231         /*
232          * Check all pipes for any open handles. We cannot
233          * idle with a handle open.
234          */
235
236         for (plist = get_first_internal_pipe(); plist;
237              plist = get_next_internal_pipe(plist)) {
238                 if (plist->pipe_handles && plist->pipe_handles->count) {
239                         return False;
240                 }
241         }
242         
243         return True;
244 }
245
246 /****************************************************************************
247  Clear a vuid out of the validity cache, and as the 'owner' of a connection.
248 ****************************************************************************/
249
250 void conn_clear_vuid_cache(uint16 vuid)
251 {
252         connection_struct *conn;
253         unsigned int i;
254
255         for (conn=Connections;conn;conn=conn->next) {
256                 if (conn->vuid == vuid) {
257                         conn->vuid = UID_FIELD_INVALID;
258                 }
259
260                 for (i=0; i<VUID_CACHE_SIZE; i++) {
261                         struct vuid_cache_entry *ent;
262
263                         ent = &conn->vuid_cache.array[i];
264
265                         if (ent->vuid == vuid) {
266                                 ent->vuid = UID_FIELD_INVALID;
267                                 TALLOC_FREE(ent->server_info);
268                                 ent->read_only = False;
269                                 ent->admin_user = False;
270                         }
271                 }
272         }
273 }
274
275 /****************************************************************************
276  Free a conn structure - internal part.
277 ****************************************************************************/
278
279 void conn_free_internal(connection_struct *conn)
280 {
281         vfs_handle_struct *handle = NULL, *thandle = NULL;
282         struct trans_state *state = NULL;
283
284         /* Free vfs_connection_struct */
285         handle = conn->vfs_handles;
286         while(handle) {
287                 DLIST_REMOVE(conn->vfs_handles, handle);
288                 thandle = handle->next;
289                 if (handle->free_data)
290                         handle->free_data(&handle->data);
291                 handle = thandle;
292         }
293
294         /* Free any pending transactions stored on this conn. */
295         for (state = conn->pending_trans; state; state = state->next) {
296                 /* state->setup is a talloc child of state. */
297                 SAFE_FREE(state->param);
298                 SAFE_FREE(state->data);
299         }
300
301         free_namearray(conn->veto_list);
302         free_namearray(conn->hide_list);
303         free_namearray(conn->veto_oplock_list);
304         free_namearray(conn->aio_write_behind_list);
305         
306         string_free(&conn->dirpath);
307         string_free(&conn->connectpath);
308         string_free(&conn->origpath);
309
310         ZERO_STRUCTP(conn);
311         talloc_destroy(conn);
312 }
313
314 /****************************************************************************
315  Free a conn structure.
316 ****************************************************************************/
317
318 void conn_free(connection_struct *conn)
319 {
320         DLIST_REMOVE(Connections, conn);
321
322         bitmap_clear(bmap, conn->cnum);
323
324         SMB_ASSERT(num_open > 0);
325         num_open--;
326
327         conn_free_internal(conn);
328 }
329  
330 /****************************************************************************
331 receive a smbcontrol message to forcibly unmount a share
332 the message contains just a share name and all instances of that
333 share are unmounted
334 the special sharename '*' forces unmount of all shares
335 ****************************************************************************/
336 void msg_force_tdis(struct messaging_context *msg,
337                     void *private_data,
338                     uint32_t msg_type,
339                     struct server_id server_id,
340                     DATA_BLOB *data)
341 {
342         connection_struct *conn, *next;
343         fstring sharename;
344
345         fstrcpy(sharename, (const char *)data->data);
346
347         if (strcmp(sharename, "*") == 0) {
348                 DEBUG(1,("Forcing close of all shares\n"));
349                 conn_close_all();
350                 return;
351         }
352
353         for (conn=Connections;conn;conn=next) {
354                 next=conn->next;
355                 if (strequal(lp_servicename(SNUM(conn)), sharename)) {
356                         DEBUG(1,("Forcing close of share %s cnum=%d\n",
357                                  sharename, conn->cnum));
358                         close_cnum(conn, (uint16)-1);
359                 }
360         }
361 }