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