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