s4-ldbwrap: added re-use of ldb contexts in ldb_wrap_connect()
[sfrench/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 "auth/gensec/schannel_state.h"
31 #include "librpc/rpc/dcerpc.h"
32 #include "param/param.h"
33
34 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
35 {
36         return 32;
37 }
38
39 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
40                                             DATA_BLOB *session_key)
41 {
42         return NT_STATUS_NOT_IMPLEMENTED;
43 }
44
45 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
46                                        const DATA_BLOB in, DATA_BLOB *out)
47 {
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;
58
59         *out = data_blob(NULL, 0);
60
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;
66                 }
67
68                 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
69
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
91
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;
100                 }
101
102                 state->state = SCHANNEL_STATE_UPDATE_1;
103
104                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
105         case GENSEC_SERVER:
106
107                 required_flags = NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
108                                  NL_FLAG_OEM_NETBIOS_DOMAIN_NAME;
109
110                 if (state->state != SCHANNEL_STATE_START) {
111                         /* no third leg on this protocol */
112                         return NT_STATUS_INVALID_PARAMETER;
113                 }
114
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;
125                 }
126
127                 if (!(required_flags == (bind_schannel.Flags & required_flags))) {
128                         return NT_STATUS_INVALID_PARAMETER;
129                 }
130
131                 workstation = bind_schannel.oem_netbios_computer.a;
132                 domain = bind_schannel.oem_netbios_domain.a;
133
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)));
137
138                         return NT_STATUS_LOGON_FAILURE;
139                 }
140
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;
145                 }
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;
155                         }
156                         return status;
157                 }
158
159                 state->creds = talloc_reference(state, creds);
160
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 */
167
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;
176                 }
177
178                 state->state = SCHANNEL_STATE_UPDATE_1;
179
180                 return NT_STATUS_OK;
181         }
182         return NT_STATUS_INVALID_PARAMETER;
183 }
184
185 /**
186  * Return the struct netlogon_creds_CredentialState.
187  *
188  * Make sure not to call this unless gensec is using schannel...
189  */
190
191 /* TODO: make this non-public */
192
193 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
194                                         TALLOC_CTX *mem_ctx,
195                                         struct netlogon_creds_CredentialState **creds)
196 {
197         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
198
199         *creds = talloc_reference(mem_ctx, state->creds);
200         if (!*creds) {
201                 return NT_STATUS_NO_MEMORY;
202         }
203         return NT_STATUS_OK;
204 }
205
206
207 /**
208  * Returns anonymous credentials for schannel, matching Win2k3.
209  *
210  */
211
212 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
213                                          struct auth_session_info **_session_info)
214 {
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);
217 }
218
219 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
220 {
221         struct schannel_state *state;
222
223         state = talloc(gensec_security, struct schannel_state);
224         if (!state) {
225                 return NT_STATUS_NO_MEMORY;
226         }
227
228         state->state = SCHANNEL_STATE_START;
229         state->seq_num = 0;
230         gensec_security->private_data = state;
231
232         return NT_STATUS_OK;
233 }
234
235 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
236 {
237         NTSTATUS status;
238         struct schannel_state *state;
239
240         status = schannel_start(gensec_security);
241         if (!NT_STATUS_IS_OK(status)) {
242                 return status;
243         }
244
245         state = (struct schannel_state *)gensec_security->private_data;
246         state->initiator = false;
247
248         return NT_STATUS_OK;
249 }
250
251 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
252 {
253         NTSTATUS status;
254         struct schannel_state *state;
255
256         status = schannel_start(gensec_security);
257         if (!NT_STATUS_IS_OK(status)) {
258                 return status;
259         }
260
261         state = (struct schannel_state *)gensec_security->private_data;
262         state->initiator = true;
263
264         return NT_STATUS_OK;
265 }
266
267
268 static bool schannel_have_feature(struct gensec_security *gensec_security,
269                                          uint32_t feature)
270 {
271         if (feature & (GENSEC_FEATURE_SIGN |
272                        GENSEC_FEATURE_SEAL)) {
273                 return true;
274         }
275         if (feature & GENSEC_FEATURE_DCE_STYLE) {
276                 return true;
277         }
278         if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
279                 return true;
280         }
281         return false;
282 }
283
284 /*
285   unseal a packet
286 */
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)
292 {
293         struct schannel_state *state =
294                 talloc_get_type(gensec_security->private_data,
295                                 struct schannel_state);
296
297         return netsec_incoming_packet(state, mem_ctx, true,
298                                       discard_const_p(uint8_t, data),
299                                       length, sig);
300 }
301
302 /*
303   check the signature on a packet
304 */
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)
310 {
311         struct schannel_state *state =
312                 talloc_get_type(gensec_security->private_data,
313                                 struct schannel_state);
314
315         return netsec_incoming_packet(state, mem_ctx, false,
316                                       discard_const_p(uint8_t, data),
317                                       length, sig);
318 }
319 /*
320   seal a packet
321 */
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)
327 {
328         struct schannel_state *state =
329                 talloc_get_type(gensec_security->private_data,
330                                 struct schannel_state);
331
332         return netsec_outgoing_packet(state, mem_ctx, true,
333                                       data, length, sig);
334 }
335
336 /*
337   sign a packet
338 */
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)
344 {
345         struct schannel_state *state =
346                 talloc_get_type(gensec_security->private_data,
347                                 struct schannel_state);
348
349         return netsec_outgoing_packet(state, mem_ctx, false,
350                                       discard_const_p(uint8_t, data),
351                                       length, sig);
352 }
353
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
370 };
371
372 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
373 {
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;
380         }
381
382         return ret;
383 }