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