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"
32 #include "auth/gensec/schannel.h"
33 #include "auth/gensec/gensec_toplevel_proto.h"
35 _PUBLIC_ NTSTATUS gensec_schannel_init(void);
37 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
39 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
42 sig_size = netsec_outgoing_sig_size(state);
47 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
49 DATA_BLOB *session_key)
51 return NT_STATUS_NOT_IMPLEMENTED;
54 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
55 const DATA_BLOB in, DATA_BLOB *out)
57 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
59 enum ndr_err_code ndr_err;
60 struct NL_AUTH_MESSAGE bind_schannel;
61 struct NL_AUTH_MESSAGE bind_schannel_ack;
62 struct netlogon_creds_CredentialState *creds;
63 const char *workstation;
66 *out = data_blob(NULL, 0);
68 switch (gensec_security->gensec_role) {
70 if (state->state != SCHANNEL_STATE_START) {
71 /* we could parse the bind ack, but we don't know what it is yet */
75 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
77 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
79 /* to support this we'd need to have access to the full domain name */
81 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
82 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
83 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
84 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
85 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
86 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
87 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
88 /* w2k3 refuses us if we use the full DNS workstation?
89 why? perhaps because we don't fill in the dNSHostName
90 attribute in the machine account? */
91 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
93 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
94 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
95 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
96 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
99 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
100 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
101 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
102 status = ndr_map_error2ntstatus(ndr_err);
103 DEBUG(3, ("Could not create schannel bind: %s\n",
108 state->state = SCHANNEL_STATE_UPDATE_1;
110 return NT_STATUS_MORE_PROCESSING_REQUIRED;
113 if (state->state != SCHANNEL_STATE_START) {
114 /* no third leg on this protocol */
115 return NT_STATUS_INVALID_PARAMETER;
118 /* parse the schannel startup blob */
119 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
120 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
121 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
122 status = ndr_map_error2ntstatus(ndr_err);
123 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
128 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
129 domain = bind_schannel.oem_netbios_domain.a;
130 if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
131 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
132 domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
133 return NT_STATUS_LOGON_FAILURE;
135 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
136 domain = bind_schannel.utf8_dns_domain.u;
137 if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
138 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
139 domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
140 return NT_STATUS_LOGON_FAILURE;
143 DEBUG(3, ("Request for schannel to without domain\n"));
144 return NT_STATUS_LOGON_FAILURE;
147 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
148 workstation = bind_schannel.oem_netbios_computer.a;
149 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
150 workstation = bind_schannel.utf8_netbios_computer.u;
152 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
153 return NT_STATUS_LOGON_FAILURE;
156 status = schannel_get_creds_state(out_mem_ctx,
157 gensec_security->settings->lp_ctx,
158 workstation, &creds);
159 if (!NT_STATUS_IS_OK(status)) {
160 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
161 workstation, nt_errstr(status)));
162 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
163 return NT_STATUS_LOGON_FAILURE;
168 state->creds = talloc_steal(state, creds);
170 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
171 bind_schannel_ack.Flags = 0;
172 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
177 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
178 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
179 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180 status = ndr_map_error2ntstatus(ndr_err);
181 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
182 workstation, nt_errstr(status)));
186 state->state = SCHANNEL_STATE_UPDATE_1;
190 return NT_STATUS_INVALID_PARAMETER;
194 * Return the struct netlogon_creds_CredentialState.
196 * Make sure not to call this unless gensec is using schannel...
199 /* TODO: make this non-public */
201 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
203 struct netlogon_creds_CredentialState **creds)
205 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
207 *creds = talloc_reference(mem_ctx, state->creds);
209 return NT_STATUS_NO_MEMORY;
216 * Returns anonymous credentials for schannel, matching Win2k3.
220 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
222 struct auth_session_info **_session_info)
224 return auth_anonymous_session_info(mem_ctx, gensec_security->settings->lp_ctx, _session_info);
227 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
229 struct schannel_state *state;
231 state = talloc(gensec_security, struct schannel_state);
233 return NT_STATUS_NO_MEMORY;
236 state->state = SCHANNEL_STATE_START;
238 gensec_security->private_data = state;
243 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
246 struct schannel_state *state;
248 status = schannel_start(gensec_security);
249 if (!NT_STATUS_IS_OK(status)) {
253 state = (struct schannel_state *)gensec_security->private_data;
254 state->initiator = false;
259 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
262 struct schannel_state *state;
264 status = schannel_start(gensec_security);
265 if (!NT_STATUS_IS_OK(status)) {
269 state = (struct schannel_state *)gensec_security->private_data;
270 state->initiator = true;
276 static bool schannel_have_feature(struct gensec_security *gensec_security,
279 if (feature & (GENSEC_FEATURE_SIGN |
280 GENSEC_FEATURE_SEAL)) {
283 if (feature & GENSEC_FEATURE_DCE_STYLE) {
286 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
295 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
296 uint8_t *data, size_t length,
297 const uint8_t *whole_pdu, size_t pdu_length,
298 const DATA_BLOB *sig)
300 struct schannel_state *state =
301 talloc_get_type(gensec_security->private_data,
302 struct schannel_state);
304 return netsec_incoming_packet(state, true,
305 discard_const_p(uint8_t, data),
310 check the signature on a packet
312 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
313 const uint8_t *data, size_t length,
314 const uint8_t *whole_pdu, size_t pdu_length,
315 const DATA_BLOB *sig)
317 struct schannel_state *state =
318 talloc_get_type(gensec_security->private_data,
319 struct schannel_state);
321 return netsec_incoming_packet(state, false,
322 discard_const_p(uint8_t, data),
328 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
330 uint8_t *data, size_t length,
331 const uint8_t *whole_pdu, size_t pdu_length,
334 struct schannel_state *state =
335 talloc_get_type(gensec_security->private_data,
336 struct schannel_state);
338 return netsec_outgoing_packet(state, mem_ctx, true,
345 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
347 const uint8_t *data, size_t length,
348 const uint8_t *whole_pdu, size_t pdu_length,
351 struct schannel_state *state =
352 talloc_get_type(gensec_security->private_data,
353 struct schannel_state);
355 return netsec_outgoing_packet(state, mem_ctx, false,
356 discard_const_p(uint8_t, data),
360 static const struct gensec_security_ops gensec_schannel_security_ops = {
362 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
363 .client_start = schannel_client_start,
364 .server_start = schannel_server_start,
365 .update = schannel_update,
366 .seal_packet = schannel_seal_packet,
367 .sign_packet = schannel_sign_packet,
368 .check_packet = schannel_check_packet,
369 .unseal_packet = schannel_unseal_packet,
370 .session_key = schannel_session_key,
371 .session_info = schannel_session_info,
372 .sig_size = schannel_sig_size,
373 .have_feature = schannel_have_feature,
375 .priority = GENSEC_SCHANNEL
378 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
381 ret = gensec_register(&gensec_schannel_security_ops);
382 if (!NT_STATUS_IS_OK(ret)) {
383 DEBUG(0,("Failed to register '%s' gensec backend!\n",
384 gensec_schannel_security_ops.name));