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.
25 enum schannel_position {
26 DCERPC_SCHANNEL_STATE_START = 0,
27 DCERPC_SCHANNEL_STATE_UPDATE_1
30 struct dcerpc_schannel_state {
32 enum schannel_position state;
33 struct schannel_state *schannel_state;
34 struct creds_CredentialState creds;
38 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
43 uint8_t new_session_key[16]);
46 wrappers for the schannel_*() functions
48 These will become static again, when we get dynamic registration, and
49 decrpc_schannel_security_ops come back here.
51 static NTSTATUS dcerpc_schannel_unseal_packet(struct gensec_security *gensec_security,
53 uint8_t *data, size_t length,
54 const uint8_t *whole_pdu, size_t pdu_length,
57 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
59 return schannel_unseal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
62 static NTSTATUS dcerpc_schannel_check_packet(struct gensec_security *gensec_security,
64 const uint8_t *data, size_t length,
65 const uint8_t *whole_pdu, size_t pdu_length,
68 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
70 return schannel_check_packet(dce_schan_state->schannel_state, data, length, sig);
73 static NTSTATUS dcerpc_schannel_seal_packet(struct gensec_security *gensec_security,
75 uint8_t *data, size_t length,
76 const uint8_t *whole_pdu, size_t pdu_length,
79 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
81 return schannel_seal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
84 static NTSTATUS dcerpc_schannel_sign_packet(struct gensec_security *gensec_security,
86 const uint8_t *data, size_t length,
87 const uint8_t *whole_pdu, size_t pdu_length,
90 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
92 return schannel_sign_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
95 static NTSTATUS dcerpc_schannel_session_key(struct gensec_security *gensec_security,
96 DATA_BLOB *session_key)
98 return NT_STATUS_NOT_IMPLEMENTED;
101 static NTSTATUS dcerpc_schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
102 const DATA_BLOB in, DATA_BLOB *out)
104 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
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);
111 switch (gensec_security->gensec_role) {
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 */
118 bind_schannel.unknown1 = 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);
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;
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",
140 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
142 return NT_STATUS_MORE_PROCESSING_REQUIRED;
145 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
146 /* no third leg on this protocol */
147 return NT_STATUS_INVALID_PARAMETER;
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)) {
157 if (bind_schannel.bind_type == 23) {
158 account_name = bind_schannel.u.info23.account_name;
160 account_name = bind_schannel.u.info3.account_name;
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)));
171 dce_schan_state->account_name = talloc_strdup(dce_schan_state->mem_ctx, account_name);
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)));
182 bind_schannel_ack.unknown1 = 1;
183 bind_schannel_ack.unknown2 = 0;
184 bind_schannel_ack.unknown3 = 0x6c0000;
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)));
194 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
198 return NT_STATUS_INVALID_PARAMETER;
202 * Return the credentials of a logged on user, including session keys
205 * Only valid after a successful authentication
207 * May only be called once per authentication.
211 NTSTATUS dcerpc_schannel_session_info(struct gensec_security *gensec_security,
212 struct auth_session_info **session_info)
214 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
216 (*session_info) = talloc_p(gensec_security, struct auth_session_info);
217 if (*session_info == NULL) {
218 return NT_STATUS_NO_MEMORY;
221 ZERO_STRUCTP(*session_info);
222 (*session_info)->refcount = 1;
224 (*session_info)->workstation = talloc_strdup(*session_info, dce_schan_state->account_name);
225 if ((*session_info)->workstation == NULL) {
226 talloc_free(*session_info);
227 return NT_STATUS_NO_MEMORY;
234 * Return the struct creds_CredentialState.
236 * Make sure not to call this unless gensec is using schannel...
239 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
241 struct creds_CredentialState **creds)
243 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
245 *creds = talloc_p(mem_ctx, struct creds_CredentialState);
247 return NT_STATUS_NO_MEMORY;
250 **creds = dce_schan_state->creds;
255 static NTSTATUS dcerpc_schannel_start(struct gensec_security *gensec_security)
257 struct dcerpc_schannel_state *dce_schan_state;
259 mem_ctx = talloc_init("dcerpc_schannel_start");
261 return NT_STATUS_NO_MEMORY;
264 dce_schan_state = talloc_p(mem_ctx, struct dcerpc_schannel_state);
265 if (!dce_schan_state) {
266 talloc_destroy(mem_ctx);
267 return NT_STATUS_NO_MEMORY;
270 dce_schan_state->mem_ctx = mem_ctx;
271 dce_schan_state->state = DCERPC_SCHANNEL_STATE_START;
274 gensec_security->private_data = dce_schan_state;
279 static NTSTATUS dcerpc_schannel_server_start(struct gensec_security *gensec_security)
282 struct dcerpc_schannel_state *dce_schan_state;
284 status = dcerpc_schannel_start(gensec_security);
286 dce_schan_state = gensec_security->private_data;
287 dce_schan_state->schannel_state = NULL;
289 if (!NT_STATUS_IS_OK(status)) {
295 static NTSTATUS dcerpc_schannel_client_start(struct gensec_security *gensec_security)
298 struct dcerpc_schannel_state *dce_schan_state;
300 status = dcerpc_schannel_start(gensec_security);
301 if (!NT_STATUS_IS_OK(status)) {
304 dce_schan_state = gensec_security->private_data;
306 status = schannel_start(&dce_schan_state->schannel_state,
307 gensec_security->user.schan_session_key,
309 if (!NT_STATUS_IS_OK(status)) {
310 DEBUG(1, ("Failed to start schannel client\n"));
314 dump_data_pw("session key:\n", dce_schan_state->schannel_state->session_key, 16);
321 static void dcerpc_schannel_end(struct gensec_security *gensec_security)
323 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
325 schannel_end(&dce_schan_state->schannel_state);
327 talloc_destroy(dce_schan_state->mem_ctx);
329 gensec_security->private_data = NULL;
334 get a schannel key using a netlogon challenge on a secondary pipe
336 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
338 const char *username,
339 const char *password,
341 uint8_t new_session_key[16])
344 struct dcerpc_pipe *p2;
345 struct netr_ServerReqChallenge r;
346 struct netr_ServerAuthenticate2 a;
347 struct netr_Credential credentials1, credentials2, credentials3;
348 struct samr_Password mach_pwd;
349 struct creds_CredentialState creds;
350 const char *workgroup, *workstation;
351 uint32_t negotiate_flags;
353 if (p->flags & DCERPC_SCHANNEL_128) {
354 negotiate_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
356 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
359 workstation = username;
363 step 1 - establish a netlogon connection, with no authentication
365 status = dcerpc_secondary_connection(p, &p2,
366 DCERPC_NETLOGON_NAME,
367 DCERPC_NETLOGON_UUID,
368 DCERPC_NETLOGON_VERSION);
372 step 2 - request a netlogon challenge
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;
379 generate_random_buffer(credentials1.data, sizeof(credentials1.data));
381 status = dcerpc_netr_ServerReqChallenge(p2, p, &r);
382 if (!NT_STATUS_IS_OK(status)) {
387 step 3 - authenticate on the netlogon pipe
389 E_md4hash(password, mach_pwd.hash);
390 creds_client_init(&creds, &credentials1, &credentials2, &mach_pwd, &credentials3,
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;
402 status = dcerpc_netr_ServerAuthenticate2(p2, p, &a);
403 if (!NT_STATUS_IS_OK(status)) {
407 if (!creds_client_check(&creds, a.out.credentials)) {
408 return NT_STATUS_UNSUCCESSFUL;
412 the schannel session key is now in creds.session_key
414 we no longer need the netlogon pipe open
416 dcerpc_pipe_close(p2);
418 memcpy(new_session_key, creds.session_key, 16);
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
427 NTSTATUS dcerpc_bind_auth_schannel(struct dcerpc_pipe *p,
428 const char *uuid, uint_t version,
430 const char *username,
431 const char *password)
436 status = gensec_client_start(p, &p->security_state.generic_state);
437 if (!NT_STATUS_IS_OK(status)) {
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;
449 status = dcerpc_schannel_key(p, domain,
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);
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);
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);
474 status = gensec_start_mech_by_authtype(p->security_state.generic_state, DCERPC_AUTH_TYPE_SCHANNEL, dcerpc_auth_level(p));
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);
482 status = dcerpc_bind_auth3(p, DCERPC_AUTH_TYPE_SCHANNEL, dcerpc_auth_level(p),
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);
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 .end = dcerpc_schannel_end
510 NTSTATUS gensec_dcerpc_schannel_init(void)
513 ret = register_backend("gensec", &gensec_dcerpc_schannel_security_ops);
514 if (!NT_STATUS_IS_OK(ret)) {
515 DEBUG(0,("Failed to register '%s' gensec backend!\n",
516 gensec_dcerpc_schannel_security_ops.name));