auth/gensec: introduce gensec_internal.h
[Samba/wip.git] / source4 / auth / gensec / schannel.c
blobeb2e100e2e526719d57671d27685a2609b127503
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_internal.h"
29 #include "auth/gensec/gensec_proto.h"
30 #include "../libcli/auth/schannel.h"
31 #include "librpc/gen_ndr/dcerpc.h"
32 #include "param/param.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 =
40 talloc_get_type_abort(gensec_security->private_data,
41 struct schannel_state);
43 return netsec_outgoing_sig_size(state);
46 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
47 struct tevent_context *ev,
48 const DATA_BLOB in, DATA_BLOB *out)
50 struct schannel_state *state =
51 talloc_get_type(gensec_security->private_data,
52 struct schannel_state);
53 NTSTATUS status;
54 enum ndr_err_code ndr_err;
55 struct NL_AUTH_MESSAGE bind_schannel;
56 struct NL_AUTH_MESSAGE bind_schannel_ack;
57 struct netlogon_creds_CredentialState *creds;
58 const char *workstation;
59 const char *domain;
61 *out = data_blob(NULL, 0);
63 switch (gensec_security->gensec_role) {
64 case GENSEC_CLIENT:
65 if (state != NULL) {
66 /* we could parse the bind ack, but we don't know what it is yet */
67 return NT_STATUS_OK;
70 creds = cli_credentials_get_netlogon_creds(gensec_security->credentials);
71 if (creds == NULL) {
72 return NT_STATUS_INVALID_PARAMETER_MIX;
75 state = netsec_create_state(gensec_security,
76 creds, true /* initiator */);
77 if (state == NULL) {
78 return NT_STATUS_NO_MEMORY;
80 gensec_security->private_data = state;
82 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
83 #if 0
84 /* to support this we'd need to have access to the full domain name */
85 /* 0x17, 23 */
86 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
87 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
88 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
89 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
90 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
91 bind_schannel.oem_netbios_computer.a = creds->computer_name;
92 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
93 /* w2k3 refuses us if we use the full DNS workstation?
94 why? perhaps because we don't fill in the dNSHostName
95 attribute in the machine account? */
96 bind_schannel.utf8_netbios_computer = creds->computer_name;
97 #else
98 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
99 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
100 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
101 bind_schannel.oem_netbios_computer.a = creds->computer_name;
102 #endif
104 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
105 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
106 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
107 status = ndr_map_error2ntstatus(ndr_err);
108 DEBUG(3, ("Could not create schannel bind: %s\n",
109 nt_errstr(status)));
110 return status;
113 return NT_STATUS_MORE_PROCESSING_REQUIRED;
114 case GENSEC_SERVER:
116 if (state != NULL) {
117 /* no third leg on this protocol */
118 return NT_STATUS_INVALID_PARAMETER;
121 /* parse the schannel startup blob */
122 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
123 (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
124 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
125 status = ndr_map_error2ntstatus(ndr_err);
126 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
127 nt_errstr(status)));
128 return status;
131 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
132 domain = bind_schannel.oem_netbios_domain.a;
133 if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
134 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
135 domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
136 return NT_STATUS_LOGON_FAILURE;
138 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
139 domain = bind_schannel.utf8_dns_domain.u;
140 if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
141 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
142 domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
143 return NT_STATUS_LOGON_FAILURE;
145 } else {
146 DEBUG(3, ("Request for schannel to without domain\n"));
147 return NT_STATUS_LOGON_FAILURE;
150 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
151 workstation = bind_schannel.oem_netbios_computer.a;
152 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
153 workstation = bind_schannel.utf8_netbios_computer.u;
154 } else {
155 DEBUG(3, ("Request for schannel to without netbios workstation\n"));
156 return NT_STATUS_LOGON_FAILURE;
159 status = schannel_get_creds_state(out_mem_ctx,
160 gensec_security->settings->lp_ctx,
161 workstation, &creds);
162 if (!NT_STATUS_IS_OK(status)) {
163 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
164 workstation, nt_errstr(status)));
165 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
166 return NT_STATUS_LOGON_FAILURE;
168 return status;
171 state = netsec_create_state(gensec_security,
172 creds, false /* not initiator */);
173 if (state == NULL) {
174 return NT_STATUS_NO_MEMORY;
176 gensec_security->private_data = state;
178 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
179 bind_schannel_ack.Flags = 0;
180 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
181 * this does not have
182 * any meaning here
183 * - gd */
185 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
186 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
187 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
188 status = ndr_map_error2ntstatus(ndr_err);
189 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
190 workstation, nt_errstr(status)));
191 return status;
194 return NT_STATUS_OK;
196 return NT_STATUS_INVALID_PARAMETER;
200 * Returns anonymous credentials for schannel, matching Win2k3.
204 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
205 TALLOC_CTX *mem_ctx,
206 struct auth_session_info **_session_info)
208 return auth_anonymous_session_info(mem_ctx, gensec_security->settings->lp_ctx, _session_info);
211 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
213 return NT_STATUS_OK;
216 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
218 return NT_STATUS_OK;
221 static bool schannel_have_feature(struct gensec_security *gensec_security,
222 uint32_t feature)
224 if (feature & (GENSEC_FEATURE_SIGN |
225 GENSEC_FEATURE_SEAL)) {
226 return true;
228 if (feature & GENSEC_FEATURE_DCE_STYLE) {
229 return true;
231 return false;
235 unseal a packet
237 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
238 uint8_t *data, size_t length,
239 const uint8_t *whole_pdu, size_t pdu_length,
240 const DATA_BLOB *sig)
242 struct schannel_state *state =
243 talloc_get_type_abort(gensec_security->private_data,
244 struct schannel_state);
246 return netsec_incoming_packet(state, true,
247 discard_const_p(uint8_t, data),
248 length, sig);
252 check the signature on a packet
254 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
255 const uint8_t *data, size_t length,
256 const uint8_t *whole_pdu, size_t pdu_length,
257 const DATA_BLOB *sig)
259 struct schannel_state *state =
260 talloc_get_type_abort(gensec_security->private_data,
261 struct schannel_state);
263 return netsec_incoming_packet(state, false,
264 discard_const_p(uint8_t, data),
265 length, sig);
268 seal a packet
270 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
271 TALLOC_CTX *mem_ctx,
272 uint8_t *data, size_t length,
273 const uint8_t *whole_pdu, size_t pdu_length,
274 DATA_BLOB *sig)
276 struct schannel_state *state =
277 talloc_get_type_abort(gensec_security->private_data,
278 struct schannel_state);
280 return netsec_outgoing_packet(state, mem_ctx, true,
281 data, length, sig);
285 sign a packet
287 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
288 TALLOC_CTX *mem_ctx,
289 const uint8_t *data, size_t length,
290 const uint8_t *whole_pdu, size_t pdu_length,
291 DATA_BLOB *sig)
293 struct schannel_state *state =
294 talloc_get_type_abort(gensec_security->private_data,
295 struct schannel_state);
297 return netsec_outgoing_packet(state, mem_ctx, false,
298 discard_const_p(uint8_t, data),
299 length, sig);
302 static const struct gensec_security_ops gensec_schannel_security_ops = {
303 .name = "schannel",
304 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
305 .client_start = schannel_client_start,
306 .server_start = schannel_server_start,
307 .update = schannel_update,
308 .seal_packet = schannel_seal_packet,
309 .sign_packet = schannel_sign_packet,
310 .check_packet = schannel_check_packet,
311 .unseal_packet = schannel_unseal_packet,
312 .session_info = schannel_session_info,
313 .sig_size = schannel_sig_size,
314 .have_feature = schannel_have_feature,
315 .enabled = true,
316 .priority = GENSEC_SCHANNEL
319 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
321 NTSTATUS ret;
322 ret = gensec_register(&gensec_schannel_security_ops);
323 if (!NT_STATUS_IS_OK(ret)) {
324 DEBUG(0,("Failed to register '%s' gensec backend!\n",
325 gensec_schannel_security_ops.name));
326 return ret;
329 return ret;