r15416: Point out that this doesn't work, but for servers this old, I just
[metze/samba/wip.git] / source / libcli / smb_composite / sesssetup.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Copyright (C) Andrew Tridgell 2005
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 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         }
169
170         /* enforce the local signing required flag */
171         if (NT_STATUS_IS_OK(c->status) && !cli_credentials_is_anonymous(state->io->in.credentials)) {
172                 if (!session->transport->negotiate.sign_info.doing_signing 
173                     && session->transport->negotiate.sign_info.mandatory_signing) {
174                         DEBUG(0, ("SMB signing required, but server does not support it\n"));
175                         c->status = NT_STATUS_ACCESS_DENIED;
176                 }
177         }
178
179         if (NT_STATUS_IS_OK(c->status)) {
180                 c->state = COMPOSITE_STATE_DONE;
181         } else {
182                 c->state = COMPOSITE_STATE_ERROR;
183         }
184         if (c->async.fn) {
185                 c->async.fn(c);
186         }
187 }
188
189
190 /*
191   send a nt1 style session setup
192 */
193 static NTSTATUS session_setup_nt1(struct composite_context *c,
194                                   struct smbcli_session *session, 
195                                   struct smb_composite_sesssetup *io,
196                                   struct smbcli_request **req) 
197 {
198         NTSTATUS nt_status;
199         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
200         const char *password = cli_credentials_get_password(io->in.credentials);
201         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
202         DATA_BLOB session_key;
203         int flags = CLI_CRED_NTLM_AUTH;
204         if (lp_client_lanman_auth()) {
205                 flags |= CLI_CRED_LANMAN_AUTH;
206         }
207
208         if (lp_client_ntlmv2_auth()) {
209                 flags |= CLI_CRED_NTLMv2_AUTH;
210         }
211
212         state->setup.nt1.level           = RAW_SESSSETUP_NT1;
213         state->setup.nt1.in.bufsize      = session->transport->options.max_xmit;
214         state->setup.nt1.in.mpx_max      = session->transport->options.max_mux;
215         state->setup.nt1.in.vc_num       = 1;
216         state->setup.nt1.in.sesskey      = io->in.sesskey;
217         state->setup.nt1.in.capabilities = io->in.capabilities;
218         state->setup.nt1.in.os           = "Unix";
219         state->setup.nt1.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
220
221         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
222                                                  &state->setup.nt1.in.user,
223                                                  &state->setup.nt1.in.domain);
224         
225
226         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
227                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
228                                                               &flags, 
229                                                               session->transport->negotiate.secblob, 
230                                                               names_blob,
231                                                               &state->setup.nt1.in.password1,
232                                                               &state->setup.nt1.in.password2,
233                                                               NULL, &session_key);
234                 NT_STATUS_NOT_OK_RETURN(nt_status);
235
236                 smbcli_transport_simple_set_signing(session->transport, session_key, 
237                                                     state->setup.nt1.in.password2);
238                 set_user_session_key(session, &session_key);
239                 
240                 data_blob_free(&session_key);
241         } else if (lp_client_plaintext_auth()) {
242                 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
243                 state->setup.nt1.in.password2 = data_blob(NULL, 0);
244         } else {
245                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
246                 return NT_STATUS_INVALID_PARAMETER;
247         }
248
249         *req = smb_raw_sesssetup_send(session, &state->setup);
250         if (!*req) {
251                 return NT_STATUS_NO_MEMORY;
252         }
253         return (*req)->status;
254 }
255
256
257 /*
258   old style session setup (pre NT1 protocol level)
259 */
260 static NTSTATUS session_setup_old(struct composite_context *c,
261                                   struct smbcli_session *session, 
262                                   struct smb_composite_sesssetup *io,
263                                   struct smbcli_request **req) 
264 {
265         NTSTATUS nt_status;
266         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
267         const char *password = cli_credentials_get_password(io->in.credentials);
268         DATA_BLOB names_blob = NTLMv2_generate_names_blob(state, session->transport->socket->hostname, lp_workgroup());
269         DATA_BLOB session_key;
270         int flags = 0;
271         if (lp_client_lanman_auth()) {
272                 flags |= CLI_CRED_LANMAN_AUTH;
273         }
274
275         if (lp_client_ntlmv2_auth()) {
276                 flags |= CLI_CRED_NTLMv2_AUTH;
277         }
278
279         state->setup.old.level      = RAW_SESSSETUP_OLD;
280         state->setup.old.in.bufsize = session->transport->options.max_xmit;
281         state->setup.old.in.mpx_max = session->transport->options.max_mux;
282         state->setup.old.in.vc_num  = 1;
283         state->setup.old.in.sesskey = io->in.sesskey;
284         state->setup.old.in.os      = "Unix";
285         state->setup.old.in.lanman  = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
286         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
287                                                  &state->setup.old.in.user,
288                                                  &state->setup.old.in.domain);
289         
290         if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
291                 nt_status = cli_credentials_get_ntlm_response(io->in.credentials, state, 
292                                                               &flags, 
293                                                               session->transport->negotiate.secblob, 
294                                                               names_blob,
295                                                               &state->setup.old.in.password,
296                                                               NULL,
297                                                               NULL, &session_key);
298                 NT_STATUS_NOT_OK_RETURN(nt_status);
299                 set_user_session_key(session, &session_key);
300                 
301                 data_blob_free(&session_key);
302         } else if (lp_client_plaintext_auth()) {
303                 state->setup.old.in.password = data_blob_talloc(state, password, strlen(password));
304         } else {
305                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
306                 return NT_STATUS_INVALID_PARAMETER;
307         }
308         
309         *req = smb_raw_sesssetup_send(session, &state->setup);
310         if (!*req) {
311                 return NT_STATUS_NO_MEMORY;
312         }
313         return (*req)->status;
314 }
315
316
317 /*
318   Modern, all singing, all dancing extended security (and possibly SPNEGO) request
319 */
320 static NTSTATUS session_setup_spnego(struct composite_context *c,
321                                      struct smbcli_session *session, 
322                                      struct smb_composite_sesssetup *io,
323                                      struct smbcli_request **req) 
324 {
325         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
326         NTSTATUS status, session_key_err;
327         DATA_BLOB session_key = data_blob(NULL, 0);
328         DATA_BLOB null_data_blob = data_blob(NULL, 0);
329         const char *chosen_oid = NULL;
330
331         state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
332         state->setup.spnego.in.bufsize      = session->transport->options.max_xmit;
333         state->setup.spnego.in.mpx_max      = session->transport->options.max_mux;
334         state->setup.spnego.in.vc_num       = 1;
335         state->setup.spnego.in.sesskey      = io->in.sesskey;
336         state->setup.spnego.in.capabilities = io->in.capabilities;
337         state->setup.spnego.in.os           = "Unix";
338         state->setup.spnego.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
339         state->setup.spnego.in.workgroup    = io->in.workgroup;
340
341         state->setup.spnego.out.vuid        = session->vuid;
342
343         smbcli_temp_set_signing(session->transport);
344
345         status = gensec_client_start(session, &session->gensec, c->event_ctx);
346         if (!NT_STATUS_IS_OK(status)) {
347                 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
348                 return status;
349         }
350
351         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
352
353         status = gensec_set_credentials(session->gensec, io->in.credentials);
354         if (!NT_STATUS_IS_OK(status)) {
355                 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n", 
356                           nt_errstr(status)));
357                 return status;
358         }
359
360         status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
361         if (!NT_STATUS_IS_OK(status)) {
362                 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
363                           nt_errstr(status)));
364                 return status;
365         }
366
367         status = gensec_set_target_service(session->gensec, "cifs");
368         if (!NT_STATUS_IS_OK(status)) {
369                 DEBUG(1, ("Failed to start set GENSEC target service: %s\n", 
370                           nt_errstr(status)));
371                 return status;
372         }
373
374         if (session->transport->negotiate.secblob.length) {
375                 chosen_oid = GENSEC_OID_SPNEGO;
376                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
377                 if (!NT_STATUS_IS_OK(status)) {
378                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
379                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
380                         chosen_oid = GENSEC_OID_NTLMSSP;
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 (fallback) GENSEC client mechanism %s: %s\n",
384                                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
385                         return status;
386                         }
387                 }
388         } else {
389                 /* without a sec blob, means raw NTLMSSP */
390                 chosen_oid = GENSEC_OID_NTLMSSP;
391                 status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
392                 if (!NT_STATUS_IS_OK(status)) {
393                         DEBUG(1, ("Failed to start set GENSEC client mechanism %s: %s\n",
394                                   gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
395                 }
396         }
397
398         if (chosen_oid == GENSEC_OID_SPNEGO) {
399                 status = gensec_update(session->gensec, state,
400                                        session->transport->negotiate.secblob,
401                                        &state->setup.spnego.in.secblob);
402         } else {
403                 status = gensec_update(session->gensec, state,
404                                        data_blob(NULL, 0),
405                                        &state->setup.spnego.in.secblob);
406
407         }
408
409         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
410             !NT_STATUS_IS_OK(status)) {
411                 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
412                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
413                 return status;
414         }
415         state->gensec_status = status;
416
417         session_key_err = gensec_session_key(session->gensec, &session_key);
418         if (NT_STATUS_IS_OK(session_key_err)) {
419                 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
420         }
421
422         *req = smb_raw_sesssetup_send(session, &state->setup);
423         if (!*req) {
424                 return NT_STATUS_NO_MEMORY;
425         }
426         return (*req)->status;
427 }
428
429
430 /*
431   composite session setup function that hides the details of all the
432   different session setup varients, including the multi-pass nature of
433   the spnego varient
434 */
435 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session, 
436                                                        struct smb_composite_sesssetup *io)
437 {
438         struct composite_context *c;
439         struct sesssetup_state *state;
440         NTSTATUS status;
441
442         c = talloc_zero(session, struct composite_context);
443         if (c == NULL) return NULL;
444
445         state = talloc(c, struct sesssetup_state);
446         if (state == NULL) {
447                 talloc_free(c);
448                 return NULL;
449         }
450
451         state->io = io;
452
453         c->state = COMPOSITE_STATE_IN_PROGRESS;
454         c->private_data = state;
455         c->event_ctx = session->transport->socket->event.ctx;
456
457         /* no session setup at all in earliest protocol varients */
458         if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
459                 ZERO_STRUCT(io->out);
460                 c->state = COMPOSITE_STATE_DONE;
461                 return c;
462         }
463
464         /* see what session setup interface we will use */
465         if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
466                 status = session_setup_old(c, session, io, &state->req);
467         } else if (!session->transport->options.use_spnego ||
468                    !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
469                 status = session_setup_nt1(c, session, io, &state->req);
470         } else {
471                 status = session_setup_spnego(c, session, io, &state->req);
472         }
473
474         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || 
475             NT_STATUS_IS_OK(status)) {
476                 state->req->async.fn = request_handler;
477                 state->req->async.private = c;
478                 return c;
479         }
480
481         c->state = COMPOSITE_STATE_ERROR;
482         c->status = status;
483         return c;
484 }
485
486
487 /*
488   receive a composite session setup reply
489 */
490 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
491 {
492         NTSTATUS status;
493         status = composite_wait(c);
494         talloc_free(c);
495         return status;
496 }
497
498 /*
499   sync version of smb_composite_sesssetup 
500 */
501 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
502 {
503         struct composite_context *c = smb_composite_sesssetup_send(session, io);
504         return smb_composite_sesssetup_recv(c);
505 }