Convert dbwrap_trans_store to NTSTATUS
[kai/samba-autobuild/.git] / source3 / registry / reg_backend_db.c
index 9b388767ec95a5b3b38dd9287ad4b958fbd75a59..4b8a4b4c374becd8a49678feb25795056beb2188 100644 (file)
@@ -46,6 +46,14 @@ static const char *builtin_registry_paths[] = {
        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",
@@ -79,115 +87,159 @@ static struct builtin_regkey_value builtin_registry_values[] = {
        { 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 bool init_registry_key_internal(const char *add_path)
 {
+       bool ret = false;
+       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) {
+               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();
+       while (next_token_talloc(frame, &p, &keyname, "\\")) {
 
-               DEBUG(6, ("init_registry_data: Adding [%s]\n",
-                         builtin_registry_paths[i]));
+               /* build up the registry path from the components */
 
-               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, "\\")) {
-
-                       /* 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) {
                                goto fail;
                        }
+               }
+               base = talloc_asprintf_append(base, "%s", keyname);
+               if (!base) {
+                       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) {
+                       goto fail;
+               }
+               if (*p) {
+                       remaining = talloc_strdup(frame, p);
+                       if (!remaining) {
                                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) {
                                        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"));
+                       goto fail;
+               }
 
-                       regdb_fetch_keys(base, subkeys);
-                       if (*subkeyname) {
-                               regsubkey_ctr_addkey( subkeys, subkeyname);
-                       }
-                       if (!regdb_store_keys( base, subkeys)) {
-                               goto fail;
-                       }
+               regdb_fetch_keys(base, subkeys);
+               if (*subkeyname) {
+                       regsubkey_ctr_addkey( subkeys, subkeyname);
+               }
+               if (!regdb_store_keys( base, subkeys)) {
+                       goto fail;
                }
+       }
+
+       ret = true;
+fail:
+       TALLOC_FREE(frame);
+       return ret;
+}
+
+/**
+ * Initialize a key in the registry:
+ * create each component key of the specified path,
+ * wrapped in one db transaction.
+ */
+bool init_registry_key(const char *add_path)
+{
+       if (regdb->transaction_start(regdb) != 0) {
+               DEBUG(0, ("init_registry_key: transaction_start failed\n"));
+               return false;
+       }
 
-               TALLOC_FREE(frame);
+       if (!init_registry_key_internal(add_path)) {
+               goto fail;
+       }
+
+       if (regdb->transaction_commit(regdb) != 0) {
+               DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
+               return false;
+       }
+
+       return true;
+
+fail:
+       if (regdb->transaction_cancel(regdb) != 0) {
+               smb_panic("init_registry_key: transaction_cancel failed\n");
+       }
+
+       return false;
+}
+
+/***********************************************************************
+ Open the registry data in the tdb
+ ***********************************************************************/
+
+bool init_registry_data(void)
+{
+       TALLOC_CTX *frame = NULL;
+       REGVAL_CTR *values;
+       int i;
+       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.
+        */
+
+       if (regdb->transaction_start(regdb) != 0) {
+               DEBUG(0, ("init_registry_data: tdb_transaction_start "
+                         "failed\n"));
+               return false;
+       }
+
+       /* loop over all of the predefined paths and add each component */
+
+       for (i=0; builtin_registry_paths[i] != NULL; i++) {
+               if (!init_registry_key_internal(builtin_registry_paths[i])) {
+                       goto fail;
+               }
        }
 
        /* loop over all of the predefined values and add each component */
@@ -196,7 +248,8 @@ static bool init_registry_data(void)
 
        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) {
                        goto fail;
                }
 
@@ -241,7 +294,7 @@ static bool init_registry_data(void)
 
        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;
@@ -253,7 +306,7 @@ static bool init_registry_data(void)
 
        TALLOC_FREE(frame);
 
-       if (regdb->transaction_cancel(regdb) == -1) {
+       if (regdb->transaction_cancel(regdb) != 0) {
                smb_panic("init_registry_data: tdb_transaction_cancel "
                          "failed\n");
        }
@@ -265,21 +318,24 @@ static bool init_registry_data(void)
  Open the registry database
  ***********************************************************************/
  
-bool regdb_init( void )
+bool regdb_init(void)
 {
        const char *vstring = "INFO/version";
        uint32 vers_id;
 
-       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;
        }
 
-       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) {
                        DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
                                state_path("registry.tdb"), strerror(errno) ));
                        return false;
@@ -293,16 +349,19 @@ bool regdb_init( void )
        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));
-       }
-
-       /* always setup the necessary keys and values */
-
-       if ( !init_registry_data() ) {
-               DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
-               return false;
+               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 false;
+               } else {
+                       DEBUG(10, ("regdb_init: stored %s = %d\n",
+                                 vstring, REGVER_V1));
+               }
        }
 
        return true;
