Add deadtime detection for SMB2. Correctly update lastused timestamp across all activ...
[nivanova/samba-autobuild/.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    Copyright (C) Jeremy Allison 2010
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 "smbd/globals.h"
24
25 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
26  * maximum size of the bitmap is the largest positive integer, but you will hit
27  * the "max connections" limit, looong before that.
28  */
29
30 #define BITMAP_BLOCK_SZ 128
31
32 /****************************************************************************
33  Init the conn structures.
34 ****************************************************************************/
35
36 void conn_init(struct smbd_server_connection *sconn)
37 {
38         sconn->smb1.tcons.Connections = NULL;
39         sconn->smb1.tcons.bmap = bitmap_talloc(sconn, BITMAP_BLOCK_SZ);
40 }
41
42 /****************************************************************************
43  Return the number of open connections.
44 ****************************************************************************/
45
46 int conn_num_open(struct smbd_server_connection *sconn)
47 {
48         return sconn->num_tcons_open;
49 }
50
51 /****************************************************************************
52  Check if a snum is in use.
53 ****************************************************************************/
54
55 bool conn_snum_used(int snum)
56 {
57         struct smbd_server_connection *sconn = smbd_server_conn;
58
59         if (sconn->using_smb2) {
60                 /* SMB2 */
61                 struct smbd_smb2_session *sess;
62                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
63                         struct smbd_smb2_tcon *ptcon;
64
65                         for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
66                                 if (ptcon->compat_conn &&
67                                                 ptcon->compat_conn->params &&
68                                                 (ptcon->compat_conn->params->service = snum)) {
69                                         return true;
70                                 }
71                         }
72                 }
73         } else {
74                 /* SMB1 */
75                 connection_struct *conn;
76                 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
77                         if (conn->params->service == snum) {
78                                 return true;
79                         }
80                 }
81         }
82         return false;
83 }
84
85 /****************************************************************************
86  Find a conn given a cnum.
87 ****************************************************************************/
88
89 connection_struct *conn_find(struct smbd_server_connection *sconn,unsigned cnum)
90 {
91         if (sconn->using_smb2) {
92                 /* SMB2 */
93                 struct smbd_smb2_session *sess;
94                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
95                         struct smbd_smb2_tcon *ptcon;
96
97                         for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
98                                 if (ptcon->compat_conn &&
99                                                 ptcon->compat_conn->cnum == cnum) {
100                                         return ptcon->compat_conn;
101                                 }
102                         }
103                 }
104         } else {
105                 /* SMB1 */
106                 int count=0;
107                 connection_struct *conn;
108                 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next,count++) {
109                         if (conn->cnum == cnum) {
110                                 if (count > 10) {
111                                         DLIST_PROMOTE(sconn->smb1.tcons.Connections,
112                                                 conn);
113                                 }
114                                 return conn;
115                         }
116                 }
117         }
118
119         return NULL;
120 }
121
122 /****************************************************************************
123  Find first available connection slot, starting from a random position.
124  The randomisation stops problems with the server dieing and clients
125  thinking the server is still available.
126 ****************************************************************************/
127
128 connection_struct *conn_new(struct smbd_server_connection *sconn)
129 {
130         connection_struct *conn;
131         int i;
132         int find_offset = 1;
133
134         if (sconn->using_smb2) {
135                 /* SMB2 */
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->sconn = sconn;
143                 return conn;
144         }
145
146         /* SMB1 */
147 find_again:
148         i = bitmap_find(sconn->smb1.tcons.bmap, find_offset);
149
150         if (i == -1) {
151                 /* Expand the connections bitmap. */
152                 int             oldsz = sconn->smb1.tcons.bmap->n;
153                 int             newsz = sconn->smb1.tcons.bmap->n +
154                                         BITMAP_BLOCK_SZ;
155                 struct bitmap * nbmap;
156
157                 if (newsz <= oldsz) {
158                         /* Integer wrap. */
159                         DEBUG(0,("ERROR! Out of connection structures\n"));
160                         return NULL;
161                 }
162
163                 DEBUG(4,("resizing connections bitmap from %d to %d\n",
164                         oldsz, newsz));
165
166                 nbmap = bitmap_talloc(sconn, newsz);
167                 if (!nbmap) {
168                         DEBUG(0,("ERROR! malloc fail.\n"));
169                         return NULL;
170                 }
171
172                 bitmap_copy(nbmap, sconn->smb1.tcons.bmap);
173                 TALLOC_FREE(sconn->smb1.tcons.bmap);
174
175                 sconn->smb1.tcons.bmap = nbmap;
176                 find_offset = oldsz; /* Start next search in the new portion. */
177
178                 goto find_again;
179         }
180
181         /* The bitmap position is used below as the connection number
182          * conn->cnum). This ends up as the TID field in the SMB header,
183          * which is limited to 16 bits (we skip 0xffff which is the
184          * NULL TID).
185          */
186         if (i > 65534) {
187                 DEBUG(0, ("Maximum connection limit reached\n"));
188                 return NULL;
189         }
190
191         if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
192             !(conn->params = TALLOC_P(conn, struct share_params))) {
193                 DEBUG(0,("TALLOC_ZERO() failed!\n"));
194                 TALLOC_FREE(conn);
195                 return NULL;
196         }
197         conn->sconn = sconn;
198         conn->cnum = i;
199         conn->force_group_gid = (gid_t)-1;
200
201         bitmap_set(sconn->smb1.tcons.bmap, i);
202
203         sconn->num_tcons_open++;
204
205         string_set(&conn->connectpath,"");
206         string_set(&conn->origpath,"");
207
208         DLIST_ADD(sconn->smb1.tcons.Connections, conn);
209
210         return conn;
211 }
212
213 /****************************************************************************
214  Close all conn structures.
215  Return true if any were closed.
216 ****************************************************************************/
217
218 bool conn_close_all(struct smbd_server_connection *sconn)
219 {
220         bool ret = false;
221         if (sconn->using_smb2) {
222                 /* SMB2 */
223                 struct smbd_smb2_session *sess;
224                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
225                         struct smbd_smb2_tcon *tcon, *tc_next;
226
227                         for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
228                                 tc_next = tcon->next;
229                                 TALLOC_FREE(tcon);
230                                 ret = true;
231                         }
232                 }
233         } else {
234                 /* SMB1 */
235                 connection_struct *conn, *next;
236
237                 for (conn=sconn->smb1.tcons.Connections;conn;conn=next) {
238                         next=conn->next;
239                         set_current_service(conn, 0, True);
240                         close_cnum(conn, conn->vuid);
241                         ret = true;
242                 }
243         }
244         return ret;
245 }
246
247 /****************************************************************************
248  Update last used timestamps.
249 ****************************************************************************/
250
251 static void conn_lastused_update(struct smbd_server_connection *sconn,time_t t)
252 {
253         if (sconn->using_smb2) {
254                 /* SMB2 */
255                 struct smbd_smb2_session *sess;
256                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
257                         struct smbd_smb2_tcon *ptcon;
258
259                         for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
260                                 connection_struct *conn = ptcon->compat_conn;
261                                 /* Update if connection wasn't idle. */
262                                 if (conn && conn->lastused != conn->lastused_count) {
263                                         conn->lastused = t;
264                                         conn->lastused_count = t;
265                                 }
266                         }
267                 }
268         } else {
269                 /* SMB1 */
270                 connection_struct *conn;
271                 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
272                         /* Update if connection wasn't idle. */
273                         if (conn->lastused != conn->lastused_count) {
274                                 conn->lastused = t;
275                                 conn->lastused_count = t;
276                         }
277                 }
278         }
279 }
280
281 /****************************************************************************
282  Idle inactive connections.
283 ****************************************************************************/
284
285 bool conn_idle_all(struct smbd_server_connection *sconn, time_t t)
286 {
287         int deadtime = lp_deadtime()*60;
288
289         conn_lastused_update(sconn, t);
290
291         if (deadtime <= 0) {
292                 deadtime = DEFAULT_SMBD_TIMEOUT;
293         }
294
295         if (sconn->using_smb2) {
296                 /* SMB2 */
297                 struct smbd_smb2_session *sess;
298                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
299                         struct smbd_smb2_tcon *ptcon;
300
301                         for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
302                                 time_t age;
303                                 connection_struct *conn = ptcon->compat_conn;
304
305                                 if (conn == NULL) {
306                                         continue;
307                                 }
308
309                                 age = t - conn->lastused;
310                                 /* close dirptrs on connections that are idle */
311                                 if (age > DPTR_IDLE_TIMEOUT) {
312                                         dptr_idlecnum(conn);
313                                 }
314
315                                 if (conn->num_files_open > 0 || age < deadtime) {
316                                         return false;
317                                 }
318                         }
319                 }
320         } else {
321                 /* SMB1 */
322                 connection_struct *conn;
323                 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
324                         time_t age = t - conn->lastused;
325
326                         /* close dirptrs on connections that are idle */
327                         if (age > DPTR_IDLE_TIMEOUT) {
328                                 dptr_idlecnum(conn);
329                         }
330
331                         if (conn->num_files_open > 0 || age < deadtime) {
332                                 return false;
333                         }
334                 }
335         }
336
337         /*
338          * Check all pipes for any open handles. We cannot
339          * idle with a handle open.
340          */
341         if (check_open_pipes()) {
342                 return false;
343         }
344
345         return true;
346 }
347
348 /****************************************************************************
349  Clear a vuid out of the validity cache, and as the 'owner' of a connection.
350 ****************************************************************************/
351
352 void conn_clear_vuid_caches(struct smbd_server_connection *sconn,uint16_t vuid)
353 {
354         connection_struct *conn;
355
356         if (sconn->using_smb2) {
357                 /* SMB2 */
358                 struct smbd_smb2_session *sess;
359                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
360                         struct smbd_smb2_tcon *ptcon;
361
362                         for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
363                                 if (ptcon->compat_conn) {
364                                         if (ptcon->compat_conn->vuid == vuid) {
365                                                 ptcon->compat_conn->vuid = UID_FIELD_INVALID;
366                                         }
367                                         conn_clear_vuid_cache(ptcon->compat_conn, vuid);
368                                 }
369                         }
370                 }
371         } else {
372                 /* SMB1 */
373                 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
374                         if (conn->vuid == vuid) {
375                                 conn->vuid = UID_FIELD_INVALID;
376                         }
377                         conn_clear_vuid_cache(conn, vuid);
378                 }
379         }
380 }
381
382 /****************************************************************************
383  Free a conn structure - internal part.
384 ****************************************************************************/
385
386 static void conn_free_internal(connection_struct *conn)
387 {
388         vfs_handle_struct *handle = NULL, *thandle = NULL;
389         struct trans_state *state = NULL;
390
391         /* Free vfs_connection_struct */
392         handle = conn->vfs_handles;
393         while(handle) {
394                 thandle = handle->next;
395                 DLIST_REMOVE(conn->vfs_handles, handle);
396                 if (handle->free_data)
397                         handle->free_data(&handle->data);
398                 handle = thandle;
399         }
400
401         /* Free any pending transactions stored on this conn. */
402         for (state = conn->pending_trans; state; state = state->next) {
403                 /* state->setup is a talloc child of state. */
404                 SAFE_FREE(state->param);
405                 SAFE_FREE(state->data);
406         }
407
408         free_namearray(conn->veto_list);
409         free_namearray(conn->hide_list);
410         free_namearray(conn->veto_oplock_list);
411         free_namearray(conn->aio_write_behind_list);
412
413         string_free(&conn->connectpath);
414         string_free(&conn->origpath);
415
416         ZERO_STRUCTP(conn);
417         talloc_destroy(conn);
418 }
419
420 /****************************************************************************
421  Free a conn structure.
422 ****************************************************************************/
423
424 void conn_free(connection_struct *conn)
425 {
426         if (conn->sconn == NULL) {
427                 conn_free_internal(conn);
428                 return;
429         }
430
431         if (conn->sconn->using_smb2) {
432                 /* SMB2 */
433                 conn_free_internal(conn);
434                 return;
435         }
436
437         /* SMB1 */
438         DLIST_REMOVE(conn->sconn->smb1.tcons.Connections, conn);
439
440         if (conn->sconn->smb1.tcons.bmap != NULL) {
441                 /*
442                  * Can be NULL for fake connections created by
443                  * create_conn_struct()
444                  */
445                 bitmap_clear(conn->sconn->smb1.tcons.bmap, conn->cnum);
446         }
447
448         SMB_ASSERT(conn->sconn->num_tcons_open > 0);
449         conn->sconn->num_tcons_open--;
450
451         conn_free_internal(conn);
452 }
453
454 /****************************************************************************
455  Receive a smbcontrol message to forcibly unmount a share.
456  The message contains just a share name and all instances of that
457  share are unmounted.
458  The special sharename '*' forces unmount of all shares.
459 ****************************************************************************/
460
461 void msg_force_tdis(struct messaging_context *msg,
462                     void *private_data,
463                     uint32_t msg_type,
464                     struct server_id server_id,
465                     DATA_BLOB *data)
466 {
467         struct smbd_server_connection *sconn;
468         connection_struct *conn, *next;
469         fstring sharename;
470
471         sconn = msg_ctx_to_sconn(msg);
472         if (sconn == NULL) {
473                 DEBUG(1, ("could not find sconn\n"));
474                 return;
475         }
476
477         fstrcpy(sharename, (const char *)data->data);
478
479         if (strcmp(sharename, "*") == 0) {
480                 DEBUG(1,("Forcing close of all shares\n"));
481                 conn_close_all(sconn);
482                 return;
483         }
484
485         if (sconn->using_smb2) {
486                 /* SMB2 */
487                 struct smbd_smb2_session *sess;
488                 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
489                         struct smbd_smb2_tcon *tcon, *tc_next;
490
491                         for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
492                                 tc_next = tcon->next;
493                                 if (tcon->compat_conn &&
494                                                 strequal(lp_servicename(SNUM(tcon->compat_conn)),
495                                                                 sharename)) {
496                                         DEBUG(1,("Forcing close of share %s cnum=%d\n",
497                                                 sharename, tcon->compat_conn->cnum));
498                                         TALLOC_FREE(tcon);
499                                 }
500                         }
501                 }
502         } else {
503                 /* SMB1 */
504                 for (conn=sconn->smb1.tcons.Connections;conn;conn=next) {
505                         next=conn->next;
506                         if (strequal(lp_servicename(SNUM(conn)), sharename)) {
507                                 DEBUG(1,("Forcing close of share %s cnum=%d\n",
508                                         sharename, conn->cnum));
509                                 close_cnum(conn, (uint16)-1);
510                         }
511                 }
512         }
513 }