52bda8aa423d5d5b80e7a60153583fb7881b398c
[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
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "dynconfig.h"
24 #include "lib/registry/registry.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "system/time.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "smb_build.h"
29
30 /* 
31  * ck/cd - change key
32  * ls - list values/keys
33  * rmval/rm - remove value
34  * rmkey/rmdir - remove key
35  * mkkey/mkdir - make key
36  * ch - change hive
37  * info - show key info
38  * save - save hive
39  * print - print value
40  * help
41  * exit
42  */
43
44 static struct registry_key *cmd_info(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
45 {
46         struct security_descriptor *sec_desc = NULL;
47         time_t last_mod;
48         WERROR error;
49         
50         printf("Name: %s\n", cur->name);
51         printf("Full path: %s\n", cur->path);
52         printf("Key Class: %s\n", cur->class_name);
53         last_mod = nt_time_to_unix(cur->last_mod);
54         printf("Time Last Modified: %s\n", ctime(&last_mod));
55
56         error = reg_get_sec_desc(mem_ctx, cur, &sec_desc);
57         if (!W_ERROR_IS_OK(error)) {
58                 printf("Error getting security descriptor\n");
59         } else {
60                 ndr_print_debug((ndr_print_fn_t)ndr_print_security_descriptor, "Security", sec_desc);
61         }
62         talloc_free(sec_desc);
63         return cur;
64 }
65
66 static struct registry_key *cmd_predef(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
67 {
68         struct registry_key *ret = NULL;
69         if (argc < 2) {
70                 fprintf(stderr, "Usage: predef predefined-key-name\n");
71         } else if (!ctx) {
72                 fprintf(stderr, "No full registry loaded, no predefined keys defined\n");
73         } else {
74                 WERROR error = reg_get_predefined_key_by_name(ctx, argv[1], &ret);
75
76                 if (!W_ERROR_IS_OK(error)) {
77                         fprintf(stderr, "Error opening predefined key %s: %s\n", argv[1], win_errstr(error));
78                         ret = NULL;
79                 }
80         }
81         return ret;
82 }
83
84 static struct registry_key *cmd_pwd(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
85 {
86         printf("%s\n", cur->path);
87         return cur;
88 }
89
90 static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
91 {
92         if (argc < 4) {
93                 fprintf(stderr, "Usage: set value-name type value\n");
94         } else {
95                 struct registry_value *val;
96                 if (reg_string_to_val(mem_ctx, argv[2], argv[3], &val->data_type, &val->data)) {
97                         WERROR error = reg_val_set(cur, argv[1], val->data_type, val->data);
98                         if (!W_ERROR_IS_OK(error)) {
99                                 fprintf(stderr, "Error setting value: %s\n", win_errstr(error));
100                                 return NULL;
101                         }
102                 } else {
103                         fprintf(stderr, "Unable to interpret data\n");
104                 }
105         }
106         return cur;
107 }
108
109 static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
110
111         struct registry_key *new = NULL;
112         WERROR error;
113         if(argc < 2) {
114                 new = cur;
115         } else {
116                 error = reg_open_key(mem_ctx, cur, argv[1], &new);
117                 if(!W_ERROR_IS_OK(error)) {
118                         DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
119                         return NULL;
120                 }
121         } 
122
123         printf("Current path is: %s\n", new->path);
124         
125         return new;
126 }
127
128 static struct registry_key *cmd_print(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
129 {
130         struct registry_value *value;
131         WERROR error;
132
133         if (argc != 2) {
134                 fprintf(stderr, "Usage: print <valuename>");
135                 return NULL;
136         }
137         
138         error = reg_key_get_value_by_name(mem_ctx, cur, argv[1], &value);
139         if (!W_ERROR_IS_OK(error)) {
140                 fprintf(stderr, "No such value '%s'\n", argv[1]);
141                 return NULL;
142         }
143
144         printf("%s\n%s\n", str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
145         return NULL;
146 }
147
148 static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
149 {
150         int i;
151         WERROR error;
152         struct registry_value *value;
153         struct registry_key *sub;
154         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &sub)); i++) {
155                 printf("K %s\n", sub->name);
156         }
157
158         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
159                 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
160         }
161
162         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &value)); i++) {
163                 printf("V \"%s\" %s %s\n", value->name, str_regtype(value->data_type), reg_val_data_string(mem_ctx, value->data_type, &value->data));
164         }
165         
166         return NULL; 
167 }
168 static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
169
170         struct registry_key *tmp;
171         if(argc < 2) {
172                 fprintf(stderr, "Usage: mkkey <keyname>\n");
173                 return NULL;
174         }
175         
176         if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) {
177                 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
178                 return NULL;
179         }
180
181         return NULL; 
182 }
183
184 static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
185
186         if(argc < 2) {
187                 fprintf(stderr, "Usage: rmkey <name>\n");
188                 return NULL;
189         }
190
191         if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) {
192                 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
193         } else {
194                 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
195         }
196         
197         return NULL; 
198 }
199
200 static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
201
202         if(argc < 2) {
203                 fprintf(stderr, "Usage: rmval <valuename>\n");
204                 return NULL;
205         }
206
207         if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) {
208                 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
209         } else {
210                 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
211         }
212
213         return NULL; 
214 }
215
216 static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *cur, int argc, char **argv)
217 {
218         exit(0);
219         return NULL; 
220 }
221
222 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int, char **);
223
224 static struct {
225         const char *name;
226         const char *alias;
227         const char *help;
228         struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_context *ctx,struct registry_key *, int argc, char **argv);
229 } regshell_cmds[] = {
230         {"ck", "cd", "Change current key", cmd_ck },
231         {"info", "i", "Show detailed information of a key", cmd_info },
232         {"list", "ls", "List values/keys in current key", cmd_ls },
233         {"print", "p", "Print value", cmd_print },
234         {"mkkey", "mkdir", "Make new key", cmd_mkkey },
235         {"rmval", "rm", "Remove value", cmd_rmval },
236         {"rmkey", "rmdir", "Remove key", cmd_rmkey },
237         {"pwd", "pwk", "Printing current key", cmd_pwd },
238         {"set", "update", "Update value", cmd_set },
239         {"help", "?", "Help", cmd_help },
240         {"exit", "quit", "Exit", cmd_exit },
241         {"predef", "predefined", "Go to predefined key", cmd_predef },
242         {NULL }
243 };
244
245 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *cur, int argc, char **argv)
246 {
247         int i;
248         printf("Available commands:\n");
249         for(i = 0; regshell_cmds[i].name; i++) {
250                 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
251         }
252         return NULL;
253
254
255 static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_context *ctx, struct registry_key *k, char *line)
256 {
257         int argc;
258         char **argv = NULL;
259         int ret, i;
260
261         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
262                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
263                 return k;
264         }
265
266         for(i = 0; regshell_cmds[i].name; i++) {
267                 if(!strcmp(regshell_cmds[i].name, argv[0]) || 
268                    (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
269                         return regshell_cmds[i].handle(mem_ctx, ctx, k, argc, argv);
270                 }
271         }
272
273         fprintf(stderr, "No such command '%s'\n", argv[0]);
274         
275         return k;
276 }
277
278 #define MAX_COMPLETIONS 100
279
280 static struct registry_key *current_key = NULL;
281
282 static char **reg_complete_command(const char *text, int start, int end)
283 {
284         /* Complete command */
285         char **matches;
286         int i, len, samelen=0, count=1;
287
288         matches = malloc_array_p(char *, MAX_COMPLETIONS);
289         if (!matches) return NULL;
290         matches[0] = NULL;
291
292         len = strlen(text);
293         for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
294                 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
295                         matches[count] = strdup(regshell_cmds[i].name);
296                         if (!matches[count])
297                                 goto cleanup;
298                         if (count == 1)
299                                 samelen = strlen(matches[count]);
300                         else
301                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
302                                         samelen--;
303                         count++;
304                 }
305         }
306
307         switch (count) {
308         case 0: /* should never happen */
309         case 1:
310                 goto cleanup;
311         case 2:
312                 matches[0] = strdup(matches[1]);
313                 break;
314         default:
315                 matches[0] = strndup(matches[1], samelen);
316         }
317         matches[count] = NULL;
318         return matches;
319
320 cleanup:
321         count--;
322         while (count >= 0) {
323                 free(matches[count]);
324                 count--;
325         }
326         free(matches);
327         return NULL;
328 }
329
330 static char **reg_complete_key(const char *text, int start, int end)
331 {
332         struct registry_key *base;
333         struct registry_key *subkey;
334         int i, j = 1;
335         int samelen = 0;
336         int len;
337         char **matches;
338         const char *base_n = "";
339         TALLOC_CTX *mem_ctx;
340         WERROR status;
341
342         matches = malloc_array_p(char *, MAX_COMPLETIONS);
343         if (!matches) return NULL;
344         matches[0] = NULL;
345         mem_ctx = talloc_init("completion");
346
347         base = current_key;
348
349         len = strlen(text);
350         for(i = 0; j < MAX_COMPLETIONS-1; i++) {
351                 status = reg_key_get_subkey_by_index(mem_ctx, base, i, &subkey);
352                 if(W_ERROR_IS_OK(status)) {
353                         if(!strncmp(text, subkey->name, len)) {
354                                 matches[j] = strdup(subkey->name);
355                                 j++;
356
357                                 if (j == 1)
358                                         samelen = strlen(matches[j]);
359                                 else
360                                         while (strncmp(matches[j], matches[j-1], samelen) != 0)
361                                                 samelen--;
362                         }
363                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
364                         break;
365                 } else {
366                         printf("Error creating completion list: %s\n", win_errstr(status));
367                         talloc_free(mem_ctx);
368                         return NULL;
369                 }
370         }
371
372         if (j == 1) { /* No matches at all */
373                 SAFE_FREE(matches);
374                 talloc_free(mem_ctx);
375                 return NULL;
376         }
377
378         if (j == 2) { /* Exact match */
379                 asprintf(&matches[0], "%s%s", base_n, matches[1]);
380         } else {
381                 asprintf(&matches[0], "%s%s", base_n, talloc_strndup(mem_ctx, matches[1], samelen));
382         }               
383         talloc_free(mem_ctx);
384
385         matches[j] = NULL;
386         return matches;
387 }
388
389 static char **reg_completion(const char *text, int start, int end)
390 {
391         smb_readline_ca_char(' ');
392
393         if (start == 0) {
394                 return reg_complete_command(text, start, end);
395         } else {
396                 return reg_complete_key(text, start, end);
397         }
398 }
399
400  int main(int argc, char **argv)
401 {
402         int opt;
403         const char *backend = NULL;
404         struct registry_key *curkey = NULL;
405         poptContext pc;
406         WERROR error;
407         TALLOC_CTX *mem_ctx = talloc_init("cmd");
408         const char *remote = NULL;
409         struct registry_context *h = NULL;
410         struct poptOption long_options[] = {
411                 POPT_AUTOHELP
412                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
413                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
414                 POPT_COMMON_SAMBA
415                 POPT_COMMON_CREDENTIALS
416                 POPT_COMMON_VERSION
417                 POPT_TABLEEND
418         };
419
420         regshell_init_subsystems;
421
422         registry_init();
423
424         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
425         
426         while((opt = poptGetNextOpt(pc)) != -1) {
427         }
428
429         if (remote) {
430                 error = reg_open_remote (&h, cmdline_credentials, remote, NULL); 
431         } else if (backend) {
432                 error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, &curkey);
433         } else {
434                 error = reg_open_local(&h);
435         }
436
437         if(!W_ERROR_IS_OK(error)) {
438                 fprintf(stderr, "Unable to open registry\n");
439                 return 1;
440         }
441
442         if (h) {
443                 uint32_t try_hkeys[] = {
444                         HKEY_CLASSES_ROOT,
445                         HKEY_CURRENT_USER,
446                         HKEY_LOCAL_MACHINE,
447                         HKEY_USERS,
448                         HKEY_PERFORMANCE_DATA,
449                         HKEY_CURRENT_CONFIG,
450                         HKEY_DYN_DATA,
451                         HKEY_PERFORMANCE_TEXT,
452                         HKEY_PERFORMANCE_NLSTEXT,
453                         0
454                 };
455                 int i;
456
457                 for (i = 0; try_hkeys[i]; i++) {
458                         WERROR err;
459                         err = reg_get_predefined_key(h, try_hkeys[i], &curkey);
460                         if (W_ERROR_IS_OK(err)) {
461                                 break;
462                         } else {
463                                 curkey = NULL;
464                         }
465                 }
466         }
467
468         if (!curkey) {
469                 fprintf(stderr, "Unable to access any of the predefined keys\n");
470                 return -1;
471         }
472         
473         poptFreeContext(pc);
474         
475         while(True) {
476                 char *line, *prompt;
477                 
478                 if(curkey->hive->root->name) {
479                         asprintf(&prompt, "%s:%s> ", curkey->hive->root->name, curkey->path);
480                 } else {
481                         asprintf(&prompt, "%s> ", curkey->path);
482                 }
483                 
484                 current_key = curkey;           /* No way to pass a void * pointer 
485                                                                            via readline :-( */
486                 line = smb_readline(prompt, NULL, reg_completion);
487
488                 if(!line)
489                         break;
490
491                 if(line[0] != '\n') {
492                         struct registry_key *new = process_cmd(mem_ctx, h, curkey, line);
493                         if(new)curkey = new;
494                 }
495         }
496         talloc_free(mem_ctx);
497
498         return 0;
499 }