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