@@ -324,7 +383,8 @@ WERROR regdb_open( void )
        
        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", 
@@ -495,7 +555,7 @@ bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
 
        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;
        }
@@ -613,7 +673,7 @@ bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
                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;
        }
@@ -622,7 +682,7 @@ bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
        return true;
 
 cancel:
-       if (regdb->transaction_cancel(regdb) == -1) {
+       if (regdb->transaction_cancel(regdb) != 0) {
                smb_panic("regdb_store_keys: transaction_cancel failed\n");
        }
 
@@ -647,8 +707,9 @@ int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
        int i;
        fstring subkeyname;
        int ret = -1;
+       int dbret = -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"));
 
@@ -664,16 +725,15 @@ int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
        }
        strupper_m(path);
 
-       rec = regdb->fetch_locked(regdb, frame, string_term_tdb_data(path));
-       if (rec == NULL) {
-               ret = 0;
+       ctr->seqnum = regdb_get_seqnum();
+
+       dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
+       if (dbret != 0) {
                goto fail;
        }
 
-       ctr->seqnum = regdb_get_seqnum();
-
-       buf = rec->value.dptr;
-       buflen = rec->value.dsize;
+       buf = value.dptr;
+       buflen = value.dsize;
 
        if ( !buf ) {
                DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
@@ -783,8 +843,9 @@ int regdb_fetch_values( const char* key, REGVAL_CTR *values )
 {
        char *keystr = NULL;
        TALLOC_CTX *ctx = talloc_stackframe();
-       struct db_record *rec;
        int ret = 0;
+       int dbret = -1;
+       TDB_DATA value;
 
        DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
 
@@ -797,19 +858,19 @@ int regdb_fetch_values( const char* key, REGVAL_CTR *values )
                goto done;
        }
 
-       rec = regdb->fetch_locked(regdb, ctx, string_term_tdb_data(keystr));
-       if (rec == NULL) {
+       values->seqnum = regdb_get_seqnum();
+
+       dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
+       if (dbret != 0) {
                goto done;
        }
 
-       values->seqnum = regdb_get_seqnum();
-
-       if (!rec->value.dptr) {
+       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:
@@ -822,7 +883,8 @@ bool regdb_store_values( const char *key, REGVAL_CTR *values )
        TDB_DATA old_data, data;
        char *keystr = NULL;
        TALLOC_CTX *ctx = talloc_stackframe();
-       int len, ret;
+       int len;
+       NTSTATUS status;
        bool result = false;
 
        DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
@@ -861,9 +923,10 @@ bool regdb_store_values( const char *key, REGVAL_CTR *values )
                goto done;
        }
 
-       ret = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
-                                TDB_REPLACE);
-       result = (ret != -1);
+       status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
+                                   TDB_REPLACE);
+
+       result = NT_STATUS_IS_OK(status);
 
 done:
        TALLOC_FREE(ctx);
@@ -913,6 +976,7 @@ static WERROR regdb_set_secdesc(const char *key,
 {
        TALLOC_CTX *mem_ctx = talloc_stackframe();
        char *tdbkey;
+       NTSTATUS status;
        WERROR err = WERR_NOMEM;
        TDB_DATA tdbdata;
        int tdb_ret;
@@ -927,10 +991,10 @@ static WERROR regdb_set_secdesc(const char *key,
                /* assuming a delete */
                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 {
+               if (tdb_ret == 0) {
                        err = WERR_OK;
+               } else {
+                       err = ntstatus_to_werror(map_nt_error_from_unix(errno));
                }
                goto done;
        }
@@ -942,10 +1006,10 @@ static WERROR regdb_set_secdesc(const char *key,
                goto done;
        }
 
-       tdb_ret = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
-                                    tdbdata, 0);
-       if (tdb_ret == -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;
        }
 
@@ -969,13 +1033,12 @@ bool regdb_values_need_update(REGVAL_CTR *values)
  */
  
 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
 };