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