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/schannel.h"
29 #include "auth/gensec/schannel_state.h"
30 #include "auth/gensec/schannel_proto.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)
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;
50 enum ndr_err_code ndr_err;
51 struct schannel_bind bind_schannel;
52 struct schannel_bind_ack bind_schannel_ack;
53 struct creds_CredentialState *creds;
55 const char *workstation;
57 *out = data_blob(NULL, 0);
59 switch (gensec_security->gensec_role) {
61 if (state->state != SCHANNEL_STATE_START) {
62 /* we could parse the bind ack, but we don't know what it is yet */
66 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
68 bind_schannel.unknown1 = 0;
70 /* to support this we'd need to have access to the full domain name */
71 bind_schannel.bind_type = 23;
72 bind_schannel.u.info23.domain = cli_credentials_get_domain(gensec_security->credentials);
73 bind_schannel.u.info23.workstation = cli_credentials_get_workstation(gensec_security->credentials);
74 bind_schannel.u.info23.dnsdomain = cli_credentials_get_realm(gensec_security->credentials);
75 /* w2k3 refuses us if we use the full DNS workstation?
76 why? perhaps because we don't fill in the dNSHostName
77 attribute in the machine account? */
78 bind_schannel.u.info23.dnsworkstation = cli_credentials_get_workstation(gensec_security->credentials);
80 bind_schannel.bind_type = 3;
81 bind_schannel.u.info3.domain = cli_credentials_get_domain(gensec_security->credentials);
82 bind_schannel.u.info3.workstation = cli_credentials_get_workstation(gensec_security->credentials);
85 ndr_err = ndr_push_struct_blob(out, out_mem_ctx,
86 lp_iconv_convenience(gensec_security->lp_ctx), &bind_schannel,
87 (ndr_push_flags_fn_t)ndr_push_schannel_bind);
88 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
89 status = ndr_map_error2ntstatus(ndr_err);
90 DEBUG(3, ("Could not create schannel bind: %s\n",
95 state->state = SCHANNEL_STATE_UPDATE_1;
97 return NT_STATUS_MORE_PROCESSING_REQUIRED;
100 if (state->state != SCHANNEL_STATE_START) {
101 /* no third leg on this protocol */
102 return NT_STATUS_INVALID_PARAMETER;
105 /* parse the schannel startup blob */
106 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
107 (ndr_pull_flags_fn_t)ndr_pull_schannel_bind);
108 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
109 status = ndr_map_error2ntstatus(ndr_err);
110 DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
115 if (bind_schannel.bind_type == 23) {
116 workstation = bind_schannel.u.info23.workstation;
117 domain = bind_schannel.u.info23.domain;
119 workstation = bind_schannel.u.info3.workstation;
120 domain = bind_schannel.u.info3.domain;
123 /* pull the session key for this client */
124 status = schannel_fetch_session_key(out_mem_ctx, gensec_security->lp_ctx, workstation,
126 if (!NT_STATUS_IS_OK(status)) {
127 DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
128 workstation, nt_errstr(status)));
129 if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
130 return NT_STATUS_LOGON_FAILURE;
135 state->creds = talloc_reference(state, creds);
137 bind_schannel_ack.unknown1 = 1;
138 bind_schannel_ack.unknown2 = 0;
139 bind_schannel_ack.unknown3 = 0x6c0000;
141 ndr_err = ndr_push_struct_blob(out, out_mem_ctx,
142 lp_iconv_convenience(gensec_security->lp_ctx), &bind_schannel_ack,
143 (ndr_push_flags_fn_t)ndr_push_schannel_bind_ack);
144 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
145 status = ndr_map_error2ntstatus(ndr_err);
146 DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
147 workstation, nt_errstr(status)));
151 state->state = SCHANNEL_STATE_UPDATE_1;
155 return NT_STATUS_INVALID_PARAMETER;
159 * Return the struct creds_CredentialState.
161 * Make sure not to call this unless gensec is using schannel...
164 /* TODO: make this non-public */
165 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
167 struct creds_CredentialState **creds)
169 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
171 *creds = talloc_reference(mem_ctx, state->creds);
173 return NT_STATUS_NO_MEMORY;
180 * Returns anonymous credentials for schannel, matching Win2k3.
184 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
185 struct auth_session_info **_session_info)
187 struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
188 return auth_anonymous_session_info(state, gensec_security->lp_ctx, _session_info);
191 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
193 struct schannel_state *state;
195 state = talloc(gensec_security, struct schannel_state);
197 return NT_STATUS_NO_MEMORY;
200 state->state = SCHANNEL_STATE_START;
202 gensec_security->private_data = state;
207 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
210 struct schannel_state *state;
212 status = schannel_start(gensec_security);
213 if (!NT_STATUS_IS_OK(status)) {
217 state = (struct schannel_state *)gensec_security->private_data;
218 state->initiator = false;
223 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
226 struct schannel_state *state;
228 status = schannel_start(gensec_security);
229 if (!NT_STATUS_IS_OK(status)) {
233 state = (struct schannel_state *)gensec_security->private_data;
234 state->initiator = true;
240 static bool schannel_have_feature(struct gensec_security *gensec_security,
243 if (feature & (GENSEC_FEATURE_SIGN |
244 GENSEC_FEATURE_SEAL)) {
247 if (feature & GENSEC_FEATURE_DCE_STYLE) {
250 if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
257 static const struct gensec_security_ops gensec_schannel_security_ops = {
259 .auth_type = DCERPC_AUTH_TYPE_SCHANNEL,
260 .client_start = schannel_client_start,
261 .server_start = schannel_server_start,
262 .update = schannel_update,
263 .seal_packet = schannel_seal_packet,
264 .sign_packet = schannel_sign_packet,
265 .check_packet = schannel_check_packet,
266 .unseal_packet = schannel_unseal_packet,
267 .session_key = schannel_session_key,
268 .session_info = schannel_session_info,
269 .sig_size = schannel_sig_size,
270 .have_feature = schannel_have_feature,
272 .priority = GENSEC_SCHANNEL
275 NTSTATUS gensec_schannel_init(void)
278 ret = gensec_register(&gensec_schannel_security_ops);
279 if (!NT_STATUS_IS_OK(ret)) {
280 DEBUG(0,("Failed to register '%s' gensec backend!\n",
281 gensec_schannel_security_ops.name));