dsdb-acl: add acl_check_access_on_objectclass() helper
[Samba/gbeck.git] / source3 / smbd / smbXsrv_open.c
blobc1754e86b2ef777c9338982940aca2a2f6f94038
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 if (server_id_is_disconnected(&global->server_id)) {
620 exists = true;
621 } else {
622 exists = serverid_exists(&global->server_id);
624 if (!exists) {
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)));
629 if (DEBUGLVL(2)) {
630 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
632 TALLOC_FREE(frame);
633 dbwrap_record_delete(db_rec);
634 *is_free = true;
635 return;
638 if (_g) {
639 *_g = talloc_move(mem_ctx, &global);
641 TALLOC_FREE(frame);
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;
648 TDB_DATA key;
649 TDB_DATA val;
650 NTSTATUS status;
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),
680 nt_errstr(status)));
681 TALLOC_FREE(global->db_rec);
682 return status;
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),
690 nt_errstr(status)));
691 TALLOC_FREE(global->db_rec);
692 return status;
695 if (DEBUGLVL(10)) {
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);
703 return NT_STATUS_OK;
706 static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table,
707 uint32_t open_global_id,
708 TALLOC_CTX *mem_ctx,
709 struct smbXsrv_open_global0 **_global)
711 TDB_DATA key;
712 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
713 struct db_record *global_rec = NULL;
714 bool is_free = false;
716 *_global = NULL;
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",
728 open_global_id,
729 hex_encode_talloc(talloc_tos(), key.dptr,
730 key.dsize)));
731 return NT_STATUS_INTERNAL_DB_ERROR;
734 smbXsrv_open_global_verify_record(global_rec,
735 &is_free,
736 NULL,
737 mem_ctx,
738 _global);
739 if (is_free) {
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);
748 return NT_STATUS_OK;
751 static int smbXsrv_open_destructor(struct smbXsrv_open *op)
753 NTSTATUS status;
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",
759 nt_errstr(status)));
762 TALLOC_FREE(op->global);
764 return 0;
767 NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
768 struct auth_session_info *session_info,
769 NTTIME now,
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;
775 void *ptr = NULL;
776 TDB_DATA val;
777 struct smbXsrv_open_global0 *global = NULL;
778 NTSTATUS status;
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 = &current_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);
804 if (op == NULL) {
805 return NT_STATUS_NO_MEMORY;
807 op->table = table;
808 op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
809 op->idle_time = now;
811 status = smbXsrv_open_global_allocate(table->global.db_ctx,
812 op, &global);
813 if (!NT_STATUS_IS_OK(status)) {
814 TALLOC_FREE(op);
815 return status;
817 op->global = global;
819 status = smbXsrv_open_local_allocate_id(table->local.db_ctx,
820 table->local.lowest_id,
821 table->local.highest_id,
823 &local_rec,
824 &op->local_id);
825 if (!NT_STATUS_IS_OK(status)) {
826 TALLOC_FREE(op);
827 return 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;
840 ptr = op;
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)) {
845 TALLOC_FREE(op);
846 return 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,
857 nt_errstr(status)));
858 TALLOC_FREE(op);
859 return status;
862 if (DEBUGLVL(10)) {
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);
874 *_open = op;
875 return NT_STATUS_OK;
878 uint32_t smbXsrv_open_hash(struct smbXsrv_open *_open)
880 uint8_t buf[8+8+8];
881 uint32_t ret;
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);
889 if (ret == 0) {
890 ret = 1;
893 return ret;
896 NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
898 struct smbXsrv_open_table *table = op->table;
899 NTSTATUS status;
900 uint8_t key_buf[SMBXSRV_OPEN_GLOBAL_TDB_KEY_SIZE];
901 TDB_DATA key;
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,
911 key_buf);
913 op->global->db_rec = dbwrap_fetch_locked(table->global.db_ctx,
914 op->global, key);
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,
920 key.dsize)));
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,
929 nt_errstr(status)));
930 return status;
933 if (DEBUGLVL(10)) {
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);
945 return NT_STATUS_OK;
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;
953 NTSTATUS status;
954 NTSTATUS error = NT_STATUS_OK;
956 if (op->table == NULL) {
957 return NT_STATUS_OK;
960 table = op->table;
961 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];
971 TDB_DATA key;
973 key = smbXsrv_open_global_id_to_key(
974 op->global->open_global_id,
975 key_buf);
977 global_rec = dbwrap_fetch_locked(table->global.db_ctx,
978 op->global, key);
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,
984 key.dsize)));
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
1001 global_rec = NULL;
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)));
1008 error = 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,
1034 key.dsize),
1035 nt_errstr(status)));
1036 error = 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];
1044 TDB_DATA key;
1046 key = smbXsrv_open_local_id_to_key(op->local_id, key_buf);
1048 local_rec = dbwrap_fetch_locked(table->local.db_ctx,
1049 op, key);
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,
1055 key.dsize)));
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,
1069 key.dsize),
1070 nt_errstr(status)));
1071 error = status;
1073 table->local.num_opens -= 1;
1075 if (op->db_rec == NULL) {
1076 TALLOC_FREE(local_rec);
1078 op->db_rec = NULL;
1080 if (op->compat) {
1081 file_free(NULL, op->compat);
1082 op->compat = NULL;
1085 return error;
1088 NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
1090 uint32_t max_opens;
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)
1119 uint32_t max_opens;
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,
1143 NTTIME now,
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,
1171 NTTIME now,
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;
1177 void *ptr = NULL;
1178 TDB_DATA val;
1179 uint32_t global_id = persistent_id & UINT32_MAX;
1180 uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
1181 NTSTATUS status;
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);
1198 if (op == NULL) {
1199 return NT_STATUS_NO_MEMORY;
1201 op->table = table;
1203 status = smbXsrv_open_global_lookup(table, global_id, op, &op->global);
1204 if (!NT_STATUS_IS_OK(status)) {
1205 TALLOC_FREE(op);
1206 return status;
1209 if (!GUID_equal(&op->global->create_guid, &create_guid)) {
1210 TALLOC_FREE(op);
1211 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1214 if (!security_token_is_sid(current_token, &op->global->open_owner)) {
1215 TALLOC_FREE(op);
1216 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1219 if (!op->global->durable) {
1220 TALLOC_FREE(op);
1221 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1224 if (table->local.num_opens >= table->local.max_opens) {
1225 TALLOC_FREE(op);
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,
1233 &local_rec,
1234 &op->local_id);
1235 if (!NT_STATUS_IS_OK(status)) {
1236 TALLOC_FREE(op);
1237 return 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);
1246 ptr = op;
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)) {
1251 TALLOC_FREE(op);
1252 return 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)) {
1260 TALLOC_FREE(op);
1261 return status;
1264 if (DEBUGLVL(10)) {
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);
1276 *_open = op;
1277 return NT_STATUS_OK;