smbd: Rename lck2->rw_probe in brl_conflict_other
[Samba.git] / source3 / smbd / smbXsrv_tcon.c
blob2cbd7613677f13b458a89e39fe55c007f9c67998
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_connection *conn,
701 struct smbXsrv_tcon_table *table,
702 NTTIME now,
703 struct smbXsrv_tcon **_tcon)
705 struct db_record *local_rec = NULL;
706 struct smbXsrv_tcon *tcon = NULL;
707 void *ptr = NULL;
708 TDB_DATA val;
709 struct smbXsrv_tcon_global0 *global = NULL;
710 NTSTATUS status;
712 if (table->local.num_tcons >= table->local.max_tcons) {
713 return NT_STATUS_INSUFFICIENT_RESOURCES;
716 tcon = talloc_zero(table, struct smbXsrv_tcon);
717 if (tcon == NULL) {
718 return NT_STATUS_NO_MEMORY;
720 tcon->table = table;
721 tcon->status = NT_STATUS_INTERNAL_ERROR;
722 tcon->idle_time = now;
724 status = smbXsrv_tcon_global_allocate(table->global.db_ctx,
725 tcon, &global);
726 if (!NT_STATUS_IS_OK(status)) {
727 TALLOC_FREE(tcon);
728 return status;
730 tcon->global = global;
732 if (conn->protocol >= PROTOCOL_SMB2_02) {
733 uint64_t id = global->tcon_global_id;
734 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
735 TDB_DATA key;
737 global->tcon_wire_id = id;
739 tcon->local_id = global->tcon_global_id;
741 key = smbXsrv_tcon_local_id_to_key(tcon->local_id, key_buf);
743 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
744 tcon, key);
745 if (local_rec == NULL) {
746 TALLOC_FREE(tcon);
747 return NT_STATUS_NO_MEMORY;
750 val = dbwrap_record_get_value(local_rec);
751 if (val.dsize != 0) {
752 TALLOC_FREE(tcon);
753 return NT_STATUS_INTERNAL_DB_CORRUPTION;
755 } else {
757 status = smb1srv_tcon_local_allocate_id(table->local.db_ctx,
758 table->local.lowest_id,
759 table->local.highest_id,
760 tcon,
761 &local_rec,
762 &tcon->local_id);
763 if (!NT_STATUS_IS_OK(status)) {
764 TALLOC_FREE(tcon);
765 return status;
768 global->tcon_wire_id = tcon->local_id;
771 global->creation_time = now;
773 global->server_id = messaging_server_id(conn->msg_ctx);
775 ptr = tcon;
776 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
777 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
778 TALLOC_FREE(local_rec);
779 if (!NT_STATUS_IS_OK(status)) {
780 TALLOC_FREE(tcon);
781 return status;
783 table->local.num_tcons += 1;
785 talloc_set_destructor(tcon, smbXsrv_tcon_destructor);
787 status = smbXsrv_tcon_global_store(global);
788 if (!NT_STATUS_IS_OK(status)) {
789 DEBUG(0,("smbXsrv_tcon_create: "
790 "global_id (0x%08x) store failed - %s\n",
791 tcon->global->tcon_global_id,
792 nt_errstr(status)));
793 TALLOC_FREE(tcon);
794 return status;
797 if (DEBUGLVL(10)) {
798 struct smbXsrv_tconB tcon_blob;
800 ZERO_STRUCT(tcon_blob);
801 tcon_blob.version = SMBXSRV_VERSION_0;
802 tcon_blob.info.info0 = tcon;
804 DEBUG(10,("smbXsrv_tcon_create: global_id (0x%08x) stored\n",
805 tcon->global->tcon_global_id));
806 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
809 *_tcon = tcon;
810 return NT_STATUS_OK;
813 NTSTATUS smbXsrv_tcon_update(struct smbXsrv_tcon *tcon)
815 struct smbXsrv_tcon_table *table = tcon->table;
816 NTSTATUS status;
817 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
818 TDB_DATA key;
820 if (tcon->global->db_rec != NULL) {
821 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
822 "Called with db_rec != NULL'\n",
823 tcon->global->tcon_global_id));
824 return NT_STATUS_INTERNAL_ERROR;
827 key = smbXsrv_tcon_global_id_to_key(tcon->global->tcon_global_id,
828 key_buf);
830 tcon->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
831 tcon->global, key);
832 if (tcon->global->db_rec == NULL) {
833 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
834 "Failed to lock global key '%s'\n",
835 tcon->global->tcon_global_id,
836 hex_encode_talloc(talloc_tos(), key.dptr,
837 key.dsize)));
838 return NT_STATUS_INTERNAL_DB_ERROR;
841 status = smbXsrv_tcon_global_store(tcon->global);
842 if (!NT_STATUS_IS_OK(status)) {
843 DEBUG(0,("smbXsrv_tcon_update: "
844 "global_id (0x%08x) store failed - %s\n",
845 tcon->global->tcon_global_id,
846 nt_errstr(status)));
847 return status;
850 if (DEBUGLVL(10)) {
851 struct smbXsrv_tconB tcon_blob;
853 ZERO_STRUCT(tcon_blob);
854 tcon_blob.version = SMBXSRV_VERSION_0;
855 tcon_blob.info.info0 = tcon;
857 DEBUG(10,("smbXsrv_tcon_update: global_id (0x%08x) stored\n",
858 tcon->global->tcon_global_id));
859 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
862 return NT_STATUS_OK;
865 NTSTATUS smbXsrv_tcon_disconnect(struct smbXsrv_tcon *tcon, uint64_t vuid)
867 struct smbXsrv_tcon_table *table;
868 struct db_record *local_rec = NULL;
869 struct db_record *global_rec = NULL;
870 NTSTATUS status;
871 NTSTATUS error = NT_STATUS_OK;
873 if (tcon->table == NULL) {
874 return NT_STATUS_OK;
877 table = tcon->table;
878 tcon->table = NULL;
880 tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
882 global_rec = tcon->global->db_rec;
883 tcon->global->db_rec = NULL;
884 if (global_rec == NULL) {
885 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
886 TDB_DATA key;
888 key = smbXsrv_tcon_global_id_to_key(
889 tcon->global->tcon_global_id,
890 key_buf);
892 global_rec = dbwrap_fetch_locked(table->global.db_ctx,
893 tcon->global, key);
894 if (global_rec == NULL) {
895 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
896 "Failed to lock global key '%s'\n",
897 tcon->global->tcon_global_id,
898 tcon->global->share_name,
899 hex_encode_talloc(global_rec, key.dptr,
900 key.dsize)));
901 error = NT_STATUS_INTERNAL_ERROR;
905 if (global_rec != NULL) {
906 status = dbwrap_record_delete(global_rec);
907 if (!NT_STATUS_IS_OK(status)) {
908 TDB_DATA key = dbwrap_record_get_key(global_rec);
910 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
911 "failed to delete global key '%s': %s\n",
912 tcon->global->tcon_global_id,
913 tcon->global->share_name,
914 hex_encode_talloc(global_rec, key.dptr,
915 key.dsize),
916 nt_errstr(status)));
917 error = status;
920 TALLOC_FREE(global_rec);
922 local_rec = tcon->db_rec;
923 if (local_rec == NULL) {
924 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
925 TDB_DATA key;
927 key = smbXsrv_tcon_local_id_to_key(tcon->local_id, key_buf);
929 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
930 tcon, key);
931 if (local_rec == NULL) {
932 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
933 "Failed to lock local key '%s'\n",
934 tcon->global->tcon_global_id,
935 tcon->global->share_name,
936 hex_encode_talloc(local_rec, key.dptr,
937 key.dsize)));
938 error = NT_STATUS_INTERNAL_ERROR;
942 if (local_rec != NULL) {
943 status = dbwrap_record_delete(local_rec);
944 if (!NT_STATUS_IS_OK(status)) {
945 TDB_DATA key = dbwrap_record_get_key(local_rec);
947 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
948 "failed to delete local key '%s': %s\n",
949 tcon->global->tcon_global_id,
950 tcon->global->share_name,
951 hex_encode_talloc(local_rec, key.dptr,
952 key.dsize),
953 nt_errstr(status)));
954 error = status;
956 table->local.num_tcons -= 1;
958 if (tcon->db_rec == NULL) {
959 TALLOC_FREE(local_rec);
961 tcon->db_rec = NULL;
963 if (tcon->compat) {
964 bool ok;
966 ok = set_current_service(tcon->compat, 0, true);
967 if (!ok) {
968 status = NT_STATUS_INTERNAL_ERROR;
969 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
970 "set_current_service() failed: %s\n",
971 tcon->global->tcon_global_id,
972 tcon->global->share_name,
973 nt_errstr(status)));
974 tcon->compat = NULL;
975 return status;
978 close_cnum(tcon->compat, vuid);
979 tcon->compat = NULL;
982 return error;
985 struct smbXsrv_tcon_disconnect_all_state {
986 uint64_t vuid;
987 NTSTATUS first_status;
988 int errors;
991 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
992 void *private_data);
994 static NTSTATUS smbXsrv_tcon_disconnect_all(struct smbXsrv_tcon_table *table,
995 uint64_t vuid)
997 struct smbXsrv_tcon_disconnect_all_state state;
998 NTSTATUS status;
999 int count = 0;
1001 if (table == NULL) {
1002 return NT_STATUS_OK;
1005 ZERO_STRUCT(state);
1006 state.vuid = vuid;
1008 status = dbwrap_traverse(table->local.db_ctx,
1009 smbXsrv_tcon_disconnect_all_callback,
1010 &state, &count);
1011 if (!NT_STATUS_IS_OK(status)) {
1012 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1013 "dbwrap_traverse() failed: %s\n",
1014 nt_errstr(status)));
1015 return status;
1018 if (!NT_STATUS_IS_OK(state.first_status)) {
1019 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1020 "count[%d] errors[%d] first[%s]\n",
1021 count, state.errors,
1022 nt_errstr(state.first_status)));
1023 return state.first_status;
1026 return NT_STATUS_OK;
1029 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
1030 void *private_data)
1032 struct smbXsrv_tcon_disconnect_all_state *state =
1033 (struct smbXsrv_tcon_disconnect_all_state *)private_data;
1034 TDB_DATA val;
1035 void *ptr = NULL;
1036 struct smbXsrv_tcon *tcon = NULL;
1037 uint64_t vuid;
1038 NTSTATUS status;
1040 val = dbwrap_record_get_value(local_rec);
1041 if (val.dsize != sizeof(ptr)) {
1042 status = NT_STATUS_INTERNAL_ERROR;
1043 if (NT_STATUS_IS_OK(state->first_status)) {
1044 state->first_status = status;
1046 state->errors++;
1047 return 0;
1050 memcpy(&ptr, val.dptr, val.dsize);
1051 tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
1053 vuid = state->vuid;
1054 if (vuid == 0 && tcon->compat) {
1055 vuid = tcon->compat->vuid;
1058 tcon->db_rec = local_rec;
1059 status = smbXsrv_tcon_disconnect(tcon, vuid);
1060 if (!NT_STATUS_IS_OK(status)) {
1061 if (NT_STATUS_IS_OK(state->first_status)) {
1062 state->first_status = status;
1064 state->errors++;
1065 return 0;
1068 return 0;
1071 NTSTATUS smb1srv_tcon_table_init(struct smbXsrv_connection *conn)
1074 * Allow a range from 1..65534 with 65534 values.
1076 conn->tcon_table = talloc_zero(conn, struct smbXsrv_tcon_table);
1077 if (conn->tcon_table == NULL) {
1078 return NT_STATUS_NO_MEMORY;
1081 return smbXsrv_tcon_table_init(conn, conn->tcon_table,
1082 1, UINT16_MAX - 1,
1083 UINT16_MAX - 1);
1086 NTSTATUS smb1srv_tcon_create(struct smbXsrv_connection *conn,
1087 NTTIME now,
1088 struct smbXsrv_tcon **_tcon)
1090 return smbXsrv_tcon_create(conn, conn->tcon_table, now,
1091 _tcon);
1094 NTSTATUS smb1srv_tcon_lookup(struct smbXsrv_connection *conn,
1095 uint16_t tree_id, NTTIME now,
1096 struct smbXsrv_tcon **tcon)
1098 uint32_t local_id = tree_id;
1100 return smbXsrv_tcon_local_lookup(conn->tcon_table,
1101 local_id, now, tcon);
1104 NTSTATUS smb1srv_tcon_disconnect_all(struct smbXsrv_connection *conn)
1107 * We do not pass a vuid here,
1108 * which means the vuid is taken from
1109 * the tcon->compat->vuid.
1111 * NOTE: that tcon->compat->vuid may point to
1112 * a none existing vuid (or the wrong one)
1113 * as the tcon can exist without a session
1114 * in SMB1.
1116 * This matches the old behavior of
1117 * conn_close_all(), but we should think
1118 * about how to fix this in future.
1120 return smbXsrv_tcon_disconnect_all(conn->tcon_table, 0);
1123 NTSTATUS smb2srv_tcon_table_init(struct smbXsrv_session *session)
1126 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
1128 session->tcon_table = talloc_zero(session, struct smbXsrv_tcon_table);
1129 if (session->tcon_table == NULL) {
1130 return NT_STATUS_NO_MEMORY;
1133 return smbXsrv_tcon_table_init(session, session->tcon_table,
1134 1, UINT32_MAX - 1,
1135 UINT16_MAX - 1);
1138 NTSTATUS smb2srv_tcon_create(struct smbXsrv_session *session,
1139 NTTIME now,
1140 struct smbXsrv_tcon **_tcon)
1142 return smbXsrv_tcon_create(session->connection, session->tcon_table,
1143 now, _tcon);
1146 NTSTATUS smb2srv_tcon_lookup(struct smbXsrv_session *session,
1147 uint32_t tree_id, NTTIME now,
1148 struct smbXsrv_tcon **tcon)
1150 uint32_t local_id = tree_id;
1152 return smbXsrv_tcon_local_lookup(session->tcon_table,
1153 local_id, now, tcon);
1156 NTSTATUS smb2srv_tcon_disconnect_all(struct smbXsrv_session *session)
1158 uint64_t vuid;
1160 if (session->compat) {
1161 vuid = session->compat->vuid;
1162 } else {
1163 vuid = 0;
1166 return smbXsrv_tcon_disconnect_all(session->tcon_table, vuid);
1169 struct smbXsrv_tcon_global_traverse_state {
1170 int (*fn)(struct smbXsrv_tcon_global0 *, void *);
1171 void *private_data;
1174 static int smbXsrv_tcon_global_traverse_fn(struct db_record *rec, void *data)
1176 int ret = -1;
1177 struct smbXsrv_tcon_global_traverse_state *state =
1178 (struct smbXsrv_tcon_global_traverse_state*)data;
1179 TDB_DATA key = dbwrap_record_get_key(rec);
1180 TDB_DATA val = dbwrap_record_get_value(rec);
1181 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1182 struct smbXsrv_tcon_globalB global_blob;
1183 enum ndr_err_code ndr_err;
1184 TALLOC_CTX *frame = talloc_stackframe();
1186 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1187 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_tcon_globalB);
1188 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1189 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1190 "key '%s' ndr_pull_struct_blob - %s\n",
1191 hex_encode_talloc(frame, key.dptr, key.dsize),
1192 ndr_errstr(ndr_err)));
1193 goto done;
1196 if (global_blob.version != SMBXSRV_VERSION_0) {
1197 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1198 "key '%s' unsuported version - %d\n",
1199 hex_encode_talloc(frame, key.dptr, key.dsize),
1200 (int)global_blob.version));
1201 goto done;
1204 global_blob.info.info0->db_rec = rec;
1205 ret = state->fn(global_blob.info.info0, state->private_data);
1206 done:
1207 TALLOC_FREE(frame);
1208 return ret;
1211 NTSTATUS smbXsrv_tcon_global_traverse(
1212 int (*fn)(struct smbXsrv_tcon_global0 *, void *),
1213 void *private_data)
1215 NTSTATUS status;
1216 int count = 0;
1217 struct smbXsrv_tcon_global_traverse_state state = {
1218 .fn = fn,
1219 .private_data = private_data,
1222 become_root();
1223 status = smbXsrv_tcon_global_init();
1224 if (!NT_STATUS_IS_OK(status)) {
1225 unbecome_root();
1226 DEBUG(0, ("Failed to initialize tcon_global: %s\n",
1227 nt_errstr(status)));
1228 return status;
1231 status = dbwrap_traverse_read(smbXsrv_tcon_global_db_ctx,
1232 smbXsrv_tcon_global_traverse_fn,
1233 &state,
1234 &count);
1235 unbecome_root();
1237 return status;