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