nsswitch: explicitly mark nss_module_register() _PUBLIC_ on FreeBSD
[Samba.git] / source3 / smbd / smbXsrv_tcon.c
blob6b105522855489955119360b3dfd1d818cc0371e
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 "lib/util/server_id.h"
24 #include "smbd/smbd.h"
25 #include "smbd/globals.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_rbt.h"
28 #include "dbwrap/dbwrap_open.h"
29 #include "messages.h"
30 #include "lib/util/util_tdb.h"
31 #include "librpc/gen_ndr/ndr_smbXsrv.h"
32 #include "serverid.h"
34 struct smbXsrv_tcon_table {
35 struct {
36 struct db_context *db_ctx;
37 uint32_t lowest_id;
38 uint32_t highest_id;
39 uint32_t max_tcons;
40 uint32_t num_tcons;
41 } local;
42 struct {
43 struct db_context *db_ctx;
44 } global;
47 static struct db_context *smbXsrv_tcon_global_db_ctx = NULL;
49 NTSTATUS smbXsrv_tcon_global_init(void)
51 char *global_path = NULL;
52 struct db_context *db_ctx = NULL;
54 if (smbXsrv_tcon_global_db_ctx != NULL) {
55 return NT_STATUS_OK;
58 global_path = lock_path(talloc_tos(), "smbXsrv_tcon_global.tdb");
59 if (global_path == NULL) {
60 return NT_STATUS_NO_MEMORY;
63 db_ctx = db_open(NULL, global_path,
64 0, /* hash_size */
65 TDB_DEFAULT |
66 TDB_CLEAR_IF_FIRST |
67 TDB_INCOMPATIBLE_HASH,
68 O_RDWR | O_CREAT, 0600,
69 DBWRAP_LOCK_ORDER_1,
70 DBWRAP_FLAG_NONE);
71 TALLOC_FREE(global_path);
72 if (db_ctx == NULL) {
73 NTSTATUS status;
75 status = map_nt_error_from_unix_common(errno);
77 return status;
80 smbXsrv_tcon_global_db_ctx = db_ctx;
82 return NT_STATUS_OK;
86 * NOTE:
87 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
88 * has the same result as integer comparison between the uint32_t
89 * values.
91 * TODO: implement string based key
94 #define SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
96 static TDB_DATA smbXsrv_tcon_global_id_to_key(uint32_t id,
97 uint8_t *key_buf)
99 TDB_DATA key;
101 RSIVAL(key_buf, 0, id);
103 key = make_tdb_data(key_buf, SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE);
105 return key;
108 #if 0
109 static NTSTATUS smbXsrv_tcon_global_key_to_id(TDB_DATA key, uint32_t *id)
111 if (id == NULL) {
112 return NT_STATUS_INVALID_PARAMETER;
115 if (key.dsize != SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE) {
116 return NT_STATUS_INTERNAL_DB_CORRUPTION;
119 *id = RIVAL(key.dptr, 0);
121 return NT_STATUS_OK;
123 #endif
125 #define SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
127 static TDB_DATA smbXsrv_tcon_local_id_to_key(uint32_t id,
128 uint8_t *key_buf)
130 TDB_DATA key;
132 RSIVAL(key_buf, 0, id);
134 key = make_tdb_data(key_buf, SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE);
136 return key;
139 static NTSTATUS smbXsrv_tcon_local_key_to_id(TDB_DATA key, uint32_t *id)
141 if (id == NULL) {
142 return NT_STATUS_INVALID_PARAMETER;
145 if (key.dsize != SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE) {
146 return NT_STATUS_INTERNAL_DB_CORRUPTION;
149 *id = RIVAL(key.dptr, 0);
151 return NT_STATUS_OK;
154 static struct db_record *smbXsrv_tcon_global_fetch_locked(
155 struct db_context *db,
156 uint32_t id,
157 TALLOC_CTX *mem_ctx)
159 TDB_DATA key;
160 uint8_t key_buf[SMBXSRV_TCON_GLOBAL_TDB_KEY_SIZE];
161 struct db_record *rec = NULL;
163 key = smbXsrv_tcon_global_id_to_key(id, key_buf);
165 rec = dbwrap_fetch_locked(db, mem_ctx, key);
167 if (rec == NULL) {
168 DBG_DEBUG("Failed to lock global id 0x%08x, key '%s'\n", id,
169 hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
172 return rec;
175 static struct db_record *smbXsrv_tcon_local_fetch_locked(
176 struct db_context *db,
177 uint32_t id,
178 TALLOC_CTX *mem_ctx)
180 TDB_DATA key;
181 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
182 struct db_record *rec = NULL;
184 key = smbXsrv_tcon_local_id_to_key(id, key_buf);
186 rec = dbwrap_fetch_locked(db, mem_ctx, key);
188 if (rec == NULL) {
189 DBG_DEBUG("Failed to lock local id 0x%08x, key '%s'\n", id,
190 hex_encode_talloc(talloc_tos(), key.dptr, key.dsize));
193 return rec;
196 static NTSTATUS smbXsrv_tcon_table_init(TALLOC_CTX *mem_ctx,
197 struct smbXsrv_tcon_table *table,
198 uint32_t lowest_id,
199 uint32_t highest_id,
200 uint32_t max_tcons)
202 NTSTATUS status;
203 uint64_t max_range;
205 if (lowest_id > highest_id) {
206 return NT_STATUS_INTERNAL_ERROR;
209 max_range = highest_id;
210 max_range -= lowest_id;
211 max_range += 1;
213 if (max_tcons > max_range) {
214 return NT_STATUS_INTERNAL_ERROR;
217 ZERO_STRUCTP(table);
218 table->local.db_ctx = db_open_rbt(table);
219 if (table->local.db_ctx == NULL) {
220 return NT_STATUS_NO_MEMORY;
222 table->local.lowest_id = lowest_id;
223 table->local.highest_id = highest_id;
224 table->local.max_tcons = max_tcons;
226 status = smbXsrv_tcon_global_init();
227 if (!NT_STATUS_IS_OK(status)) {
228 return status;
231 table->global.db_ctx = smbXsrv_tcon_global_db_ctx;
233 return NT_STATUS_OK;
236 struct smb1srv_tcon_local_allocate_state {
237 const uint32_t lowest_id;
238 const uint32_t highest_id;
239 uint32_t last_id;
240 uint32_t useable_id;
241 NTSTATUS status;
244 static int smb1srv_tcon_local_allocate_traverse(struct db_record *rec,
245 void *private_data)
247 struct smb1srv_tcon_local_allocate_state *state =
248 (struct smb1srv_tcon_local_allocate_state *)private_data;
249 TDB_DATA key = dbwrap_record_get_key(rec);
250 uint32_t id = 0;
251 NTSTATUS status;
253 status = smbXsrv_tcon_local_key_to_id(key, &id);
254 if (!NT_STATUS_IS_OK(status)) {
255 state->status = status;
256 return -1;
259 if (id <= state->last_id) {
260 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
261 return -1;
263 state->last_id = id;
265 if (id > state->useable_id) {
266 state->status = NT_STATUS_OK;
267 return -1;
270 if (state->useable_id == state->highest_id) {
271 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
272 return -1;
275 state->useable_id +=1;
276 return 0;
279 static NTSTATUS smb1srv_tcon_local_allocate_id(struct db_context *db,
280 uint32_t lowest_id,
281 uint32_t highest_id,
282 TALLOC_CTX *mem_ctx,
283 struct db_record **_rec,
284 uint32_t *_id)
286 struct smb1srv_tcon_local_allocate_state state = {
287 .lowest_id = lowest_id,
288 .highest_id = highest_id,
289 .last_id = 0,
290 .useable_id = lowest_id,
291 .status = NT_STATUS_INTERNAL_ERROR,
293 uint32_t i;
294 uint32_t range;
295 NTSTATUS status;
296 int count = 0;
298 *_rec = NULL;
299 *_id = 0;
301 if (lowest_id > highest_id) {
302 return NT_STATUS_INSUFFICIENT_RESOURCES;
306 * first we try randomly
308 range = (highest_id - lowest_id) + 1;
310 for (i = 0; i < (range / 2); i++) {
311 uint32_t id;
312 TDB_DATA val;
313 struct db_record *rec = NULL;
315 id = generate_random() % range;
316 id += lowest_id;
318 if (id < lowest_id) {
319 id = lowest_id;
321 if (id > highest_id) {
322 id = highest_id;
325 rec = smbXsrv_tcon_local_fetch_locked(db, id, mem_ctx);
326 if (rec == NULL) {
327 return NT_STATUS_INSUFFICIENT_RESOURCES;
330 val = dbwrap_record_get_value(rec);
331 if (val.dsize != 0) {
332 TALLOC_FREE(rec);
333 continue;
336 *_rec = rec;
337 *_id = id;
338 return NT_STATUS_OK;
342 * if the range is almost full,
343 * we traverse the whole table
344 * (this relies on sorted behavior of dbwrap_rbt)
346 status = dbwrap_traverse_read(db, smb1srv_tcon_local_allocate_traverse,
347 &state, &count);
348 if (NT_STATUS_IS_OK(status)) {
349 if (NT_STATUS_IS_OK(state.status)) {
350 return NT_STATUS_INTERNAL_ERROR;
353 if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
354 return state.status;
357 if (state.useable_id <= state.highest_id) {
358 state.status = NT_STATUS_OK;
359 } else {
360 return NT_STATUS_INSUFFICIENT_RESOURCES;
362 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
364 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
366 * If we get anything else it is an error, because it
367 * means we did not manage to find a free slot in
368 * the db.
370 return NT_STATUS_INSUFFICIENT_RESOURCES;
373 if (NT_STATUS_IS_OK(state.status)) {
374 uint32_t id;
375 TDB_DATA val;
376 struct db_record *rec = NULL;
378 id = state.useable_id;
380 rec = smbXsrv_tcon_local_fetch_locked(db, id, mem_ctx);
381 if (rec == NULL) {
382 return NT_STATUS_INSUFFICIENT_RESOURCES;
385 val = dbwrap_record_get_value(rec);
386 if (val.dsize != 0) {
387 TALLOC_FREE(rec);
388 return NT_STATUS_INTERNAL_DB_CORRUPTION;
391 *_rec = rec;
392 *_id = id;
393 return NT_STATUS_OK;
396 return state.status;
399 struct smbXsrv_tcon_local_fetch_state {
400 struct smbXsrv_tcon *tcon;
401 NTSTATUS status;
404 static void smbXsrv_tcon_local_fetch_parser(TDB_DATA key, TDB_DATA data,
405 void *private_data)
407 struct smbXsrv_tcon_local_fetch_state *state =
408 (struct smbXsrv_tcon_local_fetch_state *)private_data;
409 void *ptr;
411 if (data.dsize != sizeof(ptr)) {
412 state->status = NT_STATUS_INTERNAL_DB_ERROR;
413 return;
416 memcpy(&ptr, data.dptr, data.dsize);
417 state->tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
418 state->status = NT_STATUS_OK;
421 static NTSTATUS smbXsrv_tcon_local_lookup(struct smbXsrv_tcon_table *table,
422 uint32_t tcon_local_id,
423 NTTIME now,
424 struct smbXsrv_tcon **_tcon)
426 struct smbXsrv_tcon_local_fetch_state state = {
427 .tcon = NULL,
428 .status = NT_STATUS_INTERNAL_ERROR,
430 uint8_t key_buf[SMBXSRV_TCON_LOCAL_TDB_KEY_SIZE];
431 TDB_DATA key;
432 NTSTATUS status;
434 *_tcon = NULL;
436 if (tcon_local_id == 0) {
437 return NT_STATUS_NETWORK_NAME_DELETED;
440 if (table == NULL) {
441 /* this might happen before the end of negprot */
442 return NT_STATUS_NETWORK_NAME_DELETED;
445 if (table->local.db_ctx == NULL) {
446 return NT_STATUS_INTERNAL_ERROR;
449 key = smbXsrv_tcon_local_id_to_key(tcon_local_id, key_buf);
451 status = dbwrap_parse_record(table->local.db_ctx, key,
452 smbXsrv_tcon_local_fetch_parser,
453 &state);
454 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
455 return NT_STATUS_NETWORK_NAME_DELETED;
456 } else if (!NT_STATUS_IS_OK(status)) {
457 return status;
459 if (!NT_STATUS_IS_OK(state.status)) {
460 return state.status;
463 if (NT_STATUS_EQUAL(state.tcon->status, NT_STATUS_NETWORK_NAME_DELETED)) {
464 return NT_STATUS_NETWORK_NAME_DELETED;
467 state.tcon->idle_time = now;
469 *_tcon = state.tcon;
470 return state.tcon->status;
473 static int smbXsrv_tcon_global_destructor(struct smbXsrv_tcon_global0 *global)
475 return 0;
478 static void smbXsrv_tcon_global_verify_record(struct db_record *db_rec,
479 bool *is_free,
480 bool *was_free,
481 TALLOC_CTX *mem_ctx,
482 struct smbXsrv_tcon_global0 **_g);
484 static NTSTATUS smbXsrv_tcon_global_allocate(struct db_context *db,
485 TALLOC_CTX *mem_ctx,
486 struct smbXsrv_tcon_global0 **_global)
488 uint32_t i;
489 struct smbXsrv_tcon_global0 *global = NULL;
490 uint32_t last_free = 0;
491 const uint32_t min_tries = 3;
493 *_global = NULL;
495 global = talloc_zero(mem_ctx, struct smbXsrv_tcon_global0);
496 if (global == NULL) {
497 return NT_STATUS_NO_MEMORY;
499 talloc_set_destructor(global, smbXsrv_tcon_global_destructor);
502 * Here we just randomly try the whole 32-bit space
504 * We use just 32-bit, because we want to reuse the
505 * ID for SRVSVC.
507 for (i = 0; i < UINT32_MAX; i++) {
508 bool is_free = false;
509 bool was_free = false;
510 uint32_t id;
512 if (i >= min_tries && last_free != 0) {
513 id = last_free;
514 } else {
515 id = generate_random();
517 if (id == 0) {
518 id++;
520 if (id == UINT32_MAX) {
521 id--;
524 global->db_rec = smbXsrv_tcon_global_fetch_locked(db, id,
525 mem_ctx);
526 if (global->db_rec == NULL) {
527 talloc_free(global);
528 return NT_STATUS_INSUFFICIENT_RESOURCES;
531 smbXsrv_tcon_global_verify_record(global->db_rec,
532 &is_free,
533 &was_free,
534 NULL, NULL);
536 if (!is_free) {
537 TALLOC_FREE(global->db_rec);
538 continue;
541 if (!was_free && i < min_tries) {
543 * The session_id is free now,
544 * but was not free before.
546 * This happens if a smbd crashed
547 * and did not cleanup the record.
549 * If this is one of our first tries,
550 * then we try to find a real free one.
552 if (last_free == 0) {
553 last_free = id;
555 TALLOC_FREE(global->db_rec);
556 continue;
559 global->tcon_global_id = id;
561 *_global = global;
562 return NT_STATUS_OK;
565 /* should not be reached */
566 talloc_free(global);
567 return NT_STATUS_INTERNAL_ERROR;
570 static void smbXsrv_tcon_global_verify_record(struct db_record *db_rec,
571 bool *is_free,
572 bool *was_free,
573 TALLOC_CTX *mem_ctx,
574 struct smbXsrv_tcon_global0 **_g)
576 TDB_DATA key;
577 TDB_DATA val;
578 DATA_BLOB blob;
579 struct smbXsrv_tcon_globalB global_blob;
580 enum ndr_err_code ndr_err;
581 struct smbXsrv_tcon_global0 *global = NULL;
582 bool exists;
583 TALLOC_CTX *frame = talloc_stackframe();
585 *is_free = false;
587 if (was_free) {
588 *was_free = false;
590 if (_g) {
591 *_g = NULL;
594 key = dbwrap_record_get_key(db_rec);
596 val = dbwrap_record_get_value(db_rec);
597 if (val.dsize == 0) {
598 TALLOC_FREE(frame);
599 *is_free = true;
600 if (was_free) {
601 *was_free = true;
603 return;
606 blob = data_blob_const(val.dptr, val.dsize);
608 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
609 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_tcon_globalB);
610 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
611 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
612 DEBUG(1,("smbXsrv_tcon_global_verify_record: "
613 "key '%s' ndr_pull_struct_blob - %s\n",
614 hex_encode_talloc(frame, key.dptr, key.dsize),
615 nt_errstr(status)));
616 TALLOC_FREE(frame);
617 return;
620 DEBUG(10,("smbXsrv_tcon_global_verify_record\n"));
621 if (DEBUGLVL(10)) {
622 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
625 if (global_blob.version != SMBXSRV_VERSION_0) {
626 DEBUG(0,("smbXsrv_tcon_global_verify_record: "
627 "key '%s' use unsupported version %u\n",
628 hex_encode_talloc(frame, key.dptr, key.dsize),
629 global_blob.version));
630 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
631 TALLOC_FREE(frame);
632 return;
635 global = global_blob.info.info0;
637 exists = serverid_exists(&global->server_id);
638 if (!exists) {
639 struct server_id_buf idbuf;
640 DEBUG(2,("smbXsrv_tcon_global_verify_record: "
641 "key '%s' server_id %s does not exist.\n",
642 hex_encode_talloc(frame, key.dptr, key.dsize),
643 server_id_str_buf(global->server_id, &idbuf)));
644 if (DEBUGLVL(2)) {
645 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
647 TALLOC_FREE(frame);
648 dbwrap_record_delete(db_rec);
649 *is_free = true;
650 return;
653 if (_g) {
654 *_g = talloc_move(mem_ctx, &global);
656 TALLOC_FREE(frame);
659 static NTSTATUS smbXsrv_tcon_global_store(struct smbXsrv_tcon_global0 *global)
661 struct smbXsrv_tcon_globalB global_blob;
662 DATA_BLOB blob = data_blob_null;
663 TDB_DATA key;
664 TDB_DATA val;
665 NTSTATUS status;
666 enum ndr_err_code ndr_err;
669 * TODO: if we use other versions than '0'
670 * we would add glue code here, that would be able to
671 * store the information in the old format.
674 if (global->db_rec == NULL) {
675 return NT_STATUS_INTERNAL_ERROR;
678 key = dbwrap_record_get_key(global->db_rec);
679 val = dbwrap_record_get_value(global->db_rec);
681 ZERO_STRUCT(global_blob);
682 global_blob.version = smbXsrv_version_global_current();
683 if (val.dsize >= 8) {
684 global_blob.seqnum = IVAL(val.dptr, 4);
686 global_blob.seqnum += 1;
687 global_blob.info.info0 = global;
689 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
690 (ndr_push_flags_fn_t)ndr_push_smbXsrv_tcon_globalB);
691 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
692 status = ndr_map_error2ntstatus(ndr_err);
693 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' ndr_push - %s\n",
694 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
695 nt_errstr(status)));
696 TALLOC_FREE(global->db_rec);
697 return status;
700 val = make_tdb_data(blob.data, blob.length);
701 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
702 if (!NT_STATUS_IS_OK(status)) {
703 DEBUG(1,("smbXsrv_tcon_global_store: key '%s' store - %s\n",
704 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
705 nt_errstr(status)));
706 TALLOC_FREE(global->db_rec);
707 return status;
710 if (DEBUGLVL(10)) {
711 DEBUG(10,("smbXsrv_tcon_global_store: key '%s' stored\n",
712 hex_encode_talloc(global->db_rec, key.dptr, key.dsize)));
713 NDR_PRINT_DEBUG(smbXsrv_tcon_globalB, &global_blob);
716 TALLOC_FREE(global->db_rec);
718 return NT_STATUS_OK;
721 static int smbXsrv_tcon_destructor(struct smbXsrv_tcon *tcon)
723 NTSTATUS status;
725 status = smbXsrv_tcon_disconnect(tcon, 0);
726 if (!NT_STATUS_IS_OK(status)) {
727 DEBUG(0, ("smbXsrv_tcon_destructor: "
728 "smbXsrv_tcon_disconnect() failed - %s\n",
729 nt_errstr(status)));
732 TALLOC_FREE(tcon->global);
734 return 0;
737 static NTSTATUS smbXsrv_tcon_create(struct smbXsrv_tcon_table *table,
738 enum protocol_types protocol,
739 struct server_id server_id,
740 NTTIME now,
741 struct smbXsrv_tcon **_tcon)
743 struct db_record *local_rec = NULL;
744 struct smbXsrv_tcon *tcon = NULL;
745 void *ptr = NULL;
746 TDB_DATA val;
747 struct smbXsrv_tcon_global0 *global = NULL;
748 NTSTATUS status;
750 if (table->local.num_tcons >= table->local.max_tcons) {
751 return NT_STATUS_INSUFFICIENT_RESOURCES;
754 tcon = talloc_zero(table, struct smbXsrv_tcon);
755 if (tcon == NULL) {
756 return NT_STATUS_NO_MEMORY;
758 tcon->table = table;
759 tcon->status = NT_STATUS_INTERNAL_ERROR;
760 tcon->idle_time = now;
762 status = smbXsrv_tcon_global_allocate(table->global.db_ctx,
763 tcon, &global);
764 if (!NT_STATUS_IS_OK(status)) {
765 TALLOC_FREE(tcon);
766 return status;
768 tcon->global = global;
770 if (protocol >= PROTOCOL_SMB2_02) {
771 uint64_t id = global->tcon_global_id;
773 global->tcon_wire_id = id;
775 tcon->local_id = global->tcon_global_id;
777 local_rec = smbXsrv_tcon_local_fetch_locked(table->local.db_ctx,
778 tcon->local_id,
779 tcon /* TALLOC_CTX */);
780 if (local_rec == NULL) {
781 TALLOC_FREE(tcon);
782 return NT_STATUS_NO_MEMORY;
785 val = dbwrap_record_get_value(local_rec);
786 if (val.dsize != 0) {
787 TALLOC_FREE(tcon);
788 return NT_STATUS_INTERNAL_DB_CORRUPTION;
790 } else {
792 status = smb1srv_tcon_local_allocate_id(table->local.db_ctx,
793 table->local.lowest_id,
794 table->local.highest_id,
795 tcon,
796 &local_rec,
797 &tcon->local_id);
798 if (!NT_STATUS_IS_OK(status)) {
799 TALLOC_FREE(tcon);
800 return status;
803 global->tcon_wire_id = tcon->local_id;
806 global->creation_time = now;
808 global->server_id = server_id;
810 ptr = tcon;
811 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
812 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
813 TALLOC_FREE(local_rec);
814 if (!NT_STATUS_IS_OK(status)) {
815 TALLOC_FREE(tcon);
816 return status;
818 table->local.num_tcons += 1;
820 talloc_set_destructor(tcon, smbXsrv_tcon_destructor);
822 status = smbXsrv_tcon_global_store(global);
823 if (!NT_STATUS_IS_OK(status)) {
824 DEBUG(0,("smbXsrv_tcon_create: "
825 "global_id (0x%08x) store failed - %s\n",
826 tcon->global->tcon_global_id,
827 nt_errstr(status)));
828 TALLOC_FREE(tcon);
829 return status;
832 if (DEBUGLVL(10)) {
833 struct smbXsrv_tconB tcon_blob = {
834 .version = SMBXSRV_VERSION_0,
835 .info.info0 = tcon,
838 DEBUG(10,("smbXsrv_tcon_create: global_id (0x%08x) stored\n",
839 tcon->global->tcon_global_id));
840 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
843 *_tcon = tcon;
844 return NT_STATUS_OK;
847 NTSTATUS smbXsrv_tcon_update(struct smbXsrv_tcon *tcon)
849 struct smbXsrv_tcon_table *table = tcon->table;
850 NTSTATUS status;
852 if (tcon->global->db_rec != NULL) {
853 DEBUG(0, ("smbXsrv_tcon_update(0x%08x): "
854 "Called with db_rec != NULL'\n",
855 tcon->global->tcon_global_id));
856 return NT_STATUS_INTERNAL_ERROR;
859 tcon->global->db_rec = smbXsrv_tcon_global_fetch_locked(
860 table->global.db_ctx,
861 tcon->global->tcon_global_id,
862 tcon->global /* TALLOC_CTX */);
863 if (tcon->global->db_rec == NULL) {
864 return NT_STATUS_INTERNAL_DB_ERROR;
867 status = smbXsrv_tcon_global_store(tcon->global);
868 if (!NT_STATUS_IS_OK(status)) {
869 DEBUG(0,("smbXsrv_tcon_update: "
870 "global_id (0x%08x) store failed - %s\n",
871 tcon->global->tcon_global_id,
872 nt_errstr(status)));
873 return status;
876 if (DEBUGLVL(10)) {
877 struct smbXsrv_tconB tcon_blob = {
878 .version = SMBXSRV_VERSION_0,
879 .info.info0 = tcon,
882 DEBUG(10,("smbXsrv_tcon_update: global_id (0x%08x) stored\n",
883 tcon->global->tcon_global_id));
884 NDR_PRINT_DEBUG(smbXsrv_tconB, &tcon_blob);
887 return NT_STATUS_OK;
890 NTSTATUS smbXsrv_tcon_disconnect(struct smbXsrv_tcon *tcon, uint64_t vuid)
892 struct smbXsrv_tcon_table *table;
893 struct db_record *local_rec = NULL;
894 struct db_record *global_rec = NULL;
895 NTSTATUS status;
896 NTSTATUS error = NT_STATUS_OK;
898 if (tcon->table == NULL) {
899 return NT_STATUS_OK;
902 table = tcon->table;
903 tcon->table = NULL;
905 if (tcon->compat) {
906 bool ok;
908 ok = chdir_current_service(tcon->compat);
909 if (!ok) {
910 status = NT_STATUS_INTERNAL_ERROR;
911 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
912 "chdir_current_service() failed: %s\n",
913 tcon->global->tcon_global_id,
914 tcon->global->share_name,
915 nt_errstr(status)));
916 tcon->compat = NULL;
917 return status;
920 close_cnum(tcon->compat, vuid);
921 tcon->compat = NULL;
924 tcon->status = NT_STATUS_NETWORK_NAME_DELETED;
926 global_rec = tcon->global->db_rec;
927 tcon->global->db_rec = NULL;
928 if (global_rec == NULL) {
929 global_rec = smbXsrv_tcon_global_fetch_locked(
930 table->global.db_ctx,
931 tcon->global->tcon_global_id,
932 tcon->global /* TALLOC_CTX */);
933 if (global_rec == NULL) {
934 error = NT_STATUS_INTERNAL_ERROR;
938 if (global_rec != NULL) {
939 status = dbwrap_record_delete(global_rec);
940 if (!NT_STATUS_IS_OK(status)) {
941 TDB_DATA key = dbwrap_record_get_key(global_rec);
943 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
944 "failed to delete global key '%s': %s\n",
945 tcon->global->tcon_global_id,
946 tcon->global->share_name,
947 hex_encode_talloc(global_rec, key.dptr,
948 key.dsize),
949 nt_errstr(status)));
950 error = status;
953 TALLOC_FREE(global_rec);
955 local_rec = tcon->db_rec;
956 if (local_rec == NULL) {
957 local_rec = smbXsrv_tcon_local_fetch_locked(table->local.db_ctx,
958 tcon->local_id,
959 tcon /* TALLOC_CTX */);
960 if (local_rec == NULL) {
961 error = NT_STATUS_INTERNAL_ERROR;
965 if (local_rec != NULL) {
966 status = dbwrap_record_delete(local_rec);
967 if (!NT_STATUS_IS_OK(status)) {
968 TDB_DATA key = dbwrap_record_get_key(local_rec);
970 DEBUG(0, ("smbXsrv_tcon_disconnect(0x%08x, '%s'): "
971 "failed to delete local key '%s': %s\n",
972 tcon->global->tcon_global_id,
973 tcon->global->share_name,
974 hex_encode_talloc(local_rec, key.dptr,
975 key.dsize),
976 nt_errstr(status)));
977 error = status;
979 table->local.num_tcons -= 1;
981 if (tcon->db_rec == NULL) {
982 TALLOC_FREE(local_rec);
984 tcon->db_rec = NULL;
986 return error;
989 struct smbXsrv_tcon_disconnect_all_state {
990 uint64_t vuid;
991 NTSTATUS first_status;
992 int errors;
995 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
996 void *private_data);
998 static NTSTATUS smbXsrv_tcon_disconnect_all(struct smbXsrv_tcon_table *table,
999 uint64_t vuid)
1001 struct smbXsrv_tcon_disconnect_all_state state;
1002 NTSTATUS status;
1003 int count = 0;
1005 if (table == NULL) {
1006 return NT_STATUS_OK;
1009 ZERO_STRUCT(state);
1010 state.vuid = vuid;
1012 status = dbwrap_traverse(table->local.db_ctx,
1013 smbXsrv_tcon_disconnect_all_callback,
1014 &state, &count);
1015 if (!NT_STATUS_IS_OK(status)) {
1016 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1017 "dbwrap_traverse() failed: %s\n",
1018 nt_errstr(status)));
1019 return status;
1022 if (!NT_STATUS_IS_OK(state.first_status)) {
1023 DEBUG(0, ("smbXsrv_tcon_disconnect_all: "
1024 "count[%d] errors[%d] first[%s]\n",
1025 count, state.errors,
1026 nt_errstr(state.first_status)));
1027 return state.first_status;
1030 return NT_STATUS_OK;
1033 static int smbXsrv_tcon_disconnect_all_callback(struct db_record *local_rec,
1034 void *private_data)
1036 struct smbXsrv_tcon_disconnect_all_state *state =
1037 (struct smbXsrv_tcon_disconnect_all_state *)private_data;
1038 TDB_DATA val;
1039 void *ptr = NULL;
1040 struct smbXsrv_tcon *tcon = NULL;
1041 uint64_t vuid;
1042 NTSTATUS status;
1044 val = dbwrap_record_get_value(local_rec);
1045 if (val.dsize != sizeof(ptr)) {
1046 status = NT_STATUS_INTERNAL_ERROR;
1047 if (NT_STATUS_IS_OK(state->first_status)) {
1048 state->first_status = status;
1050 state->errors++;
1051 return 0;
1054 memcpy(&ptr, val.dptr, val.dsize);
1055 tcon = talloc_get_type_abort(ptr, struct smbXsrv_tcon);
1057 vuid = state->vuid;
1058 if (vuid == 0 && tcon->compat) {
1059 vuid = tcon->compat->vuid;
1062 tcon->db_rec = local_rec;
1063 status = smbXsrv_tcon_disconnect(tcon, vuid);
1064 tcon->db_rec = NULL;
1065 if (!NT_STATUS_IS_OK(status)) {
1066 if (NT_STATUS_IS_OK(state->first_status)) {
1067 state->first_status = status;
1069 state->errors++;
1070 return 0;
1073 return 0;
1076 NTSTATUS smb1srv_tcon_table_init(struct smbXsrv_connection *conn)
1078 struct smbXsrv_client *client = conn->client;
1081 * Allow a range from 1..65534 with 65534 values.
1083 client->tcon_table = talloc_zero(client, struct smbXsrv_tcon_table);
1084 if (client->tcon_table == NULL) {
1085 return NT_STATUS_NO_MEMORY;
1088 return smbXsrv_tcon_table_init(client, client->tcon_table,
1089 1, UINT16_MAX - 1,
1090 UINT16_MAX - 1);
1093 NTSTATUS smb1srv_tcon_create(struct smbXsrv_connection *conn,
1094 NTTIME now,
1095 struct smbXsrv_tcon **_tcon)
1097 struct server_id id = messaging_server_id(conn->client->msg_ctx);
1099 return smbXsrv_tcon_create(conn->client->tcon_table,
1100 conn->protocol,
1101 id, now, _tcon);
1104 NTSTATUS smb1srv_tcon_lookup(struct smbXsrv_connection *conn,
1105 uint16_t tree_id, NTTIME now,
1106 struct smbXsrv_tcon **tcon)
1108 uint32_t local_id = tree_id;
1110 return smbXsrv_tcon_local_lookup(conn->client->tcon_table,
1111 local_id, now, tcon);
1114 NTSTATUS smb1srv_tcon_disconnect_all(struct smbXsrv_client *client)
1118 * We do not pass a vuid here,
1119 * which means the vuid is taken from
1120 * the tcon->compat->vuid.
1122 * NOTE: that tcon->compat->vuid may point to
1123 * a none existing vuid (or the wrong one)
1124 * as the tcon can exist without a session
1125 * in SMB1.
1127 * This matches the old behavior of
1128 * conn_close_all(), but we should think
1129 * about how to fix this in future.
1131 return smbXsrv_tcon_disconnect_all(client->tcon_table, 0);
1134 NTSTATUS smb2srv_tcon_table_init(struct smbXsrv_session *session)
1137 * Allow a range from 1..4294967294 with 65534 (same as SMB1) values.
1139 session->tcon_table = talloc_zero(session, struct smbXsrv_tcon_table);
1140 if (session->tcon_table == NULL) {
1141 return NT_STATUS_NO_MEMORY;
1144 return smbXsrv_tcon_table_init(session, session->tcon_table,
1145 1, UINT32_MAX - 1,
1146 UINT16_MAX - 1);
1149 NTSTATUS smb2srv_tcon_create(struct smbXsrv_session *session,
1150 NTTIME now,
1151 struct smbXsrv_tcon **_tcon)
1153 struct server_id id = messaging_server_id(session->client->msg_ctx);
1155 return smbXsrv_tcon_create(session->tcon_table,
1156 PROTOCOL_SMB2_02,
1157 id, now, _tcon);
1160 NTSTATUS smb2srv_tcon_lookup(struct smbXsrv_session *session,
1161 uint32_t tree_id, NTTIME now,
1162 struct smbXsrv_tcon **tcon)
1164 uint32_t local_id = tree_id;
1166 return smbXsrv_tcon_local_lookup(session->tcon_table,
1167 local_id, now, tcon);
1170 NTSTATUS smb2srv_tcon_disconnect_all(struct smbXsrv_session *session)
1172 uint64_t vuid = session->global->session_wire_id;
1174 return smbXsrv_tcon_disconnect_all(session->tcon_table, vuid);
1177 struct smbXsrv_tcon_global_traverse_state {
1178 int (*fn)(struct smbXsrv_tcon_global0 *, void *);
1179 void *private_data;
1182 static int smbXsrv_tcon_global_traverse_fn(struct db_record *rec, void *data)
1184 int ret = -1;
1185 struct smbXsrv_tcon_global_traverse_state *state =
1186 (struct smbXsrv_tcon_global_traverse_state*)data;
1187 TDB_DATA key = dbwrap_record_get_key(rec);
1188 TDB_DATA val = dbwrap_record_get_value(rec);
1189 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1190 struct smbXsrv_tcon_globalB global_blob;
1191 enum ndr_err_code ndr_err;
1192 TALLOC_CTX *frame = talloc_stackframe();
1194 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1195 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_tcon_globalB);
1196 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1197 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1198 "key '%s' ndr_pull_struct_blob - %s\n",
1199 hex_encode_talloc(frame, key.dptr, key.dsize),
1200 ndr_errstr(ndr_err)));
1201 goto done;
1204 if (global_blob.version != SMBXSRV_VERSION_0) {
1205 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1206 "key '%s' unsupported version - %d\n",
1207 hex_encode_talloc(frame, key.dptr, key.dsize),
1208 (int)global_blob.version));
1209 goto done;
1212 if (global_blob.info.info0 == NULL) {
1213 DEBUG(1,("Invalid record in smbXsrv_tcon_global.tdb:"
1214 "key '%s' info0 NULL pointer\n",
1215 hex_encode_talloc(frame, key.dptr, key.dsize)));
1216 goto done;
1219 global_blob.info.info0->db_rec = rec;
1220 ret = state->fn(global_blob.info.info0, state->private_data);
1221 done:
1222 TALLOC_FREE(frame);
1223 return ret;
1226 NTSTATUS smbXsrv_tcon_global_traverse(
1227 int (*fn)(struct smbXsrv_tcon_global0 *, void *),
1228 void *private_data)
1230 NTSTATUS status;
1231 int count = 0;
1232 struct smbXsrv_tcon_global_traverse_state state = {
1233 .fn = fn,
1234 .private_data = private_data,
1237 become_root();
1238 status = smbXsrv_tcon_global_init();
1239 if (!NT_STATUS_IS_OK(status)) {
1240 unbecome_root();
1241 DEBUG(0, ("Failed to initialize tcon_global: %s\n",
1242 nt_errstr(status)));
1243 return status;
1246 status = dbwrap_traverse_read(smbXsrv_tcon_global_db_ctx,
1247 smbXsrv_tcon_global_traverse_fn,
1248 &state,
1249 &count);
1250 unbecome_root();
1252 return status;