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