ctdb-logging: Move variable debug_extra from debug.*
[Samba.git] / source3 / smbd / smbXsrv_tcon.c
blob48a938e798cb96d67e8bde313528e619b97a5dc0
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2011-2012
5 Copyright (C) Michael Adam 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "dbwrap/dbwrap.h"
26 #include "dbwrap/dbwrap_rbt.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "messages.h"
29 #include "lib/util/util_tdb.h"
30 #include "librpc/gen_ndr/ndr_smbXsrv.h"
31 #include "serverid.h"
33 struct smbXsrv_tcon_table {
34 struct {
35 struct db_context *db_ctx;
36 uint32_t lowest_id;
37 uint32_t highest_id;
38 uint32_t max_tcons;
39 uint32_t num_tcons;
40 } local;
41 struct {
42 struct db_context *db_ctx;
43 } global;
46 static struct db_context *smbXsrv_tcon_global_db_ctx = NULL;
48 NTSTATUS smbXsrv_tcon_global_init(void)
50 const char *global_path = NULL;
51 struct db_context *db_ctx = NULL;
53 if (smbXsrv_tcon_global_db_ctx != NULL) {
54 return NT_STATUS_OK;
57 global_path = lock_path("smbXsrv_tcon_global.tdb");
59 db_ctx = db_open(NULL, global_path,
60 0, /* hash_size */
61 TDB_DEFAULT |
62 TDB_CLEAR_IF_FIRST |
63 TDB_INCOMPATIBLE_HASH,
64 O_RDWR | O_CREAT, 0600,
65 DBWRAP_LOCK_ORDER_1,
66 DBWRAP_FLAG_NONE);
67 if (db_ctx == NULL) {
68 NTSTATUS status;
70 status = map_nt_error_from_unix_common(errno);
72 return status;
75 smbXsrv_tcon_global_db_ctx = db_ctx;
77 return NT_STATUS_OK;
81 * NOTE:
82 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
83 * has the same result as integer comparison between the uint32_t
84 * values.
86 * TODO: implement string based key
89 #define SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
91 static TDB_DATA smbXsrv_tcon_global_id_to_key(uint32_t id,
92 uint8_t *key_buf)
94 TDB_DATA key;
96 RSIVAL(key_buf, 0, id);
98 key = make_tdb_data(key_buf, SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE);
100 return key;
103 #if 0
104 static NTSTATUS smbXsrv_tcon_global_key_to_id(TDB_DATA key, uint32_t *id)
106 if (id == NULL) {
107 return NT_STATUS_INVALID_PARAMETER;
110 if (key.dsize != SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE) {
111 return NT_STATUS_INTERNAL_DB_CORRUPTION;
114 *id = RIVAL(key.dptr, 0);
116 return NT_STATUS_OK;
118 #endif
120 #define SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
122 static TDB_DATA smbXsrv_tcon_local_id_to_key(uint32_t id,
123 uint8_t *key_buf)
125 TDB_DATA key;
127 RSIVAL(key_buf, 0, id);
129 key = make_tdb_data(key_buf, SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE);
131 return key;
134 static NTSTATUS smbXsrv_tcon_local_key_to_id(TDB_DATA key, uint32_t *id)
136 if (id == NULL) {
137 return NT_STATUS_INVALID_PARAMETER;
140 if (key.dsize != SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE) {
141 return NT_STATUS_INTERNAL_DB_CORRUPTION;
144 *id = RIVAL(key.dptr, 0);
146 return NT_STATUS_OK;
149 static NTSTATUS smbXsrv_tcon_table_init(TALLOC_CTX *mem_ctx,
150 struct smbXsrv_tcon_table *table,
151 uint32_t lowest_id,
152 uint32_t highest_id,
153 uint32_t max_tcons)
155 NTSTATUS status;
156 uint64_t max_range;
158 if (lowest_id > highest_id) {
159 return NT_STATUS_INTERNAL_ERROR;
162 max_range = highest_id;
163 max_range -= lowest_id;
164 max_range += 1;
166 if (max_tcons > max_range) {
167 return NT_STATUS_INTERNAL_ERROR;
170 ZERO_STRUCTP(table);
171 table->local.db_ctx = db_open_rbt(table);
172 if (table->local.db_ctx == NULL) {
173 return NT_STATUS_NO_MEMORY;
175 table->local.lowest_id = lowest_id;
176 table->local.highest_id = highest_id;
177 table->local.max_tcons = max_tcons;
179 status = smbXsrv_tcon_global_init();
180 if (!NT_STATUS_IS_OK(status)) {
181 return status;
184 table->global.db_ctx = smbXsrv_tcon_global_db_ctx;
186 return NT_STATUS_OK;
189 struct smb1srv_tcon_local_allocate_state {
190 const uint32_t lowest_id;
191 const uint32_t highest_id;
192 uint32_t last_id;
193 uint32_t useable_id;
194 NTSTATUS status;
197 static int smb1srv_tcon_local_allocate_traverse(struct db_record *rec,
198 void *private_data)
200 struct smb1srv_tcon_local_allocate_state *state =
201 (struct smb1srv_tcon_local_allocate_state *)private_data;
202 TDB_DATA key = dbwrap_record_get_key(rec);
203 uint32_t id = 0;
204 NTSTATUS status;
206 status = smbXsrv_tcon_local_key_to_id(key, &id);
207 if (!NT_STATUS_IS_OK(status)) {
208 state->status = status;
209 return -1;
212 if (id <= state->last_id) {
213 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
214 return -1;
216 state->last_id = id;
218 if (id > state->useable_id) {
219 state->status = NT_STATUS_OK;
220 return -1;
223 if (state->useable_id == state->highest_id) {
224 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
225 return -1;
228 state->useable_id +=1;
229 return 0;
232 static NTSTATUS smb1srv_tcon_local_allocate_id(struct db_context *db,
233 uint32_t lowest_id,
234 uint32_t highest_id,
235 TALLOC_CTX *mem_ctx,
236 struct db_record **_rec,
237 uint32_t *_id)
239 struct smb1srv_tcon_local_allocate_state state = {
240 .lowest_id = lowest_id,
241 .highest_id = highest_id,
242 .last_id = 0,
243 .useable_id = lowest_id,
244 .status = NT_STATUS_INTERNAL_ERROR,
246 uint32_t i;
247 uint32_t range;
248 NTSTATUS status;
249 int count = 0;
251 *_rec = NULL;
252 *_id = 0;
254 if (lowest_id > highest_id) {
255 return NT_STATUS_INSUFFICIENT_RESOURCES;
259 * first we try randomly
261 range = (highest_id - lowest_id) + 1;
263 for (i = 0; i < (range / 2); i++) {
264 uint32_t id;
265 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
266 TDB_DATA key;
267 TDB_DATA val;
268 struct db_record *rec = NULL;
270 id = generate_random() % range;
271 id += lowest_id;
273 if (id < lowest_id) {
274 id = lowest_id;
276 if (id > highest_id) {
277 id = highest_id;
280 key = smbXsrv_tcon_local_id_to_key(id, key_buf);
282 rec = dbwrap_fetch_locked(db, mem_ctx, key);
283 if (rec == NULL) {
284 return NT_STATUS_INSUFFICIENT_RESOURCES;
287 val = dbwrap_record_get_value(rec);
288 if (val.dsize != 0) {
289 TALLOC_FREE(rec);
290 continue;
293 *_rec = rec;
294 *_id = id;
295 return NT_STATUS_OK;
299 * if the range is almost full,
300 * we traverse the whole table
301 * (this relies on sorted behavior of dbwrap_rbt)
303 status = dbwrap_traverse_read(db, smb1srv_tcon_local_allocate_traverse,
304 &state, &count);
305 if (NT_STATUS_IS_OK(status)) {
306 if (NT_STATUS_IS_OK(state.status)) {
307 return NT_STATUS_INTERNAL_ERROR;
310 if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
311 return state.status;
314 if (state.useable_id <= state.highest_id) {
315 state.status = NT_STATUS_OK;
316 } else {
317 return NT_STATUS_INSUFFICIENT_RESOURCES;
319 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
321 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
323 * If we get anything else it is an error, because it
324 * means we did not manage to find a free slot in
325 * the db.
327 return NT_STATUS_INSUFFICIENT_RESOURCES;
330 if (NT_STATUS_IS_OK(state.status)) {
331 uint32_t id;
332 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
333 TDB_DATA key;
334 TDB_DATA val;
335 struct db_record *rec = NULL;
337 id = state.useable_id;
339 key = smbXsrv_tcon_local_id_to_key(id, key_buf);
341 rec = dbwrap_fetch_locked(db, mem_ctx, key);
342 if (rec == NULL) {
343 return NT_STATUS_INSUFFICIENT_RESOURCES;
346 val = dbwrap_record_get_value(rec);
347 if (val.dsize != 0) {
348 TALLOC_FREE(rec);
349 return NT_STATUS_INTERNAL_DB_CORRUPTION;
352 *_rec = rec;
353 *_id = id;
354 return NT_STATUS_OK;
357 return state.status;
360 struct smbXsrv_tcon_local_fetch_state {
361 struct smbXsrv_tcon *tcon;
362 NTSTATUS status;
365 static void smbXsrv_tcon_local_fetch_parser(TDB_DATA key, TDB_DATA data,
366 void *private_data)
368 struct smbXsrv_tcon_local_fetch_state *state =
369 (struct smbXsrv_tcon_local_fetch_state *)private_data;
370 void *ptr;
372 if (data.dsize != sizeof(ptr)) {
373 state->status = NT_STATUS_INTERNAL_DB_ERROR;
374 return;
377 memcpy(&ptr, data.dptr, data.dsize);
378 state->tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
379 state->status = NT_STATUS_OK;
382 static NTSTATUS smbXsrv_tcon_local_lookup(struct smbXsrv_tcon_table *table,
383 uint32_t tcon_local_id,
384 NTTIME now,
385 struct smbXsrv_tcon **_tcon)
387 struct smbXsrv_tcon_local_fetch_state state = {
388 .tcon = NULL,
389 .status = NT_STATUS_INTERNAL_ERROR,
391 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
392 TDB_DATA key;
393 NTSTATUS status;
395 *_tcon = NULL;
397 if (tcon_local_id == 0) {
398 return NT_STATUS_NETWORK_NAME_DELETED;
401 if (table == NULL) {
402 /* this might happen before the end of negprot */
403 return NT_STATUS_NETWORK_NAME_DELETED;
406 if (table->local.db_ctx == NULL) {
407 return NT_STATUS_INTERNAL_ERROR;
410 key = smbXsrv_tcon_local_id_to_key(tcon_local_id, key_buf);
412 status = dbwrap_parse_record(table->local.db_ctx, key,
413 smbXsrv_tcon_local_fetch_parser,
414 &state);
415 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
416 return NT_STATUS_NETWORK_NAME_DELETED;
417 } else if (!NT_STATUS_IS_OK(status)) {
418 return status;
420 if (!NT_STATUS_IS_OK(state.status)) {
421 return state.status;
424 if (NT_STATUS_EQUAL(state.tcon->status, NT_STATUS_NETWORK_NAME_DELETED)) {
425 return NT_STATUS_NETWORK_NAME_DELETED;
428 state.tcon->idle_time = now;
430 *_tcon = state.tcon;
431 return state.tcon->status;
434 static int smbXsrv_tcon_global_destructor(struct smbXsrv_tcon_global0 *global)
436 return 0;
439 static void smbXsrv_tcon_global_verify_record(struct db_record *db_rec,
440 bool *is_free,
441 bool *was_free,
442 TALLOC_CTX *mem_ctx,
443 struct smbXsrv_tcon_global0 **_g);
445 static NTSTATUS smbXsrv_tcon_global_allocate(struct db_context *db,
446 TALLOC_CTX *mem_ctx,
447 struct smbXsrv_tcon_global0 **_global)
449 uint32_t i;
450 struct smbXsrv_tcon_global0 *global = NULL;
451 uint32_t last_free = 0;
452 const uint32_t min_tries = 3;
454 *_global = NULL;
456 global = talloc_zero(mem_ctx, struct smbXsrv_tcon_global0);
457 if (global == NULL) {
458 return NT_STATUS_NO_MEMORY;
460 talloc_set_destructor(global, smbXsrv_tcon_global_destructor);
463 * Here we just randomly try the whole 32-bit space
465 * We use just 32-bit, because we want to reuse the
466 * ID for SRVSVC.
468 for (i = 0; i < UINT32_MAX; i++) {
469 bool is_free = false;
470 bool was_free = false;
471 uint32_t id;
472 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
473 TDB_DATA key;
475 if (i >= min_tries && last_free != 0) {
476 id = last_free;
477 } else {
478 id = generate_random();
480 if (id == 0) {
481 id++;
483 if (id == UINT32_MAX) {
484 id--;
487 key = smbXsrv_tcon_global_id_to_key(id, key_buf);
489 global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
490 if (global->db_rec == NULL) {
491 talloc_free(global);
492 return NT_STATUS_INSUFFICIENT_RESOURCES;
495 smbXsrv_tcon_global_verify_record(global->db_rec,
496 &is_free,
497 &was_free,
498 NULL, NULL);
500 if (!is_free) {
501 TALLOC_FREE(global->db_rec);
502 continue;
505 if (!was_free && i < min_tries) {
507 * The session_id is free now,
508 * but was not free before.
510 * This happens if a smbd crashed
511 * and did not cleanup the record.
513 * If this is one of our first tries,
514 * then we try to find a real free one.
516 if (last_free == 0) {
517 last_free = id;
519 TALLOC_FREE(global->db_rec);
520 continue;
523 global->tcon_global_id = id;
525 *_global = global;
526 return NT_STATUS_OK;
529 /* should not be reached */
530 talloc_free(global);
531 return NT_STATUS_INTERNAL_ERROR;
534 static void smbXsrv_tcon_global_verify_record(struct db_record *db_rec,
535 bool *is_free,
536 bool *was_free,
537 TALLOC_CTX *mem_ctx,
538 struct smbXsrv_tcon_global0 **_g)
540 TDB_DATA key;
541 TDB_DATA val;
542 DATA_BLOB blob;
543 struct smbXsrv_tcon_globalB global_blob;
544 enum ndr_err_code ndr_err;
545 struct smbXsrv_tcon_global0 *global = NULL;
546 bool exists;
547 TALLOC_CTX *frame = talloc_stackframe();
549 *is_free = false;
551 if (was_free) {
552 *was_free = false;
554 if (_g) {
555 *_g = NULL;
558 key = dbwrap_record_get_key(db_rec);
560 val = dbwrap_record_get_value(db_rec);
561 if (val.dsize == 0) {
562 TALLOC_FREE(frame);
563 *is_free = true;
564 if (was_free) {
565 *was_free = true;
567 return;
570 blob = data_blob_const(val.dptr, val.dsize);
572 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
573 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_tcon_globalB);
574 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
575 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
576 DEBUG(1,("smbXsrv_tcon_global_verify_record: "
577 "key '%s' ndr_pull_struct_blob - %s\n",
578 hex_encode_talloc(frame, key.dptr, key.dsize),
579 nt_errstr(status)));
580 TALLOC_FREE(frame);
581 return;
584 DEBUG(10,("smbXsrv_tcon_global_verify_record\n"));
585 if (DEBUGLVL(10)) {
586 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
589 if (global_blob.version != SMBXSRV_VERSION_0) {
590 DEBUG(0,("smbXsrv_tcon_global_verify_record: "
591 "key '%s' use unsupported version %u\n",
592 hex_encode_talloc(frame, key.dptr, key.dsize),
593 global_blob.version));
594 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
595 TALLOC_FREE(frame);
596 return;
599 global = global_blob.info.info0;
601 exists = serverid_exists(&global->server_id);
602 if (!exists) {
603 DEBUG(2,("smbXsrv_tcon_global_verify_record: "
604 "key '%s' server_id %s does not exist.\n",
605 hex_encode_talloc(frame, key.dptr, key.dsize),
606 server_id_str(frame, &global->server_id)));
607 if (DEBUGLVL(2)) {
608 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
610 TALLOC_FREE(frame);
611 dbwrap_record_delete(db_rec);
612 *is_free = true;
613 return;
616 if (_g) {
617 *_g = talloc_move(mem_ctx, &global);
619 TALLOC_FREE(frame);
622 static NTSTATUS smbXsrv_tcon_global_store(struct smbXsrv_tcon_global0 *global)
624 struct smbXsrv_tcon_globalB global_blob;
625 DATA_BLOB blob = data_blob_null;
626 TDB_DATA key;
627 TDB_DATA val;
628 NTSTATUS status;
629 enum ndr_err_code ndr_err;
632 * TODO: if we use other versions than '0'
633 * we would add glue code here, that would be able to
634 * store the information in the old format.
637 if (global->db_rec == NULL) {
638 return NT_STATUS_INTERNAL_ERROR;
641 key = dbwrap_record_get_key(global->db_rec);
642 val = dbwrap_record_get_value(global->db_rec);
644 ZERO_STRUCT(global_blob);
645 global_blob.version = smbXsrv_version_global_current();
646 if (val.dsize >= 8) {
647 global_blob.seqnum = IVAL(val.dptr, 4);
649 global_blob.seqnum += 1;
650 global_blob.info.info0 = global;
652 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
653 (ndr_push_flags_fn_t)ndr_push_smbXsrv_tcon_globalB);
654 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
655 status = ndr_map_error2ntstatus(ndr_err);
656 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' ndr_push - %s\n",
657 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
658 nt_errstr(status)));
659 TALLOC_FREE(global->db_rec);
660 return status;
663 val = make_tdb_data(blob.data, blob.length);
664 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
665 if (!NT_STATUS_IS_OK(status)) {
666 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' store - %s\n",
667 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
668 nt_errstr(status)));
669 TALLOC_FREE(global->db_rec);
670 return status;
673 if (DEBUGLVL(10)) {
674 DEBUG(10,("smbXsrv_tcon_global_store: key '%s' stored\n",
675 hex_encode_talloc(global->db_rec, key.dptr, key.dsize)));
676 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
679 TALLOC_FREE(global->db_rec);
681 return NT_STATUS_OK;
684 static int smbXsrv_tcon_destructor(struct smbXsrv_tcon *tcon)
686 NTSTATUS status;
688 status = smbXsrv_tcon_disconnect(tcon, 0);
689 if (!NT_STATUS_IS_OK(status)) {
690 DEBUG(0, ("smbXsrv_tcon_destructor: "
691 "smbXsrv_tcon_disconnect() failed - %s\n",
692 nt_errstr(status)));
695 TALLOC_FREE(tcon->global);
697 return 0;
700 static NTSTATUS smbXsrv_tcon_create(struct smbXsrv_tcon_table *table,
701 enum protocol_types protocol,
702 struct server_id server_id,
703 NTTIME now,
704 struct smbXsrv_tcon **_tcon)
706 struct db_record *local_rec = NULL;
707 struct smbXsrv_tcon *tcon = NULL;
708 void *ptr = NULL;
709 TDB_DATA val;
710 struct smbXsrv_tcon_global0 *global = NULL;
711 NTSTATUS status;
713 if (table->local.num_tcons >= table->local.max_tcons) {
714 return NT_STATUS_INSUFFICIENT_RESOURCES;
717 tcon = talloc_zero(table, struct smbXsrv_tcon);
718 if (tcon == NULL) {
719 return NT_STATUS_NO_MEMORY;
721 tcon->table = table;
722 tcon->status = NT_STATUS_INTERNAL_ERROR;
723 tcon->idle_time = now;
725 status = smbXsrv_tcon_global_allocate(table->global.db_ctx,
726 tcon, &global);
727 if (!NT_STATUS_IS_OK(status)) {
728 TALLOC_FREE(tcon);
729 return status;
731 tcon->global = global;
733 if (protocol >= PROTOCOL_SMB2_02) {
734 uint64_t id = global->tcon_global_id;
735 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
736 TDB_DATA key;
738 global->tcon_wire_id = id;
740 tcon->local_id = global->tcon_global_id;
742 key = smbXsrv_tcon_local_id_to_key(tcon->local_id, key_buf);
744 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
745 tcon, key);
746 if (local_rec == NULL) {
747 TALLOC_FREE(tcon);
748 return NT_STATUS_NO_MEMORY;
751 val = dbwrap_record_get_value(local_rec);
752 if (val.dsize != 0) {
753 TALLOC_FREE(tcon);
754 return NT_STATUS_INTERNAL_DB_CORRUPTION;
756 } else {
758 status = smb1srv_tcon_local_allocate_id(table->local.db_ctx,
759 table->local.lowest_id,
760 table->local.highest_id,
761 tcon,
762 &local_rec,
763 &tcon->local_id);
764 if (!NT_STATUS_IS_OK(status)) {
765 TALLOC_FREE(tcon);
766 return status;
769 global->tcon_wire_id = tcon->local_id;
772 global->creation_time = now;
774 global->server_id = server_id;
776 ptr = tcon;
777 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
778 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
779 TALLOC_FREE(local_rec);
780 if (!NT_STATUS_IS_OK(status)) {
781 TALLOC_FREE(tcon);
782 return status;
784 table->local.num_tcons += 1;
786 talloc_set_destructor(tcon, smbXsrv_tcon_destructor);
788 status = smbXsrv_tcon_global_store(global);
789 if (!NT_STATUS_IS_OK(status)) {
790 DEBUG(0,("smbXsrv_tcon_create: "
791 "global_id (0x%08x) store failed - %s\n",
792 tcon->global->tcon_global_id,
793 nt_errstr(status)));
794 TALLOC_FREE(tcon);
795 return status;
798 if (DEBUGLVL(10)) {
799 struct smbXsrv_tconB tcon_blob;
801 ZERO_STRUCT(tcon_blob);
802 tcon_blob.version = SMBXSRV_VERSION_0;
803 tcon_blob.info.info0 = tcon;
805 DEBUG(10,("smbXsrv_tcon_create: global_id (0x%08x) stored\n",
806 tcon->global->tcon_global_id));
807 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
810 *_tcon = tcon;
811 return NT_STATUS_OK;
814 NTSTATUS smbXsrv_tcon_update(struct smbXsrv_tcon *tcon)
816 struct smbXsrv_tcon_table *table = tcon->table;
817 NTSTATUS status;
818 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
819 TDB_DATA key;
821 if (tcon->global->db_rec != NULL) {
822 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
823 "Called with db_rec != NULL'\n",
824 tcon->global->tcon_global_id));
825 return NT_STATUS_INTERNAL_ERROR;
828 key = smbXsrv_tcon_global_id_to_key(tcon->global->tcon_global_id,
829 key_buf);
831 tcon->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
832 tcon->global, key);
833 if (tcon->global->db_rec == NULL) {
834 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
835 "Failed to lock global key '%s'\n",
836 tcon->global->tcon_global_id,
837 hex_encode_talloc(talloc_tos(), key.dptr,
838 key.dsize)));
839 return NT_STATUS_INTERNAL_DB_ERROR;
842 status = smbXsrv_tcon_global_store(tcon->global);
843 if (!NT_STATUS_IS_OK(status)) {
844 DEBUG(0,("smbXsrv_tcon_update: "
845 "global_id (0x%08x) store failed - %s\n",
846 tcon->global->tcon_global_id,
847 nt_errstr(status)));
848 return status;
851 if (DEBUGLVL(10)) {
852 struct smbXsrv_tconB tcon_blob;
854 ZERO_STRUCT(tcon_blob);
855 tcon_blob.version = SMBXSRV_VERSION_0;
856 tcon_blob.info.info0 = tcon;
858 DEBUG(10,("smbXsrv_tcon_update: global_id (0x%08x) stored\n",
859 tcon->global->tcon_global_id));
860 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
863 return NT_STATUS_OK;
866 NTSTATUS smbXsrv_tcon_disconnect(struct smbXsrv_tcon *tcon, uint64_t vuid)
868 struct smbXsrv_tcon_table *table;
869 struct db_record *local_rec = NULL;
870 struct db_record *global_rec = NULL;
871 NTSTATUS status;
872 NTSTATUS error = NT_STATUS_OK;
874 if (tcon->table == NULL) {
875 return NT_STATUS_OK;
878 table = tcon->table;
879 tcon->table = NULL;
881 tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
883 global_rec = tcon->global->db_rec;
884 tcon->global->db_rec = NULL;
885 if (global_rec == NULL) {
886 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
887 TDB_DATA key;
889 key = smbXsrv_tcon_global_id_to_key(
890 tcon->global->tcon_global_id,
891 key_buf);
893 global_rec = dbwrap_fetch_locked(table->global.db_ctx,
894 tcon->global, key);
895 if (global_rec == NULL) {
896 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
897 "Failed to lock global key '%s'\n",
898 tcon->global->tcon_global_id,
899 tcon->global->share_name,
900 hex_encode_talloc(global_rec, key.dptr,
901 key.dsize)));
902 error = NT_STATUS_INTERNAL_ERROR;
906 if (global_rec != NULL) {
907 status = dbwrap_record_delete(global_rec);
908 if (!NT_STATUS_IS_OK(status)) {
909 TDB_DATA key = dbwrap_record_get_key(global_rec);
911 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
912 "failed to delete global key '%s': %s\n",
913 tcon->global->tcon_global_id,
914 tcon->global->share_name,
915 hex_encode_talloc(global_rec, key.dptr,
916 key.dsize),
917 nt_errstr(status)));
918 error = status;
921 TALLOC_FREE(global_rec);
923 local_rec = tcon->db_rec;
924 if (local_rec == NULL) {
925 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
926 TDB_DATA key;
928 key = smbXsrv_tcon_local_id_to_key(tcon->local_id, key_buf);
930 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
931 tcon, key);
932 if (local_rec == NULL) {
933 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
934 "Failed to lock local key '%s'\n",
935 tcon->global->tcon_global_id,
936 tcon->global->share_name,
937 hex_encode_talloc(local_rec, key.dptr,
938 key.dsize)));
939 error = NT_STATUS_INTERNAL_ERROR;
943 if (local_rec != NULL) {
944 status = dbwrap_record_delete(local_rec);
945 if (!NT_STATUS_IS_OK(status)) {
946 TDB_DATA key = dbwrap_record_get_key(local_rec);
948 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
949 "failed to delete local key '%s': %s\n",
950 tcon->global->tcon_global_id,
951 tcon->global->share_name,
952 hex_encode_talloc(local_rec, key.dptr,
953 key.dsize),
954 nt_errstr(status)));
955 error = status;
957 table->local.num_tcons -= 1;
959 if (tcon->db_rec == NULL) {
960 TALLOC_FREE(local_rec);
962 tcon->db_rec = NULL;
964 if (tcon->compat) {
965 bool ok;
967 ok = set_current_service(tcon->compat, 0, true);
968 if (!ok) {
969 status = NT_STATUS_INTERNAL_ERROR;
970 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
971 "set_current_service() failed: %s\n",
972 tcon->global->tcon_global_id,
973 tcon->global->share_name,
974 nt_errstr(status)));
975 tcon->compat = NULL;
976 return status;
979 close_cnum(tcon->compat, vuid);
980 tcon->compat = NULL;
983 return error;
986 struct smbXsrv_tcon_disconnect_all_state {
987 uint64_t vuid;
988 NTSTATUS first_status;
989 int errors;
992 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
993 void *private_data);
995 static NTSTATUS smbXsrv_tcon_disconnect_all(struct smbXsrv_tcon_table *table,
996 uint64_t vuid)
998 struct smbXsrv_tcon_disconnect_all_state state;
999 NTSTATUS status;
1000 int count = 0;
1002 if (table == NULL) {
1003 return NT_STATUS_OK;
1006 ZERO_STRUCT(state);
1007 state.vuid = vuid;
1009 status = dbwrap_traverse(table->local.db_ctx,
1010 smbXsrv_tcon_disconnect_all_callback,
1011 &state, &count);
1012 if (!NT_STATUS_IS_OK(status)) {
1013 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1014 "dbwrap_traverse() failed: %s\n",
1015 nt_errstr(status)));
1016 return status;
1019 if (!NT_STATUS_IS_OK(state.first_status)) {
1020 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1021 "count[%d] errors[%d] first[%s]\n",
1022 count, state.errors,
1023 nt_errstr(state.first_status)));
1024 return state.first_status;
1027 return NT_STATUS_OK;
1030 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
1031 void *private_data)
1033 struct smbXsrv_tcon_disconnect_all_state *state =
1034 (struct smbXsrv_tcon_disconnect_all_state *)private_data;
1035 TDB_DATA val;
1036 void *ptr = NULL;
1037 struct smbXsrv_tcon *tcon = NULL;
1038 uint64_t vuid;
1039 NTSTATUS status;
1041 val = dbwrap_record_get_value(local_rec);
1042 if (val.dsize != sizeof(ptr)) {
1043 status = NT_STATUS_INTERNAL_ERROR;
1044 if (NT_STATUS_IS_OK(state->first_status)) {
1045 state->first_status = status;
1047 state->errors++;
1048 return 0;
1051 memcpy(&ptr, val.dptr, val.dsize);
1052 tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
1054 vuid = state->vuid;
1055 if (vuid == 0 && tcon->compat) {
1056 vuid = tcon->compat->vuid;
1059 tcon->db_rec = local_rec;
1060 status = smbXsrv_tcon_disconnect(tcon, vuid);
1061 if (!NT_STATUS_IS_OK(status)) {
1062 if (NT_STATUS_IS_OK(state->first_status)) {
1063 state->first_status = status;
1065 state->errors++;
1066 return 0;
1069 return 0;
1072 NTSTATUS smb1srv_tcon_table_init(struct smbXsrv_connection *conn)
1075 * Allow a range from 1..65534 with 65534 values.
1077 conn->tcon_table = talloc_zero(conn, struct smbXsrv_tcon_table);
1078 if (conn->tcon_table == NULL) {
1079 return NT_STATUS_NO_MEMORY;
1082 return smbXsrv_tcon_table_init(conn, conn->tcon_table,
1083 1, UINT16_MAX - 1,
1084 UINT16_MAX - 1);
1087 NTSTATUS smb1srv_tcon_create(struct smbXsrv_connection *conn,
1088 NTTIME now,
1089 struct smbXsrv_tcon **_tcon)
1091 struct server_id id = messaging_server_id(conn->msg_ctx);
1093 return smbXsrv_tcon_create(conn->tcon_table,
1094 conn->protocol,
1095 id, now, _tcon);
1098 NTSTATUS smb1srv_tcon_lookup(struct smbXsrv_connection *conn,
1099 uint16_t tree_id, NTTIME now,
1100 struct smbXsrv_tcon **tcon)
1102 uint32_t local_id = tree_id;
1104 return smbXsrv_tcon_local_lookup(conn->tcon_table,
1105 local_id, now, tcon);
1108 NTSTATUS smb1srv_tcon_disconnect_all(struct smbXsrv_connection *conn)
1111 * We do not pass a vuid here,
1112 * which means the vuid is taken from
1113 * the tcon->compat->vuid.
1115 * NOTE: that tcon->compat->vuid may point to
1116 * a none existing vuid (or the wrong one)
1117 * as the tcon can exist without a session
1118 * in SMB1.
1120 * This matches the old behavior of
1121 * conn_close_all(), but we should think
1122 * about how to fix this in future.
1124 return smbXsrv_tcon_disconnect_all(conn->tcon_table, 0);
1127 NTSTATUS smb2srv_tcon_table_init(struct smbXsrv_session *session)
1130 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
1132 session->tcon_table = talloc_zero(session, struct smbXsrv_tcon_table);
1133 if (session->tcon_table == NULL) {
1134 return NT_STATUS_NO_MEMORY;
1137 return smbXsrv_tcon_table_init(session, session->tcon_table,
1138 1, UINT32_MAX - 1,
1139 UINT16_MAX - 1);
1142 NTSTATUS smb2srv_tcon_create(struct smbXsrv_session *session,
1143 NTTIME now,
1144 struct smbXsrv_tcon **_tcon)
1146 struct server_id id = messaging_server_id(session->connection->msg_ctx);
1148 return smbXsrv_tcon_create(session->tcon_table,
1149 PROTOCOL_SMB2_02,
1150 id, now, _tcon);
1153 NTSTATUS smb2srv_tcon_lookup(struct smbXsrv_session *session,
1154 uint32_t tree_id, NTTIME now,
1155 struct smbXsrv_tcon **tcon)
1157 uint32_t local_id = tree_id;
1159 return smbXsrv_tcon_local_lookup(session->tcon_table,
1160 local_id, now, tcon);
1163 NTSTATUS smb2srv_tcon_disconnect_all(struct smbXsrv_session *session)
1165 uint64_t vuid;
1167 if (session->compat) {
1168 vuid = session->compat->vuid;
1169 } else {
1170 vuid = 0;
1173 return smbXsrv_tcon_disconnect_all(session->tcon_table, vuid);
1176 struct smbXsrv_tcon_global_traverse_state {
1177 int (*fn)(struct smbXsrv_tcon_global0 *, void *);
1178 void *private_data;
1181 static int smbXsrv_tcon_global_traverse_fn(struct db_record *rec, void *data)
1183 int ret = -1;
1184 struct smbXsrv_tcon_global_traverse_state *state =
1185 (struct smbXsrv_tcon_global_traverse_state*)data;
1186 TDB_DATA key = dbwrap_record_get_key(rec);
1187 TDB_DATA val = dbwrap_record_get_value(rec);
1188 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1189 struct smbXsrv_tcon_globalB global_blob;
1190 enum ndr_err_code ndr_err;
1191 TALLOC_CTX *frame = talloc_stackframe();
1193 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1194 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_tcon_globalB);
1195 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1196 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1197 "key '%s' ndr_pull_struct_blob - %s\n",
1198 hex_encode_talloc(frame, key.dptr, key.dsize),
1199 ndr_errstr(ndr_err)));
1200 goto done;
1203 if (global_blob.version != SMBXSRV_VERSION_0) {
1204 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1205 "key '%s' unsuported version - %d\n",
1206 hex_encode_talloc(frame, key.dptr, key.dsize),
1207 (int)global_blob.version));
1208 goto done;
1211 global_blob.info.info0->db_rec = rec;
1212 ret = state->fn(global_blob.info.info0, state->private_data);
1213 done:
1214 TALLOC_FREE(frame);
1215 return ret;
1218 NTSTATUS smbXsrv_tcon_global_traverse(
1219 int (*fn)(struct smbXsrv_tcon_global0 *, void *),
1220 void *private_data)
1222 NTSTATUS status;
1223 int count = 0;
1224 struct smbXsrv_tcon_global_traverse_state state = {
1225 .fn = fn,
1226 .private_data = private_data,
1229 become_root();
1230 status = smbXsrv_tcon_global_init();
1231 if (!NT_STATUS_IS_OK(status)) {
1232 unbecome_root();
1233 DEBUG(0, ("Failed to initialize tcon_global: %s\n",
1234 nt_errstr(status)));
1235 return status;
1238 status = dbwrap_traverse_read(smbXsrv_tcon_global_db_ctx,
1239 smbXsrv_tcon_global_traverse_fn,
1240 &state,
1241 &count);
1242 unbecome_root();
1244 return status;