r13903: Don't generate prototypes for modules and binaries in include/proto.h by
[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 "auth/gensec/schannel_proto.h"
27
28
29 /**
30  * Decrypt and extract the user's passwords.  
31  * 
32  * The writes decrypted (no longer 'RID encrypted' or arcfour encrypted) passwords back into the structure
33  */
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,
38                          char **error_string) 
39 {
40
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;
46         NTSTATUS nt_status;
47
48         if (user->lm_password_present) {
49                 sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
50                 user->lmpassword = lm_hash;
51         }
52
53         if (user->nt_password_present) {
54                 sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
55                 user->ntpassword = nt_hash;
56         }
57
58         if (user->user_private_info.SensitiveData) {
59                 DATA_BLOB data;
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;
66
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;
73                         }
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;
78                         }
79                 } else {
80                         *error_string = talloc_asprintf(mem_ctx, "Failed to parse Sensitive Data for %s:\n", username);
81                         dump_data(10, data.data, data.length);
82                         return nt_status;
83                 }
84         }
85         return NT_STATUS_OK;
86 }
87
88 /**
89  * Decrypt and extract the secrets
90  * 
91  * The writes decrypted secrets back into the structure
92  */
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,
97                            char **error_string) 
98 {
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); 
102
103         creds_arcfour_crypt(creds, secret->old_cipher.cipher_data, 
104                             secret->old_cipher.maxlen); 
105
106         return NT_STATUS_OK;
107 }
108
109 /**
110  * Fix up the delta, dealing with encryption issues so that the final
111  * callback need only do the printing or application logic
112  */
113
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,
118                           char **error_string)
119 {
120         NTSTATUS nt_status = NT_STATUS_OK;
121         *error_string = NULL;
122         switch (delta->delta_type) {
123         case NETR_DELTA_USER:
124         {
125                 nt_status = fix_user(mem_ctx, 
126                                      creds,
127                                      database,
128                                      delta,
129                                      error_string);
130                 break;
131         }
132         case NETR_DELTA_SECRET:
133         {
134                 nt_status = fix_secret(mem_ctx, 
135                                        creds,
136                                        database,
137                                        delta,
138                                        error_string);
139                 break;
140         }
141         default:
142                 break;
143         }
144         return nt_status;
145 }
146
147 NTSTATUS libnet_SamSync_netlogon(struct libnet_context *ctx, TALLOC_CTX *mem_ctx, struct libnet_SamSync *r)
148 {
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}; 
159         int i;
160
161         samsync_ctx = talloc_named(mem_ctx, 0, "SamSync top context");
162
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;
168                 }
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);
174                         return nt_status;
175                 }
176         } else {
177                 machine_account = r->in.machine_account;
178         }
179
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) {
182                 r->out.error_string
183                         = talloc_asprintf(mem_ctx, 
184                                           "Our join to domain %s is not as a BDC (%d), please rejoin as a BDC",
185                                           
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;
190         }
191
192         c = talloc(samsync_ctx, struct libnet_RpcConnectDCInfo);
193         if (!c) {
194                 r->out.error_string = NULL;
195                 talloc_free(samsync_ctx);
196                 return NT_STATUS_NO_MEMORY;
197         }
198
199         if (r->in.binding_string) {
200                 c->level      = LIBNET_RPC_CONNECT_BINDING;
201                 c->in.binding = r->in.binding_string;
202         } else {
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);
206         }
207         c->in.dcerpc_iface      = &dcerpc_table_netlogon;
208
209         /* We must do this as the machine, not as any command-line
210          * user.  So we override the credentials in the
211          * libnet_context */
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;
217         }
218         *machine_net_ctx = *ctx;
219         machine_net_ctx->cred = machine_account;
220
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);
228                 } else {
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);
232                 }
233                 talloc_free(samsync_ctx);
234                 return nt_status;
235         }
236
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 */
240
241         nt_status = dcerpc_secondary_connection(c->out.dcerpc_pipe, &p,
242                                                 c->out.dcerpc_pipe->binding);
243
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);
249                 return nt_status;
250         }
251
252         nt_status = dcerpc_bind_auth_schannel(samsync_ctx, p, &dcerpc_table_netlogon,
253                                               machine_account, DCERPC_AUTH_LEVEL_PRIVACY);
254
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);
260                 return nt_status;
261         }
262
263         state = talloc(samsync_ctx, struct libnet_SamSync_state);
264         if (!state) {
265                 r->out.error_string = NULL;
266                 talloc_free(samsync_ctx);
267                 return nt_status;
268         }               
269
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;
276
277         /* initialise the callback layer.  It may wish to contact the
278          * server with ldap, now we know the name */
279         
280         if (r->in.init_fn) {
281                 char *error_string;
282                 nt_status = r->in.init_fn(samsync_ctx, 
283                                           r->in.fn_ctx,
284                                           state, 
285                                           &error_string); 
286                 if (!NT_STATUS_IS_OK(nt_status)) {
287                         r->out.error_string = talloc_steal(mem_ctx, error_string);
288                         talloc_free(samsync_ctx);
289                         return nt_status;
290                 }
291         }
292
293         /* get NETLOGON credentails */
294
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);
299                 return nt_status;
300         }
301
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);
307
308         for (i=0;i< ARRAY_SIZE(database_ids); i++) { 
309                 dbsync.in.sync_context = 0;
310                 dbsync.in.database_id = database_ids[i]; 
311                 
312                 do {
313                         int d;
314                         loop_ctx = talloc_named(samsync_ctx, 0, "DatabaseSync loop context");
315                         creds_client_authenticator(creds, &dbsync.in.credential);
316                         
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);
322                                 return nt_status;
323                         }
324                         
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;
329                         }
330                         
331                         dbsync.in.sync_context = dbsync.out.sync_context;
332                         
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, 
340                                                       creds, 
341                                                       dbsync.in.database_id,
342                                                       &dbsync.out.delta_enum_array->delta_enum[d], 
343                                                       &error_string);
344                                 if (!NT_STATUS_IS_OK(nt_status)) {
345                                         r->out.error_string = talloc_steal(mem_ctx, error_string);
346                                         talloc_free(samsync_ctx);
347                                         return nt_status;
348                                 }
349
350                                 /* Now call the callback.  This will
351                                  * do something like print the data or
352                                  * write to an ldb */
353                                 nt_status = r->in.delta_fn(delta_ctx, 
354                                                            r->in.fn_ctx,
355                                                            dbsync.in.database_id,
356                                                            &dbsync.out.delta_enum_array->delta_enum[d], 
357                                                            &error_string);
358                                 if (!NT_STATUS_IS_OK(nt_status)) {
359                                         r->out.error_string = talloc_steal(mem_ctx, error_string);
360                                         talloc_free(samsync_ctx);
361                                         return nt_status;
362                                 }
363                                 talloc_free(delta_ctx);
364                         }
365                         talloc_free(loop_ctx);
366                 } while (NT_STATUS_EQUAL(dbsync_nt_status, STATUS_MORE_ENTRIES));
367                 
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;
372                 }
373                 nt_status = NT_STATUS_OK;
374         }
375         talloc_free(samsync_ctx);
376         return nt_status;
377 }
378