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