smbstatus: Use share_mode_data->leases
[garming/samba-autobuild/.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 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "../librpc/gen_ndr/ndr_drsuapi.h"
30 #include "libnet/libnet_dssync.h"
31 #include "../libcli/security/security.h"
32 #include "passdb/machine_sid.h"
33
34 /**
35  * Basic usage function for 'net rpc vampire'
36  *
37  * @param c     A net_context structure
38  * @param argc  Standard main() style argc
39  * @param argc  Standard main() style argv.  Initial components are already
40  *              stripped
41  **/
42
43 int rpc_vampire_usage(struct net_context *c, int argc, const char **argv)
44 {
45         d_printf(_("net rpc vampire ([ldif [<ldif-filename>] | [keytab] "
46                    "[<keytab-filename]) [options]\n"
47                    "\t to pull accounts from a remote PDC where we are a BDC\n"
48                    "\t\t no args puts accounts in local passdb from smb.conf\n"
49                    "\t\t ldif - put accounts in ldif format (file defaults to "
50                    "/tmp/tmp.ldif)\n"
51                    "\t\t keytab - put account passwords in krb5 keytab "
52                    "(defaults to system keytab)\n"));
53
54         net_common_flags_usage(c, argc, argv);
55         return -1;
56 }
57
58 static NTSTATUS rpc_vampire_ds_internals(struct net_context *c,
59                                          const struct dom_sid *domain_sid,
60                                          const char *domain_name,
61                                          struct cli_state *cli,
62                                          struct rpc_pipe_client *pipe_hnd,
63                                          TALLOC_CTX *mem_ctx,
64                                          int argc,
65                                          const char **argv)
66 {
67         NTSTATUS status;
68         struct dssync_context *ctx = NULL;
69
70         if (!dom_sid_equal(domain_sid, get_global_sam_sid())) {
71                 d_printf(_("Cannot import users from %s at this time, "
72                            "as the current domain:\n\t%s: %s\nconflicts "
73                            "with the remote domain\n\t%s: %s\n"
74                            "Perhaps you need to set: \n\n\tsecurity=user\n\t"
75                            "workgroup=%s\n\n in your smb.conf?\n"),
76                          domain_name,
77                          get_global_sam_name(),
78                          sid_string_dbg(get_global_sam_sid()),
79                          domain_name,
80                          sid_string_dbg(domain_sid),
81                          domain_name);
82                 return NT_STATUS_UNSUCCESSFUL;
83         }
84
85         status = libnet_dssync_init_context(mem_ctx,
86                                             &ctx);
87         if (!NT_STATUS_IS_OK(status)) {
88                 return status;
89         }
90
91         ctx->cli                = pipe_hnd;
92         ctx->domain_name        = domain_name;
93         ctx->ops                = &libnet_dssync_passdb_ops;
94
95         status = libnet_dssync(mem_ctx, ctx);
96         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
97                 d_fprintf(stderr, "%s\n", ctx->error_message);
98                 goto out;
99         }
100
101         if (ctx->result_message) {
102                 d_fprintf(stdout, "%s\n", ctx->result_message);
103         }
104
105  out:
106         TALLOC_FREE(ctx);
107
108         return status;
109 }
110
111 int rpc_vampire_passdb(struct net_context *c, int argc, const char **argv)
112 {
113         int ret = 0;
114         NTSTATUS status;
115         struct cli_state *cli = NULL;
116         struct net_dc_info dc_info;
117
118         if (c->display_usage) {
119                 d_printf(  "%s\n"
120                            "net rpc vampire passdb\n"
121                            "    %s\n",
122                          _("Usage:"),
123                          _("Dump remote SAM database to passdb"));
124                 return 0;
125         }
126
127         status = net_make_ipc_connection(c, 0, &cli);
128         if (!NT_STATUS_IS_OK(status)) {
129                 return -1;
130         }
131
132         status = net_scan_dc(c, cli, &dc_info);
133         if (!NT_STATUS_IS_OK(status)) {
134                 return -1;
135         }
136
137         if (!dc_info.is_ad) {
138                 printf(_("DC is not running Active Directory, exiting\n"));
139                 return -1;
140         }
141
142         if (!c->opt_force) {
143                 d_printf(  "%s\n"
144                            "net rpc vampire passdb\n"
145                            "    %s\n",
146                          _("Usage:"),
147                          _("Should not be used against Active Directory, maybe use --force"));
148                 return -1;
149         }
150
151         ret = run_rpc_command(c, cli, &ndr_table_drsuapi,
152                               NET_FLAGS_SEAL | NET_FLAGS_TCP,
153                               rpc_vampire_ds_internals, argc, argv);
154         return ret;
155 }
156
157 static NTSTATUS rpc_vampire_keytab_ds_internals(struct net_context *c,
158                                                 const struct dom_sid *domain_sid,
159                                                 const char *domain_name,
160                                                 struct cli_state *cli,
161                                                 struct rpc_pipe_client *pipe_hnd,
162                                                 TALLOC_CTX *mem_ctx,
163                                                 int argc,
164                                                 const char **argv)
165 {
166         NTSTATUS status;
167         struct dssync_context *ctx = NULL;
168
169         status = libnet_dssync_init_context(mem_ctx,
170                                             &ctx);
171         if (!NT_STATUS_IS_OK(status)) {
172                 return status;
173         }
174
175         ctx->force_full_replication = c->opt_force_full_repl ? true : false;
176         ctx->clean_old_entries = c->opt_clean_old_entries ? true : false;
177
178         if (argc < 1) {
179                 /* the caller should ensure that a filename is provided */
180                 return NT_STATUS_INVALID_PARAMETER;
181         } else {
182                 ctx->output_filename = argv[0];
183         }
184
185         if (argc >= 2) {
186                 ctx->object_dns = &argv[1];
187                 ctx->object_count = argc - 1;
188                 ctx->single_object_replication = c->opt_single_obj_repl ? true
189                                                                         : false;
190         }
191
192         ctx->cli                = pipe_hnd;
193         ctx->domain_name        = domain_name;
194         ctx->ops                = &libnet_dssync_keytab_ops;
195
196         status = libnet_dssync(mem_ctx, ctx);
197         if (!NT_STATUS_IS_OK(status) && ctx->error_message) {
198                 d_fprintf(stderr, "%s\n", ctx->error_message);
199                 goto out;
200         }
201
202         if (ctx->result_message) {
203                 d_fprintf(stdout, "%s\n", ctx->result_message);
204         }
205
206  out:
207         TALLOC_FREE(ctx);
208
209         return status;
210 }
211
212 /**
213  * Basic function for 'net rpc vampire keytab'
214  *
215  * @param c     A net_context structure
216  * @param argc  Standard main() style argc
217  * @param argc  Standard main() style argv.  Initial components are already
218  *              stripped
219  **/
220
221 int rpc_vampire_keytab(struct net_context *c, int argc, const char **argv)
222 {
223         int ret = 0;
224         NTSTATUS status;
225         struct cli_state *cli = NULL;
226         struct net_dc_info dc_info;
227
228         if (c->display_usage || (argc < 1)) {
229                 d_printf("%s\n%s",
230                          _("Usage:"),
231                          _("net rpc vampire keytab <keytabfile>\n"
232                            "    Dump remote SAM database to Kerberos keytab "
233                            "file\n"));
234                 return 0;
235         }
236
237         status = net_make_ipc_connection(c, 0, &cli);
238         if (!NT_STATUS_IS_OK(status)) {
239                 return -1;
240         }
241
242         status = net_scan_dc(c, cli, &dc_info);
243         if (!NT_STATUS_IS_OK(status)) {
244                 return -1;
245         }
246
247         if (!dc_info.is_ad) {
248                 printf(_("DC is not running Active Directory, exiting\n"));
249                 return -1;
250         }
251
252         ret = run_rpc_command(c, cli, &ndr_table_drsuapi,
253                               NET_FLAGS_SEAL | NET_FLAGS_TCP,
254                               rpc_vampire_keytab_ds_internals, argc, argv);
255         return ret;
256 }