registry: free temporary data in regdb_fetch_key_internal().
[ira/wip.git] / source3 / registry / reg_backend_db.c
index 23b59aa284cfa1350851318153818328d8ba206b..d1ec8e4b61a61670960ab365866cb2f788abadc2 100644 (file)
@@ -27,6 +27,8 @@
 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
@@ -91,9 +93,9 @@ static struct builtin_regkey_value builtin_registry_values[] = {
  * Initialize a key in the registry:
  * create each component key of the specified path.
  */
-static bool init_registry_key_internal(const char *add_path)
+static WERROR init_registry_key_internal(const char *add_path)
 {
-       bool ret = false;
+       WERROR werr;
        TALLOC_CTX *frame = talloc_stackframe();
        char *path = NULL;
        char *base = NULL;
@@ -108,6 +110,7 @@ static bool init_registry_key_internal(const char *add_path)
        path = talloc_strdup(frame, add_path);
        base = talloc_strdup(frame, "");
        if (!path || !base) {
+               werr = WERR_NOMEM;
                goto fail;
        }
        p = path;
@@ -119,11 +122,13 @@ static bool init_registry_key_internal(const char *add_path)
                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;
                }
 
@@ -131,11 +136,13 @@ static bool init_registry_key_internal(const char *add_path)
 
                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;
                        }
                        p2 = remaining;
@@ -145,6 +152,7 @@ static bool init_registry_key_internal(const char *add_path)
                        {
                                subkeyname = talloc_strdup(frame,p2);
                                if (!subkeyname) {
+                                       werr = WERR_NOMEM;
                                        goto fail;
                                }
                        }
@@ -160,22 +168,28 @@ static bool init_registry_key_internal(const char *add_path)
 
                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);
+                       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;
                }
        }
 
-       ret = true;
+       werr = WERR_OK;
+
 fail:
        TALLOC_FREE(frame);
-       return ret;
+       return werr;
 }
 
 /**
@@ -183,43 +197,84 @@ fail:
  * create each component key of the specified path,
  * wrapped in one db transaction.
  */
-bool init_registry_key(const char *add_path)
+WERROR init_registry_key(const char *add_path)
 {
-       if (regdb->transaction_start(regdb) == -1) {
+       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 false;
+               return WERR_REG_IO_FAILURE;
        }
 
-       if (!init_registry_key_internal(add_path)) {
+       werr = init_registry_key_internal(add_path);
+       if (!W_ERROR_IS_OK(werr)) {
                goto fail;
        }
 
-       if (regdb->transaction_commit(regdb) == -1) {
+       if (regdb->transaction_commit(regdb) != 0) {
                DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
-               return false;
+               return WERR_REG_IO_FAILURE;
        }
 
-       return true;
+       return WERR_OK;
 
 fail:
-       if (regdb->transaction_cancel(regdb) == -1) {
+       if (regdb->transaction_cancel(regdb) != 0) {
                smb_panic("init_registry_key: transaction_cancel failed\n");
        }
 
-       return false;
+       return werr;
 }
 
 /***********************************************************************
  Open the registry data in the tdb
  ***********************************************************************/
 
-bool init_registry_data(void)
+WERROR init_registry_data(void)
 {
-       TALLOC_CTX *frame = NULL;
+       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(values);
+       }
+
+       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
@@ -228,28 +283,32 @@ bool init_registry_data(void)
         * transaction behaviour.
         */
 
-       if (regdb->transaction_start(regdb) == -1) {
+       if (regdb->transaction_start(regdb) != 0) {
                DEBUG(0, ("init_registry_data: tdb_transaction_start "
                          "failed\n"));
-               return false;
+               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 (!init_registry_key_internal(builtin_registry_paths[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;
                }
        }
 
        /* loop over all of the predefined values and add each component */
 
-       frame = talloc_stackframe();
-
        for (i=0; builtin_registry_values[i].path != NULL; i++) {
 
                values = TALLOC_ZERO_P(frame, REGVAL_CTR);
                if (values == NULL) {
+                       werr = WERR_NOMEM;
                        goto fail;
                }
 
@@ -292,53 +351,54 @@ bool init_registry_data(void)
                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;
+       goto done;
 
- fail:
-
-       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));
                regdb_refcount++;
-               return true;
+               return WERR_OK;
        }
 
-       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) {
-               regdb = db_open(NULL, state_path("registry.tdb"), 0,
-                               REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
+               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"));
@@ -349,12 +409,22 @@ 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));
+               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));
+               }
        }
 
-       return true;
+       return WERR_OK;
 }
 
 /***********************************************************************
@@ -373,7 +443,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", 
@@ -544,7 +615,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;
        }
@@ -662,7 +733,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;
        }
@@ -671,7 +742,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");
        }
 
@@ -682,6 +753,37 @@ fail:
 }
 
 
+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.
@@ -689,7 +791,7 @@ fail:
 
 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
 {
-       char *path = NULL;
+       WERROR werr;
        uint32 num_items;
        uint8 *buf;
        uint32 buflen, len;
@@ -697,32 +799,16 @@ int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
        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) {
-               ret = 0;
-               goto fail;
-       }
-
        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));
@@ -733,7 +819,12 @@ int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
 
        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));
@@ -832,8 +923,8 @@ 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;
+       TDB_DATA value;
 
        DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
 
@@ -841,24 +932,17 @@ int regdb_fetch_values( const char* key, REGVAL_CTR *values )
        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:
@@ -871,7 +955,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));
@@ -910,9 +995,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);
@@ -962,9 +1048,9 @@ 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;
 
        tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
        if (tdbkey == NULL) {
@@ -974,12 +1060,12 @@ static WERROR regdb_set_secdesc(const char *key,
 
        if (secdesc == NULL) {
                /* 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 {
+               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;
        }
@@ -991,10 +1077,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;
        }