Fix bug in SMB_FIND_INFO_STANDARD parsing found by Volker.
[Samba/gebeck_regimport.git] / source3 / smbd / smbXsrv_open.c
blobabb214497168847b36199204202c78388e846a9e
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 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 "../libcli/security/security.h"
29 #include "messages.h"
30 #include "lib/util/util_tdb.h"
31 #include "librpc/gen_ndr/ndr_smbXsrv.h"
32 #include <ccan/hash/hash.h>
33 #include "serverid.h"
35 struct smbXsrv_open_table {
36 struct {
37 struct db_context *db_ctx;
38 uint32_t lowest_id;
39 uint32_t highest_id;
40 uint32_t max_opens;
41 uint32_t num_opens;
42 } local;
43 struct {
44 struct db_context *db_ctx;
45 } global;
48 static struct db_context *smbXsrv_open_global_db_ctx = NULL;
50 NTSTATUS smbXsrv_open_global_init(void)
52 const char *global_path = NULL;
53 struct db_context *db_ctx = NULL;
55 if (smbXsrv_open_global_db_ctx != NULL) {
56 return NT_STATUS_OK;
59 global_path = lock_path("smbXsrv_open_global.tdb");
61 db_ctx = db_open(NULL, global_path,
62 0, /* hash_size */
63 TDB_DEFAULT |
64 TDB_CLEAR_IF_FIRST |
65 TDB_INCOMPATIBLE_HASH,
66 O_RDWR | O_CREAT, 0600,
67 DBWRAP_LOCK_ORDER_1);
68 if (db_ctx == NULL) {
69 NTSTATUS status;
71 status = map_nt_error_from_unix_common(errno);
73 return status;
76 smbXsrv_open_global_db_ctx = db_ctx;
78 return NT_STATUS_OK;
82 * NOTE:
83 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
84 * has the same result as integer comparison between the uint32_t
85 * values.
87 * TODO: implement string based key
90 #define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
92 static TDB_DATA smbXsrv_open_global_id_to_key(uint32_t id,
93 uint8_t *key_buf)
95 TDB_DATA key;
97 RSIVAL(key_buf, 0, id);
99 key = make_tdb_data(key_buf, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE);
101 return key;
104 #if 0
105 static NTSTATUS smbXsrv_open_global_key_to_id(TDB_DATA key, uint32_t *id)
107 if (id == NULL) {
108 return NT_STATUS_INVALID_PARAMETER;
111 if (key.dsize != SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE) {
112 return NT_STATUS_INTERNAL_DB_CORRUPTION;
115 *id = RIVAL(key.dptr, 0);
117 return NT_STATUS_OK;
119 #endif
121 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
123 static TDB_DATA smbXsrv_open_local_id_to_key(uint32_t id,
124 uint8_t *key_buf)
126 TDB_DATA key;
128 RSIVAL(key_buf, 0, id);
130 key = make_tdb_data(key_buf, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE);
132 return key;
135 static NTSTATUS smbXsrv_open_local_key_to_id(TDB_DATA key, uint32_t *id)
137 if (id == NULL) {
138 return NT_STATUS_INVALID_PARAMETER;
141 if (key.dsize != SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE) {
142 return NT_STATUS_INTERNAL_DB_CORRUPTION;
145 *id = RIVAL(key.dptr, 0);
147 return NT_STATUS_OK;
150 static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
151 uint32_t lowest_id,
152 uint32_t highest_id,
153 uint32_t max_opens)
155 struct smbXsrv_open_table *table;
156 NTSTATUS status;
157 uint64_t max_range;
159 if (lowest_id > highest_id) {
160 return NT_STATUS_INTERNAL_ERROR;
163 max_range = highest_id;
164 max_range -= lowest_id;
165 max_range += 1;
167 if (max_opens > max_range) {
168 return NT_STATUS_INTERNAL_ERROR;
171 table = talloc_zero(conn, struct smbXsrv_open_table);
172 if (table == NULL) {
173 return NT_STATUS_NO_MEMORY;
176 table->local.db_ctx = db_open_rbt(table);
177 if (table->local.db_ctx == NULL) {
178 TALLOC_FREE(table);
179 return NT_STATUS_NO_MEMORY;
181 table->local.lowest_id = lowest_id;
182 table->local.highest_id = highest_id;
183 table->local.max_opens = max_opens;
185 status = smbXsrv_open_global_init();
186 if (!NT_STATUS_IS_OK(status)) {
187 TALLOC_FREE(table);
188 return status;
191 table->global.db_ctx = smbXsrv_open_global_db_ctx;
193 conn->open_table = table;
194 return NT_STATUS_OK;
197 struct smbXsrv_open_local_allocate_state {
198 const uint32_t lowest_id;
199 const uint32_t highest_id;
200 uint32_t last_id;
201 uint32_t useable_id;
202 NTSTATUS status;
205 static int smbXsrv_open_local_allocate_traverse(struct db_record *rec,
206 void *private_data)
208 struct smbXsrv_open_local_allocate_state *state =
209 (struct smbXsrv_open_local_allocate_state *)private_data;
210 TDB_DATA key = dbwrap_record_get_key(rec);
211 uint32_t id = 0;
212 NTSTATUS status;
214 status = smbXsrv_open_local_key_to_id(key, &id);
215 if (!NT_STATUS_IS_OK(status)) {
216 state->status = status;
217 return -1;
220 if (id <= state->last_id) {
221 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
222 return -1;
224 state->last_id = id;
226 if (id > state->useable_id) {
227 state->status = NT_STATUS_OK;
228 return -1;
231 if (state->useable_id == state->highest_id) {
232 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
233 return -1;
236 state->useable_id +=1;
237 return 0;
240 static NTSTATUS smbXsrv_open_local_allocate_id(struct db_context *db,
241 uint32_t lowest_id,
242 uint32_t highest_id,
243 TALLOC_CTX *mem_ctx,
244 struct db_record **_rec,
245 uint32_t *_id)
247 struct smbXsrv_open_local_allocate_state state = {
248 .lowest_id = lowest_id,
249 .highest_id = highest_id,
250 .last_id = 0,
251 .useable_id = lowest_id,
252 .status = NT_STATUS_INTERNAL_ERROR,
254 uint32_t i;
255 uint32_t range;
256 NTSTATUS status;
257 int count = 0;
259 *_rec = NULL;
260 *_id = 0;
262 if (lowest_id > highest_id) {
263 return NT_STATUS_INSUFFICIENT_RESOURCES;
267 * first we try randomly
269 range = (highest_id - lowest_id) + 1;
271 for (i = 0; i < (range / 2); i++) {
272 uint32_t id;
273 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
274 TDB_DATA key;
275 TDB_DATA val;
276 struct db_record *rec = NULL;
278 id = generate_random() % range;
279 id += lowest_id;
281 if (id < lowest_id) {
282 id = lowest_id;
284 if (id > highest_id) {
285 id = highest_id;
288 key = smbXsrv_open_local_id_to_key(id, key_buf);
290 rec = dbwrap_fetch_locked(db, mem_ctx, key);
291 if (rec == NULL) {
292 return NT_STATUS_INSUFFICIENT_RESOURCES;
295 val = dbwrap_record_get_value(rec);
296 if (val.dsize != 0) {
297 TALLOC_FREE(rec);
298 continue;
301 *_rec = rec;
302 *_id = id;
303 return NT_STATUS_OK;
307 * if the range is almost full,
308 * we traverse the whole table
309 * (this relies on sorted behavior of dbwrap_rbt)
311 status = dbwrap_traverse_read(db, smbXsrv_open_local_allocate_traverse,
312 &state, &count);
313 if (NT_STATUS_IS_OK(status)) {
314 if (NT_STATUS_IS_OK(state.status)) {
315 return NT_STATUS_INTERNAL_ERROR;
318 if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
319 return state.status;
322 if (state.useable_id <= state.highest_id) {
323 state.status = NT_STATUS_OK;
324 } else {
325 return NT_STATUS_INSUFFICIENT_RESOURCES;
327 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
329 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
331 * If we get anything else it is an error, because it
332 * means we did not manage to find a free slot in
333 * the db.
335 return NT_STATUS_INSUFFICIENT_RESOURCES;
338 if (NT_STATUS_IS_OK(state.status)) {
339 uint32_t id;
340 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
341 TDB_DATA key;
342 TDB_DATA val;
343 struct db_record *rec = NULL;
345 id = state.useable_id;
347 key = smbXsrv_open_local_id_to_key(id, key_buf);
349 rec = dbwrap_fetch_locked(db, mem_ctx, key);
350 if (rec == NULL) {
351 return NT_STATUS_INSUFFICIENT_RESOURCES;
354 val = dbwrap_record_get_value(rec);
355 if (val.dsize != 0) {
356 TALLOC_FREE(rec);
357 return NT_STATUS_INTERNAL_DB_CORRUPTION;
360 *_rec = rec;
361 *_id = id;
362 return NT_STATUS_OK;
365 return state.status;
368 struct smbXsrv_open_local_fetch_state {
369 struct smbXsrv_open *op;
370 NTSTATUS status;
373 static void smbXsrv_open_local_fetch_parser(TDB_DATA key, TDB_DATA data,
374 void *private_data)
376 struct smbXsrv_open_local_fetch_state *state =
377 (struct smbXsrv_open_local_fetch_state *)private_data;
378 void *ptr;
380 if (data.dsize != sizeof(ptr)) {
381 state->status = NT_STATUS_INTERNAL_DB_ERROR;
382 return;
385 memcpy(&ptr, data.dptr, data.dsize);
386 state->op = talloc_get_type_abort(ptr, struct smbXsrv_open);
387 state->status = NT_STATUS_OK;
390 static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
391 uint32_t open_local_id,
392 uint32_t open_global_id,
393 NTTIME now,
394 struct smbXsrv_open **_open)
396 struct smbXsrv_open_local_fetch_state state = {
397 .op = NULL,
398 .status = NT_STATUS_INTERNAL_ERROR,
400 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
401 TDB_DATA key;
402 NTSTATUS status;
404 *_open = NULL;
406 if (open_local_id == 0) {
407 return NT_STATUS_FILE_CLOSED;
410 if (table == NULL) {
411 /* this might happen before the end of negprot */
412 return NT_STATUS_FILE_CLOSED;
415 if (table->local.db_ctx == NULL) {
416 return NT_STATUS_INTERNAL_ERROR;
419 key = smbXsrv_open_local_id_to_key(open_local_id, key_buf);
421 status = dbwrap_parse_record(table->local.db_ctx, key,
422 smbXsrv_open_local_fetch_parser,
423 &state);
424 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
425 return NT_STATUS_FILE_CLOSED;
426 } else if (!NT_STATUS_IS_OK(status)) {
427 return status;
429 if (!NT_STATUS_IS_OK(state.status)) {
430 return state.status;
433 if (NT_STATUS_EQUAL(state.op->status, NT_STATUS_FILE_CLOSED)) {
434 return NT_STATUS_FILE_CLOSED;
437 if (open_global_id == 0) {
438 /* make the global check a no-op for SMB1 */
439 open_global_id = state.op->global->open_global_id;
442 if (state.op->global->open_global_id != open_global_id) {
443 return NT_STATUS_FILE_CLOSED;
446 state.op->idle_time = now;
448 *_open = state.op;
449 return state.op->status;
452 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0 *global)
454 return 0;
457 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
458 bool *is_free,
459 bool *was_free,
460 TALLOC_CTX *mem_ctx,
461 struct smbXsrv_open_global0 **_g);
463 static NTSTATUS smbXsrv_open_global_allocate(struct db_context *db,
464 TALLOC_CTX *mem_ctx,
465 struct smbXsrv_open_global0 **_global)
467 uint32_t i;
468 struct smbXsrv_open_global0 *global = NULL;
469 uint32_t last_free = 0;
470 const uint32_t min_tries = 3;
472 *_global = NULL;
474 global = talloc_zero(mem_ctx, struct smbXsrv_open_global0);
475 if (global == NULL) {
476 return NT_STATUS_NO_MEMORY;
478 talloc_set_destructor(global, smbXsrv_open_global_destructor);
481 * Here we just randomly try the whole 32-bit space
483 * We use just 32-bit, because we want to reuse the
484 * ID for SRVSVC.
486 for (i = 0; i < UINT32_MAX; i++) {
487 bool is_free = false;
488 bool was_free = false;
489 uint32_t id;
490 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
491 TDB_DATA key;
493 if (i >= min_tries && last_free != 0) {
494 id = last_free;
495 } else {
496 id = generate_random();
498 if (id == 0) {
499 id++;
501 if (id == UINT32_MAX) {
502 id--;
505 key = smbXsrv_open_global_id_to_key(id, key_buf);
507 global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
508 if (global->db_rec == NULL) {
509 talloc_free(global);
510 return NT_STATUS_INSUFFICIENT_RESOURCES;
513 smbXsrv_open_global_verify_record(global->db_rec,
514 &is_free,
515 &was_free,
516 NULL, NULL);
518 if (!is_free) {
519 TALLOC_FREE(global->db_rec);
520 continue;
523 if (!was_free && i < min_tries) {
525 * The session_id is free now,
526 * but was not free before.
528 * This happens if a smbd crashed
529 * and did not cleanup the record.
531 * If this is one of our first tries,
532 * then we try to find a real free one.
534 if (last_free == 0) {
535 last_free = id;
537 TALLOC_FREE(global->db_rec);
538 continue;
541 global->open_global_id = id;
543 *_global = global;
544 return NT_STATUS_OK;
547 /* should not be reached */
548 talloc_free(global);
549 return NT_STATUS_INTERNAL_ERROR;
552 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
553 bool *is_free,
554 bool *was_free,
555 TALLOC_CTX *mem_ctx,
556 struct smbXsrv_open_global0 **_g)
558 TDB_DATA key;
559 TDB_DATA val;
560 DATA_BLOB blob;
561 struct smbXsrv_open_globalB global_blob;
562 enum ndr_err_code ndr_err;
563 struct smbXsrv_open_global0 *global = NULL;
564 bool exists;
565 TALLOC_CTX *frame = talloc_stackframe();
567 *is_free = false;
569 if (was_free) {
570 *was_free = false;
572 if (_g) {
573 *_g = NULL;
576 key = dbwrap_record_get_key(db_rec);
578 val = dbwrap_record_get_value(db_rec);
579 if (val.dsize == 0) {
580 TALLOC_FREE(frame);
581 *is_free = true;
582 if (was_free) {
583 *was_free = true;
585 return;
588 blob = data_blob_const(val.dptr, val.dsize);
590 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
591 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
592 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
593 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
594 DEBUG(1,("smbXsrv_open_global_verify_record: "
595 "key '%s' ndr_pull_struct_blob - %s\n",
596 hex_encode_talloc(frame, key.dptr, key.dsize),
597 nt_errstr(status)));
598 TALLOC_FREE(frame);
599 return;
602 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
603 if (DEBUGLVL(10)) {
604 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
607 if (global_blob.version != SMBXSRV_VERSION_0) {
608 DEBUG(0,("smbXsrv_open_global_verify_record: "
609 "key '%s' use unsupported version %u\n",
610 hex_encode_talloc(frame, key.dptr, key.dsize),
611 global_blob.version));
612 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
613 TALLOC_FREE(frame);
614 return;
617 global = global_blob.info.info0;
619 exists = serverid_exists(&global->server_id);
620 if (!exists) {
621 DEBUG(2,("smbXsrv_open_global_verify_record: "
622 "key '%s' server_id %s does not exist.\n",
623 hex_encode_talloc(frame, key.dptr, key.dsize),
624 server_id_str(frame, &global->server_id)));
625 if (DEBUGLVL(2)) {
626 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
628 TALLOC_FREE(frame);
629 dbwrap_record_delete(db_rec);
630 *is_free = true;
631 return;
634 if (_g) {
635 *_g = talloc_move(mem_ctx, &global);
637 TALLOC_FREE(frame);
640 static NTSTATUS smbXsrv_open_global_store(struct smbXsrv_open_global0 *global)
642 struct smbXsrv_open_globalB global_blob;
643 DATA_BLOB blob = data_blob_null;
644 TDB_DATA key;
645 TDB_DATA val;
646 NTSTATUS status;
647 enum ndr_err_code ndr_err;
650 * TODO: if we use other versions than '0'
651 * we would add glue code here, that would be able to
652 * store the information in the old format.
655 if (global->db_rec == NULL) {
656 return NT_STATUS_INTERNAL_ERROR;
659 key = dbwrap_record_get_key(global->db_rec);
660 val = dbwrap_record_get_value(global->db_rec);
662 ZERO_STRUCT(global_blob);
663 global_blob.version = smbXsrv_version_global_current();
664 if (val.dsize >= 8) {
665 global_blob.seqnum = IVAL(val.dptr, 4);
667 global_blob.seqnum += 1;
668 global_blob.info.info0 = global;
670 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
671 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
672 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
673 status = ndr_map_error2ntstatus(ndr_err);
674 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
675 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
676 nt_errstr(status)));
677 TALLOC_FREE(global->db_rec);
678 return status;
681 val = make_tdb_data(blob.data, blob.length);
682 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
683 if (!NT_STATUS_IS_OK(status)) {
684 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
685 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
686 nt_errstr(status)));
687 TALLOC_FREE(global->db_rec);
688 return status;
691 if (DEBUGLVL(10)) {
692 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
693 hex_encode_talloc(global->db_rec, key.dptr, key.dsize)));
694 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
697 TALLOC_FREE(global->db_rec);
699 return NT_STATUS_OK;
702 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
704 NTSTATUS status;
706 status = smbXsrv_open_close(op, 0);
707 if (!NT_STATUS_IS_OK(status)) {
708 DEBUG(0, ("smbXsrv_open_destructor: "
709 "smbXsrv_open_close() failed - %s\n",
710 nt_errstr(status)));
713 TALLOC_FREE(op->global);
715 return 0;
718 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
719 struct auth_session_info *session_info,
720 NTTIME now,
721 struct smbXsrv_open **_open)
723 struct smbXsrv_open_table *table = conn->open_table;
724 struct db_record *local_rec = NULL;
725 struct smbXsrv_open *op = NULL;
726 void *ptr = NULL;
727 TDB_DATA val;
728 struct smbXsrv_open_global0 *global = NULL;
729 NTSTATUS status;
730 struct dom_sid *current_sid = NULL;
731 struct security_token *current_token = NULL;
733 if (session_info == NULL) {
734 return NT_STATUS_INVALID_HANDLE;
736 current_token = session_info->security_token;
738 if (current_token == NULL) {
739 return NT_STATUS_INVALID_HANDLE;
742 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
743 current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
746 if (current_sid == NULL) {
747 return NT_STATUS_INVALID_HANDLE;
750 if (table->local.num_opens >= table->local.max_opens) {
751 return NT_STATUS_INSUFFICIENT_RESOURCES;
754 op = talloc_zero(table, struct smbXsrv_open);
755 if (op == NULL) {
756 return NT_STATUS_NO_MEMORY;
758 op->table = table;
759 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
760 op->idle_time = now;
762 status = smbXsrv_open_global_allocate(table->global.db_ctx,
763 op, &global);
764 if (!NT_STATUS_IS_OK(status)) {
765 TALLOC_FREE(op);
766 return status;
768 op->global = global;
770 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
771 table->local.lowest_id,
772 table->local.highest_id,
774 &local_rec,
775 &op->local_id);
776 if (!NT_STATUS_IS_OK(status)) {
777 TALLOC_FREE(op);
778 return status;
781 global->open_persistent_id = global->open_global_id;
782 global->open_volatile_id = op->local_id;
784 global->server_id = messaging_server_id(conn->msg_ctx);
785 global->open_time = now;
786 global->open_owner = *current_sid;
788 ptr = op;
789 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
790 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
791 TALLOC_FREE(local_rec);
792 if (!NT_STATUS_IS_OK(status)) {
793 TALLOC_FREE(op);
794 return status;
796 table->local.num_opens += 1;
798 talloc_set_destructor(op, smbXsrv_open_destructor);
800 status = smbXsrv_open_global_store(global);
801 if (!NT_STATUS_IS_OK(status)) {
802 DEBUG(0,("smbXsrv_open_create: "
803 "global_id (0x%08x) store failed - %s\n",
804 op->global->open_global_id,
805 nt_errstr(status)));
806 TALLOC_FREE(op);
807 return status;
810 if (DEBUGLVL(10)) {
811 struct smbXsrv_openB open_blob;
813 ZERO_STRUCT(open_blob);
814 open_blob.version = SMBXSRV_VERSION_0;
815 open_blob.info.info0 = op;
817 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
818 op->global->open_global_id));
819 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
822 *_open = op;
823 return NT_STATUS_OK;
826 uint32_t smbXsrv_open_hash(struct smbXsrv_open *_open)
828 uint8_t buf[8+8+8];
829 uint32_t ret;
831 SBVAL(buf, 0, _open->global->open_persistent_id);
832 SBVAL(buf, 8, _open->global->open_volatile_id);
833 SBVAL(buf, 16, _open->global->open_time);
835 ret = hash(buf, sizeof(buf), 0);
837 if (ret == 0) {
838 ret = 1;
841 return ret;
844 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
846 struct smbXsrv_open_table *table = op->table;
847 NTSTATUS status;
848 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
849 TDB_DATA key;
851 if (op->global->db_rec != NULL) {
852 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
853 "Called with db_rec != NULL'\n",
854 op->global->open_global_id));
855 return NT_STATUS_INTERNAL_ERROR;
858 key = smbXsrv_open_global_id_to_key(op->global->open_global_id,
859 key_buf);
861 op->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
862 op->global, key);
863 if (op->global->db_rec == NULL) {
864 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
865 "Failed to lock global key '%s'\n",
866 op->global->open_global_id,
867 hex_encode_talloc(talloc_tos(), key.dptr,
868 key.dsize)));
869 return NT_STATUS_INTERNAL_DB_ERROR;
872 status = smbXsrv_open_global_store(op->global);
873 if (!NT_STATUS_IS_OK(status)) {
874 DEBUG(0,("smbXsrv_open_update: "
875 "global_id (0x%08x) store failed - %s\n",
876 op->global->open_global_id,
877 nt_errstr(status)));
878 return status;
881 if (DEBUGLVL(10)) {
882 struct smbXsrv_openB open_blob;
884 ZERO_STRUCT(open_blob);
885 open_blob.version = SMBXSRV_VERSION_0;
886 open_blob.info.info0 = op;
888 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
889 op->global->open_global_id));
890 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
893 return NT_STATUS_OK;
896 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
898 struct smbXsrv_open_table *table;
899 struct db_record *local_rec = NULL;
900 struct db_record *global_rec = NULL;
901 NTSTATUS status;
902 NTSTATUS error = NT_STATUS_OK;
904 if (op->table == NULL) {
905 return NT_STATUS_OK;
908 table = op->table;
909 op->table = NULL;
911 op->status = NT_STATUS_FILE_CLOSED;
913 global_rec = op->global->db_rec;
914 op->global->db_rec = NULL;
915 if (global_rec == NULL) {
916 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
917 TDB_DATA key;
919 key = smbXsrv_open_global_id_to_key(
920 op->global->open_global_id,
921 key_buf);
923 global_rec = dbwrap_fetch_locked(table->global.db_ctx,
924 op->global, key);
925 if (global_rec == NULL) {
926 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
927 "Failed to lock global key '%s'\n",
928 op->global->open_global_id,
929 hex_encode_talloc(global_rec, key.dptr,
930 key.dsize)));
931 error = NT_STATUS_INTERNAL_ERROR;
935 if (global_rec != NULL) {
936 status = dbwrap_record_delete(global_rec);
937 if (!NT_STATUS_IS_OK(status)) {
938 TDB_DATA key = dbwrap_record_get_key(global_rec);
940 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
941 "failed to delete global key '%s': %s\n",
942 op->global->open_global_id,
943 hex_encode_talloc(global_rec, key.dptr,
944 key.dsize),
945 nt_errstr(status)));
946 error = status;
949 TALLOC_FREE(global_rec);
951 local_rec = op->db_rec;
952 if (local_rec == NULL) {
953 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
954 TDB_DATA key;
956 key = smbXsrv_open_local_id_to_key(op->local_id, key_buf);
958 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
959 op, key);
960 if (local_rec == NULL) {
961 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
962 "Failed to lock local key '%s'\n",
963 op->global->open_global_id,
964 hex_encode_talloc(local_rec, key.dptr,
965 key.dsize)));
966 error = NT_STATUS_INTERNAL_ERROR;
970 if (local_rec != NULL) {
971 status = dbwrap_record_delete(local_rec);
972 if (!NT_STATUS_IS_OK(status)) {
973 TDB_DATA key = dbwrap_record_get_key(local_rec);
975 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
976 "failed to delete local key '%s': %s\n",
977 op->global->open_global_id,
978 hex_encode_talloc(local_rec, key.dptr,
979 key.dsize),
980 nt_errstr(status)));
981 error = status;
983 table->local.num_opens -= 1;
985 if (op->db_rec == NULL) {
986 TALLOC_FREE(local_rec);
988 op->db_rec = NULL;
990 if (op->compat) {
991 file_free(NULL, op->compat);
992 op->compat = NULL;
995 return error;
998 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1000 uint32_t max_opens;
1003 * Allow a range from 1..65534.
1005 * With real_max_open_files possible ids,
1006 * truncated to the SMB1 limit of 16-bit.
1008 * 0 and 0xFFFF are no valid ids.
1010 max_opens = conn->sconn->real_max_open_files;
1011 max_opens = MIN(max_opens, UINT16_MAX - 1);
1013 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
1016 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
1017 uint16_t fnum, NTTIME now,
1018 struct smbXsrv_open **_open)
1020 struct smbXsrv_open_table *table = conn->open_table;
1021 uint32_t local_id = fnum;
1022 uint32_t global_id = 0;
1024 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1027 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
1029 uint32_t max_opens;
1032 * Allow a range from 1..4294967294.
1034 * With real_max_open_files possible ids,
1035 * truncated to 16-bit (the same as SMB1 for now).
1037 * 0 and 0xFFFFFFFF are no valid ids.
1039 * The usage of conn->sconn->real_max_open_files
1040 * is the reason that we use one open table per
1041 * transport connection (as we still have a 1:1 mapping
1042 * between process and transport connection).
1044 max_opens = conn->sconn->real_max_open_files;
1045 max_opens = MIN(max_opens, UINT16_MAX - 1);
1047 return smbXsrv_open_table_init(conn, 1, UINT32_MAX - 1, max_opens);
1050 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
1051 uint64_t persistent_id,
1052 uint64_t volatile_id,
1053 NTTIME now,
1054 struct smbXsrv_open **_open)
1056 struct smbXsrv_open_table *table = conn->open_table;
1057 uint32_t local_id = volatile_id & UINT32_MAX;
1058 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
1059 uint32_t global_id = persistent_id & UINT32_MAX;
1060 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1062 if (local_zeros != 0) {
1063 return NT_STATUS_FILE_CLOSED;
1066 if (global_zeros != 0) {
1067 return NT_STATUS_FILE_CLOSED;
1070 if (global_id == 0) {
1071 return NT_STATUS_FILE_CLOSED;
1074 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);