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"
34 _PUBLIC_ NTSTATUS gensec_schannel_init(void);
36 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
38 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
41 sig_size = netsec_outgoing_sig_size(state);
46 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
48 DATA_BLOB *session_key)
50 return NT_STATUS_NOT_IMPLEMENTED;
53 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
54 const DATA_BLOB in, DATA_BLOB *out)
56 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
58 enum ndr_err_code ndr_err;
59 struct NL_AUTH_MESSAGE bind_schannel;
60 struct NL_AUTH_MESSAGE bind_schannel_ack;
61 struct netlogon_creds_CredentialState *creds;
62 const char *workstation;
65 *out = data_blob(NULL, 0);
67 switch (gensec_security->gensec_role) {
69 if (state->state != SCHANNEL_STATE_START) {
70 /* we could parse the bind ack, but we don't know what it is yet */
74 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
76 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
78 /* to support this we'd need to have access to the full domain name */
80 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
81 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
82 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
83 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
84 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
85 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
86 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
87 /* w2k3 refuses us if we use the full DNS workstation?
88 why? perhaps because we don't fill in the dNSHostName
89 attribute in the machine account? */
90 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
92 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
93 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
94 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
95 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
98 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
99 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
100 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
101 status = ndr_map_error2ntstatus(ndr_err);
102 DEBUG(3, ("Could not create schannel bind: %s\n",
107 state->state = SCHANNEL_STATE_UPDATE_1;
109 return NT_STATUS_MORE_PROCESSING_REQUIRED;
112 if (state->state != SCHANNEL_STATE_START) {
113 /* no third leg on this protocol */
114 return NT_STATUS_INVALID_PARAMETER;
117 /* parse the schannel startup blob */
118 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
119 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
120 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
121 status = ndr_map_error2ntstatus(ndr_err);
122 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
127 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
128 domain = bind_schannel.oem_netbios_domain.a;
129 if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
130 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
131 domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
132 return NT_STATUS_LOGON_FAILURE;
134 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
135 domain = bind_schannel.utf8_dns_domain.u;
136 if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
137 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
138 domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
139 return NT_STATUS_LOGON_FAILURE;
142 DEBUG(3, ("Request for schannel to without domain\n"));
143 return NT_STATUS_LOGON_FAILURE;
146 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
147 workstation = bind_schannel.oem_netbios_computer.a;
148 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
149 workstation = bind_schannel.utf8_netbios_computer.u;
151 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
152 return NT_STATUS_LOGON_FAILURE;
155 status = schannel_get_creds_state(out_mem_ctx,
156 lpcfg_private_dir(gensec_security->settings->lp_ctx),
157 workstation, &creds);
158 if (!NT_STATUS_IS_OK(status)) {
159 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
160 workstation, nt_errstr(status)));
161 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
162 return NT_STATUS_LOGON_FAILURE;
167 state->creds = talloc_steal(state, creds);
169 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
170 bind_schannel_ack.Flags = 0;
171 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
176 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
177 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
178 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
179 status = ndr_map_error2ntstatus(ndr_err);
180 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
181 workstation, nt_errstr(status)));
185 state->state = SCHANNEL_STATE_UPDATE_1;
189 return NT_STATUS_INVALID_PARAMETER;
193 * Return the struct netlogon_creds_CredentialState.
195 * Make sure not to call this unless gensec is using schannel...
198 /* TODO: make this non-public */
200 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
202 struct netlogon_creds_CredentialState **creds)
204 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
206 *creds = talloc_reference(mem_ctx, state->creds);
208 return NT_STATUS_NO_MEMORY;
215 * Returns anonymous credentials for schannel, matching Win2k3.
219 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
221 struct auth_session_info **_session_info)
223 return auth_anonymous_session_info(mem_ctx, gensec_security->settings->lp_ctx, _session_info);
226 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
228 struct schannel_state *state;
230 state = talloc(gensec_security, struct schannel_state);
232 return NT_STATUS_NO_MEMORY;
235 state->state = SCHANNEL_STATE_START;
237 gensec_security->private_data = state;
242 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
245 struct schannel_state *state;
247 status = schannel_start(gensec_security);
248 if (!NT_STATUS_IS_OK(status)) {
252 state = (struct schannel_state *)gensec_security->private_data;
253 state->initiator = false;
258 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
261 struct schannel_state *state;
263 status = schannel_start(gensec_security);
264 if (!NT_STATUS_IS_OK(status)) {
268 state = (struct schannel_state *)gensec_security->private_data;
269 state->initiator = true;
275 static bool schannel_have_feature(struct gensec_security *gensec_security,
278 if (feature & (GENSEC_FEATURE_SIGN |
279 GENSEC_FEATURE_SEAL)) {
282 if (feature & GENSEC_FEATURE_DCE_STYLE) {
285 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
294 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
295 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, true,
304 discard_const_p(uint8_t, data),
309 check the signature on a packet
311 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
312 const uint8_t *data, size_t length,
313 const uint8_t *whole_pdu, size_t pdu_length,
314 const DATA_BLOB *sig)
316 struct schannel_state *state =
317 talloc_get_type(gensec_security->private_data,
318 struct schannel_state);
320 return netsec_incoming_packet(state, false,
321 discard_const_p(uint8_t, data),
327 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
329 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, true,
344 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
346 const uint8_t *data, size_t length,
347 const uint8_t *whole_pdu, size_t pdu_length,
350 struct schannel_state *state =
351 talloc_get_type(gensec_security->private_data,
352 struct schannel_state);
354 return netsec_outgoing_packet(state, mem_ctx, false,
355 discard_const_p(uint8_t, data),
359 static const struct gensec_security_ops gensec_schannel_security_ops = {
361 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
362 .client_start = schannel_client_start,
363 .server_start = schannel_server_start,
364 .update = schannel_update,
365 .seal_packet = schannel_seal_packet,
366 .sign_packet = schannel_sign_packet,
367 .check_packet = schannel_check_packet,
368 .unseal_packet = schannel_unseal_packet,
369 .session_key = schannel_session_key,
370 .session_info = schannel_session_info,
371 .sig_size = schannel_sig_size,
372 .have_feature = schannel_have_feature,
374 .priority = GENSEC_SCHANNEL
377 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
380 ret = gensec_register(&gensec_schannel_security_ops);
381 if (!NT_STATUS_IS_OK(ret)) {
382 DEBUG(0,("Failed to register '%s' gensec backend!\n",
383 gensec_schannel_security_ops.name));