static struct db_context *regdb = NULL;
static int regdb_refcount;
+static bool regdb_key_exists(const char *key);
+
/* List the deepest path into the registry. All part components will be created.*/
/* If you want to have a part of the path controlled by the tdb and part by
KEY_SMBCONF,
KEY_PERFLIB,
KEY_PERFLIB_009,
+ KEY_GROUP_POLICY,
+ KEY_SAMBA_GROUP_POLICY,
+ KEY_GP_MACHINE_POLICY,
+ KEY_GP_MACHINE_WIN_POLICY,
+ KEY_HKCU,
+ KEY_GP_USER_POLICY,
+ KEY_GP_USER_WIN_POLICY,
+ KEY_WINLOGON_GPEXT_PATH,
"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
KEY_PROD_OPTIONS,
"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
{ NULL, NULL, 0, { NULL } }
};
-/***********************************************************************
- Open the registry data in the tdb
- ***********************************************************************/
-
-static bool init_registry_data(void)
+/**
+ * Initialize a key in the registry:
+ * create each component key of the specified path.
+ */
+static WERROR init_registry_key_internal(const char *add_path)
{
+ WERROR werr;
+ TALLOC_CTX *frame = talloc_stackframe();
char *path = NULL;
char *base = NULL;
char *remaining = NULL;
- TALLOC_CTX *frame = NULL;
char *keyname;
char *subkeyname;
REGSUBKEY_CTR *subkeys;
- REGVAL_CTR *values;
- int i;
const char *p, *p2;
- UNISTR2 data;
- /*
- * There are potentially quite a few store operations which are all
- * indiviually wrapped in tdb transactions. Wrapping them in a single
- * transaction gives just a single transaction_commit() to actually do
- * its fsync()s. See tdb/common/transaction.c for info about nested
- * transaction behaviour.
- */
+ DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
- if (regdb->transaction_start(regdb) == -1) {
- DEBUG(0, ("init_registry_data: tdb_transaction_start "
- "failed\n"));
- return false;
+ path = talloc_strdup(frame, add_path);
+ base = talloc_strdup(frame, "");
+ if (!path || !base) {
+ werr = WERR_NOMEM;
+ goto fail;
}
+ p = path;
- /* loop over all of the predefined paths and add each component */
-
- for (i=0; builtin_registry_paths[i] != NULL; i++) {
-
- frame = talloc_stackframe();
-
- DEBUG(6, ("init_registry_data: Adding [%s]\n",
- builtin_registry_paths[i]));
-
- path = talloc_strdup(frame, builtin_registry_paths[i]);
- base = talloc_strdup(frame, "");
- if (!path || !base) {
- goto fail;
- }
- p = path;
-
- while (next_token_talloc(frame, &p, &keyname, "\\")) {
+ while (next_token_talloc(frame, &p, &keyname, "\\")) {
- /* build up the registry path from the components */
+ /* build up the registry path from the components */
- if (*base) {
- base = talloc_asprintf(frame, "%s\\", base);
- if (!base) {
- goto fail;
- }
- }
- base = talloc_asprintf_append(base, "%s", keyname);
+ if (*base) {
+ base = talloc_asprintf(frame, "%s\\", base);
if (!base) {
+ werr = WERR_NOMEM;
goto fail;
}
+ }
+ base = talloc_asprintf_append(base, "%s", keyname);
+ if (!base) {
+ werr = WERR_NOMEM;
+ goto fail;
+ }
- /* get the immediate subkeyname (if we have one ) */
+ /* get the immediate subkeyname (if we have one ) */
- subkeyname = talloc_strdup(frame, "");
- if (!subkeyname) {
+ subkeyname = talloc_strdup(frame, "");
+ if (!subkeyname) {
+ werr = WERR_NOMEM;
+ goto fail;
+ }
+ if (*p) {
+ remaining = talloc_strdup(frame, p);
+ if (!remaining) {
+ werr = WERR_NOMEM;
goto fail;
}
- if (*p) {
- remaining = talloc_strdup(frame, p);
- if (!remaining) {
+ p2 = remaining;
+
+ if (!next_token_talloc(frame, &p2,
+ &subkeyname, "\\"))
+ {
+ subkeyname = talloc_strdup(frame,p2);
+ if (!subkeyname) {
+ werr = WERR_NOMEM;
goto fail;
}
- p2 = remaining;
-
- if (!next_token_talloc(frame, &p2,
- &subkeyname, "\\"))
- {
- subkeyname = talloc_strdup(frame,p2);
- if (!subkeyname) {
- goto fail;
- }
- }
}
+ }
- DEBUG(10,("init_registry_data: Storing key [%s] with "
- "subkey [%s]\n", base,
- *subkeyname ? subkeyname : "NULL"));
+ DEBUG(10,("init_registry_key: Storing key [%s] with "
+ "subkey [%s]\n", base,
+ *subkeyname ? subkeyname : "NULL"));
- /* we don't really care if the lookup succeeds or not
- * since we are about to update the record.
- * We just want any subkeys already present */
+ /* we don't really care if the lookup succeeds or not
+ * since we are about to update the record.
+ * We just want any subkeys already present */
- if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
- DEBUG(0,("talloc() failure!\n"));
- goto fail;
- }
+ if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
+ DEBUG(0,("talloc() failure!\n"));
+ werr = WERR_NOMEM;
+ goto fail;
+ }
- regdb_fetch_keys(base, subkeys);
- if (*subkeyname) {
- regsubkey_ctr_addkey( subkeys, subkeyname);
- }
- if (!regdb_store_keys( base, subkeys)) {
+ regdb_fetch_keys(base, subkeys);
+ if (*subkeyname) {
+ werr = regsubkey_ctr_addkey(subkeys, subkeyname);
+ if (!W_ERROR_IS_OK(werr)) {
goto fail;
}
}
+ if (!regdb_store_keys( base, subkeys)) {
+ werr = WERR_CAN_NOT_COMPLETE;
+ goto fail;
+ }
+ }
+
+ werr = WERR_OK;
+
+fail:
+ TALLOC_FREE(frame);
+ return werr;
+}
+
+/**
+ * Initialize a key in the registry:
+ * create each component key of the specified path,
+ * wrapped in one db transaction.
+ */
+WERROR init_registry_key(const char *add_path)
+{
+ WERROR werr;
+
+ if (regdb_key_exists(add_path)) {
+ return WERR_OK;
+ }
+
+ if (regdb->transaction_start(regdb) != 0) {
+ DEBUG(0, ("init_registry_key: transaction_start failed\n"));
+ return WERR_REG_IO_FAILURE;
+ }
+
+ werr = init_registry_key_internal(add_path);
+ if (!W_ERROR_IS_OK(werr)) {
+ goto fail;
+ }
+
+ if (regdb->transaction_commit(regdb) != 0) {
+ DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
+ return WERR_REG_IO_FAILURE;
+ }
+
+ return WERR_OK;
+
+fail:
+ if (regdb->transaction_cancel(regdb) != 0) {
+ smb_panic("init_registry_key: transaction_cancel failed\n");
+ }
+
+ return werr;
+}
+
+/***********************************************************************
+ Open the registry data in the tdb
+ ***********************************************************************/
+
+WERROR init_registry_data(void)
+{
+ WERROR werr;
+ TALLOC_CTX *frame = talloc_stackframe();
+ REGVAL_CTR *values;
+ int i;
+ UNISTR2 data;
+
+ /*
+ * First, check for the existence of the needed keys and values.
+ * If all do already exist, we can save the writes.
+ */
+ for (i=0; builtin_registry_paths[i] != NULL; i++) {
+ if (!regdb_key_exists(builtin_registry_paths[i])) {
+ goto do_init;
+ }
+ }
+
+ for (i=0; builtin_registry_values[i].path != NULL; i++) {
+ values = TALLOC_ZERO_P(frame, REGVAL_CTR);
+ if (values == NULL) {
+ werr = WERR_NOMEM;
+ goto done;
+ }
+
+ regdb_fetch_values(builtin_registry_values[i].path, values);
+ if (!regval_ctr_key_exists(values,
+ builtin_registry_values[i].valuename))
+ {
+ TALLOC_FREE(values);
+ goto do_init;
+ }
- TALLOC_FREE(frame);
+ TALLOC_FREE(values);
}
- /* loop over all of the predefined values and add each component */
+ werr = WERR_OK;
+ goto done;
+
+do_init:
+
+ /*
+ * There are potentially quite a few store operations which are all
+ * indiviually wrapped in tdb transactions. Wrapping them in a single
+ * transaction gives just a single transaction_commit() to actually do
+ * its fsync()s. See tdb/common/transaction.c for info about nested
+ * transaction behaviour.
+ */
+
+ if (regdb->transaction_start(regdb) != 0) {
+ DEBUG(0, ("init_registry_data: tdb_transaction_start "
+ "failed\n"));
+ werr = WERR_REG_IO_FAILURE;
+ goto done;
+ }
+
+ /* loop over all of the predefined paths and add each component */
+
+ for (i=0; builtin_registry_paths[i] != NULL; i++) {
+ if (regdb_key_exists(builtin_registry_paths[i])) {
+ continue;
+ }
+ werr = init_registry_key_internal(builtin_registry_paths[i]);
+ if (!W_ERROR_IS_OK(werr)) {
+ goto fail;
+ }
+ }
- frame = talloc_stackframe();
+ /* loop over all of the predefined values and add each component */
for (i=0; builtin_registry_values[i].path != NULL; i++) {
- if (!(values = TALLOC_ZERO_P(frame, REGVAL_CTR))) {
+ values = TALLOC_ZERO_P(frame, REGVAL_CTR);
+ if (values == NULL) {
+ werr = WERR_NOMEM;
goto fail;
}
TALLOC_FREE(values);
}
- TALLOC_FREE(frame);
-
- if (regdb->transaction_commit(regdb) == -1) {
+ if (regdb->transaction_commit(regdb) != 0) {
DEBUG(0, ("init_registry_data: Could not commit "
"transaction\n"));
- return false;
+ werr = WERR_REG_IO_FAILURE;
+ } else {
+ werr = WERR_OK;
}
- return true;
-
- fail:
+ goto done;
- TALLOC_FREE(frame);
-
- if (regdb->transaction_cancel(regdb) == -1) {
+fail:
+ if (regdb->transaction_cancel(regdb) != 0) {
smb_panic("init_registry_data: tdb_transaction_cancel "
"failed\n");
}
- return false;
+done:
+ TALLOC_FREE(frame);
+ return werr;
}
/***********************************************************************
Open the registry database
***********************************************************************/
-bool regdb_init( void )
+WERROR regdb_init(void)
{
const char *vstring = "INFO/version";
uint32 vers_id;
+ WERROR werr;
- if ( regdb ) {
- DEBUG(10,("regdb_init: incrementing refcount (%d)\n", regdb_refcount));
+ if (regdb) {
+ DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
+ regdb_refcount));
regdb_refcount++;
- return true;
+ return WERR_OK;
}
- if ( !(regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
- {
- regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
- if ( !regdb ) {
+ regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
+ REG_TDB_FLAGS, O_RDWR, 0600);
+ if (!regdb) {
+ regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
+ REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
+ if (!regdb) {
+ werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
state_path("registry.tdb"), strerror(errno) ));
- return false;
+ return werr;
}
DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
vers_id = dbwrap_fetch_int32(regdb, vstring);
if ( vers_id != REGVER_V1 ) {
+ NTSTATUS status;
/* any upgrade code here if needed */
- DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
+ DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
vers_id, REGVER_V1));
+ status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
+ vstring, REGVER_V1, nt_errstr(status)));
+ return ntstatus_to_werror(status);
+ } else {
+ DEBUG(10, ("regdb_init: stored %s = %d\n",
+ vstring, REGVER_V1));
+ }
}
- /* always setup the necessary keys and values */
-
- if ( !init_registry_data() ) {
- DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
- return false;
- }
-
- return true;
+ return WERR_OK;
}
/***********************************************************************
become_root();
- regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
+ regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
+ REG_TDB_FLAGS, O_RDWR, 0600);
if ( !regdb ) {
result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
TALLOC_FREE(old_subkeys);
- if (regdb->transaction_start(regdb) == -1) {
+ if (regdb->transaction_start(regdb) != 0) {
DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
goto fail;
}
TALLOC_FREE(path);
}
- if (regdb->transaction_commit(regdb) == -1) {
+ if (regdb->transaction_commit(regdb) != 0) {
DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
goto fail;
}
return true;
cancel:
- if (regdb->transaction_cancel(regdb) == -1) {
+ if (regdb->transaction_cancel(regdb) != 0) {
smb_panic("regdb_store_keys: transaction_cancel failed\n");
}
}
+static TDB_DATA regdb_fetch_key_internal(TALLOC_CTX *mem_ctx, const char *key)
+{
+ char *path = NULL;
+ TDB_DATA data;
+
+ path = normalize_reg_path(mem_ctx, key);
+ if (!path) {
+ return make_tdb_data(NULL, 0);
+ }
+
+ data = dbwrap_fetch_bystring(regdb, mem_ctx, path);
+
+ TALLOC_FREE(path);
+ return data;
+}
+
+
+static bool regdb_key_exists(const char *key)
+{
+ TALLOC_CTX *mem_ctx = talloc_stackframe();
+ TDB_DATA value;
+ bool ret;
+
+ value = regdb_fetch_key_internal(mem_ctx, key);
+ ret = (value.dptr != NULL);
+
+ TALLOC_FREE(mem_ctx);
+ return ret;
+}
+
+
/***********************************************************************
Retrieve an array of strings containing subkeys. Memory should be
released by the caller.
int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
{
- char *path = NULL;
+ WERROR werr;
uint32 num_items;
uint8 *buf;
uint32 buflen, len;
fstring subkeyname;
int ret = -1;
TALLOC_CTX *frame = talloc_stackframe();
- struct db_record *rec;
+ TDB_DATA value;
DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
- path = talloc_strdup(frame, key);
- if (!path) {
- goto fail;
- }
-
- /* convert to key format */
- path = talloc_string_sub(frame, path, "\\", "/");
- if (!path) {
- goto fail;
- }
- strupper_m(path);
-
- rec = regdb->fetch_locked(regdb, frame, string_term_tdb_data(path));
- if (rec == NULL) {
- return 0;
- }
-
ctr->seqnum = regdb_get_seqnum();
- buf = rec->value.dptr;
- buflen = rec->value.dsize;
+ value = regdb_fetch_key_internal(frame, key);
+
+ buf = value.dptr;
+ buflen = value.dsize;
if ( !buf ) {
DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
for (i=0; i<num_items; i++) {
len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
- regsubkey_ctr_addkey(ctr, subkeyname);
+ werr = regsubkey_ctr_addkey(ctr, subkeyname);
+ if (!W_ERROR_IS_OK(werr)) {
+ DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
+ "failed: %s\n", dos_errstr(werr)));
+ goto fail;
+ }
}
DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
{
char *keystr = NULL;
TALLOC_CTX *ctx = talloc_stackframe();
- struct db_record *rec;
int ret = 0;
+ TDB_DATA value;
DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
if (!keystr) {
return 0;
}
- keystr = normalize_reg_path(ctx, keystr);
- if (!keystr) {
- goto done;
- }
-
- rec = regdb->fetch_locked(regdb, ctx, string_term_tdb_data(keystr));
- if (rec == NULL) {
- goto done;
- }
values->seqnum = regdb_get_seqnum();
- if (!rec->value.dptr) {
+ value = regdb_fetch_key_internal(ctx, keystr);
+
+ if (!value.dptr) {
/* all keys have zero values by default */
goto done;
}
- regdb_unpack_values(values, rec->value.dptr, rec->value.dsize);
+ regdb_unpack_values(values, value.dptr, value.dsize);
ret = regval_ctr_numvals(values);
done:
{
TDB_DATA old_data, data;
char *keystr = NULL;
- TALLOC_CTX *ctx = talloc_tos();
- int len, ret;
+ TALLOC_CTX *ctx = talloc_stackframe();
+ int len;
+ NTSTATUS status;
+ bool result = false;
DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
len = regdb_pack_values(values, data.dptr, data.dsize);
if (len <= 0) {
DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
- return false;
+ goto done;
}
- data.dptr = SMB_MALLOC_ARRAY( uint8, len );
+ data.dptr = TALLOC_ARRAY(ctx, uint8, len);
data.dsize = len;
len = regdb_pack_values(values, data.dptr, data.dsize);
keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
if (!keystr) {
- SAFE_FREE(data.dptr);
- return false;
+ goto done;
}
keystr = normalize_reg_path(ctx, keystr);
if (!keystr) {
- SAFE_FREE(data.dptr);
- return false;
+ goto done;
}
old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
if ((old_data.dptr != NULL)
&& (old_data.dsize == data.dsize)
- && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0)) {
- TALLOC_FREE(old_data.dptr);
- SAFE_FREE(data.dptr);
- return true;
+ && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
+ {
+ result = true;
+ goto done;
}
- ret = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
- TDB_REPLACE);
+ status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
+ TDB_REPLACE);
- TALLOC_FREE( old_data.dptr );
- SAFE_FREE( data.dptr );
+ result = NT_STATUS_IS_OK(status);
- return ret != -1 ;
+done:
+ TALLOC_FREE(ctx);
+ return result;
}
static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
TDB_DATA data;
NTSTATUS status;
TALLOC_CTX *tmp_ctx = talloc_stackframe();
+ WERROR err = WERR_OK;
DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
- if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
- return WERR_NOMEM;
+ tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
+ if (tdbkey == NULL) {
+ err = WERR_NOMEM;
+ goto done;
}
normalize_dbkey(tdbkey);
data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
- SAFE_FREE(tdbkey);
-
if (data.dptr == NULL) {
- return WERR_BADFILE;
+ err = WERR_BADFILE;
+ goto done;
}
status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
psecdesc);
- TALLOC_FREE(tmp_ctx);
-
if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
- return WERR_NOMEM;
- }
-
- if (!NT_STATUS_IS_OK(status)) {
- return WERR_REG_CORRUPT;
+ err = WERR_NOMEM;
+ } else if (!NT_STATUS_IS_OK(status)) {
+ err = WERR_REG_CORRUPT;
}
- return WERR_OK;
+done:
+ TALLOC_FREE(tmp_ctx);
+ return err;
}
static WERROR regdb_set_secdesc(const char *key,
struct security_descriptor *secdesc)
{
- prs_struct ps;
- TALLOC_CTX *mem_ctx;
+ TALLOC_CTX *mem_ctx = talloc_stackframe();
char *tdbkey;
+ NTSTATUS status;
WERROR err = WERR_NOMEM;
TDB_DATA tdbdata;
- if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
- return WERR_NOMEM;
- }
-
- ZERO_STRUCT(ps);
-
- if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
- key))) {
+ tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
+ if (tdbkey == NULL) {
goto done;
}
normalize_dbkey(tdbkey);
if (secdesc == NULL) {
/* assuming a delete */
- int tdb_ret;
-
- tdb_ret = dbwrap_trans_delete(regdb,
- string_term_tdb_data(tdbkey));
- if (tdb_ret == -1) {
- err = ntstatus_to_werror(map_nt_error_from_unix(errno));
- } else {
+ status = dbwrap_trans_delete(regdb,
+ string_term_tdb_data(tdbkey));
+ if (NT_STATUS_IS_OK(status)) {
err = WERR_OK;
+ } else {
+ err = ntstatus_to_werror(status);
}
-
goto done;
}
goto done;
}
- if (dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
- tdbdata, 0) == -1) {
- err = ntstatus_to_werror(map_nt_error_from_unix(errno));
+ status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
+ tdbdata, 0);
+ if (!NT_STATUS_IS_OK(status)) {
+ err = ntstatus_to_werror(status);
goto done;
}
done:
- prs_mem_free(&ps);
TALLOC_FREE(mem_ctx);
return err;
}
*/
REGISTRY_OPS regdb_ops = {
- regdb_fetch_keys,
- regdb_fetch_values,
- regdb_store_keys,
- regdb_store_values,
- NULL,
- regdb_get_secdesc,
- regdb_set_secdesc,
- regdb_subkeys_need_update,
- regdb_values_need_update
+ .fetch_subkeys = regdb_fetch_keys,
+ .fetch_values = regdb_fetch_values,
+ .store_subkeys = regdb_store_keys,
+ .store_values = regdb_store_values,
+ .get_secdesc = regdb_get_secdesc,
+ .set_secdesc = regdb_set_secdesc,
+ .subkeys_need_update = regdb_subkeys_need_update,
+ .values_need_update = regdb_values_need_update
};