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