r4204: Arguments to reg_del_key more like the RPC for more efficient usage
[samba.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 "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_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_pwd(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
53 {
54         printf("%s\n", cur->path);
55         return cur;
56 }
57
58 static struct registry_key *cmd_set(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
59 {
60         /* FIXME */
61         return NULL;
62 }
63
64 static struct registry_key *cmd_ck(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
65
66         struct registry_key *new = NULL;
67         WERROR error;
68         if(argc < 2) {
69                 new = cur;
70         } else {
71                 error = reg_open_key(mem_ctx, cur, argv[1], &new);
72                 if(!W_ERROR_IS_OK(error)) {
73                         DEBUG(0, ("Error opening specified key: %s\n", win_errstr(error)));
74                         return NULL;
75                 }
76         } 
77
78         printf("Current path is: %s\n", new->path);
79         
80         return new;
81 }
82
83 static struct registry_key *cmd_ls(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
84 {
85         int i;
86         WERROR error;
87         struct registry_value *value;
88         struct registry_key *sub;
89         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, cur, i, &sub)); i++) {
90                 printf("K %s\n", sub->name);
91         }
92
93         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
94                 DEBUG(0, ("Error occured while browsing thru keys: %s\n", win_errstr(error)));
95         }
96
97         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, cur, i, &value)); i++) {
98                 printf("V \"%s\" %s %s\n", value->name, str_regtype(value->data_type), reg_val_data_string(mem_ctx, value));
99         }
100         
101         return NULL; 
102 }
103 static struct registry_key *cmd_mkkey(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
104
105         struct registry_key *tmp;
106         if(argc < 2) {
107                 fprintf(stderr, "Usage: mkkey <keyname>\n");
108                 return NULL;
109         }
110         
111         if(!W_ERROR_IS_OK(reg_key_add_name(mem_ctx, cur, argv[1], 0, NULL, &tmp))) {
112                 fprintf(stderr, "Error adding new subkey '%s'\n", argv[1]);
113                 return NULL;
114         }
115
116         fprintf(stderr, "Successfully added new subkey '%s' to '%s'\n", argv[1], cur->path);
117         
118         return NULL; 
119 }
120
121 static struct registry_key *cmd_rmkey(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
122
123         if(argc < 2) {
124                 fprintf(stderr, "Usage: rmkey <name>\n");
125                 return NULL;
126         }
127
128         if(!W_ERROR_IS_OK(reg_key_del(cur, argv[1]))) {
129                 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
130         } else {
131                 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
132         }
133         
134         return NULL; 
135 }
136
137 static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
138
139         if(argc < 2) {
140                 fprintf(stderr, "Usage: rmval <valuename>\n");
141                 return NULL;
142         }
143
144         if(!W_ERROR_IS_OK(reg_del_value(cur, argv[1]))) {
145                 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
146         } else {
147                 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
148         }
149
150         return NULL; 
151 }
152
153 static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
154 {
155         exit(0);
156         return NULL; 
157 }
158
159 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_key *, int, char **);
160
161 struct {
162         const char *name;
163         const char *alias;
164         const char *help;
165         struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_key *, int argc, char **argv);
166 } regshell_cmds[] = {
167         {"ck", "cd", "Change current key", cmd_ck },
168         {"info", "i", "Show detailed information of a key", cmd_info },
169         {"list", "ls", "List values/keys in current key", cmd_ls },
170         {"mkkey", "mkdir", "Make new key", cmd_mkkey },
171         {"rmval", "rm", "Remove value", cmd_rmval },
172         {"rmkey", "rmdir", "Remove key", cmd_rmkey },
173         {"pwd", "pwk", "Printing current key", cmd_pwd },
174         {"set", "update", "Update value", cmd_set },
175         {"help", "?", "Help", cmd_help },
176         {"exit", "quit", "Exit", cmd_exit },
177         {NULL }
178 };
179
180 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
181 {
182         int i;
183         printf("Available commands:\n");
184         for(i = 0; regshell_cmds[i].name; i++) {
185                 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
186         }
187         return NULL;
188
189
190 static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_key *k, char *line)
191 {
192         int argc;
193         char **argv = NULL;
194         int ret, i;
195
196         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
197                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
198                 return k;
199         }
200
201         for(i = 0; regshell_cmds[i].name; i++) {
202                 if(!strcmp(regshell_cmds[i].name, argv[0]) || 
203                    (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
204                         return regshell_cmds[i].handle(mem_ctx, k, argc, argv);
205                 }
206         }
207
208         fprintf(stderr, "No such command '%s'\n", argv[0]);
209         
210         return k;
211 }
212
213 #define MAX_COMPLETIONS 100
214
215 static struct registry_key *current_key = NULL;
216
217 static char **reg_complete_command(const char *text, int end)
218 {
219         /* Complete command */
220         char **matches;
221         int i, len, samelen=0, count=1;
222
223         matches = malloc_array_p(char *, MAX_COMPLETIONS);
224         if (!matches) return NULL;
225         matches[0] = NULL;
226
227         len = strlen(text);
228         for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
229                 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
230                         matches[count] = strdup(regshell_cmds[i].name);
231                         if (!matches[count])
232                                 goto cleanup;
233                         if (count == 1)
234                                 samelen = strlen(matches[count]);
235                         else
236                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
237                                         samelen--;
238                         count++;
239                 }
240         }
241
242         switch (count) {
243         case 0: /* should never happen */
244         case 1:
245                 goto cleanup;
246         case 2:
247                 matches[0] = strdup(matches[1]);
248                 break;
249         default:
250                 matches[0] = strndup(matches[1], samelen);
251         }
252         matches[count] = NULL;
253         return matches;
254
255 cleanup:
256         count--;
257         while (count >= 0) {
258                 free(matches[count]);
259                 count--;
260         }
261         free(matches);
262         return NULL;
263 }
264
265 static char **reg_complete_key(const char *text, int end)
266 {
267         struct registry_key *subkey;
268         int i, j = 1;
269         int samelen = 0;
270         int len;
271         char **matches;
272         TALLOC_CTX *mem_ctx;
273
274         matches = malloc_array_p(char *, MAX_COMPLETIONS);
275         if (!matches) return NULL;
276         matches[0] = NULL;
277
278         len = strlen(text);
279         mem_ctx = talloc_init("completion");
280         for(i = 0; j < MAX_COMPLETIONS-1; i++) {
281                 WERROR status = reg_key_get_subkey_by_index(mem_ctx, current_key, i, &subkey);
282                 if(W_ERROR_IS_OK(status)) {
283                         if(!strncmp(text, subkey->name, len)) {
284                                 matches[j] = strdup(subkey->name);
285                                 j++;
286
287                                 if (j == 1)
288                                         samelen = strlen(matches[j]);
289                                 else
290                                         while (strncmp(matches[j], matches[j-1], samelen) != 0)
291                                                 samelen--;
292                         }
293                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
294                         break;
295                 } else {
296                         printf("Error creating completion list: %s\n", win_errstr(status));
297                         talloc_destroy(mem_ctx);
298                         return NULL;
299                 }
300         }
301         talloc_destroy(mem_ctx);
302
303         if (j == 1) { /* No matches at all */
304                 SAFE_FREE(matches);
305                 return NULL;
306         }
307
308         if (j == 2) { /* Exact match */
309                 matches[0] = strdup(matches[1]);
310         } else {
311                 matches[0] = strndup(matches[1], samelen);
312         }               
313
314         matches[j] = NULL;
315         return matches;
316 }
317
318 static char **reg_completion(const char *text, int start, int end)
319 {
320         smb_readline_ca_char(' ');
321
322         if (start == 0) {
323                 return reg_complete_command(text, end);
324         } else {
325                 return reg_complete_key(text, end);
326         }
327 }
328
329  int main(int argc, char **argv)
330 {
331         int opt;
332         const char *backend = NULL;
333         struct registry_key *curkey = NULL;
334         poptContext pc;
335         WERROR error;
336         TALLOC_CTX *mem_ctx = talloc_init("cmd");
337         const char *remote = NULL;
338         struct registry_context *h = NULL;
339         struct poptOption long_options[] = {
340                 POPT_AUTOHELP
341                 POPT_COMMON_CREDENTIALS
342                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
343                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
344                 POPT_TABLEEND
345         };
346
347         regshell_init_subsystems;
348
349         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
350                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
351         }
352
353         
354         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
355         
356         while((opt = poptGetNextOpt(pc)) != -1) {
357         }
358
359     setup_logging("regtree", True);
360
361         if (remote) {
362                 error = reg_open_remote (&h, cmdline_get_username(), cmdline_get_userpassword(), remote); 
363         } else if (backend) {
364                 error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, &curkey);
365         } else {
366                 error = reg_open_local(&h);
367         }
368
369         if(!W_ERROR_IS_OK(error)) {
370                 fprintf(stderr, "Unable to open registry\n");
371                 return 1;
372         }
373
374         if (h) {
375                 /*FIXME: What if HKEY_CLASSES_ROOT is not present ? */
376                 reg_get_predefined_key(h, HKEY_CLASSES_ROOT, &curkey);
377         }
378         
379         poptFreeContext(pc);
380         
381         while(True) {
382                 char *line, *prompt;
383                 
384                 if(curkey->hive->root->name) {
385                         asprintf(&prompt, "%s:%s> ", curkey->hive->root->name, curkey->path);
386                 } else {
387                         asprintf(&prompt, "%s> ", curkey->path);
388                 }
389                 
390                 current_key = curkey;           /* No way to pass a void * pointer 
391                                                                            via readline :-( */
392                 line = smb_readline(prompt, NULL, reg_completion);
393
394                 if(!line)
395                         break;
396
397                 if(line[0] != '\n') {
398                         struct registry_key *new = process_cmd(mem_ctx, curkey, line);
399                         if(new)curkey = new;
400                 }
401         }
402         talloc_destroy(mem_ctx);
403
404         return 0;
405 }