gensec: clarify memory ownership for gensec_session_info() and gensec_session_key()
[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 static NTSTATUS setup_ntlmssp_session_info(struct smbd_smb2_session *session,
155                                 NTSTATUS status)
156 {
157         if (NT_STATUS_IS_OK(status)) {
158                 status = auth_ntlmssp_steal_session_info(session,
159                                 session->auth_ntlmssp_state,
160                                 &session->session_info);
161         } else {
162                 /* Note that this session_info won't have a session
163                  * key.  But for map to guest, that's exactly the right
164                  * thing - we can't reasonably guess the key the
165                  * client wants, as the password was wrong */
166                 status = do_map_to_guest(status,
167                         &session->session_info,
168                         auth_ntlmssp_get_username(session->auth_ntlmssp_state),
169                         auth_ntlmssp_get_domain(session->auth_ntlmssp_state));
170         }
171         return status;
172 }
173
174 #ifdef HAVE_KRB5
175 static NTSTATUS smbd_smb2_session_setup_krb5(struct smbd_smb2_session *session,
176                                         struct smbd_smb2_request *smb2req,
177                                         uint8_t in_security_mode,
178                                         const DATA_BLOB *secblob,
179                                         const char *mechOID,
180                                         uint16_t *out_session_flags,
181                                         DATA_BLOB *out_security_buffer,
182                                         uint64_t *out_session_id)
183 {
184         DATA_BLOB ap_rep = data_blob_null;
185         DATA_BLOB ap_rep_wrapped = data_blob_null;
186         DATA_BLOB ticket = data_blob_null;
187         DATA_BLOB session_key = data_blob_null;
188         DATA_BLOB secblob_out = data_blob_null;
189         uint8 tok_id[2];
190         struct PAC_LOGON_INFO *logon_info = NULL;
191         char *principal = NULL;
192         char *user = NULL;
193         char *domain = NULL;
194         struct passwd *pw = NULL;
195         NTSTATUS status;
196         char *real_username;
197         fstring tmp;
198         bool username_was_mapped = false;
199         bool map_domainuser_to_guest = false;
200
201         if (!spnego_parse_krb5_wrap(talloc_tos(), *secblob, &ticket, tok_id)) {
202                 status = NT_STATUS_LOGON_FAILURE;
203                 goto fail;
204         }
205
206         status = ads_verify_ticket(smb2req, lp_realm(), 0, &ticket,
207                                    &principal, &logon_info, &ap_rep,
208                                    &session_key, true);
209
210         if (!NT_STATUS_IS_OK(status)) {
211                 DEBUG(1,("smb2: Failed to verify incoming ticket with error %s!\n",
212                         nt_errstr(status)));
213                 if (!NT_STATUS_EQUAL(status, NT_STATUS_TIME_DIFFERENCE_AT_DC)) {
214                         status = NT_STATUS_LOGON_FAILURE;
215                 }
216                 goto fail;
217         }
218
219         status = get_user_from_kerberos_info(talloc_tos(),
220                                              session->sconn->remote_hostname,
221                                              principal, logon_info,
222                                              &username_was_mapped,
223                                              &map_domainuser_to_guest,
224                                              &user, &domain,
225                                              &real_username, &pw);
226         if (!NT_STATUS_IS_OK(status)) {
227                 goto fail;
228         }
229
230         /* save the PAC data if we have it */
231         if (logon_info) {
232                 netsamlogon_cache_store(user, &logon_info->info3);
233         }
234
235         /* setup the string used by %U */
236         sub_set_smb_name(real_username);
237
238         /* reload services so that the new %U is taken into account */
239         reload_services(smb2req->sconn->msg_ctx, smb2req->sconn->sock, true);
240
241         status = make_session_info_krb5(session,
242                                         user, domain, real_username, pw,
243                                         logon_info, map_domainuser_to_guest,
244                                         username_was_mapped,
245                                         &session_key,
246                                         &session->session_info);
247         if (!NT_STATUS_IS_OK(status)) {
248                 DEBUG(1, ("smb2: make_server_info_krb5 failed\n"));
249                 goto fail;
250         }
251
252         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
253              lp_server_signing() == Required) {
254                 session->do_signing = true;
255         }
256
257         if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
258                 /* we map anonymous to guest internally */
259                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
260                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
261                 /* force no signing */
262                 session->do_signing = false;
263         }
264
265         session->session_key = session->session_info->session_key;
266
267         session->compat_vuser = talloc_zero(session, user_struct);
268         if (session->compat_vuser == NULL) {
269                 status = NT_STATUS_NO_MEMORY;
270                 goto fail;
271         }
272         session->compat_vuser->auth_ntlmssp_state = NULL;
273         session->compat_vuser->homes_snum = -1;
274         session->compat_vuser->session_info = session->session_info;
275         session->compat_vuser->session_keystr = NULL;
276         session->compat_vuser->vuid = session->vuid;
277         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
278
279         /* This is a potentially untrusted username */
280         alpha_strcpy(tmp, user, ". _-$", sizeof(tmp));
281         session->session_info->unix_info->sanitized_username =
282                                 talloc_strdup(session->session_info, tmp);
283
284         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
285                 session->compat_vuser->homes_snum =
286                         register_homes_share(session->session_info->unix_info->unix_name);
287         }
288
289         if (!session_claim(session->sconn, session->compat_vuser)) {
290                 DEBUG(1, ("smb2: Failed to claim session "
291                         "for vuid=%d\n",
292                         session->compat_vuser->vuid));
293                 goto fail;
294         }
295
296         session->status = NT_STATUS_OK;
297
298         /*
299          * we attach the session to the request
300          * so that the response can be signed
301          */
302         smb2req->session = session;
303         if (session->do_signing) {
304                 smb2req->do_signing = true;
305         }
306
307         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
308         status = NT_STATUS_OK;
309
310         /* wrap that up in a nice GSS-API wrapping */
311         ap_rep_wrapped = spnego_gen_krb5_wrap(talloc_tos(), ap_rep,
312                                 TOK_ID_KRB_AP_REP);
313
314         secblob_out = spnego_gen_auth_response(
315                                         talloc_tos(),
316                                         &ap_rep_wrapped,
317                                         status,
318                                         mechOID);
319
320         *out_security_buffer = data_blob_talloc(smb2req,
321                                                 secblob_out.data,
322                                                 secblob_out.length);
323         if (secblob_out.data && out_security_buffer->data == NULL) {
324                 status = NT_STATUS_NO_MEMORY;
325                 goto fail;
326         }
327
328         data_blob_free(&ap_rep);
329         data_blob_free(&ap_rep_wrapped);
330         data_blob_free(&ticket);
331         data_blob_free(&session_key);
332         data_blob_free(&secblob_out);
333
334         *out_session_id = session->vuid;
335
336         return NT_STATUS_OK;
337
338   fail:
339
340         data_blob_free(&ap_rep);
341         data_blob_free(&ap_rep_wrapped);
342         data_blob_free(&ticket);
343         data_blob_free(&session_key);
344         data_blob_free(&secblob_out);
345
346         ap_rep_wrapped = data_blob_null;
347         secblob_out = spnego_gen_auth_response(
348                                         talloc_tos(),
349                                         &ap_rep_wrapped,
350                                         status,
351                                         mechOID);
352
353         *out_security_buffer = data_blob_talloc(smb2req,
354                                                 secblob_out.data,
355                                                 secblob_out.length);
356         data_blob_free(&secblob_out);
357         return status;
358 }
359 #endif
360
361 static NTSTATUS smbd_smb2_spnego_negotiate(struct smbd_smb2_session *session,
362                                         struct smbd_smb2_request *smb2req,
363                                         uint8_t in_security_mode,
364                                         DATA_BLOB in_security_buffer,
365                                         uint16_t *out_session_flags,
366                                         DATA_BLOB *out_security_buffer,
367                                         uint64_t *out_session_id)
368 {
369         DATA_BLOB secblob_in = data_blob_null;
370         DATA_BLOB chal_out = data_blob_null;
371         char *kerb_mech = NULL;
372         NTSTATUS status;
373
374         /* Ensure we have no old NTLM state around. */
375         TALLOC_FREE(session->auth_ntlmssp_state);
376
377         status = parse_spnego_mechanisms(talloc_tos(), in_security_buffer,
378                         &secblob_in, &kerb_mech);
379         if (!NT_STATUS_IS_OK(status)) {
380                 goto out;
381         }
382
383 #ifdef HAVE_KRB5
384         if (kerb_mech && ((lp_security()==SEC_ADS) ||
385                                 USE_KERBEROS_KEYTAB) ) {
386                 status = smbd_smb2_session_setup_krb5(session,
387                                 smb2req,
388                                 in_security_mode,
389                                 &secblob_in,
390                                 kerb_mech,
391                                 out_session_flags,
392                                 out_security_buffer,
393                                 out_session_id);
394
395                 goto out;
396         }
397 #endif
398
399         if (kerb_mech) {
400                 /* The mechtoken is a krb5 ticket, but
401                  * we need to fall back to NTLM. */
402
403                 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
404                         "but set to downgrade to NTLMSSP\n"));
405
406                 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
407         } else {
408                 /* Fall back to NTLMSSP. */
409                 status = auth_ntlmssp_start(session->sconn->remote_address,
410                                             &session->auth_ntlmssp_state);
411                 if (!NT_STATUS_IS_OK(status)) {
412                         goto out;
413                 }
414
415                 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
416                                              secblob_in,
417                                              &chal_out);
418         }
419
420         if (!NT_STATUS_IS_OK(status) &&
421                         !NT_STATUS_EQUAL(status,
422                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
423                 goto out;
424         }
425
426         *out_security_buffer = spnego_gen_auth_response(smb2req,
427                                                 &chal_out,
428                                                 status,
429                                                 OID_NTLMSSP);
430         if (out_security_buffer->data == NULL) {
431                 status = NT_STATUS_NO_MEMORY;
432                 goto out;
433         }
434         *out_session_id = session->vuid;
435
436   out:
437
438         data_blob_free(&secblob_in);
439         data_blob_free(&chal_out);
440         TALLOC_FREE(kerb_mech);
441         if (!NT_STATUS_IS_OK(status) &&
442                         !NT_STATUS_EQUAL(status,
443                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
444                 TALLOC_FREE(session->auth_ntlmssp_state);
445                 TALLOC_FREE(session);
446         }
447         return status;
448 }
449
450 static NTSTATUS smbd_smb2_common_ntlmssp_auth_return(struct smbd_smb2_session *session,
451                                         struct smbd_smb2_request *smb2req,
452                                         uint8_t in_security_mode,
453                                         DATA_BLOB in_security_buffer,
454                                         uint16_t *out_session_flags,
455                                         uint64_t *out_session_id)
456 {
457         fstring tmp;
458
459         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
460             lp_server_signing() == Required) {
461                 session->do_signing = true;
462         }
463
464         if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
465                 /* we map anonymous to guest internally */
466                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
467                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
468                 /* force no signing */
469                 session->do_signing = false;
470         }
471
472         session->session_key = session->session_info->session_key;
473
474         session->compat_vuser = talloc_zero(session, user_struct);
475         if (session->compat_vuser == NULL) {
476                 TALLOC_FREE(session->auth_ntlmssp_state);
477                 TALLOC_FREE(session);
478                 return NT_STATUS_NO_MEMORY;
479         }
480         session->compat_vuser->auth_ntlmssp_state = session->auth_ntlmssp_state;
481         session->compat_vuser->homes_snum = -1;
482         session->compat_vuser->session_info = session->session_info;
483         session->compat_vuser->session_keystr = NULL;
484         session->compat_vuser->vuid = session->vuid;
485         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
486
487         /* This is a potentially untrusted username */
488         alpha_strcpy(tmp,
489                      auth_ntlmssp_get_username(session->auth_ntlmssp_state),
490                      ". _-$",
491                      sizeof(tmp));
492         session->session_info->unix_info->sanitized_username = talloc_strdup(
493                 session->session_info, tmp);
494
495         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
496                 session->compat_vuser->homes_snum =
497                         register_homes_share(session->session_info->unix_info->unix_name);
498         }
499
500         if (!session_claim(session->sconn, session->compat_vuser)) {
501                 DEBUG(1, ("smb2: Failed to claim session "
502                         "for vuid=%d\n",
503                         session->compat_vuser->vuid));
504                 TALLOC_FREE(session->auth_ntlmssp_state);
505                 TALLOC_FREE(session);
506                 return NT_STATUS_LOGON_FAILURE;
507         }
508
509
510         session->status = NT_STATUS_OK;
511
512         /*
513          * we attach the session to the request
514          * so that the response can be signed
515          */
516         smb2req->session = session;
517         if (session->do_signing) {
518                 smb2req->do_signing = true;
519         }
520
521         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
522
523         *out_session_id = session->vuid;
524
525         return NT_STATUS_OK;
526 }
527
528 static NTSTATUS smbd_smb2_spnego_auth(struct smbd_smb2_session *session,
529                                         struct smbd_smb2_request *smb2req,
530                                         uint8_t in_security_mode,
531                                         DATA_BLOB in_security_buffer,
532                                         uint16_t *out_session_flags,
533                                         DATA_BLOB *out_security_buffer,
534                                         uint64_t *out_session_id)
535 {
536         DATA_BLOB auth = data_blob_null;
537         DATA_BLOB auth_out = data_blob_null;
538         NTSTATUS status;
539
540         if (!spnego_parse_auth(talloc_tos(), in_security_buffer, &auth)) {
541                 TALLOC_FREE(session);
542                 return NT_STATUS_LOGON_FAILURE;
543         }
544
545         if (auth.data[0] == ASN1_APPLICATION(0)) {
546                 /* Might be a second negTokenTarg packet */
547                 DATA_BLOB secblob_in = data_blob_null;
548                 char *kerb_mech = NULL;
549
550                 status = parse_spnego_mechanisms(talloc_tos(),
551                                 in_security_buffer,
552                                 &secblob_in, &kerb_mech);
553                 if (!NT_STATUS_IS_OK(status)) {
554                         TALLOC_FREE(session);
555                         return status;
556                 }
557
558 #ifdef HAVE_KRB5
559                 if (kerb_mech && ((lp_security()==SEC_ADS) ||
560                                         USE_KERBEROS_KEYTAB) ) {
561                         status = smbd_smb2_session_setup_krb5(session,
562                                         smb2req,
563                                         in_security_mode,
564                                         &secblob_in,
565                                         kerb_mech,
566                                         out_session_flags,
567                                         out_security_buffer,
568                                         out_session_id);
569
570                         data_blob_free(&secblob_in);
571                         TALLOC_FREE(kerb_mech);
572                         if (!NT_STATUS_IS_OK(status)) {
573                                 TALLOC_FREE(session);
574                         }
575                         return status;
576                 }
577 #endif
578
579                 /* Can't blunder into NTLMSSP auth if we have
580                  * a krb5 ticket. */
581
582                 if (kerb_mech) {
583                         DEBUG(3,("smb2: network "
584                                 "misconfiguration, client sent us a "
585                                 "krb5 ticket and kerberos security "
586                                 "not enabled\n"));
587                         TALLOC_FREE(session);
588                         data_blob_free(&secblob_in);
589                         TALLOC_FREE(kerb_mech);
590                         return NT_STATUS_LOGON_FAILURE;
591                 }
592
593                 data_blob_free(&secblob_in);
594         }
595
596         if (session->auth_ntlmssp_state == NULL) {
597                 status = auth_ntlmssp_start(session->sconn->remote_address,
598                                             &session->auth_ntlmssp_state);
599                 if (!NT_STATUS_IS_OK(status)) {
600                         data_blob_free(&auth);
601                         TALLOC_FREE(session);
602                         return status;
603                 }
604         }
605
606         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
607                                      auth,
608                                      &auth_out);
609         /* We need to call setup_ntlmssp_session_info() if status==NT_STATUS_OK,
610            or if status is anything except NT_STATUS_MORE_PROCESSING_REQUIRED,
611            as this can trigger map to guest. */
612         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
613                 status = setup_ntlmssp_session_info(session, status);
614         }
615
616         if (!NT_STATUS_IS_OK(status) &&
617                         !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
618                 TALLOC_FREE(session->auth_ntlmssp_state);
619                 data_blob_free(&auth);
620                 TALLOC_FREE(session);
621                 return status;
622         }
623
624         data_blob_free(&auth);
625
626         *out_security_buffer = spnego_gen_auth_response(smb2req,
627                                 &auth_out, status, NULL);
628
629         if (out_security_buffer->data == NULL) {
630                 TALLOC_FREE(session->auth_ntlmssp_state);
631                 TALLOC_FREE(session);
632                 return NT_STATUS_NO_MEMORY;
633         }
634
635         *out_session_id = session->vuid;
636
637         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
638                 return NT_STATUS_MORE_PROCESSING_REQUIRED;
639         }
640
641         /* We're done - claim the session. */
642         return smbd_smb2_common_ntlmssp_auth_return(session,
643                                                 smb2req,
644                                                 in_security_mode,
645                                                 in_security_buffer,
646                                                 out_session_flags,
647                                                 out_session_id);
648 }
649
650 static NTSTATUS smbd_smb2_raw_ntlmssp_auth(struct smbd_smb2_session *session,
651                                         struct smbd_smb2_request *smb2req,
652                                         uint8_t in_security_mode,
653                                         DATA_BLOB in_security_buffer,
654                                         uint16_t *out_session_flags,
655                                         DATA_BLOB *out_security_buffer,
656                                         uint64_t *out_session_id)
657 {
658         NTSTATUS status;
659         DATA_BLOB secblob_out = data_blob_null;
660
661         if (session->auth_ntlmssp_state == NULL) {
662                 status = auth_ntlmssp_start(session->sconn->remote_address,
663                                             &session->auth_ntlmssp_state);
664                 if (!NT_STATUS_IS_OK(status)) {
665                         TALLOC_FREE(session);
666                         return status;
667                 }
668         }
669
670         /* RAW NTLMSSP */
671         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
672                                      in_security_buffer,
673                                      &secblob_out);
674
675         if (NT_STATUS_IS_OK(status) ||
676                         NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
677                 *out_security_buffer = data_blob_talloc(smb2req,
678                                                 secblob_out.data,
679                                                 secblob_out.length);
680                 if (secblob_out.data && out_security_buffer->data == NULL) {
681                         TALLOC_FREE(session->auth_ntlmssp_state);
682                         TALLOC_FREE(session);
683                         return NT_STATUS_NO_MEMORY;
684                 }
685         }
686
687         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
688                 *out_session_id = session->vuid;
689                 return status;
690         }
691
692         status = setup_ntlmssp_session_info(session, status);
693
694         if (!NT_STATUS_IS_OK(status)) {
695                 TALLOC_FREE(session->auth_ntlmssp_state);
696                 TALLOC_FREE(session);
697                 return status;
698         }
699         *out_session_id = session->vuid;
700
701         return smbd_smb2_common_ntlmssp_auth_return(session,
702                                                 smb2req,
703                                                 in_security_mode,
704                                                 in_security_buffer,
705                                                 out_session_flags,
706                                                 out_session_id);
707 }
708
709 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
710                                         uint64_t in_session_id,
711                                         uint8_t in_security_mode,
712                                         DATA_BLOB in_security_buffer,
713                                         uint16_t *out_session_flags,
714                                         DATA_BLOB *out_security_buffer,
715                                         uint64_t *out_session_id)
716 {
717         struct smbd_smb2_session *session;
718
719         *out_session_flags = 0;
720         *out_session_id = 0;
721
722         if (in_session_id == 0) {
723                 int id;
724
725                 /* create a new session */
726                 session = talloc_zero(smb2req->sconn, struct smbd_smb2_session);
727                 if (session == NULL) {
728                         return NT_STATUS_NO_MEMORY;
729                 }
730                 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
731                 id = idr_get_new_random(smb2req->sconn->smb2.sessions.idtree,
732                                         session,
733                                         smb2req->sconn->smb2.sessions.limit);
734                 if (id == -1) {
735                         return NT_STATUS_INSUFFICIENT_RESOURCES;
736                 }
737                 session->vuid = id;
738
739                 session->tcons.idtree = idr_init(session);
740                 if (session->tcons.idtree == NULL) {
741                         return NT_STATUS_NO_MEMORY;
742                 }
743                 session->tcons.limit = 0x0000FFFE;
744                 session->tcons.list = NULL;
745
746                 DLIST_ADD_END(smb2req->sconn->smb2.sessions.list, session,
747                               struct smbd_smb2_session *);
748                 session->sconn = smb2req->sconn;
749                 talloc_set_destructor(session, smbd_smb2_session_destructor);
750         } else {
751                 void *p;
752
753                 /* lookup an existing session */
754                 p = idr_find(smb2req->sconn->smb2.sessions.idtree, in_session_id);
755                 if (p == NULL) {
756                         return NT_STATUS_USER_SESSION_DELETED;
757                 }
758                 session = talloc_get_type_abort(p, struct smbd_smb2_session);
759         }
760
761         if (NT_STATUS_IS_OK(session->status)) {
762                 return NT_STATUS_REQUEST_NOT_ACCEPTED;
763         }
764
765         if (in_security_buffer.data[0] == ASN1_APPLICATION(0)) {
766                 return smbd_smb2_spnego_negotiate(session,
767                                                 smb2req,
768                                                 in_security_mode,
769                                                 in_security_buffer,
770                                                 out_session_flags,
771                                                 out_security_buffer,
772                                                 out_session_id);
773         } else if (in_security_buffer.data[0] == ASN1_CONTEXT(1)) {
774                 return smbd_smb2_spnego_auth(session,
775                                                 smb2req,
776                                                 in_security_mode,
777                                                 in_security_buffer,
778                                                 out_session_flags,
779                                                 out_security_buffer,
780                                                 out_session_id);
781         } else if (strncmp((char *)(in_security_buffer.data), "NTLMSSP", 7) == 0) {
782                 return smbd_smb2_raw_ntlmssp_auth(session,
783                                                 smb2req,
784                                                 in_security_mode,
785                                                 in_security_buffer,
786                                                 out_session_flags,
787                                                 out_security_buffer,
788                                                 out_session_id);
789         }
790
791         /* Unknown packet type. */
792         DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
793                 (unsigned int)in_security_buffer.data[0] ));
794         TALLOC_FREE(session->auth_ntlmssp_state);
795         TALLOC_FREE(session);
796         return NT_STATUS_LOGON_FAILURE;
797 }
798
799 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
800 {
801         const uint8_t *inbody;
802         int i = req->current_idx;
803         DATA_BLOB outbody;
804         size_t expected_body_size = 0x04;
805         size_t body_size;
806
807         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
808                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
809         }
810
811         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
812
813         body_size = SVAL(inbody, 0x00);
814         if (body_size != expected_body_size) {
815                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
816         }
817
818         /*
819          * TODO: cancel all outstanding requests on the session
820          *       and delete all tree connections.
821          */
822         smbd_smb2_session_destructor(req->session);
823         /*
824          * we may need to sign the response, so we need to keep
825          * the session until the response is sent to the wire.
826          */
827         talloc_steal(req, req->session);
828
829         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
830         if (outbody.data == NULL) {
831                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
832         }
833
834         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
835         SSVAL(outbody.data, 0x02, 0);           /* reserved */
836
837         return smbd_smb2_request_done(req, outbody, NULL);
838 }