Make grouping in if statement more explicit.
[ira/wip.git] / source / utils / net_conf.c
index eb6398f5b97eef3381d55752a4557c5687aabb5b..07eb3b890ffec3af272320961952b6f2704005d8 100644 (file)
@@ -110,64 +110,6 @@ static int net_conf_delparm_usage(int argc, const char **argv)
  * Helper functions
  */
 
-static char *format_value(TALLOC_CTX *mem_ctx, struct registry_value *value)
-{
-       char *result = NULL;
-
-       /* what if mem_ctx = NULL? */
-
-       switch (value->type) {
-       case REG_DWORD:
-               result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
-               break;
-       case REG_SZ:
-       case REG_EXPAND_SZ:
-               result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
-               break;
-       case REG_MULTI_SZ: {
-                uint32 j;
-                for (j = 0; j < value->v.multi_sz.num_strings; j++) {
-                        result = talloc_asprintf(mem_ctx, "\"%s\" ",
-                                                value->v.multi_sz.strings[j]);
-                }
-                break;
-        }
-       case REG_BINARY:
-                result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
-                                        (int)value->v.binary.length);
-                break;
-        default:
-                result = talloc_asprintf(mem_ctx, "<unprintable>");
-                break;
-        }
-       return result;
-}
-
-static WERROR list_values(TALLOC_CTX *ctx, struct registry_key *key)
-{
-       WERROR werr = WERR_OK;
-       uint32 idx = 0;
-       struct registry_value *valvalue = NULL;
-       char *valname = NULL;
-
-       for (idx = 0;
-            W_ERROR_IS_OK(werr = reg_enumvalue(ctx, key, idx, &valname,
-                                               &valvalue));
-            idx++)
-       {
-               d_printf("\t%s = %s\n", valname, format_value(ctx, valvalue));
-       }
-       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
-                d_fprintf(stderr, "Error enumerating values: %s\n",
-                          dos_errstr(werr));
-               goto done;
-        }
-       werr = WERR_OK;
-
-done:
-       return werr;
-}
-
 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
                         struct share_params *share)
 {
@@ -249,7 +191,6 @@ static int import_process_service(TALLOC_CTX *ctx,
        struct parm_struct *parm;
        int pnum = 0;
        const char *servicename;
-       struct registry_key *key;
        WERROR werr;
        char *valstr = NULL;
        TALLOC_CTX *tmp_ctx = NULL;
@@ -266,23 +207,21 @@ static int import_process_service(TALLOC_CTX *ctx,
        if (opt_testmode) {
                d_printf("[%s]\n", servicename);
        } else {
-               if (libnet_smbconf_key_exists(servicename)) {
+               if (libnet_smbconf_share_exists(servicename)) {
                        werr = libnet_smbconf_delshare(servicename);
                        if (!W_ERROR_IS_OK(werr)) {
                                goto done;
                        }
                }
-               werr = libnet_smbconf_reg_createkey_internal(tmp_ctx, servicename, &key);
-               if (!W_ERROR_IS_OK(werr)) {
-                       goto done;
-               }
        }
 
        while ((parm = lp_next_parameter(share->service, &pnum, 0)))
        {
-               if ((share->service < 0 && parm->p_class == P_LOCAL)
+               if ((share->service < 0) && (parm->p_class == P_LOCAL)
                    && !(parm->flags & FLAG_GLOBAL))
+               {
                        continue;
+               }
 
                valstr = parm_valstr(tmp_ctx, parm, share);
 
@@ -290,9 +229,14 @@ static int import_process_service(TALLOC_CTX *ctx,
                        if (opt_testmode) {
                                d_printf("\t%s = %s\n", parm->label, valstr);
                        } else {
-                               werr = libnet_smbconf_reg_setvalue_internal(key,
-                                                       parm->label, valstr);
+                               werr = libnet_smbconf_setparm(servicename,
+                                                             parm->label,
+                                                             valstr);
                                if (!W_ERROR_IS_OK(werr)) {
+                                       d_fprintf(stderr,
+                                                 "Error setting parameter '%s'"
+                                                 ": %s\n", parm->label,
+                                                  dos_errstr(werr));
                                        goto done;
                                }
                        }
@@ -328,15 +272,17 @@ static bool globals_exist(void)
  * the conf functions
  */
 
-int net_conf_list(int argc, const char **argv)
+static int net_conf_list(int argc, const char **argv)
 {
        WERROR werr = WERR_OK;
        int ret = -1;
        TALLOC_CTX *ctx;
-       struct registry_key *base_key = NULL;
-       struct registry_key *sub_key = NULL;
-       uint32 idx_key = 0;
-       char *subkey_name = NULL;
+       uint32_t num_shares;
+       char **share_names;
+       uint32_t *num_params;
+       char ***param_names;
+       char ***param_values;
+       uint32_t share_count, param_count;
 
        ctx = talloc_init("list");
 
@@ -345,54 +291,26 @@ int net_conf_list(int argc, const char **argv)
                goto done;
        }
 
-       werr = libnet_smbconf_open_basepath(ctx, REG_KEY_READ, &base_key);
+       werr = libnet_smbconf_get_config(ctx, &num_shares, &share_names,
+                                        &num_params, &param_names,
+                                        &param_values);
        if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "Error getting config: %s\n",
+                         dos_errstr(werr));
                goto done;
        }
 
-       if (libnet_smbconf_key_exists(GLOBAL_NAME))  {
-               werr = reg_openkey(ctx, base_key, GLOBAL_NAME,
-                                  REG_KEY_READ, &sub_key);
-               if (!W_ERROR_IS_OK(werr)) {
-                       d_fprintf(stderr, "Error opening subkey '%s' : %s\n",
-                                 subkey_name, dos_errstr(werr));
-                       goto done;
-               }
-               d_printf("[%s]\n", GLOBAL_NAME);
-               if (!W_ERROR_IS_OK(list_values(ctx, sub_key))) {
-                       goto done;
-               }
-               d_printf("\n");
-       }
-
-       for (idx_key = 0;
-            W_ERROR_IS_OK(werr = reg_enumkey(ctx, base_key, idx_key,
-                                             &subkey_name, NULL));
-            idx_key++)
-       {
-               if (strequal(subkey_name, GLOBAL_NAME)) {
-                       continue;
-               }
-               d_printf("[%s]\n", subkey_name);
-
-               werr = reg_openkey(ctx, base_key, subkey_name,
-                                  REG_KEY_READ, &sub_key);
-               if (!W_ERROR_IS_OK(werr)) {
-                       d_fprintf(stderr,
-                                 "Error opening subkey '%s': %s\n",
-                                 subkey_name, dos_errstr(werr));
-                       goto done;
-               }
-               if (!W_ERROR_IS_OK(list_values(ctx, sub_key))) {
-                       goto done;
+       for (share_count = 0; share_count < num_shares; share_count++) {
+               d_printf("[%s]\n", share_names[share_count]);
+               for (param_count = 0; param_count < num_params[share_count];
+                    param_count++)
+               {
+                       d_printf("\t%s = %s\n",
+                                param_names[share_count][param_count],
+                                param_values[share_count][param_count]);
                }
                d_printf("\n");
        }
-       if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
-               d_fprintf(stderr, "Error enumerating subkeys: %s\n",
-                         dos_errstr(werr));
-               goto done;
-       }
 
        ret = 0;
 
@@ -401,7 +319,7 @@ done:
        return ret;
 }
 
-int net_conf_import(int argc, const char **argv)
+static int net_conf_import(int argc, const char **argv)
 {
        int ret = -1;
        const char *filename = NULL;
@@ -487,13 +405,12 @@ done:
        return ret;
 }
 
-int net_conf_listshares(int argc, const char **argv)
+static int net_conf_listshares(int argc, const char **argv)
 {
        WERROR werr = WERR_OK;
        int ret = -1;
-       struct registry_key *key;
-       uint32 idx = 0;
-       char *subkey_name = NULL;
+       uint32_t count, num_shares = 0;
+       char **share_names = NULL;
        TALLOC_CTX *ctx;
 
        ctx = talloc_init("listshares");
@@ -503,22 +420,14 @@ int net_conf_listshares(int argc, const char **argv)
                goto done;
        }
 
-       werr = libnet_smbconf_open_basepath(ctx, SEC_RIGHTS_ENUM_SUBKEYS, &key);
+       werr = libnet_smbconf_get_share_names(ctx, &num_shares, &share_names);
        if (!W_ERROR_IS_OK(werr)) {
                goto done;
        }
 
-       for (idx = 0;
-            W_ERROR_IS_OK(werr = reg_enumkey(ctx, key, idx,
-                                             &subkey_name, NULL));
-            idx++)
+       for (count = 0; count < num_shares; count++)
        {
-               d_printf("%s\n", subkey_name);
-       }
-       if (! W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
-               d_fprintf(stderr, "Error enumerating subkeys: %s\n",
-                         dos_errstr(werr));
-               goto done;
+               d_printf("%s\n", share_names[count]);
        }
 
        ret = 0;
@@ -528,7 +437,7 @@ done:
        return ret;
 }
 
-int net_conf_drop(int argc, const char **argv)
+static int net_conf_drop(int argc, const char **argv)
 {
        int ret = -1;
        WERROR werr;
@@ -551,12 +460,16 @@ done:
        return ret;
 }
 
-int net_conf_showshare(int argc, const char **argv)
+static int net_conf_showshare(int argc, const char **argv)
 {
        int ret = -1;
        WERROR werr = WERR_OK;
-       struct registry_key *key = NULL;
+       const char *sharename = NULL;
        TALLOC_CTX *ctx;
+       uint32_t num_params;
+       uint32_t count;
+       char **param_names;
+       char **param_values;
 
        ctx = talloc_init("showshare");
 
@@ -565,15 +478,21 @@ int net_conf_showshare(int argc, const char **argv)
                goto done;
        }
 
-       werr = libnet_smbconf_open_path(ctx, argv[0], REG_KEY_READ, &key);
+       sharename = argv[0];
+
+       werr = libnet_smbconf_getshare(ctx, sharename, &num_params,
+                                      &param_names, &param_values);
        if (!W_ERROR_IS_OK(werr)) {
+               d_printf("error getting share parameters: %s\n",
+                        dos_errstr(werr));
                goto done;
        }
 
-       d_printf("[%s]\n", argv[0]);
+       d_printf("[%s]\n", sharename);
 
-       if (!W_ERROR_IS_OK(list_values(ctx, key))) {
-               goto done;
+       for (count = 0; count < num_params; count++) {
+               d_printf("\t%s = %s\n", param_names[count],
+                        param_values[count]);
        }
 
        ret = 0;
@@ -583,11 +502,16 @@ done:
        return ret;
 }
 
-int net_conf_addshare(int argc, const char **argv)
+/**
+ * Add a share, with a couple of standard parameters, partly optional.
+ *
+ * This is a high level utility function of the net conf utility,
+ * not a direct frontend to the libnet_conf API.
+ */
+static int net_conf_addshare(int argc, const char **argv)
 {
        int ret = -1;
        WERROR werr = WERR_OK;
-       struct registry_key *newkey = NULL;
        char *sharename = NULL;
        const char *path = NULL;
        const char *comment = NULL;
@@ -639,7 +563,6 @@ int net_conf_addshare(int argc, const char **argv)
                                        net_conf_addshare_usage(argc, argv);
                                        goto done;
                        }
-
                case 2:
                        path = argv[1];
                        sharename = strdup_lower(argv[0]);
@@ -673,6 +596,12 @@ int net_conf_addshare(int argc, const char **argv)
                goto done;
        }
 
+       if (libnet_smbconf_share_exists(sharename)) {
+               d_fprintf(stderr, "ERROR: share %s already exists.\n",
+                         sharename);
+               goto done;
+       }
+
        /* validate path */
 
        if (path[0] != '/') {
@@ -699,46 +628,47 @@ int net_conf_addshare(int argc, const char **argv)
        }
 
        /*
-        * create the share
+        * create the share by adding the parameters
         */
 
-       werr = libnet_smbconf_reg_createkey_internal(NULL, argv[0], &newkey);
+       werr = libnet_smbconf_setparm(sharename, "path", path);
        if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "Error setting parameter %s: %s\n",
+                         "path", dos_errstr(werr));
                goto done;
        }
 
-       /* add config params as values */
-
-       werr = libnet_smbconf_reg_setvalue_internal(newkey, "path", path);
-       if (!W_ERROR_IS_OK(werr))
-               goto done;
-
        if (comment != NULL) {
-               werr = libnet_smbconf_reg_setvalue_internal(newkey, "comment",
-                                                           comment);
-               if (!W_ERROR_IS_OK(werr))
+               werr = libnet_smbconf_setparm(sharename, "comment", comment);
+               if (!W_ERROR_IS_OK(werr)) {
+                       d_fprintf(stderr, "Error setting parameter %s: %s\n",
+                                 "comment", dos_errstr(werr));
                        goto done;
+               }
        }
 
-       werr = libnet_smbconf_reg_setvalue_internal(newkey, "guest ok",
-                                                   guest_ok);
-       if (!W_ERROR_IS_OK(werr))
+       werr = libnet_smbconf_setparm(sharename, "guest ok", guest_ok);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "Error setting parameter %s: %s\n",
+                         "'guest ok'", dos_errstr(werr));
                goto done;
