fd3bf9d9d6b1381de6f54c910146927a140fed73
[mat/samba.git] / source3 / smbd / conn_idle.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/smbd.h"
24 #include "smbd/globals.h"
25 #include "rpc_server/rpc_pipes.h"
26
27 /****************************************************************************
28  Update last used timestamps.
29 ****************************************************************************/
30
31 static void conn_lastused_update(struct smbd_server_connection *sconn,time_t t)
32 {
33         struct connection_struct *conn;
34
35         for (conn=sconn->connections; conn; conn=conn->next) {
36                 /* Update if connection wasn't idle. */
37                 if (conn->lastused != conn->lastused_count) {
38                         conn->lastused = t;
39                         conn->lastused_count = t;
40                 }
41         }
42 }
43
44 /****************************************************************************
45  Idle inactive connections.
46 ****************************************************************************/
47
48 bool conn_idle_all(struct smbd_server_connection *sconn, time_t t)
49 {
50         int deadtime = lp_deadtime()*60;
51         struct connection_struct *conn;
52
53         conn_lastused_update(sconn, t);
54
55         if (deadtime <= 0) {
56                 deadtime = DEFAULT_SMBD_TIMEOUT;
57         }
58
59         for (conn=sconn->connections;conn;conn=conn->next) {
60                 time_t age = t - conn->lastused;
61
62                 /* close dirptrs on connections that are idle */
63                 if (age > DPTR_IDLE_TIMEOUT) {
64                         dptr_idlecnum(conn);
65                 }
66
67                 if (conn->num_files_open > 0 || age < deadtime) {
68                         return false;
69                 }
70         }
71
72         /*
73          * Check all pipes for any open handles. We cannot
74          * idle with a handle open.
75          */
76         if (check_open_pipes()) {
77                 return false;
78         }
79
80         return true;
81 }
82
83 /****************************************************************************
84  Close all conn structures.
85  Return true if any were closed.
86 ****************************************************************************/
87
88 void conn_close_all(struct smbd_server_connection *sconn)
89 {
90         if (sconn->using_smb2) {
91                 smbXsrv_session_logoff_all(sconn->conn);
92         } else {
93                 smb1srv_tcon_disconnect_all(sconn->conn);
94         }
95 }
96
97
98 /****************************************************************************
99  Forcibly unmount a share.
100  All instances of the parameter 'sharename' share are unmounted.
101  The special sharename '*' forces unmount of all shares.
102 ****************************************************************************/
103
104 void conn_force_tdis(struct smbd_server_connection *sconn, const char *sharename)
105 {
106         connection_struct *conn, *next;
107         bool close_all = false;
108
109         if (strcmp(sharename, "*") == 0) {
110                 close_all = true;
111                 DEBUG(1, ("conn_force_tdis: Forcing close of all shares\n"));
112         }
113
114         /* SMB1 and SMB 2*/
115         for (conn = sconn->connections; conn; conn = next) {
116                 struct smbXsrv_tcon *tcon;
117                 bool do_close = false;
118                 NTSTATUS status;
119                 uint64_t vuid = UID_FIELD_INVALID;
120
121                 next = conn->next;
122
123                 if (close_all) {
124                         do_close = true;
125                 } else if (strequal(lp_servicename(SNUM(conn)), sharename)) {
126                         DEBUG(1, ("conn_force_tdis: Forcing close of "
127                                   "share '%s' (wire_id=0x%08x)\n",
128                                   tcon->global->share_name,
129                                   tcon->global->tcon_wire_id));
130                         do_close = true;
131                 }
132
133                 if (!do_close) {
134                         continue;
135                 }
136
137                 if (sconn->using_smb2) {
138                         vuid = conn->vuid;
139                 }
140
141                 conn = NULL;
142                 status = smbXsrv_tcon_disconnect(tcon, vuid);
143                 if (!NT_STATUS_IS_OK(status)) {
144                         DEBUG(0, ("conn_force_tdis: "
145                                   "smbXsrv_tcon_disconnect() of share '%s' "
146                                   "(wire_id=0x%08x) failed: %s\n",
147                                   tcon->global->share_name,
148                                   tcon->global->tcon_wire_id,
149                                   nt_errstr(status)));
150                 }
151
152                 TALLOC_FREE(tcon);
153         }
154 }