r10598: Factor out common code, in preperation for a move elsewhere.
[garming/samba-autobuild/.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                         data_blob_free(&lmv2_session_key);
203                         state->setup.nt1.in.password1 = lmv2_response;
204                         state->setup.nt1.in.password2 = ntlmv2_response;
205                         
206                 } else {
207
208                         state->setup.nt1.in.password2 = nt_blob(state, nt_hash,
209                                                                 session->transport->negotiate.secblob);
210                         if (lp_client_lanman_auth()) {
211                                 state->setup.nt1.in.password1 = lanman_blob(state, password, 
212                                                                             session->transport->negotiate.secblob);
213                         } else {
214                                 /* if not sending the LM password, send the NT password twice */
215                                 state->setup.nt1.in.password1 = state->setup.nt1.in.password2;
216                         }
217
218                         session_key = data_blob_talloc(session, NULL, 16);
219                         SMBsesskeygen_ntv1(nt_hash->hash, session_key.data);
220                 }
221
222                 smbcli_transport_simple_set_signing(session->transport, session_key, 
223                                                     state->setup.nt1.in.password2);
224                 set_user_session_key(session, &session_key);
225                 
226                 data_blob_free(&session_key);
227
228         } else if (lp_client_plaintext_auth()) {
229                 state->setup.nt1.in.password1 = data_blob_talloc(state, password, strlen(password));
230                 state->setup.nt1.in.password2 = data_blob(NULL, 0);
231         } else {
232                 /* could match windows client and return 'cannot logon from this workstation', but it just confuses everybody */
233                 return NT_STATUS_INVALID_PARAMETER;
234         }
235
236         *req = smb_raw_sesssetup_send(session, &state->setup);
237         if (!*req) {
238                 return NT_STATUS_NO_MEMORY;
239         }
240         return (*req)->status;
241 }
242
243
244 /*
245   old style session setup (pre NT1 protocol level)
246 */
247 static NTSTATUS session_setup_old(struct composite_context *c,
248                                   struct smbcli_session *session, 
249                                   struct smb_composite_sesssetup *io,
250                                   struct smbcli_request **req) 
251 {
252         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
253         const char *password = cli_credentials_get_password(io->in.credentials);
254
255         state->setup.old.level      = RAW_SESSSETUP_OLD;
256         state->setup.old.in.bufsize = session->transport->options.max_xmit;
257         state->setup.old.in.mpx_max = session->transport->options.max_mux;
258         state->setup.old.in.vc_num  = 1;
259         state->setup.old.in.sesskey = io->in.sesskey;
260         state->setup.old.in.os      = "Unix";
261         state->setup.old.in.lanman  = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
262         cli_credentials_get_ntlm_username_domain(io->in.credentials, state, 
263                                                  &state->setup.old.in.user,
264                                                  &state->setup.old.in.domain);
265         
266         if (!password) {
267                 state->setup.old.in.password = data_blob(NULL, 0);
268         } else if (session->transport->negotiate.sec_mode & NEGOTIATE_SECURITY_CHALLENGE_RESPONSE) {
269                 state->setup.old.in.password = lanman_blob(state, password, 
270                                                            session->transport->negotiate.secblob);
271         } else {
272                 state->setup.old.in.password = data_blob_talloc(state,
273                                                                 password, 
274                                                                 strlen(password));
275         }
276         
277         *req = smb_raw_sesssetup_send(session, &state->setup);
278         if (!*req) {
279                 return NT_STATUS_NO_MEMORY;
280         }
281         return (*req)->status;
282 }
283
284
285 /*
286   Modern, all singing, all dancing extended security (and possibly SPNEGO) request
287 */
288 static NTSTATUS session_setup_spnego(struct composite_context *c,
289                                      struct smbcli_session *session, 
290                                      struct smb_composite_sesssetup *io,
291                                      struct smbcli_request **req) 
292 {
293         struct sesssetup_state *state = talloc_get_type(c->private_data, struct sesssetup_state);
294         NTSTATUS status, session_key_err;
295         DATA_BLOB session_key = data_blob(NULL, 0);
296         DATA_BLOB null_data_blob = data_blob(NULL, 0);
297         const char *chosen_oid = NULL;
298
299         state->setup.spnego.level           = RAW_SESSSETUP_SPNEGO;
300         state->setup.spnego.in.bufsize      = session->transport->options.max_xmit;
301         state->setup.spnego.in.mpx_max      = session->transport->options.max_mux;
302         state->setup.spnego.in.vc_num       = 1;
303         state->setup.spnego.in.sesskey      = io->in.sesskey;
304         state->setup.spnego.in.capabilities = io->in.capabilities;
305         state->setup.spnego.in.os           = "Unix";
306         state->setup.spnego.in.lanman       = talloc_asprintf(state, "Samba %s", SAMBA_VERSION_STRING);
307         state->setup.spnego.in.workgroup    = io->in.workgroup;
308
309         state->setup.spnego.out.vuid        = session->vuid;
310
311         smbcli_temp_set_signing(session->transport);
312
313         status = gensec_client_start(session, &session->gensec, c->event_ctx);
314         if (!NT_STATUS_IS_OK(status)) {
315                 DEBUG(1, ("Failed to start GENSEC client mode: %s\n", nt_errstr(status)));
316                 return status;
317         }
318
319         gensec_want_feature(session->gensec, GENSEC_FEATURE_SESSION_KEY);
320
321         status = gensec_set_credentials(session->gensec, io->in.credentials);
322         if (!NT_STATUS_IS_OK(status)) {
323                 DEBUG(1, ("Failed to start set GENSEC client credentails: %s\n", 
324                           nt_errstr(status)));
325                 return status;
326         }
327
328         status = gensec_set_target_hostname(session->gensec, session->transport->socket->hostname);
329         if (!NT_STATUS_IS_OK(status)) {
330                 DEBUG(1, ("Failed to start set GENSEC target hostname: %s\n", 
331                           nt_errstr(status)));
332                 return status;
333         }
334
335         status = gensec_set_target_service(session->gensec, "cifs");
336         if (!NT_STATUS_IS_OK(status)) {
337                 DEBUG(1, ("Failed to start set GENSEC target service: %s\n", 
338                           nt_errstr(status)));
339                 return status;
340         }
341
342         if (session->transport->negotiate.secblob.length) {
343                 chosen_oid = GENSEC_OID_SPNEGO;
344         } else {
345                 /* without a sec blob, means raw NTLMSSP */
346                 chosen_oid = GENSEC_OID_NTLMSSP;
347         }
348
349         status = gensec_start_mech_by_oid(session->gensec, chosen_oid);
350         if (!NT_STATUS_IS_OK(status)) {
351                 DEBUG(1, ("Failed to start set GENSEC client SPNEGO mechanism %s: %s\n",
352                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
353                 return status;
354         }
355         
356         status = gensec_update(session->gensec, state,
357                                session->transport->negotiate.secblob,
358                                &state->setup.spnego.in.secblob);
359         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) && 
360             !NT_STATUS_IS_OK(status)) {
361                 DEBUG(1, ("Failed initial gensec_update with mechanism %s: %s\n",
362                           gensec_get_name_by_oid(chosen_oid), nt_errstr(status)));
363                 return status;
364         }
365         state->gensec_status = status;
366
367         session_key_err = gensec_session_key(session->gensec, &session_key);
368         if (NT_STATUS_IS_OK(session_key_err)) {
369                 smbcli_transport_simple_set_signing(session->transport, session_key, null_data_blob);
370         }
371
372         *req = smb_raw_sesssetup_send(session, &state->setup);
373         if (!*req) {
374                 return NT_STATUS_NO_MEMORY;
375         }
376         return (*req)->status;
377 }
378
379
380 /*
381   composite session setup function that hides the details of all the
382   different session setup varients, including the multi-pass nature of
383   the spnego varient
384 */
385 struct composite_context *smb_composite_sesssetup_send(struct smbcli_session *session, 
386                                                        struct smb_composite_sesssetup *io)
387 {
388         struct composite_context *c;
389         struct sesssetup_state *state;
390         NTSTATUS status;
391
392         c = talloc_zero(session, struct composite_context);
393         if (c == NULL) return NULL;
394
395         state = talloc(c, struct sesssetup_state);
396         if (state == NULL) {
397                 c->state = COMPOSITE_STATE_ERROR;
398                 c->status = NT_STATUS_NO_MEMORY;
399         }
400
401         state->io = io;
402
403         c->state = COMPOSITE_STATE_IN_PROGRESS;
404         c->private_data = state;
405         c->event_ctx = session->transport->socket->event.ctx;
406
407         /* no session setup at all in earliest protocol varients */
408         if (session->transport->negotiate.protocol < PROTOCOL_LANMAN1) {
409                 ZERO_STRUCT(io->out);
410                 c->state = COMPOSITE_STATE_DONE;
411                 return c;
412         }
413
414         /* see what session setup interface we will use */
415         if (session->transport->negotiate.protocol < PROTOCOL_NT1) {
416                 status = session_setup_old(c, session, io, &state->req);
417         } else if (!session->transport->options.use_spnego ||
418                    !(io->in.capabilities & CAP_EXTENDED_SECURITY)) {
419                 status = session_setup_nt1(c, session, io, &state->req);
420         } else {
421                 status = session_setup_spnego(c, session, io, &state->req);
422         }
423
424         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) || 
425             NT_STATUS_IS_OK(status)) {
426                 state->req->async.fn = request_handler;
427                 state->req->async.private = c;
428                 return c;
429         }
430
431         c->state = COMPOSITE_STATE_ERROR;
432         c->status = status;
433         return c;
434 }
435
436
437 /*
438   receive a composite session setup reply
439 */
440 NTSTATUS smb_composite_sesssetup_recv(struct composite_context *c)
441 {
442         NTSTATUS status;
443         status = composite_wait(c);
444         talloc_free(c);
445         return status;
446 }
447
448 /*
449   sync version of smb_composite_sesssetup 
450 */
451 NTSTATUS smb_composite_sesssetup(struct smbcli_session *session, struct smb_composite_sesssetup *io)
452 {
453         struct composite_context *c = smb_composite_sesssetup_send(session, io);
454         return smb_composite_sesssetup_recv(c);
455 }