r3586: Fix some of the issues with the module init functions.
[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 "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         struct registry_key *key;
124         if(argc < 2) {
125                 fprintf(stderr, "Usage: rmkey <name>\n");
126                 return NULL;
127         }
128
129         if(!W_ERROR_IS_OK(reg_open_key(mem_ctx, cur, argv[1], &key))) {
130                 fprintf(stderr, "No such subkey '%s'\n", argv[1]);
131                 return NULL;
132         }
133
134         if(!W_ERROR_IS_OK(reg_key_del(key))) {
135                 fprintf(stderr, "Error deleting '%s'\n", argv[1]);
136         } else {
137                 fprintf(stderr, "Successfully deleted '%s'\n", argv[1]);
138         }
139         
140         return NULL; 
141 }
142
143 static struct registry_key *cmd_rmval(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
144
145         struct registry_value *val;
146         if(argc < 2) {
147                 fprintf(stderr, "Usage: rmval <valuename>\n");
148                 return NULL;
149         }
150
151         if(!W_ERROR_IS_OK(reg_key_get_value_by_name(mem_ctx, cur, argv[1], &val))) {
152                 fprintf(stderr, "No such value '%s'\n", argv[1]);
153                 return NULL;
154         }
155
156         if(!W_ERROR_IS_OK(reg_del_value(val))) {
157                 fprintf(stderr, "Error deleting value '%s'\n", argv[1]);
158         } else {
159                 fprintf(stderr, "Successfully deleted value '%s'\n", argv[1]);
160         }
161
162         return NULL; 
163 }
164
165 static struct registry_key *cmd_hive(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
166 {
167         int i;
168         for(i = 0; i < cur->hive->reg_ctx->num_hives; i++) {
169
170                 if(argc == 1) {
171                         printf("%s\n", cur->hive->reg_ctx->hives[i]->name);
172                 } else if(!strcmp(cur->hive->reg_ctx->hives[i]->name, argv[1])) {
173                         return cur->hive->reg_ctx->hives[i]->root;
174                 } 
175         }
176         return NULL;
177 }
178
179 static struct registry_key *cmd_exit(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
180 {
181         exit(0);
182         return NULL; 
183 }
184
185 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_key *, int, char **);
186
187 struct {
188         const char *name;
189         const char *alias;
190         const char *help;
191         struct registry_key *(*handle)(TALLOC_CTX *mem_ctx, struct registry_key *, int argc, char **argv);
192 } regshell_cmds[] = {
193         {"ck", "cd", "Change current key", cmd_ck },
194         {"ch", "hive", "Change current hive", cmd_hive },
195         {"info", "i", "Show detailed information of a key", cmd_info },
196         {"list", "ls", "List values/keys in current key", cmd_ls },
197         {"mkkey", "mkdir", "Make new key", cmd_mkkey },
198         {"rmval", "rm", "Remove value", cmd_rmval },
199         {"rmkey", "rmdir", "Remove key", cmd_rmkey },
200         {"pwd", "pwk", "Printing current key", cmd_pwd },
201         {"set", "update", "Update value", cmd_set },
202         {"help", "?", "Help", cmd_help },
203         {"exit", "quit", "Exit", cmd_exit },
204         {NULL }
205 };
206
207 static struct registry_key *cmd_help(TALLOC_CTX *mem_ctx, struct registry_key *cur, int argc, char **argv)
208 {
209         int i;
210         printf("Available commands:\n");
211         for(i = 0; regshell_cmds[i].name; i++) {
212                 printf("%s - %s\n", regshell_cmds[i].name, regshell_cmds[i].help);
213         }
214         return NULL;
215
216
217 static struct registry_key *process_cmd(TALLOC_CTX *mem_ctx, struct registry_key *k, char *line)
218 {
219         int argc;
220         char **argv = NULL;
221         int ret, i;
222
223         if ((ret = poptParseArgvString(line, &argc, (const char ***) &argv)) != 0) {
224                 fprintf(stderr, "regshell: %s\n", poptStrerror(ret));
225                 return k;
226         }
227
228         for(i = 0; regshell_cmds[i].name; i++) {
229                 if(!strcmp(regshell_cmds[i].name, argv[0]) || 
230                    (regshell_cmds[i].alias && !strcmp(regshell_cmds[i].alias, argv[0]))) {
231                         return regshell_cmds[i].handle(mem_ctx, k, argc, argv);
232                 }
233         }
234
235         fprintf(stderr, "No such command '%s'\n", argv[0]);
236         
237         return k;
238 }
239
240 #define MAX_COMPLETIONS 100
241
242 static struct registry_key *current_key = NULL;
243
244 static char **reg_complete_command(const char *text, int end)
245 {
246         /* Complete command */
247         char **matches;
248         int i, len, samelen, count=1;
249
250         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
251         if (!matches) return NULL;
252         matches[0] = NULL;
253
254         len = strlen(text);
255         for (i=0;regshell_cmds[i].handle && count < MAX_COMPLETIONS-1;i++) {
256                 if (strncmp(text, regshell_cmds[i].name, len) == 0) {
257                         matches[count] = strdup(regshell_cmds[i].name);
258                         if (!matches[count])
259                                 goto cleanup;
260                         if (count == 1)
261                                 samelen = strlen(matches[count]);
262                         else
263                                 while (strncmp(matches[count], matches[count-1], samelen) != 0)
264                                         samelen--;
265                         count++;
266                 }
267         }
268
269         switch (count) {
270         case 0: /* should never happen */
271         case 1:
272                 goto cleanup;
273         case 2:
274                 matches[0] = strdup(matches[1]);
275                 break;
276         default:
277                 matches[0] = malloc(samelen+1);
278                 if (!matches[0])
279                         goto cleanup;
280                 strncpy(matches[0], matches[1], samelen);
281                 matches[0][samelen] = 0;
282         }
283         matches[count] = NULL;
284         return matches;
285
286 cleanup:
287         while (i >= 0) {
288                 free(matches[i]);
289                 i--;
290         }
291         free(matches);
292         return NULL;
293 }
294
295 static char **reg_complete_key(const char *text, int end)
296 {
297         struct registry_key *subkey;
298         int i, j = 0;
299         int len;
300         char **matches;
301         TALLOC_CTX *mem_ctx;
302         /* Complete argument */
303
304         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
305         if (!matches) return NULL;
306         matches[0] = NULL;
307
308         len = strlen(text);
309         mem_ctx = talloc_init("completion");
310         for(i = 0; j < MAX_COMPLETIONS-1; i++) {
311                 WERROR status = reg_key_get_subkey_by_index(mem_ctx, current_key, i, &subkey);
312                 if(W_ERROR_IS_OK(status)) {
313                         if(!strncmp(text, subkey->name, len)) {
314                                 matches[j] = strdup(subkey->name);
315                                 j++;
316                         }
317                 } else if(W_ERROR_EQUAL(status, WERR_NO_MORE_ITEMS)) {
318                         break;
319                 } else {
320                         printf("Error creating completion list: %s\n", win_errstr(status));
321                         talloc_destroy(mem_ctx);
322                         return NULL;
323                 }
324         }
325         matches[j] = NULL;
326         talloc_destroy(mem_ctx);
327         return matches;
328 }
329
330 static char **reg_completion(const char *text, int start, int end)
331 {
332         smb_readline_ca_char(' ');
333
334         if (start == 0) {
335                 return reg_complete_command(text, end);
336         } else {
337                 return reg_complete_key(text, end);
338         }
339 }
340
341  int main(int argc, char **argv)
342 {
343         int opt;
344         const char *backend = "rpc";
345         const char *credentials = NULL;
346         struct registry_key *curkey = NULL;
347         poptContext pc;
348         WERROR error;
349         TALLOC_CTX *mem_ctx = talloc_init("cmd");
350         struct registry_context *h;
351         struct poptOption long_options[] = {
352                 POPT_AUTOHELP
353                 POPT_COMMON_SAMBA
354                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
355                 {"credentials", 'c', POPT_ARG_STRING, &credentials, 0, "credentials", NULL},
356                 POPT_TABLEEND
357         };
358
359         regshell_init_subsystems;
360
361         if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
362                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
363         }
364
365         
366         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
367         
368         while((opt = poptGetNextOpt(pc)) != -1) {
369         }
370
371     setup_logging("regtree", True);
372
373         error = reg_open(&h, backend, poptPeekArg(pc), credentials);
374         if(!W_ERROR_IS_OK(error)) {
375                 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", poptGetArg(pc), backend);
376                 return 1;
377         }
378         poptFreeContext(pc);
379
380         curkey = h->hives[0]->root;
381
382         while(True) {
383                 char *line, *prompt;
384                 
385                 if(curkey->hive->name) {
386                         asprintf(&prompt, "%s:%s> ", curkey->hive->name, curkey->path);
387                 } else {
388                         asprintf(&prompt, "%s> ", curkey->path);
389                 }
390                 
391                 current_key = curkey;           /* No way to pass a void * pointer 
392                                                                            via readline :-( */
393                 line = smb_readline(prompt, NULL, reg_completion);
394
395                 if(!line)
396                         break;
397
398                 if(line[0] != '\n') {
399                         struct registry_key *new = process_cmd(mem_ctx, curkey, line);
400                         if(new)curkey = new;
401                 }
402         }
403         talloc_destroy(mem_ctx);
404
405         return 0;
406 }