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/>.
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"
30 #include "lib/util/util_tdb.h"
31 #include "librpc/gen_ndr/ndr_smbXsrv.h"
32 #include <ccan/hash/hash.h>
35 struct smbXsrv_open_table
{
37 struct db_context
*db_ctx
;
44 struct db_context
*db_ctx
;
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
) {
59 global_path
= lock_path("smbXsrv_open_global.tdb");
61 db_ctx
= db_open(NULL
, global_path
,
65 TDB_INCOMPATIBLE_HASH
,
66 O_RDWR
| O_CREAT
, 0600,
71 status
= map_nt_error_from_unix_common(errno
);
76 smbXsrv_open_global_db_ctx
= db_ctx
;
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
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
,
97 RSIVAL(key_buf
, 0, id
);
99 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
);
105 static NTSTATUS
smbXsrv_open_global_key_to_id(TDB_DATA key
, uint32_t *id
)
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);
121 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
123 static TDB_DATA
smbXsrv_open_local_id_to_key(uint32_t id
,
128 RSIVAL(key_buf
, 0, id
);
130 key
= make_tdb_data(key_buf
, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
);
135 static NTSTATUS
smbXsrv_open_local_key_to_id(TDB_DATA key
, uint32_t *id
)
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);
150 static NTSTATUS
smbXsrv_open_table_init(struct smbXsrv_connection
*conn
,
155 struct smbXsrv_open_table
*table
;
159 if (lowest_id
> highest_id
) {
160 return NT_STATUS_INTERNAL_ERROR
;
163 max_range
= highest_id
;
164 max_range
-= lowest_id
;
167 if (max_opens
> max_range
) {
168 return NT_STATUS_INTERNAL_ERROR
;
171 table
= talloc_zero(conn
, struct smbXsrv_open_table
);
173 return NT_STATUS_NO_MEMORY
;
176 table
->local
.db_ctx
= db_open_rbt(table
);
177 if (table
->local
.db_ctx
== NULL
) {
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
)) {
191 table
->global
.db_ctx
= smbXsrv_open_global_db_ctx
;
193 conn
->open_table
= table
;
197 struct smbXsrv_open_local_allocate_state
{
198 const uint32_t lowest_id
;
199 const uint32_t highest_id
;
205 static int smbXsrv_open_local_allocate_traverse(struct db_record
*rec
,
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
);
214 status
= smbXsrv_open_local_key_to_id(key
, &id
);
215 if (!NT_STATUS_IS_OK(status
)) {
216 state
->status
= status
;
220 if (id
<= state
->last_id
) {
221 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
226 if (id
> state
->useable_id
) {
227 state
->status
= NT_STATUS_OK
;
231 if (state
->useable_id
== state
->highest_id
) {
232 state
->status
= NT_STATUS_INSUFFICIENT_RESOURCES
;
236 state
->useable_id
+=1;
240 static NTSTATUS
smbXsrv_open_local_allocate_id(struct db_context
*db
,
244 struct db_record
**_rec
,
247 struct smbXsrv_open_local_allocate_state state
= {
248 .lowest_id
= lowest_id
,
249 .highest_id
= highest_id
,
251 .useable_id
= lowest_id
,
252 .status
= NT_STATUS_INTERNAL_ERROR
,
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
++) {
273 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
276 struct db_record
*rec
= NULL
;
278 id
= generate_random() % range
;
281 if (id
< lowest_id
) {
284 if (id
> highest_id
) {
288 key
= smbXsrv_open_local_id_to_key(id
, key_buf
);
290 rec
= dbwrap_fetch_locked(db
, mem_ctx
, key
);
292 return NT_STATUS_INSUFFICIENT_RESOURCES
;
295 val
= dbwrap_record_get_value(rec
);
296 if (val
.dsize
!= 0) {
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
,
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
)) {
322 if (state
.useable_id
<= state
.highest_id
) {
323 state
.status
= NT_STATUS_OK
;
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
335 return NT_STATUS_INSUFFICIENT_RESOURCES
;
338 if (NT_STATUS_IS_OK(state
.status
)) {
340 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
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
);
351 return NT_STATUS_INSUFFICIENT_RESOURCES
;
354 val
= dbwrap_record_get_value(rec
);
355 if (val
.dsize
!= 0) {
357 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
368 struct smbXsrv_open_local_fetch_state
{
369 struct smbXsrv_open
*op
;
373 static void smbXsrv_open_local_fetch_parser(TDB_DATA key
, TDB_DATA data
,
376 struct smbXsrv_open_local_fetch_state
*state
=
377 (struct smbXsrv_open_local_fetch_state
*)private_data
;
380 if (data
.dsize
!= sizeof(ptr
)) {
381 state
->status
= NT_STATUS_INTERNAL_DB_ERROR
;
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
,
394 struct smbXsrv_open
**_open
)
396 struct smbXsrv_open_local_fetch_state state
= {
398 .status
= NT_STATUS_INTERNAL_ERROR
,
400 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
406 if (open_local_id
== 0) {
407 return NT_STATUS_FILE_CLOSED
;
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
,
424 if (NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
)) {
425 return NT_STATUS_FILE_CLOSED
;
426 } else if (!NT_STATUS_IS_OK(status
)) {
429 if (!NT_STATUS_IS_OK(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
;
449 return state
.op
->status
;
452 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0
*global
)
457 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
461 struct smbXsrv_open_global0
**_g
);
463 static NTSTATUS
smbXsrv_open_global_allocate(struct db_context
*db
,
465 struct smbXsrv_open_global0
**_global
)
468 struct smbXsrv_open_global0
*global
= NULL
;
469 uint32_t last_free
= 0;
470 const uint32_t min_tries
= 3;
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
486 for (i
= 0; i
< UINT32_MAX
; i
++) {
487 bool is_free
= false;
488 bool was_free
= false;
490 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
493 if (i
>= min_tries
&& last_free
!= 0) {
496 id
= generate_random();
501 if (id
== UINT32_MAX
) {
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
) {
510 return NT_STATUS_INSUFFICIENT_RESOURCES
;
513 smbXsrv_open_global_verify_record(global
->db_rec
,
519 TALLOC_FREE(global
->db_rec
);
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) {
537 TALLOC_FREE(global
->db_rec
);
541 global
->open_global_id
= id
;
547 /* should not be reached */
549 return NT_STATUS_INTERNAL_ERROR
;
552 static void smbXsrv_open_global_verify_record(struct db_record
*db_rec
,
556 struct smbXsrv_open_global0
**_g
)
561 struct smbXsrv_open_globalB global_blob
;
562 enum ndr_err_code ndr_err
;
563 struct smbXsrv_open_global0
*global
= NULL
;
565 TALLOC_CTX
*frame
= talloc_stackframe();
576 key
= dbwrap_record_get_key(db_rec
);
578 val
= dbwrap_record_get_value(db_rec
);
579 if (val
.dsize
== 0) {
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
),
602 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
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
);
617 global
= global_blob
.info
.info0
;
619 if (server_id_is_disconnected(&global
->server_id
)) {
622 exists
= serverid_exists(&global
->server_id
);
625 DEBUG(2,("smbXsrv_open_global_verify_record: "
626 "key '%s' server_id %s does not exist.\n",
627 hex_encode_talloc(frame
, key
.dptr
, key
.dsize
),
628 server_id_str(frame
, &global
->server_id
)));
630 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
633 dbwrap_record_delete(db_rec
);
639 *_g
= talloc_move(mem_ctx
, &global
);
644 static NTSTATUS
smbXsrv_open_global_store(struct smbXsrv_open_global0
*global
)
646 struct smbXsrv_open_globalB global_blob
;
647 DATA_BLOB blob
= data_blob_null
;
651 enum ndr_err_code ndr_err
;
654 * TODO: if we use other versions than '0'
655 * we would add glue code here, that would be able to
656 * store the information in the old format.
659 if (global
->db_rec
== NULL
) {
660 return NT_STATUS_INTERNAL_ERROR
;
663 key
= dbwrap_record_get_key(global
->db_rec
);
664 val
= dbwrap_record_get_value(global
->db_rec
);
666 ZERO_STRUCT(global_blob
);
667 global_blob
.version
= smbXsrv_version_global_current();
668 if (val
.dsize
>= 8) {
669 global_blob
.seqnum
= IVAL(val
.dptr
, 4);
671 global_blob
.seqnum
+= 1;
672 global_blob
.info
.info0
= global
;
674 ndr_err
= ndr_push_struct_blob(&blob
, global
->db_rec
, &global_blob
,
675 (ndr_push_flags_fn_t
)ndr_push_smbXsrv_open_globalB
);
676 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
677 status
= ndr_map_error2ntstatus(ndr_err
);
678 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
679 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
681 TALLOC_FREE(global
->db_rec
);
685 val
= make_tdb_data(blob
.data
, blob
.length
);
686 status
= dbwrap_record_store(global
->db_rec
, val
, TDB_REPLACE
);
687 if (!NT_STATUS_IS_OK(status
)) {
688 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
689 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
),
691 TALLOC_FREE(global
->db_rec
);
696 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
697 hex_encode_talloc(global
->db_rec
, key
.dptr
, key
.dsize
)));
698 NDR_PRINT_DEBUG(smbXsrv_open_globalB
, &global_blob
);
701 TALLOC_FREE(global
->db_rec
);
706 static NTSTATUS
smbXsrv_open_global_lookup(struct smbXsrv_open_table
*table
,
707 uint32_t open_global_id
,
709 struct smbXsrv_open_global0
**_global
)
712 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
713 struct db_record
*global_rec
= NULL
;
714 bool is_free
= false;
718 if (table
->global
.db_ctx
== NULL
) {
719 return NT_STATUS_INTERNAL_ERROR
;
722 key
= smbXsrv_open_global_id_to_key(open_global_id
, key_buf
);
724 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
, mem_ctx
, key
);
725 if (global_rec
== NULL
) {
726 DEBUG(0, ("smbXsrv_open_global_lookup(0x%08x): "
727 "Failed to lock global key '%s'\n",
729 hex_encode_talloc(talloc_tos(), key
.dptr
,
731 return NT_STATUS_INTERNAL_DB_ERROR
;
734 smbXsrv_open_global_verify_record(global_rec
,
740 talloc_free(global_rec
);
741 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
744 (*_global
)->db_rec
= talloc_move(*_global
, &global_rec
);
746 talloc_set_destructor(*_global
, smbXsrv_open_global_destructor
);
751 static int smbXsrv_open_destructor(struct smbXsrv_open
*op
)
755 status
= smbXsrv_open_close(op
, 0);
756 if (!NT_STATUS_IS_OK(status
)) {
757 DEBUG(0, ("smbXsrv_open_destructor: "
758 "smbXsrv_open_close() failed - %s\n",
762 TALLOC_FREE(op
->global
);
767 NTSTATUS
smbXsrv_open_create(struct smbXsrv_connection
*conn
,
768 struct auth_session_info
*session_info
,
770 struct smbXsrv_open
**_open
)
772 struct smbXsrv_open_table
*table
= conn
->open_table
;
773 struct db_record
*local_rec
= NULL
;
774 struct smbXsrv_open
*op
= NULL
;
777 struct smbXsrv_open_global0
*global
= NULL
;
779 struct dom_sid
*current_sid
= NULL
;
780 struct security_token
*current_token
= NULL
;
782 if (session_info
== NULL
) {
783 return NT_STATUS_INVALID_HANDLE
;
785 current_token
= session_info
->security_token
;
787 if (current_token
== NULL
) {
788 return NT_STATUS_INVALID_HANDLE
;
791 if (current_token
->num_sids
> PRIMARY_USER_SID_INDEX
) {
792 current_sid
= ¤t_token
->sids
[PRIMARY_USER_SID_INDEX
];
795 if (current_sid
== NULL
) {
796 return NT_STATUS_INVALID_HANDLE
;
799 if (table
->local
.num_opens
>= table
->local
.max_opens
) {
800 return NT_STATUS_INSUFFICIENT_RESOURCES
;
803 op
= talloc_zero(table
, struct smbXsrv_open
);
805 return NT_STATUS_NO_MEMORY
;
808 op
->status
= NT_STATUS_OK
; /* TODO: start with INTERNAL_ERROR */
811 status
= smbXsrv_open_global_allocate(table
->global
.db_ctx
,
813 if (!NT_STATUS_IS_OK(status
)) {
819 status
= smbXsrv_open_local_allocate_id(table
->local
.db_ctx
,
820 table
->local
.lowest_id
,
821 table
->local
.highest_id
,
825 if (!NT_STATUS_IS_OK(status
)) {
830 global
->open_persistent_id
= global
->open_global_id
;
831 global
->open_volatile_id
= op
->local_id
;
833 global
->server_id
= messaging_server_id(conn
->msg_ctx
);
834 global
->open_time
= now
;
835 global
->open_owner
= *current_sid
;
836 if (conn
->protocol
>= PROTOCOL_SMB2_10
) {
837 global
->client_guid
= conn
->smb2
.client
.guid
;
841 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
842 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
843 TALLOC_FREE(local_rec
);
844 if (!NT_STATUS_IS_OK(status
)) {
848 table
->local
.num_opens
+= 1;
850 talloc_set_destructor(op
, smbXsrv_open_destructor
);
852 status
= smbXsrv_open_global_store(global
);
853 if (!NT_STATUS_IS_OK(status
)) {
854 DEBUG(0,("smbXsrv_open_create: "
855 "global_id (0x%08x) store failed - %s\n",
856 op
->global
->open_global_id
,
863 struct smbXsrv_openB open_blob
;
865 ZERO_STRUCT(open_blob
);
866 open_blob
.version
= SMBXSRV_VERSION_0
;
867 open_blob
.info
.info0
= op
;
869 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
870 op
->global
->open_global_id
));
871 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
878 uint32_t smbXsrv_open_hash(struct smbXsrv_open
*_open
)
883 SBVAL(buf
, 0, _open
->global
->open_persistent_id
);
884 SBVAL(buf
, 8, _open
->global
->open_volatile_id
);
885 SBVAL(buf
, 16, _open
->global
->open_time
);
887 ret
= hash(buf
, sizeof(buf
), 0);
896 NTSTATUS
smbXsrv_open_update(struct smbXsrv_open
*op
)
898 struct smbXsrv_open_table
*table
= op
->table
;
900 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
903 if (op
->global
->db_rec
!= NULL
) {
904 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
905 "Called with db_rec != NULL'\n",
906 op
->global
->open_global_id
));
907 return NT_STATUS_INTERNAL_ERROR
;
910 key
= smbXsrv_open_global_id_to_key(op
->global
->open_global_id
,
913 op
->global
->db_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
915 if (op
->global
->db_rec
== NULL
) {
916 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
917 "Failed to lock global key '%s'\n",
918 op
->global
->open_global_id
,
919 hex_encode_talloc(talloc_tos(), key
.dptr
,
921 return NT_STATUS_INTERNAL_DB_ERROR
;
924 status
= smbXsrv_open_global_store(op
->global
);
925 if (!NT_STATUS_IS_OK(status
)) {
926 DEBUG(0,("smbXsrv_open_update: "
927 "global_id (0x%08x) store failed - %s\n",
928 op
->global
->open_global_id
,
934 struct smbXsrv_openB open_blob
;
936 ZERO_STRUCT(open_blob
);
937 open_blob
.version
= SMBXSRV_VERSION_0
;
938 open_blob
.info
.info0
= op
;
940 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
941 op
->global
->open_global_id
));
942 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
948 NTSTATUS
smbXsrv_open_close(struct smbXsrv_open
*op
, NTTIME now
)
950 struct smbXsrv_open_table
*table
;
951 struct db_record
*local_rec
= NULL
;
952 struct db_record
*global_rec
= NULL
;
954 NTSTATUS error
= NT_STATUS_OK
;
956 if (op
->table
== NULL
) {
963 op
->status
= NT_STATUS_FILE_CLOSED
;
964 op
->global
->disconnect_time
= now
;
965 server_id_set_disconnected(&op
->global
->server_id
);
967 global_rec
= op
->global
->db_rec
;
968 op
->global
->db_rec
= NULL
;
969 if (global_rec
== NULL
) {
970 uint8_t key_buf
[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE
];
973 key
= smbXsrv_open_global_id_to_key(
974 op
->global
->open_global_id
,
977 global_rec
= dbwrap_fetch_locked(table
->global
.db_ctx
,
979 if (global_rec
== NULL
) {
980 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
981 "Failed to lock global key '%s'\n",
982 op
->global
->open_global_id
,
983 hex_encode_talloc(global_rec
, key
.dptr
,
985 error
= NT_STATUS_INTERNAL_ERROR
;
989 if (global_rec
!= NULL
&& op
->global
->durable
) {
991 * If it is a durable open we need to update the global part
992 * instead of deleting it
994 op
->global
->db_rec
= global_rec
;
995 status
= smbXsrv_open_global_store(op
->global
);
996 if (NT_STATUS_IS_OK(status
)) {
998 * smbXsrv_open_global_store does the free
999 * of op->global->db_rec
1003 if (!NT_STATUS_IS_OK(status
)) {
1004 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1005 "smbXsrv_open_global_store() failed - %s\n",
1006 op
->global
->open_global_id
,
1007 nt_errstr(status
)));
1011 if (NT_STATUS_IS_OK(status
) && DEBUGLVL(10)) {
1012 struct smbXsrv_openB open_blob
;
1014 ZERO_STRUCT(open_blob
);
1015 open_blob
.version
= SMBXSRV_VERSION_0
;
1016 open_blob
.info
.info0
= op
;
1018 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1019 "stored disconnect\n",
1020 op
->global
->open_global_id
));
1021 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
1025 if (global_rec
!= NULL
) {
1026 status
= dbwrap_record_delete(global_rec
);
1027 if (!NT_STATUS_IS_OK(status
)) {
1028 TDB_DATA key
= dbwrap_record_get_key(global_rec
);
1030 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1031 "failed to delete global key '%s': %s\n",
1032 op
->global
->open_global_id
,
1033 hex_encode_talloc(global_rec
, key
.dptr
,
1035 nt_errstr(status
)));
1039 TALLOC_FREE(global_rec
);
1041 local_rec
= op
->db_rec
;
1042 if (local_rec
== NULL
) {
1043 uint8_t key_buf
[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE
];
1046 key
= smbXsrv_open_local_id_to_key(op
->local_id
, key_buf
);
1048 local_rec
= dbwrap_fetch_locked(table
->local
.db_ctx
,
1050 if (local_rec
== NULL
) {
1051 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1052 "Failed to lock local key '%s'\n",
1053 op
->global
->open_global_id
,
1054 hex_encode_talloc(local_rec
, key
.dptr
,
1056 error
= NT_STATUS_INTERNAL_ERROR
;
1060 if (local_rec
!= NULL
) {
1061 status
= dbwrap_record_delete(local_rec
);
1062 if (!NT_STATUS_IS_OK(status
)) {
1063 TDB_DATA key
= dbwrap_record_get_key(local_rec
);
1065 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1066 "failed to delete local key '%s': %s\n",
1067 op
->global
->open_global_id
,
1068 hex_encode_talloc(local_rec
, key
.dptr
,
1070 nt_errstr(status
)));
1073 table
->local
.num_opens
-= 1;
1075 if (op
->db_rec
== NULL
) {
1076 TALLOC_FREE(local_rec
);
1081 file_free(NULL
, op
->compat
);
1088 NTSTATUS
smb1srv_open_table_init(struct smbXsrv_connection
*conn
)
1093 * Allow a range from 1..65534.
1095 * With real_max_open_files possible ids,
1096 * truncated to the SMB1 limit of 16-bit.
1098 * 0 and 0xFFFF are no valid ids.
1100 max_opens
= conn
->sconn
->real_max_open_files
;
1101 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1103 return smbXsrv_open_table_init(conn
, 1, UINT16_MAX
- 1, max_opens
);
1106 NTSTATUS
smb1srv_open_lookup(struct smbXsrv_connection
*conn
,
1107 uint16_t fnum
, NTTIME now
,
1108 struct smbXsrv_open
**_open
)
1110 struct smbXsrv_open_table
*table
= conn
->open_table
;
1111 uint32_t local_id
= fnum
;
1112 uint32_t global_id
= 0;
1114 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);
1117 NTSTATUS
smb2srv_open_table_init(struct smbXsrv_connection
*conn
)
1122 * Allow a range from 1..4294967294.
1124 * With real_max_open_files possible ids,
1125 * truncated to 16-bit (the same as SMB1 for now).
1127 * 0 and 0xFFFFFFFF are no valid ids.
1129 * The usage of conn->sconn->real_max_open_files
1130 * is the reason that we use one open table per
1131 * transport connection (as we still have a 1:1 mapping
1132 * between process and transport connection).
1134 max_opens
= conn
->sconn
->real_max_open_files
;
1135 max_opens
= MIN(max_opens
, UINT16_MAX
- 1);
1137 return smbXsrv_open_table_init(conn
, 1, UINT32_MAX
- 1, max_opens
);
1140 NTSTATUS
smb2srv_open_lookup(struct smbXsrv_connection
*conn
,
1141 uint64_t persistent_id
,
1142 uint64_t volatile_id
,
1144 struct smbXsrv_open
**_open
)
1146 struct smbXsrv_open_table
*table
= conn
->open_table
;
1147 uint32_t local_id
= volatile_id
& UINT32_MAX
;
1148 uint64_t local_zeros
= volatile_id
& 0xFFFFFFFF00000000LLU
;
1149 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1150 uint64_t global_zeros
= persistent_id
& 0xFFFFFFFF00000000LLU
;
1152 if (local_zeros
!= 0) {
1153 return NT_STATUS_FILE_CLOSED
;
1156 if (global_zeros
!= 0) {
1157 return NT_STATUS_FILE_CLOSED
;
1160 if (global_id
== 0) {
1161 return NT_STATUS_FILE_CLOSED
;
1164 return smbXsrv_open_local_lookup(table
, local_id
, global_id
, now
, _open
);
1167 NTSTATUS
smb2srv_open_recreate(struct smbXsrv_connection
*conn
,
1168 struct auth_session_info
*session_info
,
1169 uint64_t persistent_id
,
1170 struct GUID create_guid
,
1172 struct smbXsrv_open
**_open
)
1174 struct smbXsrv_open_table
*table
= conn
->open_table
;
1175 struct db_record
*local_rec
= NULL
;
1176 struct smbXsrv_open
*op
= NULL
;
1179 uint32_t global_id
= persistent_id
& UINT32_MAX
;
1180 uint64_t global_zeros
= persistent_id
& 0xFFFFFFFF00000000LLU
;
1182 struct security_token
*current_token
= NULL
;
1184 if (session_info
== NULL
) {
1185 return NT_STATUS_INVALID_HANDLE
;
1187 current_token
= session_info
->security_token
;
1189 if (current_token
== NULL
) {
1190 return NT_STATUS_INVALID_HANDLE
;
1193 if (global_zeros
!= 0) {
1194 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1197 op
= talloc_zero(table
, struct smbXsrv_open
);
1199 return NT_STATUS_NO_MEMORY
;
1203 status
= smbXsrv_open_global_lookup(table
, global_id
, op
, &op
->global
);
1204 if (!NT_STATUS_IS_OK(status
)) {
1209 if (!GUID_equal(&op
->global
->create_guid
, &create_guid
)) {
1211 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1214 if (!security_token_is_sid(current_token
, &op
->global
->open_owner
)) {
1216 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1219 if (!op
->global
->durable
) {
1221 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1224 if (table
->local
.num_opens
>= table
->local
.max_opens
) {
1226 return NT_STATUS_INSUFFICIENT_RESOURCES
;
1229 status
= smbXsrv_open_local_allocate_id(table
->local
.db_ctx
,
1230 table
->local
.lowest_id
,
1231 table
->local
.highest_id
,
1235 if (!NT_STATUS_IS_OK(status
)) {
1240 op
->idle_time
= now
;
1241 op
->status
= NT_STATUS_FILE_CLOSED
;
1243 op
->global
->open_volatile_id
= op
->local_id
;
1244 op
->global
->server_id
= messaging_server_id(conn
->msg_ctx
);
1247 val
= make_tdb_data((uint8_t const *)&ptr
, sizeof(ptr
));
1248 status
= dbwrap_record_store(local_rec
, val
, TDB_REPLACE
);
1249 TALLOC_FREE(local_rec
);
1250 if (!NT_STATUS_IS_OK(status
)) {
1254 table
->local
.num_opens
+= 1;
1256 talloc_set_destructor(op
, smbXsrv_open_destructor
);
1258 status
= smbXsrv_open_global_store(op
->global
);
1259 if (!NT_STATUS_IS_OK(status
)) {
1265 struct smbXsrv_openB open_blob
;
1267 ZERO_STRUCT(open_blob
);
1268 open_blob
.version
= 0;
1269 open_blob
.info
.info0
= op
;
1271 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1272 op
->global
->open_global_id
));
1273 NDR_PRINT_DEBUG(smbXsrv_openB
, &open_blob
);
1277 return NT_STATUS_OK
;