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 _PUBLIC_ NTSTATUS gensec_schannel_init(void);
35 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
37 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
40 sig_size = netsec_outgoing_sig_size(state);
45 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
46 DATA_BLOB *session_key)
48 return NT_STATUS_NOT_IMPLEMENTED;
51 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
52 const DATA_BLOB in, DATA_BLOB *out)
54 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
56 enum ndr_err_code ndr_err;
57 struct NL_AUTH_MESSAGE bind_schannel;
58 struct NL_AUTH_MESSAGE bind_schannel_ack;
59 struct netlogon_creds_CredentialState *creds;
60 const char *workstation;
63 *out = data_blob(NULL, 0);
65 switch (gensec_security->gensec_role) {
67 if (state->state != SCHANNEL_STATE_START) {
68 /* we could parse the bind ack, but we don't know what it is yet */
72 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
74 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
76 /* to support this we'd need to have access to the full domain name */
78 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
79 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
80 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
81 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
82 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
83 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
84 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
85 /* w2k3 refuses us if we use the full DNS workstation?
86 why? perhaps because we don't fill in the dNSHostName
87 attribute in the machine account? */
88 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
90 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
91 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
92 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
93 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
96 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
97 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
98 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
99 status = ndr_map_error2ntstatus(ndr_err);
100 DEBUG(3, ("Could not create schannel bind: %s\n",
105 state->state = SCHANNEL_STATE_UPDATE_1;
107 return NT_STATUS_MORE_PROCESSING_REQUIRED;
110 if (state->state != SCHANNEL_STATE_START) {
111 /* no third leg on this protocol */
112 return NT_STATUS_INVALID_PARAMETER;
115 /* parse the schannel startup blob */
116 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
117 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
118 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
119 status = ndr_map_error2ntstatus(ndr_err);
120 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
125 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
126 domain = bind_schannel.oem_netbios_domain.a;
127 if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
128 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
129 domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
130 return NT_STATUS_LOGON_FAILURE;
132 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
133 domain = bind_schannel.utf8_dns_domain.u;
134 if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
135 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
136 domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
137 return NT_STATUS_LOGON_FAILURE;
140 DEBUG(3, ("Request for schannel to without domain\n"));
141 return NT_STATUS_LOGON_FAILURE;
144 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
145 workstation = bind_schannel.oem_netbios_computer.a;
146 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
147 workstation = bind_schannel.utf8_netbios_computer.u;
149 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
150 return NT_STATUS_LOGON_FAILURE;
153 status = schannel_get_creds_state(out_mem_ctx,
154 lpcfg_private_dir(gensec_security->settings->lp_ctx),
155 workstation, &creds);
156 if (!NT_STATUS_IS_OK(status)) {
157 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
158 workstation, nt_errstr(status)));
159 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
160 return NT_STATUS_LOGON_FAILURE;
165 state->creds = talloc_steal(state, creds);
167 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
168 bind_schannel_ack.Flags = 0;
169 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
174 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
175 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 status = ndr_map_error2ntstatus(ndr_err);
178 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
179 workstation, nt_errstr(status)));
183 state->state = SCHANNEL_STATE_UPDATE_1;
187 return NT_STATUS_INVALID_PARAMETER;
191 * Return the struct netlogon_creds_CredentialState.
193 * Make sure not to call this unless gensec is using schannel...
196 /* TODO: make this non-public */
198 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
200 struct netlogon_creds_CredentialState **creds)
202 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
204 *creds = talloc_reference(mem_ctx, state->creds);
206 return NT_STATUS_NO_MEMORY;
213 * Returns anonymous credentials for schannel, matching Win2k3.
217 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
218 struct auth_session_info **_session_info)
220 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
221 return auth_anonymous_session_info(state, gensec_security->settings->lp_ctx, _session_info);
224 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
226 struct schannel_state *state;
228 state = talloc(gensec_security, struct schannel_state);
230 return NT_STATUS_NO_MEMORY;
233 state->state = SCHANNEL_STATE_START;
235 gensec_security->private_data = state;
240 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
243 struct schannel_state *state;
245 status = schannel_start(gensec_security);
246 if (!NT_STATUS_IS_OK(status)) {
250 state = (struct schannel_state *)gensec_security->private_data;
251 state->initiator = false;
256 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
259 struct schannel_state *state;
261 status = schannel_start(gensec_security);
262 if (!NT_STATUS_IS_OK(status)) {
266 state = (struct schannel_state *)gensec_security->private_data;
267 state->initiator = true;
273 static bool schannel_have_feature(struct gensec_security *gensec_security,
276 if (feature & (GENSEC_FEATURE_SIGN |
277 GENSEC_FEATURE_SEAL)) {
280 if (feature & GENSEC_FEATURE_DCE_STYLE) {
283 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
292 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
293 uint8_t *data, size_t length,
294 const uint8_t *whole_pdu, size_t pdu_length,
295 const DATA_BLOB *sig)
297 struct schannel_state *state =
298 talloc_get_type(gensec_security->private_data,
299 struct schannel_state);
301 return netsec_incoming_packet(state, true,
302 discard_const_p(uint8_t, data),
307 check the signature on a packet
309 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
310 const uint8_t *data, size_t length,
311 const uint8_t *whole_pdu, size_t pdu_length,
312 const DATA_BLOB *sig)
314 struct schannel_state *state =
315 talloc_get_type(gensec_security->private_data,
316 struct schannel_state);
318 return netsec_incoming_packet(state, false,
319 discard_const_p(uint8_t, data),
325 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
327 uint8_t *data, size_t length,
328 const uint8_t *whole_pdu, size_t pdu_length,
331 struct schannel_state *state =
332 talloc_get_type(gensec_security->private_data,
333 struct schannel_state);
335 return netsec_outgoing_packet(state, mem_ctx, true,
342 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
344 const uint8_t *data, size_t length,
345 const uint8_t *whole_pdu, size_t pdu_length,
348 struct schannel_state *state =
349 talloc_get_type(gensec_security->private_data,
350 struct schannel_state);
352 return netsec_outgoing_packet(state, mem_ctx, false,
353 discard_const_p(uint8_t, data),
357 static const struct gensec_security_ops gensec_schannel_security_ops = {
359 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
360 .client_start = schannel_client_start,
361 .server_start = schannel_server_start,
362 .update = schannel_update,
363 .seal_packet = schannel_seal_packet,
364 .sign_packet = schannel_sign_packet,
365 .check_packet = schannel_check_packet,
366 .unseal_packet = schannel_unseal_packet,
367 .session_key = schannel_session_key,
368 .session_info = schannel_session_info,
369 .sig_size = schannel_sig_size,
370 .have_feature = schannel_have_feature,
372 .priority = GENSEC_SCHANNEL
375 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
378 ret = gensec_register(&gensec_schannel_security_ops);
379 if (!NT_STATUS_IS_OK(ret)) {
380 DEBUG(0,("Failed to register '%s' gensec backend!\n",
381 gensec_schannel_security_ops.name));