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