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