s3-auth Move map to guest to directly after the check_password calls
[amitay/samba.git] / source3 / smbd / smb2_sesssetup.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) Jeremy Allison 2010
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../libcli/auth/spnego.h"
27 #include "../libcli/auth/ntlmssp.h"
28 #include "ntlmssp_wrap.h"
29 #include "../librpc/gen_ndr/krb5pac.h"
30 #include "libads/kerberos_proto.h"
31 #include "../lib/util/asn1.h"
32 #include "auth.h"
33 #include "../lib/tsocket/tsocket.h"
34 #include "../libcli/security/security.h"
35
36 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
37                                         uint64_t in_session_id,
38                                         uint8_t in_security_mode,
39                                         DATA_BLOB in_security_buffer,
40                                         uint16_t *out_session_flags,
41                                         DATA_BLOB *out_security_buffer,
42                                         uint64_t *out_session_id);
43
44 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *smb2req)
45 {
46         const uint8_t *inhdr;
47         const uint8_t *inbody;
48         int i = smb2req->current_idx;
49         uint8_t *outhdr;
50         DATA_BLOB outbody;
51         DATA_BLOB outdyn;
52         size_t expected_body_size = 0x19;
53         size_t body_size;
54         uint64_t in_session_id;
55         uint8_t in_security_mode;
56         uint16_t in_security_offset;
57         uint16_t in_security_length;
58         DATA_BLOB in_security_buffer;
59         uint16_t out_session_flags;
60         uint64_t out_session_id;
61         uint16_t out_security_offset;
62         DATA_BLOB out_security_buffer;
63         NTSTATUS status;
64
65         inhdr = (const uint8_t *)smb2req->in.vector[i+0].iov_base;
66
67         if (smb2req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
68                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
69         }
70
71         inbody = (const uint8_t *)smb2req->in.vector[i+1].iov_base;
72
73         body_size = SVAL(inbody, 0x00);
74         if (body_size != expected_body_size) {
75                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
76         }
77
78         in_security_offset = SVAL(inbody, 0x0C);
79         in_security_length = SVAL(inbody, 0x0E);
80
81         if (in_security_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
82                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
83         }
84
85         if (in_security_length > smb2req->in.vector[i+2].iov_len) {
86                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
87         }
88
89         in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
90         in_security_mode = CVAL(inbody, 0x03);
91         in_security_buffer.data = (uint8_t *)smb2req->in.vector[i+2].iov_base;
92         in_security_buffer.length = in_security_length;
93
94         status = smbd_smb2_session_setup(smb2req,
95                                          in_session_id,
96                                          in_security_mode,
97                                          in_security_buffer,
98                                          &out_session_flags,
99                                          &out_security_buffer,
100                                          &out_session_id);
101         if (!NT_STATUS_IS_OK(status) &&
102             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
103                 status = nt_status_squash(status);
104                 return smbd_smb2_request_error(smb2req, status);
105         }
106
107         out_security_offset = SMB2_HDR_BODY + 0x08;
108
109         outhdr = (uint8_t *)smb2req->out.vector[i].iov_base;
110
111         outbody = data_blob_talloc(smb2req->out.vector, NULL, 0x08);
112         if (outbody.data == NULL) {
113                 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
114         }
115
116         SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
117
118         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
119         SSVAL(outbody.data, 0x02,
120               out_session_flags);               /* session flags */
121         SSVAL(outbody.data, 0x04,
122               out_security_offset);             /* security buffer offset */
123         SSVAL(outbody.data, 0x06,
124               out_security_buffer.length);      /* security buffer length */
125
126         outdyn = out_security_buffer;
127
128         return smbd_smb2_request_done_ex(smb2req, status, outbody, &outdyn,
129                                          __location__);
130 }
131
132 static int smbd_smb2_session_destructor(struct smbd_smb2_session *session)
133 {
134         if (session->sconn == NULL) {
135                 return 0;
136         }
137
138         /* first free all tcons */
139         while (session->tcons.list) {
140                 talloc_free(session->tcons.list);
141         }
142
143         idr_remove(session->sconn->smb2.sessions.idtree, session->vuid);
144         DLIST_REMOVE(session->sconn->smb2.sessions.list, session);
145         invalidate_vuid(session->sconn, session->vuid);
146
147         session->vuid = 0;
148         session->status = NT_STATUS_USER_SESSION_DELETED;
149         session->sconn = NULL;
150
151         return 0;
152 }
153
154 #ifdef HAVE_KRB5
155 static NTSTATUS smbd_smb2_session_setup_krb5(struct smbd_smb2_session *session,
156                                         struct smbd_smb2_request *smb2req,
157                                         uint8_t in_security_mode,
158                                         const DATA_BLOB *secblob,
159                                         const char *mechOID,
160                                         uint16_t *out_session_flags,
161                                         DATA_BLOB *out_security_buffer,
162                                         uint64_t *out_session_id)
163 {
164         DATA_BLOB ap_rep = data_blob_null;
165         DATA_BLOB ap_rep_wrapped = data_blob_null;
166         DATA_BLOB ticket = data_blob_null;
167         DATA_BLOB session_key = data_blob_null;
168         DATA_BLOB secblob_out = data_blob_null;
169         uint8 tok_id[2];
170         struct PAC_LOGON_INFO *logon_info = NULL;
171         char *principal = NULL;
172         char *user = NULL;
173         char *domain = NULL;
174         struct passwd *pw = NULL;
175         NTSTATUS status;
176         char *real_username;
177         fstring tmp;
178         bool username_was_mapped = false;
179         bool map_domainuser_to_guest = false;
180
181         if (!spnego_parse_krb5_wrap(talloc_tos(), *secblob, &ticket, tok_id)) {
182                 status = NT_STATUS_LOGON_FAILURE;
183                 goto fail;
184         }
185
186         status = ads_verify_ticket(smb2req, lp_realm(), 0, &ticket,
187                                    &principal, &logon_info, &ap_rep,
188                                    &session_key, true);
189
190         if (!NT_STATUS_IS_OK(status)) {
191                 DEBUG(1,("smb2: Failed to verify incoming ticket with error %s!\n",
192                         nt_errstr(status)));
193                 if (!NT_STATUS_EQUAL(status, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
194                         status = NT_STATUS_LOGON_FAILURE;
195                 }
196                 goto fail;
197         }
198
199         status = get_user_from_kerberos_info(talloc_tos(),
200                                              session->sconn->remote_hostname,
201                                              principal, logon_info,
202                                              &username_was_mapped,
203                                              &map_domainuser_to_guest,
204                                              &user, &domain,
205                                              &real_username, &pw);
206         if (!NT_STATUS_IS_OK(status)) {
207                 goto fail;
208         }
209
210         /* save the PAC data if we have it */
211         if (logon_info) {
212                 netsamlogon_cache_store(user, &logon_info->info3);
213         }
214
215         /* setup the string used by %U */
216         sub_set_smb_name(real_username);
217
218         /* reload services so that the new %U is taken into account */
219         reload_services(smb2req->sconn->msg_ctx, smb2req->sconn->sock, true);
220
221         status = make_session_info_krb5(session,
222                                         user, domain, real_username, pw,
223                                         logon_info, map_domainuser_to_guest,
224                                         username_was_mapped,
225                                         &session_key,
226                                         &session->session_info);
227         if (!NT_STATUS_IS_OK(status)) {
228                 DEBUG(1, ("smb2: make_server_info_krb5 failed\n"));
229                 goto fail;
230         }
231
232         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
233              lp_server_signing() == Required) {
234                 session->do_signing = true;
235         }
236
237         if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
238                 /* we map anonymous to guest internally */
239                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
240                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
241                 /* force no signing */
242                 session->do_signing = false;
243         }
244
245         session->session_key = session->session_info->session_key;
246
247         session->compat_vuser = talloc_zero(session, user_struct);
248         if (session->compat_vuser == NULL) {
249                 status = NT_STATUS_NO_MEMORY;
250                 goto fail;
251         }
252         session->compat_vuser->auth_ntlmssp_state = NULL;
253         session->compat_vuser->homes_snum = -1;
254         session->compat_vuser->session_info = session->session_info;
255         session->compat_vuser->session_keystr = NULL;
256         session->compat_vuser->vuid = session->vuid;
257         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
258
259         /* This is a potentially untrusted username */
260         alpha_strcpy(tmp, user, ". _-$", sizeof(tmp));
261         session->session_info->unix_info->sanitized_username =
262                                 talloc_strdup(session->session_info, tmp);
263
264         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
265                 session->compat_vuser->homes_snum =
266                         register_homes_share(session->session_info->unix_info->unix_name);
267         }
268
269         if (!session_claim(session->sconn, session->compat_vuser)) {
270                 DEBUG(1, ("smb2: Failed to claim session "
271                         "for vuid=%d\n",
272                         session->compat_vuser->vuid));
273                 goto fail;
274         }
275
276         session->status = NT_STATUS_OK;
277
278         /*
279          * we attach the session to the request
280          * so that the response can be signed
281          */
282         smb2req->session = session;
283         if (session->do_signing) {
284                 smb2req->do_signing = true;
285         }
286
287         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
288         status = NT_STATUS_OK;
289
290         /* wrap that up in a nice GSS-API wrapping */
291         ap_rep_wrapped = spnego_gen_krb5_wrap(talloc_tos(), ap_rep,
292                                 TOK_ID_KRB_AP_REP);
293
294         secblob_out = spnego_gen_auth_response(
295                                         talloc_tos(),
296                                         &ap_rep_wrapped,
297                                         status,
298                                         mechOID);
299
300         *out_security_buffer = data_blob_talloc(smb2req,
301                                                 secblob_out.data,
302                                                 secblob_out.length);
303         if (secblob_out.data && out_security_buffer->data == NULL) {
304                 status = NT_STATUS_NO_MEMORY;
305                 goto fail;
306         }
307
308         data_blob_free(&ap_rep);
309         data_blob_free(&ap_rep_wrapped);
310         data_blob_free(&ticket);
311         data_blob_free(&session_key);
312         data_blob_free(&secblob_out);
313
314         *out_session_id = session->vuid;
315
316         return NT_STATUS_OK;
317
318   fail:
319
320         data_blob_free(&ap_rep);
321         data_blob_free(&ap_rep_wrapped);
322         data_blob_free(&ticket);
323         data_blob_free(&session_key);
324         data_blob_free(&secblob_out);
325
326         ap_rep_wrapped = data_blob_null;
327         secblob_out = spnego_gen_auth_response(
328                                         talloc_tos(),
329                                         &ap_rep_wrapped,
330                                         status,
331                                         mechOID);
332
333         *out_security_buffer = data_blob_talloc(smb2req,
334                                                 secblob_out.data,
335                                                 secblob_out.length);
336         data_blob_free(&secblob_out);
337         return status;
338 }
339 #endif
340
341 static NTSTATUS smbd_smb2_spnego_negotiate(struct smbd_smb2_session *session,
342                                         struct smbd_smb2_request *smb2req,
343                                         uint8_t in_security_mode,
344                                         DATA_BLOB in_security_buffer,
345                                         uint16_t *out_session_flags,
346                                         DATA_BLOB *out_security_buffer,
347                                         uint64_t *out_session_id)
348 {
349         DATA_BLOB secblob_in = data_blob_null;
350         DATA_BLOB chal_out = data_blob_null;
351         char *kerb_mech = NULL;
352         NTSTATUS status;
353
354         /* Ensure we have no old NTLM state around. */
355         TALLOC_FREE(session->auth_ntlmssp_state);
356
357         status = parse_spnego_mechanisms(talloc_tos(), in_security_buffer,
358                         &secblob_in, &kerb_mech);
359         if (!NT_STATUS_IS_OK(status)) {
360                 goto out;
361         }
362
363 #ifdef HAVE_KRB5
364         if (kerb_mech && ((lp_security()==SEC_ADS) ||
365                                 USE_KERBEROS_KEYTAB) ) {
366                 status = smbd_smb2_session_setup_krb5(session,
367                                 smb2req,
368                                 in_security_mode,
369                                 &secblob_in,
370                                 kerb_mech,
371                                 out_session_flags,
372                                 out_security_buffer,
373                                 out_session_id);
374
375                 goto out;
376         }
377 #endif
378
379         if (kerb_mech) {
380                 /* The mechtoken is a krb5 ticket, but
381                  * we need to fall back to NTLM. */
382
383                 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
384                         "but set to downgrade to NTLMSSP\n"));
385
386                 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
387         } else {
388                 /* Fall back to NTLMSSP. */
389                 status = auth_ntlmssp_start(session->sconn->remote_address,
390                                             &session->auth_ntlmssp_state);
391                 if (!NT_STATUS_IS_OK(status)) {
392                         goto out;
393                 }
394
395                 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
396                                              secblob_in,
397                                              &chal_out);
398         }
399
400         if (!NT_STATUS_IS_OK(status) &&
401                         !NT_STATUS_EQUAL(status,
402                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
403                 goto out;
404         }
405
406         *out_security_buffer = spnego_gen_auth_response(smb2req,
407                                                 &chal_out,
408                                                 status,
409                                                 OID_NTLMSSP);
410         if (out_security_buffer->data == NULL) {
411                 status = NT_STATUS_NO_MEMORY;
412                 goto out;
413         }
414         *out_session_id = session->vuid;
415
416   out:
417
418         data_blob_free(&secblob_in);
419         data_blob_free(&chal_out);
420         TALLOC_FREE(kerb_mech);
421         if (!NT_STATUS_IS_OK(status) &&
422                         !NT_STATUS_EQUAL(status,
423                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
424                 TALLOC_FREE(session->auth_ntlmssp_state);
425                 TALLOC_FREE(session);
426         }
427         return status;
428 }
429
430 static NTSTATUS smbd_smb2_common_ntlmssp_auth_return(struct smbd_smb2_session *session,
431                                         struct smbd_smb2_request *smb2req,
432                                         uint8_t in_security_mode,
433                                         DATA_BLOB in_security_buffer,
434                                         uint16_t *out_session_flags,
435                                         uint64_t *out_session_id)
436 {
437         fstring tmp;
438
439         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
440             lp_server_signing() == Required) {
441                 session->do_signing = true;
442         }
443
444         if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
445                 /* we map anonymous to guest internally */
446                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
447                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
448                 /* force no signing */
449                 session->do_signing = false;
450         }
451
452         session->session_key = session->session_info->session_key;
453
454         session->compat_vuser = talloc_zero(session, user_struct);
455         if (session->compat_vuser == NULL) {
456                 TALLOC_FREE(session->auth_ntlmssp_state);
457                 TALLOC_FREE(session);
458                 return NT_STATUS_NO_MEMORY;
459         }
460         session->compat_vuser->auth_ntlmssp_state = session->auth_ntlmssp_state;
461         session->compat_vuser->homes_snum = -1;
462         session->compat_vuser->session_info = session->session_info;
463         session->compat_vuser->session_keystr = NULL;
464         session->compat_vuser->vuid = session->vuid;
465         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
466
467         /* This is a potentially untrusted username */
468         alpha_strcpy(tmp,
469                      auth_ntlmssp_get_username(session->auth_ntlmssp_state),
470                      ". _-$",
471                      sizeof(tmp));
472         session->session_info->unix_info->sanitized_username = talloc_strdup(
473                 session->session_info, tmp);
474
475         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
476                 session->compat_vuser->homes_snum =
477                         register_homes_share(session->session_info->unix_info->unix_name);
478         }
479
480         if (!session_claim(session->sconn, session->compat_vuser)) {
481                 DEBUG(1, ("smb2: Failed to claim session "
482                         "for vuid=%d\n",
483                         session->compat_vuser->vuid));
484                 TALLOC_FREE(session->auth_ntlmssp_state);
485                 TALLOC_FREE(session);
486                 return NT_STATUS_LOGON_FAILURE;
487         }
488
489
490         session->status = NT_STATUS_OK;
491
492         /*
493          * we attach the session to the request
494          * so that the response can be signed
495          */
496         smb2req->session = session;
497         if (session->do_signing) {
498                 smb2req->do_signing = true;
499         }
500
501         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
502
503         *out_session_id = session->vuid;
504
505         return NT_STATUS_OK;
506 }
507
508 static NTSTATUS smbd_smb2_spnego_auth(struct smbd_smb2_session *session,
509                                         struct smbd_smb2_request *smb2req,
510                                         uint8_t in_security_mode,
511                                         DATA_BLOB in_security_buffer,
512                                         uint16_t *out_session_flags,
513                                         DATA_BLOB *out_security_buffer,
514                                         uint64_t *out_session_id)
515 {
516         DATA_BLOB auth = data_blob_null;
517         DATA_BLOB auth_out = data_blob_null;
518         NTSTATUS status;
519
520         if (!spnego_parse_auth(talloc_tos(), in_security_buffer, &auth)) {
521                 TALLOC_FREE(session);
522                 return NT_STATUS_LOGON_FAILURE;
523         }
524
525         if (auth.data[0] == ASN1_APPLICATION(0)) {
526                 /* Might be a second negTokenTarg packet */
527                 DATA_BLOB secblob_in = data_blob_null;
528                 char *kerb_mech = NULL;
529
530                 status = parse_spnego_mechanisms(talloc_tos(),
531                                 in_security_buffer,
532                                 &secblob_in, &kerb_mech);
533                 if (!NT_STATUS_IS_OK(status)) {
534                         TALLOC_FREE(session);
535                         return status;
536                 }
537
538 #ifdef HAVE_KRB5
539                 if (kerb_mech && ((lp_security()==SEC_ADS) ||
540                                         USE_KERBEROS_KEYTAB) ) {
541                         status = smbd_smb2_session_setup_krb5(session,
542                                         smb2req,
543                                         in_security_mode,
544                                         &secblob_in,
545                                         kerb_mech,
546                                         out_session_flags,
547                                         out_security_buffer,
548                                         out_session_id);
549
550                         data_blob_free(&secblob_in);
551                         TALLOC_FREE(kerb_mech);
552                         if (!NT_STATUS_IS_OK(status)) {
553                                 TALLOC_FREE(session);
554                         }
555                         return status;
556                 }
557 #endif
558
559                 /* Can't blunder into NTLMSSP auth if we have
560                  * a krb5 ticket. */
561
562                 if (kerb_mech) {
563                         DEBUG(3,("smb2: network "
564                                 "misconfiguration, client sent us a "
565                                 "krb5 ticket and kerberos security "
566                                 "not enabled\n"));
567                         TALLOC_FREE(session);
568                         data_blob_free(&secblob_in);
569                         TALLOC_FREE(kerb_mech);
570                         return NT_STATUS_LOGON_FAILURE;
571                 }
572
573                 data_blob_free(&secblob_in);
574         }
575
576         if (session->auth_ntlmssp_state == NULL) {
577                 status = auth_ntlmssp_start(session->sconn->remote_address,
578                                             &session->auth_ntlmssp_state);
579                 if (!NT_STATUS_IS_OK(status)) {
580                         data_blob_free(&auth);
581                         TALLOC_FREE(session);
582                         return status;
583                 }
584         }
585
586         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
587                                      auth,
588                                      &auth_out);
589         /* If status is NT_STATUS_OK then we need to get the token.
590          * Map to guest is now internal to auth_ntlmssp */
591         if (NT_STATUS_IS_OK(status)) {
592                 status = auth_ntlmssp_steal_session_info(session,
593                                 session->auth_ntlmssp_state,
594                                 &session->session_info);
595         }
596
597         if (!NT_STATUS_IS_OK(status) &&
598                         !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
599                 TALLOC_FREE(session->auth_ntlmssp_state);
600                 data_blob_free(&auth);
601                 TALLOC_FREE(session);
602                 return status;
603         }
604
605         data_blob_free(&auth);
606
607         *out_security_buffer = spnego_gen_auth_response(smb2req,
608                                 &auth_out, status, NULL);
609
610         if (out_security_buffer->data == NULL) {
611                 TALLOC_FREE(session->auth_ntlmssp_state);
612                 TALLOC_FREE(session);
613                 return NT_STATUS_NO_MEMORY;
614         }
615
616         *out_session_id = session->vuid;
617
618         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
619                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
620         }
621
622         /* We're done - claim the session. */
623         return smbd_smb2_common_ntlmssp_auth_return(session,
624                                                 smb2req,
625                                                 in_security_mode,
626                                                 in_security_buffer,
627                                                 out_session_flags,
628                                                 out_session_id);
629 }
630
631 static NTSTATUS smbd_smb2_raw_ntlmssp_auth(struct smbd_smb2_session *session,
632                                         struct smbd_smb2_request *smb2req,
633                                         uint8_t in_security_mode,
634                                         DATA_BLOB in_security_buffer,
635                                         uint16_t *out_session_flags,
636                                         DATA_BLOB *out_security_buffer,
637                                         uint64_t *out_session_id)
638 {
639         NTSTATUS status;
640         DATA_BLOB secblob_out = data_blob_null;
641
642         if (session->auth_ntlmssp_state == NULL) {
643                 status = auth_ntlmssp_start(session->sconn->remote_address,
644                                             &session->auth_ntlmssp_state);
645                 if (!NT_STATUS_IS_OK(status)) {
646                         TALLOC_FREE(session);
647                         return status;
648                 }
649         }
650
651         /* RAW NTLMSSP */
652         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
653                                      in_security_buffer,
654                                      &secblob_out);
655
656         if (NT_STATUS_IS_OK(status) ||
657                         NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
658                 *out_security_buffer = data_blob_talloc(smb2req,
659                                                 secblob_out.data,
660                                                 secblob_out.length);
661                 if (secblob_out.data && out_security_buffer->data == NULL) {
662                         TALLOC_FREE(session->auth_ntlmssp_state);
663                         TALLOC_FREE(session);
664                         return NT_STATUS_NO_MEMORY;
665                 }
666         }
667
668         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
669                 *out_session_id = session->vuid;
670                 return status;
671         }
672
673         status = auth_ntlmssp_steal_session_info(session,
674                                                  session->auth_ntlmssp_state,
675                                                  &session->session_info);
676
677         if (!NT_STATUS_IS_OK(status)) {
678                 TALLOC_FREE(session->auth_ntlmssp_state);
679                 TALLOC_FREE(session);
680                 return status;
681         }
682         *out_session_id = session->vuid;
683
684         return smbd_smb2_common_ntlmssp_auth_return(session,
685                                                 smb2req,
686                                                 in_security_mode,
687                                                 in_security_buffer,
688                                                 out_session_flags,
689                                                 out_session_id);
690 }
691
692 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
693                                         uint64_t in_session_id,
694                                         uint8_t in_security_mode,
695                                         DATA_BLOB in_security_buffer,
696                                         uint16_t *out_session_flags,
697                                         DATA_BLOB *out_security_buffer,
698                                         uint64_t *out_session_id)
699 {
700         struct smbd_smb2_session *session;
701
702         *out_session_flags = 0;
703         *out_session_id = 0;
704
705         if (in_session_id == 0) {
706                 int id;
707
708                 /* create a new session */
709                 session = talloc_zero(smb2req->sconn, struct smbd_smb2_session);
710                 if (session == NULL) {
711                         return NT_STATUS_NO_MEMORY;
712                 }
713                 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
714                 id = idr_get_new_random(smb2req->sconn->smb2.sessions.idtree,
715                                         session,
716                                         smb2req->sconn->smb2.sessions.limit);
717                 if (id == -1) {
718                         return NT_STATUS_INSUFFICIENT_RESOURCES;
719                 }
720                 session->vuid = id;
721
722                 session->tcons.idtree = idr_init(session);
723                 if (session->tcons.idtree == NULL) {
724                         return NT_STATUS_NO_MEMORY;
725                 }
726                 session->tcons.limit = 0x0000FFFE;
727                 session->tcons.list = NULL;
728
729                 DLIST_ADD_END(smb2req->sconn->smb2.sessions.list, session,
730                               struct smbd_smb2_session *);
731                 session->sconn = smb2req->sconn;
732                 talloc_set_destructor(session, smbd_smb2_session_destructor);
733         } else {
734                 void *p;
735
736                 /* lookup an existing session */
737                 p = idr_find(smb2req->sconn->smb2.sessions.idtree, in_session_id);
738                 if (p == NULL) {
739                         return NT_STATUS_USER_SESSION_DELETED;
740                 }
741                 session = talloc_get_type_abort(p, struct smbd_smb2_session);
742         }
743
744         if (NT_STATUS_IS_OK(session->status)) {
745                 return NT_STATUS_REQUEST_NOT_ACCEPTED;
746         }
747
748         if (in_security_buffer.data[0] == ASN1_APPLICATION(0)) {
749                 return smbd_smb2_spnego_negotiate(session,
750                                                 smb2req,
751                                                 in_security_mode,
752                                                 in_security_buffer,
753                                                 out_session_flags,
754                                                 out_security_buffer,
755                                                 out_session_id);
756         } else if (in_security_buffer.data[0] == ASN1_CONTEXT(1)) {
757                 return smbd_smb2_spnego_auth(session,
758                                                 smb2req,
759                                                 in_security_mode,
760                                                 in_security_buffer,
761                                                 out_session_flags,
762                                                 out_security_buffer,
763                                                 out_session_id);
764         } else if (strncmp((char *)(in_security_buffer.data), "NTLMSSP", 7) == 0) {
765                 return smbd_smb2_raw_ntlmssp_auth(session,
766                                                 smb2req,
767                                                 in_security_mode,
768                                                 in_security_buffer,
769                                                 out_session_flags,
770                                                 out_security_buffer,
771                                                 out_session_id);
772         }
773
774         /* Unknown packet type. */
775         DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
776                 (unsigned int)in_security_buffer.data[0] ));
777         TALLOC_FREE(session->auth_ntlmssp_state);
778         TALLOC_FREE(session);
779         return NT_STATUS_LOGON_FAILURE;
780 }
781
782 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
783 {
784         const uint8_t *inbody;
785         int i = req->current_idx;
786         DATA_BLOB outbody;
787         size_t expected_body_size = 0x04;
788         size_t body_size;
789
790         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
791                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
792         }
793
794         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
795
796         body_size = SVAL(inbody, 0x00);
797         if (body_size != expected_body_size) {
798                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
799         }
800
801         /*
802          * TODO: cancel all outstanding requests on the session
803          *       and delete all tree connections.
804          */
805         smbd_smb2_session_destructor(req->session);
806         /*
807          * we may need to sign the response, so we need to keep
808          * the session until the response is sent to the wire.
809          */
810         talloc_steal(req, req->session);
811
812         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
813         if (outbody.data == NULL) {
814                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
815         }
816
817         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
818         SSVAL(outbody.data, 0x02, 0);           /* reserved */
819
820         return smbd_smb2_request_done(req, outbody, NULL);
821 }