+       }
 
-       werr = libnet_smbconf_reg_setvalue_internal(newkey, "writeable",
-                                                   writeable);
-       if (!W_ERROR_IS_OK(werr))
+       werr = libnet_smbconf_setparm(sharename, "writeable", writeable);
+       if (!W_ERROR_IS_OK(werr)) {
+               d_fprintf(stderr, "Error setting parameter %s: %s\n",
+                         "writeable", dos_errstr(werr));
                goto done;
+       }
 
        ret = 0;
 
 done:
-       TALLOC_FREE(newkey);
        SAFE_FREE(sharename);
        return ret;
 }
 
-int net_conf_delshare(int argc, const char **argv)
+static int net_conf_delshare(int argc, const char **argv)
 {
        int ret = -1;
        const char *sharename = NULL;
@@ -790,6 +720,7 @@ static int net_conf_setparm(int argc, const char **argv)
 
 done:
        SAFE_FREE(service);
+       SAFE_FREE(param);
        return ret;
 }
 
@@ -799,7 +730,7 @@ static int net_conf_getparm(int argc, const char **argv)
        WERROR werr = WERR_OK;
        char *service = NULL;
        char *param = NULL;
-       struct registry_value *value = NULL;
+       char *valstr = NULL;
        TALLOC_CTX *ctx;
 
        ctx = talloc_init("getparm");
