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