s3:registry: wrap reg_createkey() in a transaction
authorMichael Adam <obnox@samba.org>
Thu, 12 Apr 2012 20:17:35 +0000 (22:17 +0200)
committerKarolin Seeger <kseeger@samba.org>
Thu, 10 May 2012 09:15:25 +0000 (11:15 +0200)
This is wrong layering (calling into regdb_transaction* in the reg_api code)
but fixes a potential race. It makes the multi-step create procedure atomic.

This should completely be done in the backend.
(cherry picked from commit 65d9b116d0283b010e9e3c9ecf185ca42850838e)

source3/registry/reg_api.c

index b8d8f2cc29f6873fbf682626f6f880b7eef9850d..c7777a120f3a7e89be0624feedeeeb9f00f2808b 100644 (file)
@@ -579,6 +579,13 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
                goto done;
        }
 
+       err = regdb_transaction_start();
+       if (!W_ERROR_IS_OK(err)) {
+               DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
+                         win_errstr(err)));
+               goto done;
+       }
+
        while ((end = strchr(path, '\\')) != NULL) {
                struct registry_key *tmp;
                enum winreg_CreateAction action;
@@ -588,7 +595,7 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
                err = reg_createkey(mem_ctx, key, path,
                                    KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
                if (!W_ERROR_IS_OK(err)) {
-                       goto done;
+                       goto trans_done;
                }
 
                if (key != parent) {
@@ -609,14 +616,14 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
                if (paction != NULL) {
                        *paction = REG_OPENED_EXISTING_KEY;
                }
-               goto done;
+               goto trans_done;
        }
 
        if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
                /*
                 * Something but "notfound" has happened, so bail out
                 */
-               goto done;
+               goto trans_done;
        }
 
        /*
@@ -627,7 +634,7 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
        err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
                          &create_parent);
        if (!W_ERROR_IS_OK(err)) {
-               goto done;
+               goto trans_done;
        }
 
        /*
@@ -635,10 +642,14 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
         */
 
        err = fill_subkey_cache(create_parent);
-       if (!W_ERROR_IS_OK(err)) goto done;
+       if (!W_ERROR_IS_OK(err)) {
+               goto trans_done;
+       }
 
        err = create_reg_subkey(key->key, path);
-       W_ERROR_NOT_OK_GOTO_DONE(err);
+       if (!W_ERROR_IS_OK(err)) {
+               goto trans_done;
+       }
 
        /*
         * Now open the newly created key
@@ -649,6 +660,19 @@ WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
                *paction = REG_CREATED_NEW_KEY;
        }
 
+trans_done:
+       if (W_ERROR_IS_OK(err)) {
+               err = regdb_transaction_commit();
+               if (!W_ERROR_IS_OK(err)) {
+                       DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
+               }
+       } else {
+               WERROR err1 = regdb_transaction_cancel();
+               if (!W_ERROR_IS_OK(err1)) {
+                       DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
+               }
+       }
+
  done:
        TALLOC_FREE(mem_ctx);
        return err;