2 Unix SMB/CIFS implementation.
4 Extract the user/system database from a remote SamSync server
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libnet/libnet.h"
26 #include "auth/gensec/schannel_proto.h"
27 #include "libcli/auth/proto.h"
31 * Decrypt and extract the user's passwords.
33 * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted) passwords back into the structure
35 static NTSTATUS fix_user(TALLOC_CTX *mem_ctx,
36 struct creds_CredentialState *creds,
37 enum netr_SamDatabaseID database,
38 struct netr_DELTA_ENUM *delta,
42 uint32_t rid = delta->delta_id_union.rid;
43 struct netr_DELTA_USER *user = delta->delta_union.user;
44 struct samr_Password lm_hash;
45 struct samr_Password nt_hash;
46 const char *username = user->account_name.string;
49 if (user->lm_password_present) {
50 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
51 user->lmpassword = lm_hash;
54 if (user->nt_password_present) {
55 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
56 user->ntpassword = nt_hash;
59 if (user->user_private_info.SensitiveData) {
61 struct netr_USER_KEYS keys;
62 data.data = user->user_private_info.SensitiveData;
63 data.length = user->user_private_info.DataLength;
64 creds_arcfour_crypt(creds, data.data, data.length);
65 user->user_private_info.SensitiveData = data.data;
66 user->user_private_info.DataLength = data.length;
68 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
69 if (NT_STATUS_IS_OK(nt_status)) {
70 if (keys.keys.keys2.lmpassword.length == 16) {
71 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
72 user->lmpassword = lm_hash;
73 user->lm_password_present = True;
75 if (keys.keys.keys2.ntpassword.length == 16) {
76 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
77 user->ntpassword = nt_hash;
78 user->nt_password_present = True;
81 *error_string = talloc_asprintf(mem_ctx, "Failed to parse Sensitive Data for %s:\n", username);
82 dump_data(10, data.data, data.length);
90 * Decrypt and extract the secrets
92 * The writes decrypted secrets back into the structure
94 static NTSTATUS fix_secret(TALLOC_CTX *mem_ctx,
95 struct creds_CredentialState *creds,
96 enum netr_SamDatabaseID database,
97 struct netr_DELTA_ENUM *delta,
100 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
101 creds_arcfour_crypt(creds, secret->current_cipher.cipher_data,
102 secret->current_cipher.maxlen);
104 creds_arcfour_crypt(creds, secret->old_cipher.cipher_data,
105 secret->old_cipher.maxlen);
111 * Fix up the delta, dealing with encryption issues so that the final
112 * callback need only do the printing or application logic
115 static NTSTATUS fix_delta(TALLOC_CTX *mem_ctx,
116 struct creds_CredentialState *creds,
117 enum netr_SamDatabaseID database,
118 struct netr_DELTA_ENUM *delta,
121 NTSTATUS nt_status = NT_STATUS_OK;
122 *error_string = NULL;
123 switch (delta->delta_type) {
124 case NETR_DELTA_USER:
126 nt_status = fix_user(mem_ctx,
133 case NETR_DELTA_SECRET:
135 nt_status = fix_secret(mem_ctx,
148 NTSTATUS libnet_SamSync_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamSync *r)
150 NTSTATUS nt_status, dbsync_nt_status;
151 TALLOC_CTX *samsync_ctx, *loop_ctx, *delta_ctx;
152 struct creds_CredentialState *creds;
153 struct netr_DatabaseSync dbsync;
154 struct cli_credentials *machine_account;
155 struct dcerpc_pipe *p;
156 struct libnet_context *machine_net_ctx;
157 struct libnet_RpcConnectDCInfo *c;
158 struct libnet_SamSync_state *state;
159 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
162 samsync_ctx = talloc_named(mem_ctx, 0, "SamSync top context");
164 if (!r->in.machine_account) {
165 machine_account = cli_credentials_init(samsync_ctx);
166 if (!machine_account) {
167 talloc_free(samsync_ctx);
168 return NT_STATUS_NO_MEMORY;
170 cli_credentials_set_conf(machine_account);
171 nt_status = cli_credentials_set_machine_account(machine_account);
172 if (!NT_STATUS_IS_OK(nt_status)) {
173 r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain machine account password - are we joined to the domain?");
174 talloc_free(samsync_ctx);
178 machine_account = r->in.machine_account;
181 /* We cannot do this unless we are a BDC. Check, before we get odd errors later */
182 if (cli_credentials_get_secure_channel_type(machine_account) != SEC_CHAN_BDC) {
184 = talloc_asprintf(mem_ctx,
185 "Our join to domain %s is not as a BDC (%d), please rejoin as a BDC",
187 cli_credentials_get_domain(machine_account),
188 cli_credentials_get_secure_channel_type(machine_account));
189 talloc_free(samsync_ctx);
190 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
193 c = talloc(samsync_ctx, struct libnet_RpcConnectDCInfo);
195 r->out.error_string = NULL;
196 talloc_free(samsync_ctx);
197 return NT_STATUS_NO_MEMORY;
200 if (r->in.binding_string) {
201 c->level = LIBNET_RPC_CONNECT_BINDING;
202 c->in.binding = r->in.binding_string;
204 /* prepare connect to the NETLOGON pipe of PDC */
205 c->level = LIBNET_RPC_CONNECT_PDC;
206 c->in.name = cli_credentials_get_domain(machine_account);
208 c->in.dcerpc_iface = &dcerpc_table_netlogon;
210 /* We must do this as the machine, not as any command-line
211 * user. So we override the credentials in the
213 machine_net_ctx = talloc(samsync_ctx, struct libnet_context);
214 if (!machine_net_ctx) {
215 r->out.error_string = NULL;
216 talloc_free(samsync_ctx);
217 return NT_STATUS_NO_MEMORY;
219 *machine_net_ctx = *ctx;
220 machine_net_ctx->cred = machine_account;
222 /* connect to the NETLOGON pipe of the PDC */
223 nt_status = libnet_RpcConnectDCInfo(machine_net_ctx, c);
224 if (!NT_STATUS_IS_OK(nt_status)) {
225 if (r->in.binding_string) {
226 r->out.error_string = talloc_asprintf(mem_ctx,
227 "Connection to NETLOGON pipe of DC %s failed: %s",
228 r->in.binding_string, c->out.error_string);
230 r->out.error_string = talloc_asprintf(mem_ctx,
231 "Connection to NETLOGON pipe of DC for %s failed: %s",
232 c->in.name, c->out.error_string);
234 talloc_free(samsync_ctx);
238 /* This makes a new pipe, on which we can do schannel. We
239 * should do this in the RpcConnect code, but the abstaction
240 * layers do not suit yet */
242 nt_status = dcerpc_secondary_connection(c->out.dcerpc_pipe, &p,
243 c->out.dcerpc_pipe->binding);
245 if (!NT_STATUS_IS_OK(nt_status)) {
246 r->out.error_string = talloc_asprintf(mem_ctx,
247 "Secondary connection to NETLOGON pipe of DC %s failed: %s",
248 dcerpc_server_name(p), nt_errstr(nt_status));
249 talloc_free(samsync_ctx);
253 nt_status = dcerpc_bind_auth_schannel(samsync_ctx, p, &dcerpc_table_netlogon,
254 machine_account, DCERPC_AUTH_LEVEL_PRIVACY);
256 if (!NT_STATUS_IS_OK(nt_status)) {
257 r->out.error_string = talloc_asprintf(mem_ctx,
258 "SCHANNEL authentication to NETLOGON pipe of DC %s failed: %s",
259 dcerpc_server_name(p), nt_errstr(nt_status));
260 talloc_free(samsync_ctx);
264 state = talloc(samsync_ctx, struct libnet_SamSync_state);
266 r->out.error_string = NULL;
267 talloc_free(samsync_ctx);
271 state->domain_name = c->out.domain_name;
272 state->domain_sid = c->out.domain_sid;
273 state->realm = c->out.realm;
274 state->domain_guid = c->out.guid;
275 state->machine_net_ctx = machine_net_ctx;
276 state->netlogon_pipe = p;
278 /* initialise the callback layer. It may wish to contact the
279 * server with ldap, now we know the name */
283 nt_status = r->in.init_fn(samsync_ctx,
287 if (!NT_STATUS_IS_OK(nt_status)) {
288 r->out.error_string = talloc_steal(mem_ctx, error_string);
289 talloc_free(samsync_ctx);
294 /* get NETLOGON credentails */
296 nt_status = dcerpc_schannel_creds(p->conn->security_state.generic_state, samsync_ctx, &creds);
297 if (!NT_STATUS_IS_OK(nt_status)) {
298 r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain NETLOGON credentials from DCERPC/GENSEC layer");
299 talloc_free(samsync_ctx);
303 /* Setup details for the syncronisation */
304 dbsync.in.logon_server = talloc_asprintf(samsync_ctx, "\\\\%s", dcerpc_server_name(p));
305 dbsync.in.computername = cli_credentials_get_workstation(machine_account);
306 dbsync.in.preferredmaximumlength = (uint32_t)-1;
307 ZERO_STRUCT(dbsync.in.return_authenticator);
309 for (i=0;i< ARRAY_SIZE(database_ids); i++) {
310 dbsync.in.sync_context = 0;
311 dbsync.in.database_id = database_ids[i];
315 loop_ctx = talloc_named(samsync_ctx, 0, "DatabaseSync loop context");
316 creds_client_authenticator(creds, &dbsync.in.credential);
318 dbsync_nt_status = dcerpc_netr_DatabaseSync(p, loop_ctx, &dbsync);
319 if (!NT_STATUS_IS_OK(dbsync_nt_status) &&
320 !NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES)) {
321 r->out.error_string = talloc_asprintf(mem_ctx, "DatabaseSync failed - %s", nt_errstr(nt_status));
322 talloc_free(samsync_ctx);
326 if (!creds_client_check(creds, &dbsync.out.return_authenticator.cred)) {
327 r->out.error_string = talloc_strdup(mem_ctx, "Credential chaining on incoming DatabaseSync failed");
328 talloc_free(samsync_ctx);
329 return NT_STATUS_ACCESS_DENIED;
332 dbsync.in.sync_context = dbsync.out.sync_context;
334 /* For every single remote 'delta' entry: */
335 for (d=0; d < dbsync.out.delta_enum_array->num_deltas; d++) {
336 char *error_string = NULL;
337 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
338 /* 'Fix' elements, by decrypting and
339 * de-obfustiating the data */
340 nt_status = fix_delta(delta_ctx,
342 dbsync.in.database_id,
343 &dbsync.out.delta_enum_array->delta_enum[d],
345 if (!NT_STATUS_IS_OK(nt_status)) {
346 r->out.error_string = talloc_steal(mem_ctx, error_string);
347 talloc_free(samsync_ctx);
351 /* Now call the callback. This will
352 * do something like print the data or
354 nt_status = r->in.delta_fn(delta_ctx,
356 dbsync.in.database_id,
357 &dbsync.out.delta_enum_array->delta_enum[d],
359 if (!NT_STATUS_IS_OK(nt_status)) {
360 r->out.error_string = talloc_steal(mem_ctx, error_string);
361 talloc_free(samsync_ctx);
364 talloc_free(delta_ctx);
366 talloc_free(loop_ctx);
367 } while (NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES));
369 if (!NT_STATUS_IS_OK(dbsync_nt_status)) {
370 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_SamSync_netlogon failed: unexpected inconsistancy. Should not get error %s here", nt_errstr(nt_status));
371 talloc_free(samsync_ctx);
372 return dbsync_nt_status;
374 nt_status = NT_STATUS_OK;
376 talloc_free(samsync_ctx);