2 Unix SMB/CIFS implementation.
4 dcerpc schannel operations
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "librpc/gen_ndr/ndr_schannel.h"
25 #include "auth/auth.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/gensec/gensec.h"
28 #include "auth/gensec/gensec_proto.h"
29 #include "../libcli/auth/schannel.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "param/param.h"
33 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
38 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
39 DATA_BLOB *session_key)
41 return NT_STATUS_NOT_IMPLEMENTED;
44 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
45 const DATA_BLOB in, DATA_BLOB *out)
47 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
49 enum ndr_err_code ndr_err;
50 struct NL_AUTH_MESSAGE bind_schannel;
51 struct NL_AUTH_MESSAGE bind_schannel_ack;
52 struct netlogon_creds_CredentialState *creds;
53 const char *workstation;
55 uint32_t required_flags;
57 *out = data_blob(NULL, 0);
59 switch (gensec_security->gensec_role) {
61 if (state->state != SCHANNEL_STATE_START) {
62 /* we could parse the bind ack, but we don't know what it is yet */
66 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
68 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
70 /* to support this we'd need to have access to the full domain name */
72 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
73 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
74 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
75 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
76 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
77 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
78 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
79 /* w2k3 refuses us if we use the full DNS workstation?
80 why? perhaps because we don't fill in the dNSHostName
81 attribute in the machine account? */
82 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
84 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
85 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
86 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
87 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
90 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
91 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
92 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
93 status = ndr_map_error2ntstatus(ndr_err);
94 DEBUG(3, ("Could not create schannel bind: %s\n",
99 state->state = SCHANNEL_STATE_UPDATE_1;
101 return NT_STATUS_MORE_PROCESSING_REQUIRED;
104 required_flags = NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
105 NL_FLAG_OEM_NETBIOS_DOMAIN_NAME;
107 if (state->state != SCHANNEL_STATE_START) {
108 /* no third leg on this protocol */
109 return NT_STATUS_INVALID_PARAMETER;
112 /* parse the schannel startup blob */
113 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
114 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
115 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
116 status = ndr_map_error2ntstatus(ndr_err);
117 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
122 if (!(required_flags == (bind_schannel.Flags & required_flags))) {
123 return NT_STATUS_INVALID_PARAMETER;
126 workstation = bind_schannel.oem_netbios_computer.a;
127 domain = bind_schannel.oem_netbios_domain.a;
129 if (strcasecmp_m(domain, lp_workgroup(gensec_security->settings->lp_ctx)) != 0) {
130 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
131 domain, lp_workgroup(gensec_security->settings->lp_ctx)));
133 return NT_STATUS_LOGON_FAILURE;
136 status = schannel_get_creds_state(out_mem_ctx,
137 lp_private_dir(gensec_security->settings->lp_ctx),
138 workstation, &creds);
139 if (!NT_STATUS_IS_OK(status)) {
140 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
141 workstation, nt_errstr(status)));
142 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
143 return NT_STATUS_LOGON_FAILURE;
148 state->creds = talloc_steal(state, creds);
150 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
151 bind_schannel_ack.Flags = 0;
152 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
157 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
158 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
159 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
160 status = ndr_map_error2ntstatus(ndr_err);
161 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
162 workstation, nt_errstr(status)));
166 state->state = SCHANNEL_STATE_UPDATE_1;
170 return NT_STATUS_INVALID_PARAMETER;
174 * Return the struct netlogon_creds_CredentialState.
176 * Make sure not to call this unless gensec is using schannel...
179 /* TODO: make this non-public */
181 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
183 struct netlogon_creds_CredentialState **creds)
185 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
187 *creds = talloc_reference(mem_ctx, state->creds);
189 return NT_STATUS_NO_MEMORY;
196 * Returns anonymous credentials for schannel, matching Win2k3.
200 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
201 struct auth_session_info **_session_info)
203 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
204 return auth_anonymous_session_info(state, gensec_security->settings->lp_ctx, _session_info);
207 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
209 struct schannel_state *state;
211 state = talloc(gensec_security, struct schannel_state);
213 return NT_STATUS_NO_MEMORY;
216 state->state = SCHANNEL_STATE_START;
218 gensec_security->private_data = state;
223 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
226 struct schannel_state *state;
228 status = schannel_start(gensec_security);
229 if (!NT_STATUS_IS_OK(status)) {
233 state = (struct schannel_state *)gensec_security->private_data;
234 state->initiator = false;
239 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
242 struct schannel_state *state;
244 status = schannel_start(gensec_security);
245 if (!NT_STATUS_IS_OK(status)) {
249 state = (struct schannel_state *)gensec_security->private_data;
250 state->initiator = true;
256 static bool schannel_have_feature(struct gensec_security *gensec_security,
259 if (feature & (GENSEC_FEATURE_SIGN |
260 GENSEC_FEATURE_SEAL)) {
263 if (feature & GENSEC_FEATURE_DCE_STYLE) {
266 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
275 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
277 uint8_t *data, size_t length,
278 const uint8_t *whole_pdu, size_t pdu_length,
279 const DATA_BLOB *sig)
281 struct schannel_state *state =
282 talloc_get_type(gensec_security->private_data,
283 struct schannel_state);
285 return netsec_incoming_packet(state, mem_ctx, true,
286 discard_const_p(uint8_t, data),
291 check the signature on a packet
293 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
295 const uint8_t *data, size_t length,
296 const uint8_t *whole_pdu, size_t pdu_length,
297 const DATA_BLOB *sig)
299 struct schannel_state *state =
300 talloc_get_type(gensec_security->private_data,
301 struct schannel_state);
303 return netsec_incoming_packet(state, mem_ctx, false,
304 discard_const_p(uint8_t, data),
310 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
312 uint8_t *data, size_t length,
313 const uint8_t *whole_pdu, size_t pdu_length,
316 struct schannel_state *state =
317 talloc_get_type(gensec_security->private_data,
318 struct schannel_state);
320 return netsec_outgoing_packet(state, mem_ctx, true,
327 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
329 const uint8_t *data, size_t length,
330 const uint8_t *whole_pdu, size_t pdu_length,
333 struct schannel_state *state =
334 talloc_get_type(gensec_security->private_data,
335 struct schannel_state);
337 return netsec_outgoing_packet(state, mem_ctx, false,
338 discard_const_p(uint8_t, data),
342 static const struct gensec_security_ops gensec_schannel_security_ops = {
344 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
345 .client_start = schannel_client_start,
346 .server_start = schannel_server_start,
347 .update = schannel_update,
348 .seal_packet = schannel_seal_packet,
349 .sign_packet = schannel_sign_packet,
350 .check_packet = schannel_check_packet,
351 .unseal_packet = schannel_unseal_packet,
352 .session_key = schannel_session_key,
353 .session_info = schannel_session_info,
354 .sig_size = schannel_sig_size,
355 .have_feature = schannel_have_feature,
357 .priority = GENSEC_SCHANNEL
360 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
363 ret = gensec_register(&gensec_schannel_security_ops);
364 if (!NT_STATUS_IS_OK(ret)) {
365 DEBUG(0,("Failed to register '%s' gensec backend!\n",
366 gensec_schannel_security_ops.name));