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