r10016: Support reading security descriptors on keys.
[ira/wip.git] / source4 / lib / registry / tools / regtree.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
27 static void print_tree(int l, struct registry_key *p, int fullpath, int novals)
28 {
29         struct registry_key *subkey;
30         struct registry_value *value;
31         struct security_descriptor *sec_desc;
32         WERROR error;
33         int i;
34         TALLOC_CTX *mem_ctx;
35
36         for(i = 0; i < l; i++) putchar(' ');
37         
38         /* Hive name */
39         if(p->hive->root == p) {
40                 if(p->hive->root->name) printf("%s\n", p->hive->root->name); else printf("<No Name>\n");
41         } else {
42                 if(!p->name) printf("<No Name>\n");
43                 if(fullpath) printf("%s\n", p->path);
44                 else printf("%s\n", p->name);
45         }
46
47         mem_ctx = talloc_init("print_tree");
48         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, p, i, &subkey)); i++) {
49                 print_tree(l+1, subkey, fullpath, novals);
50         }
51         talloc_free(mem_ctx);
52
53         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
54                 DEBUG(0, ("Error occured while fetching subkeys for '%s': %s\n", p->path, win_errstr(error)));
55         }
56
57         if(!novals) {
58                 mem_ctx = talloc_init("print_tree");
59                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, p, i, &value)); i++) {
60                         int j;
61                         char *desc;
62                         for(j = 0; j < l+1; j++) putchar(' ');
63                         desc = reg_val_description(mem_ctx, value);
64                         printf("%s\n", desc);
65                 }
66                 talloc_free(mem_ctx);
67
68                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
69                         DEBUG(0, ("Error occured while fetching values for '%s': %s\n", p->path, win_errstr(error)));
70                 }
71         }
72
73         mem_ctx = talloc_init("sec_desc");
74         if (NT_STATUS_IS_ERR(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
75                 DEBUG(0, ("Error getting security descriptor\n"));
76         }
77         talloc_free(mem_ctx);
78 }
79
80 int main(int argc, char **argv)
81 {
82         int opt, i;
83         const char *backend = NULL;
84         const char *remote = NULL;
85         poptContext pc;
86         struct registry_context *h = NULL;
87         struct registry_key *root = NULL;
88         WERROR error;
89         int fullpath = 0, no_values = 0;
90         struct poptOption long_options[] = {
91                 POPT_AUTOHELP
92                 {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
93                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
94                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
95                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
96                 POPT_COMMON_SAMBA       
97                 POPT_COMMON_CREDENTIALS 
98                 POPT_TABLEEND
99         };
100
101         regtree_init_subsystems;
102
103         pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
104         
105         while((opt = poptGetNextOpt(pc)) != -1) {
106         }
107
108         if (remote) {
109                 error = reg_open_remote(&h, cmdline_credentials, remote, NULL);
110         } else if (backend) {
111             error = reg_open_hive(NULL, backend, poptGetArg(pc), NULL, &root);
112         } else {
113                 error = reg_open_local (&h);
114         }
115
116         if(!W_ERROR_IS_OK(error)) {
117                 fprintf(stderr, "Unable to open '%s' with backend '%s':%s \n", poptGetArg(pc), backend, win_errstr(error));
118                 return 1;
119         }
120         poptFreeContext(pc);
121
122         error = WERR_OK;
123         
124         if (!h) {
125                 print_tree(0, root, fullpath, no_values);
126         } else {
127                 for(i = HKEY_CLASSES_ROOT; i < HKEY_PERFORMANCE_NLSTEXT; i++) {
128                         error = reg_get_predefined_key(h, i, &root);
129                         if (!W_ERROR_IS_OK(error)) {
130                                 fprintf(stderr, "Skipping %s\n", reg_get_predef_name(i));
131                                 continue;
132                         }
133                         print_tree(0, root, fullpath, no_values);
134                 }
135         }
136
137         return 0;
138 }