r25000: Fix some more C++ compatibility warnings.
[metze/samba/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
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 _NORETURN_ static WERROR cmd_exit(struct regshell_context *ctx, int argc, char **argv)
266 {
267         exit(0);
268         return WERR_OK;
269 }
270
271 static WERROR cmd_help(struct regshell_context *ctx, int, char **);
272
273 static struct {
274         const char *name;
275         const char *alias;
276         const char *help;
277         WERROR (*handle)(struct regshell_context *ctx, int argc, char **argv);
278 } regshell_cmds[] = {
279         {"ck", "cd", "Change current key", cmd_ck },
280         {"info", "i", "Show detailed information of a key", cmd_info },
281         {"list", "ls", "List values/keys in current key", cmd_ls },
282         {"print", "p", "Print value", cmd_print },
283         {"mkkey", "mkdir", "Make new key", cmd_mkkey },
284         {"rmval", "rm", "Remove value", cmd_rmval },
285         {"rmkey", "rmdir", "Remove key", cmd_rmkey },
286         {"pwd", "pwk", "Printing current key", cmd_pwd },
287         {"set", "update", "Update value", cmd_set },
288         {"help", "?", "Help", cmd_help },
289         {"exit", "quit", "Exit", cmd_exit },
290         {"predef", "predefined", "Go to predefined key", cmd_predef },
291         {NULL }
292 };
293
294 static WERROR cmd_help(struct regshell_context *ctx,
295                                                                          int argc, char **argv)
296 {
297         int i;
298         printf("Available commands:\n");
299         for(i = 0; regshell_cmds[i].name; i++) {
300                 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
301         }
302         return WERR_OK;
303
304
305 static WERROR process_cmd(struct regshell_context *ctx,
306                                                                                 char *line)
307 {
308         int argc;
309         char **argv = NULL;
310         int ret, i;
311
312         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
313                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
314                 return WERR_INVALID_PARAM;
315         }
316
317         for(i = 0; regshell_cmds[i].name; i++) {
318                 if(!strcmp(regshell_cmds[i].name, argv[0]) || 
319                    (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
320                         return regshell_cmds[i].handle(ctx, argc, argv);
321                 }
322         }
323
324         fprintf(stderr, "No such command '%s'\n", argv[0]);
325         
326         return WERR_INVALID_PARAM;
327 }
328
329 #define MAX_COMPLETIONS 100
330
331 static struct registry_key *current_key = NULL;
332
333 static char **reg_complete_command(const char *text, int start, int end)
334 {
335         /* Complete command */
336         char **matches;
337         int i, len, samelen=0, count=1;
338
339         matches = malloc_array_p(char *, MAX_COMPLETIONS);
340         if (!matches) return NULL;
341         matches[0] = NULL;
342
343         len = strlen(text);
344         for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
345                 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
346                         matches[count] = strdup(regshell_cmds[i].name);
347                         if (!matches[count])
348                                 goto cleanup;
349                         if (count == 1)
350                                 samelen = strlen(matches[count]);
351                         else
352                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
353                                         samelen--;
354                         count++;
355                 }
356         }
357
358         switch (count) {
359         case 0: /* should never happen */
360         case 1:
361                 goto cleanup;
362         case 2:
363                 matches[0] = strdup(matches[1]);
364                 break;
365         default:
366                 matches[0] = strndup(matches[1], samelen);
367         }
368         matches[count] = NULL;
369         return matches;
370
371 cleanup:
372         count--;
373         while (count >= 0) {
374                 free(matches[count]);
375                 count--;
376         }
377         free(matches);
378         return NULL;
379 }
380
381 static char **reg_complete_key(const char *text, int start, int end)
382 {
383         struct registry_key *base;
384         const char *subkeyname;
385         int i, j = 1;
386         int samelen = 0;
387         int len;
388         char **matches;
389         const char *base_n = "";
390         TALLOC_CTX *mem_ctx;
391         WERROR status;
392
393         matches = malloc_array_p(char *, MAX_COMPLETIONS);
394         if (!matches) return NULL;
395         matches[0] = NULL;
396         mem_ctx = talloc_init("completion");
397
398         base = current_key;
399
400         len = strlen(text);
401         for(i = 0; j < MAX_COMPLETIONS-1; i++) {
402                 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkeyname, 
403                                                                                          NULL, NULL);
404                 if(W_ERROR_IS_OK(status)) {
405                         if(!strncmp(text, subkeyname, len)) {
406                                 matches[j] = strdup(subkeyname);
407                                 j++;
408
409                                 if (j == 1)
410                                         samelen = strlen(matches[j]);
411                                 else
412                                         while (strncmp(matches[j], matches[j-1], samelen) != 0)
413                                                 samelen--;
414                         }
415                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
416                         break;
417                 } else {
418                         printf("Error creating completion list: %s\n", win_errstr(status));
419                         talloc_free(mem_ctx);
420                         return NULL;
421                 }
422         }
423
424         if (j == 1) { /* No matches at all */
425                 SAFE_FREE(matches);
426                 talloc_free(mem_ctx);
427                 return NULL;
428         }
429
430         if (j == 2) { /* Exact match */
431                 asprintf(&matches[0], "%s%s", base_n, matches[1]);
432         } else {
433                 asprintf(&matches[0], "%s%s", base_n, 
434                                 talloc_strndup(mem_ctx, matches[1], samelen));
435         }               
436         talloc_free(mem_ctx);
437
438         matches[j] = NULL;
439         return matches;
440 }
441
442 static char **reg_completion(const char *text, int start, int end)
443 {
444         smb_readline_ca_char(' ');
445
446         if (start == 0) {
447                 return reg_complete_command(text, start, end);
448         } else {
449                 return reg_complete_key(text, start, end);
450         }
451 }
452
453 int main(int argc, char **argv)
454 {
455         int opt;
456         const char *file = NULL;
457         poptContext pc;
458         const char *remote = NULL;
459         struct regshell_context *ctx;
460         bool ret = true;
461         struct poptOption long_options[] = {
462                 POPT_AUTOHELP
463                 {"file", 'F', POPT_ARG_STRING, &file, 0, "open hive file", NULL },
464                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
465                 POPT_COMMON_SAMBA
466                 POPT_COMMON_CREDENTIALS
467                 POPT_COMMON_VERSION
468                 { NULL }
469         };
470
471         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
472         
473         while((opt = poptGetNextOpt(pc)) != -1) {
474         }
475
476         ctx = talloc_zero(NULL, struct regshell_context);
477
478         if (remote != NULL) {
479                 ctx->registry = reg_common_open_remote(remote, cmdline_credentials);
480         } else if (file != NULL) {
481                 ctx->current = reg_common_open_file(file, cmdline_credentials);
482                 if (ctx->current == NULL)
483                         return 1;
484                 ctx->registry = ctx->current->context;
485                 ctx->path = talloc_strdup(ctx, "");
486         } else {
487                 ctx->registry = reg_common_open_local(cmdline_credentials);
488         }
489
490         if (ctx->registry == NULL)
491                 return 1;
492
493         if (ctx->current == NULL) {
494                 int i;
495
496                 for (i = 0; reg_predefined_keys[i].handle; i++) {
497                         WERROR err;
498                         err = reg_get_predefined_key(ctx->registry, 
499                                                                                  reg_predefined_keys[i].handle, 
500                                                                                  &ctx->current);
501                         if (W_ERROR_IS_OK(err)) {
502                                 ctx->path = talloc_strdup(ctx, reg_predefined_keys[i].name);
503                                 break;
504                         } else {
505                                 ctx->current = NULL;
506                         }
507                 }
508         }
509
510         if (ctx->current == NULL) {
511                 fprintf(stderr, "Unable to access any of the predefined keys\n");
512                 return -1;
513         }
514         
515         poptFreeContext(pc);
516         
517         while (true) {
518                 char *line, *prompt;
519                 
520                 asprintf(&prompt, "%s> ", ctx->path);
521                 
522                 current_key = ctx->current;             /* No way to pass a void * pointer 
523                                                                                            via readline :-( */
524                 line = smb_readline(prompt, NULL, reg_completion);
525
526                 if (line == NULL)
527                         break;
528
529                 if (line[0] != '\n') {
530                         ret = W_ERROR_IS_OK(process_cmd(ctx, line));
531                 }
532         }
533         talloc_free(ctx);
534
535         return (ret?0:1);
536 }