299f0e96531beaa8e8c4a74e06ed6acc749378ec
[samba.git] / source4 / lib / registry / tools / regdiff.c
1 /*
2    Unix SMB/CIFS implementation.
3    simple registry frontend
4
5    Copyright (C) Jelmer Vernooij 2004-2007
6    Copyright (C) Wilco Baan Hofman 2006
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/registry/registry.h"
24 #include "lib/events/events.h"
25 #include "lib/cmdline/cmdline.h"
26 #include "lib/registry/tools/common.h"
27 #include "param/param.h"
28
29 enum reg_backend { REG_UNKNOWN, REG_LOCAL, REG_REMOTE, REG_NULL };
30
31 static struct registry_context *open_backend(TALLOC_CTX *mem_ctx,
32                                              poptContext pc,
33                                              struct tevent_context *ev_ctx,
34                                              struct loadparm_context *lp_ctx,
35                                              enum reg_backend backend,
36                                              const char *remote_host)
37 {
38         WERROR error;
39         struct registry_context *ctx;
40         struct cli_credentials *creds = samba_cmdline_get_creds();
41
42         switch (backend) {
43         case REG_UNKNOWN:
44                 poptPrintUsage(pc, stderr, 0);
45                 return NULL;
46         case REG_LOCAL:
47                 error = reg_open_samba(mem_ctx, &ctx, ev_ctx, lp_ctx, NULL,
48                                 creds);
49                 break;
50         case REG_REMOTE:
51                 error = reg_open_remote(mem_ctx, &ctx, NULL,
52                                 creds, lp_ctx,
53                                         remote_host, ev_ctx);
54                 break;
55         case REG_NULL:
56                 error = reg_open_local(mem_ctx, &ctx);
57                 break;
58         }
59
60         if (!W_ERROR_IS_OK(error)) {
61                 fprintf(stderr, "Error: %s\n", win_errstr(error));
62                 return NULL;
63         }
64
65         return ctx;
66 }
67
68 int main(int argc, char **argv)
69 {
70         const char **argv_const = discard_const_p(const char *, argv);
71         int opt;
72         poptContext pc;
73         char *outputfile = NULL;
74         enum reg_backend backend1 = REG_UNKNOWN, backend2 = REG_UNKNOWN;
75         const char *remote1 = NULL, *remote2 = NULL;
76         struct registry_context *h1 = NULL, *h2 = NULL;
77         WERROR error;
78         struct poptOption long_options[] = {
79                 POPT_AUTOHELP
80                 {"output", 'o', POPT_ARG_STRING, &outputfile, 0, "output file to use", NULL },
81                 {"null", 'n', POPT_ARG_NONE, NULL, 'n', "Diff from NULL", NULL },
82                 {"remote", 'R', POPT_ARG_STRING, NULL, 'R', "Connect to remote server" , NULL },
83                 {"local", 'L', POPT_ARG_NONE, NULL, 'L', "Open local registry", NULL },
84                 POPT_COMMON_SAMBA
85                 POPT_COMMON_CREDENTIALS
86                 POPT_COMMON_VERSION
87                 {0}
88         };
89         TALLOC_CTX *ctx;
90         void *callback_data;
91         struct tevent_context *ev_ctx;
92         struct reg_diff_callbacks *callbacks;
93         struct loadparm_context *lp_ctx = NULL;
94         bool ok;
95
96         ctx = talloc_init("regdiff");
97         if (ctx == NULL) {
98                 exit(ENOMEM);
99         }
100
101         ok = samba_cmdline_init(ctx,
102                                 SAMBA_CMDLINE_CONFIG_CLIENT,
103                                 false /* require_smbconf */);
104         if (!ok) {
105                 DBG_ERR("Failed to init cmdline parser!\n");
106                 TALLOC_FREE(ctx);
107                 exit(1);
108         }
109
110         pc = samba_popt_get_context(getprogname(),
111                                     argc,
112                                     argv_const,
113                                     long_options,
114                                     0);
115         if (pc == NULL) {
116                 DBG_ERR("Failed to setup popt context!\n");
117                 TALLOC_FREE(ctx);
118                 exit(1);
119         }
120
121         while((opt = poptGetNextOpt(pc)) != -1) {
122                 error = WERR_OK;
123                 switch(opt)     {
124                 case 'L':
125                         if (backend1 == REG_UNKNOWN)
126                                 backend1 = REG_LOCAL;
127                         else if (backend2 == REG_UNKNOWN)
128                                 backend2 = REG_LOCAL;
129                         break;
130                 case 'n':
131                         if (backend1 == REG_UNKNOWN)
132                                 backend1 = REG_NULL;
133                         else if (backend2 == REG_UNKNOWN)
134                                 backend2 = REG_NULL;
135                         break;
136                 case 'R':
137                         if (backend1 == REG_UNKNOWN) {
138                                 backend1 = REG_REMOTE;
139                                 remote1 = poptGetOptArg(pc);
140                         } else if (backend2 == REG_UNKNOWN) {
141                                 backend2 = REG_REMOTE;
142                                 remote2 = poptGetOptArg(pc);
143                         }
144                         break;
145                 }
146
147         }
148
149         ev_ctx = s4_event_context_init(ctx);
150         lp_ctx = samba_cmdline_get_lp_ctx();
151
152         h1 = open_backend(ctx, pc, ev_ctx, lp_ctx, backend1, remote1);
153         if (h1 == NULL)
154                 return 1;
155
156         h2 = open_backend(ctx, pc, ev_ctx, lp_ctx, backend2, remote2);
157         if (h2 == NULL)
158                 return 1;
159
160         poptFreeContext(pc);
161         samba_cmdline_burn(argc, argv);
162
163         error = reg_dotreg_diff_save(ctx, outputfile, &callbacks, &callback_data);
164         if (!W_ERROR_IS_OK(error)) {
165                 fprintf(stderr, "Problem saving registry diff to '%s': %s\n",
166                         outputfile, win_errstr(error));
167                 return -1;
168         }
169
170         error = reg_generate_diff(h1, h2, callbacks, callback_data);
171         if (!W_ERROR_IS_OK(error)) {
172                 fprintf(stderr, "Unable to generate diff between keys: %s\n",
173                         win_errstr(error));
174                 return -1;
175         }
176
177         return 0;
178 }