net: use netapi function to list users.
[samba.git] / source3 / utils / net_rpc_samsync.c
1 /*
2    Unix SMB/CIFS implementation.
3    dump the remote SAM using rpc samsync operations
4
5    Copyright (C) Andrew Tridgell 2002
6    Copyright (C) Tim Potter 2001,2002
7    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2005
8    Modified by Volker Lendecke 2002
9    Copyright (C) Jeremy Allison 2005.
10    Copyright (C) Guenther Deschner 2008.
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "utils/net.h"
28
29 /* dump sam database via samsync rpc calls */
30 NTSTATUS rpc_samdump_internals(struct net_context *c,
31                                 const DOM_SID *domain_sid,
32                                 const char *domain_name,
33                                 struct cli_state *cli,
34                                 struct rpc_pipe_client *pipe_hnd,
35                                 TALLOC_CTX *mem_ctx,
36                                 int argc,
37                                 const char **argv)
38 {
39         struct samsync_context *ctx = NULL;
40         NTSTATUS status;
41
42         status = libnet_samsync_init_context(mem_ctx,
43                                              domain_sid,
44                                              &ctx);
45         if (!NT_STATUS_IS_OK(status)) {
46                 return status;
47         }
48
49         ctx->mode               = NET_SAMSYNC_MODE_DUMP;
50         ctx->cli                = pipe_hnd;
51         ctx->delta_fn           = display_sam_entries;
52         ctx->domain_name        = domain_name;
53
54         libnet_samsync(SAM_DATABASE_DOMAIN, ctx);
55
56         libnet_samsync(SAM_DATABASE_BUILTIN, ctx);
57
58         libnet_samsync(SAM_DATABASE_PRIVS, ctx);
59
60         TALLOC_FREE(ctx);
61
62         return NT_STATUS_OK;
63 }
64
65 /**
66  * Basic usage function for 'net rpc vampire'
67  *
68  * @param c     A net_context structure
69  * @param argc  Standard main() style argc
70  * @param argc  Standard main() style argv.  Initial components are already
71  *              stripped
72  **/
73
74 int rpc_vampire_usage(struct net_context *c, int argc, const char **argv)
75 {
76         d_printf("net rpc vampire ([ldif [<ldif-filename>] | [keytab] [<keytab-filename]) [options]\n"
77                  "\t to pull accounts from a remote PDC where we are a BDC\n"
78                  "\t\t no args puts accounts in local passdb from smb.conf\n"
79                  "\t\t ldif - put accounts in ldif format (file defaults to "
80                  "/tmp/tmp.ldif)\n"
81                  "\t\t keytab - put account passwords in krb5 keytab (defaults "
82                  "to system keytab)\n");
83
84         net_common_flags_usage(c, argc, argv);
85         return -1;
86 }
87
88
89 /* dump sam database via samsync rpc calls */
90 NTSTATUS rpc_vampire_internals(struct net_context *c,
91                                 const DOM_SID *domain_sid,
92                                 const char *domain_name,
93                                 struct cli_state *cli,
94                                 struct rpc_pipe_client *pipe_hnd,
95                                 TALLOC_CTX *mem_ctx,
96                                 int argc,
97                                 const char **argv)
98 {
99         NTSTATUS result;
100         struct samsync_context *ctx = NULL;
101
102         if (!sid_equal(domain_sid, get_global_sam_sid())) {
103                 d_printf("Cannot import users from %s at this time, "
104                          "as the current domain:\n\t%s: %s\nconflicts "
105                          "with the remote domain\n\t%s: %s\n"
106                          "Perhaps you need to set: \n\n\tsecurity=user\n\t"
107                          "workgroup=%s\n\n in your smb.conf?\n",
108                          domain_name,
109                          get_global_sam_name(),
110                          sid_string_dbg(get_global_sam_sid()),
111                          domain_name,
112                          sid_string_dbg(domain_sid),
113                          domain_name);
114                 return NT_STATUS_UNSUCCESSFUL;
115         }
116
117         result = libnet_samsync_init_context(mem_ctx,
118                                              domain_sid,
119                                              &ctx);
120         if (!NT_STATUS_IS_OK(result)) {
121                 return result;
122         }
123
124         ctx->mode               = NET_SAMSYNC_MODE_FETCH_PASSDB;
125         ctx->cli                = pipe_hnd;
126         ctx->delta_fn           = fetch_sam_entries;
127         ctx->domain_name        = domain_name;
128
129         /* fetch domain */
130         result = libnet_samsync(SAM_DATABASE_DOMAIN, ctx);
131
132         if (!NT_STATUS_IS_OK(result) && ctx->error_message) {
133                 d_fprintf(stderr, "%s\n", ctx->error_message);
134                 goto fail;
135         }
136
137         if (ctx->result_message) {
138                 d_fprintf(stdout, "%s\n", ctx->result_message);
139         }
140
141         /* fetch builtin */
142         ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
143         ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
144         result = libnet_samsync(SAM_DATABASE_BUILTIN, ctx);
145
146         if (!NT_STATUS_IS_OK(result) && ctx->error_message) {
147                 d_fprintf(stderr, "%s\n", ctx->error_message);
148                 goto fail;
149         }
150
151         if (ctx->result_message) {
152                 d_fprintf(stdout, "%s\n", ctx->result_message);
153         }
154
155  fail:
156         TALLOC_FREE(ctx);
157         return result;
158 }
159
160 NTSTATUS rpc_vampire_ldif_internals(struct net_context *c,
161                                     const DOM_SID *domain_sid,
162                                     const char *domain_name,
163                                     struct cli_state *cli,
164                                     struct rpc_pipe_client *pipe_hnd,
165                                     TALLOC_CTX *mem_ctx,
166                                     int argc,
167                                     const char **argv)
168 {
169         NTSTATUS status;
170         struct samsync_context *ctx = NULL;
171
172         status = libnet_samsync_init_context(mem_ctx,
173                                              domain_sid,
174                                              &ctx);
175         if (!NT_STATUS_IS_OK(status)) {
176                 return status;
177         }
178
179         if (argc >= 1) {
180                 ctx->output_filename = argv[0];
181         }
182
183         ctx->mode               = NET_SAMSYNC_MODE_FETCH_LDIF;
184         ctx->cli                = pipe_hnd;
185         ctx->delta_fn           = fetch_sam_entries_ldif;
186         ctx->domain_name        = domain_name;
187
188         /* fetch domain */
189         status = libnet_samsync(SAM_DATABASE_DOMAIN, ctx);
190
191         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
192                 d_fprintf(stderr, "%s\n", ctx->error_message);
193                 goto fail;
194         }
195
196         if (ctx->result_message) {
197                 d_fprintf(stdout, "%s\n", ctx->result_message);
198         }
199
200         /* fetch builtin */
201         ctx->domain_sid = sid_dup_talloc(mem_ctx, &global_sid_Builtin);
202         ctx->domain_sid_str = sid_string_talloc(mem_ctx, ctx->domain_sid);
203         status = libnet_samsync(SAM_DATABASE_BUILTIN, ctx);
204
205         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
206                 d_fprintf(stderr, "%s\n", ctx->error_message);
207                 goto fail;
208         }
209
210         if (ctx->result_message) {
211                 d_fprintf(stdout, "%s\n", ctx->result_message);
212         }
213
214  fail:
215         TALLOC_FREE(ctx);
216         return status;
217 }
218
219 int rpc_vampire_ldif(struct net_context *c, int argc, const char **argv)
220 {
221         if (c->display_usage) {
222                 d_printf("Usage:\n"
223                          "net rpc vampire ldif\n"
224                          "    Dump remote SAM database to LDIF file or stdout\n");
225                 return 0;
226         }
227
228         return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id, 0,
229                                rpc_vampire_ldif_internals, argc, argv);
230 }
231
232
233 NTSTATUS rpc_vampire_keytab_internals(struct net_context *c,
234                                       const DOM_SID *domain_sid,
235                                       const char *domain_name,
236                                       struct cli_state *cli,
237                                       struct rpc_pipe_client *pipe_hnd,
238                                       TALLOC_CTX *mem_ctx,
239                                       int argc,
240                                       const char **argv)
241 {
242         NTSTATUS status;
243         struct samsync_context *ctx = NULL;
244
245         status = libnet_samsync_init_context(mem_ctx,
246                                              domain_sid,
247                                              &ctx);
248         if (!NT_STATUS_IS_OK(status)) {
249                 return status;
250         }
251
252         if (argc >= 1) {
253                 ctx->output_filename = argv[0];
254         }
255
256         ctx->mode               = NET_SAMSYNC_MODE_FETCH_KEYTAB;
257         ctx->cli                = pipe_hnd;
258         ctx->delta_fn           = fetch_sam_entries_keytab;
259         ctx->domain_name        = domain_name;
260         ctx->username           = c->opt_user_name;
261         ctx->password           = c->opt_password;
262
263         /* fetch domain */
264         status = libnet_samsync(SAM_DATABASE_DOMAIN, ctx);
265
266         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
267                 d_fprintf(stderr, "%s\n", ctx->error_message);
268                 goto out;
269         }
270
271         if (ctx->result_message) {
272                 d_fprintf(stdout, "%s\n", ctx->result_message);
273         }
274
275  out:
276         TALLOC_FREE(ctx);
277
278         return status;
279 }
280
281 static NTSTATUS rpc_vampire_keytab_ds_internals(struct net_context *c,
282                                                 const DOM_SID *domain_sid,
283                                                 const char *domain_name,
284                                                 struct cli_state *cli,
285                                                 struct rpc_pipe_client *pipe_hnd,
286                                                 TALLOC_CTX *mem_ctx,
287                                                 int argc,
288                                                 const char **argv)
289 {
290         NTSTATUS status;
291         struct dssync_context *ctx = NULL;
292
293         status = libnet_dssync_init_context(mem_ctx,
294                                             &ctx);
295         if (!NT_STATUS_IS_OK(status)) {
296                 return status;
297         }
298
299         ctx->force_full_replication = c->opt_force_full_repl ? true : false;
300         ctx->clean_old_entries = c->opt_clean_old_entries ? true : false;
301
302         if (argc >= 1) {
303                 ctx->output_filename = argv[0];
304         }
305         if (argc >= 2) {
306                 ctx->object_dns = &argv[1];
307                 ctx->object_count = argc - 1;
308                 ctx->single_object_replication = c->opt_single_obj_repl ? true
309                                                                         : false;
310         }
311
312         ctx->cli                = pipe_hnd;
313         ctx->domain_name        = domain_name;
314         ctx->ops                = &libnet_dssync_keytab_ops;
315
316         status = libnet_dssync(mem_ctx, ctx);
317         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
318                 d_fprintf(stderr, "%s\n", ctx->error_message);
319                 goto out;
320         }
321
322         if (ctx->result_message) {
323                 d_fprintf(stdout, "%s\n", ctx->result_message);
324         }
325
326  out:
327         TALLOC_FREE(ctx);
328
329         return status;
330 }
331
332 /**
333  * Basic function for 'net rpc vampire keytab'
334  *
335  * @param c     A net_context structure
336  * @param argc  Standard main() style argc
337  * @param argc  Standard main() style argv.  Initial components are already
338  *              stripped
339  **/
340
341 int rpc_vampire_keytab(struct net_context *c, int argc, const char **argv)
342 {
343         int ret = 0;
344
345         if (c->display_usage) {
346                 d_printf("Usage:\n"
347                          "net rpc vampire keytab\n"
348                          "    Dump remote SAM database to Kerberos keytab file\n");
349                 return 0;
350         }
351
352         ret = run_rpc_command(c, NULL, &ndr_table_drsuapi.syntax_id,
353                               NET_FLAGS_SEAL,
354                               rpc_vampire_keytab_ds_internals, argc, argv);
355         if (ret == 0) {
356                 return 0;
357         }
358
359         return run_rpc_command(c, NULL, &ndr_table_netlogon.syntax_id, 0,
360                                rpc_vampire_keytab_internals,
361                                argc, argv);
362 }