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