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