Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into openchange
[mat/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 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
89         c->status = smb_raw_sesssetup_recv(req, state, &state->setup);
90         state->req = NULL;
91
92         switch (state->setup.old.level) {
93         case RAW_SESSSETUP_OLD:
94                 state->io->out.vuid = state->setup.old.out.vuid;
95                 /* This doesn't work, as this only happens on old
96                  * protocols, where this comparison won't match. */
97                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
98                         /* we neet to reset the vuid for a new try */
99                         session->vuid = 0;
100                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
101                                 nt_status = session_setup_old(c, session, 
102                                                               state->io, 
103                                                               &state->req);
104                                 if (NT_STATUS_IS_OK(nt_status)) {
105                                         c->status = nt_status;
106                                         composite_continue_smb(c, state->req, request_handler, c);
107                                         return;
108                                 }
109                         }
110                 }
111                 break;
112
113         case RAW_SESSSETUP_NT1:
114                 state->io->out.vuid = state->setup.nt1.out.vuid;
115                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
116                         /* we neet to reset the vuid for a new try */
117                         session->vuid = 0;
118                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
119                                 nt_status = session_setup_nt1(c, session, 
120                                                               state->io, 
121                                                               &state->req);
122                                 if (NT_STATUS_IS_OK(nt_status)) {
123                                         c->status = nt_status;
124                                         composite_continue_smb(c, state->req, request_handler, c);
125                                         return;
126                                 }
127                         }
128                 }
129                 break;
130
131         case RAW_SESSSETUP_SPNEGO:
132                 state->io->out.vuid = state->setup.spnego.out.vuid;
133                 if (NT_STATUS_EQUAL(c->status, NT_STATUS_LOGON_FAILURE)) {
134                         /* we need to reset the vuid for a new try */
135                         session->vuid = 0;
136                         if (cli_credentials_wrong_password(state->io->in.credentials)) {
137                                 nt_status = session_setup_spnego(c, session, 
138                                                                       state->io, 
139                                                                       &state->req);
140                                 if (NT_STATUS_IS_OK(nt_status)) {
141                                         c->status = nt_status;
142                                         composite_continue_smb(c, state->req, request_handler, c);
143                                         return;
144                                 }
145                         }
146                 }
147                 if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
148                     !NT_STATUS_IS_OK(c->status)) {
149                         break;
150                 }
151                 if (NT_STATUS_EQUAL(state->gensec_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
152
153                         /* The status value here, from the earlier pass at GENSEC is
154                          * vital to the security of the system.  Even if the other end
155                          * accepts, if GENSEC claims 'MORE_PROCESSING_REQUIRED' then
156                          * you must keep feeding it blobs, or else the remote
157                          * host/attacker might avoid mutal authentication
158                          * requirements */
159                         
160                         state->gensec_status = gensec_update(session->gensec, state,
161                                                          state->setup.spnego.out.secblob,
162                                                          &state->setup.spnego.in.secblob);
163                         c->status = state->gensec_status;
164                         if (!NT_STATUS_EQUAL(c->status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
165                             !NT_STATUS_IS_OK(c->status)) {
166                                 break;
167                         }
168                 } else {
169                         state->setup.spnego.in.secblob = data_blob(NULL, 0);
170                 }
171
172                 /* we need to do another round of session setup. We keep going until both sides
173                    are happy */
174                 session_key_err = gensec_session_key(session->gensec, &session_key);
175                 if (NT_STATUS_IS_OK(session_key_err)) {
176                         set_user_session_key(session, &session_key);
177                         smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
178                 }
179
180                 if (state->setup.spnego.in.secblob.length) {
181                         /* 
182                          * set the session->vuid value only for calling
183                          * smb_raw_sesssetup_send()
184                          */
185                         uint16_t vuid = session->vuid;
186                         session->vuid = state->io->out.vuid;
187                         state->req = smb_raw_sesssetup_send(session, &state->setup);
188                         session->vuid = vuid;
189                         composite_continue_smb(c, state->req, request_handler, c);
190                         return;
191                 }
192                 break;
193
194         case RAW_SESSSETUP_SMB2:
195                 c->status = NT_STATUS_INTERNAL_ERROR;
196                 break;
197         }
198
199         /* enforce the local signing required flag */
200         if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
201                 if (!session->transport->negotiate.sign_info.doing_signing 
202                     && session->transport->negotiate.sign_info.mandatory_signing) {
203                         DEBUG(0, ("SMB signing required, but server does not support it\n"));
204                         c->status = NT_STATUS_ACCESS_DENIED;
205                 }
206         }
207
208         if (!NT_STATUS_IS_OK(c->status)) {
209                 composite_error(c, c->status);
210                 return;
211         }
212
213         composite_done(c);
214 }
215
216
217 /*
218   send a nt1 style session setup
219 */
220 static NTSTATUS session_setup_nt1(struct composite_context *c,
221                                   struct smbcli_session *session, 
222                                   struct smb_composite_sesssetup *io,
223                                   struct smbcli_request **req) 
224 {
225         NTSTATUS nt_status;
226         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
227         const char *password = cli_credentials_get_password(io->in.credentials);
228         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
229         DATA_BLOB session_key;
230         int flags = CLI_CRED_NTLM_AUTH;
231         if (session->options.lanman_auth) {
232                 flags |= CLI_CRED_LANMAN_AUTH;
233         }
234
235         if (session->options.ntlmv2_auth) {
236                 flags |= CLI_CRED_NTLMv2_AUTH;
237         }
238
239         state->setup.nt1.level           = RAW_SESSSETUP_NT1;
240         state->setup.nt1.in.bufsize      = session->transport->options.max_xmit;
241         state->setup.nt1.in.mpx_max      = session->transport->options.max_mux;
242         state->setup.nt1.in.vc_num       = 1;
243         state->setup.nt1.in.sesskey      = io->in.sesskey;
244         state->setup.nt1.in.capabilities = io->in.capabilities;
245         state->setup.nt1.in.os           = "Unix";
246         state->setup.nt1.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
247
248         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
249                                                  &state->setup.nt1.in.user,
250                                                  &state->setup.nt1.in.domain);
251         
252
253         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
254                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
255                                                               &flags, 
256                                                               session->transport->negotiate.secblob, 
257                                                               names_blob,
258                                                               &state->setup.nt1.in.password1,
259                                                               &state->setup.nt1.in.password2,
260                                                               NULL, &session_key);
261                 NT_STATUS_NOT_OK_RETURN(nt_status);
262
263                 smbcli_transport_simple_set_signing(session->transport, session_key, 
264                                                     state->setup.nt1.in.password2);
265                 set_user_session_key(session, &session_key);
266                 
267                 data_blob_free(&session_key);
268         } else if (session->options.plaintext_auth) {
269                 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
270                 state->setup.nt1.in.password2 = data_blob(NULL, 0);
271         } else {
272                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
273                 return NT_STATUS_INVALID_PARAMETER;
274         }
275
276         *req = smb_raw_sesssetup_send(session, &state->setup);
277         if (!*req) {
278                 return NT_STATUS_NO_MEMORY;
279         }
280         return (*req)->status;
281 }
282
283
284 /*
285   old style session setup (pre NT1 protocol level)
286 */
287 static NTSTATUS session_setup_old(struct composite_context *c,
288                                   struct smbcli_session *session, 
289                                   struct smb_composite_sesssetup *io,
290                                   struct smbcli_request **req) 
291 {
292         NTSTATUS nt_status;
293         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
294         const char *password = cli_credentials_get_password(io->in.credentials);
295         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, lp_iconv_convenience(global_loadparm), session->transport->socket->hostname, lp_workgroup(global_loadparm));
296         DATA_BLOB session_key;
297         int flags = 0;
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.old.level      = RAW_SESSSETUP_OLD;
307         state->setup.old.in.bufsize = session->transport->options.max_xmit;
308         state->setup.old.in.mpx_max = session->transport->options.max_mux;
309         state->setup.old.in.vc_num  = 1;
310         state->setup.old.in.sesskey = io->in.sesskey;
311         state->setup.old.in.os      = "Unix";
312         state->setup.old.in.lanman  = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
313         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
314                                                  &state->setup.old.in.user,
315                                                  &state->setup.old.in.domain);
316         
317         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
318                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
319                                                               &flags, 
320                                                               session->transport->negotiate.secblob, 
321                                                               names_blob,
322                                                               &state->setup.old.in.password,
323                                                               NULL,
324                                                               NULL, &session_key);
325                 NT_STATUS_NOT_OK_RETURN(nt_status);
326                 set_user_session_key(session, &session_key);
327                 
328                 data_blob_free(&session_key);
329         } else if (session->options.plaintext_auth) {
330                 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
331         } else {
332                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
333                 return NT_STATUS_INVALID_PARAMETER;
334         }
335         
336         *req = smb_raw_sesssetup_send(session, &state->setup);
337         if (!*req) {
338                 return NT_STATUS_NO_MEMORY;
339         }
340         return (*req)->status;
341 }
342
343
344 /*
345   Modern, all singing, all dancing extended security (and possibly SPNEGO) request
346 */
347 static NTSTATUS session_setup_spnego(struct composite_context *c,
348                                      struct smbcli_session *session, 
349                                      struct smb_composite_sesssetup *io,
350                                      struct smbcli_request **req) 
351 {
352         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
353         NTSTATUS status, session_key_err;
354         DATA_BLOB session_key = data_blob(NULL, 0);
355         DATA_BLOB null_data_blob = data_blob(NULL, 0);
356         const char *chosen_oid = NULL;
357
358         state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
359         state->setup.spnego.in.bufsize      = session->transport->options.max_xmit;
360         state->setup.spnego.in.mpx_max      = session->transport->options.max_mux;
361         state->setup.spnego.in.vc_num       = 1;
362         state->setup.spnego.in.sesskey      = io->in.sesskey;
363         state->setup.spnego.in.capabilities = io->in.capabilities;
364         state->setup.spnego.in.os           = "Unix";
365         state->setup.spnego.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
366         state->setup.spnego.in.workgroup    = io->in.workgroup;
367
368         smbcli_temp_set_signing(session->transport);
369
370         status = gensec_client_start(session, &session->gensec, c->event_ctx,
371                                      global_loadparm);
372         if (!NT_STATUS_IS_OK(status)) {
373                 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
374                 return status;
375         }
376
377         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
378
379         status = gensec_set_credentials(session->gensec, io->in.credentials);
380         if (!NT_STATUS_IS_OK(status)) {
381                 DEBUG(1, ("Failed to start set GENSEC client credentials: %s\n", 
382                           nt_errstr(status)));
383                 return status;
384         }
385
386         status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
387         if (!NT_STATUS_IS_OK(status)) {
388                 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
389                           nt_errstr(status)));
390                 return status;
391         }
392
393         status = gensec_set_target_service(session->gensec, "cifs");
394         if (!NT_STATUS_IS_OK(status)) {
395                 DEBUG(1, ("Failed to start set GENSEC target service: %s\n", 
396                           nt_errstr(status)));
397                 return status;
398         }
399
400         if (session->transport->negotiate.secblob.length) {
401                 chosen_oid = GENSEC_OID_SPNEGO;
402                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
403                 if (!NT_STATUS_IS_OK(status)) {
404                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
405                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
406                         chosen_oid = GENSEC_OID_NTLMSSP;
407                         status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
408                         if (!NT_STATUS_IS_OK(status)) {
409                                 DEBUG(1, ("Failed to start set (fallback) GENSEC client mechanism %s: %s\n",
410                                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
411                         return status;
412                         }
413                 }
414         } else {
415                 /* without a sec blob, means raw NTLMSSP */
416                 chosen_oid = GENSEC_OID_NTLMSSP;
417                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
418                 if (!NT_STATUS_IS_OK(status)) {
419                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
420                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
421                 }
422         }
423
424         if ((const void *)chosen_oid == (const void *)GENSEC_OID_SPNEGO) {
425                 status = gensec_update(session->gensec, state,
426                                        session->transport->negotiate.secblob,
427                                        &state->setup.spnego.in.secblob);
428         } else {
429                 status = gensec_update(session->gensec, state,
430                                        data_blob(NULL, 0),
431                                        &state->setup.spnego.in.secblob);
432
433         }
434
435         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
436             !NT_STATUS_IS_OK(status)) {
437                 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
438                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
439                 return status;
440         }
441         state->gensec_status = status;
442
443         session_key_err = gensec_session_key(session->gensec, &session_key);
444         if (NT_STATUS_IS_OK(session_key_err)) {
445                 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
446         }
447
448         *req = smb_raw_sesssetup_send(session, &state->setup);
449         if (!*req) {
450                 return NT_STATUS_NO_MEMORY;
451         }
452         return (*req)->status;
453 }
454
455
456 /*
457   composite session setup function that hides the details of all the
458   different session setup varients, including the multi-pass nature of
459   the spnego varient
460 */
461 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session, 
462                                                        struct smb_composite_sesssetup *io)
463 {
464         struct composite_context *c;
465         struct sesssetup_state *state;
466         NTSTATUS status;
467
468         c = composite_create(session, session->transport->socket->event.ctx);
469         if (c == NULL) return NULL;
470
471         state = talloc_zero(c, struct sesssetup_state);
472         if (composite_nomem(state, c)) return c;
473         c->private_data = state;
474
475         state->io = io;
476
477         talloc_set_destructor(state, sesssetup_state_destructor);
478
479         /* no session setup at all in earliest protocol varients */
480         if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
481                 ZERO_STRUCT(io->out);
482                 composite_done(c);
483                 return c;
484         }
485
486         /* see what session setup interface we will use */
487         if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
488                 status = session_setup_old(c, session, io, &state->req);
489         } else if (!session->transport->options.use_spnego ||
490                    !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
491                 status = session_setup_nt1(c, session, io, &state->req);
492         } else {
493                 status = session_setup_spnego(c, session, io, &state->req);
494         }
495
496         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || 
497             NT_STATUS_IS_OK(status)) {
498                 composite_continue_smb(c, state->req, request_handler, c);      
499                 return c;
500         }
501
502         composite_error(c, status);
503         return c;
504 }
505
506
507 /*
508   receive a composite session setup reply
509 */
510 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
511 {
512         NTSTATUS status;
513         status = composite_wait(c);
514         talloc_free(c);
515         return status;
516 }
517
518 /*
519   sync version of smb_composite_sesssetup 
520 */
521 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
522 {
523         struct composite_context *c = smb_composite_sesssetup_send(session, io);
524         return smb_composite_sesssetup_recv(c);
525 }