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