r24725: Don't segfault if hive file can't be found
[abartlet/samba.git/.git] / source4 / lib / registry / tools / regshell.c
1 /* 
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004-2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/registry.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "lib/events/events.h"
25 #include "system/time.h"
26 #include "lib/smbreadline/smbreadline.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "lib/registry/tools/common.h"
29
30 struct regshell_context {
31         struct registry_context *registry;
32         const char *path;
33         struct registry_key *current;
34 };
35
36 /* *
37  * ck/cd - change key
38  * ls - list values/keys
39  * rmval/rm - remove value
40  * rmkey/rmdir - remove key
41  * mkkey/mkdir - make key
42  * ch - change hive
43  * info - show key info
44  * save - save hive
45  * print - print value
46  * help
47  * exit
48  */
49
50 static WERROR cmd_info(struct regshell_context *ctx, int argc, char **argv)
51 {
52         struct security_descriptor *sec_desc = NULL;
53         time_t last_mod;
54         WERROR error;
55         const char *classname;
56         NTTIME last_change;
57
58         error = reg_key_get_info(ctx, ctx->current, &classname, NULL, NULL, &last_change);
59         if (!W_ERROR_IS_OK(error)) {
60                 printf("Error getting key info: %s\n", win_errstr(error));
61                 return error;
62         }
63
64         
65         printf("Name: %s\n", strchr(ctx->path, '\\')?strrchr(ctx->path, '\\')+1: 
66                    ctx->path);
67         printf("Full path: %s\n", ctx->path);
68         printf("Key Class: %s\n", classname);
69         last_mod = nt_time_to_unix(last_change);
70         printf("Time Last Modified: %s\n", ctime(&last_mod));
71
72         error = reg_get_sec_desc(ctx, ctx->current, &sec_desc);
73         if (!W_ERROR_IS_OK(error)) {
74                 printf("Error getting security descriptor\n");
75                 return error;
76         } 
77         ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);
78         talloc_free(sec_desc);
79
80         return WERR_OK;
81 }
82
83 static WERROR cmd_predef(struct regshell_context *ctx, int argc, char **argv)
84 {
85         struct registry_key *ret = NULL;
86         if (argc < 2) {
87                 fprintf(stderr, "Usage: predef predefined-key-name\n");
88         } else if (!ctx) {
89                 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
90         } else {
91                 WERROR error = reg_get_predefined_key_by_name(ctx->registry, argv[1], &ret);
92
93                 if (!W_ERROR_IS_OK(error)) {
94                         fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error));
95                         return error;
96                 }
97         }
98
99         return WERR_OK;
100 }
101
102 static WERROR cmd_pwd(struct regshell_context *ctx,
103                                                                         int argc, char **argv)
104 {
105         printf("%s\n", ctx->path);
106         return WERR_OK;
107 }
108
109 static WERROR cmd_set(struct regshell_context *ctx, int argc, char **argv)
110 {
111         struct registry_value val;
112         WERROR error;
113
114         if (argc < 4) {
115                 fprintf(stderr, "Usage: set value-name type value\n");
116                 return WERR_INVALID_PARAM;
117         } 
118
119         if (!reg_string_to_val(ctx, argv[2], argv[3], &val.data_type, 
120                                                    &val.data)) {
121                 fprintf(stderr, "Unable to interpret data\n");
122                 return WERR_INVALID_PARAM;
123         }
124
125         error = reg_val_set(ctx->current, argv[1], val.data_type, val.data);
126         if (!W_ERROR_IS_OK(error)) {
127                 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
128                 return error;
129         }
130
131         return WERR_OK;
132 }
133
134 static WERROR cmd_ck(struct regshell_context *ctx, int argc, char **argv)
135
136         struct registry_key *new = NULL;
137         WERROR error;
138
139         if(argc < 2) {
140                 new = ctx->current;
141         } else {
142                 error = reg_open_key(ctx->registry, ctx->current, argv[1], &new);
143                 if(!W_ERROR_IS_OK(error)) {
144                         DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
145                         return error;
146                 }
147         } 
148
149         ctx->path = talloc_asprintf(ctx, "%s\\%s", ctx->path, argv[1]);
150         printf("Current path is: %s\n", ctx->path);
151         ctx->current = new;
152         
153         return WERR_OK;
154 }
155
156 static WERROR cmd_print(struct regshell_context *ctx, int argc, char **argv)
157 {
158         uint32_t value_type;
159         DATA_BLOB value_data;
160         WERROR error;
161
162         if (argc != 2) {
163                 fprintf(stderr, "Usage: print <valuename>");
164                 return WERR_INVALID_PARAM;
165         }
166         
167         error = reg_key_get_value_by_name(ctx, ctx->current, argv[1], 
168                                                                           &value_type, &value_data);
169         if (!W_ERROR_IS_OK(error)) {
170                 fprintf(stderr, "No such value '%s'\n", argv[1]);
171                 return error;
172         }
173
174         printf("%s\n%s\n", str_regtype(value_type), 
175                    reg_val_data_string(ctx, value_type, value_data));
176
177         return WERR_OK;
178 }
179
180 static WERROR cmd_ls(struct regshell_context *ctx, int argc, char **argv)
181 {
182         int i;
183         WERROR error;
184         struct registry_value *value;
185         uint32_t data_type;
186         DATA_BLOB data;
187         const char *name;
188
189         for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(ctx, ctx->current, i, &name, NULL, NULL)); i++) {
190                 printf("K %s\n", name);
191         }
192
193         if (!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
194                 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
195         }
196
197         for (i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(ctx, ctx->current, i, &name, &data_type, &data)); i++) {
198                 printf("V \"%s\" %s %s\n", value->name, str_regtype(data_type), 
199                            reg_val_data_string(ctx, data_type, data));
200         }
201         
202         return WERR_OK; 
203 }
204 static WERROR cmd_mkkey(struct regshell_context *ctx, int argc, char **argv)
205
206         struct registry_key *tmp;
207         WERROR error;
208
209         if(argc < 2) {
210                 fprintf(stderr, "Usage: mkkey <keyname>\n");
211                 return WERR_INVALID_PARAM;
212         }
213
214         error = reg_key_add_name(ctx, ctx->current, argv[1], 0, NULL, &tmp);
215         
216         if (!W_ERROR_IS_OK(error)) {
217                 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
218                 return error;
219         }
220
221         return WERR_OK; 
222 }
223
224 static WERROR cmd_rmkey(struct regshell_context *ctx,
225                                                                           int argc, char **argv)
226
227         WERROR error;
228
229         if(argc < 2) {
230                 fprintf(stderr, "Usage: rmkey <name>\n");
231                 return WERR_INVALID_PARAM;
232         }
233
234         error = reg_key_del(ctx->current, argv[1]);
235         if(!W_ERROR_IS_OK(error)) {
236                 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
237                 return error;
238         } else {
239                 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
240         }
241         
242         return WERR_OK;
243 }
244
245 static WERROR cmd_rmval(struct regshell_context *ctx, int argc, char **argv)
246
247         WERROR error;
248
249         if(argc < 2) {
250                 fprintf(stderr, "Usage: rmval <valuename>\n");
251                 return WERR_INVALID_PARAM;
252         }
253
254         error = reg_del_value(ctx->current, argv[1]);
255         if(!W_ERROR_IS_OK(error)) {
256                 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
257                 return error;
258         } else {
259                 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
260         }
261
262         return WERR_OK; 
263 }
264
265 static WERROR cmd_exit(struct regshell_context *ctx,
266                                                                          int argc, char **argv)
267 {
268         exit(0);
269         return WERR_OK;
270 }
271
272 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
273
274 static struct {
275         const char *name;
276         const char *alias;
277         const char *help;
278         WERROR (*handle)(struct regshell_context *ctx,
279                                                                    int argc, char **argv);
280 } regshell_cmds[] = {
281         {"ck", "cd", "Change current key", cmd_ck },
282         {"info", "i", "Show detailed information of a key", cmd_info },
283         {"list", "ls", "List values/keys in current key", cmd_ls },
284         {"print", "p", "Print value", cmd_print },
285         {"mkkey", "mkdir", "Make new key", cmd_mkkey },
286         {"rmval", "rm", "Remove value", cmd_rmval },
287         {"rmkey", "rmdir", "Remove key", cmd_rmkey },
288         {"pwd", "pwk", "Printing current key", cmd_pwd },
289         {"set", "update", "Update value", cmd_set },
290         {"help", "?", "Help", cmd_help },
291         {"exit", "quit", "Exit", cmd_exit },
292         {"predef", "predefined", "Go to predefined key", cmd_predef },
293         {NULL }
294 };
295
296 static WERROR cmd_help(struct regshell_context *ctx,
297                                                                          int argc, char **argv)
298 {
299         int i;
300         printf("Available commands:\n");
301         for(i = 0; regshell_cmds[i].name; i++) {
302                 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
303         }
304         return WERR_OK;
305
306
307 static WERROR process_cmd(struct regshell_context *ctx,
308                                                                                 char *line)
309 {
310         int argc;
311         char **argv = NULL;
312         int ret, i;
313
314         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
315                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
316                 return WERR_INVALID_PARAM;
317         }
318
319         for(i = 0; regshell_cmds[i].name; i++) {
320                 if(!strcmp(regshell_cmds[i].name, argv[0]) || 
321                    (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
322                         return regshell_cmds[i].handle(ctx, argc, argv);
323                 }
324         }
325
326         fprintf(stderr, "No such command '%s'\n", argv[0]);
327         
328         return WERR_INVALID_PARAM;
329 }
330
331 #define MAX_COMPLETIONS 100
332
333 static struct registry_key *current_key = NULL;
334
335 static char **reg_complete_command(const char *text, int start, int end)
336 {
337         /* Complete command */
338         char **matches;
339         int i, len, samelen=0, count=1;
340
341         matches = malloc_array_p(char *, MAX_COMPLETIONS);
342         if (!matches) return NULL;
343         matches[0] = NULL;
344
345         len = strlen(text);
346         for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
347                 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
348                         matches[count] = strdup(regshell_cmds[i].name);
349                         if (!matches[count])
350                                 goto cleanup;
351                         if (count == 1)
352                                 samelen = strlen(matches[count]);
353                         else
354                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
355                                         samelen--;
356                         count++;
357                 }
358         }
359
360         switch (count) {
361         case 0: /* should never happen */
362         case 1:
363                 goto cleanup;
364         case 2:
365                 matches[0] = strdup(matches[1]);
366                 break;
367         default:
368                 matches[0] = strndup(matches[1], samelen);
369         }
370         matches[count] = NULL;
371         return matches;
372
373 cleanup:
374         count--;
375         while (count >= 0) {
376                 free(matches[count]);
377                 count--;
378         }
379         free(matches);
380         return NULL;
381 }
382
383 static char **reg_complete_key(const char *text, int start, int end)
384 {
385         struct registry_key *base;
386         const char *subkeyname;
387         int i, j = 1;
388         int samelen = 0;
389         int len;
390         char **matches;
391         const char *base_n = "";
392         TALLOC_CTX *mem_ctx;
393         WERROR status;
394
395         matches = malloc_array_p(char *, MAX_COMPLETIONS);
396         if (!matches) return NULL;
397         matches[0] = NULL;
398         mem_ctx = talloc_init("completion");
399
400         base = current_key;
401
402         len = strlen(text);
403         for(i = 0; j < MAX_COMPLETIONS-1; i++) {
404                 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkeyname, 
405                                                                                          NULL, NULL);
406                 if(W_ERROR_IS_OK(status)) {
407                         if(!strncmp(text, subkeyname, len)) {
408                                 matches[j] = strdup(subkeyname);
409                                 j++;
410
411                                 if (j == 1)
412                                         samelen = strlen(matches[j]);
413                                 else
414                                         while (strncmp(matches[j], matches[j-1], samelen) != 0)
415                                                 samelen--;
416                         }
417                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
418                         break;
419                 } else {
420                         printf("Error creating completion list: %s\n", win_errstr(status));
421                         talloc_free(mem_ctx);
422                         return NULL;
423                 }
424         }
425
426         if (j == 1) { /* No matches at all */
427                 SAFE_FREE(matches);
428                 talloc_free(mem_ctx);
429                 return NULL;
430         }
431
432         if (j == 2) { /* Exact match */
433                 asprintf(&matches[0], "%s%s", base_n, matches[1]);
434         } else {
435                 asprintf(&matches[0], "%s%s", base_n, 
436                                 talloc_strndup(mem_ctx, matches[1], samelen));
437         }               
438         talloc_free(mem_ctx);
439
440         matches[j] = NULL;
441         return matches;
442 }
443
444 static char **reg_completion(const char *text, int start, int end)
445 {
446         smb_readline_ca_char(' ');
447
448         if (start == 0) {
449                 return reg_complete_command(text, start, end);
450         } else {
451                 return reg_complete_key(text, start, end);
452         }
453 }
454
455 int main(int argc, char **argv)
456 {
457         int opt;
458         const char *file = NULL;
459         poptContext pc;
460         const char *remote = NULL;
461         struct regshell_context *ctx;
462         bool ret = true;
463         struct poptOption long_options[] = {
464                 POPT_AUTOHELP
465                 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
466                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
467                 POPT_COMMON_SAMBA
468                 POPT_COMMON_CREDENTIALS
469                 POPT_COMMON_VERSION
470                 { NULL }
471         };
472
473         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
474         
475         while((opt = poptGetNextOpt(pc)) != -1) {
476         }
477
478         ctx = talloc_zero(NULL, struct regshell_context);
479
480         if (remote != NULL) {
481                 ctx->registry = reg_common_open_remote(remote, cmdline_credentials);
482         } else if (file != NULL) {
483                 ctx->current = reg_common_open_file(file, cmdline_credentials);
484                 if (ctx->current == NULL)
485                         return 1;
486                 ctx->registry = ctx->current->context;
487                 ctx->path = talloc_strdup(ctx, "");
488         } else {
489                 ctx->registry = reg_common_open_local(cmdline_credentials);
490         }
491
492         if (ctx->registry == NULL)
493                 return 1;
494
495         if (ctx->current == NULL) {
496                 int i;
497
498                 for (i = 0; reg_predefined_keys[i].handle; i++) {
499                         WERROR err;
500                         err = reg_get_predefined_key(ctx->registry, 
501                                                                                  reg_predefined_keys[i].handle, 
502                                                                                  &ctx->current);
503                         if (W_ERROR_IS_OK(err)) {
504                                 ctx->path = talloc_strdup(ctx, reg_predefined_keys[i].name);
505                                 break;
506                         } else {
507                                 ctx->current = NULL;
508                         }
509                 }
510         }
511
512         if (ctx->current == NULL) {
513                 fprintf(stderr, "Unable to access any of the predefined keys\n");
514                 return -1;
515         }
516         
517         poptFreeContext(pc);
518         
519         while (true) {
520                 char *line, *prompt;
521                 
522                 asprintf(&prompt, "%s> ", ctx->path);
523                 
524                 current_key = ctx->current;             /* No way to pass a void * pointer 
525                                                                                            via readline :-( */
526                 line = smb_readline(prompt, NULL, reg_completion);
527
528                 if (line == NULL)
529                         break;
530
531                 if (line[0] != '\n') {
532                         ret = W_ERROR_IS_OK(process_cmd(ctx, line));
533                 }
534         }
535         talloc_free(ctx);
536
537         return (ret?0:1);
538 }