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"
30 * Decrypt and extract the user's passwords.
32 * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted) passwords back into the structure
34 static NTSTATUS fix_user(TALLOC_CTX *mem_ctx,
35 struct creds_CredentialState *creds,
36 enum netr_SamDatabaseID database,
37 struct netr_DELTA_ENUM *delta,
41 uint32_t rid = delta->delta_id_union.rid;
42 struct netr_DELTA_USER *user = delta->delta_union.user;
43 struct samr_Password lm_hash;
44 struct samr_Password nt_hash;
45 const char *username = user->account_name.string;
48 if (user->lm_password_present) {
49 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
50 user->lmpassword = lm_hash;
53 if (user->nt_password_present) {
54 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
55 user->ntpassword = nt_hash;
58 if (user->user_private_info.SensitiveData) {
60 struct netr_USER_KEYS keys;
61 data.data = user->user_private_info.SensitiveData;
62 data.length = user->user_private_info.DataLength;
63 creds_arcfour_crypt(creds, data.data, data.length);
64 user->user_private_info.SensitiveData = data.data;
65 user->user_private_info.DataLength = data.length;
67 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
68 if (NT_STATUS_IS_OK(nt_status)) {
69 if (keys.keys.keys2.lmpassword.length == 16) {
70 sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
71 user->lmpassword = lm_hash;
72 user->lm_password_present = True;
74 if (keys.keys.keys2.ntpassword.length == 16) {
75 sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
76 user->ntpassword = nt_hash;
77 user->nt_password_present = True;
80 *error_string = talloc_asprintf(mem_ctx, "Failed to parse Sensitive Data for %s:\n", username);
81 dump_data(10, data.data, data.length);
89 * Decrypt and extract the secrets
91 * The writes decrypted secrets back into the structure
93 static NTSTATUS fix_secret(TALLOC_CTX *mem_ctx,
94 struct creds_CredentialState *creds,
95 enum netr_SamDatabaseID database,
96 struct netr_DELTA_ENUM *delta,
99 struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
100 creds_arcfour_crypt(creds, secret->current_cipher.cipher_data,
101 secret->current_cipher.maxlen);
103 creds_arcfour_crypt(creds, secret->old_cipher.cipher_data,
104 secret->old_cipher.maxlen);
110 * Fix up the delta, dealing with encryption issues so that the final
111 * callback need only do the printing or application logic
114 static NTSTATUS fix_delta(TALLOC_CTX *mem_ctx,
115 struct creds_CredentialState *creds,
116 enum netr_SamDatabaseID database,
117 struct netr_DELTA_ENUM *delta,
120 NTSTATUS nt_status = NT_STATUS_OK;
121 *error_string = NULL;
122 switch (delta->delta_type) {
123 case NETR_DELTA_USER:
125 nt_status = fix_user(mem_ctx,
132 case NETR_DELTA_SECRET:
134 nt_status = fix_secret(mem_ctx,
147 NTSTATUS libnet_SamSync_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamSync *r)
149 NTSTATUS nt_status, dbsync_nt_status;
150 TALLOC_CTX *samsync_ctx, *loop_ctx, *delta_ctx;
151 struct creds_CredentialState *creds;
152 struct netr_DatabaseSync dbsync;
153 struct cli_credentials *machine_account;
154 struct dcerpc_pipe *p;
155 struct libnet_context *machine_net_ctx;
156 struct libnet_RpcConnectDCInfo *c;
157 struct libnet_SamSync_state *state;
158 const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS};
161 samsync_ctx = talloc_named(mem_ctx, 0, "SamSync top context");
163 if (!r->in.machine_account) {
164 machine_account = cli_credentials_init(samsync_ctx);
165 if (!machine_account) {
166 talloc_free(samsync_ctx);
167 return NT_STATUS_NO_MEMORY;
169 cli_credentials_set_conf(machine_account);
170 nt_status = cli_credentials_set_machine_account(machine_account);
171 if (!NT_STATUS_IS_OK(nt_status)) {
172 r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain machine account password - are we joined to the domain?");
173 talloc_free(samsync_ctx);
177 machine_account = r->in.machine_account;
180 /* We cannot do this unless we are a BDC. Check, before we get odd errors later */
181 if (cli_credentials_get_secure_channel_type(machine_account) != SEC_CHAN_BDC) {
183 = talloc_asprintf(mem_ctx,
184 "Our join to domain %s is not as a BDC (%d), please rejoin as a BDC",
186 cli_credentials_get_domain(machine_account),
187 cli_credentials_get_secure_channel_type(machine_account));
188 talloc_free(samsync_ctx);
189 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
192 c = talloc(samsync_ctx, struct libnet_RpcConnectDCInfo);
194 r->out.error_string = NULL;
195 talloc_free(samsync_ctx);
196 return NT_STATUS_NO_MEMORY;
199 if (r->in.binding_string) {
200 c->level = LIBNET_RPC_CONNECT_BINDING;
201 c->in.binding = r->in.binding_string;
203 /* prepare connect to the NETLOGON pipe of PDC */
204 c->level = LIBNET_RPC_CONNECT_PDC;
205 c->in.name = cli_credentials_get_domain(machine_account);
207 c->in.dcerpc_iface = &dcerpc_table_netlogon;
209 /* We must do this as the machine, not as any command-line
210 * user. So we override the credentials in the
212 machine_net_ctx = talloc(samsync_ctx, struct libnet_context);
213 if (!machine_net_ctx) {
214 r->out.error_string = NULL;
215 talloc_free(samsync_ctx);
216 return NT_STATUS_NO_MEMORY;
218 *machine_net_ctx = *ctx;
219 machine_net_ctx->cred = machine_account;
221 /* connect to the NETLOGON pipe of the PDC */
222 nt_status = libnet_RpcConnectDCInfo(machine_net_ctx, c);
223 if (!NT_STATUS_IS_OK(nt_status)) {
224 if (r->in.binding_string) {
225 r->out.error_string = talloc_asprintf(mem_ctx,
226 "Connection to NETLOGON pipe of DC %s failed: %s",
227 r->in.binding_string, c->out.error_string);
229 r->out.error_string = talloc_asprintf(mem_ctx,
230 "Connection to NETLOGON pipe of DC for %s failed: %s",
231 c->in.name, c->out.error_string);
233 talloc_free(samsync_ctx);
237 /* This makes a new pipe, on which we can do schannel. We
238 * should do this in the RpcConnect code, but the abstaction
239 * layers do not suit yet */
241 nt_status = dcerpc_secondary_connection(c->out.dcerpc_pipe, &p,
242 c->out.dcerpc_pipe->binding);
244 if (!NT_STATUS_IS_OK(nt_status)) {
245 r->out.error_string = talloc_asprintf(mem_ctx,
246 "Secondary connection to NETLOGON pipe of DC %s failed: %s",
247 dcerpc_server_name(p), nt_errstr(nt_status));
248 talloc_free(samsync_ctx);
252 nt_status = dcerpc_bind_auth_schannel(samsync_ctx, p, &dcerpc_table_netlogon,
253 machine_account, DCERPC_AUTH_LEVEL_PRIVACY);
255 if (!NT_STATUS_IS_OK(nt_status)) {
256 r->out.error_string = talloc_asprintf(mem_ctx,
257 "SCHANNEL authentication to NETLOGON pipe of DC %s failed: %s",
258 dcerpc_server_name(p), nt_errstr(nt_status));
259 talloc_free(samsync_ctx);
263 state = talloc(samsync_ctx, struct libnet_SamSync_state);
265 r->out.error_string = NULL;
266 talloc_free(samsync_ctx);
270 state->domain_name = c->out.domain_name;
271 state->domain_sid = c->out.domain_sid;
272 state->realm = c->out.realm;
273 state->domain_guid = c->out.guid;
274 state->machine_net_ctx = machine_net_ctx;
275 state->netlogon_pipe = p;
277 /* initialise the callback layer. It may wish to contact the
278 * server with ldap, now we know the name */
282 nt_status = r->in.init_fn(samsync_ctx,
286 if (!NT_STATUS_IS_OK(nt_status)) {
287 r->out.error_string = talloc_steal(mem_ctx, error_string);
288 talloc_free(samsync_ctx);
293 /* get NETLOGON credentails */
295 nt_status = dcerpc_schannel_creds(p->conn->security_state.generic_state, samsync_ctx, &creds);
296 if (!NT_STATUS_IS_OK(nt_status)) {
297 r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain NETLOGON credentials from DCERPC/GENSEC layer");
298 talloc_free(samsync_ctx);
302 /* Setup details for the syncronisation */
303 dbsync.in.logon_server = talloc_asprintf(samsync_ctx, "\\\\%s", dcerpc_server_name(p));
304 dbsync.in.computername = cli_credentials_get_workstation(machine_account);
305 dbsync.in.preferredmaximumlength = (uint32_t)-1;
306 ZERO_STRUCT(dbsync.in.return_authenticator);
308 for (i=0;i< ARRAY_SIZE(database_ids); i++) {
309 dbsync.in.sync_context = 0;
310 dbsync.in.database_id = database_ids[i];
314 loop_ctx = talloc_named(samsync_ctx, 0, "DatabaseSync loop context");
315 creds_client_authenticator(creds, &dbsync.in.credential);
317 dbsync_nt_status = dcerpc_netr_DatabaseSync(p, loop_ctx, &dbsync);
318 if (!NT_STATUS_IS_OK(dbsync_nt_status) &&
319 !NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES)) {
320 r->out.error_string = talloc_asprintf(mem_ctx, "DatabaseSync failed - %s", nt_errstr(nt_status));
321 talloc_free(samsync_ctx);
325 if (!creds_client_check(creds, &dbsync.out.return_authenticator.cred)) {
326 r->out.error_string = talloc_strdup(mem_ctx, "Credential chaining on incoming DatabaseSync failed");
327 talloc_free(samsync_ctx);
328 return NT_STATUS_ACCESS_DENIED;
331 dbsync.in.sync_context = dbsync.out.sync_context;
333 /* For every single remote 'delta' entry: */
334 for (d=0; d < dbsync.out.delta_enum_array->num_deltas; d++) {
335 char *error_string = NULL;
336 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
337 /* 'Fix' elements, by decrypting and
338 * de-obfustiating the data */
339 nt_status = fix_delta(delta_ctx,
341 dbsync.in.database_id,
342 &dbsync.out.delta_enum_array->delta_enum[d],
344 if (!NT_STATUS_IS_OK(nt_status)) {
345 r->out.error_string = talloc_steal(mem_ctx, error_string);
346 talloc_free(samsync_ctx);
350 /* Now call the callback. This will
351 * do something like print the data or
353 nt_status = r->in.delta_fn(delta_ctx,
355 dbsync.in.database_id,
356 &dbsync.out.delta_enum_array->delta_enum[d],
358 if (!NT_STATUS_IS_OK(nt_status)) {
359 r->out.error_string = talloc_steal(mem_ctx, error_string);
360 talloc_free(samsync_ctx);
363 talloc_free(delta_ctx);
365 talloc_free(loop_ctx);
366 } while (NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES));
368 if (!NT_STATUS_IS_OK(dbsync_nt_status)) {
369 r->out.error_string = talloc_asprintf(mem_ctx, "libnet_SamSync_netlogon failed: unexpected inconsistancy. Should not get error %s here", nt_errstr(nt_status));
370 talloc_free(samsync_ctx);
371 return dbsync_nt_status;
373 nt_status = NT_STATUS_OK;
375 talloc_free(samsync_ctx);