s3:smbd: remember the time of the session setup auth_time
[kai/samba-autobuild/.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 "../auth/gensec/gensec.h"
27 #include "auth.h"
28 #include "../lib/tsocket/tsocket.h"
29 #include "../libcli/security/security.h"
30 #include "../lib/util/tevent_ntstatus.h"
31
32 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
33                                         struct tevent_context *ev,
34                                         struct smbd_smb2_request *smb2req,
35                                         uint64_t in_session_id,
36                                         uint8_t in_flags,
37                                         uint8_t in_security_mode,
38                                         uint64_t in_previous_session_id,
39                                         DATA_BLOB in_security_buffer);
40 static NTSTATUS smbd_smb2_session_setup_recv(struct tevent_req *req,
41                                         uint16_t *out_session_flags,
42                                         TALLOC_CTX *mem_ctx,
43                                         DATA_BLOB *out_security_buffer,
44                                         uint64_t *out_session_id);
45
46 static void smbd_smb2_request_sesssetup_done(struct tevent_req *subreq);
47
48 NTSTATUS smbd_smb2_request_process_sesssetup(struct smbd_smb2_request *smb2req)
49 {
50         const uint8_t *inhdr;
51         const uint8_t *inbody;
52         uint64_t in_session_id;
53         uint8_t in_flags;
54         uint8_t in_security_mode;
55         uint64_t in_previous_session_id;
56         uint16_t in_security_offset;
57         uint16_t in_security_length;
58         DATA_BLOB in_security_buffer;
59         NTSTATUS status;
60         struct tevent_req *subreq;
61
62         status = smbd_smb2_request_verify_sizes(smb2req, 0x19);
63         if (!NT_STATUS_IS_OK(status)) {
64                 return smbd_smb2_request_error(smb2req, status);
65         }
66         inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
67         inbody = SMBD_SMB2_IN_BODY_PTR(smb2req);
68
69         in_session_id = BVAL(inhdr, SMB2_HDR_SESSION_ID);
70
71         in_flags = CVAL(inbody, 0x02);
72         in_security_mode = CVAL(inbody, 0x03);
73         /* Capabilities = IVAL(inbody, 0x04) */
74         /* Channel = IVAL(inbody, 0x08) */
75         in_security_offset = SVAL(inbody, 0x0C);
76         in_security_length = SVAL(inbody, 0x0E);
77         in_previous_session_id = BVAL(inbody, 0x10);
78
79         if (in_security_offset != (SMB2_HDR_BODY + SMBD_SMB2_IN_BODY_LEN(smb2req))) {
80                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
81         }
82
83         if (in_security_length > SMBD_SMB2_IN_DYN_LEN(smb2req)) {
84                 return smbd_smb2_request_error(smb2req, NT_STATUS_INVALID_PARAMETER);
85         }
86
87         in_security_buffer.data = SMBD_SMB2_IN_DYN_PTR(smb2req);
88         in_security_buffer.length = in_security_length;
89
90         subreq = smbd_smb2_session_setup_send(smb2req,
91                                               smb2req->sconn->ev_ctx,
92                                               smb2req,
93                                               in_session_id,
94                                               in_flags,
95                                               in_security_mode,
96                                               in_previous_session_id,
97                                               in_security_buffer);
98         if (subreq == NULL) {
99                 return smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
100         }
101         tevent_req_set_callback(subreq, smbd_smb2_request_sesssetup_done, smb2req);
102
103         return smbd_smb2_request_pending_queue(smb2req, subreq, 500);
104 }
105
106 static void smbd_smb2_request_sesssetup_done(struct tevent_req *subreq)
107 {
108         struct smbd_smb2_request *smb2req =
109                 tevent_req_callback_data(subreq,
110                 struct smbd_smb2_request);
111         uint8_t *outhdr;
112         DATA_BLOB outbody;
113         DATA_BLOB outdyn;
114         uint16_t out_session_flags = 0;
115         uint64_t out_session_id = 0;
116         uint16_t out_security_offset;
117         DATA_BLOB out_security_buffer = data_blob_null;
118         NTSTATUS status;
119         NTSTATUS error; /* transport error */
120
121         status = smbd_smb2_session_setup_recv(subreq,
122                                               &out_session_flags,
123                                               smb2req,
124                                               &out_security_buffer,
125                                               &out_session_id);
126         TALLOC_FREE(subreq);
127         if (!NT_STATUS_IS_OK(status) &&
128             !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
129                 status = nt_status_squash(status);
130                 error = smbd_smb2_request_error(smb2req, status);
131                 if (!NT_STATUS_IS_OK(error)) {
132                         smbd_server_connection_terminate(smb2req->xconn,
133                                                          nt_errstr(error));
134                         return;
135                 }
136                 return;
137         }
138
139         out_security_offset = SMB2_HDR_BODY + 0x08;
140
141         outhdr = SMBD_SMB2_OUT_HDR_PTR(smb2req);
142
143         outbody = smbd_smb2_generate_outbody(smb2req, 0x08);
144         if (outbody.data == NULL) {
145                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
146                 if (!NT_STATUS_IS_OK(error)) {
147                         smbd_server_connection_terminate(smb2req->xconn,
148                                                          nt_errstr(error));
149                         return;
150                 }
151                 return;
152         }
153
154         SBVAL(outhdr, SMB2_HDR_SESSION_ID, out_session_id);
155
156         SSVAL(outbody.data, 0x00, 0x08 + 1);    /* struct size */
157         SSVAL(outbody.data, 0x02,
158               out_session_flags);               /* session flags */
159         SSVAL(outbody.data, 0x04,
160               out_security_offset);             /* security buffer offset */
161         SSVAL(outbody.data, 0x06,
162               out_security_buffer.length);      /* security buffer length */
163
164         outdyn = out_security_buffer;
165
166         error = smbd_smb2_request_done_ex(smb2req, status, outbody, &outdyn,
167                                            __location__);
168         if (!NT_STATUS_IS_OK(error)) {
169                 smbd_server_connection_terminate(smb2req->xconn,
170                                                  nt_errstr(error));
171                 return;
172         }
173 }
174
175 static NTSTATUS smbd_smb2_auth_generic_return(struct smbXsrv_session *session,
176                                         struct smbd_smb2_request *smb2req,
177                                         uint8_t in_security_mode,
178                                         struct auth_session_info *session_info,
179                                         uint16_t *out_session_flags,
180                                         uint64_t *out_session_id)
181 {
182         NTSTATUS status;
183         bool guest = false;
184         uint8_t session_key[16];
185         struct smbXsrv_session *x = session;
186         struct smbXsrv_connection *xconn = smb2req->xconn;
187
188         if ((in_security_mode & SMB2_NEGOTIATE_SIGNING_REQUIRED) ||
189             lp_server_signing() == SMB_SIGNING_REQUIRED) {
190                 x->global->signing_required = true;
191         }
192
193         if (lp_smb_encrypt(-1) == SMB_SIGNING_REQUIRED) {
194                 x->global->encryption_required = true;
195         }
196
197         if (security_session_user_level(session_info, NULL) < SECURITY_USER) {
198                 /* we map anonymous to guest internally */
199                 *out_session_flags |= SMB2_SESSION_FLAG_IS_GUEST;
200                 *out_session_flags |= SMB2_SESSION_FLAG_IS_NULL;
201                 /* force no signing */
202                 x->global->signing_required = false;
203                 guest = true;
204         }
205
206         if (guest && x->global->encryption_required) {
207                 DEBUG(1,("reject guest session as encryption is required\n"));
208                 return NT_STATUS_ACCESS_DENIED;
209         }
210
211         if (!(xconn->smb2.server.capabilities & SMB2_CAP_ENCRYPTION)) {
212                 if (x->global->encryption_required) {
213                         DEBUG(1,("reject session with dialect[0x%04X] "
214                                  "as encryption is required\n",
215                                  xconn->smb2.server.dialect));
216                         return NT_STATUS_ACCESS_DENIED;
217                 }
218         }
219
220         if (x->global->encryption_required) {
221                 *out_session_flags |= SMB2_SESSION_FLAG_ENCRYPT_DATA;
222         }
223
224         ZERO_STRUCT(session_key);
225         memcpy(session_key, session_info->session_key.data,
226                MIN(session_info->session_key.length, sizeof(session_key)));
227
228         x->global->signing_key = data_blob_talloc(x->global,
229                                                   session_key,
230                                                   sizeof(session_key));
231         if (x->global->signing_key.data == NULL) {
232                 ZERO_STRUCT(session_key);
233                 return NT_STATUS_NO_MEMORY;
234         }
235
236         if (xconn->protocol >= PROTOCOL_SMB2_24) {
237                 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCMAC");
238                 const DATA_BLOB context = data_blob_string_const_null("SmbSign");
239
240                 smb2_key_derivation(session_key, sizeof(session_key),
241                                     label.data, label.length,
242                                     context.data, context.length,
243                                     x->global->signing_key.data);
244         }
245
246         if (xconn->protocol >= PROTOCOL_SMB2_24) {
247                 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
248                 const DATA_BLOB context = data_blob_string_const_null("ServerIn ");
249
250                 x->global->decryption_key = data_blob_talloc(x->global,
251                                                              session_key,
252                                                              sizeof(session_key));
253                 if (x->global->decryption_key.data == NULL) {
254                         ZERO_STRUCT(session_key);
255                         return NT_STATUS_NO_MEMORY;
256                 }
257
258                 smb2_key_derivation(session_key, sizeof(session_key),
259                                     label.data, label.length,
260                                     context.data, context.length,
261                                     x->global->decryption_key.data);
262         }
263
264         if (xconn->protocol >= PROTOCOL_SMB2_24) {
265                 const DATA_BLOB label = data_blob_string_const_null("SMB2AESCCM");
266                 const DATA_BLOB context = data_blob_string_const_null("ServerOut");
267
268                 x->global->encryption_key = data_blob_talloc(x->global,
269                                                              session_key,
270                                                              sizeof(session_key));
271                 if (x->global->encryption_key.data == NULL) {
272                         ZERO_STRUCT(session_key);
273                         return NT_STATUS_NO_MEMORY;
274                 }
275
276                 smb2_key_derivation(session_key, sizeof(session_key),
277                                     label.data, label.length,
278                                     context.data, context.length,
279                                     x->global->encryption_key.data);
280
281                 generate_random_buffer((uint8_t *)&x->nonce_high, sizeof(x->nonce_high));
282                 x->nonce_low = 1;
283         }
284
285         x->global->application_key = data_blob_dup_talloc(x->global,
286                                                 x->global->signing_key);
287         if (x->global->application_key.data == NULL) {
288                 ZERO_STRUCT(session_key);
289                 return NT_STATUS_NO_MEMORY;
290         }
291
292         if (xconn->protocol >= PROTOCOL_SMB2_24) {
293                 const DATA_BLOB label = data_blob_string_const_null("SMB2APP");
294                 const DATA_BLOB context = data_blob_string_const_null("SmbRpc");
295
296                 smb2_key_derivation(session_key, sizeof(session_key),
297                                     label.data, label.length,
298                                     context.data, context.length,
299                                     x->global->application_key.data);
300         }
301         ZERO_STRUCT(session_key);
302
303         x->global->channels[0].signing_key = data_blob_dup_talloc(x->global->channels,
304                                                 x->global->signing_key);
305         if (x->global->channels[0].signing_key.data == NULL) {
306                 return NT_STATUS_NO_MEMORY;
307         }
308
309         data_blob_clear_free(&session_info->session_key);
310         session_info->session_key = data_blob_dup_talloc(session_info,
311                                                 x->global->application_key);
312         if (session_info->session_key.data == NULL) {
313                 return NT_STATUS_NO_MEMORY;
314         }
315
316         session->compat = talloc_zero(session, struct user_struct);
317         if (session->compat == NULL) {
318                 return NT_STATUS_NO_MEMORY;
319         }
320         session->compat->session = session;
321         session->compat->homes_snum = -1;
322         session->compat->session_info = session_info;
323         session->compat->session_keystr = NULL;
324         session->compat->vuid = session->global->session_wire_id;
325         DLIST_ADD(smb2req->sconn->users, session->compat);
326         smb2req->sconn->num_users++;
327
328         if (security_session_user_level(session_info, NULL) >= SECURITY_USER) {
329                 session->compat->homes_snum =
330                         register_homes_share(session_info->unix_info->unix_name);
331         }
332
333         set_current_user_info(session_info->unix_info->sanitized_username,
334                               session_info->unix_info->unix_name,
335                               session_info->info->domain_name);
336
337         reload_services(smb2req->sconn, conn_snum_used, true);
338
339         session->status = NT_STATUS_OK;
340         session->global->auth_session_info = session_info;
341         session->global->auth_session_info_seqnum += 1;
342         session->global->channels[0].auth_session_info_seqnum =
343                 session->global->auth_session_info_seqnum;
344         session->global->auth_time = timeval_to_nttime(&smb2req->request_time);
345         session->global->expiration_time = gensec_expire_time(session->gensec);
346
347         if (!session_claim(session)) {
348                 DEBUG(1, ("smb2: Failed to claim session "
349                         "for vuid=%llu\n",
350                         (unsigned long long)session->compat->vuid));
351                 return NT_STATUS_LOGON_FAILURE;
352         }
353
354         status = smbXsrv_session_update(session);
355         if (!NT_STATUS_IS_OK(status)) {
356                 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
357                           (unsigned long long)session->compat->vuid,
358                           nt_errstr(status)));
359                 return NT_STATUS_LOGON_FAILURE;
360         }
361
362         /*
363          * we attach the session to the request
364          * so that the response can be signed
365          */
366         smb2req->session = session;
367         if (!guest) {
368                 smb2req->do_signing = true;
369         }
370
371         global_client_caps |= (CAP_LEVEL_II_OPLOCKS|CAP_STATUS32);
372
373         *out_session_id = session->global->session_wire_id;
374
375         return NT_STATUS_OK;
376 }
377
378 static NTSTATUS smbd_smb2_reauth_generic_return(struct smbXsrv_session *session,
379                                         struct smbd_smb2_request *smb2req,
380                                         struct auth_session_info *session_info,
381                                         uint16_t *out_session_flags,
382                                         uint64_t *out_session_id)
383 {
384         NTSTATUS status;
385         struct smbXsrv_session *x = session;
386         struct smbXsrv_connection *xconn = smb2req->xconn;
387
388         data_blob_clear_free(&session_info->session_key);
389         session_info->session_key = data_blob_dup_talloc(session_info,
390                                                 x->global->application_key);
391         if (session_info->session_key.data == NULL) {
392                 return NT_STATUS_NO_MEMORY;
393         }
394
395         session->compat->session_info = session_info;
396         session->compat->vuid = session->global->session_wire_id;
397
398         session->compat->homes_snum =
399                         register_homes_share(session_info->unix_info->unix_name);
400
401         set_current_user_info(session_info->unix_info->sanitized_username,
402                               session_info->unix_info->unix_name,
403                               session_info->info->domain_name);
404
405         reload_services(smb2req->sconn, conn_snum_used, true);
406
407         session->status = NT_STATUS_OK;
408         TALLOC_FREE(session->global->auth_session_info);
409         session->global->auth_session_info = session_info;
410         session->global->auth_session_info_seqnum += 1;
411         session->global->channels[0].auth_session_info_seqnum =
412                 session->global->auth_session_info_seqnum;
413         session->global->auth_time = timeval_to_nttime(&smb2req->request_time);
414         session->global->expiration_time = gensec_expire_time(session->gensec);
415
416         status = smbXsrv_session_update(session);
417         if (!NT_STATUS_IS_OK(status)) {
418                 DEBUG(0, ("smb2: Failed to update session for vuid=%llu - %s\n",
419                           (unsigned long long)session->compat->vuid,
420                           nt_errstr(status)));
421                 return NT_STATUS_LOGON_FAILURE;
422         }
423
424         conn_clear_vuid_caches(xconn->sconn, session->compat->vuid);
425
426         *out_session_id = session->global->session_wire_id;
427
428         return NT_STATUS_OK;
429 }
430
431 struct smbd_smb2_session_setup_state {
432         struct tevent_context *ev;
433         struct smbd_smb2_request *smb2req;
434         uint64_t in_session_id;
435         uint8_t in_flags;
436         uint8_t in_security_mode;
437         uint64_t in_previous_session_id;
438         DATA_BLOB in_security_buffer;
439         struct smbXsrv_session *session;
440         struct auth_session_info *session_info;
441         uint16_t out_session_flags;
442         DATA_BLOB out_security_buffer;
443         uint64_t out_session_id;
444         /* The following pointer is owned by state->session. */
445         struct smbd_smb2_session_setup_state **pp_self_ref;
446 };
447
448 static int pp_self_ref_destructor(struct smbd_smb2_session_setup_state **pp_state)
449 {
450         (*pp_state)->session = NULL;
451         /*
452          * To make things clearer, ensure the pp_self_ref
453          * pointer is nulled out. We're never going to
454          * access this again.
455          */
456         (*pp_state)->pp_self_ref = NULL;
457         return 0;
458 }
459
460 static int smbd_smb2_session_setup_state_destructor(struct smbd_smb2_session_setup_state *state)
461 {
462         struct smbXsrv_connection *xconn;
463         struct smbd_smb2_request *preq;
464
465         /*
466          * If state->session is not NULL,
467          * we move the session from the session table to the request on failure
468          * so that the error response can be correctly signed, but the session
469          * is then really deleted when the request is done.
470          */
471
472         if (state->session == NULL) {
473                 return 0;
474         }
475
476         state->session->status = NT_STATUS_USER_SESSION_DELETED;
477         state->smb2req->session = talloc_move(state->smb2req, &state->session);
478
479         /*
480          * We've made this session owned by the current request.
481          * Ensure that any outstanding requests don't also refer
482          * to it.
483          */
484         xconn = state->smb2req->xconn;
485
486         for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
487                 if (preq == state->smb2req) {
488                         continue;
489                 }
490                 if (preq->session == state->smb2req->session) {
491                         preq->session = NULL;
492                         /*
493                          * If we no longer have a session we can't
494                          * sign or encrypt replies.
495                          */
496                         preq->do_signing = false;
497                         preq->do_encryption = false;
498                 }
499         }
500
501         return 0;
502 }
503
504 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq);
505 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq);
506 static void smbd_smb2_session_setup_auth_return(struct tevent_req *req);
507
508 /************************************************************************
509  We have to tag the state->session pointer with memory talloc'ed
510  on it to ensure it gets NULL'ed out if the underlying struct smbXsrv_session
511  is deleted by shutdown whilst this request is in flight.
512 ************************************************************************/
513
514 static NTSTATUS tag_state_session_ptr(struct smbd_smb2_session_setup_state *state)
515 {
516         state->pp_self_ref = talloc_zero(state->session,
517                         struct smbd_smb2_session_setup_state *);
518         if (state->pp_self_ref == NULL) {
519                 return NT_STATUS_NO_MEMORY;
520         }
521         *state->pp_self_ref = state;
522         talloc_set_destructor(state->pp_self_ref, pp_self_ref_destructor);
523         return NT_STATUS_OK;
524 }
525
526 static struct tevent_req *smbd_smb2_session_setup_send(TALLOC_CTX *mem_ctx,
527                                         struct tevent_context *ev,
528                                         struct smbd_smb2_request *smb2req,
529                                         uint64_t in_session_id,
530                                         uint8_t in_flags,
531                                         uint8_t in_security_mode,
532                                         uint64_t in_previous_session_id,
533                                         DATA_BLOB in_security_buffer)
534 {
535         struct tevent_req *req;
536         struct smbd_smb2_session_setup_state *state;
537         NTSTATUS status;
538         NTTIME now = timeval_to_nttime(&smb2req->request_time);
539         struct tevent_req *subreq;
540
541         req = tevent_req_create(mem_ctx, &state,
542                                 struct smbd_smb2_session_setup_state);
543         if (req == NULL) {
544                 return NULL;
545         }
546         state->ev = ev;
547         state->smb2req = smb2req;
548         state->in_session_id = in_session_id;
549         state->in_flags = in_flags;
550         state->in_security_mode = in_security_mode;
551         state->in_previous_session_id = in_previous_session_id;
552         state->in_security_buffer = in_security_buffer;
553
554         if (in_flags & SMB2_SESSION_FLAG_BINDING) {
555                 if (smb2req->xconn->protocol < PROTOCOL_SMB2_22) {
556                         tevent_req_nterror(req, NT_STATUS_REQUEST_NOT_ACCEPTED);
557                         return tevent_req_post(req, ev);
558                 }
559
560                 /*
561                  * We do not support multi channel.
562                  */
563                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
564                 return tevent_req_post(req, ev);
565         }
566
567         talloc_set_destructor(state, smbd_smb2_session_setup_state_destructor);
568
569         if (state->in_session_id == 0) {
570                 /* create a new session */
571                 status = smbXsrv_session_create(state->smb2req->xconn,
572                                                 now, &state->session);
573                 if (tevent_req_nterror(req, status)) {
574                         return tevent_req_post(req, ev);
575                 }
576         } else {
577                 status = smb2srv_session_lookup(state->smb2req->xconn,
578                                                 state->in_session_id, now,
579                                                 &state->session);
580                 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
581                         status = NT_STATUS_OK;
582                 }
583                 if (NT_STATUS_IS_OK(status)) {
584                         state->session->status = NT_STATUS_MORE_PROCESSING_REQUIRED;
585                         status = NT_STATUS_MORE_PROCESSING_REQUIRED;
586                         TALLOC_FREE(state->session->gensec);
587                 }
588                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
589                         tevent_req_nterror(req, status);
590                         return tevent_req_post(req, ev);
591                 }
592         }
593
594         status = tag_state_session_ptr(state);
595         if (tevent_req_nterror(req, status)) {
596                 return tevent_req_post(req, ev);
597         }
598
599         if (state->session->gensec == NULL) {
600                 status = auth_generic_prepare(state->session,
601                                               state->smb2req->xconn->remote_address,
602                                               &state->session->gensec);
603                 if (tevent_req_nterror(req, status)) {
604                         return tevent_req_post(req, ev);
605                 }
606
607                 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_SESSION_KEY);
608                 gensec_want_feature(state->session->gensec, GENSEC_FEATURE_UNIX_TOKEN);
609
610                 status = gensec_start_mech_by_oid(state->session->gensec,
611                                                   GENSEC_OID_SPNEGO);
612                 if (tevent_req_nterror(req, status)) {
613                         return tevent_req_post(req, ev);
614                 }
615         }
616
617         become_root();
618         subreq = gensec_update_send(state, state->ev,
619                                     state->session->gensec,
620                                     state->in_security_buffer);
621         unbecome_root();
622         if (tevent_req_nomem(subreq, req)) {
623                 return tevent_req_post(req, ev);
624         }
625         tevent_req_set_callback(subreq, smbd_smb2_session_setup_gensec_done, req);
626
627         return req;
628 }
629
630 static void smbd_smb2_session_setup_gensec_done(struct tevent_req *subreq)
631 {
632         struct tevent_req *req =
633                 tevent_req_callback_data(subreq,
634                 struct tevent_req);
635         struct smbd_smb2_session_setup_state *state =
636                 tevent_req_data(req,
637                 struct smbd_smb2_session_setup_state);
638         NTSTATUS status;
639
640         become_root();
641         status = gensec_update_recv(subreq, state,
642                                     &state->out_security_buffer);
643         unbecome_root();
644         TALLOC_FREE(subreq);
645         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED) &&
646             !NT_STATUS_IS_OK(status)) {
647                 tevent_req_nterror(req, status);
648                 return;
649         }
650
651         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
652                 state->out_session_id = state->session->global->session_wire_id;
653                 /* we want to keep the session */
654                 state->session = NULL;
655                 TALLOC_FREE(state->pp_self_ref);
656                 tevent_req_nterror(req, status);
657                 return;
658         }
659
660         status = gensec_session_info(state->session->gensec,
661                                      state->session->global,
662                                      &state->session_info);
663         if (tevent_req_nterror(req, status)) {
664                 return;
665         }
666
667         if ((state->in_previous_session_id != 0) &&
668              (state->session->global->session_wire_id !=
669               state->in_previous_session_id))
670         {
671                 subreq = smb2srv_session_close_previous_send(state, state->ev,
672                                                 state->smb2req->xconn,
673                                                 state->session_info,
674                                                 state->in_previous_session_id,
675                                                 state->session->global->session_wire_id);
676                 if (tevent_req_nomem(subreq, req)) {
677                         return;
678                 }
679                 tevent_req_set_callback(subreq,
680                                         smbd_smb2_session_setup_previous_done,
681                                         req);
682                 return;
683         }
684
685         smbd_smb2_session_setup_auth_return(req);
686 }
687
688 static void smbd_smb2_session_setup_previous_done(struct tevent_req *subreq)
689 {
690         struct tevent_req *req =
691                 tevent_req_callback_data(subreq,
692                 struct tevent_req);
693         NTSTATUS status;
694
695         status = smb2srv_session_close_previous_recv(subreq);
696         TALLOC_FREE(subreq);
697         if (tevent_req_nterror(req, status)) {
698                 return;
699         }
700
701         smbd_smb2_session_setup_auth_return(req);
702 }
703
704 static void smbd_smb2_session_setup_auth_return(struct tevent_req *req)
705 {
706         struct smbd_smb2_session_setup_state *state =
707                 tevent_req_data(req,
708                 struct smbd_smb2_session_setup_state);
709         NTSTATUS status;
710
711         if (state->session->global->auth_session_info != NULL) {
712                 status = smbd_smb2_reauth_generic_return(state->session,
713                                                          state->smb2req,
714                                                          state->session_info,
715                                                          &state->out_session_flags,
716                                                          &state->out_session_id);
717                 if (tevent_req_nterror(req, status)) {
718                         return;
719                 }
720                 /* we want to keep the session */
721                 state->session = NULL;
722                 TALLOC_FREE(state->pp_self_ref);
723                 tevent_req_done(req);
724                 return;
725         }
726
727         status = smbd_smb2_auth_generic_return(state->session,
728                                                state->smb2req,
729                                                state->in_security_mode,
730                                                state->session_info,
731                                                &state->out_session_flags,
732                                                &state->out_session_id);
733         if (tevent_req_nterror(req, status)) {
734                 return;
735         }
736
737         /* we want to keep the session */
738         state->session = NULL;
739         TALLOC_FREE(state->pp_self_ref);
740         tevent_req_done(req);
741         return;
742 }
743
744 static NTSTATUS smbd_smb2_session_setup_recv(struct tevent_req *req,
745                                         uint16_t *out_session_flags,
746                                         TALLOC_CTX *mem_ctx,
747                                         DATA_BLOB *out_security_buffer,
748                                         uint64_t *out_session_id)
749 {
750         struct smbd_smb2_session_setup_state *state =
751                 tevent_req_data(req,
752                 struct smbd_smb2_session_setup_state);
753         NTSTATUS status;
754
755         if (tevent_req_is_nterror(req, &status)) {
756                 if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
757                         tevent_req_received(req);
758                         return nt_status_squash(status);
759                 }
760         } else {
761                 status = NT_STATUS_OK;
762         }
763
764         *out_session_flags = state->out_session_flags;
765         *out_security_buffer = state->out_security_buffer;
766         *out_session_id = state->out_session_id;
767
768         talloc_steal(mem_ctx, out_security_buffer->data);
769         tevent_req_received(req);
770         return status;
771 }
772
773 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
774                                         struct tevent_context *ev,
775                                         struct smbd_smb2_request *smb2req);
776 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req);
777 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq);
778
779 NTSTATUS smbd_smb2_request_process_logoff(struct smbd_smb2_request *req)
780 {
781         NTSTATUS status;
782         struct tevent_req *subreq = NULL;
783
784         status = smbd_smb2_request_verify_sizes(req, 0x04);
785         if (!NT_STATUS_IS_OK(status)) {
786                 return smbd_smb2_request_error(req, status);
787         }
788
789         subreq = smbd_smb2_logoff_send(req, req->sconn->ev_ctx, req);
790         if (subreq == NULL) {
791                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
792         }
793         tevent_req_set_callback(subreq, smbd_smb2_request_logoff_done, req);
794
795         /*
796          * Wait a long time before going async on this to allow
797          * requests we're waiting on to finish. Set timeout to 10 secs.
798          */
799         return smbd_smb2_request_pending_queue(req, subreq, 10000000);
800 }
801
802 static void smbd_smb2_request_logoff_done(struct tevent_req *subreq)
803 {
804         struct smbd_smb2_request *smb2req =
805                 tevent_req_callback_data(subreq,
806                 struct smbd_smb2_request);
807         DATA_BLOB outbody;
808         NTSTATUS status;
809         NTSTATUS error;
810
811         status = smbd_smb2_logoff_recv(subreq);
812         TALLOC_FREE(subreq);
813         if (!NT_STATUS_IS_OK(status)) {
814                 error = smbd_smb2_request_error(smb2req, status);
815                 if (!NT_STATUS_IS_OK(error)) {
816                         smbd_server_connection_terminate(smb2req->xconn,
817                                                         nt_errstr(error));
818                         return;
819                 }
820                 return;
821         }
822
823         outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
824         if (outbody.data == NULL) {
825                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
826                 if (!NT_STATUS_IS_OK(error)) {
827                         smbd_server_connection_terminate(smb2req->xconn,
828                                                         nt_errstr(error));
829                         return;
830                 }
831                 return;
832         }
833
834         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
835         SSVAL(outbody.data, 0x02, 0);           /* reserved */
836
837         error = smbd_smb2_request_done(smb2req, outbody, NULL);
838         if (!NT_STATUS_IS_OK(error)) {
839                 smbd_server_connection_terminate(smb2req->xconn,
840                                                 nt_errstr(error));
841                 return;
842         }
843 }
844
845 struct smbd_smb2_logout_state {
846         struct smbd_smb2_request *smb2req;
847         struct tevent_queue *wait_queue;
848 };
849
850 static void smbd_smb2_logoff_wait_done(struct tevent_req *subreq);
851
852 static struct tevent_req *smbd_smb2_logoff_send(TALLOC_CTX *mem_ctx,
853                                         struct tevent_context *ev,
854                                         struct smbd_smb2_request *smb2req)
855 {
856         struct tevent_req *req;
857         struct smbd_smb2_logout_state *state;
858         struct tevent_req *subreq;
859         struct smbd_smb2_request *preq;
860         struct smbXsrv_connection *xconn = smb2req->xconn;
861
862         req = tevent_req_create(mem_ctx, &state,
863                         struct smbd_smb2_logout_state);
864         if (req == NULL) {
865                 return NULL;
866         }
867         state->smb2req = smb2req;
868
869         state->wait_queue = tevent_queue_create(state, "logoff_wait_queue");
870         if (tevent_req_nomem(state->wait_queue, req)) {
871                 return tevent_req_post(req, ev);
872         }
873
874         /*
875          * Make sure that no new request will be able to use this session.
876          */
877         smb2req->session->status = NT_STATUS_USER_SESSION_DELETED;
878
879         for (preq = xconn->smb2.requests; preq != NULL; preq = preq->next) {
880                 if (preq == smb2req) {
881                         /* Can't cancel current request. */
882                         continue;
883                 }
884                 if (preq->session != smb2req->session) {
885                         /* Request on different session. */
886                         continue;
887                 }
888
889                 /*
890                  * Never cancel anything in a compound
891                  * request. Way too hard to deal with
892                  * the result.
893                  */
894                 if (!preq->compound_related && preq->subreq != NULL) {
895                         tevent_req_cancel(preq->subreq);
896                 }
897
898                 /*
899                  * Now wait until the request is finished.
900                  *
901                  * We don't set a callback, as we just want to block the
902                  * wait queue and the talloc_free() of the request will
903                  * remove the item from the wait queue.
904                  */
905                 subreq = tevent_queue_wait_send(preq, ev, state->wait_queue);
906                 if (tevent_req_nomem(subreq, req)) {
907                         return tevent_req_post(req, ev);
908                 }
909         }
910
911         /*
912          * Now we add our own waiter to the end of the queue,
913          * this way we get notified when all pending requests are finished
914          * and send to the socket.
915          */
916         subreq = tevent_queue_wait_send(state, ev, state->wait_queue);
917         if (tevent_req_nomem(subreq, req)) {
918                 return tevent_req_post(req, ev);
919         }
920         tevent_req_set_callback(subreq, smbd_smb2_logoff_wait_done, req);
921
922         return req;
923 }
924
925 static void smbd_smb2_logoff_wait_done(struct tevent_req *subreq)
926 {
927         struct tevent_req *req = tevent_req_callback_data(
928                 subreq, struct tevent_req);
929         struct smbd_smb2_logout_state *state = tevent_req_data(
930                 req, struct smbd_smb2_logout_state);
931         NTSTATUS status;
932
933         tevent_queue_wait_recv(subreq);
934         TALLOC_FREE(subreq);
935
936         /*
937          * As we've been awoken, we may have changed
938          * uid in the meantime. Ensure we're still
939          * root (SMB2_OP_LOGOFF has .as_root = true).
940          */
941         change_to_root_user();
942
943         status = smbXsrv_session_logoff(state->smb2req->session);
944         if (tevent_req_nterror(req, status)) {
945                 return;
946         }
947
948         /*
949          * we may need to sign the response, so we need to keep
950          * the session until the response is sent to the wire.
951          */
952         talloc_steal(state->smb2req, state->smb2req->session);
953
954         tevent_req_done(req);
955 }
956
957 static NTSTATUS smbd_smb2_logoff_recv(struct tevent_req *req)
958 {
959         return tevent_req_simple_recv_ntstatus(req);
960 }