/src/samba/source3/smbd/conn.c
Line | Count | Source |
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 | | static void conn_free_internal(connection_struct *conn); |
28 | | |
29 | | /**************************************************************************** |
30 | | * Remove a conn struct from conn->sconn->connections |
31 | | * if not already done. |
32 | | ****************************************************************************/ |
33 | | |
34 | | static int conn_struct_destructor(connection_struct *conn) |
35 | 0 | { |
36 | 0 | if (conn->sconn != NULL) { |
37 | 0 | DLIST_REMOVE(conn->sconn->connections, conn); |
38 | 0 | SMB_ASSERT(conn->sconn->num_connections > 0); |
39 | 0 | conn->sconn->num_connections--; |
40 | 0 | conn->sconn = NULL; |
41 | 0 | } |
42 | 0 | conn_free_internal(conn); |
43 | 0 | return 0; |
44 | 0 | } |
45 | | |
46 | | /**************************************************************************** |
47 | | Return the number of open connections. |
48 | | ****************************************************************************/ |
49 | | |
50 | | int conn_num_open(struct smbd_server_connection *sconn) |
51 | 0 | { |
52 | 0 | return sconn->num_connections; |
53 | 0 | } |
54 | | |
55 | | /**************************************************************************** |
56 | | Check if a snum is in use. |
57 | | ****************************************************************************/ |
58 | | |
59 | | bool conn_snum_used(struct smbd_server_connection *sconn, |
60 | | int snum) |
61 | 0 | { |
62 | 0 | struct connection_struct *conn; |
63 | |
|
64 | 0 | for (conn=sconn->connections; conn; conn=conn->next) { |
65 | 0 | if (conn->params->service == snum) { |
66 | 0 | return true; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | 0 | return false; |
71 | 0 | } |
72 | | |
73 | | enum protocol_types conn_protocol(struct smbd_server_connection *sconn) |
74 | 0 | { |
75 | 0 | if ((sconn != NULL) && |
76 | 0 | (sconn->client != NULL) && |
77 | 0 | (sconn->client->connections != NULL)) { |
78 | 0 | return sconn->client->connections->protocol; |
79 | 0 | } |
80 | | /* |
81 | | * Default to what source3/lib/util.c has as default for the |
82 | | * static Protocol variable to not change behaviour. |
83 | | */ |
84 | 0 | return PROTOCOL_COREPLUS; |
85 | 0 | } |
86 | | |
87 | | bool conn_using_smb2(struct smbd_server_connection *sconn) |
88 | 0 | { |
89 | 0 | enum protocol_types proto = conn_protocol(sconn); |
90 | 0 | return (proto >= PROTOCOL_SMB2_02); |
91 | 0 | } |
92 | | |
93 | | connection_struct *conn_new(struct smbd_server_connection *sconn) |
94 | 0 | { |
95 | 0 | connection_struct *conn = NULL; |
96 | |
|
97 | 0 | conn = talloc_zero(NULL, connection_struct); |
98 | 0 | if (conn == NULL) { |
99 | 0 | goto nomem; |
100 | 0 | } |
101 | 0 | conn->params = talloc(conn, struct share_params); |
102 | 0 | if (conn->params == NULL) { |
103 | 0 | goto nomem; |
104 | 0 | } |
105 | 0 | conn->vuid_cache = talloc_zero(conn, struct vuid_cache); |
106 | 0 | if (conn->vuid_cache == NULL) { |
107 | 0 | goto nomem; |
108 | 0 | } |
109 | 0 | conn->connectpath = talloc_strdup(conn, ""); |
110 | 0 | if (conn->connectpath == NULL) { |
111 | 0 | goto nomem; |
112 | 0 | } |
113 | 0 | conn->cwd_fsp = talloc_zero(conn, struct files_struct); |
114 | 0 | if (conn->cwd_fsp == NULL) { |
115 | 0 | goto nomem; |
116 | 0 | } |
117 | 0 | conn->cwd_fsp->fsp_name = cp_smb_basename(conn->cwd_fsp, "."); |
118 | 0 | if (conn->cwd_fsp->fsp_name == NULL) { |
119 | 0 | goto nomem; |
120 | 0 | } |
121 | 0 | conn->cwd_fsp->fh = fd_handle_create(conn->cwd_fsp); |
122 | 0 | if (conn->cwd_fsp->fh == NULL) { |
123 | 0 | goto nomem; |
124 | 0 | } |
125 | 0 | conn->sconn = sconn; |
126 | 0 | conn->force_group_gid = (gid_t)-1; |
127 | 0 | fsp_set_fd(conn->cwd_fsp, -1); |
128 | 0 | conn->cwd_fsp->fnum = FNUM_FIELD_INVALID; |
129 | 0 | conn->cwd_fsp->conn = conn; |
130 | |
|
131 | 0 | DLIST_ADD(sconn->connections, conn); |
132 | 0 | sconn->num_connections++; |
133 | | |
134 | | /* |
135 | | * Catches the case where someone forgets to call |
136 | | * conn_free(). |
137 | | */ |
138 | 0 | talloc_set_destructor(conn, conn_struct_destructor); |
139 | 0 | return conn; |
140 | | |
141 | 0 | nomem: |
142 | 0 | DBG_ERR("talloc failed"); |
143 | 0 | TALLOC_FREE(conn); |
144 | 0 | return NULL; |
145 | 0 | } |
146 | | |
147 | | /**************************************************************************** |
148 | | Clear a vuid out of the connection's vuid cache |
149 | | ****************************************************************************/ |
150 | | |
151 | | static void conn_clear_vuid_cache(connection_struct *conn, uint64_t vuid) |
152 | 0 | { |
153 | 0 | struct vuid_cache_entry *ent = NULL; |
154 | 0 | int i; |
155 | |
|
156 | 0 | for (i=0; i<VUID_CACHE_SIZE; i++) { |
157 | 0 | ent = &conn->vuid_cache->array[i]; |
158 | 0 | if (ent->vuid == vuid) { |
159 | 0 | break; |
160 | 0 | } |
161 | 0 | } |
162 | 0 | if (i == VUID_CACHE_SIZE) { |
163 | 0 | return; |
164 | 0 | } |
165 | | |
166 | 0 | ent->vuid = UID_FIELD_INVALID; |
167 | | |
168 | | /* |
169 | | * We need to keep conn->session_info around |
170 | | * if it's equal to ent->session_info as a SMBulogoff |
171 | | * is often followed by a SMBtdis (with an invalid |
172 | | * vuid). The debug code (or regular code in |
173 | | * vfs_full_audit) wants to refer to the |
174 | | * conn->session_info pointer to print debug |
175 | | * statements. Theoretically this is a bug, |
176 | | * as once the vuid is gone the session_info |
177 | | * on the conn struct isn't valid any more, |
178 | | * but there's enough code that assumes |
179 | | * conn->session_info is never null that |
180 | | * it's easier to hold onto the old pointer |
181 | | * until we get a new sessionsetupX. |
182 | | * As everything is hung off the |
183 | | * conn pointer as a talloc context we're not |
184 | | * leaking memory here. See bug #6315. JRA. |
185 | | */ |
186 | 0 | if (conn->session_info == ent->session_info) { |
187 | 0 | ent->session_info = NULL; |
188 | 0 | } else { |
189 | 0 | TALLOC_FREE(ent->session_info); |
190 | 0 | } |
191 | 0 | ent->read_only = False; |
192 | 0 | ent->share_access = 0; |
193 | 0 | TALLOC_FREE(ent->veto_list); |
194 | 0 | TALLOC_FREE(ent->hide_list); |
195 | 0 | } |
196 | | |
197 | | /**************************************************************************** |
198 | | Clear a vuid out of the validity cache, and as the 'owner' of a connection. |
199 | | |
200 | | Called from invalidate_vuid() |
201 | | ****************************************************************************/ |
202 | | |
203 | | void conn_clear_vuid_caches(struct smbd_server_connection *sconn, uint64_t vuid) |
204 | 0 | { |
205 | 0 | connection_struct *conn; |
206 | |
|
207 | 0 | for (conn=sconn->connections; conn;conn=conn->next) { |
208 | 0 | if (conn->vuid == vuid) { |
209 | 0 | conn->vuid = UID_FIELD_INVALID; |
210 | 0 | } |
211 | 0 | conn_clear_vuid_cache(conn, vuid); |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | /**************************************************************************** |
216 | | Free a conn structure - internal part. |
217 | | ****************************************************************************/ |
218 | | |
219 | | static void conn_free_internal(connection_struct *conn) |
220 | 0 | { |
221 | 0 | vfs_handle_struct *handle = NULL, *thandle = NULL; |
222 | 0 | struct trans_state *state = NULL; |
223 | | |
224 | | /* Free vfs_connection_struct */ |
225 | 0 | handle = conn->vfs_handles; |
226 | 0 | while(handle) { |
227 | 0 | thandle = handle->next; |
228 | 0 | DLIST_REMOVE(conn->vfs_handles, handle); |
229 | 0 | if (handle->free_data) |
230 | 0 | handle->free_data(&handle->data); |
231 | 0 | handle = thandle; |
232 | 0 | } |
233 | | |
234 | | /* Free any pending transactions stored on this conn. */ |
235 | 0 | for (state = conn->pending_trans; state; state = state->next) { |
236 | | /* state->setup is a talloc child of state. */ |
237 | 0 | SAFE_FREE(state->param); |
238 | 0 | SAFE_FREE(state->data); |
239 | 0 | } |
240 | |
|
241 | 0 | ZERO_STRUCTP(conn); |
242 | 0 | } |
243 | | |
244 | | /**************************************************************************** |
245 | | Free a conn structure. |
246 | | ****************************************************************************/ |
247 | | |
248 | | void conn_free(connection_struct *conn) |
249 | 0 | { |
250 | 0 | TALLOC_FREE(conn); |
251 | 0 | } |
252 | | |
253 | | /* |
254 | | * Correctly initialize a share with case options. |
255 | | */ |
256 | | void conn_setup_case_options(connection_struct *conn) |
257 | 0 | { |
258 | 0 | int snum = conn->params->service; |
259 | |
|
260 | 0 | if (lp_case_sensitive(snum) == Auto) { |
261 | | /* We will be setting this per packet. Set to be case |
262 | | * insensitive for now. */ |
263 | 0 | conn->case_sensitive = false; |
264 | 0 | } else { |
265 | 0 | conn->case_sensitive = (bool)lp_case_sensitive(snum); |
266 | 0 | } |
267 | |
|
268 | 0 | conn->case_preserve = lp_preserve_case(snum); |
269 | 0 | conn->short_case_preserve = lp_short_preserve_case(snum); |
270 | 0 | } |