regtree... sddl
[metze/samba/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-2007
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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/registry.h"
23 #include "lib/registry/tools/common.h"
24 #include "lib/events/events.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "param/param.h"
27 #include "../libcli/security/security.h"
28
29 /**
30  * Print a registry key recursively
31  *
32  * @param level Level at which to print
33  * @param p Key to print
34  * @param fullpath Whether the full pat hshould be printed or just the last bit
35  * @param novals Whether values should not be printed
36  */
37 static void print_tree(unsigned int level, struct registry_key *p,
38                        const char *name,
39                        bool fullpath, bool novals)
40 {
41         struct registry_key *subkey;
42         const char *valuename, *keyname;
43         uint32_t valuetype;
44         DATA_BLOB valuedata;
45         struct security_descriptor *sec_desc;
46         WERROR error;
47         unsigned int i;
48         TALLOC_CTX *mem_ctx;
49
50         for(i = 0; i < level; i++) putchar(' ');
51         puts(name);
52
53         mem_ctx = talloc_init("print_tree");
54         for (i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx,
55                                                                       p,
56                                                                       i,
57                                                                       &keyname,
58                                                                       NULL,
59                                                                       NULL)); i++) {
60
61                 SMB_ASSERT(strlen(keyname) > 0);
62                 if (!W_ERROR_IS_OK(reg_open_key(mem_ctx, p, keyname, &subkey)))
63                         continue;
64
65                 print_tree(level+1, subkey, (fullpath && strlen(name))?
66                                                talloc_asprintf(mem_ctx, "%s\\%s",
67                                                                name, keyname):
68                                                keyname, fullpath, novals);
69                 talloc_free(subkey);
70         }
71         talloc_free(mem_ctx);
72
73         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
74                 DEBUG(0, ("Error occurred while fetching subkeys for '%s': %s\n",
75                                   name, win_errstr(error)));
76         }
77
78         if (!novals) {
79                 mem_ctx = talloc_init("print_tree");
80                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(
81                         mem_ctx, p, i, &valuename, &valuetype, &valuedata));
82                         i++) {
83                         unsigned int j;
84                         for(j = 0; j < level+1; j++) putchar(' ');
85                         printf("%s\n",  reg_val_description(mem_ctx,
86                                 valuename, valuetype, valuedata));
87                 }
88                 talloc_free(mem_ctx);
89
90                 if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
91                         DEBUG(0, ("Error occurred while fetching values for '%s': %s\n",
92                                 name, win_errstr(error)));
93                 }
94         }
95
96         mem_ctx = talloc_init("sec_desc");
97         if (!W_ERROR_IS_OK(reg_get_sec_desc(mem_ctx, p, &sec_desc))) {
98                 DEBUG(0, ("Error getting security descriptor\n"));
99         }
100         printf("sddl: %s\n", sddl_encode(mem_ctx, sec_desc, NULL));
101         talloc_free(mem_ctx);
102 }
103
104 int main(int argc, const char **argv)
105 {
106         int opt;
107         unsigned int i;
108         const char *file = NULL;
109         const char *remote = NULL;
110         poptContext pc;
111         struct registry_context *h = NULL;
112         struct registry_key *start_key = NULL;
113         struct tevent_context *ev_ctx;
114         WERROR error;
115         bool fullpath = false, no_values = false;
116         struct poptOption long_options[] = {
117                 POPT_AUTOHELP
118                 {"file", 'F', POPT_ARG_STRING, &file, 0, "file path", NULL },
119                 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
120                 {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
121                 {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
122                 POPT_COMMON_SAMBA
123                 POPT_COMMON_CREDENTIALS
124                 POPT_COMMON_VERSION
125                 { NULL }
126         };
127
128         pc = poptGetContext(argv[0], argc, argv, long_options,0);
129
130         while((opt = poptGetNextOpt(pc)) != -1) {
131         }
132
133         ev_ctx = s4_event_context_init(NULL);
134
135         if (remote != NULL) {
136                 h = reg_common_open_remote(remote, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
137         } else if (file != NULL) {
138                 start_key = reg_common_open_file(file, ev_ctx, cmdline_lp_ctx, cmdline_credentials);
139         } else {
140                 h = reg_common_open_local(cmdline_credentials, ev_ctx, cmdline_lp_ctx);
141         }
142
143         if (h == NULL && start_key == NULL)
144                 return 1;
145
146         poptFreeContext(pc);
147
148         error = WERR_OK;
149
150         if (start_key != NULL) {
151                 print_tree(0, start_key, "", fullpath, no_values);
152         } else {
153                 for(i = 0; reg_predefined_keys[i].handle; i++) {
154                         error = reg_get_predefined_key(h,
155                                                        reg_predefined_keys[i].handle,
156                                                        &start_key);
157                         if (!W_ERROR_IS_OK(error)) {
158                                 fprintf(stderr, "Skipping %s: %s\n",
159                                         reg_predefined_keys[i].name,
160                                         win_errstr(error));
161                                 continue;
162                         }
163                         SMB_ASSERT(start_key != NULL);
164                         print_tree(0, start_key, reg_predefined_keys[i].name,
165                                    fullpath, no_values);
166                 }
167         }
168
169         return 0;
170 }