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