auth: move credentials layer to the top level
[samba.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         return auth_anonymous_session_info(mem_ctx, gensec_security->settings->lp_ctx, _session_info);
224 }
225
226 static NTSTATUS schannel_start(struct gensec_security *gensec_security)
227 {
228         struct schannel_state *state;
229
230         state = talloc(gensec_security, struct schannel_state);
231         if (!state) {
232                 return NT_STATUS_NO_MEMORY;
233         }
234
235         state->state = SCHANNEL_STATE_START;
236         state->seq_num = 0;
237         gensec_security->private_data = state;
238
239         return NT_STATUS_OK;
240 }
241
242 static NTSTATUS schannel_server_start(struct gensec_security *gensec_security)
243 {
244         NTSTATUS status;
245         struct schannel_state *state;
246
247         status = schannel_start(gensec_security);
248         if (!NT_STATUS_IS_OK(status)) {
249                 return status;
250         }
251
252         state = (struct schannel_state *)gensec_security->private_data;
253         state->initiator = false;
254
255         return NT_STATUS_OK;
256 }
257
258 static NTSTATUS schannel_client_start(struct gensec_security *gensec_security)
259 {
260         NTSTATUS status;
261         struct schannel_state *state;
262
263         status = schannel_start(gensec_security);
264         if (!NT_STATUS_IS_OK(status)) {
265                 return status;
266         }
267
268         state = (struct schannel_state *)gensec_security->private_data;
269         state->initiator = true;
270
271         return NT_STATUS_OK;
272 }
273
274
275 static bool schannel_have_feature(struct gensec_security *gensec_security,
276                                          uint32_t feature)
277 {
278         if (feature & (GENSEC_FEATURE_SIGN |
279                        GENSEC_FEATURE_SEAL)) {
280                 return true;
281         }
282         if (feature & GENSEC_FEATURE_DCE_STYLE) {
283                 return true;
284         }
285         if (feature & GENSEC_FEATURE_ASYNC_REPLIES) {
286                 return true;
287         }
288         return false;
289 }
290
291 /*
292   unseal a packet
293 */
294 static NTSTATUS schannel_unseal_packet(struct gensec_security *gensec_security,
295                                        uint8_t *data, size_t length,
296                                        const uint8_t *whole_pdu, size_t pdu_length,
297                                        const DATA_BLOB *sig)
298 {
299         struct schannel_state *state =
300                 talloc_get_type(gensec_security->private_data,
301                                 struct schannel_state);
302
303         return netsec_incoming_packet(state, true,
304                                       discard_const_p(uint8_t, data),
305                                       length, sig);
306 }
307
308 /*
309   check the signature on a packet
310 */
311 static NTSTATUS schannel_check_packet(struct gensec_security *gensec_security,
312                                       const uint8_t *data, size_t length,
313                                       const uint8_t *whole_pdu, size_t pdu_length,
314                                       const DATA_BLOB *sig)
315 {
316         struct schannel_state *state =
317                 talloc_get_type(gensec_security->private_data,
318                                 struct schannel_state);
319
320         return netsec_incoming_packet(state, false,
321                                       discard_const_p(uint8_t, data),
322                                       length, sig);
323 }
324 /*
325   seal a packet
326 */
327 static NTSTATUS schannel_seal_packet(struct gensec_security *gensec_security,
328                                      TALLOC_CTX *mem_ctx,
329                                      uint8_t *data, size_t length,
330                                      const uint8_t *whole_pdu, size_t pdu_length,
331                                      DATA_BLOB *sig)
332 {
333         struct schannel_state *state =
334                 talloc_get_type(gensec_security->private_data,
335                                 struct schannel_state);
336
337         return netsec_outgoing_packet(state, mem_ctx, true,
338                                       data, length, sig);
339 }
340
341 /*
342   sign a packet
343 */
344 static NTSTATUS schannel_sign_packet(struct gensec_security *gensec_security,
345                                      TALLOC_CTX *mem_ctx,
346                                      const uint8_t *data, size_t length,
347                                      const uint8_t *whole_pdu, size_t pdu_length,
348                                      DATA_BLOB *sig)
349 {
350         struct schannel_state *state =
351                 talloc_get_type(gensec_security->private_data,
352                                 struct schannel_state);
353
354         return netsec_outgoing_packet(state, mem_ctx, false,
355                                       discard_const_p(uint8_t, data),
356                                       length, sig);
357 }
358
359 static const struct gensec_security_ops gensec_schannel_security_ops = {
360         .name           = "schannel",
361         .auth_type      = DCERPC_AUTH_TYPE_SCHANNEL,
362         .client_start   = schannel_client_start,
363         .server_start   = schannel_server_start,
364         .update         = schannel_update,
365         .seal_packet    = schannel_seal_packet,
366         .sign_packet    = schannel_sign_packet,
367         .check_packet   = schannel_check_packet,
368         .unseal_packet  = schannel_unseal_packet,
369         .session_key    = schannel_session_key,
370         .session_info   = schannel_session_info,
371         .sig_size       = schannel_sig_size,
372         .have_feature   = schannel_have_feature,
373         .enabled        = true,
374         .priority       = GENSEC_SCHANNEL
375 };
376
377 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
378 {
379         NTSTATUS ret;
380         ret = gensec_register(&gensec_schannel_security_ops);
381         if (!NT_STATUS_IS_OK(ret)) {
382                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
383                         gensec_schannel_security_ops.name));
384                 return ret;
385         }
386
387         return ret;
388 }