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