Typofix in comment.
[ira/wip.git] / source / libnet / libnet_conf.c
index 3a64c3d84499a12f7a106a04d8c88931cd1422ac..be9edad4e911c462db99138edc680cefdfb71c2a 100644 (file)
@@ -62,42 +62,36 @@ done:
 /*
  * check if a subkey of KEY_SMBCONF of a given name exists
  */
-bool libnet_smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
+bool libnet_smbconf_key_exists(const char *subkeyname)
 {
-       bool ret = False;
+       bool ret = false;
        WERROR werr = WERR_OK;
-       TALLOC_CTX *mem_ctx;
-       struct registry_key *key;
-
-       if (!(mem_ctx = talloc_new(ctx))) {
-               d_fprintf(stderr, "ERROR: Out of memory...!\n");
-               goto done;
-       }
+       TALLOC_CTX *mem_ctx = talloc_stackframe();
+       struct registry_key *key = NULL;
 
        werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
        if (W_ERROR_IS_OK(werr)) {
-               ret = True;
+               ret = true;
        }
 
-done:
        TALLOC_FREE(mem_ctx);
        return ret;
 }
 
-static bool libnet_smbconf_value_exists(TALLOC_CTX *ctx,
-                                       struct registry_key *key,
+static bool libnet_smbconf_value_exists(struct registry_key *key,
                                        const char *param)
 {
-       bool ret = False;
+       bool ret = false;
        WERROR werr = WERR_OK;
+       TALLOC_CTX *ctx = talloc_stackframe();
        struct registry_value *value = NULL;
 
        werr = reg_queryvalue(ctx, key, param, &value);
        if (W_ERROR_IS_OK(werr)) {
-               ret = True;
+               ret = true;
        }
 
-       TALLOC_FREE(value);
+       TALLOC_FREE(ctx);
        return ret;
 }
 
@@ -249,6 +243,73 @@ done:
  *
  **********************************************************************/
 
+/**
+ * Drop the whole configuration (restarting empty).
+ */
+WERROR libnet_smbconf_drop(void)
+{
+       char *path, *p;
+       WERROR werr = WERR_OK;
+       NT_USER_TOKEN *token;
+       struct registry_key *parent_key = NULL;
+       struct registry_key *new_key = NULL;
+       TALLOC_CTX* mem_ctx = talloc_stackframe();
+       enum winreg_CreateAction action;
+
+       if (!(token = registry_create_admin_token(mem_ctx))) {
+               /* what is the appropriate error code here? */
+               werr = WERR_CAN_NOT_COMPLETE;
+               goto done;
+       }
+
+       path = talloc_strdup(mem_ctx, KEY_SMBCONF);
+       if (path == NULL) {
+               werr = WERR_NOMEM;
+               goto done;
+       }
+       p = strrchr(path, '\\');
+       *p = '\0';
+       werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
+
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
+
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
+                            &new_key, &action);
+
+done:
+       TALLOC_FREE(mem_ctx);
+       return werr;
+}
+
+/**
+ * delete a service from configuration
+ */
+WERROR libnet_smbconf_delshare(const char *servicename)
+{
+       WERROR werr = WERR_OK;
+       struct registry_key *key = NULL;
+       TALLOC_CTX *ctx = talloc_stackframe();
+
+       werr = libnet_smbconf_open_basepath(ctx, REG_KEY_WRITE, &key);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
+
+       werr = reg_deletekey_recursive(key, key, servicename);
+
+done:
+       TALLOC_FREE(ctx);
+       return werr;
+}
+
 WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
                              const char *service,
                              const char *param,
@@ -257,17 +318,21 @@ WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
        WERROR werr;
        struct registry_key *key = NULL;
 
-       if (!libnet_smbconf_key_exists(mem_ctx, service)) {
+       if (!libnet_smbconf_key_exists(service)) {
                werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
                                                             &key);
        } else {
                werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
                                                &key);
        }
-       W_ERROR_NOT_OK_RETURN(werr);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
        werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
 
+done:
+       TALLOC_FREE(key);
        return werr;
 }
 
@@ -279,19 +344,25 @@ WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
        WERROR werr;
        struct registry_key *key = NULL;
 
-       if (!libnet_smbconf_key_exists(mem_ctx, service)) {
-               return WERR_NO_SUCH_SERVICE;
+       if (!libnet_smbconf_key_exists(service)) {
+               werr = WERR_NO_SUCH_SERVICE;
+               goto done;
        }
 
        werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
-       W_ERROR_NOT_OK_RETURN(werr);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
-       if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
-               return WERR_INVALID_PARAM;
+       if (!libnet_smbconf_value_exists(key, param)) {
+               werr = WERR_INVALID_PARAM;
+               goto done;
        }
 
        werr = reg_queryvalue(mem_ctx, key, param, value);
 
+done:
+       TALLOC_FREE(key);
        return werr;
 }
 
@@ -302,26 +373,31 @@ WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
        struct registry_key *key = NULL;
        WERROR werr = WERR_OK;
 
-       if (!libnet_smbconf_key_exists(mem_ctx, service)) {
+       if (!libnet_smbconf_key_exists(service)) {
                return WERR_NO_SUCH_SERVICE;
        }
 
        werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
-       W_ERROR_NOT_OK_RETURN(werr);
+       if (!W_ERROR_IS_OK(werr)) {
+               goto done;
+       }
 
-       if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
-               return WERR_INVALID_PARAM;
+       if (!libnet_smbconf_value_exists(key, param)) {
+               werr = WERR_INVALID_PARAM;
+               goto done;
        }
 
        werr = reg_deletevalue(key, param);
 
+done:
+       TALLOC_FREE(key);
        return werr;
 }
 
 
 /**********************************************************************
  *
- * Convenience functions, that are also exportet.
+ * Convenience functions that are also exported.
  *
  **********************************************************************/