s4-dsdb: create a static system_session context
[Samba/cd1.git] / source4 / utils / net / net_vampire.c
blob1bcc8db5f0e4adea64305e6508916cb3e4a298eb
1 /*
2 Samba Unix/Linux SMB client library
3 Distributed SMB/CIFS Server Management Utility
5 Copyright (C) 2004 Stefan Metzmacher <metze@samba.org>
6 Copyright (C) 2005 Andrew Bartlett <abartlet@samba.org>
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 "utils/net/net.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/samr.h"
26 #include "auth/auth.h"
27 #include "libcli/security/security.h"
28 #include "param/param.h"
29 #include "lib/events/events.h"
31 static int net_samdump_keytab_usage(struct net_context *ctx, int argc, const char **argv)
33 d_printf("net samdump keytab <keytab>\n");
34 return 0;
37 static int net_samdump_keytab_help(struct net_context *ctx, int argc, const char **argv)
39 d_printf("Dumps kerberos keys of a domain into a keytab.\n");
40 return 0;
43 static int net_samdump_keytab(struct net_context *ctx, int argc, const char **argv)
45 NTSTATUS status;
46 struct libnet_context *libnetctx;
47 struct libnet_SamDump_keytab r;
49 switch (argc) {
50 case 0:
51 return net_samdump_keytab_usage(ctx, argc, argv);
52 break;
53 case 1:
54 r.in.keytab_name = argv[0];
55 break;
58 libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
59 if (!libnetctx) {
60 return -1;
62 libnetctx->cred = ctx->credentials;
64 r.out.error_string = NULL;
65 r.in.machine_account = NULL;
66 r.in.binding_string = NULL;
68 status = libnet_SamDump_keytab(libnetctx, ctx, &r);
69 if (!NT_STATUS_IS_OK(status)) {
70 DEBUG(0,("libnet_SamDump returned %s: %s\n",
71 nt_errstr(status),
72 r.out.error_string));
73 return -1;
76 talloc_free(libnetctx);
78 return 0;
81 /* main function table */
82 static const struct net_functable net_samdump_functable[] = {
83 {"keytab", "dump keys into a keytab\n", net_samdump_keytab, net_samdump_keytab_usage},
84 {NULL, NULL, NULL, NULL}
87 int net_samdump(struct net_context *ctx, int argc, const char **argv)
89 NTSTATUS status;
90 struct libnet_context *libnetctx;
91 struct libnet_SamDump r;
92 int rc;
94 switch (argc) {
95 case 0:
96 break;
97 case 1:
98 default:
99 rc = net_run_function(ctx, argc, argv, net_samdump_functable,
100 net_samdump_usage);
101 return rc;
104 libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
105 if (!libnetctx) {
106 return -1;
108 libnetctx->cred = ctx->credentials;
110 r.out.error_string = NULL;
111 r.in.machine_account = NULL;
112 r.in.binding_string = NULL;
114 status = libnet_SamDump(libnetctx, ctx, &r);
115 if (!NT_STATUS_IS_OK(status)) {
116 DEBUG(0,("libnet_SamDump returned %s: %s\n",
117 nt_errstr(status),
118 r.out.error_string));
119 return -1;
122 talloc_free(libnetctx);
124 return 0;
127 int net_samdump_usage(struct net_context *ctx, int argc, const char **argv)
129 d_printf("net samdump\n");
130 d_printf("net samdump keytab <keytab>\n");
131 return 0;
134 int net_samdump_help(struct net_context *ctx, int argc, const char **argv)
136 d_printf("Dumps the sam of the domain we are joined to.\n");
137 return 0;
140 int net_samsync_ldb(struct net_context *ctx, int argc, const char **argv)
142 NTSTATUS status;
143 struct libnet_context *libnetctx;
144 struct libnet_samsync_ldb r;
146 libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
147 if (!libnetctx) {
148 return -1;
150 libnetctx->cred = ctx->credentials;
152 r.out.error_string = NULL;
153 r.in.machine_account = NULL;
154 r.in.binding_string = NULL;
156 /* Needed to override the ACLs on ldb */
157 r.in.session_info = system_session(ctx->lp_ctx);
159 status = libnet_samsync_ldb(libnetctx, libnetctx, &r);
160 if (!NT_STATUS_IS_OK(status)) {
161 DEBUG(0,("libnet_samsync_ldb returned %s: %s\n",
162 nt_errstr(status),
163 r.out.error_string));
164 return -1;
167 talloc_free(libnetctx);
169 return 0;
172 int net_samsync_ldb_usage(struct net_context *ctx, int argc, const char **argv)
174 d_printf("net samsync\n");
175 return 0;
178 int net_samsync_ldb_help(struct net_context *ctx, int argc, const char **argv)
180 d_printf("Synchronise into the local ldb the SAM of a domain.\n");
181 return 0;
184 int net_vampire(struct net_context *ctx, int argc, const char **argv)
186 NTSTATUS status;
187 struct libnet_context *libnetctx;
188 struct libnet_Vampire *r;
189 char *tmp, *targetdir = NULL;
190 const char *domain_name;
192 switch (argc) {
193 case 0: /* no args -> fail */
194 return net_vampire_usage(ctx, argc, argv);
195 case 1: /* only DOMAIN */
196 tmp = talloc_strdup(ctx, argv[0]);
197 break;
198 case 2: /* domain and target dir */
199 tmp = talloc_strdup(ctx, argv[0]);
200 targetdir = talloc_strdup(ctx, argv[1]);
201 break;
202 default: /* too many args -> fail */
203 return net_vampire_usage(ctx, argc, argv);
206 domain_name = tmp;
208 libnetctx = libnet_context_init(ctx->event_ctx, ctx->lp_ctx);
209 if (!libnetctx) {
210 return -1;
212 libnetctx->cred = ctx->credentials;
213 r = talloc(ctx, struct libnet_Vampire);
214 if (!r) {
215 return -1;
217 /* prepare parameters for the vampire */
218 r->in.netbios_name = lp_netbios_name(ctx->lp_ctx);
219 r->in.domain_name = domain_name;
220 r->in.targetdir = targetdir;
221 r->out.error_string = NULL;
223 /* do the domain vampire */
224 status = libnet_Vampire(libnetctx, r, r);
226 if (!NT_STATUS_IS_OK(status)) {
227 d_fprintf(stderr, "Vampire of domain failed: %s\n",
228 r->out.error_string ? r->out.error_string : nt_errstr(status));
229 talloc_free(r);
230 talloc_free(libnetctx);
231 return -1;
233 d_printf("Vampired domain %s (%s)\n", r->out.domain_name, dom_sid_string(ctx, r->out.domain_sid));
235 talloc_free(libnetctx);
236 return 0;
239 int net_vampire_usage(struct net_context *ctx, int argc, const char **argv)
241 d_printf("net vampire <domain> [options]\n");
242 return 0;
245 int net_vampire_help(struct net_context *ctx, int argc, const char **argv)
247 d_printf("Join and synchronise a remote AD domain to the local server.\n");
248 return 0;