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;
40 wrappers for the schannel_*() functions
42 These will become static again, when we get dynamic registration, and
43 decrpc_schannel_security_ops come back here.
45 static NTSTATUS dcerpc_schannel_unseal_packet(struct gensec_security *gensec_security,
47 uint8_t *data, size_t length,
48 const uint8_t *whole_pdu, size_t pdu_length,
51 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
53 return schannel_unseal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
56 static NTSTATUS dcerpc_schannel_check_packet(struct gensec_security *gensec_security,
58 const uint8_t *data, size_t length,
59 const uint8_t *whole_pdu, size_t pdu_length,
62 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
64 return schannel_check_packet(dce_schan_state->schannel_state, data, length, sig);
67 static NTSTATUS dcerpc_schannel_seal_packet(struct gensec_security *gensec_security,
69 uint8_t *data, size_t length,
70 const uint8_t *whole_pdu, size_t pdu_length,
73 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
75 return schannel_seal_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
78 static NTSTATUS dcerpc_schannel_sign_packet(struct gensec_security *gensec_security,
80 const uint8_t *data, size_t length,
81 const uint8_t *whole_pdu, size_t pdu_length,
84 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
86 return schannel_sign_packet(dce_schan_state->schannel_state, mem_ctx, data, length, sig);
89 static size_t dcerpc_schannel_sig_size(struct gensec_security *gensec_security)
94 static NTSTATUS dcerpc_schannel_session_key(struct gensec_security *gensec_security,
95 DATA_BLOB *session_key)
97 return NT_STATUS_NOT_IMPLEMENTED;
100 static NTSTATUS dcerpc_schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
101 const DATA_BLOB in, DATA_BLOB *out)
103 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
105 struct schannel_bind bind_schannel;
106 struct schannel_bind_ack bind_schannel_ack;
107 const char *account_name;
108 *out = data_blob(NULL, 0);
110 switch (gensec_security->gensec_role) {
112 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
113 /* we could parse the bind ack, but we don't know what it is yet */
117 status = schannel_start(&dce_schan_state->schannel_state,
118 dce_schan_state->creds->session_key,
120 if (!NT_STATUS_IS_OK(status)) {
121 DEBUG(1, ("Failed to start schannel client\n"));
124 talloc_steal(dce_schan_state, dce_schan_state->schannel_state);
126 bind_schannel.unknown1 = 0;
128 /* to support this we'd need to have access to the full domain name */
129 bind_schannel.bind_type = 23;
130 bind_schannel.u.info23.domain = gensec_security->user.domain;
131 bind_schannel.u.info23.account_name = gensec_security->user.name;
132 bind_schannel.u.info23.dnsdomain = str_format_nbt_domain(out_mem_ctx, fulldomainname);
133 bind_schannel.u.info23.workstation = str_format_nbt_domain(out_mem_ctx, gensec_security->user.name);
135 bind_schannel.bind_type = 3;
136 bind_schannel.u.info3.domain = gensec_security->user.domain;
137 bind_schannel.u.info3.account_name = gensec_security->user.name;
140 status = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
141 (ndr_push_flags_fn_t)ndr_push_schannel_bind);
142 if (!NT_STATUS_IS_OK(status)) {
143 DEBUG(3, ("Could not create schannel bind: %s\n",
148 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
150 return NT_STATUS_MORE_PROCESSING_REQUIRED;
153 if (dce_schan_state->state != DCERPC_SCHANNEL_STATE_START) {
154 /* no third leg on this protocol */
155 return NT_STATUS_INVALID_PARAMETER;
158 /* parse the schannel startup blob */
159 status = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
160 (ndr_pull_flags_fn_t)ndr_pull_schannel_bind);
161 if (!NT_STATUS_IS_OK(status)) {
165 if (bind_schannel.bind_type == 23) {
166 account_name = bind_schannel.u.info23.account_name;
168 account_name = bind_schannel.u.info3.account_name;
171 /* pull the session key for this client */
172 status = schannel_fetch_session_key(out_mem_ctx, account_name, &dce_schan_state->creds);
173 if (!NT_STATUS_IS_OK(status)) {
174 DEBUG(3, ("Could not find session key for attempted schannel connection on %s: %s\n",
175 account_name, nt_errstr(status)));
179 dce_schan_state->account_name = talloc_strdup(dce_schan_state, account_name);
181 /* start up the schannel server code */
182 status = schannel_start(&dce_schan_state->schannel_state,
183 dce_schan_state->creds->session_key, False);
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(3, ("Could not initialise schannel state for account %s: %s\n",
186 account_name, nt_errstr(status)));
189 talloc_steal(dce_schan_state, dce_schan_state->schannel_state);
191 bind_schannel_ack.unknown1 = 1;
192 bind_schannel_ack.unknown2 = 0;
193 bind_schannel_ack.unknown3 = 0x6c0000;
195 status = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
196 (ndr_push_flags_fn_t)ndr_push_schannel_bind_ack);
197 if (!NT_STATUS_IS_OK(status)) {
198 DEBUG(3, ("Could not return schannel bind ack for account %s: %s\n",
199 account_name, nt_errstr(status)));
203 dce_schan_state->state = DCERPC_SCHANNEL_STATE_UPDATE_1;
207 return NT_STATUS_INVALID_PARAMETER;
211 * Return the credentials of a logged on user, including session keys
214 * Only valid after a successful authentication
216 * May only be called once per authentication.
220 NTSTATUS dcerpc_schannel_session_info(struct gensec_security *gensec_security,
221 struct auth_session_info **session_info)
223 (*session_info) = talloc(gensec_security, struct auth_session_info);
224 NT_STATUS_HAVE_NO_MEMORY(*session_info);
226 ZERO_STRUCTP(*session_info);
232 * Return the struct creds_CredentialState.
234 * Make sure not to call this unless gensec is using schannel...
237 NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
239 struct creds_CredentialState **creds)
241 struct dcerpc_schannel_state *dce_schan_state = gensec_security->private_data;
243 *creds = talloc_reference(mem_ctx, dce_schan_state->creds);
245 return NT_STATUS_NO_MEMORY;
254 static int dcerpc_schannel_destroy(void *ptr)
256 struct dcerpc_schannel_state *dce_schan_state = ptr;
258 schannel_end(&dce_schan_state->schannel_state);
263 static NTSTATUS dcerpc_schannel_start(struct gensec_security *gensec_security)
265 struct dcerpc_schannel_state *dce_schan_state;
267 dce_schan_state = talloc(gensec_security, struct dcerpc_schannel_state);
268 if (!dce_schan_state) {
269 return NT_STATUS_NO_MEMORY;
272 dce_schan_state->state = DCERPC_SCHANNEL_STATE_START;
273 gensec_security->private_data = dce_schan_state;
275 talloc_set_destructor(dce_schan_state, dcerpc_schannel_destroy);
280 static NTSTATUS dcerpc_schannel_server_start(struct gensec_security *gensec_security)
284 status = dcerpc_schannel_start(gensec_security);
285 if (!NT_STATUS_IS_OK(status)) {
292 static NTSTATUS dcerpc_schannel_client_start(struct gensec_security *gensec_security)
296 status = dcerpc_schannel_start(gensec_security);
297 if (!NT_STATUS_IS_OK(status)) {
306 get a schannel key using a netlogon challenge on a secondary pipe
308 static NTSTATUS dcerpc_schannel_key(struct dcerpc_pipe *p,
310 const char *username,
311 const char *password,
313 struct creds_CredentialState *creds)
316 struct dcerpc_pipe *p2;
317 struct netr_ServerReqChallenge r;
318 struct netr_ServerAuthenticate2 a;
319 struct netr_Credential credentials1, credentials2, credentials3;
320 struct samr_Password mach_pwd;
321 const char *workgroup, *workstation;
322 uint32_t negotiate_flags;
324 if (p->conn->flags & DCERPC_SCHANNEL_128) {
325 negotiate_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
327 negotiate_flags = NETLOGON_NEG_AUTH2_FLAGS;
330 workstation = username;
334 step 1 - establish a netlogon connection, with no authentication
336 status = dcerpc_secondary_connection(p, &p2,
337 DCERPC_NETLOGON_NAME,
338 DCERPC_NETLOGON_UUID,
339 DCERPC_NETLOGON_VERSION);
340 if (!NT_STATUS_IS_OK(status)) {
346 step 2 - request a netlogon challenge
348 r.in.server_name = talloc_asprintf(p, "\\\\%s", dcerpc_server_name(p));
349 r.in.computer_name = workstation;
350 r.in.credentials = &credentials1;
351 r.out.credentials = &credentials2;
353 generate_random_buffer(credentials1.data, sizeof(credentials1.data));
355 status = dcerpc_netr_ServerReqChallenge(p2, p, &r);
356 if (!NT_STATUS_IS_OK(status)) {
361 step 3 - authenticate on the netlogon pipe
363 E_md4hash(password, mach_pwd.hash);
364 creds_client_init(creds, &credentials1, &credentials2, &mach_pwd, &credentials3,
367 a.in.server_name = r.in.server_name;
368 a.in.account_name = talloc_asprintf(p, "%s$", workstation);
369 a.in.secure_channel_type = chan_type;
370 a.in.computer_name = workstation;
371 a.in.negotiate_flags = &negotiate_flags;
372 a.out.negotiate_flags = &negotiate_flags;
373 a.in.credentials = &credentials3;
374 a.out.credentials = &credentials3;
376 status = dcerpc_netr_ServerAuthenticate2(p2, p, &a);
377 if (!NT_STATUS_IS_OK(status)) {
381 if (!creds_client_check(creds, a.out.credentials)) {
382 return NT_STATUS_UNSUCCESSFUL;
386 the schannel session key is now in creds.session_key
388 we no longer need the netlogon pipe open
390 dcerpc_pipe_close(p2);
396 do a schannel style bind on a dcerpc pipe. The username is usually
397 of the form HOSTNAME$ and the password is the domain trust password
399 NTSTATUS dcerpc_bind_auth_schannel_withkey(struct dcerpc_pipe *p,
400 const char *uuid, uint_t version,
402 const char *username,
403 const char *password,
404 struct creds_CredentialState *creds)
407 struct dcerpc_schannel_state *dce_schan_state;
409 status = gensec_client_start(p, &p->conn->security_state.generic_state);
410 if (!NT_STATUS_IS_OK(status)) {
414 status = gensec_set_username(p->conn->security_state.generic_state, username);
415 if (!NT_STATUS_IS_OK(status)) {
416 DEBUG(1, ("Failed to set schannel username to %s: %s\n", username, nt_errstr(status)));
417 talloc_free(p->conn->security_state.generic_state);
418 p->conn->security_state.generic_state = NULL;
422 status = gensec_set_domain(p->conn->security_state.generic_state, domain);
423 if (!NT_STATUS_IS_OK(status)) {
424 DEBUG(1, ("Failed to set schannel domain to %s: %s\n", domain, nt_errstr(status)));
425 talloc_free(p->conn->security_state.generic_state);
426 p->conn->security_state.generic_state = NULL;
430 status = gensec_start_mech_by_authtype(p->conn->security_state.generic_state,
431 DCERPC_AUTH_TYPE_SCHANNEL,
432 dcerpc_auth_level(p->conn));
434 if (!NT_STATUS_IS_OK(status)) {
435 DEBUG(1, ("Failed to start SCHANNEL GENSEC backend: %s\n", nt_errstr(status)));
436 talloc_free(p->conn->security_state.generic_state);
437 p->conn->security_state.generic_state = NULL;
441 dce_schan_state = p->conn->security_state.generic_state->private_data;
442 dce_schan_state->creds = talloc_reference(dce_schan_state, creds);
444 status = dcerpc_bind_auth(p, DCERPC_AUTH_TYPE_SCHANNEL, dcerpc_auth_level(p->conn),
447 if (!NT_STATUS_IS_OK(status)) {
448 DEBUG(1, ("Failed to bind to pipe with SCHANNEL: %s\n", nt_errstr(status)));
449 talloc_free(p->conn->security_state.generic_state);
450 p->conn->security_state.generic_state = NULL;
457 NTSTATUS dcerpc_bind_auth_schannel(struct dcerpc_pipe *p,
458 const char *uuid, uint_t version,
460 const char *username,
461 const char *password)
465 struct creds_CredentialState *creds;
466 creds = talloc(p, struct creds_CredentialState);
468 return NT_STATUS_NO_MEMORY;
471 if (p->conn->flags & DCERPC_SCHANNEL_BDC) {
472 chan_type = SEC_CHAN_BDC;
473 } else if (p->conn->flags & DCERPC_SCHANNEL_WORKSTATION) {
474 chan_type = SEC_CHAN_WKSTA;
475 } else if (p->conn->flags & DCERPC_SCHANNEL_DOMAIN) {
476 chan_type = SEC_CHAN_DOMAIN;
479 status = dcerpc_schannel_key(p, domain,
485 if (!NT_STATUS_IS_OK(status)) {
486 DEBUG(1, ("Failed to fetch schannel session key: %s\n",
491 return dcerpc_bind_auth_schannel_withkey(p, uuid, version, domain,
496 static BOOL dcerpc_schannel_have_feature(struct gensec_security *gensec_security,
499 if (feature & (GENSEC_FEATURE_SESSION_KEY |
500 GENSEC_FEATURE_SIGN |
501 GENSEC_FEATURE_SEAL)) {
508 static const struct gensec_security_ops gensec_dcerpc_schannel_security_ops = {
509 .name = "dcerpc_schannel",
510 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
511 .client_start = dcerpc_schannel_client_start,
512 .server_start = dcerpc_schannel_server_start,
513 .update = dcerpc_schannel_update,
514 .seal_packet = dcerpc_schannel_seal_packet,
515 .sign_packet = dcerpc_schannel_sign_packet,
516 .check_packet = dcerpc_schannel_check_packet,
517 .unseal_packet = dcerpc_schannel_unseal_packet,
518 .session_key = dcerpc_schannel_session_key,
519 .session_info = dcerpc_schannel_session_info,
520 .sig_size = dcerpc_schannel_sig_size,
521 .have_feature = dcerpc_schannel_have_feature,
525 NTSTATUS gensec_dcerpc_schannel_init(void)
528 ret = gensec_register(&gensec_dcerpc_schannel_security_ops);
529 if (!NT_STATUS_IS_OK(ret)) {
530 DEBUG(0,("Failed to register '%s' gensec backend!\n",
531 gensec_dcerpc_schannel_security_ops.name));