LCOV - code coverage report
Current view: top level - source3/smbd - smbXsrv_open.c (source / functions) Hit Total Coverage
Test: coverage report for master 98b443d9 Lines: 449 667 67.3 %
Date: 2024-05-31 13:13:24 Functions: 27 29 93.1 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Stefan Metzmacher 2012
       5             :    Copyright (C) Michael Adam 2012
       6             : 
       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.
      11             : 
      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.
      16             : 
      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/>.
      19             : */
      20             : 
      21             : #include "smbXsrv_open.h"
      22             : #include "includes.h"
      23             : #include "system/filesys.h"
      24             : #include "lib/util/server_id.h"
      25             : #include "smbd/smbd.h"
      26             : #include "smbd/globals.h"
      27             : #include "dbwrap/dbwrap.h"
      28             : #include "dbwrap/dbwrap_rbt.h"
      29             : #include "dbwrap/dbwrap_open.h"
      30             : #include "../libcli/security/security.h"
      31             : #include "messages.h"
      32             : #include "lib/util/util_tdb.h"
      33             : #include "librpc/gen_ndr/ndr_smbXsrv.h"
      34             : #include "serverid.h"
      35             : #include "source3/include/util_tdb.h"
      36             : #include "lib/util/idtree_random.h"
      37             : #include "lib/util/time_basic.h"
      38             : 
      39             : struct smbXsrv_open_table {
      40             :         struct {
      41             :                 struct idr_context *idr;
      42             :                 struct db_context *replay_cache_db_ctx;
      43             :                 uint32_t lowest_id;
      44             :                 uint32_t highest_id;
      45             :                 uint32_t max_opens;
      46             :                 uint32_t num_opens;
      47             :         } local;
      48             :         struct {
      49             :                 struct db_context *db_ctx;
      50             :         } global;
      51             : };
      52             : 
      53             : static struct db_context *smbXsrv_open_global_db_ctx = NULL;
      54             : 
      55       31217 : NTSTATUS smbXsrv_open_global_init(void)
      56             : {
      57       31217 :         char *global_path = NULL;
      58       31217 :         struct db_context *db_ctx = NULL;
      59             : 
      60       31217 :         if (smbXsrv_open_global_db_ctx != NULL) {
      61       31217 :                 return NT_STATUS_OK;
      62             :         }
      63             : 
      64           0 :         global_path = lock_path(talloc_tos(), "smbXsrv_open_global.tdb");
      65           0 :         if (global_path == NULL) {
      66           0 :                 return NT_STATUS_NO_MEMORY;
      67             :         }
      68             : 
      69           0 :         db_ctx = db_open(NULL, global_path,
      70             :                          SMBD_VOLATILE_TDB_HASH_SIZE,
      71             :                          SMBD_VOLATILE_TDB_FLAGS,
      72             :                          O_RDWR | O_CREAT, 0600,
      73             :                          DBWRAP_LOCK_ORDER_1,
      74             :                          DBWRAP_FLAG_NONE);
      75           0 :         TALLOC_FREE(global_path);
      76           0 :         if (db_ctx == NULL) {
      77           0 :                 NTSTATUS status = map_nt_error_from_unix_common(errno);
      78           0 :                 return status;
      79             :         }
      80             : 
      81           0 :         smbXsrv_open_global_db_ctx = db_ctx;
      82             : 
      83           0 :         return NT_STATUS_OK;
      84             : }
      85             : 
      86             : /*
      87             :  * NOTE:
      88             :  * We need to store the keys in big endian so that dbwrap_rbt's memcmp
      89             :  * has the same result as integer comparison between the uint32_t
      90             :  * values.
      91             :  *
      92             :  * TODO: implement string based key
      93             :  */
      94             : 
      95             : struct smbXsrv_open_global_key_buf { uint8_t buf[sizeof(uint32_t)]; };
      96             : 
      97     1136570 : static TDB_DATA smbXsrv_open_global_id_to_key(
      98             :         uint32_t id, struct smbXsrv_open_global_key_buf *key_buf)
      99             : {
     100     1136570 :         RSIVAL(key_buf->buf, 0, id);
     101             : 
     102     2268908 :         return (TDB_DATA) {
     103     1132338 :                 .dptr = key_buf->buf,
     104             :                 .dsize = sizeof(key_buf->buf),
     105             :         };
     106             : }
     107             : 
     108       31217 : static NTSTATUS smbXsrv_open_table_init(struct smbXsrv_connection *conn,
     109             :                                         uint32_t lowest_id,
     110             :                                         uint32_t highest_id,
     111             :                                         uint32_t max_opens)
     112             : {
     113       31217 :         struct smbXsrv_client *client = conn->client;
     114         854 :         struct smbXsrv_open_table *table;
     115         854 :         NTSTATUS status;
     116         854 :         uint64_t max_range;
     117             : 
     118       31217 :         if (lowest_id > highest_id) {
     119           0 :                 return NT_STATUS_INTERNAL_ERROR;
     120             :         }
     121             : 
     122       31217 :         max_range = highest_id;
     123       31217 :         max_range -= lowest_id;
     124       31217 :         max_range += 1;
     125             : 
     126       31217 :         if (max_opens > max_range) {
     127           0 :                 return NT_STATUS_INTERNAL_ERROR;
     128             :         }
     129             : 
     130       31217 :         table = talloc_zero(client, struct smbXsrv_open_table);
     131       31217 :         if (table == NULL) {
     132           0 :                 return NT_STATUS_NO_MEMORY;
     133             :         }
     134             : 
     135       31217 :         table->local.idr = idr_init(table);
     136       31217 :         if (table->local.idr == NULL) {
     137           0 :                 TALLOC_FREE(table);
     138           0 :                 return NT_STATUS_NO_MEMORY;
     139             :         }
     140       31217 :         table->local.replay_cache_db_ctx = db_open_rbt(table);
     141       31217 :         if (table->local.replay_cache_db_ctx == NULL) {
     142           0 :                 TALLOC_FREE(table);
     143           0 :                 return NT_STATUS_NO_MEMORY;
     144             :         }
     145       31217 :         table->local.lowest_id = lowest_id;
     146       31217 :         table->local.highest_id = highest_id;
     147       31217 :         table->local.max_opens = max_opens;
     148             : 
     149       31217 :         status = smbXsrv_open_global_init();
     150       31217 :         if (!NT_STATUS_IS_OK(status)) {
     151           0 :                 TALLOC_FREE(table);
     152           0 :                 return status;
     153             :         }
     154             : 
     155       31217 :         table->global.db_ctx = smbXsrv_open_global_db_ctx;
     156             : 
     157       31217 :         client->open_table = table;
     158       31217 :         return NT_STATUS_OK;
     159             : }
     160             : 
     161     1040902 : static NTSTATUS smbXsrv_open_local_lookup(struct smbXsrv_open_table *table,
     162             :                                           uint32_t open_local_id,
     163             :                                           uint32_t open_global_id,
     164             :                                           NTTIME now,
     165             :                                           struct smbXsrv_open **_open)
     166             : {
     167     1040902 :         struct smbXsrv_open *op = NULL;
     168             : 
     169     1040902 :         *_open = NULL;
     170             : 
     171     1040902 :         if (open_local_id == 0) {
     172          17 :                 return NT_STATUS_FILE_CLOSED;
     173             :         }
     174             : 
     175     1040885 :         if (table == NULL) {
     176             :                 /* this might happen before the end of negprot */
     177           0 :                 return NT_STATUS_FILE_CLOSED;
     178             :         }
     179             : 
     180     1040885 :         if (table->local.idr == NULL) {
     181           0 :                 return NT_STATUS_INTERNAL_ERROR;
     182             :         }
     183             : 
     184     1040885 :         op = idr_find(table->local.idr, open_local_id);
     185     1040885 :         if (op == NULL) {
     186        3467 :                 return NT_STATUS_FILE_CLOSED;
     187             :         }
     188             : 
     189     1037418 :         if (open_global_id == 0) {
     190             :                 /* make the global check a no-op for SMB1 */
     191      209211 :                 open_global_id = op->global->open_global_id;
     192             :         }
     193             : 
     194     1037418 :         if (op->global->open_global_id != open_global_id) {
     195           4 :                 return NT_STATUS_FILE_CLOSED;
     196             :         }
     197             : 
     198     1037414 :         if (now != 0) {
     199     1037414 :                 op->idle_time = now;
     200             :         }
     201             : 
     202     1037414 :         *_open = op;
     203     1037414 :         return NT_STATUS_OK;
     204             : }
     205             : 
     206         226 : static NTSTATUS smbXsrv_open_global_parse_record(
     207             :         TALLOC_CTX *mem_ctx,
     208             :         TDB_DATA key,
     209             :         TDB_DATA val,
     210             :         struct smbXsrv_open_global0 **global)
     211             : {
     212         226 :         DATA_BLOB blob = data_blob_const(val.dptr, val.dsize);
     213           0 :         struct smbXsrv_open_globalB global_blob;
     214           0 :         enum ndr_err_code ndr_err;
     215           0 :         NTSTATUS status;
     216         226 :         TALLOC_CTX *frame = talloc_stackframe();
     217             : 
     218         226 :         ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
     219             :                         (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
     220         226 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     221           0 :                 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
     222             :                          "key '%s' ndr_pull_struct_blob - %s\n",
     223             :                          tdb_data_dbg(key),
     224             :                          ndr_errstr(ndr_err)));
     225           0 :                 status = ndr_map_error2ntstatus(ndr_err);
     226           0 :                 goto done;
     227             :         }
     228             : 
     229         226 :         DBG_DEBUG("\n");
     230         226 :         if (CHECK_DEBUGLVL(10)) {
     231           0 :                 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
     232             :         }
     233             : 
     234         226 :         if (global_blob.version != SMBXSRV_VERSION_0) {
     235           0 :                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     236           0 :                 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
     237             :                          "key '%s' unsupported version - %d - %s\n",
     238             :                          tdb_data_dbg(key),
     239             :                          (int)global_blob.version,
     240             :                          nt_errstr(status)));
     241           0 :                 goto done;
     242             :         }
     243             : 
     244         226 :         if (global_blob.info.info0 == NULL) {
     245           0 :                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     246           0 :                 DEBUG(1,("Invalid record in smbXsrv_open_global.tdb:"
     247             :                          "key '%s' info0 NULL pointer - %s\n",
     248             :                          tdb_data_dbg(key),
     249             :                          nt_errstr(status)));
     250           0 :                 goto done;
     251             :         }
     252             : 
     253         226 :         *global = talloc_move(mem_ctx, &global_blob.info.info0);
     254         226 :         status = NT_STATUS_OK;
     255         226 : done:
     256         226 :         talloc_free(frame);
     257         226 :         return status;
     258             : }
     259             : 
     260      567565 : static NTSTATUS smbXsrv_open_global_verify_record(
     261             :         TDB_DATA key,
     262             :         TDB_DATA val,
     263             :         TALLOC_CTX *mem_ctx,
     264             :         struct smbXsrv_open_global0 **_global0)
     265             : {
     266      567565 :         struct smbXsrv_open_global0 *global0 = NULL;
     267        2116 :         struct server_id_buf buf;
     268        2116 :         NTSTATUS status;
     269             : 
     270      567565 :         if (val.dsize == 0) {
     271      567371 :                 return NT_STATUS_NOT_FOUND;
     272             :         }
     273             : 
     274         194 :         status = smbXsrv_open_global_parse_record(mem_ctx, key, val, &global0);
     275         194 :         if (!NT_STATUS_IS_OK(status)) {
     276           0 :                 DBG_WARNING("smbXsrv_open_global_parse_record for %s failed: "
     277             :                             "%s\n",
     278             :                             tdb_data_dbg(key),
     279             :                             nt_errstr(status));
     280           0 :                 return status;
     281             :         }
     282         194 :         *_global0 = global0;
     283             : 
     284         194 :         if (server_id_is_disconnected(&global0->server_id)) {
     285         181 :                 return NT_STATUS_OK;
     286             :         }
     287          13 :         if (serverid_exists(&global0->server_id)) {
     288          13 :                 return NT_STATUS_OK;
     289             :         }
     290             : 
     291           0 :         DBG_WARNING("smbd %s did not clean up record %s\n",
     292             :                     server_id_str_buf(global0->server_id, &buf),
     293             :                     tdb_data_dbg(key));
     294             : 
     295           0 :         return NT_STATUS_FATAL_APP_EXIT;
     296             : }
     297             : 
     298      568893 : static NTSTATUS smbXsrv_open_global_store(
     299             :         struct db_record *rec,
     300             :         TDB_DATA key,
     301             :         TDB_DATA oldval,
     302             :         struct smbXsrv_open_global0 *global)
     303             : {
     304        2116 :         struct smbXsrv_open_globalB global_blob;
     305      568893 :         DATA_BLOB blob = data_blob_null;
     306      568893 :         TDB_DATA val = { .dptr = NULL, };
     307        2116 :         NTSTATUS status;
     308        2116 :         enum ndr_err_code ndr_err;
     309             : 
     310             :         /*
     311             :          * TODO: if we use other versions than '0'
     312             :          * we would add glue code here, that would be able to
     313             :          * store the information in the old format.
     314             :          */
     315             : 
     316      571009 :         global_blob = (struct smbXsrv_open_globalB) {
     317      568893 :                 .version = smbXsrv_version_global_current(),
     318             :         };
     319             : 
     320      568893 :         if (oldval.dsize >= 8) {
     321        1528 :                 global_blob.seqnum = IVAL(oldval.dptr, 4);
     322             :         }
     323      568893 :         global_blob.seqnum += 1;
     324      568893 :         global_blob.info.info0 = global;
     325             : 
     326      568893 :         ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &global_blob,
     327             :                         (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
     328      568893 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     329           0 :                 DBG_WARNING("key '%s' ndr_push - %s\n",
     330             :                             tdb_data_dbg(key),
     331             :                             ndr_map_error2string(ndr_err));
     332           0 :                 return ndr_map_error2ntstatus(ndr_err);
     333             :         }
     334             : 
     335      568893 :         val = make_tdb_data(blob.data, blob.length);
     336      568893 :         status = dbwrap_record_store(rec, val, TDB_REPLACE);
     337      568893 :         TALLOC_FREE(blob.data);
     338      568893 :         if (!NT_STATUS_IS_OK(status)) {
     339           0 :                 DBG_WARNING("key '%s' store - %s\n",
     340             :                             tdb_data_dbg(key),
     341             :                             nt_errstr(status));
     342           0 :                 return status;
     343             :         }
     344             : 
     345      568893 :         if (CHECK_DEBUGLVL(10)) {
     346           0 :                 DBG_DEBUG("key '%s' stored\n", tdb_data_dbg(key));
     347           0 :                 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
     348             :         }
     349             : 
     350      568893 :         return NT_STATUS_OK;
     351             : }
     352             : 
     353             : struct smbXsrv_open_global_allocate_state {
     354             :         uint32_t id;
     355             :         struct smbXsrv_open_global0 *global;
     356             :         NTSTATUS status;
     357             : };
     358             : 
     359      567357 : static void smbXsrv_open_global_allocate_fn(
     360             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     361             : {
     362      567357 :         struct smbXsrv_open_global_allocate_state *state = private_data;
     363      567357 :         struct smbXsrv_open_global0 *global = state->global;
     364      567357 :         struct smbXsrv_open_global0 *tmp_global0 = NULL;
     365      567357 :         TDB_DATA key = dbwrap_record_get_key(rec);
     366             : 
     367      567357 :         state->status = smbXsrv_open_global_verify_record(
     368             :                 key, oldval, talloc_tos(), &tmp_global0);
     369             : 
     370      567357 :         if (NT_STATUS_IS_OK(state->status)) {
     371             :                 /*
     372             :                  * Found an existing record
     373             :                  */
     374           0 :                 TALLOC_FREE(tmp_global0);
     375           0 :                 state->status = NT_STATUS_RETRY;
     376           0 :                 return;
     377             :         }
     378             : 
     379      567357 :         if (NT_STATUS_EQUAL(state->status, NT_STATUS_NOT_FOUND)) {
     380             :                 /*
     381             :                  * Found an empty slot
     382             :                  */
     383      567357 :                 global->open_global_id = state->id;
     384      567357 :                 global->open_persistent_id = state->id;
     385             : 
     386      567357 :                 state->status = smbXsrv_open_global_store(
     387      567357 :                         rec, key, (TDB_DATA) { .dsize = 0, }, state->global);
     388      567357 :                 if (!NT_STATUS_IS_OK(state->status)) {
     389           0 :                         DBG_WARNING("smbXsrv_open_global_store() for "
     390             :                                     "id %"PRIu32" failed: %s\n",
     391             :                                     state->id,
     392             :                                     nt_errstr(state->status));
     393             :                 }
     394      567357 :                 return;
     395             :         }
     396             : 
     397           0 :         if (NT_STATUS_EQUAL(state->status, NT_STATUS_FATAL_APP_EXIT)) {
     398           0 :                 NTSTATUS status;
     399             : 
     400           0 :                 TALLOC_FREE(tmp_global0);
     401             : 
     402             :                 /*
     403             :                  * smbd crashed
     404             :                  */
     405           0 :                 status = dbwrap_record_delete(rec);
     406           0 :                 if (!NT_STATUS_IS_OK(status)) {
     407           0 :                         DBG_WARNING("dbwrap_record_delete() failed "
     408             :                                     "for record %"PRIu32": %s\n",
     409             :                                     state->id,
     410             :                                     nt_errstr(status));
     411           0 :                         state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     412           0 :                         return;
     413             :                 }
     414           0 :                 return;
     415             :         }
     416             : }
     417             : 
     418      567357 : static NTSTATUS smbXsrv_open_global_allocate(
     419             :         struct db_context *db, struct smbXsrv_open_global0 *global)
     420             : {
     421      567357 :         struct smbXsrv_open_global_allocate_state state = {
     422             :                 .global = global,
     423             :         };
     424        2116 :         uint32_t i;
     425      567357 :         uint32_t last_free = 0;
     426      567357 :         const uint32_t min_tries = 3;
     427             : 
     428             :         /*
     429             :          * Here we just randomly try the whole 32-bit space
     430             :          *
     431             :          * We use just 32-bit, because we want to reuse the
     432             :          * ID for SRVSVC.
     433             :          */
     434      567357 :         for (i = 0; i < UINT32_MAX; i++) {
     435        2116 :                 struct smbXsrv_open_global_key_buf key_buf;
     436        2116 :                 TDB_DATA key;
     437        2116 :                 NTSTATUS status;
     438             : 
     439      567357 :                 if (i >= min_tries && last_free != 0) {
     440           0 :                         state.id = last_free;
     441             :                 } else {
     442      567357 :                         generate_nonce_buffer(
     443             :                                 (uint8_t *)&state.id, sizeof(state.id));
     444      567357 :                         state.id = MAX(state.id, 1);
     445      567357 :                         state.id = MIN(state.id, UINT32_MAX-1);
     446             :                 }
     447             : 
     448      567357 :                 key = smbXsrv_open_global_id_to_key(state.id, &key_buf);
     449             : 
     450      567357 :                 status = dbwrap_do_locked(
     451             :                         db, key, smbXsrv_open_global_allocate_fn, &state);
     452             : 
     453      567357 :                 if (!NT_STATUS_IS_OK(status)) {
     454           0 :                         DBG_WARNING("dbwrap_do_locked() failed: %s\n",
     455             :                                     nt_errstr(status));
     456           0 :                         return NT_STATUS_INTERNAL_DB_ERROR;
     457             :                 }
     458             : 
     459      567357 :                 if (NT_STATUS_IS_OK(state.status)) {
     460             :                         /*
     461             :                          * Found an empty slot, done.
     462             :                          */
     463      567357 :                         DBG_DEBUG("Found slot %"PRIu32"\n", state.id);
     464      567357 :                         return NT_STATUS_OK;
     465             :                 }
     466             : 
     467           0 :                 if (NT_STATUS_EQUAL(state.status, NT_STATUS_FATAL_APP_EXIT)) {
     468             : 
     469           0 :                         if ((i < min_tries) && (last_free == 0)) {
     470             :                                 /*
     471             :                                  * Remember "id" as free but also try
     472             :                                  * others to not recycle ids too
     473             :                                  * quickly.
     474             :                                  */
     475           0 :                                 last_free = state.id;
     476             :                         }
     477           0 :                         continue;
     478             :                 }
     479             : 
     480           0 :                 if (NT_STATUS_EQUAL(state.status, NT_STATUS_RETRY)) {
     481             :                         /*
     482             :                          * Normal collision, try next
     483             :                          */
     484           0 :                         DBG_DEBUG("Found record for id %"PRIu32"\n",
     485             :                                   state.id);
     486           0 :                         continue;
     487             :                 }
     488             : 
     489           0 :                 DBG_WARNING("smbXsrv_open_global_allocate_fn() failed: %s\n",
     490             :                             nt_errstr(state.status));
     491           0 :                 return state.status;
     492             :         }
     493             : 
     494             :         /* should not be reached */
     495           0 :         return NT_STATUS_INTERNAL_ERROR;
     496             : }
     497             : 
     498      567499 : static int smbXsrv_open_destructor(struct smbXsrv_open *op)
     499             : {
     500        2116 :         NTSTATUS status;
     501             : 
     502      567499 :         status = smbXsrv_open_close(op, 0);
     503      567499 :         if (!NT_STATUS_IS_OK(status)) {
     504           0 :                 DEBUG(0, ("smbXsrv_open_destructor: "
     505             :                           "smbXsrv_open_close() failed - %s\n",
     506             :                           nt_errstr(status)));
     507             :         }
     508             : 
     509      567499 :         TALLOC_FREE(op->global);
     510             : 
     511      567499 :         return 0;
     512             : }
     513             : 
     514      567359 : NTSTATUS smbXsrv_open_create(struct smbXsrv_connection *conn,
     515             :                              struct auth_session_info *session_info,
     516             :                              NTTIME now,
     517             :                              struct smbXsrv_open **_open)
     518             : {
     519      567359 :         struct smbXsrv_open_table *table = conn->client->open_table;
     520      567359 :         struct smbXsrv_open *op = NULL;
     521      567359 :         struct smbXsrv_open_global0 *global = NULL;
     522        2116 :         NTSTATUS status;
     523      567359 :         struct dom_sid *current_sid = NULL;
     524      567359 :         struct security_token *current_token = NULL;
     525        2116 :         int local_id;
     526             : 
     527      567359 :         if (session_info == NULL) {
     528           0 :                 return NT_STATUS_INVALID_HANDLE;
     529             :         }
     530      567359 :         current_token = session_info->security_token;
     531             : 
     532      567359 :         if ((current_token == NULL) ||
     533      567359 :             (current_token->num_sids <= PRIMARY_USER_SID_INDEX)) {
     534           0 :                 return NT_STATUS_INVALID_HANDLE;
     535             :         }
     536      567359 :         current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
     537             : 
     538      567359 :         if (table->local.num_opens >= table->local.max_opens) {
     539           2 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
     540             :         }
     541             : 
     542      567357 :         op = talloc_zero(table, struct smbXsrv_open);
     543      567357 :         if (op == NULL) {
     544           0 :                 return NT_STATUS_NO_MEMORY;
     545             :         }
     546      567357 :         op->table = table;
     547      567357 :         op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
     548      567357 :         op->idle_time = now;
     549             : 
     550      567357 :         global = talloc_zero(op, struct smbXsrv_open_global0);
     551      567357 :         if (global == NULL) {
     552           0 :                 TALLOC_FREE(op);
     553           0 :                 return NT_STATUS_NO_MEMORY;
     554             :         }
     555      567357 :         op->global = global;
     556             : 
     557             :         /*
     558             :          * We mark every slot as invalid using 0xFF.
     559             :          * Valid values are masked with 0xF.
     560             :          */
     561      567357 :         memset(global->lock_sequence_array, 0xFF,
     562             :                sizeof(global->lock_sequence_array));
     563             : 
     564      569473 :         local_id = idr_get_new_random(
     565             :                 table->local.idr,
     566             :                 op,
     567      567357 :                 table->local.lowest_id,
     568      567357 :                 table->local.highest_id);
     569      567357 :         if (local_id == -1) {
     570           0 :                 TALLOC_FREE(op);
     571           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
     572             :         }
     573      567357 :         op->local_id = local_id;
     574             : 
     575      567357 :         global->open_volatile_id = op->local_id;
     576             : 
     577      567357 :         global->server_id = messaging_server_id(conn->client->msg_ctx);
     578      567357 :         global->open_time = now;
     579      567357 :         global->open_owner = *current_sid;
     580      567357 :         if (conn->protocol >= PROTOCOL_SMB2_10) {
     581      476060 :                 global->client_guid = conn->smb2.client.guid;
     582             :         }
     583             : 
     584      567357 :         status = smbXsrv_open_global_allocate(table->global.db_ctx,
     585             :                                               global);
     586      567357 :         if (!NT_STATUS_IS_OK(status)) {
     587           0 :                 int ret = idr_remove(table->local.idr, local_id);
     588           0 :                 SMB_ASSERT(ret == 0);
     589             : 
     590           0 :                 DBG_WARNING("smbXsrv_open_global_allocate() failed: %s\n",
     591             :                             nt_errstr(status));
     592           0 :                 TALLOC_FREE(op);
     593           0 :                 return status;
     594             :         }
     595             : 
     596      567357 :         table->local.num_opens += 1;
     597      567357 :         talloc_set_destructor(op, smbXsrv_open_destructor);
     598             : 
     599      567357 :         if (CHECK_DEBUGLVL(10)) {
     600           0 :                 struct smbXsrv_openB open_blob = {
     601             :                         .version = SMBXSRV_VERSION_0,
     602             :                         .info.info0 = op,
     603             :                 };
     604             : 
     605           0 :                 DEBUG(10,("smbXsrv_open_create: global_id (0x%08x) stored\n",
     606             :                          op->global->open_global_id));
     607           0 :                 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
     608             :         }
     609             : 
     610      567357 :         *_open = op;
     611      567357 :         return NT_STATUS_OK;
     612             : }
     613             : 
     614        1190 : static NTSTATUS smbXsrv_open_set_replay_cache(struct smbXsrv_open *op)
     615             : {
     616           0 :         struct GUID *create_guid;
     617           0 :         struct GUID_txt_buf buf;
     618           0 :         char *guid_string;
     619        1190 :         struct db_context *db = op->table->local.replay_cache_db_ctx;
     620        1190 :         struct smbXsrv_open_replay_cache rc = {
     621        1190 :                 .idle_time = op->idle_time,
     622        1190 :                 .local_id = op->local_id,
     623             :         };
     624        1190 :         uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE] = { 0 };
     625        1190 :         DATA_BLOB blob = { .data = data, .length = sizeof(data), };
     626           0 :         enum ndr_err_code ndr_err;
     627           0 :         NTSTATUS status;
     628           0 :         TDB_DATA val;
     629             : 
     630        1190 :         if (!(op->flags & SMBXSRV_OPEN_NEED_REPLAY_CACHE)) {
     631         452 :                 return NT_STATUS_OK;
     632             :         }
     633             : 
     634         738 :         if (op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE) {
     635           0 :                 return NT_STATUS_OK;
     636             :         }
     637             : 
     638         738 :         create_guid = &op->global->create_guid;
     639         738 :         guid_string = GUID_buf_string(create_guid, &buf);
     640             : 
     641         738 :         ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
     642             :                 (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
     643         738 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     644           0 :                 status = ndr_map_error2ntstatus(ndr_err);
     645           0 :                 return status;
     646             :         }
     647         738 :         val = make_tdb_data(blob.data, blob.length);
     648             : 
     649         738 :         status = dbwrap_store_bystring(db, guid_string, val, TDB_REPLACE);
     650             : 
     651         738 :         if (NT_STATUS_IS_OK(status)) {
     652         738 :                 op->flags |= SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
     653         738 :                 op->flags &= ~SMBXSRV_OPEN_NEED_REPLAY_CACHE;
     654             :         }
     655             : 
     656         738 :         return status;
     657             : }
     658             : 
     659          78 : NTSTATUS smbXsrv_open_purge_replay_cache(struct smbXsrv_client *client,
     660             :                                          const struct GUID *create_guid)
     661             : {
     662           0 :         struct GUID_txt_buf buf;
     663           0 :         NTSTATUS status;
     664             : 
     665          78 :         if (client->open_table == NULL) {
     666           0 :                 return NT_STATUS_OK;
     667             :         }
     668             : 
     669          78 :         status = dbwrap_purge_bystring(
     670          78 :                 client->open_table->local.replay_cache_db_ctx,
     671          78 :                 GUID_buf_string(create_guid, &buf));
     672          78 :         return status;
     673             : }
     674             : 
     675     1395862 : static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
     676             : {
     677       10208 :         struct GUID *create_guid;
     678       10208 :         struct GUID_txt_buf buf;
     679       10208 :         struct db_context *db;
     680       10208 :         NTSTATUS status;
     681             : 
     682     1395862 :         if (op->table == NULL) {
     683         160 :                 return NT_STATUS_OK;
     684             :         }
     685             : 
     686     1395702 :         db = op->table->local.replay_cache_db_ctx;
     687             : 
     688     1395702 :         if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
     689     1394964 :                 return NT_STATUS_OK;
     690             :         }
     691             : 
     692         738 :         create_guid = &op->global->create_guid;
     693         738 :         if (GUID_all_zero(create_guid)) {
     694           0 :                 return NT_STATUS_OK;
     695             :         }
     696             : 
     697         738 :         status = dbwrap_purge_bystring(db, GUID_buf_string(create_guid, &buf));
     698             : 
     699         738 :         if (NT_STATUS_IS_OK(status)) {
     700         738 :                 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
     701             :         }
     702             : 
     703         738 :         return status;
     704             : }
     705             : 
     706             : struct smbXsrv_open_update_state {
     707             :         struct smbXsrv_open_global0 *global;
     708             :         NTSTATUS status;
     709             : };
     710             : 
     711        1190 : static void smbXsrv_open_update_fn(
     712             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     713             : {
     714        1190 :         struct smbXsrv_open_update_state *state = private_data;
     715        1190 :         TDB_DATA key = dbwrap_record_get_key(rec);
     716             : 
     717        1190 :         state->status = smbXsrv_open_global_store(
     718             :                 rec, key, oldval, state->global);
     719        1190 : }
     720             : 
     721        1190 : NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
     722             : {
     723        1190 :         struct smbXsrv_open_update_state state = { .global = op->global, };
     724        1190 :         struct smbXsrv_open_table *table = op->table;
     725           0 :         struct smbXsrv_open_global_key_buf key_buf;
     726        1190 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
     727        1190 :                 op->global->open_global_id, &key_buf);
     728           0 :         NTSTATUS status;
     729             : 
     730        1190 :         status = dbwrap_do_locked(
     731             :                 table->global.db_ctx, key, smbXsrv_open_update_fn, &state);
     732        1190 :         if (!NT_STATUS_IS_OK(status)) {
     733           0 :                 DBG_WARNING("global_id (0x%08x) dbwrap_do_locked failed: %s\n",
     734             :                             op->global->open_global_id,
     735             :                             nt_errstr(status));
     736           0 :                 return NT_STATUS_INTERNAL_DB_ERROR;
     737             :         }
     738             : 
     739        1190 :         if (!NT_STATUS_IS_OK(state.status)) {
     740           0 :                 DBG_WARNING("global_id (0x%08x) smbXsrv_open_global_store "
     741             :                             "failed: %s\n",
     742             :                             op->global->open_global_id,
     743             :                             nt_errstr(state.status));
     744           0 :                 return state.status;
     745             :         }
     746             : 
     747        1190 :         status = smbXsrv_open_set_replay_cache(op);
     748        1190 :         if (!NT_STATUS_IS_OK(status)) {
     749           0 :                 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
     750             :                         nt_errstr(status));
     751           0 :                 return status;
     752             :         }
     753             : 
     754        1190 :         if (CHECK_DEBUGLVL(10)) {
     755           0 :                 struct smbXsrv_openB open_blob = {
     756             :                         .version = SMBXSRV_VERSION_0,
     757             :                         .info.info0 = op,
     758             :                 };
     759             : 
     760           0 :                 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
     761             :                           op->global->open_global_id));
     762           0 :                 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
     763             :         }
     764             : 
     765        1190 :         return NT_STATUS_OK;
     766             : }
     767             : 
     768             : struct smbXsrv_open_close_state {
     769             :         struct smbXsrv_open *op;
     770             :         NTSTATUS status;
     771             : };
     772             : 
     773      567499 : static void smbXsrv_open_close_fn(
     774             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     775             : {
     776      567499 :         struct smbXsrv_open_close_state *state = private_data;
     777      567499 :         struct smbXsrv_open_global0 *global = state->op->global;
     778      567499 :         TDB_DATA key = dbwrap_record_get_key(rec);
     779             : 
     780      567499 :         if (global->durable) {
     781             :                 /*
     782             :                  * Durable open -- we need to update the global part
     783             :                  * instead of deleting it
     784             :                  */
     785         188 :                 state->status = smbXsrv_open_global_store(
     786             :                         rec, key, oldval, global);
     787         188 :                 if (!NT_STATUS_IS_OK(state->status)) {
     788           0 :                         DBG_WARNING("failed to store global key '%s': %s\n",
     789             :                                     tdb_data_dbg(key),
     790             :                                     nt_errstr(state->status));
     791         188 :                         return;
     792             :                 }
     793             : 
     794         188 :                 if (CHECK_DEBUGLVL(10)) {
     795           0 :                         struct smbXsrv_openB open_blob = {
     796             :                                 .version = SMBXSRV_VERSION_0,
     797           0 :                                 .info.info0 = state->op,
     798             :                         };
     799             : 
     800           0 :                         DBG_DEBUG("(0x%08x) stored disconnect\n",
     801             :                                   global->open_global_id);
     802           0 :                         NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
     803             :                 }
     804         188 :                 return;
     805             :         }
     806             : 
     807      567311 :         state->status = dbwrap_record_delete(rec);
     808      567311 :         if (!NT_STATUS_IS_OK(state->status)) {
     809           0 :                 DBG_WARNING("failed to delete global key '%s': %s\n",
     810             :                             tdb_data_dbg(key),
     811             :                             nt_errstr(state->status));
     812             :         }
     813             : }
     814             : 
     815      567659 : NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
     816             : {
     817      567659 :         struct smbXsrv_open_close_state state = { .op = op, };
     818      567659 :         struct smbXsrv_open_global0 *global = op->global;
     819        2116 :         struct smbXsrv_open_table *table;
     820        2116 :         NTSTATUS status;
     821      567659 :         NTSTATUS error = NT_STATUS_OK;
     822        2116 :         struct smbXsrv_open_global_key_buf key_buf;
     823      567659 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
     824             :                 global->open_global_id, &key_buf);
     825        2116 :         int ret;
     826             : 
     827      567659 :         error = smbXsrv_open_clear_replay_cache(op);
     828      567659 :         if (!NT_STATUS_IS_OK(error)) {
     829           0 :                 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
     830             :                         nt_errstr(error));
     831             :         }
     832             : 
     833      567659 :         if (op->table == NULL) {
     834         160 :                 return error;
     835             :         }
     836             : 
     837      567499 :         table = op->table;
     838      567499 :         op->table = NULL;
     839             : 
     840      567499 :         op->status = NT_STATUS_FILE_CLOSED;
     841      567499 :         global->disconnect_time = now;
     842      567499 :         server_id_set_disconnected(&global->server_id);
     843             : 
     844      567499 :         status = dbwrap_do_locked(
     845             :                 table->global.db_ctx, key, smbXsrv_open_close_fn, &state);
     846      567499 :         if (!NT_STATUS_IS_OK(status)) {
     847           0 :                 DBG_WARNING("dbwrap_do_locked() for %s failed: %s\n",
     848             :                             tdb_data_dbg(key),
     849             :                             nt_errstr(status));
     850           0 :                 error = status;
     851      567499 :         } else if (!NT_STATUS_IS_OK(state.status)) {
     852           0 :                 DBG_WARNING("smbXsrv_open_close_fn() for %s failed: %s\n",
     853             :                             tdb_data_dbg(key),
     854             :                             nt_errstr(state.status));
     855           0 :                 error = state.status;
     856             :         }
     857             : 
     858      567499 :         ret = idr_remove(table->local.idr, op->local_id);
     859      567499 :         SMB_ASSERT(ret == 0);
     860             : 
     861      567499 :         table->local.num_opens -= 1;
     862             : 
     863      567499 :         if (op->compat) {
     864           0 :                 op->compat->op = NULL;
     865           0 :                 file_free(NULL, op->compat);
     866           0 :                 op->compat = NULL;
     867             :         }
     868             : 
     869      567499 :         return error;
     870             : }
     871             : 
     872        5779 : NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
     873             : {
     874         133 :         uint32_t max_opens;
     875             : 
     876             :         /*
     877             :          * Allow a range from 1..65534.
     878             :          *
     879             :          * With real_max_open_files possible ids,
     880             :          * truncated to the SMB1 limit of 16-bit.
     881             :          *
     882             :          * 0 and 0xFFFF are no valid ids.
     883             :          */
     884        5779 :         max_opens = conn->client->sconn->real_max_open_files;
     885        5779 :         max_opens = MIN(max_opens, UINT16_MAX - 1);
     886             : 
     887        5779 :         return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
     888             : }
     889             : 
     890      211884 : NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
     891             :                              uint16_t fnum, NTTIME now,
     892             :                              struct smbXsrv_open **_open)
     893             : {
     894      211884 :         struct smbXsrv_open_table *table = conn->client->open_table;
     895      211884 :         uint32_t local_id = fnum;
     896      211884 :         uint32_t global_id = 0;
     897             : 
     898      211884 :         return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
     899             : }
     900             : 
     901       25438 : NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
     902             : {
     903         721 :         uint32_t max_opens;
     904         721 :         uint32_t highest_id;
     905             : 
     906             :         /*
     907             :          * Allow a range from 1..4294967294.
     908             :          *
     909             :          * With real_max_open_files possible ids,
     910             :          * truncated to 16-bit (the same as SMB1 for now).
     911             :          *
     912             :          * 0 and 0xFFFFFFFF are no valid ids.
     913             :          *
     914             :          * The usage of conn->sconn->real_max_open_files
     915             :          * is the reason that we use one open table per
     916             :          * transport connection (as we still have a 1:1 mapping
     917             :          * between process and transport connection).
     918             :          */
     919       25438 :         max_opens = conn->client->sconn->real_max_open_files;
     920       25438 :         max_opens = MIN(max_opens, UINT16_MAX - 1);
     921             : 
     922             :         /*
     923             :          * idtree uses "int" for local IDs. Limit the maximum ID to
     924             :          * what "int" can hold.
     925             :          */
     926       25438 :         highest_id = UINT32_MAX-1;
     927       25438 :         highest_id = MIN(highest_id, INT_MAX);
     928             : 
     929       25438 :         return smbXsrv_open_table_init(conn, 1, highest_id, max_opens);
     930             : }
     931             : 
     932      842488 : NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
     933             :                              uint64_t persistent_id,
     934             :                              uint64_t volatile_id,
     935             :                              NTTIME now,
     936             :                              struct smbXsrv_open **_open)
     937             : {
     938      842488 :         struct smbXsrv_open_table *table = conn->client->open_table;
     939      842488 :         uint32_t local_id = volatile_id & UINT32_MAX;
     940      842488 :         uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
     941      842488 :         uint32_t global_id = persistent_id & UINT32_MAX;
     942      842488 :         uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
     943        8092 :         NTSTATUS status;
     944             : 
     945      842488 :         if (local_zeros != 0) {
     946       11491 :                 return NT_STATUS_FILE_CLOSED;
     947             :         }
     948             : 
     949      830997 :         if (global_zeros != 0) {
     950           0 :                 return NT_STATUS_FILE_CLOSED;
     951             :         }
     952             : 
     953      830997 :         if (global_id == 0) {
     954        2053 :                 return NT_STATUS_FILE_CLOSED;
     955             :         }
     956             : 
     957      828944 :         status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
     958             :                                            _open);
     959      828944 :         if (!NT_STATUS_IS_OK(status)) {
     960         741 :                 return status;
     961             :         }
     962             : 
     963             :         /*
     964             :          * Clear the replay cache for this create_guid if it exists:
     965             :          * This is based on the assumption that this lookup will be
     966             :          * triggered by a client request using the file-id for lookup.
     967             :          * Hence the client has proven that it has in fact seen the
     968             :          * reply to its initial create call. So subsequent create replays
     969             :          * should be treated as invalid. Hence the index for create_guid
     970             :          * lookup needs to be removed.
     971             :          */
     972      828203 :         status = smbXsrv_open_clear_replay_cache(*_open);
     973             : 
     974      828203 :         return status;
     975             : }
     976             : 
     977             : /*
     978             :  * This checks or marks the replay cache, we have the following
     979             :  * cases:
     980             :  *
     981             :  * 1. There is no record in the cache
     982             :  *    => we add the passes caller_req_guid as holder_req_guid
     983             :  *       together with local_id as 0.
     984             :  *    => We return STATUS_FWP_RESERVED in order to indicate
     985             :  *       that the caller holds the current reservation
     986             :  *
     987             :  * 2. There is a record in the cache and holder_req_guid
     988             :  *    is already the same as caller_req_guid and local_id is 0
     989             :  *    => We return STATUS_FWP_RESERVED in order to indicate
     990             :  *       that the caller holds the current reservation
     991             :  *
     992             :  * 3. There is a record in the cache with a holder_req_guid
     993             :  *    other than caller_req_guid (and local_id is 0):
     994             :  *    => We return NT_STATUS_FILE_NOT_AVAILABLE to indicate
     995             :  *       the original request is still pending
     996             :  *
     997             :  * 4. There is a record in the cache with a zero holder_req_guid
     998             :  *    and a valid local_id:
     999             :  *    => We lookup the existing open by local_id
    1000             :  *    => We return NT_STATUS_OK together with the smbXsrv_open
    1001             :  *
    1002             :  *
    1003             :  * With NT_STATUS_OK the caller can continue the replay processing.
    1004             :  *
    1005             :  * With STATUS_FWP_RESERVED the caller should continue the normal
    1006             :  * open processing:
    1007             :  * - On success:
    1008             :  *   - smbXsrv_open_update()/smbXsrv_open_set_replay_cache()
    1009             :  *     will convert the record to a zero holder_req_guid
    1010             :  *     with a valid local_id.
    1011             :  * - On failure:
    1012             :  *   - smbXsrv_open_purge_replay_cache() should cleanup
    1013             :  *     the reservation.
    1014             :  *
    1015             :  * All other values should be returned to the client,
    1016             :  * while NT_STATUS_FILE_NOT_AVAILABLE will trigger the
    1017             :  * retry loop on the client.
    1018             :  */
    1019        1116 : NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
    1020             :                                           struct GUID caller_req_guid,
    1021             :                                           struct GUID create_guid,
    1022             :                                           const char *name,
    1023             :                                           NTTIME now,
    1024             :                                           struct smbXsrv_open **_open)
    1025             : {
    1026        1116 :         TALLOC_CTX *frame = talloc_stackframe();
    1027           0 :         NTSTATUS status;
    1028        1116 :         struct smbXsrv_open_table *table = conn->client->open_table;
    1029        1116 :         struct db_context *db = table->local.replay_cache_db_ctx;
    1030           0 :         struct GUID_txt_buf tmp_guid_buf;
    1031           0 :         struct GUID_txt_buf _create_guid_buf;
    1032        1116 :         const char *create_guid_str = GUID_buf_string(&create_guid, &_create_guid_buf);
    1033        1116 :         TDB_DATA create_guid_key = string_term_tdb_data(create_guid_str);
    1034        1116 :         struct db_record *db_rec = NULL;
    1035        1116 :         struct smbXsrv_open *op = NULL;
    1036        1116 :         struct smbXsrv_open_replay_cache rc = {
    1037             :                 .holder_req_guid = caller_req_guid,
    1038             :                 .idle_time = now,
    1039             :                 .local_id = 0,
    1040             :         };
    1041           0 :         enum ndr_err_code ndr_err;
    1042        1116 :         DATA_BLOB blob = data_blob_null;
    1043           0 :         TDB_DATA val;
    1044             : 
    1045        1116 :         *_open = NULL;
    1046             : 
    1047        1116 :         db_rec = dbwrap_fetch_locked(db, frame, create_guid_key);
    1048        1116 :         if (db_rec == NULL) {
    1049           0 :                 TALLOC_FREE(frame);
    1050           0 :                 return NT_STATUS_INTERNAL_DB_ERROR;
    1051             :         }
    1052             : 
    1053        1116 :         val = dbwrap_record_get_value(db_rec);
    1054        1116 :         if (val.dsize == 0) {
    1055           0 :                 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE];
    1056             : 
    1057         816 :                 blob = data_blob_const(data, ARRAY_SIZE(data));
    1058         816 :                 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
    1059             :                         (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
    1060         816 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1061           0 :                         status = ndr_map_error2ntstatus(ndr_err);
    1062           0 :                         TALLOC_FREE(frame);
    1063           0 :                         return status;
    1064             :                 }
    1065             : 
    1066         816 :                 val = make_tdb_data(blob.data, blob.length);
    1067         816 :                 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
    1068         816 :                 if (!NT_STATUS_IS_OK(status)) {
    1069           0 :                         TALLOC_FREE(frame);
    1070           0 :                         return status;
    1071             :                 }
    1072             : 
    1073             :                 /*
    1074             :                  * We're the new holder
    1075             :                  */
    1076         816 :                 *_open = NULL;
    1077         816 :                 TALLOC_FREE(frame);
    1078         816 :                 return NT_STATUS_FWP_RESERVED;
    1079             :         }
    1080             : 
    1081         300 :         if (val.dsize != SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE) {
    1082           0 :                 TALLOC_FREE(frame);
    1083           0 :                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
    1084             :         }
    1085             : 
    1086         300 :         blob = data_blob_const(val.dptr, val.dsize);
    1087         300 :         ndr_err = ndr_pull_struct_blob_all_noalloc(&blob, &rc,
    1088             :                         (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_replay_cache);
    1089         300 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1090           0 :                 status = ndr_map_error2ntstatus(ndr_err);
    1091           0 :                 TALLOC_FREE(frame);
    1092           0 :                 return status;
    1093             :         }
    1094         300 :         if (rc.local_id != 0) {
    1095          74 :                 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
    1096             :                         /*
    1097             :                          * This should not happen
    1098             :                          */
    1099           0 :                         status = NT_STATUS_INTERNAL_ERROR;
    1100           0 :                         DBG_ERR("caller %s already holds local_id %u for create %s [%s] - %s\n",
    1101             :                                 GUID_buf_string(&caller_req_guid, &tmp_guid_buf),
    1102             :                                 (unsigned)rc.local_id,
    1103             :                                 create_guid_str,
    1104             :                                 name,
    1105             :                                 nt_errstr(status));
    1106             : 
    1107           0 :                         TALLOC_FREE(frame);
    1108           0 :                         return status;
    1109             :                 }
    1110             : 
    1111          74 :                 status = smbXsrv_open_local_lookup(table,
    1112             :                                                    rc.local_id,
    1113             :                                                    0, /* global_id */
    1114             :                                                    now,
    1115             :                                                    &op);
    1116          74 :                 if (!NT_STATUS_IS_OK(status)) {
    1117           0 :                         DBG_ERR("holder %s stale for local_id %u for create %s [%s] - %s\n",
    1118             :                                 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
    1119             :                                 (unsigned)rc.local_id,
    1120             :                                 create_guid_str,
    1121             :                                 name,
    1122             :                                 nt_errstr(status));
    1123             : 
    1124           0 :                         TALLOC_FREE(frame);
    1125           0 :                         return status;
    1126             :                 }
    1127             : 
    1128             :                 /*
    1129             :                  * We found an open the caller can reuse.
    1130             :                  */
    1131          74 :                 SMB_ASSERT(op != NULL);
    1132          74 :                 *_open = op;
    1133          74 :                 TALLOC_FREE(frame);
    1134          74 :                 return NT_STATUS_OK;
    1135             :         }
    1136             : 
    1137         226 :         if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
    1138             :                 /*
    1139             :                  * We're still the holder
    1140             :                  */
    1141          74 :                 *_open = NULL;
    1142          74 :                 TALLOC_FREE(frame);
    1143          74 :                 return NT_STATUS_FWP_RESERVED;
    1144             :         }
    1145             : 
    1146             :         /*
    1147             :          * The original request (or a former replay) is still
    1148             :          * pending, ask the client to retry by sending FILE_NOT_AVAILABLE.
    1149             :          */
    1150         152 :         status = NT_STATUS_FILE_NOT_AVAILABLE;
    1151         152 :         DBG_DEBUG("holder %s still pending for create %s [%s] - %s\n",
    1152             :                    GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
    1153             :                    create_guid_str,
    1154             :                    name,
    1155             :                    nt_errstr(status));
    1156         152 :         TALLOC_FREE(frame);
    1157         152 :         return status;
    1158             : }
    1159             : 
    1160             : struct smb2srv_open_recreate_state {
    1161             :         struct smbXsrv_open *op;
    1162             :         const struct GUID *create_guid;
    1163             :         struct security_token *current_token;
    1164             :         struct server_id me;
    1165             : 
    1166             :         NTSTATUS status;
    1167             : };
    1168             : 
    1169         208 : static void smb2srv_open_recreate_fn(
    1170             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
    1171             : {
    1172         208 :         struct smb2srv_open_recreate_state *state = private_data;
    1173         208 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1174         208 :         struct smbXsrv_open_global0 *global = NULL;
    1175             : 
    1176         208 :         state->status = smbXsrv_open_global_verify_record(
    1177         208 :                 key, oldval, state->op, &state->op->global);
    1178         208 :         if (!NT_STATUS_IS_OK(state->status)) {
    1179          14 :                 DBG_WARNING("smbXsrv_open_global_verify_record for %s "
    1180             :                             "failed: %s\n",
    1181             :                             tdb_data_dbg(key),
    1182             :                             nt_errstr(state->status));
    1183          14 :                 goto not_found;
    1184             :         }
    1185         194 :         global = state->op->global;
    1186             : 
    1187             :         /*
    1188             :          * If the provided create_guid is NULL, this means that
    1189             :          * the reconnect request was a v1 request. In that case
    1190             :          * we should skip the create GUID verification, since
    1191             :          * it is valid to v1-reconnect a v2-opened handle.
    1192             :          */
    1193         194 :         if ((state->create_guid != NULL) &&
    1194          90 :             !GUID_equal(&global->create_guid, state->create_guid)) {
    1195           0 :                 struct GUID_txt_buf buf1, buf2;
    1196          36 :                 DBG_NOTICE("%s != %s in %s\n",
    1197             :                            GUID_buf_string(&global->create_guid, &buf1),
    1198             :                            GUID_buf_string(state->create_guid, &buf2),
    1199             :                            tdb_data_dbg(key));
    1200          36 :                 goto not_found;
    1201             :         }
    1202             : 
    1203         158 :         if (!security_token_is_sid(
    1204         158 :                     state->current_token, &global->open_owner)) {
    1205           0 :                 struct dom_sid_buf buf;
    1206           0 :                 DBG_NOTICE("global owner %s not in our token in %s\n",
    1207             :                            dom_sid_str_buf(&global->open_owner, &buf),
    1208             :                            tdb_data_dbg(key));
    1209           0 :                 goto not_found;
    1210             :         }
    1211             : 
    1212         158 :         if (!global->durable) {
    1213           0 :                 DBG_NOTICE("%"PRIu64"/%"PRIu64" not durable in %s\n",
    1214             :                            global->open_persistent_id,
    1215             :                            global->open_volatile_id,
    1216             :                            tdb_data_dbg(key));
    1217           0 :                 goto not_found;
    1218             :         }
    1219             : 
    1220         158 :         global->open_volatile_id = state->op->local_id;
    1221         158 :         global->server_id = state->me;
    1222             : 
    1223         158 :         state->status = smbXsrv_open_global_store(rec, key, oldval, global);
    1224         158 :         if (!NT_STATUS_IS_OK(state->status)) {
    1225           0 :                 DBG_WARNING("smbXsrv_open_global_store for %s failed: %s\n",
    1226             :                             tdb_data_dbg(key),
    1227             :                             nt_errstr(state->status));
    1228         158 :                 return;
    1229             :         }
    1230         158 :         return;
    1231             : 
    1232          50 : not_found:
    1233          50 :         state->status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
    1234             : }
    1235             : 
    1236         208 : NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
    1237             :                                struct auth_session_info *session_info,
    1238             :                                uint64_t persistent_id,
    1239             :                                const struct GUID *create_guid,
    1240             :                                NTTIME now,
    1241             :                                struct smbXsrv_open **_open)
    1242             : {
    1243         208 :         struct smbXsrv_open_table *table = conn->client->open_table;
    1244         208 :         struct smb2srv_open_recreate_state state = {
    1245             :                 .create_guid = create_guid,
    1246         208 :                 .me = messaging_server_id(conn->client->msg_ctx),
    1247             :         };
    1248           0 :         struct smbXsrv_open_global_key_buf key_buf;
    1249         208 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
    1250             :                 persistent_id & UINT32_MAX, &key_buf);
    1251           0 :         int ret, local_id;
    1252           0 :         NTSTATUS status;
    1253             : 
    1254         208 :         if (session_info == NULL) {
    1255           0 :                 DEBUG(10, ("session_info=NULL\n"));
    1256           0 :                 return NT_STATUS_INVALID_HANDLE;
    1257             :         }
    1258         208 :         state.current_token = session_info->security_token;
    1259             : 
    1260         208 :         if (state.current_token == NULL) {
    1261           0 :                 DEBUG(10, ("current_token=NULL\n"));
    1262           0 :                 return NT_STATUS_INVALID_HANDLE;
    1263             :         }
    1264             : 
    1265         208 :         if ((persistent_id & 0xFFFFFFFF00000000LLU) != 0) {
    1266             :                 /*
    1267             :                  * We only use 32 bit for the persistent ID
    1268             :                  */
    1269           0 :                 DBG_DEBUG("persistent_id=%"PRIx64"\n", persistent_id);
    1270           0 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
    1271             :         }
    1272             : 
    1273         208 :         if (table->local.num_opens >= table->local.max_opens) {
    1274           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
    1275             :         }
    1276             : 
    1277         208 :         state.op = talloc_zero(table, struct smbXsrv_open);
    1278         208 :         if (state.op == NULL) {
    1279           0 :                 return NT_STATUS_NO_MEMORY;
    1280             :         }
    1281         208 :         state.op->table = table;
    1282             : 
    1283         208 :         local_id =  idr_get_new_random(
    1284             :                 table->local.idr,
    1285         208 :                 state.op,
    1286         208 :                 table->local.lowest_id,
    1287         208 :                 table->local.highest_id);
    1288         208 :         if (local_id == -1) {
    1289           0 :                 TALLOC_FREE(state.op);
    1290           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
    1291             :         }
    1292         208 :         state.op->local_id = local_id;
    1293         208 :         SMB_ASSERT(state.op->local_id == local_id); /* No coercion loss */
    1294             : 
    1295         208 :         table->local.num_opens += 1;
    1296             : 
    1297         208 :         state.op->idle_time = now;
    1298         208 :         state.op->status = NT_STATUS_FILE_CLOSED;
    1299             : 
    1300         208 :         status = dbwrap_do_locked(
    1301             :                 table->global.db_ctx, key, smb2srv_open_recreate_fn, &state);
    1302         208 :         if (!NT_STATUS_IS_OK(status)) {
    1303           0 :                 DBG_DEBUG("dbwrap_do_locked() for %s failed: %s\n",
    1304             :                           tdb_data_dbg(key),
    1305             :                           nt_errstr(status));
    1306           0 :                 goto fail;
    1307             :         }
    1308             : 
    1309         208 :         if (!NT_STATUS_IS_OK(state.status)) {
    1310          50 :                 status = state.status;
    1311          50 :                 DBG_DEBUG("smb2srv_open_recreate_fn for %s failed: %s\n",
    1312             :                           tdb_data_dbg(key),
    1313             :                           nt_errstr(status));
    1314          50 :                 goto fail;
    1315             :         }
    1316             : 
    1317         158 :         talloc_set_destructor(state.op, smbXsrv_open_destructor);
    1318             : 
    1319         158 :         if (CHECK_DEBUGLVL(10)) {
    1320           0 :                 struct smbXsrv_openB open_blob = {
    1321           0 :                         .info.info0 = state.op,
    1322             :                 };
    1323           0 :                 DBG_DEBUG("global_id (0x%08x) stored\n",
    1324             :                           state.op->global->open_global_id);
    1325           0 :                 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
    1326             :         }
    1327             : 
    1328         158 :         *_open = state.op;
    1329             : 
    1330         158 :         return NT_STATUS_OK;
    1331          50 : fail:
    1332          50 :         table->local.num_opens -= 1;
    1333             : 
    1334          50 :         ret = idr_remove(table->local.idr, state.op->local_id);
    1335          50 :         SMB_ASSERT(ret == 0);
    1336          50 :         TALLOC_FREE(state.op);
    1337          50 :         return status;
    1338             : }
    1339             : 
    1340             : struct smbXsrv_open_global_traverse_state {
    1341             :         int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *);
    1342             :         void *private_data;
    1343             : };
    1344             : 
    1345           0 : static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
    1346             : {
    1347           0 :         struct smbXsrv_open_global_traverse_state *state =
    1348             :                 (struct smbXsrv_open_global_traverse_state*)data;
    1349           0 :         struct smbXsrv_open_global0 *global = NULL;
    1350           0 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1351           0 :         TDB_DATA val = dbwrap_record_get_value(rec);
    1352           0 :         NTSTATUS status;
    1353           0 :         int ret = -1;
    1354             : 
    1355           0 :         status = smbXsrv_open_global_parse_record(
    1356             :                 talloc_tos(), key, val, &global);
    1357           0 :         if (!NT_STATUS_IS_OK(status)) {
    1358           0 :                 return -1;
    1359             :         }
    1360             : 
    1361           0 :         ret = state->fn(rec, global, state->private_data);
    1362           0 :         talloc_free(global);
    1363           0 :         return ret;
    1364             : }
    1365             : 
    1366           0 : NTSTATUS smbXsrv_open_global_traverse(
    1367             :         int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *),
    1368             :         void *private_data)
    1369             : {
    1370             : 
    1371           0 :         NTSTATUS status;
    1372           0 :         int count = 0;
    1373           0 :         struct smbXsrv_open_global_traverse_state state = {
    1374             :                 .fn = fn,
    1375             :                 .private_data = private_data,
    1376             :         };
    1377             : 
    1378           0 :         become_root();
    1379           0 :         status = smbXsrv_open_global_init();
    1380           0 :         if (!NT_STATUS_IS_OK(status)) {
    1381           0 :                 unbecome_root();
    1382           0 :                 DEBUG(0, ("Failed to initialize open_global: %s\n",
    1383             :                           nt_errstr(status)));
    1384           0 :                 return status;
    1385             :         }
    1386             : 
    1387           0 :         status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
    1388             :                                       smbXsrv_open_global_traverse_fn,
    1389             :                                       &state,
    1390             :                                       &count);
    1391           0 :         unbecome_root();
    1392             : 
    1393           0 :         return status;
    1394             : }
    1395             : 
    1396             : struct smbXsrv_open_cleanup_state {
    1397             :         uint32_t global_id;
    1398             :         NTSTATUS status;
    1399             : };
    1400             : 
    1401         156 : static void smbXsrv_open_cleanup_fn(
    1402             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
    1403             : {
    1404         156 :         struct smbXsrv_open_cleanup_state *state = private_data;
    1405         156 :         struct smbXsrv_open_global0 *global = NULL;
    1406         156 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1407         156 :         bool delete_open = false;
    1408             : 
    1409         156 :         if (oldval.dsize == 0) {
    1410         124 :                 DBG_DEBUG("[global: 0x%08x] "
    1411             :                           "empty record in %s, skipping...\n",
    1412             :                           state->global_id,
    1413             :                           dbwrap_name(dbwrap_record_get_db(rec)));
    1414         124 :                 state->status = NT_STATUS_OK;
    1415         124 :                 return;
    1416             :         }
    1417             : 
    1418          32 :         state->status = smbXsrv_open_global_parse_record(
    1419             :                 talloc_tos(), key, oldval, &global);
    1420          32 :         if (!NT_STATUS_IS_OK(state->status)) {
    1421           0 :                 DBG_WARNING("[global: %x08x] "
    1422             :                             "smbXsrv_open_global_parse_record() in %s "
    1423             :                             "failed: %s, deleting record\n",
    1424             :                             state->global_id,
    1425             :                             dbwrap_name(dbwrap_record_get_db(rec)),
    1426             :                             nt_errstr(state->status));
    1427           0 :                 delete_open = true;
    1428           0 :                 goto do_delete;
    1429             :         }
    1430             : 
    1431          32 :         if (server_id_is_disconnected(&global->server_id)) {
    1432          32 :                 struct timeval now = timeval_current();
    1433           0 :                 struct timeval disconnect_time;
    1434           0 :                 struct timeval_buf buf;
    1435           0 :                 int64_t tdiff;
    1436             : 
    1437          32 :                 nttime_to_timeval(&disconnect_time, global->disconnect_time);
    1438          32 :                 tdiff = usec_time_diff(&now, &disconnect_time);
    1439          32 :                 delete_open = (tdiff >= 1000*global->durable_timeout_msec);
    1440             : 
    1441          32 :                 DBG_DEBUG("[global: 0x%08x] "
    1442             :                           "disconnected at [%s] %"PRIi64"s ago with "
    1443             :                           "timeout of %"PRIu32"s -%s reached\n",
    1444             :                           state->global_id,
    1445             :                           timeval_str_buf(&disconnect_time,
    1446             :                                           false,
    1447             :                                           false,
    1448             :                                           &buf),
    1449             :                           tdiff/1000000,
    1450             :                           global->durable_timeout_msec / 1000,
    1451             :                           delete_open ? "" : " not");
    1452           0 :         } else if (!serverid_exists(&global->server_id)) {
    1453           0 :                 struct server_id_buf idbuf;
    1454           0 :                 DBG_DEBUG("[global: 0x%08x] "
    1455             :                           "server[%s] does not exist\n",
    1456             :                           state->global_id,
    1457             :                           server_id_str_buf(global->server_id, &idbuf));
    1458           0 :                 delete_open = true;
    1459             :         }
    1460             : 
    1461          32 :         if (!delete_open) {
    1462           4 :                 state->status = NT_STATUS_OK;
    1463           4 :                 return;
    1464             :         }
    1465          28 : do_delete:
    1466          28 :         state->status = dbwrap_record_delete(rec);
    1467          28 :         if (!NT_STATUS_IS_OK(state->status)) {
    1468           0 :                 DBG_WARNING("[global: 0x%08x] "
    1469             :                             "failed to delete record "
    1470             :                             "from %s: %s\n",
    1471             :                             state->global_id,
    1472             :                             dbwrap_name(dbwrap_record_get_db(rec)),
    1473             :                             nt_errstr(state->status));
    1474           0 :                 return;
    1475             :         }
    1476             : 
    1477          28 :         DBG_DEBUG("[global: 0x%08x] "
    1478             :                   "deleted record from %s\n",
    1479             :                   state->global_id,
    1480             :                   dbwrap_name(dbwrap_record_get_db(rec)));
    1481             : }
    1482             : 
    1483         156 : NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
    1484             : {
    1485         156 :         struct smbXsrv_open_cleanup_state state = {
    1486             :                 .global_id = persistent_id & UINT32_MAX,
    1487             :         };
    1488           0 :         struct smbXsrv_open_global_key_buf key_buf;
    1489         156 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
    1490             :                 state.global_id, &key_buf);
    1491           0 :         NTSTATUS status;
    1492             : 
    1493         156 :         status = dbwrap_do_locked(
    1494             :                 smbXsrv_open_global_db_ctx,
    1495             :                 key,
    1496             :                 smbXsrv_open_cleanup_fn,
    1497             :                 &state);
    1498         156 :         if (!NT_STATUS_IS_OK(status)) {
    1499           0 :                 DBG_DEBUG("[global: 0x%08x] dbwrap_do_locked failed: %s\n",
    1500             :                           state.global_id,
    1501             :                           nt_errstr(status));
    1502           0 :                 return status;
    1503             :         }
    1504             : 
    1505         156 :         return state.status;
    1506             : }

Generated by: LCOV version 1.14