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