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