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