s4:registry: add a TALLOC_CTX argument to reg_open_remote()
[Samba/gebeck_regimport.git] / source4 / lib / registry / tools / regdiff.c
blob3683e76da6614f968753390369d14340c0848ae1
1 /*
2 Unix SMB/CIFS implementation.
3 simple registry frontend
5 Copyright (C) Jelmer Vernooij 2004-2007
6 Copyright (C) Wilco Baan Hofman 2006
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.
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.
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/>.
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"
29 enum reg_backend { REG_UNKNOWN, REG_LOCAL, REG_REMOTE, REG_NULL };
31 static struct registry_context *open_backend(poptContext pc,
32 struct tevent_context *ev_ctx,
33 struct loadparm_context *lp_ctx,
34 enum reg_backend backend,
35 const char *remote_host)
37 WERROR error;
38 struct registry_context *ctx;
40 switch (backend) {
41 case REG_UNKNOWN:
42 poptPrintUsage(pc, stderr, 0);
43 return NULL;
44 case REG_LOCAL:
45 error = reg_open_samba(NULL, &ctx, ev_ctx, lp_ctx, NULL, cmdline_credentials);
46 break;
47 case REG_REMOTE:
48 error = reg_open_remote(NULL, &ctx, NULL, cmdline_credentials, lp_ctx,
49 remote_host, ev_ctx);
50 break;
51 case REG_NULL:
52 error = reg_open_local(NULL, &ctx);
53 break;
56 if (!W_ERROR_IS_OK(error)) {
57 fprintf(stderr, "Error: %s\n", win_errstr(error));
58 return NULL;
61 return ctx;
64 int main(int argc, const char **argv)
66 int opt;
67 poptContext pc;
68 char *outputfile = NULL;
69 enum reg_backend backend1 = REG_UNKNOWN, backend2 = REG_UNKNOWN;
70 const char *remote1 = NULL, *remote2 = NULL;
71 struct registry_context *h1 = NULL, *h2 = NULL;
72 WERROR error;
73 struct poptOption long_options[] = {
74 POPT_AUTOHELP
75 {"output", 'o', POPT_ARG_STRING, &outputfile, 0, "output file to use", NULL },
76 {"null", 'n', POPT_ARG_NONE, NULL, 'n', "Diff from NULL", NULL },
77 {"remote", 'R', POPT_ARG_STRING, NULL, 'R', "Connect to remote server" , NULL },
78 {"local", 'L', POPT_ARG_NONE, NULL, 'L', "Open local registry", NULL },
79 POPT_COMMON_SAMBA
80 POPT_COMMON_CREDENTIALS
81 POPT_COMMON_VERSION
82 { NULL }
84 TALLOC_CTX *ctx;
85 void *callback_data;
86 struct tevent_context *ev_ctx;
87 struct reg_diff_callbacks *callbacks;
89 ctx = talloc_init("regdiff");
91 pc = poptGetContext(argv[0], argc, argv, long_options,0);
93 while((opt = poptGetNextOpt(pc)) != -1) {
94 error = WERR_OK;
95 switch(opt) {
96 case 'L':
97 if (backend1 == REG_UNKNOWN)
98 backend1 = REG_LOCAL;
99 else if (backend2 == REG_UNKNOWN)
100 backend2 = REG_LOCAL;
101 break;
102 case 'n':
103 if (backend1 == REG_UNKNOWN)
104 backend1 = REG_NULL;
105 else if (backend2 == REG_UNKNOWN)
106 backend2 = REG_NULL;
107 break;
108 case 'R':
109 if (backend1 == REG_UNKNOWN) {
110 backend1 = REG_REMOTE;
111 remote1 = poptGetOptArg(pc);
112 } else if (backend2 == REG_UNKNOWN) {
113 backend2 = REG_REMOTE;
114 remote2 = poptGetOptArg(pc);
116 break;
121 ev_ctx = s4_event_context_init(NULL);
123 h1 = open_backend(pc, ev_ctx, cmdline_lp_ctx, backend1, remote1);
124 if (h1 == NULL)
125 return 1;
127 h2 = open_backend(pc, ev_ctx, cmdline_lp_ctx, backend2, remote2);
128 if (h2 == NULL)
129 return 1;
131 poptFreeContext(pc);
133 error = reg_dotreg_diff_save(ctx, outputfile, &callbacks, &callback_data);
134 if (!W_ERROR_IS_OK(error)) {
135 fprintf(stderr, "Problem saving registry diff to '%s': %s\n",
136 outputfile, win_errstr(error));
137 return -1;
140 error = reg_generate_diff(h1, h2, callbacks, callback_data);
141 if (!W_ERROR_IS_OK(error)) {
142 fprintf(stderr, "Unable to generate diff between keys: %s\n",
143 win_errstr(error));
144 return -1;
147 return 0;