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