1996861a2a62afd62dd6dcdd3a36801eef44b526
[jelmer/samba4-debian.git] / source / 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/popt_common.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(poptContext pc,
32                                              struct loadparm_context *lp_ctx,
33                                              enum reg_backend backend,
34                                              const char *remote_host)
35 {
36         WERROR error;
37         struct registry_context *ctx;
38
39         switch (backend) {
40         case REG_UNKNOWN:
41                 poptPrintUsage(pc, stderr, 0);
42                 return NULL;
43         case REG_LOCAL:
44                 error = reg_open_samba(NULL, &ctx, lp_ctx, NULL, cmdline_credentials);
45                 break;
46         case REG_REMOTE:
47                 error = reg_open_remote(&ctx, NULL, cmdline_credentials,
48                                         remote_host, NULL);
49                 break;
50         case REG_NULL:
51                 error = reg_open_local(NULL, &ctx, NULL, cmdline_credentials);
52                 break;
53         }
54
55         if (!W_ERROR_IS_OK(error)) {
56                 fprintf(stderr, "Error: %s\n", win_errstr(error));
57                 return NULL;
58         }
59
60         return ctx;
61 }
62
63 int main(int argc, const char **argv)
64 {
65         int opt;
66         poptContext pc;
67         char *outputfile = NULL;
68         enum reg_backend backend1 = REG_UNKNOWN, backend2 = REG_UNKNOWN;
69         const char *remote1 = NULL, *remote2 = NULL;
70         struct registry_context *h1 = NULL, *h2 = NULL;
71         WERROR error;
72         struct poptOption long_options[] = {
73                 POPT_AUTOHELP
74                 {"output", 'o', POPT_ARG_STRING, &outputfile, 0, "output file to use", NULL },
75                 {"null", 'n', POPT_ARG_NONE, NULL, 'n', "Diff from NULL", NULL },
76                 {"remote", 'R', POPT_ARG_STRING, NULL, 'R', "Connect to remote server" , NULL },
77                 {"local", 'L', POPT_ARG_NONE, NULL, 'L', "Open local registry", NULL },
78                 POPT_COMMON_SAMBA
79                 POPT_COMMON_CREDENTIALS
80                 POPT_COMMON_VERSION
81                 { NULL }
82         };
83         TALLOC_CTX *ctx;
84         void *callback_data;
85         struct reg_diff_callbacks *callbacks;
86
87         ctx = talloc_init("regdiff");
88
89         pc = poptGetContext(argv[0], argc, argv, long_options,0);
90
91         while((opt = poptGetNextOpt(pc)) != -1) {
92                 error = WERR_OK;
93                 switch(opt)     {
94                 case 'L':
95                         if (backend1 == REG_UNKNOWN)
96                                 backend1 = REG_LOCAL;
97                         else if (backend2 == REG_UNKNOWN)
98                                 backend2 = REG_LOCAL;
99                         break;
100                 case 'n':
101                         if (backend1 == REG_UNKNOWN)
102                                 backend1 = REG_NULL;
103                         else if (backend2 == REG_UNKNOWN)
104                                 backend2 = REG_NULL;
105                         break;
106                 case 'R':
107                         if (backend1 == REG_UNKNOWN) {
108                                 backend1 = REG_REMOTE;
109                                 remote1 = poptGetOptArg(pc);
110                         } else if (backend2 == REG_UNKNOWN) {
111                                 backend2 = REG_REMOTE;
112                                 remote2 = poptGetOptArg(pc);
113                         }
114                         break;
115                 }
116
117         }
118
119         h1 = open_backend(pc, global_loadparm, backend1, remote1);
120         if (h1 == NULL)
121                 return 1;
122
123         h2 = open_backend(pc, global_loadparm, backend2, remote2);
124         if (h2 == NULL)
125                 return 1;
126
127         poptFreeContext(pc);
128
129         error = reg_dotreg_diff_save(ctx, outputfile, &callbacks,
130                                      &callback_data);
131         if (!W_ERROR_IS_OK(error)) {
132                 fprintf(stderr, "Problem saving registry diff to '%s': %s\n",
133                         outputfile, win_errstr(error));
134                 return -1;
135         }
136
137         error = reg_generate_diff(h1, h2, callbacks, callback_data);
138         if (!W_ERROR_IS_OK(error)) {
139                 fprintf(stderr, "Unable to generate diff between keys: %s\n",
140                         win_errstr(error));
141                 return -1;
142         }
143
144         return 0;
145 }