90b90fca9c8c768c7a113655fd6ddf2bfb238209
[ira/wip.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 "auth/gensec/schannel.h"
30 #include "librpc/rpc/dcerpc.h"
31 #include "param/param.h"
32 #include "auth/session_proto.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_free(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 static NTSTATUS schannel_seal_packet_wrap(struct gensec_security *gensec_security,
285                                           TALLOC_CTX *mem_ctx,
286                                           uint8_t *data, size_t length,
287                                           const uint8_t *whole_pdu, size_t pdu_length,
288                                           DATA_BLOB *sig)
289 {
290         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
291
292         return schannel_seal_packet(state, mem_ctx, data, length, sig);
293 }
294
295 static NTSTATUS schannel_sign_packet_wrap(struct gensec_security *gensec_security,
296                                           TALLOC_CTX *mem_ctx,
297                                           const uint8_t *data, size_t length,
298                                           const uint8_t *whole_pdu, size_t pdu_length,
299                                           DATA_BLOB *sig)
300 {
301         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
302
303         return schannel_sign_packet(state, mem_ctx, data, length, sig);
304 }
305
306 static NTSTATUS schannel_check_packet_wrap(struct gensec_security *gensec_security,
307                                            TALLOC_CTX *mem_ctx,
308                                            const uint8_t *data, size_t length,
309                                            const uint8_t *whole_pdu, size_t pdu_length,
310                                            const DATA_BLOB *sig)
311 {
312         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
313
314         return schannel_check_packet(state, mem_ctx, data, length, sig);
315 }
316
317 static NTSTATUS schannel_unseal_packet_wrap(struct gensec_security *gensec_security,
318                                             TALLOC_CTX *mem_ctx,
319                                             uint8_t *data, size_t length,
320                                             const uint8_t *whole_pdu, size_t pdu_length,
321                                             const DATA_BLOB *sig)
322 {
323         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
324
325         return schannel_unseal_packet(state, mem_ctx, data, length, sig);
326 }
327
328 static const struct gensec_security_ops gensec_schannel_security_ops = {
329         .name           = "schannel",
330         .auth_type      = DCERPC_AUTH_TYPE_SCHANNEL,
331         .client_start   = schannel_client_start,
332         .server_start   = schannel_server_start,
333         .update         = schannel_update,
334         .seal_packet    = schannel_seal_packet_wrap,
335         .sign_packet    = schannel_sign_packet_wrap,
336         .check_packet   = schannel_check_packet_wrap,
337         .unseal_packet  = schannel_unseal_packet_wrap,
338         .session_key    = schannel_session_key,
339         .session_info   = schannel_session_info,
340         .sig_size       = schannel_sig_size,
341         .have_feature   = schannel_have_feature,
342         .enabled        = true,
343         .priority       = GENSEC_SCHANNEL
344 };
345
346 _PUBLIC_ NTSTATUS gensec_schannel_init(void)
347 {
348         NTSTATUS ret;
349         ret = gensec_register(&gensec_schannel_security_ops);
350         if (!NT_STATUS_IS_OK(ret)) {
351                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
352                         gensec_schannel_security_ops.name));
353                 return ret;
354         }
355
356         return ret;
357 }