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