2 Unix SMB/CIFS implementation.
4 dcerpc schannel operations
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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.
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "auth/auth.h"
27 enum schannel_position {
28 DCERPC_SCHANNEL_STATE_START = 0,
29 DCERPC_SCHANNEL_STATE_UPDATE_1
32 struct dcerpc_schannel_state {
33 enum schannel_position state;
34 struct schannel_state *schannel_state;
35 struct creds_CredentialState *creds;
39 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
44 struct creds_CredentialState *creds);
47 wrappers for the schannel_*() functions
49 These will become static again, when we get dynamic registration, and
50 decrpc_schannel_security_ops come back here.
52 static NTSTATUS dcerpc_schannel_unseal_packet(struct gensec_security *gensec_security,
54 uint8_t *data, size_t length,
55 const uint8_t *whole_pdu, size_t pdu_length,
58 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
60 return schannel_unseal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
63 static NTSTATUS dcerpc_schannel_check_packet(struct gensec_security *gensec_security,
65 const uint8_t *data, size_t length,
66 const uint8_t *whole_pdu, size_t pdu_length,
69 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
71 return schannel_check_packet(dce_schan_state->schannel_state, data, length, sig);
74 static NTSTATUS dcerpc_schannel_seal_packet(struct gensec_security *gensec_security,
76 uint8_t *data, size_t length,
77 const uint8_t *whole_pdu, size_t pdu_length,
80 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
82 return schannel_seal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
85 static NTSTATUS dcerpc_schannel_sign_packet(struct gensec_security *gensec_security,
87 const uint8_t *data, size_t length,
88 const uint8_t *whole_pdu, size_t pdu_length,
91 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
93 return schannel_sign_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
96 static size_t dcerpc_schannel_sig_size(struct gensec_security *gensec_security)
101 static NTSTATUS dcerpc_schannel_session_key(struct gensec_security *gensec_security,
102 DATA_BLOB *session_key)
104 return NT_STATUS_NOT_IMPLEMENTED;
107 static NTSTATUS dcerpc_schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
108 const DATA_BLOB in, DATA_BLOB *out)
110 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
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);
117 switch (gensec_security->gensec_role) {
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 */
124 status = schannel_start(&dce_schan_state->schannel_state,
125 dce_schan_state->creds->session_key,
127 if (!NT_STATUS_IS_OK(status)) {
128 DEBUG(1, ("Failed to start schannel client\n"));
131 talloc_steal(dce_schan_state, dce_schan_state->schannel_state);
133 bind_schannel.unknown1 = 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);
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;
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",
155 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
157 return NT_STATUS_MORE_PROCESSING_REQUIRED;
160 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
161 /* no third leg on this protocol */
162 return NT_STATUS_INVALID_PARAMETER;
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)) {
172 if (bind_schannel.bind_type == 23) {
173 account_name = bind_schannel.u.info23.account_name;
175 account_name = bind_schannel.u.info3.account_name;
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)));
186 dce_schan_state->account_name = talloc_strdup(dce_schan_state, account_name);
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)));
196 talloc_steal(dce_schan_state, dce_schan_state->schannel_state);
198 bind_schannel_ack.unknown1 = 1;
199 bind_schannel_ack.unknown2 = 0;
200 bind_schannel_ack.unknown3 = 0x6c0000;
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)));
210 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
214 return NT_STATUS_INVALID_PARAMETER;
218 * Return the credentials of a logged on user, including session keys
221 * Only valid after a successful authentication
223 * May only be called once per authentication.
227 NTSTATUS dcerpc_schannel_session_info(struct gensec_security *gensec_security,
228 struct auth_session_info **session_info)
230 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
232 (*session_info) = talloc_p(gensec_security, struct auth_session_info);
233 if (*session_info == NULL) {
234 return NT_STATUS_NO_MEMORY;
237 ZERO_STRUCTP(*session_info);
238 (*session_info)->refcount = 1;
240 (*session_info)->workstation = talloc_strdup(*session_info, dce_schan_state->account_name);
241 if ((*session_info)->workstation == NULL) {
242 talloc_free(*session_info);
243 return NT_STATUS_NO_MEMORY;
250 * Return the struct creds_CredentialState.
252 * Make sure not to call this unless gensec is using schannel...
255 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
257 struct creds_CredentialState **creds)
259 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
261 *creds = talloc_reference(mem_ctx, dce_schan_state->creds);
263 return NT_STATUS_NO_MEMORY;
272 static int dcerpc_schannel_destroy(void *ptr)
274 struct dcerpc_schannel_state *dce_schan_state = ptr;
276 schannel_end(&dce_schan_state->schannel_state);
281 static NTSTATUS dcerpc_schannel_start(struct gensec_security *gensec_security)
283 struct dcerpc_schannel_state *dce_schan_state;
285 dce_schan_state = talloc_p(gensec_security, struct dcerpc_schannel_state);
286 if (!dce_schan_state) {
287 return NT_STATUS_NO_MEMORY;
290 dce_schan_state->state = DCERPC_SCHANNEL_STATE_START;
291 gensec_security->private_data = dce_schan_state;
292 gensec_security->have_features =
293 GENSEC_FEATURE_SESSION_KEY |
294 GENSEC_FEATURE_SIGN |
297 talloc_set_destructor(dce_schan_state, dcerpc_schannel_destroy);
302 static NTSTATUS dcerpc_schannel_server_start(struct gensec_security *gensec_security)
306 status = dcerpc_schannel_start(gensec_security);
307 if (!NT_STATUS_IS_OK(status)) {
314 static NTSTATUS dcerpc_schannel_client_start(struct gensec_security *gensec_security)
318 status = dcerpc_schannel_start(gensec_security);
319 if (!NT_STATUS_IS_OK(status)) {
328 get a schannel key using a netlogon challenge on a secondary pipe
330 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
332 const char *username,
333 const char *password,
335 struct creds_CredentialState *creds)
338 struct dcerpc_pipe *p2;
339 struct netr_ServerReqChallenge r;
340 struct netr_ServerAuthenticate2 a;
341 struct netr_Credential credentials1, credentials2, credentials3;
342 struct samr_Password mach_pwd;
343 const char *workgroup, *workstation;
344 uint32_t negotiate_flags;
346 if (p->flags & DCERPC_SCHANNEL_128) {
347 negotiate_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
349 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
352 workstation = username;
356 step 1 - establish a netlogon connection, with no authentication
358 status = dcerpc_secondary_connection(p, &p2,
359 DCERPC_NETLOGON_NAME,
360 DCERPC_NETLOGON_UUID,
361 DCERPC_NETLOGON_VERSION);
362 if (!NT_STATUS_IS_OK(status)) {
368 step 2 - request a netlogon challenge
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;
375 generate_random_buffer(credentials1.data, sizeof(credentials1.data));
377 status = dcerpc_netr_ServerReqChallenge(p2, p, &r);
378 if (!NT_STATUS_IS_OK(status)) {
383 step 3 - authenticate on the netlogon pipe
385 E_md4hash(password, mach_pwd.hash);
386 creds_client_init(creds, &credentials1, &credentials2, &mach_pwd, &credentials3,
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;
398 status = dcerpc_netr_ServerAuthenticate2(p2, p, &a);
399 if (!NT_STATUS_IS_OK(status)) {
403 if (!creds_client_check(creds, a.out.credentials)) {
404 return NT_STATUS_UNSUCCESSFUL;
408 the schannel session key is now in creds.session_key
410 we no longer need the netlogon pipe open
412 dcerpc_pipe_close(p2);
418 do a schannel style bind on a dcerpc pipe. The username is usually
419 of the form HOSTNAME$ and the password is the domain trust password
421 NTSTATUS dcerpc_bind_auth_schannel_withkey(struct dcerpc_pipe *p,
422 const char *uuid, uint_t version,
424 const char *username,
425 const char *password,
426 struct creds_CredentialState *creds)
429 struct dcerpc_schannel_state *dce_schan_state;
431 status = gensec_client_start(p, &p->security_state.generic_state);
432 if (!NT_STATUS_IS_OK(status)) {
436 status = gensec_set_username(p->security_state.generic_state, username);
437 if (!NT_STATUS_IS_OK(status)) {
438 DEBUG(1, ("Failed to set schannel username to %s: %s\n", username, nt_errstr(status)));
439 talloc_free(p->security_state.generic_state);
440 p->security_state.generic_state = NULL;
444 status = gensec_set_domain(p->security_state.generic_state, domain);
445 if (!NT_STATUS_IS_OK(status)) {
446 DEBUG(1, ("Failed to set schannel domain to %s: %s\n", domain, nt_errstr(status)));
447 talloc_free(p->security_state.generic_state);
448 p->security_state.generic_state = NULL;
452 status = gensec_start_mech_by_authtype(p->security_state.generic_state, DCERPC_AUTH_TYPE_SCHANNEL, dcerpc_auth_level(p));
454 if (!NT_STATUS_IS_OK(status)) {
455 DEBUG(1, ("Failed to start SCHANNEL GENSEC backend: %s\n", nt_errstr(status)));
456 talloc_free(p->security_state.generic_state);
457 p->security_state.generic_state = NULL;
461 dce_schan_state = p->security_state.generic_state->private_data;
462 dce_schan_state->creds = talloc_reference(dce_schan_state, creds);
464 status = dcerpc_bind_auth3(p, DCERPC_AUTH_TYPE_SCHANNEL, dcerpc_auth_level(p),
467 if (!NT_STATUS_IS_OK(status)) {
468 DEBUG(1, ("Failed to bind to pipe with SCHANNEL: %s\n", nt_errstr(status)));
469 talloc_free(p->security_state.generic_state);
470 p->security_state.generic_state = NULL;
477 NTSTATUS dcerpc_bind_auth_schannel(struct dcerpc_pipe *p,
478 const char *uuid, uint_t version,
480 const char *username,
481 const char *password)
485 struct creds_CredentialState *creds;
486 creds = talloc_p(p, struct creds_CredentialState);
488 return NT_STATUS_NO_MEMORY;
491 if (p->flags & DCERPC_SCHANNEL_BDC) {
492 chan_type = SEC_CHAN_BDC;
493 } else if (p->flags & DCERPC_SCHANNEL_WORKSTATION) {
494 chan_type = SEC_CHAN_WKSTA;
495 } else if (p->flags & DCERPC_SCHANNEL_DOMAIN) {
496 chan_type = SEC_CHAN_DOMAIN;
499 status = dcerpc_schannel_key(p, domain,
505 if (!NT_STATUS_IS_OK(status)) {
506 DEBUG(1, ("Failed to fetch schannel session key: %s\n",
511 return dcerpc_bind_auth_schannel_withkey(p, uuid, version, domain,
516 static const struct gensec_security_ops gensec_dcerpc_schannel_security_ops = {
517 .name = "dcerpc_schannel",
518 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
519 .client_start = dcerpc_schannel_client_start,
520 .server_start = dcerpc_schannel_server_start,
521 .update = dcerpc_schannel_update,
522 .seal_packet = dcerpc_schannel_seal_packet,
523 .sign_packet = dcerpc_schannel_sign_packet,
524 .check_packet = dcerpc_schannel_check_packet,
525 .unseal_packet = dcerpc_schannel_unseal_packet,
526 .session_key = dcerpc_schannel_session_key,
527 .session_info = dcerpc_schannel_session_info,
528 .sig_size = dcerpc_schannel_sig_size,
531 NTSTATUS gensec_dcerpc_schannel_init(void)
534 ret = gensec_register(&gensec_dcerpc_schannel_security_ops);
535 if (!NT_STATUS_IS_OK(ret)) {
536 DEBUG(0,("Failed to register '%s' gensec backend!\n",
537 gensec_dcerpc_schannel_security_ops.name));