libcli/smb_composite: for spnego session setups check the smb signature manually
[metze/samba/wip.git] / source / libcli / smb_composite / sesssetup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20   a composite API for making handling a generic async session setup
21 */
22
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/composite/composite.h"
27 #include "libcli/smb_composite/smb_composite.h"
28 #include "libcli/smb_composite/proto.h"
29 #include "libcli/auth/libcli_auth.h"
30 #include "auth/auth.h"
31 #include "auth/gensec/gensec.h"
32 #include "auth/credentials/credentials.h"
33 #include "version.h"
34 #include "param/param.h"
35
36 struct sesssetup_state {
37         union smb_sesssetup setup;
38         NTSTATUS gensec_status;
39         struct smb_composite_sesssetup *io;
40         struct smbcli_request *req;
41 };
42
43 static int sesssetup_state_destructor(struct sesssetup_state *state)
44 {
45         if (state->req) {
46                 talloc_free(state->req);
47                 state->req = NULL;
48         }
49
50         return 0;
51 }
52
53 static NTSTATUS session_setup_old(struct composite_context *c,
54                                   struct smbcli_session *session, 
55                                   struct smb_composite_sesssetup *io,
56                                   struct smbcli_request **req); 
57 static NTSTATUS session_setup_nt1(struct composite_context *c,
58                                   struct smbcli_session *session, 
59                                   struct smb_composite_sesssetup *io,
60                                   struct smbcli_request **req); 
61 static NTSTATUS session_setup_spnego(struct composite_context *c,
62                                      struct smbcli_session *session, 
63                                      struct smb_composite_sesssetup *io,
64                                      struct smbcli_request **req);
65
66 /*
67   store the user session key for a transport
68 */
69 static void set_user_session_key(struct smbcli_session *session,
70                                  const DATA_BLOB *session_key)
71 {
72         session->user_session_key = data_blob_talloc(session, 
73                                                      session_key->data, 
74                                                      session_key->length);
75 }
76
77 /*
78   handler for completion of a smbcli_request sub-request
79 */
80 static void request_handler(struct smbcli_request *req)
81 {
82         struct composite_context *c = (struct composite_context *)req->async.private;
83         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
84         struct smbcli_session *session = req->session;
85         DATA_BLOB session_key = data_blob(NULL, 0);
86         DATA_BLOB null_data_blob = data_blob(NULL, 0);
87         NTSTATUS session_key_err, nt_status;
88         struct smbcli_request *check_req = NULL;
89
90         if (req->sign_caller_checks) {
91                 req->do_not_free = true;
92                 check_req = req;
93         }
94
95         c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
96         state->req = NULL;
97
98         switch (state->setup.old.level) {
99         case RAW_SESSSETUP_OLD:
100                 state->io->out.vuid = state->setup.old.out.vuid;
101                 /* This doesn't work, as this only happens on old
102                  * protocols, where this comparison won't match. */
103                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
104                         /* we neet to reset the vuid for a new try */
105                         session->vuid = 0;
106                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
107                                 nt_status = session_setup_old(c, session, 
108                                                               state->io, 
109                                                               &state->req);
110                                 if (NT_STATUS_IS_OK(nt_status)) {
111                                         talloc_free(check_req);
112                                         c->status = nt_status;
113                                         composite_continue_smb(c, state->req, request_handler, c);
114                                         return;
115                                 }
116                         }
117                 }
118                 break;
119
120         case RAW_SESSSETUP_NT1:
121                 state->io->out.vuid = state->setup.nt1.out.vuid;
122                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
123                         /* we neet to reset the vuid for a new try */
124                         session->vuid = 0;
125                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
126                                 nt_status = session_setup_nt1(c, session, 
127                                                               state->io, 
128                                                               &state->req);
129                                 if (NT_STATUS_IS_OK(nt_status)) {
130                                         talloc_free(check_req);
131                                         c->status = nt_status;
132                                         composite_continue_smb(c, state->req, request_handler, c);
133                                         return;
134                                 }
135                         }
136                 }
137                 break;
138
139         case RAW_SESSSETUP_SPNEGO:
140                 state->io->out.vuid = state->setup.spnego.out.vuid;
141                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
142                         /* we need to reset the vuid for a new try */
143                         session->vuid = 0;
144                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
145                                 nt_status = session_setup_spnego(c, session, 
146                                                                       state->io, 
147                                                                       &state->req);
148                                 if (NT_STATUS_IS_OK(nt_status)) {
149                                         talloc_free(check_req);
150                                         c->status = nt_status;
151                                         composite_continue_smb(c, state->req, request_handler, c);
152                                         return;
153                                 }
154                         }
155                 }
156                 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
157                     !NT_STATUS_IS_OK(c->status)) {
158                         talloc_free(check_req);
159                         break;
160                 }
161                 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
162
163                         /* The status value here, from the earlier pass at GENSEC is
164                          * vital to the security of the system.  Even if the other end
165                          * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
166                          * you must keep feeding it blobs, or else the remote
167                          * host/attacker might avoid mutal authentication
168                          * requirements */
169                         
170                         state->gensec_status = gensec_update(session->gensec, state,
171                                                          state->setup.spnego.out.secblob,
172                                                          &state->setup.spnego.in.secblob);
173                         c->status = state->gensec_status;
174                         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
175                             !NT_STATUS_IS_OK(c->status)) {
176                                 talloc_free(check_req);
177                                 break;
178                         }
179                 } else {
180                         state->setup.spnego.in.secblob = data_blob(NULL, 0);
181                 }
182
183                 /* we need to do another round of session setup. We keep going until both sides
184                    are happy */
185                 session_key_err = gensec_session_key(session->gensec, &session_key);
186                 if (NT_STATUS_IS_OK(session_key_err)) {
187                         set_user_session_key(session, &session_key);
188                         smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
189                 }
190
191                 if (state->setup.spnego.in.secblob.length) {
192                         if (check_req) {
193                                 check_req->sign_caller_checks = false;
194                                 if (!smbcli_request_check_sign_mac(check_req)) {
195                                         talloc_free(check_req);
196                                         c->status = NT_STATUS_ACCESS_DENIED;
197                                         break;
198                                 }
199                                 talloc_free(check_req);
200                         }
201
202                         /* 
203                          * set the session->vuid value only for calling
204                          * smb_raw_sesssetup_send()
205                          */
206                         uint16_t vuid = session->vuid;
207                         session->vuid = state->io->out.vuid;
208                         state->req = smb_raw_sesssetup_send(session, &state->setup);
209                         session->vuid = vuid;
210                         composite_continue_smb(c, state->req, request_handler, c);
211                         return;
212                 }
213                 break;
214
215         case RAW_SESSSETUP_SMB2:
216                 c->status = NT_STATUS_INTERNAL_ERROR;
217                 break;
218         }
219
220         if (check_req) {
221                 check_req->sign_caller_checks = false;
222                 if (!smbcli_request_check_sign_mac(check_req)) {
223                         c->status = NT_STATUS_ACCESS_DENIED;
224                 }
225                 talloc_free(check_req);
226         }
227
228         /* enforce the local signing required flag */
229         if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
230                 if (!session->transport->negotiate.sign_info.doing_signing 
231                     && session->transport->negotiate.sign_info.mandatory_signing) {
232                         DEBUG(0, ("SMB signing required, but server does not support it\n"));
233                         c->status = NT_STATUS_ACCESS_DENIED;
234                 }
235         }
236
237         if (!NT_STATUS_IS_OK(c->status)) {
238                 composite_error(c, c->status);
239                 return;
240         }
241
242         composite_done(c);
243 }
244
245
246 /*
247   send a nt1 style session setup
248 */
249 static NTSTATUS session_setup_nt1(struct composite_context *c,
250                                   struct smbcli_session *session, 
251                                   struct smb_composite_sesssetup *io,
252                                   struct smbcli_request **req) 
253 {
254         NTSTATUS nt_status;
255         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
256         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
257         DATA_BLOB session_key;
258         int flags = CLI_CRED_NTLM_AUTH;
259         if (session->options.lanman_auth) {
260                 flags |= CLI_CRED_LANMAN_AUTH;
261         }
262
263         if (session->options.ntlmv2_auth) {
264                 flags |= CLI_CRED_NTLMv2_AUTH;
265         }
266
267         state->setup.nt1.level           = RAW_SESSSETUP_NT1;
268         state->setup.nt1.in.bufsize      = session->transport->options.max_xmit;
269         state->setup.nt1.in.mpx_max      = session->transport->options.max_mux;
270         state->setup.nt1.in.vc_num       = 1;
271         state->setup.nt1.in.sesskey      = io->in.sesskey;
272         state->setup.nt1.in.capabilities = io->in.capabilities;
273         state->setup.nt1.in.os           = "Unix";
274         state->setup.nt1.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
275
276         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
277                                                  &state->setup.nt1.in.user,
278                                                  &state->setup.nt1.in.domain);
279         
280
281         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
282                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
283                                                               &flags, 
284                                                               session->transport->negotiate.secblob, 
285                                                               names_blob,
286                                                               &state->setup.nt1.in.password1,
287                                                               &state->setup.nt1.in.password2,
288                                                               NULL, &session_key);
289                 NT_STATUS_NOT_OK_RETURN(nt_status);
290
291                 smbcli_transport_simple_set_signing(session->transport, session_key, 
292                                                     state->setup.nt1.in.password2);
293                 set_user_session_key(session, &session_key);
294                 
295                 data_blob_free(&session_key);
296         } else if (session->options.plaintext_auth) {
297                 const char *password = cli_credentials_get_password(io->in.credentials);
298                 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
299                 state->setup.nt1.in.password2 = data_blob(NULL, 0);
300         } else {
301                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
302                 return NT_STATUS_INVALID_PARAMETER;
303         }
304
305         *req = smb_raw_sesssetup_send(session, &state->setup);
306         if (!*req) {
307                 return NT_STATUS_NO_MEMORY;
308         }
309         return (*req)->status;
310 }
311
312
313 /*
314   old style session setup (pre NT1 protocol level)
315 */
316 static NTSTATUS session_setup_old(struct composite_context *c,
317                                   struct smbcli_session *session, 
318                                   struct smb_composite_sesssetup *io,
319                                   struct smbcli_request **req) 
320 {
321         NTSTATUS nt_status;
322         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
323         const char *password = cli_credentials_get_password(io->in.credentials);
324         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
325         DATA_BLOB session_key;
326         int flags = 0;
327         if (session->options.lanman_auth) {
328                 flags |= CLI_CRED_LANMAN_AUTH;
329         }
330
331         if (session->options.ntlmv2_auth) {
332                 flags |= CLI_CRED_NTLMv2_AUTH;
333         }
334
335         state->setup.old.level      = RAW_SESSSETUP_OLD;
336         state->setup.old.in.bufsize = session->transport->options.max_xmit;
337         state->setup.old.in.mpx_max = session->transport->options.max_mux;
338         state->setup.old.in.vc_num  = 1;
339         state->setup.old.in.sesskey = io->in.sesskey;
340         state->setup.old.in.os      = "Unix";
341         state->setup.old.in.lanman  = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
342         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
343                                                  &state->setup.old.in.user,
344                                                  &state->setup.old.in.domain);
345         
346         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
347                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
348                                                               &flags, 
349                                                               session->transport->negotiate.secblob, 
350                                                               names_blob,
351                                                               &state->setup.old.in.password,
352                                                               NULL,
353                                                               NULL, &session_key);
354                 NT_STATUS_NOT_OK_RETURN(nt_status);
355                 set_user_session_key(session, &session_key);
356                 
357                 data_blob_free(&session_key);
358         } else if (session->options.plaintext_auth) {
359                 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
360         } else {
361                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
362                 return NT_STATUS_INVALID_PARAMETER;
363         }
364         
365         *req = smb_raw_sesssetup_send(session, &state->setup);
366         if (!*req) {
367                 return NT_STATUS_NO_MEMORY;
368         }
369         return (*req)->status;
370 }
371
372
373 /*
374   Modern, all singing, all dancing extended security (and possibly SPNEGO) request
375 */
376 static NTSTATUS session_setup_spnego(struct composite_context *c,
377                                      struct smbcli_session *session, 
378                                      struct smb_composite_sesssetup *io,
379                                      struct smbcli_request **req) 
380 {
381         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
382         NTSTATUS status, session_key_err;
383         DATA_BLOB session_key = data_blob(NULL, 0);
384         DATA_BLOB null_data_blob = data_blob(NULL, 0);
385         const char *chosen_oid = NULL;
386
387         state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
388         state->setup.spnego.in.bufsize      = session->transport->options.max_xmit;
389         state->setup.spnego.in.mpx_max      = session->transport->options.max_mux;
390         state->setup.spnego.in.vc_num       = 1;
391         state->setup.spnego.in.sesskey      = io->in.sesskey;
392         state->setup.spnego.in.capabilities = io->in.capabilities;
393         state->setup.spnego.in.os           = "Unix";
394         state->setup.spnego.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
395         state->setup.spnego.in.workgroup    = io->in.workgroup;
396
397         smbcli_temp_set_signing(session->transport);
398
399         status = gensec_client_start(session, &session->gensec, c->event_ctx,
400                                      global_loadparm);
401         if (!NT_STATUS_IS_OK(status)) {
402                 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
403                 return status;
404         }
405
406         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
407
408         status = gensec_set_credentials(session->gensec, io->in.credentials);
409         if (!NT_STATUS_IS_OK(status)) {
410                 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n", 
411                           nt_errstr(status)));
412                 return status;
413         }
414
415         status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
416         if (!NT_STATUS_IS_OK(status)) {
417                 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
418                           nt_errstr(status)));
419                 return status;
420         }
421
422         status = gensec_set_target_service(session->gensec, "cifs");
423         if (!NT_STATUS_IS_OK(status)) {
424                 DEBUG(1, ("Failed to start set GENSEC target service: %s\n", 
425                           nt_errstr(status)));
426                 return status;
427         }
428
429         if (session->transport->negotiate.secblob.length) {
430                 chosen_oid = GENSEC_OID_SPNEGO;
431                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
432                 if (!NT_STATUS_IS_OK(status)) {
433                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
434                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
435                         chosen_oid = GENSEC_OID_NTLMSSP;
436                         status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
437                         if (!NT_STATUS_IS_OK(status)) {
438                                 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
439                                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
440                         return status;
441                         }
442                 }
443         } else {
444                 /* without a sec blob, means raw NTLMSSP */
445                 chosen_oid = GENSEC_OID_NTLMSSP;
446                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
447                 if (!NT_STATUS_IS_OK(status)) {
448                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
449                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
450                 }
451         }
452
453         if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
454                 status = gensec_update(session->gensec, state,
455                                        session->transport->negotiate.secblob,
456                                        &state->setup.spnego.in.secblob);
457         } else {
458                 status = gensec_update(session->gensec, state,
459                                        data_blob(NULL, 0),
460                                        &state->setup.spnego.in.secblob);
461
462         }
463
464         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
465             !NT_STATUS_IS_OK(status)) {
466                 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
467                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
468                 return status;
469         }
470         state->gensec_status = status;
471
472         session_key_err = gensec_session_key(session->gensec, &session_key);
473         if (NT_STATUS_IS_OK(session_key_err)) {
474                 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
475         }
476
477         *req = smb_raw_sesssetup_send(session, &state->setup);
478         if (!*req) {
479                 return NT_STATUS_NO_MEMORY;
480         }
481
482         if (NT_STATUS_IS_OK(session_key_err)) {
483                 /*
484                  * as the response might be signed with a session key from within the response
485                  * we need to check the signiture of the response ourself.
486                  */
487                 (*req)->sign_caller_checks = true;
488         }
489
490         return (*req)->status;
491 }
492
493
494 /*
495   composite session setup function that hides the details of all the
496   different session setup varients, including the multi-pass nature of
497   the spnego varient
498 */
499 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session, 
500                                                        struct smb_composite_sesssetup *io)
501 {
502         struct composite_context *c;
503         struct sesssetup_state *state;
504         NTSTATUS status;
505
506         c = composite_create(session, session->transport->socket->event.ctx);
507         if (c == NULL) return NULL;
508
509         state = talloc_zero(c, struct sesssetup_state);
510         if (composite_nomem(state, c)) return c;
511         c->private_data = state;
512
513         state->io = io;
514
515         talloc_set_destructor(state, sesssetup_state_destructor);
516
517         /* no session setup at all in earliest protocol varients */
518         if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
519                 ZERO_STRUCT(io->out);
520                 composite_done(c);
521                 return c;
522         }
523
524         /* see what session setup interface we will use */
525         if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
526                 status = session_setup_old(c, session, io, &state->req);
527         } else if (!session->transport->options.use_spnego ||
528                    !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
529                 status = session_setup_nt1(c, session, io, &state->req);
530         } else {
531                 status = session_setup_spnego(c, session, io, &state->req);
532         }
533
534         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || 
535             NT_STATUS_IS_OK(status)) {
536                 composite_continue_smb(c, state->req, request_handler, c);      
537                 return c;
538         }
539
540         composite_error(c, status);
541         return c;
542 }
543
544
545 /*
546   receive a composite session setup reply
547 */
548 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
549 {
550         NTSTATUS status;
551         status = composite_wait(c);
552         talloc_free(c);
553         return status;
554 }
555
556 /*
557   sync version of smb_composite_sesssetup 
558 */
559 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
560 {
561         struct composite_context *c = smb_composite_sesssetup_send(session, io);
562         return smb_composite_sesssetup_recv(c);
563 }