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