s4:registry: Fix size type and loop
[metze/samba/wip.git] / source4 / lib / registry / tools / regshell.c
index 003cbd15e07aaafab222cc573aaf6063929c73c9..48251c33ea4a038f22b80749067c8b3c9c3bd8c9 100644 (file)
@@ -24,7 +24,7 @@
 #include "lib/cmdline/popt_common.h"
 #include "lib/events/events.h"
 #include "system/time.h"
-#include "lib/smbreadline/smbreadline.h"
+#include "../libcli/smbreadline/smbreadline.h"
 #include "librpc/gen_ndr/ndr_security.h"
 #include "lib/registry/tools/common.h"
 #include "param/param.h"
@@ -37,9 +37,9 @@ struct regshell_context {
        struct registry_key *root;
 };
 
-static WERROR get_full_path(struct regshell_context *ctx, char *path, char **ret_path) 
+static WERROR get_full_path(struct regshell_context *ctx, const char *path, char **ret_path)
 {
-       char *dir;
+       const char *dir;
        char *tmp;
        char *new_path;
 
@@ -49,7 +49,7 @@ static WERROR get_full_path(struct regshell_context *ctx, char *path, char **ret
                new_path = talloc_strdup(ctx, ctx->path);
        }               
 
-       dir = strtok(path, "\\");
+       dir = strtok(discard_const_p(char, path), "\\");
        if (dir == NULL) {
                *ret_path = new_path;
                return WERR_OK;
@@ -98,7 +98,7 @@ static WERROR get_full_path(struct regshell_context *ctx, char *path, char **ret
  * exit
  */
 
-static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_info(struct regshell_context *ctx, int argc, const char **argv)
 {
        struct security_descriptor *sec_desc = NULL;
        time_t last_mod;
@@ -125,7 +125,7 @@ static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
        if (classname != NULL)
                printf("Key Class: %s\n", classname);
        last_mod = nt_time_to_unix(last_change);
-       printf("Time Last Modified: %s\n", ctime(&last_mod));
+       printf("Time Last Modified: %s", ctime(&last_mod));
        printf("Number of subkeys: %d\n", num_subkeys);
        printf("Number of values: %d\n", num_values);
 
@@ -140,8 +140,8 @@ static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
 
        error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
        if (!W_ERROR_IS_OK(error)) {
-               printf("Error getting security descriptor\n");
-               return error;
+               printf("Error getting security descriptor: %s\n", win_errstr(error));
+               return WERR_OK;
        }
        ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor,
                        "Security", sec_desc);
@@ -150,7 +150,7 @@ static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
        return WERR_OK;
 }
 
-static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_predef(struct regshell_context *ctx, int argc, const char **argv)
 {
        struct registry_key *ret = NULL;
        if (argc < 2) {
@@ -176,7 +176,7 @@ static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
 }
 
 static WERROR cmd_pwd(struct regshell_context *ctx,
-                     int argc, char **argv)
+                     int argc, const char **argv)
 {
        if (ctx->predef) {
                printf("%s\\", ctx->predef);
@@ -185,21 +185,19 @@ static WERROR cmd_pwd(struct regshell_context *ctx,
        return WERR_OK;
 }
 
-static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_set(struct regshell_context *ctx, int argc, const char **argv)
 {
        struct registry_value val;
        WERROR error;
 
        if (argc < 4) {
                fprintf(stderr, "Usage: set value-name type value\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
-       if (!reg_string_to_val(ctx, lp_iconv_convenience(cmdline_lp_ctx), 
-                              argv[2], argv[3], &val.data_type,
-                              &val.data)) {
+       if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type, &val.data)) {
                fprintf(stderr, "Unable to interpret data\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
        error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
@@ -211,7 +209,7 @@ static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
        return WERR_OK;
 }
 
-static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_ck(struct regshell_context *ctx, int argc, const char **argv)
 {
        struct registry_key *nkey = NULL;
        char *full_path;
@@ -220,7 +218,7 @@ static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
        if(argc == 2) {
                if (!W_ERROR_IS_OK(get_full_path(ctx, argv[1], &full_path))) {
                        fprintf(stderr, "Unable to parse the supplied path\n");
-                       return WERR_INVALID_PARAM;
+                       return WERR_INVALID_PARAMETER;
                }
                error = reg_open_key(ctx->registry, ctx->root, full_path,
                                     &nkey);
@@ -240,7 +238,7 @@ static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
        return WERR_OK;
 }
 
-static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_print(struct regshell_context *ctx, int argc, const char **argv)
 {
        uint32_t value_type;
        DATA_BLOB value_data;
@@ -248,7 +246,7 @@ static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
 
        if (argc != 2) {
                fprintf(stderr, "Usage: print <valuename>\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
        error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],
@@ -259,12 +257,12 @@ static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
        }
 
        printf("%s\n%s\n", str_regtype(value_type),
-                  reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), value_type, value_data));
+                  reg_val_data_string(ctx, value_type, value_data));
 
        return WERR_OK;
 }
 
-static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_ls(struct regshell_context *ctx, int argc, const char **argv)
 {
        unsigned int i;
        WERROR error;
@@ -290,18 +288,18 @@ static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
        for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx,
                ctx->current, i, &name, &valuetype, &valuedata)); i++)
                printf("V \"%s\" %s %s\n", name, str_regtype(valuetype),
-                          reg_val_data_string(ctx, lp_iconv_convenience(cmdline_lp_ctx), valuetype, valuedata));
+                          reg_val_data_string(ctx, valuetype, valuedata));
 
        return WERR_OK;
 }
-static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, const char **argv)
 {
        struct registry_key *tmp;
        WERROR error;
 
        if(argc < 2) {
                fprintf(stderr, "Usage: mkkey <keyname>\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
        error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
@@ -316,16 +314,16 @@ static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
 }
 
 static WERROR cmd_rmkey(struct regshell_context *ctx,
-                       int argc, char **argv)
+                       int argc, const char **argv)
 {
        WERROR error;
 
        if(argc < 2) {
                fprintf(stderr, "Usage: rmkey <name>\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
-       error = reg_key_del(ctx->current, argv[1]);
+       error = reg_key_del(ctx, ctx->current, argv[1]);
        if(!W_ERROR_IS_OK(error)) {
                fprintf(stderr, "Error deleting '%s'\n", argv[1]);
                return error;
@@ -336,16 +334,16 @@ static WERROR cmd_rmkey(struct regshell_context *ctx,
        return WERR_OK;
 }
 
-static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
+static WERROR cmd_rmval(struct regshell_context *ctx, int argc, const char **argv)
 {
        WERROR error;
 
        if(argc < 2) {
                fprintf(stderr, "Usage: rmval <valuename>\n");
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
-       error = reg_del_value(ctx->current, argv[1]);
+       error = reg_del_value(ctx, ctx->current, argv[1]);
        if(!W_ERROR_IS_OK(error)) {
                fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
                return error;
@@ -357,18 +355,18 @@ static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
 }
 
 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx,
-                                 int argc, char **argv)
+                                 int argc, const char **argv)
 {
        exit(0);
 }
 
-static WERROR cmd_help(struct regshell_context *ctx, int, char **);
+static WERROR cmd_help(struct regshell_context *ctx, int, const char **);
 
 static struct {
        const char *name;
        const char *alias;
        const char *help;
-       WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
+       WERROR (*handle)(struct regshell_context *ctx, int argc, const char **argv);
 } regshell_cmds[] = {
        {"ck", "cd", "Change current key", cmd_ck },
        {"info", "i", "Show detailed information of a key", cmd_info },
@@ -386,7 +384,7 @@ static struct {
 };
 
 static WERROR cmd_help(struct regshell_context *ctx,
-                      int argc, char **argv)
+                      int argc, const char **argv)
 {
        unsigned int i;
        printf("Available commands:\n");
@@ -401,12 +399,12 @@ static WERROR process_cmd(struct regshell_context *ctx,
                          char *line)
 {
        int argc;
-       char **argv = NULL;
+       const char **argv = NULL;
        int ret, i;
 
-       if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
+       if ((ret = poptParseArgvString(line, &argc, &argv)) != 0) {
                fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
-               return WERR_INVALID_PARAM;
+               return WERR_INVALID_PARAMETER;
        }
 
        for(i = 0; regshell_cmds[i].name; i++) {
@@ -418,7 +416,7 @@ static WERROR process_cmd(struct regshell_context *ctx,
 
        fprintf(stderr, "No such command '%s'\n", argv[0]);
 
-       return WERR_INVALID_PARAM;
+       return WERR_INVALID_PARAMETER;
 }
 
 #define MAX_COMPLETIONS 100
@@ -430,7 +428,7 @@ static char **reg_complete_command(const char *text, int start, int end)
        /* Complete command */
        char **matches;
        size_t len, samelen=0;
-       unsigned int i, count=1;
+       size_t i, count = 1;
 
        matches = malloc_array_p(char *, MAX_COMPLETIONS);
        if (!matches) return NULL;
@@ -465,10 +463,8 @@ static char **reg_complete_command(const char *text, int start, int end)
        return matches;
 
 cleanup:
-       count--;
-       while (count >= 0) {
-               free(matches[count]);
-               count--;
+       for (i = 0; i < count; i++) {
+               free(matches[i]);
        }
        free(matches);
        return NULL;
@@ -484,6 +480,7 @@ static char **reg_complete_key(const char *text, int start, int end)
        const char *base_n = "";
        TALLOC_CTX *mem_ctx;
        WERROR status;
+       int ret;
 
        matches = malloc_array_p(char *, MAX_COMPLETIONS);
        if (!matches) return NULL;
@@ -510,8 +507,15 @@ static char **reg_complete_key(const char *text, int start, int end)
                } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
                        break;
                } else {
+                       int n;
+
                        printf("Error creating completion list: %s\n",
                                win_errstr(status));
+
+                       for (n = j; n >= 0; n--) {
+                               SAFE_FREE(matches[n]);
+                       }
+                       SAFE_FREE(matches);
                        talloc_free(mem_ctx);
                        return NULL;
                }
@@ -524,12 +528,16 @@ static char **reg_complete_key(const char *text, int start, int end)
        }
 
        if (j == 2) { /* Exact match */
-               asprintf(&matches[0], "%s%s", base_n, matches[1]);
+               ret = asprintf(&matches[0], "%s%s", base_n, matches[1]);
        } else {
-               asprintf(&matches[0], "%s%s", base_n,
+               ret = asprintf(&matches[0], "%s%s", base_n,
                                talloc_strndup(mem_ctx, matches[1], samelen));
        }
        talloc_free(mem_ctx);
+       if (ret == -1) {
+               SAFE_FREE(matches);
+               return NULL;
+       }
 
        matches[j] = NULL;
        return matches;
@@ -546,7 +554,7 @@ static char **reg_completion(const char *text, int start, int end)
        }
 }
 
-int main(int argc, char **argv)
+int main(int argc, const char **argv)
 {
        int opt;
        const char *file = NULL;
@@ -565,7 +573,7 @@ int main(int argc, char **argv)
                { NULL }
        };
 
-       pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
+       pc = poptGetContext(argv[0], argc, argv, long_options,0);
 
        while((opt = poptGetNextOpt(pc)) != -1) {
        }
@@ -576,9 +584,12 @@ int main(int argc, char **argv)
 
        if (remote != NULL) {
                ctx->registry = reg_common_open_remote(remote, ev_ctx,
-                                        cmdline_lp_ctx, cmdline_credentials);
+                                        cmdline_lp_ctx,
+                                       popt_get_cmdline_credentials());
        } else if (file != NULL) {
-               ctx->current = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
+               ctx->current = reg_common_open_file(file, ev_ctx,
+                                       cmdline_lp_ctx,
+                                       popt_get_cmdline_credentials());
                if (ctx->current == NULL)
                        return 1;
                ctx->registry = ctx->current->context;
@@ -586,7 +597,9 @@ int main(int argc, char **argv)
                ctx->predef = NULL;
                ctx->root = ctx->current;
        } else {
-               ctx->registry = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
+               ctx->registry = reg_common_open_local(
+                                       popt_get_cmdline_credentials(),
+                                       ev_ctx, cmdline_lp_ctx);
        }
 
        if (ctx->registry == NULL)
@@ -616,7 +629,7 @@ int main(int argc, char **argv)
 
        if (ctx->current == NULL) {
                fprintf(stderr, "Unable to access any of the predefined keys\n");
-               return -1;
+               return 1;
        }
 
        poptFreeContext(pc);
@@ -624,7 +637,11 @@ int main(int argc, char **argv)
        while (true) {
                char *line, *prompt;
 
-               asprintf(&prompt, "%s\\%s> ", ctx->predef?ctx->predef:"", ctx->path);
+               if (asprintf(&prompt, "%s\\%s> ", ctx->predef?ctx->predef:"",
+                            ctx->path) < 0) {
+                       ret = false;
+                       break;
+               }
 
                current_key = ctx->current;             /* No way to pass a void * pointer
                                                           via readline :-( */