s3:smb2_server: use smbd_smb2_request_verify_sizes() in smb2_sesssetup.c
[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
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->msg_ctx, smb2req->sconn->sock, 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() == 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         }
236
237         session->session_key = session->session_info->session_key;
238
239         session->compat_vuser = talloc_zero(session, user_struct);
240         if (session->compat_vuser == NULL) {
241                 status = NT_STATUS_NO_MEMORY;
242                 goto fail;
243         }
244         session->compat_vuser->auth_ntlmssp_state = NULL;
245         session->compat_vuser->homes_snum = -1;
246         session->compat_vuser->session_info = session->session_info;
247         session->compat_vuser->session_keystr = NULL;
248         session->compat_vuser->vuid = session->vuid;
249         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
250
251         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
252                 session->compat_vuser->homes_snum =
253                         register_homes_share(session->session_info->unix_info->unix_name);
254         }
255
256         if (!session_claim(session->sconn, session->compat_vuser)) {
257                 DEBUG(1, ("smb2: Failed to claim session "
258                         "for vuid=%d\n",
259                         session->compat_vuser->vuid));
260                 goto fail;
261         }
262
263         session->status = NT_STATUS_OK;
264
265         /*
266          * we attach the session to the request
267          * so that the response can be signed
268          */
269         smb2req->session = session;
270         if (session->do_signing) {
271                 smb2req->do_signing = true;
272         }
273
274         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
275         status = NT_STATUS_OK;
276
277         /* wrap that up in a nice GSS-API wrapping */
278         ap_rep_wrapped = spnego_gen_krb5_wrap(talloc_tos(), ap_rep,
279                                 TOK_ID_KRB_AP_REP);
280
281         secblob_out = spnego_gen_auth_response(
282                                         talloc_tos(),
283                                         &ap_rep_wrapped,
284                                         status,
285                                         mechOID);
286
287         *out_security_buffer = data_blob_talloc(smb2req,
288                                                 secblob_out.data,
289                                                 secblob_out.length);
290         if (secblob_out.data && out_security_buffer->data == NULL) {
291                 status = NT_STATUS_NO_MEMORY;
292                 goto fail;
293         }
294
295         data_blob_free(&ap_rep);
296         data_blob_free(&ap_rep_wrapped);
297         data_blob_free(&ticket);
298         data_blob_free(&session_key);
299         data_blob_free(&secblob_out);
300
301         *out_session_id = session->vuid;
302
303         return NT_STATUS_OK;
304
305   fail:
306
307         data_blob_free(&ap_rep);
308         data_blob_free(&ap_rep_wrapped);
309         data_blob_free(&ticket);
310         data_blob_free(&session_key);
311         data_blob_free(&secblob_out);
312
313         ap_rep_wrapped = data_blob_null;
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         data_blob_free(&secblob_out);
324         return status;
325 }
326 #endif
327
328 static NTSTATUS smbd_smb2_spnego_negotiate(struct smbd_smb2_session *session,
329                                         struct smbd_smb2_request *smb2req,
330                                         uint8_t in_security_mode,
331                                         DATA_BLOB in_security_buffer,
332                                         uint16_t *out_session_flags,
333                                         DATA_BLOB *out_security_buffer,
334                                         uint64_t *out_session_id)
335 {
336         DATA_BLOB secblob_in = data_blob_null;
337         DATA_BLOB chal_out = data_blob_null;
338         char *kerb_mech = NULL;
339         NTSTATUS status;
340
341         /* Ensure we have no old NTLM state around. */
342         TALLOC_FREE(session->auth_ntlmssp_state);
343
344         status = parse_spnego_mechanisms(talloc_tos(), in_security_buffer,
345                         &secblob_in, &kerb_mech);
346         if (!NT_STATUS_IS_OK(status)) {
347                 goto out;
348         }
349
350 #ifdef HAVE_KRB5
351         if (kerb_mech && ((lp_security()==SEC_ADS) ||
352                                 USE_KERBEROS_KEYTAB) ) {
353                 status = smbd_smb2_session_setup_krb5(session,
354                                 smb2req,
355                                 in_security_mode,
356                                 &secblob_in,
357                                 kerb_mech,
358                                 out_session_flags,
359                                 out_security_buffer,
360                                 out_session_id);
361
362                 goto out;
363         }
364 #endif
365
366         if (kerb_mech) {
367                 /* The mechtoken is a krb5 ticket, but
368                  * we need to fall back to NTLM. */
369
370                 DEBUG(3,("smb2: Got krb5 ticket in SPNEGO "
371                         "but set to downgrade to NTLMSSP\n"));
372
373                 status = NT_STATUS_MORE_PROCESSING_REQUIRED;
374         } else {
375                 /* Fall back to NTLMSSP. */
376                 status = auth_ntlmssp_prepare(session->sconn->remote_address,
377                                             &session->auth_ntlmssp_state);
378                 if (!NT_STATUS_IS_OK(status)) {
379                         goto out;
380                 }
381
382                 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
383
384                 status = auth_ntlmssp_start(session->auth_ntlmssp_state);
385                 if (!NT_STATUS_IS_OK(status)) {
386                         goto out;
387                 }
388
389                 status = auth_ntlmssp_update(session->auth_ntlmssp_state,
390                                              talloc_tos(),
391                                              secblob_in,
392                                              &chal_out);
393         }
394
395         if (!NT_STATUS_IS_OK(status) &&
396                         !NT_STATUS_EQUAL(status,
397                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
398                 goto out;
399         }
400
401         *out_security_buffer = spnego_gen_auth_response(smb2req,
402                                                 &chal_out,
403                                                 status,
404                                                 OID_NTLMSSP);
405         if (out_security_buffer->data == NULL) {
406                 status = NT_STATUS_NO_MEMORY;
407                 goto out;
408         }
409         *out_session_id = session->vuid;
410
411   out:
412
413         data_blob_free(&secblob_in);
414         data_blob_free(&chal_out);
415         TALLOC_FREE(kerb_mech);
416         if (!NT_STATUS_IS_OK(status) &&
417                         !NT_STATUS_EQUAL(status,
418                                 NT_STATUS_MORE_PROCESSING_REQUIRED)) {
419                 TALLOC_FREE(session->auth_ntlmssp_state);
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         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
433             lp_server_signing() == Required) {
434                 session->do_signing = true;
435         }
436
437         if (security_session_user_level(session->session_info, NULL) < SECURITY_USER) {
438                 /* we map anonymous to guest internally */
439                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
440                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
441                 /* force no signing */
442                 session->do_signing = false;
443         }
444
445         session->session_key = session->session_info->session_key;
446
447         session->compat_vuser = talloc_zero(session, user_struct);
448         if (session->compat_vuser == NULL) {
449                 TALLOC_FREE(session->auth_ntlmssp_state);
450                 TALLOC_FREE(session);
451                 return NT_STATUS_NO_MEMORY;
452         }
453         session->compat_vuser->auth_ntlmssp_state = session->auth_ntlmssp_state;
454         session->compat_vuser->homes_snum = -1;
455         session->compat_vuser->session_info = session->session_info;
456         session->compat_vuser->session_keystr = NULL;
457         session->compat_vuser->vuid = session->vuid;
458         DLIST_ADD(session->sconn->smb1.sessions.validated_users, session->compat_vuser);
459
460         if (security_session_user_level(session->session_info, NULL) >= SECURITY_USER) {
461                 session->compat_vuser->homes_snum =
462                         register_homes_share(session->session_info->unix_info->unix_name);
463         }
464
465         if (!session_claim(session->sconn, session->compat_vuser)) {
466                 DEBUG(1, ("smb2: Failed to claim session "
467                         "for vuid=%d\n",
468                         session->compat_vuser->vuid));
469                 TALLOC_FREE(session->auth_ntlmssp_state);
470                 TALLOC_FREE(session);
471                 return NT_STATUS_LOGON_FAILURE;
472         }
473
474
475         session->status = NT_STATUS_OK;
476
477         /*
478          * we attach the session to the request
479          * so that the response can be signed
480          */
481         smb2req->session = session;
482         if (session->do_signing) {
483                 smb2req->do_signing = true;
484         }
485
486         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
487
488         *out_session_id = session->vuid;
489
490         return NT_STATUS_OK;
491 }
492
493 static NTSTATUS smbd_smb2_spnego_auth(struct smbd_smb2_session *session,
494                                         struct smbd_smb2_request *smb2req,
495                                         uint8_t in_security_mode,
496                                         DATA_BLOB in_security_buffer,
497                                         uint16_t *out_session_flags,
498                                         DATA_BLOB *out_security_buffer,
499                                         uint64_t *out_session_id)
500 {
501         DATA_BLOB auth = data_blob_null;
502         DATA_BLOB auth_out = data_blob_null;
503         NTSTATUS status;
504
505         if (!spnego_parse_auth(talloc_tos(), in_security_buffer, &auth)) {
506                 TALLOC_FREE(session);
507                 return NT_STATUS_LOGON_FAILURE;
508         }
509
510         if (auth.data[0] == ASN1_APPLICATION(0)) {
511                 /* Might be a second negTokenTarg packet */
512                 DATA_BLOB secblob_in = data_blob_null;
513                 char *kerb_mech = NULL;
514
515                 status = parse_spnego_mechanisms(talloc_tos(),
516                                 in_security_buffer,
517                                 &secblob_in, &kerb_mech);
518                 if (!NT_STATUS_IS_OK(status)) {
519                         TALLOC_FREE(session);
520                         return status;
521                 }
522
523 #ifdef HAVE_KRB5
524                 if (kerb_mech && ((lp_security()==SEC_ADS) ||
525                                         USE_KERBEROS_KEYTAB) ) {
526                         status = smbd_smb2_session_setup_krb5(session,
527                                         smb2req,
528                                         in_security_mode,
529                                         &secblob_in,
530                                         kerb_mech,
531                                         out_session_flags,
532                                         out_security_buffer,
533                                         out_session_id);
534
535                         data_blob_free(&secblob_in);
536                         TALLOC_FREE(kerb_mech);
537                         if (!NT_STATUS_IS_OK(status)) {
538                                 TALLOC_FREE(session);
539                         }
540                         return status;
541                 }
542 #endif
543
544                 /* Can't blunder into NTLMSSP auth if we have
545                  * a krb5 ticket. */
546
547                 if (kerb_mech) {
548                         DEBUG(3,("smb2: network "
549                                 "misconfiguration, client sent us a "
550                                 "krb5 ticket and kerberos security "
551                                 "not enabled\n"));
552                         TALLOC_FREE(session);
553                         data_blob_free(&secblob_in);
554                         TALLOC_FREE(kerb_mech);
555                         return NT_STATUS_LOGON_FAILURE;
556                 }
557
558                 data_blob_free(&secblob_in);
559         }
560
561         if (session->auth_ntlmssp_state == NULL) {
562                 status = auth_ntlmssp_prepare(session->sconn->remote_address,
563                                             &session->auth_ntlmssp_state);
564                 if (!NT_STATUS_IS_OK(status)) {
565                         data_blob_free(&auth);
566                         TALLOC_FREE(session);
567                         return status;
568                 }
569
570                 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
571
572                 status = auth_ntlmssp_start(session->auth_ntlmssp_state);
573                 if (!NT_STATUS_IS_OK(status)) {
574                         data_blob_free(&auth);
575                         TALLOC_FREE(session);
576                         return status;
577                 }
578         }
579
580         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
581                                      talloc_tos(), auth,
582                                      &auth_out);
583         /* If status is NT_STATUS_OK then we need to get the token.
584          * Map to guest is now internal to auth_ntlmssp */
585         if (NT_STATUS_IS_OK(status)) {
586                 status = auth_ntlmssp_session_info(session,
587                                                    session->auth_ntlmssp_state,
588                                                    &session->session_info);
589         }
590
591         if (!NT_STATUS_IS_OK(status) &&
592                         !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
593                 TALLOC_FREE(session->auth_ntlmssp_state);
594                 data_blob_free(&auth);
595                 TALLOC_FREE(session);
596                 return status;
597         }
598
599         data_blob_free(&auth);
600
601         *out_security_buffer = spnego_gen_auth_response(smb2req,
602                                 &auth_out, status, NULL);
603
604         if (out_security_buffer->data == NULL) {
605                 TALLOC_FREE(session->auth_ntlmssp_state);
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         if (session->auth_ntlmssp_state == NULL) {
636                 status = auth_ntlmssp_prepare(session->sconn->remote_address,
637                                             &session->auth_ntlmssp_state);
638                 if (!NT_STATUS_IS_OK(status)) {
639                         TALLOC_FREE(session);
640                         return status;
641                 }
642
643                 auth_ntlmssp_want_feature(session->auth_ntlmssp_state, NTLMSSP_FEATURE_SESSION_KEY);
644
645                 if (session->sconn->use_gensec_hook) {
646                         status = auth_generic_start(session->auth_ntlmssp_state, GENSEC_OID_SPNEGO);
647                 } else {
648                         status = auth_ntlmssp_start(session->auth_ntlmssp_state);
649                 }
650                 if (!NT_STATUS_IS_OK(status)) {
651                         TALLOC_FREE(session);
652                         return status;
653                 }
654         }
655
656         /* RAW NTLMSSP */
657         status = auth_ntlmssp_update(session->auth_ntlmssp_state,
658                                      smb2req,
659                                      in_security_buffer,
660                                      out_security_buffer);
661
662         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
663                 *out_session_id = session->vuid;
664                 return status;
665         }
666
667         status = auth_ntlmssp_session_info(session,
668                                            session->auth_ntlmssp_state,
669                                            &session->session_info);
670
671         if (!NT_STATUS_IS_OK(status)) {
672                 TALLOC_FREE(session->auth_ntlmssp_state);
673                 TALLOC_FREE(session);
674                 return status;
675         }
676         *out_session_id = session->vuid;
677
678         return smbd_smb2_common_ntlmssp_auth_return(session,
679                                                 smb2req,
680                                                 in_security_mode,
681                                                 in_security_buffer,
682                                                 out_session_flags,
683                                                 out_session_id);
684 }
685
686 static NTSTATUS smbd_smb2_session_setup(struct smbd_smb2_request *smb2req,
687                                         uint64_t in_session_id,
688                                         uint8_t in_security_mode,
689                                         DATA_BLOB in_security_buffer,
690                                         uint16_t *out_session_flags,
691                                         DATA_BLOB *out_security_buffer,
692                                         uint64_t *out_session_id)
693 {
694         struct smbd_smb2_session *session;
695
696         *out_session_flags = 0;
697         *out_session_id = 0;
698
699         if (in_session_id == 0) {
700                 int id;
701
702                 /* create a new session */
703                 session = talloc_zero(smb2req->sconn, struct smbd_smb2_session);
704                 if (session == NULL) {
705                         return NT_STATUS_NO_MEMORY;
706                 }
707                 session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
708                 id = idr_get_new_random(smb2req->sconn->smb2.sessions.idtree,
709                                         session,
710                                         smb2req->sconn->smb2.sessions.limit);
711                 if (id == -1) {
712                         return NT_STATUS_INSUFFICIENT_RESOURCES;
713                 }
714                 session->vuid = id;
715
716                 session->tcons.idtree = idr_init(session);
717                 if (session->tcons.idtree == NULL) {
718                         return NT_STATUS_NO_MEMORY;
719                 }
720                 session->tcons.limit = 0x0000FFFE;
721                 session->tcons.list = NULL;
722
723                 DLIST_ADD_END(smb2req->sconn->smb2.sessions.list, session,
724                               struct smbd_smb2_session *);
725                 session->sconn = smb2req->sconn;
726                 talloc_set_destructor(session, smbd_smb2_session_destructor);
727         } else {
728                 void *p;
729
730                 /* lookup an existing session */
731                 p = idr_find(smb2req->sconn->smb2.sessions.idtree, in_session_id);
732                 if (p == NULL) {
733                         return NT_STATUS_USER_SESSION_DELETED;
734                 }
735                 session = talloc_get_type_abort(p, struct smbd_smb2_session);
736         }
737
738         if (NT_STATUS_IS_OK(session->status)) {
739                 return NT_STATUS_REQUEST_NOT_ACCEPTED;
740         }
741
742         /* Handle either raw NTLMSSP or hand off the whole blob to
743          * GENSEC.  The processing at this layer is essentially
744          * identical regardless.  In particular, both rely only on the
745          * status code (not the contents of the packet) and do not
746          * wrap the result */
747         if (session->sconn->use_gensec_hook
748             || ntlmssp_blob_matches_magic(&in_security_buffer)) {
749                 return smbd_smb2_raw_ntlmssp_auth(session,
750                                                 smb2req,
751                                                 in_security_mode,
752                                                 in_security_buffer,
753                                                 out_session_flags,
754                                                 out_security_buffer,
755                                                 out_session_id);
756         } else if (in_security_buffer.data[0] == ASN1_APPLICATION(0)) {
757                 return smbd_smb2_spnego_negotiate(session,
758                                                 smb2req,
759                                                 in_security_mode,
760                                                 in_security_buffer,
761                                                 out_session_flags,
762                                                 out_security_buffer,
763                                                 out_session_id);
764         } else if (in_security_buffer.data[0] == ASN1_CONTEXT(1)) {
765                 return smbd_smb2_spnego_auth(session,
766                                                 smb2req,
767                                                 in_security_mode,
768                                                 in_security_buffer,
769                                                 out_session_flags,
770                                                 out_security_buffer,
771                                                 out_session_id);
772         }
773
774         /* Unknown packet type. */
775         DEBUG(1,("Unknown packet type %u in smb2 sessionsetup\n",
776                 (unsigned int)in_security_buffer.data[0] ));
777         TALLOC_FREE(session->auth_ntlmssp_state);
778         TALLOC_FREE(session);
779         return NT_STATUS_LOGON_FAILURE;
780 }
781
782 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
783 {
784         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 }