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