smbd: Slightly simplify downgrade_lease()
[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    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 "lib/util/bitmap.h"
26
27 /****************************************************************************
28  Return the number of open connections.
29 ****************************************************************************/
30
31 int conn_num_open(struct smbd_server_connection *sconn)
32 {
33         return sconn->num_connections;
34 }
35
36 /****************************************************************************
37  Check if a snum is in use.
38 ****************************************************************************/
39
40 bool conn_snum_used(struct smbd_server_connection *sconn,
41                     int snum)
42 {
43         struct connection_struct *conn;
44
45         for (conn=sconn->connections; conn; conn=conn->next) {
46                 if (conn->params->service == snum) {
47                         return true;
48                 }
49         }
50
51         return false;
52 }
53
54 /****************************************************************************
55  Find first available connection slot, starting from a random position.
56  The randomisation stops problems with the server dieing and clients
57  thinking the server is still available.
58 ****************************************************************************/
59
60 connection_struct *conn_new(struct smbd_server_connection *sconn)
61 {
62         connection_struct *conn;
63
64         if (!(conn=talloc_zero(NULL, connection_struct)) ||
65             !(conn->params = talloc(conn, struct share_params)) ||
66             !(conn->vuid_cache = talloc_zero(conn, struct vuid_cache)) ||
67             !(conn->connectpath = talloc_strdup(conn, "")) ||
68             !(conn->origpath = talloc_strdup(conn, ""))) {
69                 DEBUG(0,("TALLOC_ZERO() failed!\n"));
70                 TALLOC_FREE(conn);
71                 return NULL;
72         }
73         conn->sconn = sconn;
74         conn->force_group_gid = (gid_t)-1;
75
76         DLIST_ADD(sconn->connections, conn);
77         sconn->num_connections++;
78
79         return conn;
80 }
81
82 /****************************************************************************
83  Clear a vuid out of the connection's vuid cache
84 ****************************************************************************/
85
86 static void conn_clear_vuid_cache(connection_struct *conn, uint64_t vuid)
87 {
88         int i;
89
90         for (i=0; i<VUID_CACHE_SIZE; i++) {
91                 struct vuid_cache_entry *ent;
92
93                 ent = &conn->vuid_cache->array[i];
94
95                 if (ent->vuid == vuid) {
96                         ent->vuid = UID_FIELD_INVALID;
97
98                         conn->user_ev_ctx = NULL;
99
100                         if (conn->user_vfs_evg == ent->user_vfs_evg) {
101                                 conn->user_vfs_evg = NULL;
102                         }
103                         TALLOC_FREE(ent->user_vfs_evg);
104
105                         /*
106                          * We need to keep conn->session_info around
107                          * if it's equal to ent->session_info as a SMBulogoff
108                          * is often followed by a SMBtdis (with an invalid
109                          * vuid). The debug code (or regular code in
110                          * vfs_full_audit) wants to refer to the
111                          * conn->session_info pointer to print debug
112                          * statements. Theoretically this is a bug,
113                          * as once the vuid is gone the session_info
114                          * on the conn struct isn't valid any more,
115                          * but there's enough code that assumes
116                          * conn->session_info is never null that
117                          * it's easier to hold onto the old pointer
118                          * until we get a new sessionsetupX.
119                          * As everything is hung off the
120                          * conn pointer as a talloc context we're not
121                          * leaking memory here. See bug #6315. JRA.
122                          */
123                         if (conn->session_info == ent->session_info) {
124                                 ent->session_info = NULL;
125                         } else {
126                                 TALLOC_FREE(ent->session_info);
127                         }
128
129                         ent->read_only = False;
130                         ent->share_access = 0;
131                 }
132         }
133 }
134
135 /****************************************************************************
136  Clear a vuid out of the validity cache, and as the 'owner' of a connection.
137
138  Called from invalidate_vuid()
139 ****************************************************************************/
140
141 void conn_clear_vuid_caches(struct smbd_server_connection *sconn, uint64_t vuid)
142 {
143         connection_struct *conn;
144
145         for (conn=sconn->connections; conn;conn=conn->next) {
146                 if (conn->vuid == vuid) {
147                         conn->vuid = UID_FIELD_INVALID;
148                 }
149                 conn_clear_vuid_cache(conn, vuid);
150         }
151 }
152
153 /****************************************************************************
154  Free a conn structure - internal part.
155 ****************************************************************************/
156
157 static void conn_free_internal(connection_struct *conn)
158 {
159         vfs_handle_struct *handle = NULL, *thandle = NULL;
160         struct trans_state *state = NULL;
161
162         /* Free vfs_connection_struct */
163         handle = conn->vfs_handles;
164         while(handle) {
165                 thandle = handle->next;
166                 DLIST_REMOVE(conn->vfs_handles, handle);
167                 if (handle->free_data)
168                         handle->free_data(&handle->data);
169                 handle = thandle;
170         }
171
172         /* Free any pending transactions stored on this conn. */
173         for (state = conn->pending_trans; state; state = state->next) {
174                 /* state->setup is a talloc child of state. */
175                 SAFE_FREE(state->param);
176                 SAFE_FREE(state->data);
177         }
178
179         free_namearray(conn->veto_list);
180         free_namearray(conn->hide_list);
181         free_namearray(conn->veto_oplock_list);
182         free_namearray(conn->aio_write_behind_list);
183
184         ZERO_STRUCTP(conn);
185         talloc_destroy(conn);
186 }
187
188 /****************************************************************************
189  Free a conn structure.
190 ****************************************************************************/
191
192 void conn_free(connection_struct *conn)
193 {
194         if (conn->sconn == NULL) {
195                 conn_free_internal(conn);
196                 return;
197         }
198
199         DLIST_REMOVE(conn->sconn->connections, conn);
200         SMB_ASSERT(conn->sconn->num_connections > 0);
201         conn->sconn->num_connections--;
202
203         conn_free_internal(conn);
204 }