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