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