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