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