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