libsmbconf: Convert smbconf_drop() to sbcErr.
[samba.git] / source3 / lib / smbconf / smbconf_reg.c
index ba27b482067ad2d158e179dc61b58c22ab89ff5f..844a9b6b483bf218e43d7f330a3c8d7ff13aeb35 100644 (file)
 #include "includes.h"
 #include "lib/smbconf/smbconf_private.h"
 #include "registry.h"
+#include "registry/reg_api.h"
 #include "registry/reg_backend_db.h"
 #include "registry/reg_util_token.h"
+#include "registry/reg_api_util.h"
+#include "registry/reg_init_smbconf.h"
 #include "lib/smbconf/smbconf_init.h"
 #include "lib/smbconf/smbconf_reg.h"
+#include "../libcli/registry/util_reg.h"
 
 #define INCLUDES_VALNAME "includes"
 
@@ -565,9 +569,10 @@ done:
 /**
  * initialize the registry smbconf backend
  */
-static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
+static sbcErr smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
 {
        WERROR werr = WERR_OK;
+       sbcErr err;
        struct security_token *token;
 
        if (path == NULL) {
@@ -584,17 +589,19 @@ static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
        werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
        if (!W_ERROR_IS_OK(werr)) {
                DEBUG(1, ("Error creating admin token\n"));
+               err = SBC_ERR_UNKNOWN_FAILURE;
                goto done;
        }
        rpd(ctx)->open = false;
 
        werr = registry_init_smbconf(path);
        if (!W_ERROR_IS_OK(werr)) {
+               err = SBC_ERR_BADFILE;
                goto done;
        }
 
-       werr = ctx->ops->open_conf(ctx);
-       if (!W_ERROR_IS_OK(werr)) {
+       err = ctx->ops->open_conf(ctx);
+       if (!SBC_ERROR_IS_OK(err)) {
                DEBUG(1, ("Error opening the registry.\n"));
                goto done;
        }
@@ -603,11 +610,12 @@ static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
                             KEY_ENUMERATE_SUB_KEYS | REG_KEY_WRITE,
                             token, &rpd(ctx)->base_key);
        if (!W_ERROR_IS_OK(werr)) {
+               err = SBC_ERR_UNKNOWN_FAILURE;
                goto done;
        }
 
 done:
-       return werr;
+       return err;
 }
 
 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
@@ -636,19 +644,21 @@ static bool smbconf_reg_is_writeable(struct smbconf_ctx *ctx)
        return true;
 }
 
-static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
+static sbcErr smbconf_reg_open(struct smbconf_ctx *ctx)
 {
        WERROR werr;
 
        if (rpd(ctx)->open) {
-               return WERR_OK;
+               return SBC_ERR_OK;
        }
 
        werr = regdb_open();
-       if (W_ERROR_IS_OK(werr)) {
-               rpd(ctx)->open = true;
+       if (!W_ERROR_IS_OK(werr)) {
+               return SBC_ERR_BADFILE;
        }
-       return werr;
+
+       rpd(ctx)->open = true;
+       return SBC_ERR_OK;
 }
 
 static int smbconf_reg_close(struct smbconf_ctx *ctx)
@@ -678,7 +688,7 @@ static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
                return;
        }
 
-       if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
+       if (!SBC_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
                return;
        }
 
@@ -688,10 +698,11 @@ static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
 /**
  * Drop the whole configuration (restarting empty) - registry version
  */
-static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
+static sbcErr smbconf_reg_drop(struct smbconf_ctx *ctx)
 {
        char *path, *p;
        WERROR werr = WERR_OK;
+       sbcErr err = SBC_ERR_OK;
        struct registry_key *parent_key = NULL;
        struct registry_key *new_key = NULL;
        TALLOC_CTX* mem_ctx = talloc_stackframe();
@@ -701,35 +712,44 @@ static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
        werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
        if (!W_ERROR_IS_OK(werr)) {
                DEBUG(1, ("Error creating admin token\n"));
+               err = SBC_ERR_UNKNOWN_FAILURE;
                goto done;
        }
 
        path = talloc_strdup(mem_ctx, ctx->path);
        if (path == NULL) {
-               werr = WERR_NOMEM;
+               err = SBC_ERR_NOMEM;
                goto done;
        }
        p = strrchr(path, '\\');
+       if (p == NULL) {
+               err = SBC_ERR_INVALID_PARAM;
+               goto done;
+       }
        *p = '\0';
        werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token,
                             &parent_key);
-
        if (!W_ERROR_IS_OK(werr)) {
+               err = SBC_ERR_IO_FAILURE;
                goto done;
        }
 
-       werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
-
+       werr = reg_deletekey_recursive(parent_key, p+1);
        if (!W_ERROR_IS_OK(werr)) {
+               err = SBC_ERR_IO_FAILURE;
                goto done;
        }
 
        werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
                             &new_key, &action);
+       if (!W_ERROR_IS_OK(werr)) {
+               err = SBC_ERR_IO_FAILURE;
+               goto done;
+       }
 
 done:
        talloc_free(mem_ctx);
-       return werr;
+       return err;
 }
 
 /**
@@ -910,8 +930,7 @@ static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
        TALLOC_CTX *mem_ctx = talloc_stackframe();
 
        if (servicename != NULL) {
-               werr = reg_deletekey_recursive(mem_ctx, rpd(ctx)->base_key,
-                                              servicename);
+               werr = reg_deletekey_recursive(rpd(ctx)->base_key, servicename);
        } else {
                werr = smbconf_reg_delete_values(rpd(ctx)->base_key);
        }
@@ -1149,7 +1168,7 @@ struct smbconf_ops smbconf_ops_reg = {
  * initialize the smbconf registry backend
  * the only function that is exported from this module
  */
-WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
+sbcErr smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
                        const char *path)
 {
        return smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_reg);