From: Volker Lendecke Date: Thu, 26 Jan 2023 07:46:31 +0000 (+0100) Subject: smbd: Move smbXsrv_open_global_verify_record() down in smbXsrv_open.c X-Git-Tag: talloc-2.4.1~1636 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fafebc46c8bf624736995f3a87819b3c075cb383;p=thirdparty%2Fsamba.git smbd: Move smbXsrv_open_global_verify_record() down in smbXsrv_open.c Avoid prototypes Signed-off-by: Volker Lendecke Reviewed-by: Stefan Metzmacher --- diff --git a/source3/smbd/smbXsrv_open.c b/source3/smbd/smbXsrv_open.c index c3042978da8..a7198001012 100644 --- a/source3/smbd/smbXsrv_open.c +++ b/source3/smbd/smbXsrv_open.c @@ -225,98 +225,6 @@ static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table, return NT_STATUS_OK; } -static NTSTATUS smbXsrv_open_global_verify_record( - TDB_DATA key, - TDB_DATA val, - TALLOC_CTX *mem_ctx, - struct smbXsrv_open_global0 **_global0); - -static NTSTATUS smbXsrv_open_global_allocate( - struct db_context *db, struct smbXsrv_open_global0 *global) -{ - uint32_t i; - uint32_t last_free = 0; - const uint32_t min_tries = 3; - - /* - * Here we just randomly try the whole 32-bit space - * - * We use just 32-bit, because we want to reuse the - * ID for SRVSVC. - */ - for (i = 0; i < UINT32_MAX; i++) { - struct smbXsrv_open_global_key_buf key_buf; - struct smbXsrv_open_global0 *tmp_global0 = NULL; - TDB_DATA key, val; - uint32_t id; - NTSTATUS status; - - if (i >= min_tries && last_free != 0) { - id = last_free; - } else { - generate_nonce_buffer((uint8_t *)&id, sizeof(id)); - id = MAX(id, 1); - id = MIN(id, UINT32_MAX-1); - } - - key = smbXsrv_open_global_id_to_key(id, &key_buf); - - global->db_rec = dbwrap_fetch_locked(db, global, key); - if (global->db_rec == NULL) { - return NT_STATUS_INSUFFICIENT_RESOURCES; - } - val = dbwrap_record_get_value(global->db_rec); - - status = smbXsrv_open_global_verify_record( - key, val, talloc_tos(), &tmp_global0); - - if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) { - /* - * Found an empty slot - */ - global->open_global_id = id; - return NT_STATUS_OK; - } - - TALLOC_FREE(tmp_global0); - - if (NT_STATUS_EQUAL(status, NT_STATUS_FATAL_APP_EXIT)) { - /* - * smbd crashed - */ - status = dbwrap_record_delete(global->db_rec); - if (!NT_STATUS_IS_OK(status)) { - DBG_WARNING("dbwrap_record_delete() failed " - "for record %"PRIu32": %s\n", - id, - nt_errstr(status)); - return NT_STATUS_INTERNAL_DB_CORRUPTION; - } - - if ((i < min_tries) && (last_free == 0)) { - /* - * Remember "id" as free but also try - * others to not recycle ids too - * quickly. - */ - last_free = id; - } - } - - if (!NT_STATUS_IS_OK(status)) { - DBG_WARNING("smbXsrv_open_global_verify_record() " - "failed for %s: %s, ignoring\n", - tdb_data_dbg(key), - nt_errstr(status)); - } - - TALLOC_FREE(global->db_rec); - } - - /* should not be reached */ - return NT_STATUS_INTERNAL_ERROR; -} - static NTSTATUS smbXsrv_open_global_parse_record( TALLOC_CTX *mem_ctx, TDB_DATA key, @@ -468,6 +376,92 @@ static NTSTATUS smbXsrv_open_global_store(struct smbXsrv_open_global0 *global) return NT_STATUS_OK; } +static NTSTATUS smbXsrv_open_global_allocate( + struct db_context *db, struct smbXsrv_open_global0 *global) +{ + uint32_t i; + uint32_t last_free = 0; + const uint32_t min_tries = 3; + + /* + * Here we just randomly try the whole 32-bit space + * + * We use just 32-bit, because we want to reuse the + * ID for SRVSVC. + */ + for (i = 0; i < UINT32_MAX; i++) { + struct smbXsrv_open_global_key_buf key_buf; + struct smbXsrv_open_global0 *tmp_global0 = NULL; + TDB_DATA key, val; + uint32_t id; + NTSTATUS status; + + if (i >= min_tries && last_free != 0) { + id = last_free; + } else { + generate_nonce_buffer((uint8_t *)&id, sizeof(id)); + id = MAX(id, 1); + id = MIN(id, UINT32_MAX-1); + } + + key = smbXsrv_open_global_id_to_key(id, &key_buf); + + global->db_rec = dbwrap_fetch_locked(db, global, key); + if (global->db_rec == NULL) { + return NT_STATUS_INSUFFICIENT_RESOURCES; + } + val = dbwrap_record_get_value(global->db_rec); + + status = smbXsrv_open_global_verify_record( + key, val, talloc_tos(), &tmp_global0); + + if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) { + /* + * Found an empty slot + */ + global->open_global_id = id; + return NT_STATUS_OK; + } + + TALLOC_FREE(tmp_global0); + + if (NT_STATUS_EQUAL(status, NT_STATUS_FATAL_APP_EXIT)) { + /* + * smbd crashed + */ + status = dbwrap_record_delete(global->db_rec); + if (!NT_STATUS_IS_OK(status)) { + DBG_WARNING("dbwrap_record_delete() failed " + "for record %"PRIu32": %s\n", + id, + nt_errstr(status)); + return NT_STATUS_INTERNAL_DB_CORRUPTION; + } + + if ((i < min_tries) && (last_free == 0)) { + /* + * Remember "id" as free but also try + * others to not recycle ids too + * quickly. + */ + last_free = id; + } + } + + if (!NT_STATUS_IS_OK(status)) { + DBG_WARNING("smbXsrv_open_global_verify_record() " + "failed for %s: %s, ignoring\n", + tdb_data_dbg(key), + nt_errstr(status)); + } + + TALLOC_FREE(global->db_rec); + } + + /* should not be reached */ + return NT_STATUS_INTERNAL_ERROR; +} + static NTSTATUS smbXsrv_open_global_lookup(struct smbXsrv_open_table *table, uint32_t open_global_id, TALLOC_CTX *mem_ctx,