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