2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2012
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.
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.
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/>.
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"
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)
35 * If the supported versions doesn't match a version of any
36 * of the other nodes, it refused to start.
38 * This should prevent silent corruption of the internal
39 * databases and structures, if two incompatible implementations
42 * In future this can be used to implement rolling code upgrades
43 * in a cluster, but for now it is simple.
46 static struct db_context *smbXsrv_version_global_db_ctx = NULL;
47 static uint32_t smbXsrv_version_global_current_version = UINT32_MAX;
49 NTSTATUS smbXsrv_version_global_init(const struct server_id *server_id)
51 const char *global_path = NULL;
52 struct db_context *db_ctx = NULL;
53 struct db_record *db_rec = NULL;
57 struct smbXsrv_version_globalB global_blob;
58 enum ndr_err_code ndr_err;
59 struct smbXsrv_version_global0 *global = NULL;
61 uint32_t num_valid = 0;
62 struct smbXsrv_version_node0 *valid = NULL;
63 struct smbXsrv_version_node0 *local_node = NULL;
66 const char *key_string = "smbXsrv_version_global";
69 if (smbXsrv_version_global_db_ctx != NULL) {
73 frame = talloc_stackframe();
75 global_path = lock_path(talloc_tos(), "smbXsrv_version_global.tdb");
76 if (global_path == NULL) {
78 return NT_STATUS_NO_MEMORY;
81 db_ctx = db_open(NULL, global_path,
85 TDB_INCOMPATIBLE_HASH,
86 O_RDWR | O_CREAT, 0600,
90 status = map_nt_error_from_unix_common(errno);
91 DBG_ERR("failed to open[%s] - %s\n",
98 key = string_term_tdb_data(key_string);
100 db_rec = dbwrap_fetch_locked(db_ctx, db_ctx, key);
101 if (db_rec == NULL) {
102 status = NT_STATUS_INTERNAL_DB_ERROR;
103 DBG_ERR("dbwrap_fetch_locked(%s) - %s\n",
111 val = dbwrap_record_get_value(db_rec);
112 if (val.dsize == 0) {
113 global = talloc_zero(frame, struct smbXsrv_version_global0);
114 if (global == NULL) {
115 DBG_ERR("talloc_zero failed - %s\n", __location__);
117 return NT_STATUS_NO_MEMORY;
119 global_blob = (struct smbXsrv_version_globalB) {
120 .version = SMBXSRV_VERSION_CURRENT,
121 .info.info0 = global,
124 blob = data_blob_const(val.dptr, val.dsize);
126 ndr_err = ndr_pull_struct_blob(&blob, frame, &global_blob,
127 (ndr_pull_flags_fn_t)ndr_pull_smbXsrv_version_globalB);
128 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
129 status = ndr_map_error2ntstatus(ndr_err);
130 DBG_ERR("ndr_pull_smbXsrv_version_globalB - %s\n",
136 switch (global_blob.version) {
137 case SMBXSRV_VERSION_0:
138 global = global_blob.info.info0;
139 if (global == NULL) {
140 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
143 status = NT_STATUS_OK;
146 status = NT_STATUS_REVISION_MISMATCH;
150 if (!NT_STATUS_IS_OK(status)) {
151 DBG_ERR("%s\n", nt_errstr(status));
152 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
158 valid = talloc_zero_array(global,
159 struct smbXsrv_version_node0,
160 global->num_nodes + 1);
162 DBG_ERR("talloc_zero_array failed - %s\n", __location__);
164 return NT_STATUS_NO_MEMORY;
168 for (i=0; i < global->num_nodes; i++) {
169 struct smbXsrv_version_node0 *n = &global->nodes[i];
171 exists = serverid_exists(&n->server_id);
176 if (n->min_version > n->max_version) {
177 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
178 DBG_ERR("%s\n", nt_errstr(status));
179 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
184 if (n->min_version > global_blob.version) {
185 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
186 DBG_ERR("%s\n", nt_errstr(status));
187 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
192 if (n->max_version < global_blob.version) {
193 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
194 DBG_ERR("%s\n", nt_errstr(status));
195 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
200 valid[num_valid] = *n;
201 if (server_id->vnn == n->server_id.vnn) {
202 local_node = &valid[num_valid];
207 if (local_node == NULL) {
208 local_node = &valid[num_valid];
212 *local_node = (struct smbXsrv_version_node0){
213 .server_id = *server_id,
214 .min_version = SMBXSRV_VERSION_0,
215 .max_version = SMBXSRV_VERSION_CURRENT,
216 .current_version = global_blob.version,
219 global->num_nodes = num_valid;
220 global->nodes = valid;
222 global_blob.seqnum += 1;
223 global_blob.info.info0 = global;
225 ndr_err = ndr_push_struct_blob(&blob, db_rec, &global_blob,
226 (ndr_push_flags_fn_t)ndr_push_smbXsrv_version_globalB);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 status = ndr_map_error2ntstatus(ndr_err);
229 DBG_ERR("ndr_push_smbXsrv_version_globalB - %s\n",
235 val = make_tdb_data(blob.data, blob.length);
236 status = dbwrap_record_store(db_rec, val, TDB_REPLACE);
238 if (!NT_STATUS_IS_OK(status)) {
239 DBG_ERR("dbwrap_record_store - %s\n", nt_errstr(status));
246 NDR_PRINT_DEBUG(smbXsrv_version_globalB, &global_blob);
249 smbXsrv_version_global_db_ctx = db_ctx;
250 smbXsrv_version_global_current_version = global_blob.version;
256 uint32_t smbXsrv_version_global_current(void)
258 return smbXsrv_version_global_current_version;