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