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