r1993: Allow WinXP domain logon to progress a bit further (it seems broken for me).
[samba.git] / source4 / librpc / rpc / dcerpc_schannel.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    dcerpc schannel operations
5
6    Copyright (C) Andrew Tridgell 2004
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 #include "includes.h"
24
25 enum schannel_position {
26         DCERPC_SCHANNEL_STATE_START = 0,
27         DCERPC_SCHANNEL_STATE_UPDATE_1
28 };
29
30 struct dcerpc_schannel_state {
31         TALLOC_CTX *mem_ctx;
32         enum schannel_position state;
33         struct schannel_state *schannel_state;
34         struct creds_CredentialState creds;
35         char *account_name;
36 };
37
38 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
39                                     const char *domain,
40                                     const char *username,
41                                     const char *password,
42                                     int chan_type,
43                                     uint8_t new_session_key[16]);
44
45 /*
46   wrappers for the schannel_*() functions
47
48   These will become static again, when we get dynamic registration, and
49   decrpc_schannel_security_ops come back here.
50 */
51 static NTSTATUS dcerpc_schannel_unseal_packet(struct gensec_security *gensec_security, 
52                                               TALLOC_CTX *mem_ctx, 
53                                               uint8_t *data, size_t length, DATA_BLOB *sig)
54 {
55         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
56         
57         return schannel_unseal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
58 }
59
60 static NTSTATUS dcerpc_schannel_check_packet(struct gensec_security *gensec_security, 
61                                              TALLOC_CTX *mem_ctx, 
62                                              const uint8_t *data, size_t length, 
63                                              const DATA_BLOB *sig)
64 {
65         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
66
67         return schannel_check_packet(dce_schan_state->schannel_state, data, length, sig);
68 }
69
70 static NTSTATUS dcerpc_schannel_seal_packet(struct gensec_security *gensec_security, 
71                                             TALLOC_CTX *mem_ctx, 
72                                             uint8_t *data, size_t length, 
73                                             DATA_BLOB *sig)
74 {
75         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
76
77         return schannel_seal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
78 }
79
80 static NTSTATUS dcerpc_schannel_sign_packet(struct gensec_security *gensec_security, 
81                                             TALLOC_CTX *mem_ctx, 
82                                             const uint8_t *data, size_t length, 
83                                             DATA_BLOB *sig)
84 {
85         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
86
87         return schannel_sign_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
88 }
89
90 static NTSTATUS dcerpc_schannel_session_key(struct gensec_security *gensec_security, 
91                                             DATA_BLOB *session_key)
92 {
93         return NT_STATUS_NOT_IMPLEMENTED;
94 }
95
96 static NTSTATUS dcerpc_schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx, 
97                                        const DATA_BLOB in, DATA_BLOB *out) 
98 {
99         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
100         NTSTATUS status;
101         struct schannel_bind bind_schannel;
102         struct schannel_bind_ack bind_schannel_ack;
103         const char *account_name;
104         *out = data_blob(NULL, 0);
105
106         switch (gensec_security->gensec_role) {
107         case GENSEC_CLIENT:
108                 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
109                         /* we could parse the bind ack, but we don't know what it is yet */
110                         return NT_STATUS_OK;
111                 }
112                 
113                 bind_schannel.unknown1 = 0;
114 #if 0
115                 /* to support this we'd need to have access to the full domain name */
116                 bind_schannel.bind_type = 23;
117                 bind_schannel.u.info23.domain = gensec_security->user.domain;
118                 bind_schannel.u.info23.account_name = gensec_security->user.name;
119                 bind_schannel.u.info23.dnsdomain = str_format_nbt_domain(out_mem_ctx, fulldomainname);
120                 bind_schannel.u.info23.workstation = str_format_nbt_domain(out_mem_ctx, gensec_security->user.name);
121 #else
122                 bind_schannel.bind_type = 3;
123                 bind_schannel.u.info3.domain = gensec_security->user.domain;
124                 bind_schannel.u.info3.account_name = gensec_security->user.name;
125 #endif
126                 
127                 status = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
128                                               (ndr_push_flags_fn_t)ndr_push_schannel_bind);
129                 if (!NT_STATUS_IS_OK(status)) {
130                         DEBUG(3, ("Could not create schannel bind: %s\n",
131                                   nt_errstr(status)));
132                         return status;
133                 }
134                 
135                 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
136
137                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
138         case GENSEC_SERVER:
139                 
140                 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
141                         /* no third leg on this protocol */
142                         return NT_STATUS_INVALID_PARAMETER;
143                 }
144                 
145                 /* parse the schannel startup blob */
146                 status = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel, 
147                                               (ndr_pull_flags_fn_t)ndr_pull_schannel_bind);
148                 if (!NT_STATUS_IS_OK(status)) {
149                         return status;
150                 }
151                 
152                 if (bind_schannel.bind_type == 23) {
153                         account_name = bind_schannel.u.info23.account_name;
154                 } else {
155                         account_name = bind_schannel.u.info3.account_name;
156                 }
157                 
158                 /* pull the session key for this client */
159                 status = schannel_fetch_session_key(out_mem_ctx, account_name, &dce_schan_state->creds);
160                 if (!NT_STATUS_IS_OK(status)) {
161                         DEBUG(3, ("Could not find session key for attempted schannel connection on %s: %s\n",
162                                   account_name, nt_errstr(status)));
163                         return status;
164                 }
165
166                 dce_schan_state->account_name = talloc_strdup(dce_schan_state->mem_ctx, account_name);
167                 
168                 /* start up the schannel server code */
169                 status = schannel_start(&dce_schan_state->schannel_state, 
170                                         dce_schan_state->creds.session_key, False);
171                 if (!NT_STATUS_IS_OK(status)) {
172                         DEBUG(3, ("Could not initialise schannel state for account %s: %s\n",
173                                   account_name, nt_errstr(status)));
174                         return status;
175                 }
176                 
177                 bind_schannel_ack.unknown1 = 1;
178                 bind_schannel_ack.unknown2 = 0;
179                 bind_schannel_ack.unknown3 = 0x6c0000;
180                 
181                 status = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack, 
182                                               (ndr_push_flags_fn_t)ndr_push_schannel_bind_ack);
183                 if (!NT_STATUS_IS_OK(status)) {
184                         DEBUG(3, ("Could not return schannel bind ack for account %s: %s\n",
185                                   account_name, nt_errstr(status)));
186                         return status;
187                 }
188
189                 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
190
191                 return NT_STATUS_OK;
192         }
193         return NT_STATUS_INVALID_PARAMETER;
194 }
195
196 /** 
197  * Return the credentials of a logged on user, including session keys
198  * etc.
199  *
200  * Only valid after a successful authentication
201  *
202  * May only be called once per authentication.
203  *
204  */
205
206 NTSTATUS dcerpc_schannel_session_info(struct gensec_security *gensec_security,
207                                       struct auth_session_info **session_info)
208
209         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
210         TALLOC_CTX *mem_ctx;
211         mem_ctx = talloc_init("dcerpc_schannel_start");
212         if (!mem_ctx) {
213                 return NT_STATUS_NO_MEMORY;
214         }
215
216         (*session_info) = talloc_p(mem_ctx, struct auth_session_info);
217         if (*session_info == NULL) {
218                 talloc_destroy(mem_ctx);
219                 return NT_STATUS_NO_MEMORY;
220         }
221
222         ZERO_STRUCTP(*session_info);
223         (*session_info)->mem_ctx = mem_ctx;
224         (*session_info)->refcount = 1;
225         
226         (*session_info)->workstation = talloc_strdup(mem_ctx, dce_schan_state->account_name);
227         if ((*session_info)->workstation == NULL) {
228                 talloc_destroy(mem_ctx);
229                 return NT_STATUS_NO_MEMORY;
230         }
231         return NT_STATUS_OK;
232 }
233                 
234
235 /**
236  * Return the struct creds_CredentialState.
237  *
238  * Make sure not to call this unless gensec is using schannel...
239  */
240
241 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
242                                TALLOC_CTX *mem_ctx,
243                                struct creds_CredentialState **creds)
244
245         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
246
247         *creds = talloc_p(mem_ctx, struct creds_CredentialState);
248         if (!*creds) {
249                 return NT_STATUS_NO_MEMORY;
250         }
251
252         **creds = dce_schan_state->creds;
253         return NT_STATUS_OK;
254 }
255                 
256
257 static NTSTATUS dcerpc_schannel_start(struct gensec_security *gensec_security)
258 {
259         struct dcerpc_schannel_state *dce_schan_state;
260         TALLOC_CTX *mem_ctx;
261         mem_ctx = talloc_init("dcerpc_schannel_start");
262         if (!mem_ctx) {
263                 return NT_STATUS_NO_MEMORY;
264         }
265
266         dce_schan_state = talloc_p(mem_ctx, struct dcerpc_schannel_state);
267         if (!dce_schan_state) {
268                 talloc_destroy(mem_ctx);
269                 return NT_STATUS_NO_MEMORY;
270         }
271
272         dce_schan_state->mem_ctx = mem_ctx;
273         dce_schan_state->state = DCERPC_SCHANNEL_STATE_START;
274         
275
276         gensec_security->private_data = dce_schan_state;
277         
278         return NT_STATUS_OK;
279 }
280
281 static NTSTATUS dcerpc_schannel_server_start(struct gensec_security *gensec_security) 
282 {
283         NTSTATUS status;
284
285         status = dcerpc_schannel_start(gensec_security);
286
287         if (!NT_STATUS_IS_OK(status)) {
288                 return status;
289         }
290         return NT_STATUS_OK;
291 }
292
293 static NTSTATUS dcerpc_schannel_client_start(struct gensec_security *gensec_security) 
294 {
295         NTSTATUS status;
296         struct dcerpc_schannel_state *dce_schan_state;
297
298         status = dcerpc_schannel_start(gensec_security);
299         if (!NT_STATUS_IS_OK(status)) {
300                 return status;
301         }
302         dce_schan_state = gensec_security->private_data;
303
304         status = schannel_start(&dce_schan_state->schannel_state, 
305                                 gensec_security->user.schan_session_key, 
306                                 True);
307         if (!NT_STATUS_IS_OK(status)) {
308                 DEBUG(1, ("Failed to start schannel client\n"));
309                 return status;
310         }
311
312         dump_data_pw("session key:\n", dce_schan_state->schannel_state->session_key, 16);
313         return NT_STATUS_OK;
314 }
315
316 /*
317   end crypto state
318 */
319 static void dcerpc_schannel_end(struct gensec_security *gensec_security)
320 {
321         struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
322
323         schannel_end(&dce_schan_state->schannel_state);
324
325         talloc_destroy(dce_schan_state->mem_ctx);
326
327         gensec_security->private_data = NULL;
328 }
329
330
331 /*
332   get a schannel key using a netlogon challenge on a secondary pipe
333 */
334 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
335                                     const char *domain,
336                                     const char *username,
337                                     const char *password,
338                                     int chan_type,
339                                     uint8_t new_session_key[16])
340 {
341         NTSTATUS status;
342         struct dcerpc_pipe *p2;
343         struct netr_ServerReqChallenge r;
344         struct netr_ServerAuthenticate2 a;
345         struct netr_Credential credentials1, credentials2, credentials3;
346         struct samr_Password mach_pwd;
347         struct creds_CredentialState creds;
348         const char *workgroup, *workstation;
349         uint32_t negotiate_flags;
350
351         if (p->flags & DCERPC_SCHANNEL_128) {
352                 negotiate_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
353         } else {
354                 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
355         }
356
357         workstation = username;
358         workgroup = domain;
359
360         /*
361           step 1 - establish a netlogon connection, with no authentication
362         */
363         status = dcerpc_secondary_connection(p, &p2, 
364                                              DCERPC_NETLOGON_NAME, 
365                                              DCERPC_NETLOGON_UUID, 
366                                              DCERPC_NETLOGON_VERSION);
367
368
369         /*
370           step 2 - request a netlogon challenge
371         */
372         r.in.server_name = talloc_asprintf(p->mem_ctx, "\\\\%s", dcerpc_server_name(p));
373         r.in.computer_name = workstation;
374         r.in.credentials = &credentials1;
375         r.out.credentials = &credentials2;
376
377         generate_random_buffer(credentials1.data, sizeof(credentials1.data));
378
379         status = dcerpc_netr_ServerReqChallenge(p2, p->mem_ctx, &r);
380         if (!NT_STATUS_IS_OK(status)) {
381                 return status;
382         }
383
384         /*
385           step 3 - authenticate on the netlogon pipe
386         */
387         E_md4hash(password, mach_pwd.hash);
388         creds_client_init(&creds, &credentials1, &credentials2, &mach_pwd, &credentials3,
389                           negotiate_flags);
390
391         a.in.server_name = r.in.server_name;
392         a.in.account_name = talloc_asprintf(p->mem_ctx, "%s$", workstation);
393         a.in.secure_channel_type = chan_type;
394         a.in.computer_name = workstation;
395         a.in.negotiate_flags = &negotiate_flags;
396         a.out.negotiate_flags = &negotiate_flags;
397         a.in.credentials = &credentials3;
398         a.out.credentials = &credentials3;
399
400         status = dcerpc_netr_ServerAuthenticate2(p2, p->mem_ctx, &a);
401         if (!NT_STATUS_IS_OK(status)) {
402                 return status;
403         }
404
405         if (!creds_client_check(&creds, a.out.credentials)) {
406                 return NT_STATUS_UNSUCCESSFUL;
407         }
408
409         /*
410           the schannel session key is now in creds.session_key
411
412           we no longer need the netlogon pipe open
413         */
414         dcerpc_pipe_close(p2);
415
416         memcpy(new_session_key, creds.session_key, 16);
417
418         return NT_STATUS_OK;
419 }
420
421 /*
422   do a schannel style bind on a dcerpc pipe. The username is usually
423   of the form HOSTNAME$ and the password is the domain trust password
424 */
425 NTSTATUS dcerpc_bind_auth_schannel(struct dcerpc_pipe *p,
426                                    const char *uuid, uint_t version,
427                                    const char *domain,
428                                    const char *username,
429                                    const char *password)
430 {
431         NTSTATUS status;
432         int chan_type = 0;
433
434         status = gensec_client_start(&p->security_state.generic_state);
435         if (!NT_STATUS_IS_OK(status)) {
436                 return status;
437         }
438
439         if (p->flags & DCERPC_SCHANNEL_BDC) {
440                 chan_type = SEC_CHAN_BDC;
441         } else if (p->flags & DCERPC_SCHANNEL_WORKSTATION) {
442                 chan_type = SEC_CHAN_WKSTA;
443         } else if (p->flags & DCERPC_SCHANNEL_DOMAIN) {
444                 chan_type = SEC_CHAN_DOMAIN;
445         }
446
447         status = dcerpc_schannel_key(p, domain, 
448                                      username,
449                                      password, 
450                                      chan_type, 
451                                      p->security_state.generic_state->user.schan_session_key);
452         if (!NT_STATUS_IS_OK(status)) {
453                 DEBUG(1, ("Failed to fetch schannel session key: %s\n", nt_errstr(status)));
454                 gensec_end(&p->security_state.generic_state);
455                 return status;
456         }
457         
458         status = gensec_set_username(p->security_state.generic_state, username);
459         if (!NT_STATUS_IS_OK(status)) {
460                 DEBUG(1, ("Failed to set schannel username to %s: %s\n", username, nt_errstr(status)));
461                 gensec_end(&p->security_state.generic_state);
462                 return status;
463         }
464         
465         status = gensec_set_domain(p->security_state.generic_state, domain);
466         if (!NT_STATUS_IS_OK(status)) {
467                 DEBUG(1, ("Failed to set schannel domain to %s: %s\n", domain, nt_errstr(status)));
468                 gensec_end(&p->security_state.generic_state);
469                 return status;
470         }
471         
472         status = gensec_start_mech_by_authtype(p->security_state.generic_state, DCERPC_AUTH_TYPE_SCHANNEL);
473
474         if (!NT_STATUS_IS_OK(status)) {
475                 DEBUG(1, ("Failed to start SCHANNEL GENSEC backend: %s\n", nt_errstr(status)));
476                 gensec_end(&p->security_state.generic_state);
477                 return status;
478         }
479
480         status = dcerpc_bind_auth3(p, DCERPC_AUTH_TYPE_SCHANNEL,
481                                   uuid, version);
482
483         if (!NT_STATUS_IS_OK(status)) {
484                 DEBUG(1, ("Failed to bind to pipe with SCHANNEL: %s\n", nt_errstr(status)));
485                 gensec_end(&p->security_state.generic_state);
486                 return status;
487         }
488
489         return NT_STATUS_OK;
490 }
491
492
493 static const struct gensec_security_ops gensec_dcerpc_schannel_security_ops = {
494         .name           = "dcerpc_schannel",
495         .auth_type      = DCERPC_AUTH_TYPE_SCHANNEL,
496         .client_start   = dcerpc_schannel_client_start,
497         .server_start   = dcerpc_schannel_server_start,
498         .update         = dcerpc_schannel_update,
499         .seal_packet    = dcerpc_schannel_seal_packet,
500         .sign_packet    = dcerpc_schannel_sign_packet,
501         .check_packet   = dcerpc_schannel_check_packet,
502         .unseal_packet  = dcerpc_schannel_unseal_packet,
503         .session_key    = dcerpc_schannel_session_key,
504         .session_info   = dcerpc_schannel_session_info,
505         .end            = dcerpc_schannel_end
506 };
507
508 NTSTATUS gensec_dcerpc_schannel_init(void)
509 {
510         NTSTATUS ret;
511         ret = register_backend("gensec", &gensec_dcerpc_schannel_security_ops);
512         if (!NT_STATUS_IS_OK(ret)) {
513                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
514                         gensec_dcerpc_schannel_security_ops.name));
515                 return ret;
516         }
517
518         return ret;
519 }