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"
29 * Decrypt and extract the user's passwords.
31 * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted) passwords back into the structure
33 static NTSTATUS fix_user(TALLOC_CTX *mem_ctx,
34 struct creds_CredentialState *creds,
35 enum netr_SamDatabaseID database,
36 struct netr_DELTA_ENUM *delta,
40 uint32_t rid = delta->delta_id_union.rid;
41 struct netr_DELTA_USER *user = delta->delta_union.user;
42 struct samr_Password lm_hash;
43 struct samr_Password nt_hash;
44 const char *username = user->account_name.string;
47 if (user->lm_password_present) {
48 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
49 user->lmpassword = lm_hash;
52 if (user->nt_password_present) {
53 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
54 user->ntpassword = nt_hash;
57 if (user->user_private_info.SensitiveData) {
59 struct netr_USER_KEYS keys;
60 data.data = user->user_private_info.SensitiveData;
61 data.length = user->user_private_info.DataLength;
62 creds_arcfour_crypt(creds, data.data, data.length);
63 user->user_private_info.SensitiveData = data.data;
64 user->user_private_info.DataLength = data.length;
66 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
67 if (NT_STATUS_IS_OK(nt_status)) {
68 if (keys.keys.keys2.lmpassword.length == 16) {
69 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
70 user->lmpassword = lm_hash;
71 user->lm_password_present = True;
73 if (keys.keys.keys2.ntpassword.length == 16) {
74 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
75 user->ntpassword = nt_hash;
76 user->nt_password_present = True;
79 *error_string = talloc_asprintf(mem_ctx, "Failed to parse Sensitive Data for %s:\n", username);
80 dump_data(10, data.data, data.length);
88 * Decrypt and extract the secrets
90 * The writes decrypted secrets back into the structure
92 static NTSTATUS fix_secret(TALLOC_CTX *mem_ctx,
93 struct creds_CredentialState *creds,
94 enum netr_SamDatabaseID database,
95 struct netr_DELTA_ENUM *delta,
98 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
99 creds_arcfour_crypt(creds, secret->current_cipher.cipher_data,
100 secret->current_cipher.maxlen);
102 creds_arcfour_crypt(creds, secret->old_cipher.cipher_data,
103 secret->old_cipher.maxlen);
109 * Fix up the delta, dealing with encryption issues so that the final
110 * callback need only do the printing or application logic
113 static NTSTATUS fix_delta(TALLOC_CTX *mem_ctx,
114 struct creds_CredentialState *creds,
115 enum netr_SamDatabaseID database,
116 struct netr_DELTA_ENUM *delta,
119 NTSTATUS nt_status = NT_STATUS_OK;
120 *error_string = NULL;
121 switch (delta->delta_type) {
122 case NETR_DELTA_USER:
124 nt_status = fix_user(mem_ctx,
131 case NETR_DELTA_SECRET:
133 nt_status = fix_secret(mem_ctx,
146 NTSTATUS libnet_SamSync_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamSync *r)
148 NTSTATUS nt_status, dbsync_nt_status;
149 TALLOC_CTX *samsync_ctx, *loop_ctx, *delta_ctx;
150 struct creds_CredentialState *creds;
151 struct netr_DatabaseSync dbsync;
152 struct cli_credentials *machine_account;
153 struct dcerpc_binding *b;
154 struct dcerpc_pipe *p;
155 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
158 /* TODO: This is bogus */
159 const char **bindings = lp_passwordserver();
162 if (bindings && bindings[0]) {
163 binding = bindings[0];
165 return NT_STATUS_INVALID_PARAMETER;
168 samsync_ctx = talloc_named(mem_ctx, 0, "SamSync top context");
170 if (!r->machine_account) {
171 machine_account = cli_credentials_init(samsync_ctx);
172 if (!machine_account) {
173 talloc_free(samsync_ctx);
174 return NT_STATUS_NO_MEMORY;
176 cli_credentials_set_conf(machine_account);
177 nt_status = cli_credentials_set_machine_account(machine_account);
178 if (!NT_STATUS_IS_OK(nt_status)) {
179 r->error_string = talloc_strdup(mem_ctx, "Could not obtain machine account password - are we joined to the domain?");
180 talloc_free(samsync_ctx);
184 machine_account = r->machine_account;
187 if (cli_credentials_get_secure_channel_type(machine_account) != SEC_CHAN_BDC) {
189 = talloc_asprintf(mem_ctx,
190 "Our join to domain %s is not as a BDC (%d), please rejoin as a BDC",
192 cli_credentials_get_domain(machine_account),
193 cli_credentials_get_secure_channel_type(machine_account));
194 talloc_free(samsync_ctx);
195 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
198 /* Connect to DC (take a binding string for now) */
200 nt_status = dcerpc_parse_binding(samsync_ctx, binding, &b);
201 if (!NT_STATUS_IS_OK(nt_status)) {
202 r->error_string = talloc_asprintf(mem_ctx, "Bad binding string %s\n", binding);
203 talloc_free(samsync_ctx);
204 return NT_STATUS_INVALID_PARAMETER;
207 /* We like schannel */
208 b->flags &= ~DCERPC_AUTH_OPTIONS;
209 b->flags |= DCERPC_SCHANNEL | DCERPC_SEAL /* | DCERPC_SCHANNEL_128 */;
212 nt_status = dcerpc_pipe_connect_b(samsync_ctx, &p, b,
213 &dcerpc_table_netlogon,
214 machine_account, ctx->event_ctx);
216 if (!NT_STATUS_IS_OK(nt_status)) {
217 talloc_free(samsync_ctx);
221 /* get NETLOGON credentails */
223 nt_status = dcerpc_schannel_creds(p->conn->security_state.generic_state, samsync_ctx, &creds);
224 if (!NT_STATUS_IS_OK(nt_status)) {
225 r->error_string = talloc_strdup(mem_ctx, "Could not obtain NETLOGON credentials from DCERPC/GENSEC layer");
226 talloc_free(samsync_ctx);
230 dbsync.in.logon_server = talloc_asprintf(samsync_ctx, "\\\\%s", dcerpc_server_name(p));
231 dbsync.in.computername = cli_credentials_get_workstation(machine_account);
232 dbsync.in.preferredmaximumlength = (uint32_t)-1;
233 ZERO_STRUCT(dbsync.in.return_authenticator);
235 for (i=0;i< ARRAY_SIZE(database_ids); i++) {
236 dbsync.in.sync_context = 0;
237 dbsync.in.database_id = database_ids[i];
241 loop_ctx = talloc_named(samsync_ctx, 0, "DatabaseSync loop context");
242 creds_client_authenticator(creds, &dbsync.in.credential);
244 dbsync_nt_status = dcerpc_netr_DatabaseSync(p, loop_ctx, &dbsync);
245 if (!NT_STATUS_IS_OK(dbsync_nt_status) &&
246 !NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES)) {
247 r->error_string = talloc_asprintf(samsync_ctx, "DatabaseSync failed - %s", nt_errstr(nt_status));
248 talloc_free(samsync_ctx);
252 if (!creds_client_check(creds, &dbsync.out.return_authenticator.cred)) {
253 r->error_string = talloc_strdup(samsync_ctx, "Credential chaining failed");
254 talloc_free(samsync_ctx);
255 return NT_STATUS_ACCESS_DENIED;
258 dbsync.in.sync_context = dbsync.out.sync_context;
260 for (d=0; d < dbsync.out.delta_enum_array->num_deltas; d++) {
261 char *error_string = NULL;
262 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
263 nt_status = fix_delta(delta_ctx,
265 dbsync.in.database_id,
266 &dbsync.out.delta_enum_array->delta_enum[d],
268 if (!NT_STATUS_IS_OK(nt_status)) {
269 r->error_string = talloc_steal(samsync_ctx, error_string);
270 talloc_free(samsync_ctx);
273 nt_status = r->delta_fn(delta_ctx,
276 dbsync.in.database_id,
277 &dbsync.out.delta_enum_array->delta_enum[d],
279 if (!NT_STATUS_IS_OK(nt_status)) {
280 r->error_string = talloc_steal(samsync_ctx, error_string);
281 talloc_free(samsync_ctx);
284 talloc_free(delta_ctx);
286 talloc_free(loop_ctx);
287 } while (NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES));
288 nt_status = dbsync_nt_status;
290 talloc_free(samsync_ctx);