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