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