s4-auth: Make sure we use the correct credential state.
[Samba/gebeck_regimport.git] / source4 / auth / gensec / schannel.c
blobe7c545fb66c16f5257005bb9bde3c9b367f8971b
1 /*
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/>.
23 #include "includes.h"
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;
40 uint32_t sig_size;
42 sig_size = netsec_outgoing_sig_size(state);
44 return sig_size;
47 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
48 TALLOC_CTX *mem_ctx,
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 struct tevent_context *ev,
56 const DATA_BLOB in, DATA_BLOB *out)
58 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
59 NTSTATUS status;
60 enum ndr_err_code ndr_err;
61 struct NL_AUTH_MESSAGE bind_schannel;
62 struct NL_AUTH_MESSAGE bind_schannel_ack;
63 struct netlogon_creds_CredentialState *creds;
64 const char *workstation;
65 const char *domain;
67 *out = data_blob(NULL, 0);
69 switch (gensec_security->gensec_role) {
70 case GENSEC_CLIENT:
71 if (state->state != SCHANNEL_STATE_START) {
72 /* we could parse the bind ack, but we don't know what it is yet */
73 return NT_STATUS_OK;
76 state->creds = cli_credentials_get_netlogon_creds(gensec_security->credentials);
77 if (state->creds == NULL) {
78 return NT_STATUS_INVALID_PARAMETER_MIX;
81 * We need to create a reference here or we don't get
82 * updates performed on the credentials if we create a
83 * copy.
85 state->creds = talloc_reference(state, state->creds);
86 if (state->creds == NULL) {
87 return NT_STATUS_NO_MEMORY;
90 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
91 #if 0
92 /* to support this we'd need to have access to the full domain name */
93 /* 0x17, 23 */
94 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
95 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
96 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
97 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
98 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
99 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
100 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
101 /* w2k3 refuses us if we use the full DNS workstation?
102 why? perhaps because we don't fill in the dNSHostName
103 attribute in the machine account? */
104 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
105 #else
106 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
107 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
108 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
109 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
110 #endif
112 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
113 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
114 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
115 status = ndr_map_error2ntstatus(ndr_err);
116 DEBUG(3, ("Could not create schannel bind: %s\n",
117 nt_errstr(status)));
118 return status;
121 state->state = SCHANNEL_STATE_UPDATE_1;
123 return NT_STATUS_MORE_PROCESSING_REQUIRED;
124 case GENSEC_SERVER:
126 if (state->state != SCHANNEL_STATE_START) {
127 /* no third leg on this protocol */
128 return NT_STATUS_INVALID_PARAMETER;
131 /* parse the schannel startup blob */
132 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
133 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
134 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
135 status = ndr_map_error2ntstatus(ndr_err);
136 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
137 nt_errstr(status)));
138 return status;
141 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
142 domain = bind_schannel.oem_netbios_domain.a;
143 if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
144 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
145 domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
146 return NT_STATUS_LOGON_FAILURE;
148 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
149 domain = bind_schannel.utf8_dns_domain.u;
150 if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
151 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
152 domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
153 return NT_STATUS_LOGON_FAILURE;
155 } else {
156 DEBUG(3, ("Request for schannel to without domain\n"));
157 return NT_STATUS_LOGON_FAILURE;
160 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
161 workstation = bind_schannel.oem_netbios_computer.a;
162 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
163 workstation = bind_schannel.utf8_netbios_computer.u;
164 } else {
165 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
166 return NT_STATUS_LOGON_FAILURE;
169 status = schannel_get_creds_state(out_mem_ctx,
170 gensec_security->settings->lp_ctx,
171 workstation, &creds);
172 if (!NT_STATUS_IS_OK(status)) {
173 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
174 workstation, nt_errstr(status)));
175 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
176 return NT_STATUS_LOGON_FAILURE;
178 return status;
181 state->creds = talloc_steal(state, creds);
183 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
184 bind_schannel_ack.Flags = 0;
185 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
186 * this does not have
187 * any meaning here
188 * - gd */
190 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
191 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
192 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
193 status = ndr_map_error2ntstatus(ndr_err);
194 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
195 workstation, nt_errstr(status)));
196 return status;
199 state->state = SCHANNEL_STATE_UPDATE_1;
201 return NT_STATUS_OK;
203 return NT_STATUS_INVALID_PARAMETER;
207 * Return the struct netlogon_creds_CredentialState.
209 * Make sure not to call this unless gensec is using schannel...
212 /* TODO: make this non-public */
214 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
215 TALLOC_CTX *mem_ctx,
216 struct netlogon_creds_CredentialState **creds)
218 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
220 *creds = talloc_reference(mem_ctx, state->creds);
221 if (!*creds) {
222 return NT_STATUS_NO_MEMORY;
224 return NT_STATUS_OK;
229 * Returns anonymous credentials for schannel, matching Win2k3.
233 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
234 TALLOC_CTX *mem_ctx,
235 struct auth_session_info **_session_info)
237 return auth_anonymous_session_info(mem_ctx, gensec_security->settings->lp_ctx, _session_info);
240 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
242 struct schannel_state *state;
244 state = talloc_zero(gensec_security, struct schannel_state);
245 if (!state) {
246 return NT_STATUS_NO_MEMORY;
249 state->state = SCHANNEL_STATE_START;
250 gensec_security->private_data = state;
252 return NT_STATUS_OK;
255 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
257 NTSTATUS status;
258 struct schannel_state *state;
260 status = schannel_start(gensec_security);
261 if (!NT_STATUS_IS_OK(status)) {
262 return status;
265 state = (struct schannel_state *)gensec_security->private_data;
266 state->initiator = false;
268 return NT_STATUS_OK;
271 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
273 NTSTATUS status;
274 struct schannel_state *state;
276 status = schannel_start(gensec_security);
277 if (!NT_STATUS_IS_OK(status)) {
278 return status;
281 state = (struct schannel_state *)gensec_security->private_data;
282 state->initiator = true;
284 return NT_STATUS_OK;
288 static bool schannel_have_feature(struct gensec_security *gensec_security,
289 uint32_t feature)
291 if (feature & (GENSEC_FEATURE_SIGN |
292 GENSEC_FEATURE_SEAL)) {
293 return true;
295 if (feature & GENSEC_FEATURE_DCE_STYLE) {
296 return true;
298 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
299 return true;
301 return false;
305 unseal a packet
307 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
308 uint8_t *data, size_t length,
309 const uint8_t *whole_pdu, size_t pdu_length,
310 const DATA_BLOB *sig)
312 struct schannel_state *state =
313 talloc_get_type(gensec_security->private_data,
314 struct schannel_state);
316 return netsec_incoming_packet(state, true,
317 discard_const_p(uint8_t, data),
318 length, sig);
322 check the signature on a packet
324 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
325 const uint8_t *data, size_t length,
326 const uint8_t *whole_pdu, size_t pdu_length,
327 const DATA_BLOB *sig)
329 struct schannel_state *state =
330 talloc_get_type(gensec_security->private_data,
331 struct schannel_state);
333 return netsec_incoming_packet(state, false,
334 discard_const_p(uint8_t, data),
335 length, sig);
338 seal a packet
340 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
341 TALLOC_CTX *mem_ctx,
342 uint8_t *data, size_t length,
343 const uint8_t *whole_pdu, size_t pdu_length,
344 DATA_BLOB *sig)
346 struct schannel_state *state =
347 talloc_get_type(gensec_security->private_data,
348 struct schannel_state);
350 return netsec_outgoing_packet(state, mem_ctx, true,
351 data, length, sig);
355 sign a packet
357 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
358 TALLOC_CTX *mem_ctx,
359 const uint8_t *data, size_t length,
360 const uint8_t *whole_pdu, size_t pdu_length,
361 DATA_BLOB *sig)
363 struct schannel_state *state =
364 talloc_get_type(gensec_security->private_data,
365 struct schannel_state);
367 return netsec_outgoing_packet(state, mem_ctx, false,
368 discard_const_p(uint8_t, data),
369 length, sig);
372 static const struct gensec_security_ops gensec_schannel_security_ops = {
373 .name = "schannel",
374 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
375 .client_start = schannel_client_start,
376 .server_start = schannel_server_start,
377 .update = schannel_update,
378 .seal_packet = schannel_seal_packet,
379 .sign_packet = schannel_sign_packet,
380 .check_packet = schannel_check_packet,
381 .unseal_packet = schannel_unseal_packet,
382 .session_key = schannel_session_key,
383 .session_info = schannel_session_info,
384 .sig_size = schannel_sig_size,
385 .have_feature = schannel_have_feature,
386 .enabled = true,
387 .priority = GENSEC_SCHANNEL
390 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
392 NTSTATUS ret;
393 ret = gensec_register(&gensec_schannel_security_ops);
394 if (!NT_STATUS_IS_OK(ret)) {
395 DEBUG(0,("Failed to register '%s' gensec backend!\n",
396 gensec_schannel_security_ops.name));
397 return ret;
400 return ret;