LCOV - code coverage report
Current view: top level - source3/smbd - smbXsrv_version.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 2 137 1.5 %
Date: 2024-02-28 12:06:22 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    Copyright (C) Stefan Metzmacher 2012
       5             : 
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             : 
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             : 
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "system/filesys.h"
      22             : #include "smbd/globals.h"
      23             : #include "dbwrap/dbwrap.h"
      24             : #include "dbwrap/dbwrap_open.h"
      25             : #include "lib/util/util_tdb.h"
      26             : #include "librpc/gen_ndr/ndr_smbXsrv.h"
      27             : #include "serverid.h"
      28             : 
      29             : /*
      30             :  * This implements a version scheme for file server internal
      31             :  * states. smbXsrv_version_global.tdb stores the possible
      32             :  * and current versions of structure formats (struct smbXsrv_*_global)
      33             :  * per cluster node.
      34             :  *
      35             :  * If the supported versions doesn't match a version of any
      36             :  * of the other nodes, it refused to start.
      37             :  *
      38             :  * This should prevent silent corruption of the internal
      39             :  * databases and structures, if two incompatible implementations
      40             :  * read and write.
      41             :  *
      42             :  * In future this can be used to implement rolling code upgrades
      43             :  * in a cluster, but for now it is simple.
      44             :  */
      45             : 
      46             : static struct db_context *smbXsrv_version_global_db_ctx = NULL;
      47             : static uint32_t smbXsrv_version_global_current_version = UINT32_MAX;
      48             : 
      49           0 : NTSTATUS smbXsrv_version_global_init(const struct server_id *server_id)
      50             : {
      51           0 :         const char *global_path = NULL;
      52           0 :         struct db_context *db_ctx = NULL;
      53           0 :         struct db_record *db_rec = NULL;
      54           0 :         TDB_DATA key;
      55           0 :         TDB_DATA val;
      56           0 :         DATA_BLOB blob;
      57           0 :         struct smbXsrv_version_globalB global_blob;
      58           0 :         enum ndr_err_code ndr_err;
      59           0 :         struct smbXsrv_version_global0 *global = NULL;
      60           0 :         uint32_t i;
      61           0 :         uint32_t num_valid = 0;
      62           0 :         struct smbXsrv_version_node0 *valid = NULL;
      63           0 :         struct smbXsrv_version_node0 *local_node = NULL;
      64           0 :         bool exists;
      65           0 :         NTSTATUS status;
      66           0 :         const char *key_string = "smbXsrv_version_global";
      67           0 :         TALLOC_CTX *frame;
      68             : 
      69           0 :         if (smbXsrv_version_global_db_ctx != NULL) {
      70           0 :                 return NT_STATUS_OK;
      71             :         }
      72             : 
      73           0 :         frame = talloc_stackframe();
      74             : 
      75           0 :         global_path = lock_path(talloc_tos(), "smbXsrv_version_global.tdb");
      76           0 :         if (global_path == NULL) {
      77           0 :                 TALLOC_FREE(frame);
      78           0 :                 return NT_STATUS_NO_MEMORY;
      79             :         }
      80             : 
      81           0 :         db_ctx = db_open(NULL, global_path,
      82             :                          0, /* hash_size */
      83             :                          TDB_DEFAULT |
      84             :                          TDB_CLEAR_IF_FIRST |
      85             :                          TDB_INCOMPATIBLE_HASH,
      86             :                          O_RDWR | O_CREAT, 0600,
      87             :                          DBWRAP_LOCK_ORDER_1,
      88             :                          DBWRAP_FLAG_NONE);
      89           0 :         if (db_ctx == NULL) {
      90           0 :                 status = map_nt_error_from_unix_common(errno);
      91           0 :                 DEBUG(0,("smbXsrv_version_global_init: "
      92             :                          "failed to open[%s] - %s\n",
      93             :                          global_path, nt_errstr(status)));
      94           0 :                 TALLOC_FREE(frame);
      95           0 :                 return status;
      96             :         }
      97             : 
      98           0 :         key = string_term_tdb_data(key_string);
      99             : 
     100           0 :         db_rec = dbwrap_fetch_locked(db_ctx, db_ctx, key);
     101           0 :         if (db_rec == NULL) {
     102           0 :                 status = NT_STATUS_INTERNAL_DB_ERROR;
     103           0 :                 DEBUG(0,("smbXsrv_version_global_init: "
     104             :                          "dbwrap_fetch_locked(%s) - %s\n",
     105             :                          key_string, nt_errstr(status)));
     106           0 :                 TALLOC_FREE(frame);
     107           0 :                 return status;
     108             :         }
     109             : 
     110           0 :         val = dbwrap_record_get_value(db_rec);
     111           0 :         if (val.dsize == 0) {
     112           0 :                 global = talloc_zero(frame, struct smbXsrv_version_global0);
     113           0 :                 if (global == NULL) {
     114           0 :                         DEBUG(0,("smbXsrv_version_global_init: "
     115             :                                  "talloc_zero failed - %s\n", __location__));
     116           0 :                         TALLOC_FREE(frame);
     117           0 :                         return NT_STATUS_NO_MEMORY;
     118             :                 }
     119           0 :                 ZERO_STRUCT(global_blob);
     120           0 :                 global_blob.version = SMBXSRV_VERSION_CURRENT;
     121           0 :                 global_blob.info.info0 = global;
     122             :         } else {
     123           0 :                 blob = data_blob_const(val.dptr, val.dsize);
     124             : 
     125           0 :                 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
     126             :                         (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_version_globalB);
     127           0 :                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     128           0 :                         status = ndr_map_error2ntstatus(ndr_err);
     129           0 :                         DEBUG(0,("smbXsrv_version_global_init: "
     130             :                                  "ndr_pull_smbXsrv_version_globalB - %s\n",
     131             :                                  nt_errstr(status)));
     132           0 :                         TALLOC_FREE(frame);
     133           0 :                         return status;
     134             :                 }
     135             : 
     136           0 :                 switch (global_blob.version) {
     137           0 :                 case SMBXSRV_VERSION_0:
     138           0 :                         global = global_blob.info.info0;
     139           0 :                         if (global == NULL) {
     140           0 :                                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     141           0 :                                 break;
     142             :                         }
     143           0 :                         status = NT_STATUS_OK;
     144           0 :                         break;
     145           0 :                 default:
     146           0 :                         status = NT_STATUS_REVISION_MISMATCH;
     147           0 :                         break;
     148             :                 }
     149             : 
     150           0 :                 if (!NT_STATUS_IS_OK(status)) {
     151           0 :                         DEBUG(0,("smbXsrv_version_global_init - %s\n",
     152             :                                  nt_errstr(status)));
     153           0 :                         NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
     154           0 :                         TALLOC_FREE(frame);
     155           0 :                         return status;
     156             :                 }
     157             :         }
     158             : 
     159           0 :         valid = talloc_zero_array(global,
     160             :                                   struct smbXsrv_version_node0,
     161             :                                   global->num_nodes + 1);
     162           0 :         if (valid == NULL) {
     163           0 :                 DEBUG(0,("smbXsrv_version_global_init: "
     164             :                          "talloc_zero_array failed - %s\n", __location__));
     165           0 :                 TALLOC_FREE(frame);
     166           0 :                 return NT_STATUS_NO_MEMORY;
     167             :         }
     168             : 
     169           0 :         num_valid = 0;
     170           0 :         for (i=0; i < global->num_nodes; i++) {
     171           0 :                 struct smbXsrv_version_node0 *n = &global->nodes[i];
     172             : 
     173           0 :                 exists = serverid_exists(&n->server_id);
     174           0 :                 if (!exists) {
     175           0 :                         continue;
     176             :                 }
     177             : 
     178           0 :                 if (n->min_version > n->max_version) {
     179           0 :                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     180           0 :                         DEBUG(0,("smbXsrv_version_global_init - %s\n",
     181             :                                  nt_errstr(status)));
     182           0 :                         NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
     183           0 :                         TALLOC_FREE(frame);
     184           0 :                         return status;
     185             :                 }
     186             : 
     187           0 :                 if (n->min_version > global_blob.version) {
     188           0 :                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     189           0 :                         DEBUG(0,("smbXsrv_version_global_init - %s\n",
     190             :                                  nt_errstr(status)));
     191           0 :                         NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
     192           0 :                         TALLOC_FREE(frame);
     193           0 :                         return status;
     194             :                 }
     195             : 
     196           0 :                 if (n->max_version < global_blob.version) {
     197           0 :                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
     198           0 :                         DEBUG(0,("smbXsrv_version_global_init - %s\n",
     199             :                                  nt_errstr(status)));
     200           0 :                         NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
     201           0 :                         TALLOC_FREE(frame);
     202           0 :                         return status;
     203             :                 }
     204             : 
     205           0 :                 valid[num_valid] = *n;
     206           0 :                 if (server_id->vnn == n->server_id.vnn) {
     207           0 :                         local_node = &valid[num_valid];
     208             :                 }
     209           0 :                 num_valid++;
     210             :         }
     211             : 
     212           0 :         if (local_node == NULL) {
     213           0 :                 local_node = &valid[num_valid];
     214           0 :                 num_valid++;
     215             :         }
     216             : 
     217           0 :         local_node->server_id = *server_id;
     218           0 :         local_node->min_version = SMBXSRV_VERSION_0;
     219           0 :         local_node->max_version = SMBXSRV_VERSION_CURRENT;
     220           0 :         local_node->current_version = global_blob.version;
     221             : 
     222           0 :         global->num_nodes = num_valid;
     223           0 :         global->nodes = valid;
     224             : 
     225           0 :         global_blob.seqnum += 1;
     226           0 :         global_blob.info.info0 = global;
     227             : 
     228           0 :         ndr_err = ndr_push_struct_blob(&blob, db_rec, &global_blob,
     229             :                         (ndr_push_flags_fn_t)ndr_push_smbXsrv_version_globalB);
     230           0 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     231           0 :                 status = ndr_map_error2ntstatus(ndr_err);
     232           0 :                 DEBUG(0,("smbXsrv_version_global_init: "
     233             :                          "ndr_push_smbXsrv_version_globalB - %s\n",
     234             :                          nt_errstr(status)));
     235           0 :                 TALLOC_FREE(frame);
     236           0 :                 return status;
     237             :         }
     238             : 
     239           0 :         val = make_tdb_data(blob.data, blob.length);
     240           0 :         status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
     241           0 :         TALLOC_FREE(db_rec);
     242           0 :         if (!NT_STATUS_IS_OK(status)) {
     243           0 :                 DEBUG(0,("smbXsrv_version_global_init: "
     244             :                          "dbwrap_record_store - %s\n",
     245             :                          nt_errstr(status)));
     246           0 :                 TALLOC_FREE(frame);
     247           0 :                 return status;
     248             :         }
     249             : 
     250           0 :         DEBUG(10,("smbXsrv_version_global_init\n"));
     251           0 :         if (DEBUGLVL(10)) {
     252           0 :                 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
     253             :         }
     254             : 
     255           0 :         smbXsrv_version_global_db_ctx = db_ctx;
     256           0 :         smbXsrv_version_global_current_version = global_blob.version;
     257             : 
     258           0 :         TALLOC_FREE(frame);
     259           0 :         return NT_STATUS_OK;
     260             : }
     261             : 
     262      942952 : uint32_t smbXsrv_version_global_current(void)
     263             : {
     264      942952 :         return smbXsrv_version_global_current_version;
     265             : }

Generated by: LCOV version 1.14