Rev 11184: The registry library compiles again. in	file:///home/jelmer/bzr.samba-old/4.0-regwrite/ At file:///home/jelmer/bzr.samba-old/4.0-regwrite/ ------------------------------------------------------------ revno: 11184 revision-id: jelmer@samba.org-20070610234755-tpxetmbupthvqfp8 parent: jelmer@samba.org-20070610222652-11vkd62zyiyk971l committer: Jelmer Vernooij  branch nick: 4.0-regwrite timestamp: Mon 2007-06-11 01:47:55 +0200 message:   The registry library compiles again. modified:   source/lib/registry/tools/regshell.c svn-v2:20@0c0555d6-39d7-0310-84fc-f1cc0bd64818-branches%2fSAMBA_4_0-source%2flib%2fregistry%2ftools%2fregshell.c === modified file 'source/lib/registry/tools/regshell.c' --- a/source/lib/registry/tools/regshell.c	2007-06-10 22:26:52 +0000 +++ b/source/lib/registry/tools/regshell.c	2007-06-10 23:47:55 +0000 @@ -27,7 +27,13 @@  #include "lib/smbreadline/smbreadline.h"  #include "librpc/gen_ndr/ndr_security.h"   -/*  +struct regshell_context { +	struct registry_context *registry; +	const char *path; +	struct registry_key *current; +}; + +/* *   * ck/cd - change key   * ls - list values/keys   * rmval/rm - remove value @@ -41,10 +47,7 @@   * exit   */   -static struct registry_key *cmd_info(TALLOC_CTX *mem_ctx,  -									 struct registry_context *ctx, -									 struct registry_key *cur,  -									 int argc, char **argv) +static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)  {  	struct security_descriptor *sec_desc = NULL;  	time_t last_mod; @@ -52,34 +55,32 @@  	const char *classname;  	NTTIME last_change;   -	error = reg_key_get_info(mem_ctx, cur,  -					 &classname, NULL, NULL, &last_change); +	error = reg_key_get_info(ctx, ctx->current, &classname, NULL, NULL, &last_change);  	if (!W_ERROR_IS_OK(error)) {  		printf("Error getting key info: %s\\n", win_errstr(error)); -		return cur; +		return error;  	}    	 -	printf("Name: %s\\n", cur->name); -	printf("Full path: %s\\n", cur->path); +	printf("Name: %s\\n", strchr(ctx->path, '\\\\')?strrchr(ctx->path, '\\\\')+1:  +		   ctx->path); +	printf("Full path: %s\\n", ctx->path);  	printf("Key Class: %s\\n", classname);  	last_mod = nt_time_to_unix(last_change);  	printf("Time Last Modified: %s\\n", ctime(&last_mod));   -	error = reg_get_sec_desc(mem_ctx, cur, &sec_desc); +	error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);  	if (!W_ERROR_IS_OK(error)) {  		printf("Error getting security descriptor\\n"); -		return cur; +		return error;  	}   	ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);  	talloc_free(sec_desc); -	return cur; + +	return WERR_OK;  }   -static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx,  -									   struct registry_context *ctx,  -									   struct registry_key *cur,  -									   int argc, char **argv) +static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)  {  	struct registry_key *ret = NULL;  	if (argc < 2) { @@ -87,71 +88,71 @@  	} else if (!ctx) {  		fprintf(stderr, "No full registry loaded, no predefined keys defined\\n");  	} else { -		WERROR error = reg_get_predefined_key_by_name(ctx, argv[1], &ret); +		WERROR error = reg_get_predefined_key_by_name(ctx->registry, argv[1], &ret);    		if (!W_ERROR_IS_OK(error)) {  			fprintf(stderr, "Error opening predefined key %s: %s\\n", argv[1], win_errstr(error)); -			ret = NULL; +			return error;  		}  	} -	return ret; + +	return WERR_OK;  }   -static struct registry_key *cmd_pwd(TALLOC_CTX *mem_ctx,  -									struct registry_context *ctx, -									struct registry_key *cur,  +static WERROR cmd_pwd(struct regshell_context *ctx,  									int argc, char **argv)  { -	printf("%s\\n", cur->path); -	return cur; +	printf("%s\\n", ctx->path); +	return WERR_OK;  }   -static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)  {  	struct registry_value val;  	WERROR error;    	if (argc < 4) {  		fprintf(stderr, "Usage: set value-name type value\\n"); -		return cur; +		return WERR_INVALID_PARAM;  	}    -	if (!reg_string_to_val(mem_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 cur; +		return WERR_INVALID_PARAM;  	}   -	error = reg_val_set(cur, argv[1], val.data_type, val.data); +	error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);  	if (!W_ERROR_IS_OK(error)) {  		fprintf(stderr, "Error setting value: %s\\n", win_errstr(error)); -		return NULL; +		return error;  	} -	return cur; + +	return WERR_OK;  }   -static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx,  -								   struct registry_context *ctx, -								   struct registry_key *cur,  -								   int argc, char **argv) +static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)  {   	struct registry_key *new = NULL;  	WERROR error; +  	if(argc < 2) { -		new = cur; +		new = ctx->current;  	} else { -		error = reg_open_key(mem_ctx, cur, argv[1], &new); +		error = reg_open_key(ctx->registry, ctx->current, argv[1], &new);  		if(!W_ERROR_IS_OK(error)) {  			DEBUG(0, ("Error opening specified key: %s\\n", win_errstr(error))); -			return NULL; +			return error;  		}  	}    -	printf("Current path is: %s\\n", new->path); +	/* FIXME: Set ctx->path, ctx->current */ +	printf("Current path is: %s\\n", ctx->path);  	 -	return new; +	return WERR_OK;  }   -static struct registry_key *cmd_print(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)  {  	uint32_t value_type;  	DATA_BLOB value_data; @@ -159,25 +160,23 @@    	if (argc != 2) {  		fprintf(stderr, "Usage: print "); -		return NULL; +		return WERR_INVALID_PARAM;  	}  	 -	error = reg_key_get_value_by_name(mem_ctx, cur, argv[1],  +	error = reg_key_get_value_by_name(ctx, ctx->current, argv[1],   									  &value_type, &value_data);  	if (!W_ERROR_IS_OK(error)) {  		fprintf(stderr, "No such value '%s'\\n", argv[1]); -		return NULL; +		return error;  	}    	printf("%s\\n%s\\n", str_regtype(value_type),  -		   reg_val_data_string(mem_ctx, value_type, value_data)); -	return NULL; +		   reg_val_data_string(ctx, value_type, value_data)); + +	return WERR_OK;  }   -static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx,  -								   struct registry_context *ctx, -								   struct registry_key *cur,  -								   int argc, char **argv) +static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)  {  	int i;  	WERROR error; @@ -186,82 +185,97 @@  	DATA_BLOB data;  	const char *name;   -	for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &name, NULL, NULL)); i++) { +	for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx, ctx->current, i, &name, NULL, NULL)); i++) {  		printf("K %s\\n", name);  	}   -	if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { +	if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {  		DEBUG(0, ("Error occured while browsing thru keys: %s\\n", win_errstr(error)));  	}   -	for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &name, &data_type, &data)); i++) { +	for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx, ctx->current, i, &name, &data_type, &data)); i++) {  		printf("V \\"%s\\" %s %s\\n", value->name, str_regtype(data_type),  -			   reg_val_data_string(mem_ctx, data_type, data)); +			   reg_val_data_string(ctx, data_type, data));  	}  	 -	return NULL;  +	return WERR_OK;   } -static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)  {   	struct registry_key *tmp; +	WERROR error; +  	if(argc < 2) {  		fprintf(stderr, "Usage: mkkey \\n"); -		return NULL; +		return WERR_INVALID_PARAM;  	} + +	error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);  	 -	if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) { +	if (!W_ERROR_IS_OK(error)) {  		fprintf(stderr, "Error adding new subkey '%s'\\n", argv[1]); -		return NULL; +		return error;  	}   -	return NULL;  +	return WERR_OK;   }   -static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_rmkey(struct regshell_context *ctx, +									  int argc, char **argv)  {  +	WERROR error; +  	if(argc < 2) {  		fprintf(stderr, "Usage: rmkey \\n"); -		return NULL; +		return WERR_INVALID_PARAM;  	}   -	if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) { +	error = reg_key_del(ctx->current, argv[1]); +	if(!W_ERROR_IS_OK(error)) {  		fprintf(stderr, "Error deleting '%s'\\n", argv[1]); +		return error;  	} else {  		fprintf(stderr, "Successfully deleted '%s'\\n", argv[1]);  	}  	 -	return NULL;  +	return WERR_OK;  }   -static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)  {  +	WERROR error; +  	if(argc < 2) {  		fprintf(stderr, "Usage: rmval \\n"); -		return NULL; +		return WERR_INVALID_PARAM;  	}   -	if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) { +	error = reg_del_value(ctx->current, argv[1]); +	if(!W_ERROR_IS_OK(error)) {  		fprintf(stderr, "Error deleting value '%s'\\n", argv[1]); +		return error;  	} else {  		fprintf(stderr, "Successfully deleted value '%s'\\n", argv[1]);  	}   -	return NULL;  +	return WERR_OK;   }   -static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv) +static WERROR cmd_exit(struct regshell_context *ctx, +									 int argc, char **argv)  {  	exit(0); -	return NULL;  +	return WERR_OK;  }   -static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int, char **); +static WERROR cmd_help(struct regshell_context *ctx, int, char **);    static struct {  	const char *name;  	const char *alias;  	const char *help; -	struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int argc, char **argv); +	WERROR (*handle)(struct regshell_context *ctx, +								   int argc, char **argv);  } regshell_cmds[] = {  	{"ck", "cd", "Change current key", cmd_ck },  	{"info", "i", "Show detailed information of a key", cmd_info }, @@ -278,17 +292,19 @@  	{NULL }  };   -static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv) +static WERROR cmd_help(struct regshell_context *ctx, +									 int argc, char **argv)  {  	int i;  	printf("Available commands:\\n");  	for(i = 0; regshell_cmds[i].name; i++) {  		printf("%s - %s\\n", regshell_cmds[i].name, regshell_cmds[i].help);  	} -	return NULL; +	return WERR_OK;  }    -static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *k, char *line) +static WERROR process_cmd(struct regshell_context *ctx, +										char *line)  {  	int argc;  	char **argv = NULL; @@ -296,19 +312,19 @@    	if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {  		fprintf(stderr, "regshell: %s\\n", poptStrerror(ret)); -		return k; +		return WERR_INVALID_PARAM;  	}    	for(i = 0; regshell_cmds[i].name; i++) {  		if(!strcmp(regshell_cmds[i].name, argv[0]) ||   		   (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) { -			return regshell_cmds[i].handle(mem_ctx, ctx, k, argc, argv); +			return regshell_cmds[i].handle(ctx, argc, argv);  		}  	}    	fprintf(stderr, "No such command '%s'\\n", argv[0]);  	 -	return k; +	return WERR_INVALID_PARAM;  }    #define MAX_COMPLETIONS 100 @@ -439,13 +455,11 @@  {  	int opt;  	const char *backend = NULL; -	struct registry_key *curkey = NULL; -	struct hive_key *hivekey = NULL;  	poptContext pc;  	WERROR error; -	TALLOC_CTX *mem_ctx = talloc_init("cmd");  	const char *remote = NULL; -	struct registry_context *h = NULL; +	struct regshell_context *ctx; +	bool ret = true;  	struct poptOption long_options[] = {  		POPT_AUTOHELP  		{"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL}, @@ -460,13 +474,16 @@  	while((opt = poptGetNextOpt(pc)) != -1) {  	}   +	ctx = talloc_zero(NULL, struct regshell_context); +  	if (remote != NULL) { -		error = reg_open_remote (&h, NULL, cmdline_credentials, remote, NULL);  +		error = reg_open_remote (&ctx->registry, NULL,  +								 cmdline_credentials, remote, NULL);   	} else if (backend != NULL) { -		error = reg_open_hive(NULL, poptGetArg(pc), NULL, cmdline_credentials,  -							  &hivekey); +		error = reg_open_hive(ctx, poptGetArg(pc), NULL, cmdline_credentials,  +							  &ctx->current);  	} else { -		error = reg_open_local(NULL, &h, NULL, cmdline_credentials); +		error = reg_open_local(ctx, &ctx->registry, NULL, cmdline_credentials);  	}    	if (!W_ERROR_IS_OK(error)) { @@ -474,21 +491,23 @@  		return 1;  	}   -	if (h != NULL) { +	if (ctx->registry != NULL) {  		int i;    		for (i = 0; reg_predefined_keys[i].handle; i++) {  			WERROR err; -			err = reg_get_predefined_key(h, reg_predefined_keys[i].handle, &curkey); +			err = reg_get_predefined_key(ctx->registry,  +										 reg_predefined_keys[i].handle,  +										 &ctx->current);  			if (W_ERROR_IS_OK(err)) {  				break;  			} else { -				curkey = NULL; +				ctx->current = NULL;  			}  		}  	}   -	if (!curkey) { +	if (ctx->current == NULL) {  		fprintf(stderr, "Unable to access any of the predefined keys\\n");  		return -1;  	} @@ -498,21 +517,20 @@  	while (true) {  		char *line, *prompt;  		 -		asprintf(&prompt, "%s> ", curkey->path); +		asprintf(&prompt, "%s> ", ctx->path);  		 -		current_key = curkey; 		/* No way to pass a void * pointer  -									   via readline :-( */ +		current_key = ctx->current; 		/* No way to pass a void * pointer  +									   		   via readline :-( */  		line = smb_readline(prompt, NULL, reg_completion);   -		if(!line) +		if (line == NULL)  			break;   -		if(line[0] != '\\n') { -			struct registry_key *new = process_cmd(mem_ctx, h, curkey, line); -			if(new)curkey = new; +		if (line[0] != '\\n') { +			ret = W_ERROR_IS_OK(process_cmd(ctx, line));  		}  	} -	talloc_free(mem_ctx); +	talloc_free(ctx);   -	return 0; +	return (ret?0:1);  }