1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | dcerpc schannel operations
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2004
|
---|
7 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
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.
|
---|
18 |
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
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 "librpc/rpc/dcerpc.h"
|
---|
31 | #include "param/param.h"
|
---|
32 |
|
---|
33 | static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
|
---|
34 | {
|
---|
35 | struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
|
---|
36 | uint32_t sig_size;
|
---|
37 |
|
---|
38 | sig_size = netsec_outgoing_sig_size(state);
|
---|
39 |
|
---|
40 | return sig_size;
|
---|
41 | }
|
---|
42 |
|
---|
43 | static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
|
---|
44 | DATA_BLOB *session_key)
|
---|
45 | {
|
---|
46 | return NT_STATUS_NOT_IMPLEMENTED;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
|
---|
50 | const DATA_BLOB in, DATA_BLOB *out)
|
---|
51 | {
|
---|
52 | struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
|
---|
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;
|
---|
60 |
|
---|
61 | *out = data_blob(NULL, 0);
|
---|
62 |
|
---|
63 | switch (gensec_security->gensec_role) {
|
---|
64 | case GENSEC_CLIENT:
|
---|
65 | if (state->state != SCHANNEL_STATE_START) {
|
---|
66 | /* we could parse the bind ack, but we don't know what it is yet */
|
---|
67 | return NT_STATUS_OK;
|
---|
68 | }
|
---|
69 |
|
---|
70 | state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
|
---|
71 |
|
---|
72 | bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
|
---|
73 | #if 0
|
---|
74 | /* to support this we'd need to have access to the full domain name */
|
---|
75 | /* 0x17, 23 */
|
---|
76 | bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
|
---|
77 | NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
|
---|
78 | NL_FLAG_UTF8_DNS_DOMAIN_NAME |
|
---|
79 | NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
|
---|
80 | bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
|
---|
81 | bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
|
---|
82 | bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
|
---|
83 | /* w2k3 refuses us if we use the full DNS workstation?
|
---|
84 | why? perhaps because we don't fill in the dNSHostName
|
---|
85 | attribute in the machine account? */
|
---|
86 | bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
|
---|
87 | #else
|
---|
88 | bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
|
---|
89 | NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
|
---|
90 | bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
|
---|
91 | bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
|
---|
95 | (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
|
---|
96 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
97 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
98 | DEBUG(3, ("Could not create schannel bind: %s\n",
|
---|
99 | nt_errstr(status)));
|
---|
100 | return status;
|
---|
101 | }
|
---|
102 |
|
---|
103 | state->state = SCHANNEL_STATE_UPDATE_1;
|
---|
104 |
|
---|
105 | return NT_STATUS_MORE_PROCESSING_REQUIRED;
|
---|
106 | case GENSEC_SERVER:
|
---|
107 |
|
---|
108 | if (state->state != SCHANNEL_STATE_START) {
|
---|
109 | /* no third leg on this protocol */
|
---|
110 | return NT_STATUS_INVALID_PARAMETER;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /* parse the schannel startup blob */
|
---|
114 | ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
|
---|
115 | (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
|
---|
116 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
117 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
118 | DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
|
---|
119 | nt_errstr(status)));
|
---|
120 | return status;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
|
---|
124 | domain = bind_schannel.oem_netbios_domain.a;
|
---|
125 | if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
|
---|
126 | DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
|
---|
127 | domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
|
---|
128 | return NT_STATUS_LOGON_FAILURE;
|
---|
129 | }
|
---|
130 | } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
|
---|
131 | domain = bind_schannel.utf8_dns_domain.u;
|
---|
132 | if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
|
---|
133 | DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
|
---|
134 | domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
|
---|
135 | return NT_STATUS_LOGON_FAILURE;
|
---|
136 | }
|
---|
137 | } else {
|
---|
138 | DEBUG(3, ("Request for schannel to without domain\n"));
|
---|
139 | return NT_STATUS_LOGON_FAILURE;
|
---|
140 | }
|
---|
141 |
|
---|
142 | if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
|
---|
143 | workstation = bind_schannel.oem_netbios_computer.a;
|
---|
144 | } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
|
---|
145 | workstation = bind_schannel.utf8_netbios_computer.u;
|
---|
146 | } else {
|
---|
147 | DEBUG(3, ("Request for schannel to without netbios workstation\n"));
|
---|
148 | return NT_STATUS_LOGON_FAILURE;
|
---|
149 | }
|
---|
150 |
|
---|
151 | status = schannel_get_creds_state(out_mem_ctx,
|
---|
152 | lpcfg_private_dir(gensec_security->settings->lp_ctx),
|
---|
153 | workstation, &creds);
|
---|
154 | if (!NT_STATUS_IS_OK(status)) {
|
---|
155 | DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
|
---|
156 | workstation, nt_errstr(status)));
|
---|
157 | if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
|
---|
158 | return NT_STATUS_LOGON_FAILURE;
|
---|
159 | }
|
---|
160 | return status;
|
---|
161 | }
|
---|
162 |
|
---|
163 | state->creds = talloc_steal(state, creds);
|
---|
164 |
|
---|
165 | bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
|
---|
166 | bind_schannel_ack.Flags = 0;
|
---|
167 | bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
|
---|
168 | * this does not have
|
---|
169 | * any meaning here
|
---|
170 | * - gd */
|
---|
171 |
|
---|
172 | ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
|
---|
173 | (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
|
---|
174 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
175 | status = ndr_map_error2ntstatus(ndr_err);
|
---|
176 | DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
|
---|
177 | workstation, nt_errstr(status)));
|
---|
178 | return status;
|
---|
179 | }
|
---|
180 |
|
---|
181 | state->state = SCHANNEL_STATE_UPDATE_1;
|
---|
182 |
|
---|
183 | return NT_STATUS_OK;
|
---|
184 | }
|
---|
185 | return NT_STATUS_INVALID_PARAMETER;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Return the struct netlogon_creds_CredentialState.
|
---|
190 | *
|
---|
191 | * Make sure not to call this unless gensec is using schannel...
|
---|
192 | */
|
---|
193 |
|
---|
194 | /* TODO: make this non-public */
|
---|
195 |
|
---|
196 | _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
|
---|
197 | TALLOC_CTX *mem_ctx,
|
---|
198 | struct netlogon_creds_CredentialState **creds)
|
---|
199 | {
|
---|
200 | struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
|
---|
201 |
|
---|
202 | *creds = talloc_reference(mem_ctx, state->creds);
|
---|
203 | if (!*creds) {
|
---|
204 | return NT_STATUS_NO_MEMORY;
|
---|
205 | }
|
---|
206 | return NT_STATUS_OK;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Returns anonymous credentials for schannel, matching Win2k3.
|
---|
212 | *
|
---|
213 | */
|
---|
214 |
|
---|
215 | static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
|
---|
216 | struct auth_session_info **_session_info)
|
---|
217 | {
|
---|
218 | struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
|
---|
219 | return auth_anonymous_session_info(state, gensec_security->settings->lp_ctx, _session_info);
|
---|
220 | }
|
---|
221 |
|
---|
222 | static NTSTATUS schannel_start(struct gensec_security *gensec_security)
|
---|
223 | {
|
---|
224 | struct schannel_state *state;
|
---|
225 |
|
---|
226 | state = talloc(gensec_security, struct schannel_state);
|
---|
227 | if (!state) {
|
---|
228 | return NT_STATUS_NO_MEMORY;
|
---|
229 | }
|
---|
230 |
|
---|
231 | state->state = SCHANNEL_STATE_START;
|
---|
232 | state->seq_num = 0;
|
---|
233 | gensec_security->private_data = state;
|
---|
234 |
|
---|
235 | return NT_STATUS_OK;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
|
---|
239 | {
|
---|
240 | NTSTATUS status;
|
---|
241 | struct schannel_state *state;
|
---|
242 |
|
---|
243 | status = schannel_start(gensec_security);
|
---|
244 | if (!NT_STATUS_IS_OK(status)) {
|
---|
245 | return status;
|
---|
246 | }
|
---|
247 |
|
---|
248 | state = (struct schannel_state *)gensec_security->private_data;
|
---|
249 | state->initiator = false;
|
---|
250 |
|
---|
251 | return NT_STATUS_OK;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
|
---|
255 | {
|
---|
256 | NTSTATUS status;
|
---|
257 | struct schannel_state *state;
|
---|
258 |
|
---|
259 | status = schannel_start(gensec_security);
|
---|
260 | if (!NT_STATUS_IS_OK(status)) {
|
---|
261 | return status;
|
---|
262 | }
|
---|
263 |
|
---|
264 | state = (struct schannel_state *)gensec_security->private_data;
|
---|
265 | state->initiator = true;
|
---|
266 |
|
---|
267 | return NT_STATUS_OK;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | static bool schannel_have_feature(struct gensec_security *gensec_security,
|
---|
272 | uint32_t feature)
|
---|
273 | {
|
---|
274 | if (feature & (GENSEC_FEATURE_SIGN |
|
---|
275 | GENSEC_FEATURE_SEAL)) {
|
---|
276 | return true;
|
---|
277 | }
|
---|
278 | if (feature & GENSEC_FEATURE_DCE_STYLE) {
|
---|
279 | return true;
|
---|
280 | }
|
---|
281 | if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
|
---|
282 | return true;
|
---|
283 | }
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /*
|
---|
288 | unseal a packet
|
---|
289 | */
|
---|
290 | static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
|
---|
291 | TALLOC_CTX *mem_ctx,
|
---|
292 | uint8_t *data, size_t length,
|
---|
293 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
294 | const DATA_BLOB *sig)
|
---|
295 | {
|
---|
296 | struct schannel_state *state =
|
---|
297 | talloc_get_type(gensec_security->private_data,
|
---|
298 | struct schannel_state);
|
---|
299 |
|
---|
300 | return netsec_incoming_packet(state, mem_ctx, true,
|
---|
301 | discard_const_p(uint8_t, data),
|
---|
302 | length, sig);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /*
|
---|
306 | check the signature on a packet
|
---|
307 | */
|
---|
308 | static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
|
---|
309 | TALLOC_CTX *mem_ctx,
|
---|
310 | const uint8_t *data, size_t length,
|
---|
311 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
312 | const DATA_BLOB *sig)
|
---|
313 | {
|
---|
314 | struct schannel_state *state =
|
---|
315 | talloc_get_type(gensec_security->private_data,
|
---|
316 | struct schannel_state);
|
---|
317 |
|
---|
318 | return netsec_incoming_packet(state, mem_ctx, false,
|
---|
319 | discard_const_p(uint8_t, data),
|
---|
320 | length, sig);
|
---|
321 | }
|
---|
322 | /*
|
---|
323 | seal a packet
|
---|
324 | */
|
---|
325 | static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
|
---|
326 | TALLOC_CTX *mem_ctx,
|
---|
327 | uint8_t *data, size_t length,
|
---|
328 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
329 | DATA_BLOB *sig)
|
---|
330 | {
|
---|
331 | struct schannel_state *state =
|
---|
332 | talloc_get_type(gensec_security->private_data,
|
---|
333 | struct schannel_state);
|
---|
334 |
|
---|
335 | return netsec_outgoing_packet(state, mem_ctx, true,
|
---|
336 | data, length, sig);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /*
|
---|
340 | sign a packet
|
---|
341 | */
|
---|
342 | static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
|
---|
343 | TALLOC_CTX *mem_ctx,
|
---|
344 | const uint8_t *data, size_t length,
|
---|
345 | const uint8_t *whole_pdu, size_t pdu_length,
|
---|
346 | DATA_BLOB *sig)
|
---|
347 | {
|
---|
348 | struct schannel_state *state =
|
---|
349 | talloc_get_type(gensec_security->private_data,
|
---|
350 | struct schannel_state);
|
---|
351 |
|
---|
352 | return netsec_outgoing_packet(state, mem_ctx, false,
|
---|
353 | discard_const_p(uint8_t, data),
|
---|
354 | length, sig);
|
---|
355 | }
|
---|
356 |
|
---|
357 | static const struct gensec_security_ops gensec_schannel_security_ops = {
|
---|
358 | .name = "schannel",
|
---|
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,
|
---|
371 | .enabled = true,
|
---|
372 | .priority = GENSEC_SCHANNEL
|
---|
373 | };
|
---|
374 |
|
---|
375 | _PUBLIC_ NTSTATUS gensec_schannel_init(void)
|
---|
376 | {
|
---|
377 | NTSTATUS ret;
|
---|
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));
|
---|
382 | return ret;
|
---|
383 | }
|
---|
384 |
|
---|
385 | return ret;
|
---|
386 | }
|
---|