libcli/auth: Provide a struct loadparm_context to schannel calls
[rusty/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 #include "auth/gensec/gensec_toplevel_proto.h"
34
35 _PUBLIC_ NTSTATUS gensec_schannel_init(void);
36
37 static size_t schannel_sig_size(struct gensec_security *gensec_security, size_t data_size)
38 {
39         struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
40         uint32_t sig_size;
41
42         sig_size = netsec_outgoing_sig_size(state);
43
44         return sig_size;
45 }
46
47 static NTSTATUS schannel_session_key(struct gensec_security *gensec_security,
48                                      TALLOC_CTX *mem_ctx,
49                                      DATA_BLOB *session_key)
50 {
51         return NT_STATUS_NOT_IMPLEMENTED;
52 }
53
54 static NTSTATUS schannel_update(struct gensec_security *gensec_security, TALLOC_CTX *out_mem_ctx,
55                                        const DATA_BLOB in, DATA_BLOB *out)
56 {
57         struct schannel_state *state = (struct schannel_state *)gensec_security->private_data;
58         NTSTATUS status;
59         enum ndr_err_code ndr_err;
60         struct NL_AUTH_MESSAGE bind_schannel;
61         struct NL_AUTH_MESSAGE bind_schannel_ack;
62         struct netlogon_creds_CredentialState *creds;
63         const char *workstation;
64         const char *domain;
65
66         *out = data_blob(NULL, 0);
67
68         switch (gensec_security->gensec_role) {
69         case GENSEC_CLIENT:
70                 if (state->state != SCHANNEL_STATE_START) {
71                         /* we could parse the bind ack, but we don't know what it is yet */
72                         return NT_STATUS_OK;
73                 }
74
75                 state->creds = talloc_reference(state, cli_credentials_get_netlogon_creds(gensec_security->credentials));
76
77                 bind_schannel.MessageType = NL_NEGOTIATE_REQUEST;
78 #if 0
79                 /* to support this we'd need to have access to the full domain name */
80                 /* 0x17, 23 */
81                 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
82                                       NL_FLAG_OEM_NETBIOS_COMPUTER_NAME |
83                                       NL_FLAG_UTF8_DNS_DOMAIN_NAME |
84                                       NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME;
85                 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
86                 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
87                 bind_schannel.utf8_dns_domain = cli_credentials_get_realm(gensec_security->credentials);
88                 /* w2k3 refuses us if we use the full DNS workstation?
89                  why? perhaps because we don't fill in the dNSHostName
90                  attribute in the machine account? */
91                 bind_schannel.utf8_netbios_computer = cli_credentials_get_workstation(gensec_security->credentials);
92 #else
93                 bind_schannel.Flags = NL_FLAG_OEM_NETBIOS_DOMAIN_NAME |
94                                       NL_FLAG_OEM_NETBIOS_COMPUTER_NAME;
95                 bind_schannel.oem_netbios_domain.a = cli_credentials_get_domain(gensec_security->credentials);
96                 bind_schannel.oem_netbios_computer.a = cli_credentials_get_workstation(gensec_security->credentials);
97 #endif
98
99                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel,
100                                                (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
101                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
102                         status = ndr_map_error2ntstatus(ndr_err);
103                         DEBUG(3, ("Could not create schannel bind: %s\n",
104                                   nt_errstr(status)));
105                         return status;
106                 }
107
108                 state->state = SCHANNEL_STATE_UPDATE_1;
109
110                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
111         case GENSEC_SERVER:
112
113                 if (state->state != SCHANNEL_STATE_START) {
114                         /* no third leg on this protocol */
115                         return NT_STATUS_INVALID_PARAMETER;
116                 }
117
118                 /* parse the schannel startup blob */
119                 ndr_err = ndr_pull_struct_blob(&in, out_mem_ctx, &bind_schannel,
120                         (ndr_pull_flags_fn_t)ndr_pull_NL_AUTH_MESSAGE);
121                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
122                         status = ndr_map_error2ntstatus(ndr_err);
123                         DEBUG(3, ("Could not parse incoming schannel bind: %s\n",
124                                   nt_errstr(status)));
125                         return status;
126                 }
127
128                 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_DOMAIN_NAME) {
129                         domain = bind_schannel.oem_netbios_domain.a;
130                         if (strcasecmp_m(domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)) != 0) {
131                                 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
132                                           domain, lpcfg_workgroup(gensec_security->settings->lp_ctx)));
133                                 return NT_STATUS_LOGON_FAILURE;
134                         }
135                 } else if (bind_schannel.Flags & NL_FLAG_UTF8_DNS_DOMAIN_NAME) {
136                         domain = bind_schannel.utf8_dns_domain.u;
137                         if (strcasecmp_m(domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)) != 0) {
138                                 DEBUG(3, ("Request for schannel to incorrect domain: %s != our domain %s\n",
139                                           domain, lpcfg_dnsdomain(gensec_security->settings->lp_ctx)));
140                                 return NT_STATUS_LOGON_FAILURE;
141                         }
142                 } else {
143                         DEBUG(3, ("Request for schannel to without domain\n"));
144                         return NT_STATUS_LOGON_FAILURE;
145                 }
146
147                 if (bind_schannel.Flags & NL_FLAG_OEM_NETBIOS_COMPUTER_NAME) {
148                         workstation = bind_schannel.oem_netbios_computer.a;
149                 } else if (bind_schannel.Flags & NL_FLAG_UTF8_NETBIOS_COMPUTER_NAME) {
150                         workstation = bind_schannel.utf8_netbios_computer.u;
151                 } else {
152                         DEBUG(3, ("Request for schannel to without netbios workstation\n"));
153                         return NT_STATUS_LOGON_FAILURE;
154                 }
155
156                 status = schannel_get_creds_state(out_mem_ctx,
157                                                   gensec_security->settings->lp_ctx,
158                                                   workstation, &creds);
159                 if (!NT_STATUS_IS_OK(status)) {
160                         DEBUG(3, ("Could not find session key for attempted schannel connection from %s: %s\n",
161                                   workstation, nt_errstr(status)));
162                         if (NT_STATUS_EQUAL(status, NT_STATUS_INVALID_HANDLE)) {
163                                 return NT_STATUS_LOGON_FAILURE;
164                         }
165                         return status;
166                 }
167
168                 state->creds = talloc_steal(state, creds);
169
170                 bind_schannel_ack.MessageType = NL_NEGOTIATE_RESPONSE;
171                 bind_schannel_ack.Flags = 0;
172                 bind_schannel_ack.Buffer.dummy = 0x6c0000; /* actually I think
173                                                             * this does not have
174                                                             * any meaning here
175                                                             * - gd */
176
177                 ndr_err = ndr_push_struct_blob(out, out_mem_ctx, &bind_schannel_ack,
178                                                (ndr_push_flags_fn_t)ndr_push_NL_AUTH_MESSAGE);
179                 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180                         status = ndr_map_error2ntstatus(ndr_err);
181                         DEBUG(3, ("Could not return schannel bind ack for client %s: %s\n",
182                                   workstation, nt_errstr(status)));
183                         return status;
184                 }
185
186                 state->state = SCHANNEL_STATE_UPDATE_1;
187
188                 return NT_STATUS_OK;
189         }
190         return NT_STATUS_INVALID_PARAMETER;
191 }
192
193 /**
194  * Return the struct netlogon_creds_CredentialState.
195  *
196  * Make sure not to call this unless gensec is using schannel...
197  */
198
199 /* TODO: make this non-public */
200
201 _PUBLIC_ NTSTATUS dcerpc_schannel_creds(struct gensec_security *gensec_security,
202                                         TALLOC_CTX *mem_ctx,
203                                         struct netlogon_creds_CredentialState **creds)
204 {
205         struct schannel_state *state = talloc_get_type(gensec_security->private_data, struct schannel_state);
206
207         *creds = talloc_reference(mem_ctx, state->creds);
208         if (!*creds) {
209                 return NT_STATUS_NO_MEMORY;
210         }
211         return NT_STATUS_OK;
212 }
213
214
215 /**
216  * Returns anonymous credentials for schannel, matching Win2k3.
217  *
218  */
219
220 static NTSTATUS schannel_session_info(struct gensec_security *gensec_security,
221                                       TALLOC_CTX *mem_ctx,
222                                       struct auth_session_info **_session_info)
223 {
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 }