buildtools: Rename perl vendorarch configure option.
[Samba.git] / source3 / smbd / smbXsrv_open.c
blobb15be287691b49bab76df436278554e826f8eb90
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 DBWRAP_FLAG_NONE);
69 if (db_ctx == NULL) {
70 NTSTATUS status;
72 status = map_nt_error_from_unix_common(errno);
74 return status;
77 smbXsrv_open_global_db_ctx = db_ctx;
79 return NT_STATUS_OK;
83 * NOTE:
84 * We need to store the keys in big endian so that dbwrap_rbt's memcmp
85 * has the same result as integer comparison between the uint32_t
86 * values.
88 * TODO: implement string based key
91 #define SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE sizeof(uint32_t)
93 static TDB_DATA smbXsrv_open_global_id_to_key(uint32_t id,
94 uint8_t *key_buf)
96 TDB_DATA key;
98 RSIVAL(key_buf, 0, id);
100 key = make_tdb_data(key_buf, SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE);
102 return key;
105 #if 0
106 static NTSTATUS smbXsrv_open_global_key_to_id(TDB_DATA key, uint32_t *id)
108 if (id == NULL) {
109 return NT_STATUS_INVALID_PARAMETER;
112 if (key.dsize != SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE) {
113 return NT_STATUS_INTERNAL_DB_CORRUPTION;
116 *id = RIVAL(key.dptr, 0);
118 return NT_STATUS_OK;
120 #endif
122 #define SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE sizeof(uint32_t)
124 static TDB_DATA smbXsrv_open_local_id_to_key(uint32_t id,
125 uint8_t *key_buf)
127 TDB_DATA key;
129 RSIVAL(key_buf, 0, id);
131 key = make_tdb_data(key_buf, SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE);
133 return key;
136 static NTSTATUS smbXsrv_open_local_key_to_id(TDB_DATA key, uint32_t *id)
138 if (id == NULL) {
139 return NT_STATUS_INVALID_PARAMETER;
142 if (key.dsize != SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE) {
143 return NT_STATUS_INTERNAL_DB_CORRUPTION;
146 *id = RIVAL(key.dptr, 0);
148 return NT_STATUS_OK;
151 static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
152 uint32_t lowest_id,
153 uint32_t highest_id,
154 uint32_t max_opens)
156 struct smbXsrv_open_table *table;
157 NTSTATUS status;
158 uint64_t max_range;
160 if (lowest_id > highest_id) {
161 return NT_STATUS_INTERNAL_ERROR;
164 max_range = highest_id;
165 max_range -= lowest_id;
166 max_range += 1;
168 if (max_opens > max_range) {
169 return NT_STATUS_INTERNAL_ERROR;
172 table = talloc_zero(conn, struct smbXsrv_open_table);
173 if (table == NULL) {
174 return NT_STATUS_NO_MEMORY;
177 table->local.db_ctx = db_open_rbt(table);
178 if (table->local.db_ctx == NULL) {
179 TALLOC_FREE(table);
180 return NT_STATUS_NO_MEMORY;
182 table->local.lowest_id = lowest_id;
183 table->local.highest_id = highest_id;
184 table->local.max_opens = max_opens;
186 status = smbXsrv_open_global_init();
187 if (!NT_STATUS_IS_OK(status)) {
188 TALLOC_FREE(table);
189 return status;
192 table->global.db_ctx = smbXsrv_open_global_db_ctx;
194 conn->open_table = table;
195 return NT_STATUS_OK;
198 struct smbXsrv_open_local_allocate_state {
199 const uint32_t lowest_id;
200 const uint32_t highest_id;
201 uint32_t last_id;
202 uint32_t useable_id;
203 NTSTATUS status;
206 static int smbXsrv_open_local_allocate_traverse(struct db_record *rec,
207 void *private_data)
209 struct smbXsrv_open_local_allocate_state *state =
210 (struct smbXsrv_open_local_allocate_state *)private_data;
211 TDB_DATA key = dbwrap_record_get_key(rec);
212 uint32_t id = 0;
213 NTSTATUS status;
215 status = smbXsrv_open_local_key_to_id(key, &id);
216 if (!NT_STATUS_IS_OK(status)) {
217 state->status = status;
218 return -1;
221 if (id <= state->last_id) {
222 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
223 return -1;
225 state->last_id = id;
227 if (id > state->useable_id) {
228 state->status = NT_STATUS_OK;
229 return -1;
232 if (state->useable_id == state->highest_id) {
233 state->status = NT_STATUS_INSUFFICIENT_RESOURCES;
234 return -1;
237 state->useable_id +=1;
238 return 0;
241 static NTSTATUS smbXsrv_open_local_allocate_id(struct db_context *db,
242 uint32_t lowest_id,
243 uint32_t highest_id,
244 TALLOC_CTX *mem_ctx,
245 struct db_record **_rec,
246 uint32_t *_id)
248 struct smbXsrv_open_local_allocate_state state = {
249 .lowest_id = lowest_id,
250 .highest_id = highest_id,
251 .last_id = 0,
252 .useable_id = lowest_id,
253 .status = NT_STATUS_INTERNAL_ERROR,
255 uint32_t i;
256 uint32_t range;
257 NTSTATUS status;
258 int count = 0;
260 *_rec = NULL;
261 *_id = 0;
263 if (lowest_id > highest_id) {
264 return NT_STATUS_INSUFFICIENT_RESOURCES;
268 * first we try randomly
270 range = (highest_id - lowest_id) + 1;
272 for (i = 0; i < (range / 2); i++) {
273 uint32_t id;
274 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
275 TDB_DATA key;
276 TDB_DATA val;
277 struct db_record *rec = NULL;
279 id = generate_random() % range;
280 id += lowest_id;
282 if (id < lowest_id) {
283 id = lowest_id;
285 if (id > highest_id) {
286 id = highest_id;
289 key = smbXsrv_open_local_id_to_key(id, key_buf);
291 rec = dbwrap_fetch_locked(db, mem_ctx, key);
292 if (rec == NULL) {
293 return NT_STATUS_INSUFFICIENT_RESOURCES;
296 val = dbwrap_record_get_value(rec);
297 if (val.dsize != 0) {
298 TALLOC_FREE(rec);
299 continue;
302 *_rec = rec;
303 *_id = id;
304 return NT_STATUS_OK;
308 * if the range is almost full,
309 * we traverse the whole table
310 * (this relies on sorted behavior of dbwrap_rbt)
312 status = dbwrap_traverse_read(db, smbXsrv_open_local_allocate_traverse,
313 &state, &count);
314 if (NT_STATUS_IS_OK(status)) {
315 if (NT_STATUS_IS_OK(state.status)) {
316 return NT_STATUS_INTERNAL_ERROR;
319 if (!NT_STATUS_EQUAL(state.status, NT_STATUS_INTERNAL_ERROR)) {
320 return state.status;
323 if (state.useable_id <= state.highest_id) {
324 state.status = NT_STATUS_OK;
325 } else {
326 return NT_STATUS_INSUFFICIENT_RESOURCES;
328 } else if (!NT_STATUS_EQUAL(status, NT_STATUS_INTERNAL_DB_CORRUPTION)) {
330 * Here we really expect NT_STATUS_INTERNAL_DB_CORRUPTION!
332 * If we get anything else it is an error, because it
333 * means we did not manage to find a free slot in
334 * the db.
336 return NT_STATUS_INSUFFICIENT_RESOURCES;
339 if (NT_STATUS_IS_OK(state.status)) {
340 uint32_t id;
341 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
342 TDB_DATA key;
343 TDB_DATA val;
344 struct db_record *rec = NULL;
346 id = state.useable_id;
348 key = smbXsrv_open_local_id_to_key(id, key_buf);
350 rec = dbwrap_fetch_locked(db, mem_ctx, key);
351 if (rec == NULL) {
352 return NT_STATUS_INSUFFICIENT_RESOURCES;
355 val = dbwrap_record_get_value(rec);
356 if (val.dsize != 0) {
357 TALLOC_FREE(rec);
358 return NT_STATUS_INTERNAL_DB_CORRUPTION;
361 *_rec = rec;
362 *_id = id;
363 return NT_STATUS_OK;
366 return state.status;
369 struct smbXsrv_open_local_fetch_state {
370 struct smbXsrv_open *op;
371 NTSTATUS status;
374 static void smbXsrv_open_local_fetch_parser(TDB_DATA key, TDB_DATA data,
375 void *private_data)
377 struct smbXsrv_open_local_fetch_state *state =
378 (struct smbXsrv_open_local_fetch_state *)private_data;
379 void *ptr;
381 if (data.dsize != sizeof(ptr)) {
382 state->status = NT_STATUS_INTERNAL_DB_ERROR;
383 return;
386 memcpy(&ptr, data.dptr, data.dsize);
387 state->op = talloc_get_type_abort(ptr, struct smbXsrv_open);
388 state->status = NT_STATUS_OK;
391 static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
392 uint32_t open_local_id,
393 uint32_t open_global_id,
394 NTTIME now,
395 struct smbXsrv_open **_open)
397 struct smbXsrv_open_local_fetch_state state = {
398 .op = NULL,
399 .status = NT_STATUS_INTERNAL_ERROR,
401 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
402 TDB_DATA key;
403 NTSTATUS status;
405 *_open = NULL;
407 if (open_local_id == 0) {
408 return NT_STATUS_FILE_CLOSED;
411 if (table == NULL) {
412 /* this might happen before the end of negprot */
413 return NT_STATUS_FILE_CLOSED;
416 if (table->local.db_ctx == NULL) {
417 return NT_STATUS_INTERNAL_ERROR;
420 key = smbXsrv_open_local_id_to_key(open_local_id, key_buf);
422 status = dbwrap_parse_record(table->local.db_ctx, key,
423 smbXsrv_open_local_fetch_parser,
424 &state);
425 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
426 return NT_STATUS_FILE_CLOSED;
427 } else if (!NT_STATUS_IS_OK(status)) {
428 return status;
430 if (!NT_STATUS_IS_OK(state.status)) {
431 return state.status;
434 if (NT_STATUS_EQUAL(state.op->status, NT_STATUS_FILE_CLOSED)) {
435 return NT_STATUS_FILE_CLOSED;
438 if (open_global_id == 0) {
439 /* make the global check a no-op for SMB1 */
440 open_global_id = state.op->global->open_global_id;
443 if (state.op->global->open_global_id != open_global_id) {
444 return NT_STATUS_FILE_CLOSED;
447 state.op->idle_time = now;
449 *_open = state.op;
450 return state.op->status;
453 static int smbXsrv_open_global_destructor(struct smbXsrv_open_global0 *global)
455 return 0;
458 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
459 bool *is_free,
460 bool *was_free,
461 TALLOC_CTX *mem_ctx,
462 struct smbXsrv_open_global0 **_g);
464 static NTSTATUS smbXsrv_open_global_allocate(struct db_context *db,
465 TALLOC_CTX *mem_ctx,
466 struct smbXsrv_open_global0 **_global)
468 uint32_t i;
469 struct smbXsrv_open_global0 *global = NULL;
470 uint32_t last_free = 0;
471 const uint32_t min_tries = 3;
473 *_global = NULL;
475 global = talloc_zero(mem_ctx, struct smbXsrv_open_global0);
476 if (global == NULL) {
477 return NT_STATUS_NO_MEMORY;
479 talloc_set_destructor(global, smbXsrv_open_global_destructor);
482 * Here we just randomly try the whole 32-bit space
484 * We use just 32-bit, because we want to reuse the
485 * ID for SRVSVC.
487 for (i = 0; i < UINT32_MAX; i++) {
488 bool is_free = false;
489 bool was_free = false;
490 uint32_t id;
491 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
492 TDB_DATA key;
494 if (i >= min_tries && last_free != 0) {
495 id = last_free;
496 } else {
497 id = generate_random();
499 if (id == 0) {
500 id++;
502 if (id == UINT32_MAX) {
503 id--;
506 key = smbXsrv_open_global_id_to_key(id, key_buf);
508 global->db_rec = dbwrap_fetch_locked(db, mem_ctx, key);
509 if (global->db_rec == NULL) {
510 talloc_free(global);
511 return NT_STATUS_INSUFFICIENT_RESOURCES;
514 smbXsrv_open_global_verify_record(global->db_rec,
515 &is_free,
516 &was_free,
517 NULL, NULL);
519 if (!is_free) {
520 TALLOC_FREE(global->db_rec);
521 continue;
524 if (!was_free && i < min_tries) {
526 * The session_id is free now,
527 * but was not free before.
529 * This happens if a smbd crashed
530 * and did not cleanup the record.
532 * If this is one of our first tries,
533 * then we try to find a real free one.
535 if (last_free == 0) {
536 last_free = id;
538 TALLOC_FREE(global->db_rec);
539 continue;
542 global->open_global_id = id;
544 *_global = global;
545 return NT_STATUS_OK;
548 /* should not be reached */
549 talloc_free(global);
550 return NT_STATUS_INTERNAL_ERROR;
553 static void smbXsrv_open_global_verify_record(struct db_record *db_rec,
554 bool *is_free,
555 bool *was_free,
556 TALLOC_CTX *mem_ctx,
557 struct smbXsrv_open_global0 **_g)
559 TDB_DATA key;
560 TDB_DATA val;
561 DATA_BLOB blob;
562 struct smbXsrv_open_globalB global_blob;
563 enum ndr_err_code ndr_err;
564 struct smbXsrv_open_global0 *global = NULL;
565 bool exists;
566 TALLOC_CTX *frame = talloc_stackframe();
568 *is_free = false;
570 if (was_free) {
571 *was_free = false;
573 if (_g) {
574 *_g = NULL;
577 key = dbwrap_record_get_key(db_rec);
579 val = dbwrap_record_get_value(db_rec);
580 if (val.dsize == 0) {
581 TALLOC_FREE(frame);
582 *is_free = true;
583 if (was_free) {
584 *was_free = true;
586 return;
589 blob = data_blob_const(val.dptr, val.dsize);
591 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
592 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
593 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
594 NTSTATUS status = ndr_map_error2ntstatus(ndr_err);
595 DEBUG(1,("smbXsrv_open_global_verify_record: "
596 "key '%s' ndr_pull_struct_blob - %s\n",
597 hex_encode_talloc(frame, key.dptr, key.dsize),
598 nt_errstr(status)));
599 TALLOC_FREE(frame);
600 return;
603 DEBUG(10,("smbXsrv_open_global_verify_record\n"));
604 if (CHECK_DEBUGLVL(10)) {
605 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
608 if (global_blob.version != SMBXSRV_VERSION_0) {
609 DEBUG(0,("smbXsrv_open_global_verify_record: "
610 "key '%s' use unsupported version %u\n",
611 hex_encode_talloc(frame, key.dptr, key.dsize),
612 global_blob.version));
613 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
614 TALLOC_FREE(frame);
615 return;
618 global = global_blob.info.info0;
620 if (server_id_is_disconnected(&global->server_id)) {
621 exists = true;
622 } else {
623 exists = serverid_exists(&global->server_id);
625 if (!exists) {
626 DEBUG(2,("smbXsrv_open_global_verify_record: "
627 "key '%s' server_id %s does not exist.\n",
628 hex_encode_talloc(frame, key.dptr, key.dsize),
629 server_id_str(frame, &global->server_id)));
630 if (CHECK_DEBUGLVL(2)) {
631 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
633 TALLOC_FREE(frame);
634 dbwrap_record_delete(db_rec);
635 *is_free = true;
636 return;
639 if (_g) {
640 *_g = talloc_move(mem_ctx, &global);
642 TALLOC_FREE(frame);
645 static NTSTATUS smbXsrv_open_global_store(struct smbXsrv_open_global0 *global)
647 struct smbXsrv_open_globalB global_blob;
648 DATA_BLOB blob = data_blob_null;
649 TDB_DATA key;
650 TDB_DATA val;
651 NTSTATUS status;
652 enum ndr_err_code ndr_err;
655 * TODO: if we use other versions than '0'
656 * we would add glue code here, that would be able to
657 * store the information in the old format.
660 if (global->db_rec == NULL) {
661 return NT_STATUS_INTERNAL_ERROR;
664 key = dbwrap_record_get_key(global->db_rec);
665 val = dbwrap_record_get_value(global->db_rec);
667 ZERO_STRUCT(global_blob);
668 global_blob.version = smbXsrv_version_global_current();
669 if (val.dsize >= 8) {
670 global_blob.seqnum = IVAL(val.dptr, 4);
672 global_blob.seqnum += 1;
673 global_blob.info.info0 = global;
675 ndr_err = ndr_push_struct_blob(&blob, global->db_rec, &global_blob,
676 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
677 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
678 status = ndr_map_error2ntstatus(ndr_err);
679 DEBUG(1,("smbXsrv_open_global_store: key '%s' ndr_push - %s\n",
680 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
681 nt_errstr(status)));
682 TALLOC_FREE(global->db_rec);
683 return status;
686 val = make_tdb_data(blob.data, blob.length);
687 status = dbwrap_record_store(global->db_rec, val, TDB_REPLACE);
688 if (!NT_STATUS_IS_OK(status)) {
689 DEBUG(1,("smbXsrv_open_global_store: key '%s' store - %s\n",
690 hex_encode_talloc(global->db_rec, key.dptr, key.dsize),
691 nt_errstr(status)));
692 TALLOC_FREE(global->db_rec);
693 return status;
696 if (CHECK_DEBUGLVL(10)) {
697 DEBUG(10,("smbXsrv_open_global_store: key '%s' stored\n",
698 hex_encode_talloc(global->db_rec, key.dptr, key.dsize)));
699 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
702 TALLOC_FREE(global->db_rec);
704 return NT_STATUS_OK;
707 static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
708 uint32_t open_global_id,
709 TALLOC_CTX *mem_ctx,
710 struct smbXsrv_open_global0 **_global)
712 TDB_DATA key;
713 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
714 struct db_record *global_rec = NULL;
715 bool is_free = false;
717 *_global = NULL;
719 if (table->global.db_ctx == NULL) {
720 return NT_STATUS_INTERNAL_ERROR;
723 key = smbXsrv_open_global_id_to_key(open_global_id, key_buf);
725 global_rec = dbwrap_fetch_locked(table->global.db_ctx, mem_ctx, key);
726 if (global_rec == NULL) {
727 DEBUG(0, ("smbXsrv_open_global_lookup(0x%08x): "
728 "Failed to lock global key '%s'\n",
729 open_global_id,
730 hex_encode_talloc(talloc_tos(), key.dptr,
731 key.dsize)));
732 return NT_STATUS_INTERNAL_DB_ERROR;
735 smbXsrv_open_global_verify_record(global_rec,
736 &is_free,
737 NULL,
738 mem_ctx,
739 _global);
740 if (is_free) {
741 talloc_free(global_rec);
742 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
745 (*_global)->db_rec = talloc_move(*_global, &global_rec);
747 talloc_set_destructor(*_global, smbXsrv_open_global_destructor);
749 return NT_STATUS_OK;
752 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
754 NTSTATUS status;
756 status = smbXsrv_open_close(op, 0);
757 if (!NT_STATUS_IS_OK(status)) {
758 DEBUG(0, ("smbXsrv_open_destructor: "
759 "smbXsrv_open_close() failed - %s\n",
760 nt_errstr(status)));
763 TALLOC_FREE(op->global);
765 return 0;
768 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
769 struct auth_session_info *session_info,
770 NTTIME now,
771 struct smbXsrv_open **_open)
773 struct smbXsrv_open_table *table = conn->open_table;
774 struct db_record *local_rec = NULL;
775 struct smbXsrv_open *op = NULL;
776 void *ptr = NULL;
777 TDB_DATA val;
778 struct smbXsrv_open_global0 *global = NULL;
779 NTSTATUS status;
780 struct dom_sid *current_sid = NULL;
781 struct security_token *current_token = NULL;
783 if (session_info == NULL) {
784 return NT_STATUS_INVALID_HANDLE;
786 current_token = session_info->security_token;
788 if (current_token == NULL) {
789 return NT_STATUS_INVALID_HANDLE;
792 if (current_token->num_sids > PRIMARY_USER_SID_INDEX) {
793 current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
796 if (current_sid == NULL) {
797 return NT_STATUS_INVALID_HANDLE;
800 if (table->local.num_opens >= table->local.max_opens) {
801 return NT_STATUS_INSUFFICIENT_RESOURCES;
804 op = talloc_zero(table, struct smbXsrv_open);
805 if (op == NULL) {
806 return NT_STATUS_NO_MEMORY;
808 op->table = table;
809 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
810 op->idle_time = now;
812 status = smbXsrv_open_global_allocate(table->global.db_ctx,
813 op, &global);
814 if (!NT_STATUS_IS_OK(status)) {
815 TALLOC_FREE(op);
816 return status;
818 op->global = global;
820 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
821 table->local.lowest_id,
822 table->local.highest_id,
824 &local_rec,
825 &op->local_id);
826 if (!NT_STATUS_IS_OK(status)) {
827 TALLOC_FREE(op);
828 return status;
831 global->open_persistent_id = global->open_global_id;
832 global->open_volatile_id = op->local_id;
834 global->server_id = messaging_server_id(conn->msg_ctx);
835 global->open_time = now;
836 global->open_owner = *current_sid;
837 if (conn->protocol >= PROTOCOL_SMB2_10) {
838 global->client_guid = conn->smb2.client.guid;
841 ptr = op;
842 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
843 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
844 TALLOC_FREE(local_rec);
845 if (!NT_STATUS_IS_OK(status)) {
846 TALLOC_FREE(op);
847 return status;
849 table->local.num_opens += 1;
851 talloc_set_destructor(op, smbXsrv_open_destructor);
853 status = smbXsrv_open_global_store(global);
854 if (!NT_STATUS_IS_OK(status)) {
855 DEBUG(0,("smbXsrv_open_create: "
856 "global_id (0x%08x) store failed - %s\n",
857 op->global->open_global_id,
858 nt_errstr(status)));
859 TALLOC_FREE(op);
860 return status;
863 if (CHECK_DEBUGLVL(10)) {
864 struct smbXsrv_openB open_blob;
866 ZERO_STRUCT(open_blob);
867 open_blob.version = SMBXSRV_VERSION_0;
868 open_blob.info.info0 = op;
870 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
871 op->global->open_global_id));
872 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
875 *_open = op;
876 return NT_STATUS_OK;
879 uint32_t smbXsrv_open_hash(struct smbXsrv_open *_open)
881 uint8_t buf[8+8+8];
882 uint32_t ret;
884 SBVAL(buf, 0, _open->global->open_persistent_id);
885 SBVAL(buf, 8, _open->global->open_volatile_id);
886 SBVAL(buf, 16, _open->global->open_time);
888 ret = hash(buf, sizeof(buf), 0);
890 if (ret == 0) {
891 ret = 1;
894 return ret;
897 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
899 struct smbXsrv_open_table *table = op->table;
900 NTSTATUS status;
901 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
902 TDB_DATA key;
904 if (op->global->db_rec != NULL) {
905 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
906 "Called with db_rec != NULL'\n",
907 op->global->open_global_id));
908 return NT_STATUS_INTERNAL_ERROR;
911 key = smbXsrv_open_global_id_to_key(op->global->open_global_id,
912 key_buf);
914 op->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
915 op->global, key);
916 if (op->global->db_rec == NULL) {
917 DEBUG(0, ("smbXsrv_open_update(0x%08x): "
918 "Failed to lock global key '%s'\n",
919 op->global->open_global_id,
920 hex_encode_talloc(talloc_tos(), key.dptr,
921 key.dsize)));
922 return NT_STATUS_INTERNAL_DB_ERROR;
925 status = smbXsrv_open_global_store(op->global);
926 if (!NT_STATUS_IS_OK(status)) {
927 DEBUG(0,("smbXsrv_open_update: "
928 "global_id (0x%08x) store failed - %s\n",
929 op->global->open_global_id,
930 nt_errstr(status)));
931 return status;
934 if (CHECK_DEBUGLVL(10)) {
935 struct smbXsrv_openB open_blob;
937 ZERO_STRUCT(open_blob);
938 open_blob.version = SMBXSRV_VERSION_0;
939 open_blob.info.info0 = op;
941 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
942 op->global->open_global_id));
943 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
946 return NT_STATUS_OK;
949 NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
951 struct smbXsrv_open_table *table;
952 struct db_record *local_rec = NULL;
953 struct db_record *global_rec = NULL;
954 NTSTATUS status;
955 NTSTATUS error = NT_STATUS_OK;
957 if (op->table == NULL) {
958 return NT_STATUS_OK;
961 table = op->table;
962 op->table = NULL;
964 op->status = NT_STATUS_FILE_CLOSED;
965 op->global->disconnect_time = now;
966 server_id_set_disconnected(&op->global->server_id);
968 global_rec = op->global->db_rec;
969 op->global->db_rec = NULL;
970 if (global_rec == NULL) {
971 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
972 TDB_DATA key;
974 key = smbXsrv_open_global_id_to_key(
975 op->global->open_global_id,
976 key_buf);
978 global_rec = dbwrap_fetch_locked(table->global.db_ctx,
979 op->global, key);
980 if (global_rec == NULL) {
981 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
982 "Failed to lock global key '%s'\n",
983 op->global->open_global_id,
984 hex_encode_talloc(global_rec, key.dptr,
985 key.dsize)));
986 error = NT_STATUS_INTERNAL_ERROR;
990 if (global_rec != NULL && op->global->durable) {
992 * If it is a durable open we need to update the global part
993 * instead of deleting it
995 op->global->db_rec = global_rec;
996 status = smbXsrv_open_global_store(op->global);
997 if (NT_STATUS_IS_OK(status)) {
999 * smbXsrv_open_global_store does the free
1000 * of op->global->db_rec
1002 global_rec = NULL;
1004 if (!NT_STATUS_IS_OK(status)) {
1005 DEBUG(0,("smbXsrv_open_close(0x%08x)"
1006 "smbXsrv_open_global_store() failed - %s\n",
1007 op->global->open_global_id,
1008 nt_errstr(status)));
1009 error = status;
1012 if (NT_STATUS_IS_OK(status) && CHECK_DEBUGLVL(10)) {
1013 struct smbXsrv_openB open_blob;
1015 ZERO_STRUCT(open_blob);
1016 open_blob.version = SMBXSRV_VERSION_0;
1017 open_blob.info.info0 = op;
1019 DEBUG(10,("smbXsrv_open_close(0x%08x): "
1020 "stored disconnect\n",
1021 op->global->open_global_id));
1022 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1026 if (global_rec != NULL) {
1027 status = dbwrap_record_delete(global_rec);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 TDB_DATA key = dbwrap_record_get_key(global_rec);
1031 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1032 "failed to delete global key '%s': %s\n",
1033 op->global->open_global_id,
1034 hex_encode_talloc(global_rec, key.dptr,
1035 key.dsize),
1036 nt_errstr(status)));
1037 error = status;
1040 TALLOC_FREE(global_rec);
1042 local_rec = op->db_rec;
1043 if (local_rec == NULL) {
1044 uint8_t key_buf[SMBXSRV_OPEN_LOCAL_TDB_KEY_SIZE];
1045 TDB_DATA key;
1047 key = smbXsrv_open_local_id_to_key(op->local_id, key_buf);
1049 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
1050 op, key);
1051 if (local_rec == NULL) {
1052 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1053 "Failed to lock local key '%s'\n",
1054 op->global->open_global_id,
1055 hex_encode_talloc(local_rec, key.dptr,
1056 key.dsize)));
1057 error = NT_STATUS_INTERNAL_ERROR;
1061 if (local_rec != NULL) {
1062 status = dbwrap_record_delete(local_rec);
1063 if (!NT_STATUS_IS_OK(status)) {
1064 TDB_DATA key = dbwrap_record_get_key(local_rec);
1066 DEBUG(0, ("smbXsrv_open_close(0x%08x): "
1067 "failed to delete local key '%s': %s\n",
1068 op->global->open_global_id,
1069 hex_encode_talloc(local_rec, key.dptr,
1070 key.dsize),
1071 nt_errstr(status)));
1072 error = status;
1074 table->local.num_opens -= 1;
1076 if (op->db_rec == NULL) {
1077 TALLOC_FREE(local_rec);
1079 op->db_rec = NULL;
1081 if (op->compat) {
1082 op->compat->op = NULL;
1083 file_free(NULL, op->compat);
1084 op->compat = NULL;
1087 return error;
1090 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1092 uint32_t max_opens;
1095 * Allow a range from 1..65534.
1097 * With real_max_open_files possible ids,
1098 * truncated to the SMB1 limit of 16-bit.
1100 * 0 and 0xFFFF are no valid ids.
1102 max_opens = conn->sconn->real_max_open_files;
1103 max_opens = MIN(max_opens, UINT16_MAX - 1);
1105 return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
1108 NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
1109 uint16_t fnum, NTTIME now,
1110 struct smbXsrv_open **_open)
1112 struct smbXsrv_open_table *table = conn->open_table;
1113 uint32_t local_id = fnum;
1114 uint32_t global_id = 0;
1116 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1119 NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
1121 uint32_t max_opens;
1124 * Allow a range from 1..4294967294.
1126 * With real_max_open_files possible ids,
1127 * truncated to 16-bit (the same as SMB1 for now).
1129 * 0 and 0xFFFFFFFF are no valid ids.
1131 * The usage of conn->sconn->real_max_open_files
1132 * is the reason that we use one open table per
1133 * transport connection (as we still have a 1:1 mapping
1134 * between process and transport connection).
1136 max_opens = conn->sconn->real_max_open_files;
1137 max_opens = MIN(max_opens, UINT16_MAX - 1);
1139 return smbXsrv_open_table_init(conn, 1, UINT32_MAX - 1, max_opens);
1142 NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
1143 uint64_t persistent_id,
1144 uint64_t volatile_id,
1145 NTTIME now,
1146 struct smbXsrv_open **_open)
1148 struct smbXsrv_open_table *table = conn->open_table;
1149 uint32_t local_id = volatile_id & UINT32_MAX;
1150 uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
1151 uint32_t global_id = persistent_id & UINT32_MAX;
1152 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1154 if (local_zeros != 0) {
1155 return NT_STATUS_FILE_CLOSED;
1158 if (global_zeros != 0) {
1159 return NT_STATUS_FILE_CLOSED;
1162 if (global_id == 0) {
1163 return NT_STATUS_FILE_CLOSED;
1166 return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
1169 NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
1170 struct auth_session_info *session_info,
1171 uint64_t persistent_id,
1172 const struct GUID *create_guid,
1173 NTTIME now,
1174 struct smbXsrv_open **_open)
1176 struct smbXsrv_open_table *table = conn->open_table;
1177 struct db_record *local_rec = NULL;
1178 struct smbXsrv_open *op = NULL;
1179 void *ptr = NULL;
1180 TDB_DATA val;
1181 uint32_t global_id = persistent_id & UINT32_MAX;
1182 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1183 NTSTATUS status;
1184 struct security_token *current_token = NULL;
1186 if (session_info == NULL) {
1187 return NT_STATUS_INVALID_HANDLE;
1189 current_token = session_info->security_token;
1191 if (current_token == NULL) {
1192 return NT_STATUS_INVALID_HANDLE;
1195 if (global_zeros != 0) {
1196 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1199 op = talloc_zero(table, struct smbXsrv_open);
1200 if (op == NULL) {
1201 return NT_STATUS_NO_MEMORY;
1203 op->table = table;
1205 status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
1206 if (!NT_STATUS_IS_OK(status)) {
1207 TALLOC_FREE(op);
1208 return status;
1212 * If the provided create_guid is NULL, this means that
1213 * the reconnect request was a v1 request. In that case
1214 * we should skipt the create GUID verification, since
1215 * it is valid to v1-reconnect a v2-opened handle.
1217 if ((create_guid != NULL) &&
1218 !GUID_equal(&op->global->create_guid, create_guid))
1220 TALLOC_FREE(op);
1221 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1224 if (!security_token_is_sid(current_token, &op->global->open_owner)) {
1225 TALLOC_FREE(op);
1226 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1229 if (!op->global->durable) {
1230 TALLOC_FREE(op);
1231 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1234 if (table->local.num_opens >= table->local.max_opens) {
1235 TALLOC_FREE(op);
1236 return NT_STATUS_INSUFFICIENT_RESOURCES;
1239 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
1240 table->local.lowest_id,
1241 table->local.highest_id,
1243 &local_rec,
1244 &op->local_id);
1245 if (!NT_STATUS_IS_OK(status)) {
1246 TALLOC_FREE(op);
1247 return status;
1250 op->idle_time = now;
1251 op->status = NT_STATUS_FILE_CLOSED;
1253 op->global->open_volatile_id = op->local_id;
1254 op->global->server_id = messaging_server_id(conn->msg_ctx);
1256 ptr = op;
1257 val = make_tdb_data((uint8_t const *)&ptr, sizeof(ptr));
1258 status = dbwrap_record_store(local_rec, val, TDB_REPLACE);
1259 TALLOC_FREE(local_rec);
1260 if (!NT_STATUS_IS_OK(status)) {
1261 TALLOC_FREE(op);
1262 return status;
1264 table->local.num_opens += 1;
1266 talloc_set_destructor(op, smbXsrv_open_destructor);
1268 status = smbXsrv_open_global_store(op->global);
1269 if (!NT_STATUS_IS_OK(status)) {
1270 TALLOC_FREE(op);
1271 return status;
1274 if (CHECK_DEBUGLVL(10)) {
1275 struct smbXsrv_openB open_blob;
1277 ZERO_STRUCT(open_blob);
1278 open_blob.version = 0;
1279 open_blob.info.info0 = op;
1281 DEBUG(10,("smbXsrv_open_recreate: global_id (0x%08x) stored\n",
1282 op->global->open_global_id));
1283 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
1286 *_open = op;
1287 return NT_STATUS_OK;
1291 static NTSTATUS smbXsrv_open_global_parse_record(TALLOC_CTX *mem_ctx,
1292 struct db_record *rec,
1293 struct smbXsrv_open_global0 **global)
1295 TDB_DATA key = dbwrap_record_get_key(rec);
1296 TDB_DATA val = dbwrap_record_get_value(rec);
1297 DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
1298 struct smbXsrv_open_globalB global_blob;
1299 enum ndr_err_code ndr_err;
1300 NTSTATUS status;
1301 TALLOC_CTX *frame = talloc_stackframe();
1303 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
1304 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
1305 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1306 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1307 "key '%s' ndr_pull_struct_blob - %s\n",
1308 hex_encode_talloc(frame, key.dptr, key.dsize),
1309 ndr_errstr(ndr_err)));
1310 status = ndr_map_error2ntstatus(ndr_err);
1311 goto done;
1314 if (global_blob.version != SMBXSRV_VERSION_0) {
1315 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
1316 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
1317 "key '%s' unsuported version - %d - %s\n",
1318 hex_encode_talloc(frame, key.dptr, key.dsize),
1319 (int)global_blob.version,
1320 nt_errstr(status)));
1321 goto done;
1324 *global = talloc_move(mem_ctx, &global_blob.info.info0);
1325 status = NT_STATUS_OK;
1326 done:
1327 talloc_free(frame);
1328 return status;
1331 struct smbXsrv_open_global_traverse_state {
1332 int (*fn)(struct smbXsrv_open_global0 *, void *);
1333 void *private_data;
1336 static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
1338 struct smbXsrv_open_global_traverse_state *state =
1339 (struct smbXsrv_open_global_traverse_state*)data;
1340 struct smbXsrv_open_global0 *global = NULL;
1341 NTSTATUS status;
1342 int ret = -1;
1344 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &global);
1345 if (!NT_STATUS_IS_OK(status)) {
1346 return -1;
1349 global->db_rec = rec;
1350 ret = state->fn(global, state->private_data);
1351 talloc_free(global);
1352 return ret;
1355 NTSTATUS smbXsrv_open_global_traverse(
1356 int (*fn)(struct smbXsrv_open_global0 *, void *),
1357 void *private_data)
1360 NTSTATUS status;
1361 int count = 0;
1362 struct smbXsrv_open_global_traverse_state state = {
1363 .fn = fn,
1364 .private_data = private_data,
1367 become_root();
1368 status = smbXsrv_open_global_init();
1369 if (!NT_STATUS_IS_OK(status)) {
1370 unbecome_root();
1371 DEBUG(0, ("Failed to initialize open_global: %s\n",
1372 nt_errstr(status)));
1373 return status;
1376 status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
1377 smbXsrv_open_global_traverse_fn,
1378 &state,
1379 &count);
1380 unbecome_root();
1382 return status;
1385 NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
1387 NTSTATUS status = NT_STATUS_OK;
1388 TALLOC_CTX *frame = talloc_stackframe();
1389 struct smbXsrv_open_global0 *op = NULL;
1390 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
1391 TDB_DATA key;
1392 TDB_DATA val;
1393 struct db_record *rec;
1394 bool delete_open = false;
1395 uint32_t global_id = persistent_id & UINT32_MAX;
1397 key = smbXsrv_open_global_id_to_key(global_id, key_buf);
1398 rec = dbwrap_fetch_locked(smbXsrv_open_global_db_ctx, frame, key);
1399 if (rec == NULL) {
1400 status = NT_STATUS_NOT_FOUND;
1401 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1402 "failed to fetch record from %s - %s\n",
1403 global_id, dbwrap_name(smbXsrv_open_global_db_ctx),
1404 nt_errstr(status)));
1405 goto done;
1408 val = dbwrap_record_get_value(rec);
1409 if (val.dsize == 0) {
1410 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1411 "empty record in %s, skipping...\n",
1412 global_id, dbwrap_name(smbXsrv_open_global_db_ctx)));
1413 goto done;
1416 status = smbXsrv_open_global_parse_record(talloc_tos(), rec, &op);
1417 if (!NT_STATUS_IS_OK(status)) {
1418 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1419 "failed to read record: %s\n",
1420 global_id, nt_errstr(status)));
1421 goto done;
1424 if (server_id_is_disconnected(&op->server_id)) {
1425 struct timeval now, disconnect_time;
1426 int64_t tdiff;
1427 now = timeval_current();
1428 nttime_to_timeval(&disconnect_time, op->disconnect_time);
1429 tdiff = usec_time_diff(&now, &disconnect_time);
1430 delete_open = (tdiff >= 1000*op->durable_timeout_msec);
1432 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1433 "disconnected at [%s] %us ago with "
1434 "timeout of %us -%s reached\n",
1435 global_id,
1436 nt_time_string(frame, op->disconnect_time),
1437 (unsigned)(tdiff/1000000),
1438 op->durable_timeout_msec / 1000,
1439 delete_open ? "" : " not"));
1440 } else if (!serverid_exists(&op->server_id)) {
1441 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1442 "server[%s] does not exist\n",
1443 global_id, server_id_str(frame, &op->server_id)));
1444 delete_open = true;
1447 if (!delete_open) {
1448 goto done;
1451 status = dbwrap_record_delete(rec);
1452 if (!NT_STATUS_IS_OK(status)) {
1453 DEBUG(1, ("smbXsrv_open_cleanup[global: 0x%08x] "
1454 "failed to delete record"
1455 "from %s: %s\n", global_id,
1456 dbwrap_name(smbXsrv_open_global_db_ctx),
1457 nt_errstr(status)));
1458 goto done;
1461 DEBUG(10, ("smbXsrv_open_cleanup[global: 0x%08x] "
1462 "delete record from %s\n",
1463 global_id,
1464 dbwrap_name(smbXsrv_open_global_db_ctx)));
1466 done:
1467 talloc_free(frame);
1468 return status;