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