r21255: Add a debugging option to avoid rid decryption in the samsync output.
[bbaumbach/samba-autobuild/.git] / source4 / libnet / libnet_vampire.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    Extract the user/system database from a remote SamSync server
5
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
7    
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.
12    
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.
17    
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.
21 */
22
23
24 #include "includes.h"
25 #include "libnet/libnet.h"
26 #include "libcli/auth/libcli_auth.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/credentials/credentials.h"
29 #include "auth/gensec/schannel_proto.h"
30 #include "librpc/gen_ndr/ndr_netlogon.h"
31 #include "librpc/gen_ndr/ndr_netlogon_c.h"
32
33
34 /**
35  * Decrypt and extract the user's passwords.  
36  * 
37  * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted) passwords back into the structure
38  */
39 static NTSTATUS fix_user(TALLOC_CTX *mem_ctx,
40                          struct creds_CredentialState *creds,
41                          enum netr_SamDatabaseID database,
42                          struct netr_DELTA_ENUM *delta,
43                          char **error_string) 
44 {
45
46         uint32_t rid = delta->delta_id_union.rid;
47         struct netr_DELTA_USER *user = delta->delta_union.user;
48         struct samr_Password lm_hash;
49         struct samr_Password nt_hash;
50         const char *username = user->account_name.string;
51         NTSTATUS nt_status;
52
53         if (lp_parm_bool(-1, "vampire", "rid_decrypt", True)) {
54                 if (user->lm_password_present) {
55                         sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
56                         user->lmpassword = lm_hash;
57                 }
58                 
59                 if (user->nt_password_present) {
60                         sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
61                         user->ntpassword = nt_hash;
62                 }
63         }
64
65         if (user->user_private_info.SensitiveData) {
66                 DATA_BLOB data;
67                 struct netr_USER_KEYS keys;
68                 data.data = user->user_private_info.SensitiveData;
69                 data.length = user->user_private_info.DataLength;
70                 creds_arcfour_crypt(creds, data.data, data.length);
71                 user->user_private_info.SensitiveData = data.data;
72                 user->user_private_info.DataLength = data.length;
73
74                 nt_status = ndr_pull_struct_blob(&data, mem_ctx, &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
75                 if (NT_STATUS_IS_OK(nt_status)) {
76                         if (keys.keys.keys2.lmpassword.length == 16) {
77                                 if (lp_parm_bool(-1, "vampire", "rid decrypt", True)) {
78                                         sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
79                                         user->lmpassword = lm_hash;
80                                 } else {
81                                         user->lmpassword = keys.keys.keys2.lmpassword.pwd;
82                                 }
83                                 user->lm_password_present = True;
84                         }
85                         if (keys.keys.keys2.ntpassword.length == 16) {
86                                 if (lp_parm_bool(-1, "vampire", "rid decrypt", True)) {
87                                         sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
88                                         user->ntpassword = nt_hash;
89                                 } else {
90                                         user->ntpassword = keys.keys.keys2.ntpassword.pwd;
91                                 }
92                                 user->nt_password_present = True;
93                         }
94                 } else {
95                         *error_string = talloc_asprintf(mem_ctx, "Failed to parse Sensitive Data for %s:", username);
96                         dump_data(10, data.data, data.length);
97                         return nt_status;
98                 }
99         }
100         return NT_STATUS_OK;
101 }
102
103 /**
104  * Decrypt and extract the secrets
105  * 
106  * The writes decrypted secrets back into the structure
107  */
108 static NTSTATUS fix_secret(TALLOC_CTX *mem_ctx,
109                            struct creds_CredentialState *creds,
110                            enum netr_SamDatabaseID database,
111                            struct netr_DELTA_ENUM *delta,
112                            char **error_string) 
113 {
114         struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
115         creds_arcfour_crypt(creds, secret->current_cipher.cipher_data, 
116                             secret->current_cipher.maxlen); 
117
118         creds_arcfour_crypt(creds, secret->old_cipher.cipher_data, 
119                             secret->old_cipher.maxlen); 
120
121         return NT_STATUS_OK;
122 }
123
124 /**
125  * Fix up the delta, dealing with encryption issues so that the final
126  * callback need only do the printing or application logic
127  */
128
129 static NTSTATUS fix_delta(TALLOC_CTX *mem_ctx,          
130                           struct creds_CredentialState *creds,
131                           enum netr_SamDatabaseID database,
132                           struct netr_DELTA_ENUM *delta,
133                           char **error_string)
134 {
135         NTSTATUS nt_status = NT_STATUS_OK;
136         *error_string = NULL;
137         switch (delta->delta_type) {
138         case NETR_DELTA_USER:
139         {
140                 nt_status = fix_user(mem_ctx, 
141                                      creds,
142                                      database,
143                                      delta,
144                                      error_string);
145                 break;
146         }
147         case NETR_DELTA_SECRET:
148         {
149                 nt_status = fix_secret(mem_ctx, 
150                                        creds,
151                                        database,
152                                        delta,
153                                        error_string);
154                 break;
155         }
156         default:
157                 break;
158         }
159         return nt_status;
160 }
161
162 NTSTATUS libnet_SamSync_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamSync *r)
163 {
164         NTSTATUS nt_status, dbsync_nt_status;
165         TALLOC_CTX *samsync_ctx, *loop_ctx, *delta_ctx;
166         struct creds_CredentialState *creds;
167         struct netr_DatabaseSync dbsync;
168         struct cli_credentials *machine_account;
169         struct dcerpc_pipe *p;
170         struct libnet_context *machine_net_ctx;
171         struct libnet_RpcConnect *c;
172         struct libnet_SamSync_state *state;
173         const enum netr_SamDatabaseID database_ids[] = {SAM_DATABASE_DOMAIN, SAM_DATABASE_BUILTIN, SAM_DATABASE_PRIVS}; 
174         int i;
175
176         samsync_ctx = talloc_named(mem_ctx, 0, "SamSync top context");
177
178         if (!r->in.machine_account) { 
179                 machine_account = cli_credentials_init(samsync_ctx);
180                 if (!machine_account) {
181                         talloc_free(samsync_ctx);
182                         return NT_STATUS_NO_MEMORY;
183                 }
184                 cli_credentials_set_conf(machine_account);
185                 nt_status = cli_credentials_set_machine_account(machine_account);
186                 if (!NT_STATUS_IS_OK(nt_status)) {
187                         r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain machine account password - are we joined to the domain?");
188                         talloc_free(samsync_ctx);
189                         return nt_status;
190                 }
191         } else {
192                 machine_account = r->in.machine_account;
193         }
194
195         /* We cannot do this unless we are a BDC.  Check, before we get odd errors later */
196         if (cli_credentials_get_secure_channel_type(machine_account) != SEC_CHAN_BDC) {
197                 r->out.error_string
198                         = talloc_asprintf(mem_ctx, 
199                                           "Our join to domain %s is not as a BDC (%d), please rejoin as a BDC",
200                                           cli_credentials_get_domain(machine_account),
201                                           cli_credentials_get_secure_channel_type(machine_account));
202                 talloc_free(samsync_ctx);
203                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
204         }
205
206         c = talloc(samsync_ctx, struct libnet_RpcConnect);
207         if (!c) {
208                 r->out.error_string = NULL;
209                 talloc_free(samsync_ctx);
210                 return NT_STATUS_NO_MEMORY;
211         }
212
213         c->level              = LIBNET_RPC_CONNECT_DC_INFO;
214         if (r->in.binding_string) {
215                 c->in.binding = r->in.binding_string;
216                 c->in.name    = NULL;
217         } else {
218                 c->in.binding = NULL;
219                 c->in.name    = cli_credentials_get_domain(machine_account);
220         }
221         
222         /* prepare connect to the NETLOGON pipe of PDC */
223         c->in.dcerpc_iface      = &dcerpc_table_netlogon;
224
225         /* We must do this as the machine, not as any command-line
226          * user.  So we override the credentials in the
227          * libnet_context */
228         machine_net_ctx = talloc(samsync_ctx, struct libnet_context);
229         if (!machine_net_ctx) {
230                 r->out.error_string = NULL;
231                 talloc_free(samsync_ctx);
232                 return NT_STATUS_NO_MEMORY;
233         }
234         *machine_net_ctx = *ctx;
235         machine_net_ctx->cred = machine_account;
236
237         /* connect to the NETLOGON pipe of the PDC */
238         nt_status = libnet_RpcConnect(machine_net_ctx, samsync_ctx, c);
239         if (!NT_STATUS_IS_OK(nt_status)) {
240                 if (r->in.binding_string) {
241                         r->out.error_string = talloc_asprintf(mem_ctx,
242                                                               "Connection to NETLOGON pipe of DC %s failed: %s",
243                                                               r->in.binding_string, c->out.error_string);
244                 } else {
245                         r->out.error_string = talloc_asprintf(mem_ctx,
246                                                               "Connection to NETLOGON pipe of DC for %s failed: %s",
247                                                               c->in.name, c->out.error_string);
248                 }
249                 talloc_free(samsync_ctx);
250                 return nt_status;
251         }
252
253         /* This makes a new pipe, on which we can do schannel.  We
254          * should do this in the RpcConnect code, but the abstaction
255          * layers do not suit yet */
256
257         nt_status = dcerpc_secondary_connection(c->out.dcerpc_pipe, &p,
258                                                 c->out.dcerpc_pipe->binding);
259
260         if (!NT_STATUS_IS_OK(nt_status)) {
261                 r->out.error_string = talloc_asprintf(mem_ctx,
262                                                       "Secondary connection to NETLOGON pipe of DC %s failed: %s",
263                                                       dcerpc_server_name(p), nt_errstr(nt_status));
264                 talloc_free(samsync_ctx);
265                 return nt_status;
266         }
267
268         nt_status = dcerpc_bind_auth_schannel(samsync_ctx, p, &dcerpc_table_netlogon,
269                                               machine_account, DCERPC_AUTH_LEVEL_PRIVACY);
270
271         if (!NT_STATUS_IS_OK(nt_status)) {
272                 r->out.error_string = talloc_asprintf(mem_ctx,
273                                                       "SCHANNEL authentication to NETLOGON pipe of DC %s failed: %s",
274                                                       dcerpc_server_name(p), nt_errstr(nt_status));
275                 talloc_free(samsync_ctx);
276                 return nt_status;
277         }
278
279         state = talloc(samsync_ctx, struct libnet_SamSync_state);
280         if (!state) {
281                 r->out.error_string = NULL;
282                 talloc_free(samsync_ctx);
283                 return nt_status;
284         }               
285
286         state->domain_name     = c->out.domain_name;
287         state->domain_sid      = c->out.domain_sid;
288         state->realm           = c->out.realm;
289         state->domain_guid     = c->out.guid;
290         state->machine_net_ctx = machine_net_ctx;
291         state->netlogon_pipe   = p;
292
293         /* initialise the callback layer.  It may wish to contact the
294          * server with ldap, now we know the name */
295         
296         if (r->in.init_fn) {
297                 char *error_string;
298                 nt_status = r->in.init_fn(samsync_ctx, 
299                                           r->in.fn_ctx,
300                                           state, 
301                                           &error_string); 
302                 if (!NT_STATUS_IS_OK(nt_status)) {
303                         r->out.error_string = talloc_steal(mem_ctx, error_string);
304                         talloc_free(samsync_ctx);
305                         return nt_status;
306                 }
307         }
308
309         /* get NETLOGON credentails */
310
311         nt_status = dcerpc_schannel_creds(p->conn->security_state.generic_state, samsync_ctx, &creds);
312         if (!NT_STATUS_IS_OK(nt_status)) {
313                 r->out.error_string = talloc_strdup(mem_ctx, "Could not obtain NETLOGON credentials from DCERPC/GENSEC layer");
314                 talloc_free(samsync_ctx);
315                 return nt_status;
316         }
317
318         /* Setup details for the synchronisation */
319         dbsync.in.logon_server = talloc_asprintf(samsync_ctx, "\\\\%s", dcerpc_server_name(p));
320         dbsync.in.computername = cli_credentials_get_workstation(machine_account);
321         dbsync.in.preferredmaximumlength = (uint32_t)-1;
322         ZERO_STRUCT(dbsync.in.return_authenticator);
323
324         for (i=0;i< ARRAY_SIZE(database_ids); i++) { 
325                 dbsync.in.sync_context = 0;
326                 dbsync.in.database_id = database_ids[i]; 
327                 
328                 do {
329                         int d;
330                         loop_ctx = talloc_named(samsync_ctx, 0, "DatabaseSync loop context");
331                         creds_client_authenticator(creds, &dbsync.in.credential);
332                         
333                         dbsync_nt_status = dcerpc_netr_DatabaseSync(p, loop_ctx, &dbsync);
334                         if (!NT_STATUS_IS_OK(dbsync_nt_status) &&
335                             !NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES)) {
336                                 r->out.error_string = talloc_asprintf(mem_ctx, "DatabaseSync failed - %s", nt_errstr(nt_status));
337                                 talloc_free(samsync_ctx);
338                                 return nt_status;
339                         }
340                         
341                         if (!creds_client_check(creds, &dbsync.out.return_authenticator.cred)) {
342                                 r->out.error_string = talloc_strdup(mem_ctx, "Credential chaining on incoming DatabaseSync failed");
343                                 talloc_free(samsync_ctx);
344                                 return NT_STATUS_ACCESS_DENIED;
345                         }
346                         
347                         dbsync.in.sync_context = dbsync.out.sync_context;
348                         
349                         /* For every single remote 'delta' entry: */
350                         for (d=0; d < dbsync.out.delta_enum_array->num_deltas; d++) {
351                                 char *error_string = NULL;
352                                 delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
353                                 /* 'Fix' elements, by decrypting and
354                                  * de-obfuscating the data */
355                                 nt_status = fix_delta(delta_ctx, 
356                                                       creds, 
357                                                       dbsync.in.database_id,
358                                                       &dbsync.out.delta_enum_array->delta_enum[d], 
359                                                       &error_string);
360                                 if (!NT_STATUS_IS_OK(nt_status)) {
361                                         r->out.error_string = talloc_steal(mem_ctx, error_string);
362                                         talloc_free(samsync_ctx);
363                                         return nt_status;
364                                 }
365
366                                 /* Now call the callback.  This will
367                                  * do something like print the data or
368                                  * write to an ldb */
369                                 nt_status = r->in.delta_fn(delta_ctx, 
370                                                            r->in.fn_ctx,
371                                                            dbsync.in.database_id,
372                                                            &dbsync.out.delta_enum_array->delta_enum[d], 
373                                                            &error_string);
374                                 if (!NT_STATUS_IS_OK(nt_status)) {
375                                         r->out.error_string = talloc_steal(mem_ctx, error_string);
376                                         talloc_free(samsync_ctx);
377                                         return nt_status;
378                                 }
379                                 talloc_free(delta_ctx);
380                         }
381                         talloc_free(loop_ctx);
382                 } while (NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES));
383                 
384                 if (!NT_STATUS_IS_OK(dbsync_nt_status)) {
385                         r->out.error_string = talloc_asprintf(mem_ctx, "libnet_SamSync_netlogon failed: unexpected inconsistancy. Should not get error %s here", nt_errstr(nt_status));
386                         talloc_free(samsync_ctx);
387                         return dbsync_nt_status;
388                 }
389                 nt_status = NT_STATUS_OK;
390         }
391         talloc_free(samsync_ctx);
392         return nt_status;
393 }
394