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