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