@@ -811,7 +742,7 @@ static int net_conf_getparm(int argc, const char **argv)
        service = strdup_lower(argv[0]);
        param = strdup_lower(argv[1]);
 
-       werr = libnet_smbconf_getparm(ctx, service, param, &value);
+       werr = libnet_smbconf_getparm(ctx, service, param, &valstr);
 
        if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
                d_fprintf(stderr,
@@ -829,7 +760,7 @@ static int net_conf_getparm(int argc, const char **argv)
                goto done;
        }
 
-       d_printf("%s\n", format_value(ctx, value));
+       d_printf("%s\n", valstr);
 
        ret = 0;
 done:
@@ -845,9 +776,6 @@ static int net_conf_delparm(int argc, const char **argv)
        WERROR werr = WERR_OK;
        char *service = NULL;
        char *param = NULL;
-       TALLOC_CTX *ctx;
-
-       ctx = talloc_init("delparm");
 
        if (argc != 2) {
                net_conf_delparm_usage(argc, argv);
@@ -856,7 +784,7 @@ static int net_conf_delparm(int argc, const char **argv)
        service = strdup_lower(argv[0]);
        param = strdup_lower(argv[1]);
 
-       werr = libnet_smbconf_delparm(ctx, service, param);
+       werr = libnet_smbconf_delparm(service, param);
 
        if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
                d_fprintf(stderr,
@@ -877,6 +805,8 @@ static int net_conf_delparm(int argc, const char **argv)
        ret = 0;
 
 done:
+       SAFE_FREE(service);
+       SAFE_FREE(param);
        return ret;
 }