LCOV - code coverage report
Current view: top level - source3/smbd - smbXsrv_open.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 452 673 67.2 %
Date: 2024-04-21 15:09:00 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       30863 : NTSTATUS smbXsrv_open_global_init(void)
      56             : {
      57       30863 :         char *global_path = NULL;
      58       30863 :         struct db_context *db_ctx = NULL;
      59             : 
      60       30863 :         if (smbXsrv_open_global_db_ctx != NULL) {
      61       30863 :                 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     1137704 : static TDB_DATA smbXsrv_open_global_id_to_key(
      98             :         uint32_t id, struct smbXsrv_open_global_key_buf *key_buf)
      99             : {
     100     1137704 :         RSIVAL(key_buf->buf, 0, id);
     101             : 
     102     2271176 :         return (TDB_DATA) {
     103     1133472 :                 .dptr = key_buf->buf,
     104             :                 .dsize = sizeof(key_buf->buf),
     105             :         };
     106             : }
     107             : 
     108       30863 : 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       30863 :         struct smbXsrv_client *client = conn->client;
     114         834 :         struct smbXsrv_open_table *table;
     115         834 :         NTSTATUS status;
     116         834 :         uint64_t max_range;
     117             : 
     118       30863 :         if (lowest_id > highest_id) {
     119           0 :                 return NT_STATUS_INTERNAL_ERROR;
     120             :         }
     121             : 
     122       30863 :         max_range = highest_id;
     123       30863 :         max_range -= lowest_id;
     124       30863 :         max_range += 1;
     125             : 
     126       30863 :         if (max_opens > max_range) {
     127           0 :                 return NT_STATUS_INTERNAL_ERROR;
     128             :         }
     129             : 
     130       30863 :         table = talloc_zero(client, struct smbXsrv_open_table);
     131       30863 :         if (table == NULL) {
     132           0 :                 return NT_STATUS_NO_MEMORY;
     133             :         }
     134             : 
     135       30863 :         table->local.idr = idr_init(table);
     136       30863 :         if (table->local.idr == NULL) {
     137           0 :                 TALLOC_FREE(table);
     138           0 :                 return NT_STATUS_NO_MEMORY;
     139             :         }
     140       30863 :         table->local.replay_cache_db_ctx = db_open_rbt(table);
     141       30863 :         if (table->local.replay_cache_db_ctx == NULL) {
     142           0 :                 TALLOC_FREE(table);
     143           0 :                 return NT_STATUS_NO_MEMORY;
     144             :         }
     145       30863 :         table->local.lowest_id = lowest_id;
     146       30863 :         table->local.highest_id = highest_id;
     147       30863 :         table->local.max_opens = max_opens;
     148             : 
     149       30863 :         status = smbXsrv_open_global_init();
     150       30863 :         if (!NT_STATUS_IS_OK(status)) {
     151           0 :                 TALLOC_FREE(table);
     152           0 :                 return status;
     153             :         }
     154             : 
     155       30863 :         table->global.db_ctx = smbXsrv_open_global_db_ctx;
     156             : 
     157       30863 :         client->open_table = table;
     158       30863 :         return NT_STATUS_OK;
     159             : }
     160             : 
     161     1039850 : 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     1039850 :         struct smbXsrv_open *op = NULL;
     168             : 
     169     1039850 :         *_open = NULL;
     170             : 
     171     1039850 :         if (open_local_id == 0) {
     172          17 :                 return NT_STATUS_FILE_CLOSED;
     173             :         }
     174             : 
     175     1039833 :         if (table == NULL) {
     176             :                 /* this might happen before the end of negprot */
     177           0 :                 return NT_STATUS_FILE_CLOSED;
     178             :         }
     179             : 
     180     1039833 :         if (table->local.idr == NULL) {
     181           0 :                 return NT_STATUS_INTERNAL_ERROR;
     182             :         }
     183             : 
     184     1039833 :         op = idr_find(table->local.idr, open_local_id);
     185     1039833 :         if (op == NULL) {
     186        3467 :                 return NT_STATUS_FILE_CLOSED;
     187             :         }
     188             : 
     189     1036366 :         if (open_global_id == 0) {
     190             :                 /* make the global check a no-op for SMB1 */
     191      209234 :                 open_global_id = op->global->open_global_id;
     192             :         }
     193             : 
     194     1036366 :         if (op->global->open_global_id != open_global_id) {
     195           4 :                 return NT_STATUS_FILE_CLOSED;
     196             :         }
     197             : 
     198     1036362 :         if (now != 0) {
     199     1036362 :                 op->idle_time = now;
     200             :         }
     201             : 
     202     1036362 :         *_open = op;
     203     1036362 :         return NT_STATUS_OK;
     204             : }
     205             : 
     206         207 : 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         207 :         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         207 :         TALLOC_CTX *frame = talloc_stackframe();
     217             : 
     218         207 :         ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
     219             :                         (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_globalB);
     220         207 :         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         207 :         DBG_DEBUG("\n");
     230         207 :         if (CHECK_DEBUGLVL(10)) {
     231           0 :                 NDR_PRINT_DEBUG(smbXsrv_open_globalB, &global_blob);
     232             :         }
     233             : 
     234         207 :         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         207 :         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         207 :         *global = talloc_move(mem_ctx, &global_blob.info.info0);
     254         207 :         status = NT_STATUS_OK;
     255         207 : done:
     256         207 :         talloc_free(frame);
     257         207 :         return status;
     258             : }
     259             : 
     260      568185 : 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      568185 :         struct smbXsrv_open_global0 *global0 = NULL;
     267        2116 :         struct server_id_buf buf;
     268        2116 :         NTSTATUS status;
     269             : 
     270      568185 :         if (val.dsize == 0) {
     271      567991 :                 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      569513 : 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      569513 :         DATA_BLOB blob = data_blob_null;
     306      569513 :         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      571629 :         global_blob = (struct smbXsrv_open_globalB) {
     317      569513 :                 .version = smbXsrv_version_global_current(),
     318             :         };
     319             : 
     320      569513 :         if (oldval.dsize >= 8) {
     321        1528 :                 global_blob.seqnum = IVAL(oldval.dptr, 4);
     322             :         }
     323      569513 :         global_blob.seqnum += 1;
     324      569513 :         global_blob.info.info0 = global;
     325             : 
     326      569513 :         ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &global_blob,
     327             :                         (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_globalB);
     328      569513 :         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      569513 :         val = make_tdb_data(blob.data, blob.length);
     336      569513 :         status = dbwrap_record_store(rec, val, TDB_REPLACE);
     337      569513 :         TALLOC_FREE(blob.data);
     338      569513 :         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      569513 :         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      569513 :         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      567977 : static void smbXsrv_open_global_allocate_fn(
     360             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     361             : {
     362      567977 :         struct smbXsrv_open_global_allocate_state *state = private_data;
     363      567977 :         struct smbXsrv_open_global0 *global = state->global;
     364      567977 :         struct smbXsrv_open_global0 *tmp_global0 = NULL;
     365      567977 :         TDB_DATA key = dbwrap_record_get_key(rec);
     366             : 
     367      567977 :         state->status = smbXsrv_open_global_verify_record(
     368             :                 key, oldval, talloc_tos(), &tmp_global0);
     369             : 
     370      567977 :         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      567977 :         if (NT_STATUS_EQUAL(state->status, NT_STATUS_NOT_FOUND)) {
     380             :                 /*
     381             :                  * Found an empty slot
     382             :                  */
     383      567977 :                 global->open_global_id = state->id;
     384      567977 :                 global->open_persistent_id = state->id;
     385             : 
     386      567977 :                 state->status = smbXsrv_open_global_store(
     387      567977 :                         rec, key, (TDB_DATA) { .dsize = 0, }, state->global);
     388      567977 :                 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      567977 :                 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      567977 : static NTSTATUS smbXsrv_open_global_allocate(
     419             :         struct db_context *db, struct smbXsrv_open_global0 *global)
     420             : {
     421      567977 :         struct smbXsrv_open_global_allocate_state state = {
     422             :                 .global = global,
     423             :         };
     424        2116 :         uint32_t i;
     425      567977 :         uint32_t last_free = 0;
     426      567977 :         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      567977 :         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      567977 :                 if (i >= min_tries && last_free != 0) {
     440           0 :                         state.id = last_free;
     441             :                 } else {
     442      567977 :                         generate_nonce_buffer(
     443             :                                 (uint8_t *)&state.id, sizeof(state.id));
     444      567977 :                         state.id = MAX(state.id, 1);
     445      567977 :                         state.id = MIN(state.id, UINT32_MAX-1);
     446             :                 }
     447             : 
     448      567977 :                 key = smbXsrv_open_global_id_to_key(state.id, &key_buf);
     449             : 
     450      567977 :                 status = dbwrap_do_locked(
     451             :                         db, key, smbXsrv_open_global_allocate_fn, &state);
     452             : 
     453      567977 :                 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      567977 :                 if (NT_STATUS_IS_OK(state.status)) {
     460             :                         /*
     461             :                          * Found an empty slot, done.
     462             :                          */
     463      567977 :                         DBG_DEBUG("Found slot %"PRIu32"\n", state.id);
     464      567977 :                         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      568119 : static int smbXsrv_open_destructor(struct smbXsrv_open *op)
     499             : {
     500        2116 :         NTSTATUS status;
     501             : 
     502      568119 :         status = smbXsrv_open_close(op, 0);
     503      568119 :         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      568119 :         TALLOC_FREE(op->global);
     510             : 
     511      568119 :         return 0;
     512             : }
     513             : 
     514      567979 : 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      567979 :         struct smbXsrv_open_table *table = conn->client->open_table;
     520      567979 :         struct smbXsrv_open *op = NULL;
     521      567979 :         struct smbXsrv_open_global0 *global = NULL;
     522        2116 :         NTSTATUS status;
     523      567979 :         struct dom_sid *current_sid = NULL;
     524      567979 :         struct security_token *current_token = NULL;
     525        2116 :         int local_id;
     526             : 
     527      567979 :         if (session_info == NULL) {
     528           0 :                 return NT_STATUS_INVALID_HANDLE;
     529             :         }
     530      567979 :         current_token = session_info->security_token;
     531             : 
     532      567979 :         if ((current_token == NULL) ||
     533      567979 :             (current_token->num_sids <= PRIMARY_USER_SID_INDEX)) {
     534           0 :                 return NT_STATUS_INVALID_HANDLE;
     535             :         }
     536      567979 :         current_sid = &current_token->sids[PRIMARY_USER_SID_INDEX];
     537             : 
     538      567979 :         if (table->local.num_opens >= table->local.max_opens) {
     539           2 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
     540             :         }
     541             : 
     542      567977 :         op = talloc_zero(table, struct smbXsrv_open);
     543      567977 :         if (op == NULL) {
     544           0 :                 return NT_STATUS_NO_MEMORY;
     545             :         }
     546      567977 :         op->table = table;
     547      567977 :         op->status = NT_STATUS_OK; /* TODO: start with INTERNAL_ERROR */
     548      567977 :         op->idle_time = now;
     549             : 
     550      567977 :         global = talloc_zero(op, struct smbXsrv_open_global0);
     551      567977 :         if (global == NULL) {
     552           0 :                 TALLOC_FREE(op);
     553           0 :                 return NT_STATUS_NO_MEMORY;
     554             :         }
     555      567977 :         op->global = global;
     556             : 
     557             :         /*
     558             :          * We mark every slot as invalid using 0xFF.
     559             :          * Valid values are masked with 0xF.
     560             :          */
     561      567977 :         memset(global->lock_sequence_array, 0xFF,
     562             :                sizeof(global->lock_sequence_array));
     563             : 
     564      570093 :         local_id = idr_get_new_random(
     565             :                 table->local.idr,
     566             :                 op,
     567      567977 :                 table->local.lowest_id,
     568      567977 :                 table->local.highest_id);
     569      567977 :         if (local_id == -1) {
     570           0 :                 TALLOC_FREE(op);
     571           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
     572             :         }
     573      567977 :         op->local_id = local_id;
     574             : 
     575      567977 :         global->open_volatile_id = op->local_id;
     576             : 
     577      567977 :         global->server_id = messaging_server_id(conn->client->msg_ctx);
     578      567977 :         global->open_time = now;
     579      567977 :         global->open_owner = *current_sid;
     580      567977 :         if (conn->protocol >= PROTOCOL_SMB2_10) {
     581      476721 :                 global->client_guid = conn->smb2.client.guid;
     582             :         }
     583             : 
     584      567977 :         status = smbXsrv_open_global_allocate(table->global.db_ctx,
     585             :                                               global);
     586      567977 :         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      567977 :         table->local.num_opens += 1;
     597      567977 :         talloc_set_destructor(op, smbXsrv_open_destructor);
     598             : 
     599      567977 :         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      567977 :         *_open = op;
     611      567977 :         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 :         char *guid_string;
     664           0 :         struct db_context *db;
     665             : 
     666          78 :         if (client->open_table == NULL) {
     667           0 :                 return NT_STATUS_OK;
     668             :         }
     669             : 
     670          78 :         db = client->open_table->local.replay_cache_db_ctx;
     671             : 
     672          78 :         guid_string = GUID_buf_string(create_guid, &buf);
     673          78 :         if (guid_string == NULL) {
     674           0 :                 return NT_STATUS_INVALID_PARAMETER;
     675             :         }
     676             : 
     677          78 :         return dbwrap_purge_bystring(db, guid_string);
     678             : }
     679             : 
     680     1395407 : static NTSTATUS smbXsrv_open_clear_replay_cache(struct smbXsrv_open *op)
     681             : {
     682       10073 :         struct GUID *create_guid;
     683       10073 :         struct GUID_txt_buf buf;
     684       10073 :         char *guid_string;
     685       10073 :         struct db_context *db;
     686       10073 :         NTSTATUS status;
     687             : 
     688     1395407 :         if (op->table == NULL) {
     689         160 :                 return NT_STATUS_OK;
     690             :         }
     691             : 
     692     1395247 :         db = op->table->local.replay_cache_db_ctx;
     693             : 
     694     1395247 :         if (!(op->flags & SMBXSRV_OPEN_HAVE_REPLAY_CACHE)) {
     695     1394509 :                 return NT_STATUS_OK;
     696             :         }
     697             : 
     698         738 :         create_guid = &op->global->create_guid;
     699         738 :         if (GUID_all_zero(create_guid)) {
     700           0 :                 return NT_STATUS_OK;
     701             :         }
     702             : 
     703         738 :         guid_string = GUID_buf_string(create_guid, &buf);
     704         738 :         if (guid_string == NULL) {
     705           0 :                 return NT_STATUS_INVALID_PARAMETER;
     706             :         }
     707             : 
     708         738 :         status = dbwrap_purge_bystring(db, guid_string);
     709             : 
     710         738 :         if (NT_STATUS_IS_OK(status)) {
     711         738 :                 op->flags &= ~SMBXSRV_OPEN_HAVE_REPLAY_CACHE;
     712             :         }
     713             : 
     714         738 :         return status;
     715             : }
     716             : 
     717             : struct smbXsrv_open_update_state {
     718             :         struct smbXsrv_open_global0 *global;
     719             :         NTSTATUS status;
     720             : };
     721             : 
     722        1190 : static void smbXsrv_open_update_fn(
     723             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     724             : {
     725        1190 :         struct smbXsrv_open_update_state *state = private_data;
     726        1190 :         TDB_DATA key = dbwrap_record_get_key(rec);
     727             : 
     728        1190 :         state->status = smbXsrv_open_global_store(
     729             :                 rec, key, oldval, state->global);
     730        1190 : }
     731             : 
     732        1190 : NTSTATUS smbXsrv_open_update(struct smbXsrv_open *op)
     733             : {
     734        1190 :         struct smbXsrv_open_update_state state = { .global = op->global, };
     735        1190 :         struct smbXsrv_open_table *table = op->table;
     736           0 :         struct smbXsrv_open_global_key_buf key_buf;
     737        1190 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
     738        1190 :                 op->global->open_global_id, &key_buf);
     739           0 :         NTSTATUS status;
     740             : 
     741        1190 :         status = dbwrap_do_locked(
     742             :                 table->global.db_ctx, key, smbXsrv_open_update_fn, &state);
     743        1190 :         if (!NT_STATUS_IS_OK(status)) {
     744           0 :                 DBG_WARNING("global_id (0x%08x) dbwrap_do_locked failed: %s\n",
     745             :                             op->global->open_global_id,
     746             :                             nt_errstr(status));
     747           0 :                 return NT_STATUS_INTERNAL_DB_ERROR;
     748             :         }
     749             : 
     750        1190 :         if (!NT_STATUS_IS_OK(state.status)) {
     751           0 :                 DBG_WARNING("global_id (0x%08x) smbXsrv_open_global_store "
     752             :                             "failed: %s\n",
     753             :                             op->global->open_global_id,
     754             :                             nt_errstr(state.status));
     755           0 :                 return state.status;
     756             :         }
     757             : 
     758        1190 :         status = smbXsrv_open_set_replay_cache(op);
     759        1190 :         if (!NT_STATUS_IS_OK(status)) {
     760           0 :                 DBG_ERR("smbXsrv_open_set_replay_cache failed: %s\n",
     761             :                         nt_errstr(status));
     762           0 :                 return status;
     763             :         }
     764             : 
     765        1190 :         if (CHECK_DEBUGLVL(10)) {
     766           0 :                 struct smbXsrv_openB open_blob = {
     767             :                         .version = SMBXSRV_VERSION_0,
     768             :                         .info.info0 = op,
     769             :                 };
     770             : 
     771           0 :                 DEBUG(10,("smbXsrv_open_update: global_id (0x%08x) stored\n",
     772             :                           op->global->open_global_id));
     773           0 :                 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
     774             :         }
     775             : 
     776        1190 :         return NT_STATUS_OK;
     777             : }
     778             : 
     779             : struct smbXsrv_open_close_state {
     780             :         struct smbXsrv_open *op;
     781             :         NTSTATUS status;
     782             : };
     783             : 
     784      568119 : static void smbXsrv_open_close_fn(
     785             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
     786             : {
     787      568119 :         struct smbXsrv_open_close_state *state = private_data;
     788      568119 :         struct smbXsrv_open_global0 *global = state->op->global;
     789      568119 :         TDB_DATA key = dbwrap_record_get_key(rec);
     790             : 
     791      568119 :         if (global->durable) {
     792             :                 /*
     793             :                  * Durable open -- we need to update the global part
     794             :                  * instead of deleting it
     795             :                  */
     796         188 :                 state->status = smbXsrv_open_global_store(
     797             :                         rec, key, oldval, global);
     798         188 :                 if (!NT_STATUS_IS_OK(state->status)) {
     799           0 :                         DBG_WARNING("failed to store global key '%s': %s\n",
     800             :                                     tdb_data_dbg(key),
     801             :                                     nt_errstr(state->status));
     802         188 :                         return;
     803             :                 }
     804             : 
     805         188 :                 if (CHECK_DEBUGLVL(10)) {
     806           0 :                         struct smbXsrv_openB open_blob = {
     807             :                                 .version = SMBXSRV_VERSION_0,
     808           0 :                                 .info.info0 = state->op,
     809             :                         };
     810             : 
     811           0 :                         DBG_DEBUG("(0x%08x) stored disconnect\n",
     812             :                                   global->open_global_id);
     813           0 :                         NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
     814             :                 }
     815         188 :                 return;
     816             :         }
     817             : 
     818      567931 :         state->status = dbwrap_record_delete(rec);
     819      567931 :         if (!NT_STATUS_IS_OK(state->status)) {
     820           0 :                 DBG_WARNING("failed to delete global key '%s': %s\n",
     821             :                             tdb_data_dbg(key),
     822             :                             nt_errstr(state->status));
     823             :         }
     824             : }
     825             : 
     826      568279 : NTSTATUS smbXsrv_open_close(struct smbXsrv_open *op, NTTIME now)
     827             : {
     828      568279 :         struct smbXsrv_open_close_state state = { .op = op, };
     829      568279 :         struct smbXsrv_open_global0 *global = op->global;
     830        2116 :         struct smbXsrv_open_table *table;
     831        2116 :         NTSTATUS status;
     832      568279 :         NTSTATUS error = NT_STATUS_OK;
     833        2116 :         struct smbXsrv_open_global_key_buf key_buf;
     834      568279 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
     835             :                 global->open_global_id, &key_buf);
     836        2116 :         int ret;
     837             : 
     838      568279 :         error = smbXsrv_open_clear_replay_cache(op);
     839      568279 :         if (!NT_STATUS_IS_OK(error)) {
     840           0 :                 DBG_ERR("smbXsrv_open_clear_replay_cache failed: %s\n",
     841             :                         nt_errstr(error));
     842             :         }
     843             : 
     844      568279 :         if (op->table == NULL) {
     845         160 :                 return error;
     846             :         }
     847             : 
     848      568119 :         table = op->table;
     849      568119 :         op->table = NULL;
     850             : 
     851      568119 :         op->status = NT_STATUS_FILE_CLOSED;
     852      568119 :         global->disconnect_time = now;
     853      568119 :         server_id_set_disconnected(&global->server_id);
     854             : 
     855      568119 :         status = dbwrap_do_locked(
     856             :                 table->global.db_ctx, key, smbXsrv_open_close_fn, &state);
     857      568119 :         if (!NT_STATUS_IS_OK(status)) {
     858           0 :                 DBG_WARNING("dbwrap_do_locked() for %s failed: %s\n",
     859             :                             tdb_data_dbg(key),
     860             :                             nt_errstr(status));
     861           0 :                 error = status;
     862      568119 :         } else if (!NT_STATUS_IS_OK(state.status)) {
     863           0 :                 DBG_WARNING("smbXsrv_open_close_fn() for %s failed: %s\n",
     864             :                             tdb_data_dbg(key),
     865             :                             nt_errstr(state.status));
     866           0 :                 error = state.status;
     867             :         }
     868             : 
     869      568119 :         ret = idr_remove(table->local.idr, op->local_id);
     870      568119 :         SMB_ASSERT(ret == 0);
     871             : 
     872      568119 :         table->local.num_opens -= 1;
     873             : 
     874      568119 :         if (op->compat) {
     875           0 :                 op->compat->op = NULL;
     876           0 :                 file_free(NULL, op->compat);
     877           0 :                 op->compat = NULL;
     878             :         }
     879             : 
     880      568119 :         return error;
     881             : }
     882             : 
     883        5777 : NTSTATUS smb1srv_open_table_init(struct smbXsrv_connection *conn)
     884             : {
     885         133 :         uint32_t max_opens;
     886             : 
     887             :         /*
     888             :          * Allow a range from 1..65534.
     889             :          *
     890             :          * With real_max_open_files possible ids,
     891             :          * truncated to the SMB1 limit of 16-bit.
     892             :          *
     893             :          * 0 and 0xFFFF are no valid ids.
     894             :          */
     895        5777 :         max_opens = conn->client->sconn->real_max_open_files;
     896        5777 :         max_opens = MIN(max_opens, UINT16_MAX - 1);
     897             : 
     898        5777 :         return smbXsrv_open_table_init(conn, 1, UINT16_MAX - 1, max_opens);
     899             : }
     900             : 
     901      211907 : NTSTATUS smb1srv_open_lookup(struct smbXsrv_connection *conn,
     902             :                              uint16_t fnum, NTTIME now,
     903             :                              struct smbXsrv_open **_open)
     904             : {
     905      211907 :         struct smbXsrv_open_table *table = conn->client->open_table;
     906      211907 :         uint32_t local_id = fnum;
     907      211907 :         uint32_t global_id = 0;
     908             : 
     909      211907 :         return smbXsrv_open_local_lookup(table, local_id, global_id, now, _open);
     910             : }
     911             : 
     912       25086 : NTSTATUS smb2srv_open_table_init(struct smbXsrv_connection *conn)
     913             : {
     914         701 :         uint32_t max_opens;
     915         701 :         uint32_t highest_id;
     916             : 
     917             :         /*
     918             :          * Allow a range from 1..4294967294.
     919             :          *
     920             :          * With real_max_open_files possible ids,
     921             :          * truncated to 16-bit (the same as SMB1 for now).
     922             :          *
     923             :          * 0 and 0xFFFFFFFF are no valid ids.
     924             :          *
     925             :          * The usage of conn->sconn->real_max_open_files
     926             :          * is the reason that we use one open table per
     927             :          * transport connection (as we still have a 1:1 mapping
     928             :          * between process and transport connection).
     929             :          */
     930       25086 :         max_opens = conn->client->sconn->real_max_open_files;
     931       25086 :         max_opens = MIN(max_opens, UINT16_MAX - 1);
     932             : 
     933             :         /*
     934             :          * idtree uses "int" for local IDs. Limit the maximum ID to
     935             :          * what "int" can hold.
     936             :          */
     937       25086 :         highest_id = UINT32_MAX-1;
     938       25086 :         highest_id = MIN(highest_id, INT_MAX);
     939             : 
     940       25086 :         return smbXsrv_open_table_init(conn, 1, highest_id, max_opens);
     941             : }
     942             : 
     943      841147 : NTSTATUS smb2srv_open_lookup(struct smbXsrv_connection *conn,
     944             :                              uint64_t persistent_id,
     945             :                              uint64_t volatile_id,
     946             :                              NTTIME now,
     947             :                              struct smbXsrv_open **_open)
     948             : {
     949      841147 :         struct smbXsrv_open_table *table = conn->client->open_table;
     950      841147 :         uint32_t local_id = volatile_id & UINT32_MAX;
     951      841147 :         uint64_t local_zeros = volatile_id & 0xFFFFFFFF00000000LLU;
     952      841147 :         uint32_t global_id = persistent_id & UINT32_MAX;
     953      841147 :         uint64_t global_zeros = persistent_id & 0xFFFFFFFF00000000LLU;
     954        7957 :         NTSTATUS status;
     955             : 
     956      841147 :         if (local_zeros != 0) {
     957       11225 :                 return NT_STATUS_FILE_CLOSED;
     958             :         }
     959             : 
     960      829922 :         if (global_zeros != 0) {
     961           0 :                 return NT_STATUS_FILE_CLOSED;
     962             :         }
     963             : 
     964      829922 :         if (global_id == 0) {
     965        2053 :                 return NT_STATUS_FILE_CLOSED;
     966             :         }
     967             : 
     968      827869 :         status = smbXsrv_open_local_lookup(table, local_id, global_id, now,
     969             :                                            _open);
     970      827869 :         if (!NT_STATUS_IS_OK(status)) {
     971         741 :                 return status;
     972             :         }
     973             : 
     974             :         /*
     975             :          * Clear the replay cache for this create_guid if it exists:
     976             :          * This is based on the assumption that this lookup will be
     977             :          * triggered by a client request using the file-id for lookup.
     978             :          * Hence the client has proven that it has in fact seen the
     979             :          * reply to its initial create call. So subsequent create replays
     980             :          * should be treated as invalid. Hence the index for create_guid
     981             :          * lookup needs to be removed.
     982             :          */
     983      827128 :         status = smbXsrv_open_clear_replay_cache(*_open);
     984             : 
     985      827128 :         return status;
     986             : }
     987             : 
     988             : /*
     989             :  * This checks or marks the replay cache, we have the following
     990             :  * cases:
     991             :  *
     992             :  * 1. There is no record in the cache
     993             :  *    => we add the passes caller_req_guid as holder_req_guid
     994             :  *       together with local_id as 0.
     995             :  *    => We return STATUS_FWP_RESERVED in order to indicate
     996             :  *       that the caller holds the current reservation
     997             :  *
     998             :  * 2. There is a record in the cache and holder_req_guid
     999             :  *    is already the same as caller_req_guid and local_id is 0
    1000             :  *    => We return STATUS_FWP_RESERVED in order to indicate
    1001             :  *       that the caller holds the current reservation
    1002             :  *
    1003             :  * 3. There is a record in the cache with a holder_req_guid
    1004             :  *    other than caller_req_guid (and local_id is 0):
    1005             :  *    => We return NT_STATUS_FILE_NOT_AVAILABLE to indicate
    1006             :  *       the original request is still pending
    1007             :  *
    1008             :  * 4. There is a record in the cache with a zero holder_req_guid
    1009             :  *    and a valid local_id:
    1010             :  *    => We lookup the existing open by local_id
    1011             :  *    => We return NT_STATUS_OK together with the smbXsrv_open
    1012             :  *
    1013             :  *
    1014             :  * With NT_STATUS_OK the caller can continue the replay processing.
    1015             :  *
    1016             :  * With STATUS_FWP_RESERVED the caller should continue the normal
    1017             :  * open processing:
    1018             :  * - On success:
    1019             :  *   - smbXsrv_open_update()/smbXsrv_open_set_replay_cache()
    1020             :  *     will convert the record to a zero holder_req_guid
    1021             :  *     with a valid local_id.
    1022             :  * - On failure:
    1023             :  *   - smbXsrv_open_purge_replay_cache() should cleanup
    1024             :  *     the reservation.
    1025             :  *
    1026             :  * All other values should be returned to the client,
    1027             :  * while NT_STATUS_FILE_NOT_AVAILABLE will trigger the
    1028             :  * retry loop on the client.
    1029             :  */
    1030        1116 : NTSTATUS smb2srv_open_lookup_replay_cache(struct smbXsrv_connection *conn,
    1031             :                                           struct GUID caller_req_guid,
    1032             :                                           struct GUID create_guid,
    1033             :                                           const char *name,
    1034             :                                           NTTIME now,
    1035             :                                           struct smbXsrv_open **_open)
    1036             : {
    1037        1116 :         TALLOC_CTX *frame = talloc_stackframe();
    1038           0 :         NTSTATUS status;
    1039        1116 :         struct smbXsrv_open_table *table = conn->client->open_table;
    1040        1116 :         struct db_context *db = table->local.replay_cache_db_ctx;
    1041           0 :         struct GUID_txt_buf tmp_guid_buf;
    1042           0 :         struct GUID_txt_buf _create_guid_buf;
    1043        1116 :         const char *create_guid_str = GUID_buf_string(&create_guid, &_create_guid_buf);
    1044        1116 :         TDB_DATA create_guid_key = string_term_tdb_data(create_guid_str);
    1045        1116 :         struct db_record *db_rec = NULL;
    1046        1116 :         struct smbXsrv_open *op = NULL;
    1047        1116 :         struct smbXsrv_open_replay_cache rc = {
    1048             :                 .holder_req_guid = caller_req_guid,
    1049             :                 .idle_time = now,
    1050             :                 .local_id = 0,
    1051             :         };
    1052           0 :         enum ndr_err_code ndr_err;
    1053        1116 :         DATA_BLOB blob = data_blob_null;
    1054           0 :         TDB_DATA val;
    1055             : 
    1056        1116 :         *_open = NULL;
    1057             : 
    1058        1116 :         db_rec = dbwrap_fetch_locked(db, frame, create_guid_key);
    1059        1116 :         if (db_rec == NULL) {
    1060           0 :                 TALLOC_FREE(frame);
    1061           0 :                 return NT_STATUS_INTERNAL_DB_ERROR;
    1062             :         }
    1063             : 
    1064        1116 :         val = dbwrap_record_get_value(db_rec);
    1065        1116 :         if (val.dsize == 0) {
    1066           0 :                 uint8_t data[SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE];
    1067             : 
    1068         816 :                 blob = data_blob_const(data, ARRAY_SIZE(data));
    1069         816 :                 ndr_err = ndr_push_struct_into_fixed_blob(&blob, &rc,
    1070             :                         (ndr_push_flags_fn_t)ndr_push_smbXsrv_open_replay_cache);
    1071         816 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1072           0 :                         status = ndr_map_error2ntstatus(ndr_err);
    1073           0 :                         TALLOC_FREE(frame);
    1074           0 :                         return status;
    1075             :                 }
    1076             : 
    1077         816 :                 val = make_tdb_data(blob.data, blob.length);
    1078         816 :                 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
    1079         816 :                 if (!NT_STATUS_IS_OK(status)) {
    1080           0 :                         TALLOC_FREE(frame);
    1081           0 :                         return status;
    1082             :                 }
    1083             : 
    1084             :                 /*
    1085             :                  * We're the new holder
    1086             :                  */
    1087         816 :                 *_open = NULL;
    1088         816 :                 TALLOC_FREE(frame);
    1089         816 :                 return NT_STATUS_FWP_RESERVED;
    1090             :         }
    1091             : 
    1092         300 :         if (val.dsize != SMBXSRV_OPEN_REPLAY_CACHE_FIXED_SIZE) {
    1093           0 :                 TALLOC_FREE(frame);
    1094           0 :                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
    1095             :         }
    1096             : 
    1097         300 :         blob = data_blob_const(val.dptr, val.dsize);
    1098         300 :         ndr_err = ndr_pull_struct_blob_all_noalloc(&blob, &rc,
    1099             :                         (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_open_replay_cache);
    1100         300 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1101           0 :                 status = ndr_map_error2ntstatus(ndr_err);
    1102           0 :                 TALLOC_FREE(frame);
    1103           0 :                 return status;
    1104             :         }
    1105         300 :         if (rc.local_id != 0) {
    1106          74 :                 if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
    1107             :                         /*
    1108             :                          * This should not happen
    1109             :                          */
    1110           0 :                         status = NT_STATUS_INTERNAL_ERROR;
    1111           0 :                         DBG_ERR("caller %s already holds local_id %u for create %s [%s] - %s\n",
    1112             :                                 GUID_buf_string(&caller_req_guid, &tmp_guid_buf),
    1113             :                                 (unsigned)rc.local_id,
    1114             :                                 create_guid_str,
    1115             :                                 name,
    1116             :                                 nt_errstr(status));
    1117             : 
    1118           0 :                         TALLOC_FREE(frame);
    1119           0 :                         return status;
    1120             :                 }
    1121             : 
    1122          74 :                 status = smbXsrv_open_local_lookup(table,
    1123             :                                                    rc.local_id,
    1124             :                                                    0, /* global_id */
    1125             :                                                    now,
    1126             :                                                    &op);
    1127          74 :                 if (!NT_STATUS_IS_OK(status)) {
    1128           0 :                         DBG_ERR("holder %s stale for local_id %u for create %s [%s] - %s\n",
    1129             :                                 GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
    1130             :                                 (unsigned)rc.local_id,
    1131             :                                 create_guid_str,
    1132             :                                 name,
    1133             :                                 nt_errstr(status));
    1134             : 
    1135           0 :                         TALLOC_FREE(frame);
    1136           0 :                         return status;
    1137             :                 }
    1138             : 
    1139             :                 /*
    1140             :                  * We found an open the caller can reuse.
    1141             :                  */
    1142          74 :                 SMB_ASSERT(op != NULL);
    1143          74 :                 *_open = op;
    1144          74 :                 TALLOC_FREE(frame);
    1145          74 :                 return NT_STATUS_OK;
    1146             :         }
    1147             : 
    1148         226 :         if (GUID_equal(&rc.holder_req_guid, &caller_req_guid)) {
    1149             :                 /*
    1150             :                  * We're still the holder
    1151             :                  */
    1152          74 :                 *_open = NULL;
    1153          74 :                 TALLOC_FREE(frame);
    1154          74 :                 return NT_STATUS_FWP_RESERVED;
    1155             :         }
    1156             : 
    1157             :         /*
    1158             :          * The original request (or a former replay) is still
    1159             :          * pending, ask the client to retry by sending FILE_NOT_AVAILABLE.
    1160             :          */
    1161         152 :         status = NT_STATUS_FILE_NOT_AVAILABLE;
    1162         152 :         DBG_DEBUG("holder %s still pending for create %s [%s] - %s\n",
    1163             :                    GUID_buf_string(&rc.holder_req_guid, &tmp_guid_buf),
    1164             :                    create_guid_str,
    1165             :                    name,
    1166             :                    nt_errstr(status));
    1167         152 :         TALLOC_FREE(frame);
    1168         152 :         return status;
    1169             : }
    1170             : 
    1171             : struct smb2srv_open_recreate_state {
    1172             :         struct smbXsrv_open *op;
    1173             :         const struct GUID *create_guid;
    1174             :         struct security_token *current_token;
    1175             :         struct server_id me;
    1176             : 
    1177             :         NTSTATUS status;
    1178             : };
    1179             : 
    1180         208 : static void smb2srv_open_recreate_fn(
    1181             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
    1182             : {
    1183         208 :         struct smb2srv_open_recreate_state *state = private_data;
    1184         208 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1185         208 :         struct smbXsrv_open_global0 *global = NULL;
    1186             : 
    1187         208 :         state->status = smbXsrv_open_global_verify_record(
    1188         208 :                 key, oldval, state->op, &state->op->global);
    1189         208 :         if (!NT_STATUS_IS_OK(state->status)) {
    1190          14 :                 DBG_WARNING("smbXsrv_open_global_verify_record for %s "
    1191             :                             "failed: %s\n",
    1192             :                             tdb_data_dbg(key),
    1193             :                             nt_errstr(state->status));
    1194          14 :                 goto not_found;
    1195             :         }
    1196         194 :         global = state->op->global;
    1197             : 
    1198             :         /*
    1199             :          * If the provided create_guid is NULL, this means that
    1200             :          * the reconnect request was a v1 request. In that case
    1201             :          * we should skip the create GUID verification, since
    1202             :          * it is valid to v1-reconnect a v2-opened handle.
    1203             :          */
    1204         194 :         if ((state->create_guid != NULL) &&
    1205          90 :             !GUID_equal(&global->create_guid, state->create_guid)) {
    1206           0 :                 struct GUID_txt_buf buf1, buf2;
    1207          36 :                 DBG_NOTICE("%s != %s in %s\n",
    1208             :                            GUID_buf_string(&global->create_guid, &buf1),
    1209             :                            GUID_buf_string(state->create_guid, &buf2),
    1210             :                            tdb_data_dbg(key));
    1211          36 :                 goto not_found;
    1212             :         }
    1213             : 
    1214         158 :         if (!security_token_is_sid(
    1215         158 :                     state->current_token, &global->open_owner)) {
    1216           0 :                 struct dom_sid_buf buf;
    1217           0 :                 DBG_NOTICE("global owner %s not in our token in %s\n",
    1218             :                            dom_sid_str_buf(&global->open_owner, &buf),
    1219             :                            tdb_data_dbg(key));
    1220           0 :                 goto not_found;
    1221             :         }
    1222             : 
    1223         158 :         if (!global->durable) {
    1224           0 :                 DBG_NOTICE("%"PRIu64"/%"PRIu64" not durable in %s\n",
    1225             :                            global->open_persistent_id,
    1226             :                            global->open_volatile_id,
    1227             :                            tdb_data_dbg(key));
    1228           0 :                 goto not_found;
    1229             :         }
    1230             : 
    1231         158 :         global->open_volatile_id = state->op->local_id;
    1232         158 :         global->server_id = state->me;
    1233             : 
    1234         158 :         state->status = smbXsrv_open_global_store(rec, key, oldval, global);
    1235         158 :         if (!NT_STATUS_IS_OK(state->status)) {
    1236           0 :                 DBG_WARNING("smbXsrv_open_global_store for %s failed: %s\n",
    1237             :                             tdb_data_dbg(key),
    1238             :                             nt_errstr(state->status));
    1239         158 :                 return;
    1240             :         }
    1241         158 :         return;
    1242             : 
    1243          50 : not_found:
    1244          50 :         state->status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
    1245             : }
    1246             : 
    1247         208 : NTSTATUS smb2srv_open_recreate(struct smbXsrv_connection *conn,
    1248             :                                struct auth_session_info *session_info,
    1249             :                                uint64_t persistent_id,
    1250             :                                const struct GUID *create_guid,
    1251             :                                NTTIME now,
    1252             :                                struct smbXsrv_open **_open)
    1253             : {
    1254         208 :         struct smbXsrv_open_table *table = conn->client->open_table;
    1255         208 :         struct smb2srv_open_recreate_state state = {
    1256             :                 .create_guid = create_guid,
    1257         208 :                 .me = messaging_server_id(conn->client->msg_ctx),
    1258             :         };
    1259           0 :         struct smbXsrv_open_global_key_buf key_buf;
    1260         208 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
    1261             :                 persistent_id & UINT32_MAX, &key_buf);
    1262           0 :         int ret, local_id;
    1263           0 :         NTSTATUS status;
    1264             : 
    1265         208 :         if (session_info == NULL) {
    1266           0 :                 DEBUG(10, ("session_info=NULL\n"));
    1267           0 :                 return NT_STATUS_INVALID_HANDLE;
    1268             :         }
    1269         208 :         state.current_token = session_info->security_token;
    1270             : 
    1271         208 :         if (state.current_token == NULL) {
    1272           0 :                 DEBUG(10, ("current_token=NULL\n"));
    1273           0 :                 return NT_STATUS_INVALID_HANDLE;
    1274             :         }
    1275             : 
    1276         208 :         if ((persistent_id & 0xFFFFFFFF00000000LLU) != 0) {
    1277             :                 /*
    1278             :                  * We only use 32 bit for the persistent ID
    1279             :                  */
    1280           0 :                 DBG_DEBUG("persistent_id=%"PRIx64"\n", persistent_id);
    1281           0 :                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
    1282             :         }
    1283             : 
    1284         208 :         if (table->local.num_opens >= table->local.max_opens) {
    1285           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
    1286             :         }
    1287             : 
    1288         208 :         state.op = talloc_zero(table, struct smbXsrv_open);
    1289         208 :         if (state.op == NULL) {
    1290           0 :                 return NT_STATUS_NO_MEMORY;
    1291             :         }
    1292         208 :         state.op->table = table;
    1293             : 
    1294         208 :         local_id =  idr_get_new_random(
    1295             :                 table->local.idr,
    1296         208 :                 state.op,
    1297         208 :                 table->local.lowest_id,
    1298         208 :                 table->local.highest_id);
    1299         208 :         if (local_id == -1) {
    1300           0 :                 TALLOC_FREE(state.op);
    1301           0 :                 return NT_STATUS_INSUFFICIENT_RESOURCES;
    1302             :         }
    1303         208 :         state.op->local_id = local_id;
    1304         208 :         SMB_ASSERT(state.op->local_id == local_id); /* No coercion loss */
    1305             : 
    1306         208 :         table->local.num_opens += 1;
    1307             : 
    1308         208 :         state.op->idle_time = now;
    1309         208 :         state.op->status = NT_STATUS_FILE_CLOSED;
    1310             : 
    1311         208 :         status = dbwrap_do_locked(
    1312             :                 table->global.db_ctx, key, smb2srv_open_recreate_fn, &state);
    1313         208 :         if (!NT_STATUS_IS_OK(status)) {
    1314           0 :                 DBG_DEBUG("dbwrap_do_locked() for %s failed: %s\n",
    1315             :                           tdb_data_dbg(key),
    1316             :                           nt_errstr(status));
    1317           0 :                 goto fail;
    1318             :         }
    1319             : 
    1320         208 :         if (!NT_STATUS_IS_OK(state.status)) {
    1321          50 :                 status = state.status;
    1322          50 :                 DBG_DEBUG("smb2srv_open_recreate_fn for %s failed: %s\n",
    1323             :                           tdb_data_dbg(key),
    1324             :                           nt_errstr(status));
    1325          50 :                 goto fail;
    1326             :         }
    1327             : 
    1328         158 :         talloc_set_destructor(state.op, smbXsrv_open_destructor);
    1329             : 
    1330         158 :         if (CHECK_DEBUGLVL(10)) {
    1331           0 :                 struct smbXsrv_openB open_blob = {
    1332           0 :                         .info.info0 = state.op,
    1333             :                 };
    1334           0 :                 DBG_DEBUG("global_id (0x%08x) stored\n",
    1335             :                           state.op->global->open_global_id);
    1336           0 :                 NDR_PRINT_DEBUG(smbXsrv_openB, &open_blob);
    1337             :         }
    1338             : 
    1339         158 :         *_open = state.op;
    1340             : 
    1341         158 :         return NT_STATUS_OK;
    1342          50 : fail:
    1343          50 :         table->local.num_opens -= 1;
    1344             : 
    1345          50 :         ret = idr_remove(table->local.idr, state.op->local_id);
    1346          50 :         SMB_ASSERT(ret == 0);
    1347          50 :         TALLOC_FREE(state.op);
    1348          50 :         return status;
    1349             : }
    1350             : 
    1351             : struct smbXsrv_open_global_traverse_state {
    1352             :         int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *);
    1353             :         void *private_data;
    1354             : };
    1355             : 
    1356           0 : static int smbXsrv_open_global_traverse_fn(struct db_record *rec, void *data)
    1357             : {
    1358           0 :         struct smbXsrv_open_global_traverse_state *state =
    1359             :                 (struct smbXsrv_open_global_traverse_state*)data;
    1360           0 :         struct smbXsrv_open_global0 *global = NULL;
    1361           0 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1362           0 :         TDB_DATA val = dbwrap_record_get_value(rec);
    1363           0 :         NTSTATUS status;
    1364           0 :         int ret = -1;
    1365             : 
    1366           0 :         status = smbXsrv_open_global_parse_record(
    1367             :                 talloc_tos(), key, val, &global);
    1368           0 :         if (!NT_STATUS_IS_OK(status)) {
    1369           0 :                 return -1;
    1370             :         }
    1371             : 
    1372           0 :         ret = state->fn(rec, global, state->private_data);
    1373           0 :         talloc_free(global);
    1374           0 :         return ret;
    1375             : }
    1376             : 
    1377           0 : NTSTATUS smbXsrv_open_global_traverse(
    1378             :         int (*fn)(struct db_record *rec, struct smbXsrv_open_global0 *, void *),
    1379             :         void *private_data)
    1380             : {
    1381             : 
    1382           0 :         NTSTATUS status;
    1383           0 :         int count = 0;
    1384           0 :         struct smbXsrv_open_global_traverse_state state = {
    1385             :                 .fn = fn,
    1386             :                 .private_data = private_data,
    1387             :         };
    1388             : 
    1389           0 :         become_root();
    1390           0 :         status = smbXsrv_open_global_init();
    1391           0 :         if (!NT_STATUS_IS_OK(status)) {
    1392           0 :                 unbecome_root();
    1393           0 :                 DEBUG(0, ("Failed to initialize open_global: %s\n",
    1394             :                           nt_errstr(status)));
    1395           0 :                 return status;
    1396             :         }
    1397             : 
    1398           0 :         status = dbwrap_traverse_read(smbXsrv_open_global_db_ctx,
    1399             :                                       smbXsrv_open_global_traverse_fn,
    1400             :                                       &state,
    1401             :                                       &count);
    1402           0 :         unbecome_root();
    1403             : 
    1404           0 :         return status;
    1405             : }
    1406             : 
    1407             : struct smbXsrv_open_cleanup_state {
    1408             :         uint32_t global_id;
    1409             :         NTSTATUS status;
    1410             : };
    1411             : 
    1412          50 : static void smbXsrv_open_cleanup_fn(
    1413             :         struct db_record *rec, TDB_DATA oldval, void *private_data)
    1414             : {
    1415          50 :         struct smbXsrv_open_cleanup_state *state = private_data;
    1416          50 :         struct smbXsrv_open_global0 *global = NULL;
    1417          50 :         TDB_DATA key = dbwrap_record_get_key(rec);
    1418          50 :         bool delete_open = false;
    1419             : 
    1420          50 :         if (oldval.dsize == 0) {
    1421          37 :                 DBG_DEBUG("[global: 0x%08x] "
    1422             :                           "empty record in %s, skipping...\n",
    1423             :                           state->global_id,
    1424             :                           dbwrap_name(dbwrap_record_get_db(rec)));
    1425          37 :                 state->status = NT_STATUS_OK;
    1426          37 :                 return;
    1427             :         }
    1428             : 
    1429          13 :         state->status = smbXsrv_open_global_parse_record(
    1430             :                 talloc_tos(), key, oldval, &global);
    1431          13 :         if (!NT_STATUS_IS_OK(state->status)) {
    1432           0 :                 DBG_WARNING("[global: %x08x] "
    1433             :                             "smbXsrv_open_global_parse_record() in %s "
    1434             :                             "failed: %s, deleting record\n",
    1435             :                             state->global_id,
    1436             :                             dbwrap_name(dbwrap_record_get_db(rec)),
    1437             :                             nt_errstr(state->status));
    1438           0 :                 delete_open = true;
    1439           0 :                 goto do_delete;
    1440             :         }
    1441             : 
    1442          13 :         if (server_id_is_disconnected(&global->server_id)) {
    1443          13 :                 struct timeval now = timeval_current();
    1444           0 :                 struct timeval disconnect_time;
    1445           0 :                 struct timeval_buf buf;
    1446           0 :                 int64_t tdiff;
    1447             : 
    1448          13 :                 nttime_to_timeval(&disconnect_time, global->disconnect_time);
    1449          13 :                 tdiff = usec_time_diff(&now, &disconnect_time);
    1450          13 :                 delete_open = (tdiff >= 1000*global->durable_timeout_msec);
    1451             : 
    1452          13 :                 DBG_DEBUG("[global: 0x%08x] "
    1453             :                           "disconnected at [%s] %"PRIi64"s ago with "
    1454             :                           "timeout of %"PRIu32"s -%s reached\n",
    1455             :                           state->global_id,
    1456             :                           timeval_str_buf(&disconnect_time,
    1457             :                                           false,
    1458             :                                           false,
    1459             :                                           &buf),
    1460             :                           tdiff/1000000,
    1461             :                           global->durable_timeout_msec / 1000,
    1462             :                           delete_open ? "" : " not");
    1463           0 :         } else if (!serverid_exists(&global->server_id)) {
    1464           0 :                 struct server_id_buf idbuf;
    1465           0 :                 DBG_DEBUG("[global: 0x%08x] "
    1466             :                           "server[%s] does not exist\n",
    1467             :                           state->global_id,
    1468             :                           server_id_str_buf(global->server_id, &idbuf));
    1469           0 :                 delete_open = true;
    1470             :         }
    1471             : 
    1472          13 :         if (!delete_open) {
    1473           2 :                 state->status = NT_STATUS_OK;
    1474           2 :                 return;
    1475             :         }
    1476          11 : do_delete:
    1477          11 :         state->status = dbwrap_record_delete(rec);
    1478          11 :         if (!NT_STATUS_IS_OK(state->status)) {
    1479           0 :                 DBG_WARNING("[global: 0x%08x] "
    1480             :                             "failed to delete record "
    1481             :                             "from %s: %s\n",
    1482             :                             state->global_id,
    1483             :                             dbwrap_name(dbwrap_record_get_db(rec)),
    1484             :                             nt_errstr(state->status));
    1485           0 :                 return;
    1486             :         }
    1487             : 
    1488          11 :         DBG_DEBUG("[global: 0x%08x] "
    1489             :                   "deleted record from %s\n",
    1490             :                   state->global_id,
    1491             :                   dbwrap_name(dbwrap_record_get_db(rec)));
    1492             : }
    1493             : 
    1494          50 : NTSTATUS smbXsrv_open_cleanup(uint64_t persistent_id)
    1495             : {
    1496          50 :         struct smbXsrv_open_cleanup_state state = {
    1497             :                 .global_id = persistent_id & UINT32_MAX,
    1498             :         };
    1499           0 :         struct smbXsrv_open_global_key_buf key_buf;
    1500          50 :         TDB_DATA key = smbXsrv_open_global_id_to_key(
    1501             :                 state.global_id, &key_buf);
    1502           0 :         NTSTATUS status;
    1503             : 
    1504          50 :         status = dbwrap_do_locked(
    1505             :                 smbXsrv_open_global_db_ctx,
    1506             :                 key,
    1507             :                 smbXsrv_open_cleanup_fn,
    1508             :                 &state);
    1509          50 :         if (!NT_STATUS_IS_OK(status)) {
    1510           0 :                 DBG_DEBUG("[global: 0x%08x] dbwrap_do_locked failed: %s\n",
    1511             :                           state.global_id,
    1512             :                           nt_errstr(status));
    1513           0 :                 return status;
    1514             :         }
    1515             : 
    1516          50 :         return state.status;
    1517             : }

Generated by: LCOV version 1.14