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