s4-drs: Add DRSUAPI_DRS_NONGC_RO_REP bit to DRS_OPTIONS
[Samba/fernandojvsilva.git] / source4 / auth / gensec / schannel.c
blob7b8bdec27a7b15327b3178de12bc90b6499fd61b
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 "auth/gensec/schannel_state.h"
31 #include "librpc/rpc/dcerpc.h"
32 #include "param/param.h"
34 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
36 return 32;
39 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
40 DATA_BLOB *session_key)
42 return NT_STATUS_NOT_IMPLEMENTED;
45 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
46 const DATA_BLOB in, DATA_BLOB *out)
48 struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
49 NTSTATUS status;
50 enum ndr_err_code ndr_err;
51 struct NL_AUTH_MESSAGE bind_schannel;
52 struct NL_AUTH_MESSAGE bind_schannel_ack;
53 struct netlogon_creds_CredentialState *creds;
54 struct ldb_context *schannel_ldb;
55 const char *workstation;
56 const char *domain;
57 uint32_t required_flags;
59 *out = data_blob(NULL, 0);
61 switch (gensec_security->gensec_role) {
62 case GENSEC_CLIENT:
63 if (state->state != SCHANNEL_STATE_START) {
64 /* we could parse the bind ack, but we don't know what it is yet */
65 return NT_STATUS_OK;
68 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
70 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
71 #if 0
72 /* to support this we'd need to have access to the full domain name */
73 /* 0x17, 23 */
74 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
75 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
76 NL_FLAG_UTF8_DNS_DOMAIN_NAME |
77 NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
78 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
79 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
80 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
81 /* w2k3 refuses us if we use the full DNS workstation?
82 why? perhaps because we don't fill in the dNSHostName
83 attribute in the machine account? */
84 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
85 #else
86 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
87 NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
88 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
89 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
90 #endif
92 ndr_err = ndr_push_struct_blob(out, out_mem_ctx,
93 gensec_security->settings->iconv_convenience, &bind_schannel,
94 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
95 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
96 status = ndr_map_error2ntstatus(ndr_err);
97 DEBUG(3, ("Could not create schannel bind: %s\n",
98 nt_errstr(status)));
99 return status;
102 state->state = SCHANNEL_STATE_UPDATE_1;
104 return NT_STATUS_MORE_PROCESSING_REQUIRED;
105 case GENSEC_SERVER:
107 required_flags = NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
108 NL_FLAG_OEM_NETBIOS_DOMAIN_NAME;
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,
117 gensec_security->settings->iconv_convenience,
118 &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",
123 nt_errstr(status)));
124 return status;
127 if (!(required_flags == (bind_schannel.Flags & required_flags))) {
128 return NT_STATUS_INVALID_PARAMETER;
131 workstation = bind_schannel.oem_netbios_computer.a;
132 domain = bind_schannel.oem_netbios_domain.a;
134 if (strcasecmp_m(domain, lp_workgroup(gensec_security->settings->lp_ctx)) != 0) {
135 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
136 domain, lp_workgroup(gensec_security->settings->lp_ctx)));
138 return NT_STATUS_LOGON_FAILURE;
141 schannel_ldb = schannel_db_connect(out_mem_ctx, gensec_security->event_ctx,
142 gensec_security->settings->lp_ctx);
143 if (!schannel_ldb) {
144 return NT_STATUS_ACCESS_DENIED;
146 /* pull the session key for this client */
147 status = schannel_fetch_session_key_ldb(schannel_ldb,
148 out_mem_ctx, workstation, &creds);
149 talloc_unlink(out_mem_ctx, schannel_ldb);
150 if (!NT_STATUS_IS_OK(status)) {
151 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
152 workstation, nt_errstr(status)));
153 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
154 return NT_STATUS_LOGON_FAILURE;
156 return status;
159 state->creds = talloc_steal(state, creds);
161 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
162 bind_schannel_ack.Flags = 0;
163 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
164 * this does not have
165 * any meaning here
166 * - gd */
168 ndr_err = ndr_push_struct_blob(out, out_mem_ctx,
169 gensec_security->settings->iconv_convenience, &bind_schannel_ack,
170 (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
171 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
172 status = ndr_map_error2ntstatus(ndr_err);
173 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
174 workstation, nt_errstr(status)));
175 return status;
178 state->state = SCHANNEL_STATE_UPDATE_1;
180 return NT_STATUS_OK;
182 return NT_STATUS_INVALID_PARAMETER;
186 * Return the struct netlogon_creds_CredentialState.
188 * Make sure not to call this unless gensec is using schannel...
191 /* TODO: make this non-public */
193 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
194 TALLOC_CTX *mem_ctx,
195 struct netlogon_creds_CredentialState **creds)
197 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
199 *creds = talloc_reference(mem_ctx, state->creds);
200 if (!*creds) {
201 return NT_STATUS_NO_MEMORY;
203 return NT_STATUS_OK;
208 * Returns anonymous credentials for schannel, matching Win2k3.
212 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
213 struct auth_session_info **_session_info)
215 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
216 return auth_anonymous_session_info(state, gensec_security->event_ctx, gensec_security->settings->lp_ctx, _session_info);
219 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
221 struct schannel_state *state;
223 state = talloc(gensec_security, struct schannel_state);
224 if (!state) {
225 return NT_STATUS_NO_MEMORY;
228 state->state = SCHANNEL_STATE_START;
229 state->seq_num = 0;
230 gensec_security->private_data = state;
232 return NT_STATUS_OK;
235 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
237 NTSTATUS status;
238 struct schannel_state *state;
240 status = schannel_start(gensec_security);
241 if (!NT_STATUS_IS_OK(status)) {
242 return status;
245 state = (struct schannel_state *)gensec_security->private_data;
246 state->initiator = false;
248 return NT_STATUS_OK;
251 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
253 NTSTATUS status;
254 struct schannel_state *state;
256 status = schannel_start(gensec_security);
257 if (!NT_STATUS_IS_OK(status)) {
258 return status;
261 state = (struct schannel_state *)gensec_security->private_data;
262 state->initiator = true;
264 return NT_STATUS_OK;
268 static bool schannel_have_feature(struct gensec_security *gensec_security,
269 uint32_t feature)
271 if (feature & (GENSEC_FEATURE_SIGN |
272 GENSEC_FEATURE_SEAL)) {
273 return true;
275 if (feature & GENSEC_FEATURE_DCE_STYLE) {
276 return true;
278 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
279 return true;
281 return false;
285 unseal a packet
287 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
288 TALLOC_CTX *mem_ctx,
289 uint8_t *data, size_t length,
290 const uint8_t *whole_pdu, size_t pdu_length,
291 const DATA_BLOB *sig)
293 struct schannel_state *state =
294 talloc_get_type(gensec_security->private_data,
295 struct schannel_state);
297 return netsec_incoming_packet(state, mem_ctx, true,
298 discard_const_p(uint8_t, data),
299 length, sig);
303 check the signature on a packet
305 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
306 TALLOC_CTX *mem_ctx,
307 const uint8_t *data, size_t length,
308 const uint8_t *whole_pdu, size_t pdu_length,
309 const DATA_BLOB *sig)
311 struct schannel_state *state =
312 talloc_get_type(gensec_security->private_data,
313 struct schannel_state);
315 return netsec_incoming_packet(state, mem_ctx, false,
316 discard_const_p(uint8_t, data),
317 length, sig);
320 seal a packet
322 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
323 TALLOC_CTX *mem_ctx,
324 uint8_t *data, size_t length,
325 const uint8_t *whole_pdu, size_t pdu_length,
326 DATA_BLOB *sig)
328 struct schannel_state *state =
329 talloc_get_type(gensec_security->private_data,
330 struct schannel_state);
332 return netsec_outgoing_packet(state, mem_ctx, true,
333 data, length, sig);
337 sign a packet
339 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
340 TALLOC_CTX *mem_ctx,
341 const uint8_t *data, size_t length,
342 const uint8_t *whole_pdu, size_t pdu_length,
343 DATA_BLOB *sig)
345 struct schannel_state *state =
346 talloc_get_type(gensec_security->private_data,
347 struct schannel_state);
349 return netsec_outgoing_packet(state, mem_ctx, false,
350 discard_const_p(uint8_t, data),
351 length, sig);
354 static const struct gensec_security_ops gensec_schannel_security_ops = {
355 .name = "schannel",
356 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
357 .client_start = schannel_client_start,
358 .server_start = schannel_server_start,
359 .update = schannel_update,
360 .seal_packet = schannel_seal_packet,
361 .sign_packet = schannel_sign_packet,
362 .check_packet = schannel_check_packet,
363 .unseal_packet = schannel_unseal_packet,
364 .session_key = schannel_session_key,
365 .session_info = schannel_session_info,
366 .sig_size = schannel_sig_size,
367 .have_feature = schannel_have_feature,
368 .enabled = true,
369 .priority = GENSEC_SCHANNEL
372 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
374 NTSTATUS ret;
375 ret = gensec_register(&gensec_schannel_security_ops);
376 if (!NT_STATUS_IS_OK(ret)) {
377 DEBUG(0,("Failed to register '%s' gensec backend!\n",
378 gensec_schannel_security_ops.name));
379 return ret;
382 return ret;