s3-smbd: enable SMB2 lock sequence checking when SMB3 is enabled.
[obnox/samba/samba-obnox.git] / source3 / smbd / smb2_lock.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 "../lib/util/tevent_ntstatus.h"
27 #include "messages.h"
28
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_SMB2
31
32 struct smbd_smb2_lock_element {
33         uint64_t offset;
34         uint64_t length;
35         uint32_t flags;
36 };
37
38 struct smbd_smb2_lock_state {
39         struct smbd_smb2_request *smb2req;
40         struct smb_request *smb1req;
41         struct blocking_lock_record *blr;
42         uint16_t lock_count;
43         struct smbd_lock_element *locks;
44         uint8_t lock_sequence_value;
45         uint8_t *lock_sequence_element;
46 };
47
48 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
49                                 struct blocking_lock_record *blr);
50
51 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
52                                                  struct tevent_context *ev,
53                                                  struct smbd_smb2_request *smb2req,
54                                                  struct files_struct *in_fsp,
55                                                  uint32_t in_lock_sequence,
56                                                  uint16_t in_lock_count,
57                                                  struct smbd_smb2_lock_element *in_locks);
58 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req);
59
60 static void smbd_smb2_request_lock_done(struct tevent_req *subreq);
61 NTSTATUS smbd_smb2_request_process_lock(struct smbd_smb2_request *req)
62 {
63         const uint8_t *inbody;
64         uint16_t in_lock_count;
65         uint32_t in_lock_sequence;
66         uint64_t in_file_id_persistent;
67         uint64_t in_file_id_volatile;
68         struct files_struct *in_fsp;
69         struct smbd_smb2_lock_element *in_locks;
70         struct tevent_req *subreq;
71         const uint8_t *lock_buffer;
72         uint16_t l;
73         NTSTATUS status;
74
75         status = smbd_smb2_request_verify_sizes(req, 0x30);
76         if (!NT_STATUS_IS_OK(status)) {
77                 return smbd_smb2_request_error(req, status);
78         }
79         inbody = SMBD_SMB2_IN_BODY_PTR(req);
80
81         in_lock_count                   = CVAL(inbody, 0x02);
82         if (req->xconn->protocol >= PROTOCOL_SMB2_10) {
83                 in_lock_sequence        = IVAL(inbody, 0x04);
84         } else {
85                 /* 0x04 - 4 bytes reserved */
86                 in_lock_sequence        = 0;
87         }
88         in_file_id_persistent           = BVAL(inbody, 0x08);
89         in_file_id_volatile             = BVAL(inbody, 0x10);
90
91         if (in_lock_count < 1) {
92                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
93         }
94
95         if (((in_lock_count - 1) * 0x18) > SMBD_SMB2_IN_DYN_LEN(req)) {
96                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
97         }
98
99         in_locks = talloc_array(req, struct smbd_smb2_lock_element,
100                                 in_lock_count);
101         if (in_locks == NULL) {
102                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
103         }
104
105         l = 0;
106         lock_buffer = inbody + 0x18;
107
108         in_locks[l].offset      = BVAL(lock_buffer, 0x00);
109         in_locks[l].length      = BVAL(lock_buffer, 0x08);
110         in_locks[l].flags       = IVAL(lock_buffer, 0x10);
111         /* 0x14 - 4 reserved bytes */
112
113         status = req->session->status;
114         if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_SESSION_EXPIRED)) {
115                 /*
116                  * We need to catch NT_STATUS_NETWORK_SESSION_EXPIRED
117                  * for lock requests only.
118                  *
119                  * Unlock requests still need to be processed!
120                  *
121                  * This means smbd_smb2_request_check_session()
122                  * can't handle the difference and always
123                  * allows SMB2_OP_LOCK.
124                  */
125                 if (in_locks[0].flags != SMB2_LOCK_FLAG_UNLOCK) {
126                         return smbd_smb2_request_error(req, status);
127                 }
128         }
129
130         lock_buffer = SMBD_SMB2_IN_DYN_PTR(req);
131
132         for (l=1; l < in_lock_count; l++) {
133                 in_locks[l].offset      = BVAL(lock_buffer, 0x00);
134                 in_locks[l].length      = BVAL(lock_buffer, 0x08);
135                 in_locks[l].flags       = IVAL(lock_buffer, 0x10);
136                 /* 0x14 - 4 reserved bytes */
137
138                 lock_buffer += 0x18;
139         }
140
141         in_fsp = file_fsp_smb2(req, in_file_id_persistent, in_file_id_volatile);
142         if (in_fsp == NULL) {
143                 return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
144         }
145
146         subreq = smbd_smb2_lock_send(req, req->ev_ctx,
147                                      req, in_fsp,
148                                      in_lock_sequence,
149                                      in_lock_count,
150                                      in_locks);
151         if (subreq == NULL) {
152                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
153         }
154         tevent_req_set_callback(subreq, smbd_smb2_request_lock_done, req);
155
156         return smbd_smb2_request_pending_queue(req, subreq, 500);
157 }
158
159 static void smbd_smb2_request_lock_done(struct tevent_req *subreq)
160 {
161         struct smbd_smb2_request *smb2req = tevent_req_callback_data(subreq,
162                                         struct smbd_smb2_request);
163         DATA_BLOB outbody;
164         NTSTATUS status;
165         NTSTATUS error; /* transport error */
166
167         status = smbd_smb2_lock_recv(subreq);
168         TALLOC_FREE(subreq);
169         if (!NT_STATUS_IS_OK(status)) {
170                 error = smbd_smb2_request_error(smb2req, status);
171                 if (!NT_STATUS_IS_OK(error)) {
172                         smbd_server_connection_terminate(smb2req->xconn,
173                                                          nt_errstr(error));
174                         return;
175                 }
176                 return;
177         }
178
179         outbody = smbd_smb2_generate_outbody(smb2req, 0x04);
180         if (outbody.data == NULL) {
181                 error = smbd_smb2_request_error(smb2req, NT_STATUS_NO_MEMORY);
182                 if (!NT_STATUS_IS_OK(error)) {
183                         smbd_server_connection_terminate(smb2req->xconn,
184                                                          nt_errstr(error));
185                         return;
186                 }
187                 return;
188         }
189
190         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
191         SSVAL(outbody.data, 0x02, 0);           /* reserved */
192
193         error = smbd_smb2_request_done(smb2req, outbody, NULL);
194         if (!NT_STATUS_IS_OK(error)) {
195                 smbd_server_connection_terminate(smb2req->xconn,
196                                                  nt_errstr(error));
197                 return;
198         }
199 }
200
201 static struct tevent_req *smbd_smb2_lock_send(TALLOC_CTX *mem_ctx,
202                                                  struct tevent_context *ev,
203                                                  struct smbd_smb2_request *smb2req,
204                                                  struct files_struct *fsp,
205                                                  uint32_t in_lock_sequence,
206                                                  uint16_t in_lock_count,
207                                                  struct smbd_smb2_lock_element *in_locks)
208 {
209         struct tevent_req *req;
210         struct smbd_smb2_lock_state *state;
211         struct smb_request *smb1req;
212         int32_t timeout = -1;
213         bool isunlock = false;
214         uint16_t i;
215         struct smbd_lock_element *locks;
216         NTSTATUS status;
217         bool async = false;
218         bool check_lock_sequence = false;
219         uint32_t lock_sequence_bucket = 0;
220
221         req = tevent_req_create(mem_ctx, &state,
222                         struct smbd_smb2_lock_state);
223         if (req == NULL) {
224                 return NULL;
225         }
226         state->smb2req = smb2req;
227         smb2req->subreq = req; /* So we can find this when going async. */
228
229         smb1req = smbd_smb2_fake_smb_request(smb2req);
230         if (tevent_req_nomem(smb1req, req)) {
231                 return tevent_req_post(req, ev);
232         }
233         state->smb1req = smb1req;
234
235         DEBUG(10,("smbd_smb2_lock_send: %s - %s\n",
236                   fsp_str_dbg(fsp), fsp_fnum_dbg(fsp)));
237
238         /*
239          * Windows sets check_lock_sequence = true
240          * only for resilient and persistent handles.
241          *
242          * According to MS-SMB2 section 3.3.5.14 a server SHOULD activate
243          * processing of lock sequence numbers for EITHER
244          * - protocol dialect 2.1 and resilient handles
245          * OR
246          * - protocol dialect 3.x.
247          *
248          * TODO: What about other handles using multi-channel?
249          */
250         if (smb2req->xconn->protocol >= PROTOCOL_SMB2_22) {
251                 check_lock_sequence = true;
252         }
253
254         if (check_lock_sequence) {
255                 state->lock_sequence_value = in_lock_sequence & 0xF;
256                 lock_sequence_bucket = in_lock_sequence >> 4;
257         }
258         if ((lock_sequence_bucket > 0) &&
259             (lock_sequence_bucket <= sizeof(fsp->op->global->lock_sequence_array)))
260         {
261                 uint32_t idx = lock_sequence_bucket - 1;
262                 uint8_t *array = fsp->op->global->lock_sequence_array;
263
264                 state->lock_sequence_element = &array[idx];
265         }
266
267         if (state->lock_sequence_element != NULL) {
268                 if (*state->lock_sequence_element == state->lock_sequence_value)
269                 {
270                         DBG_INFO("replayed smb2 lock request detected: "
271                                  "file %s, value %u, bucket %u\n",
272                                  fsp_str_dbg(fsp),
273                                  (unsigned)state->lock_sequence_value,
274                                  (unsigned)lock_sequence_bucket);
275                         tevent_req_done(req);
276                         return tevent_req_post(req, ev);
277                 }
278                 *state->lock_sequence_element = 0xFF;
279         }
280
281         locks = talloc_array(state, struct smbd_lock_element, in_lock_count);
282         if (locks == NULL) {
283                 tevent_req_nterror(req, NT_STATUS_NO_MEMORY);
284                 return tevent_req_post(req, ev);
285         }
286
287         switch (in_locks[0].flags) {
288         case SMB2_LOCK_FLAG_SHARED:
289         case SMB2_LOCK_FLAG_EXCLUSIVE:
290                 if (in_lock_count > 1) {
291                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
292                         return tevent_req_post(req, ev);
293                 }
294                 timeout = -1;
295                 break;
296
297         case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
298         case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
299                 timeout = 0;
300                 break;
301
302         case SMB2_LOCK_FLAG_UNLOCK:
303                 /* only the first lock gives the UNLOCK bit - see
304                    MS-SMB2 3.3.5.14 */
305                 isunlock = true;
306                 timeout = 0;
307                 break;
308
309         default:
310                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
311                 return tevent_req_post(req, ev);
312         }
313
314         if (!isunlock && (in_lock_count > 1)) {
315
316                 /*
317                  * 3.3.5.14.2 says we SHOULD fail with INVALID_PARAMETER if we
318                  * have more than one lock and one of those is blocking.
319                  */
320
321                 for (i=0; i<in_lock_count; i++) {
322                         uint32_t flags = in_locks[i].flags;
323
324                         if ((flags & SMB2_LOCK_FLAG_FAIL_IMMEDIATELY) == 0) {
325                                 tevent_req_nterror(
326                                         req, NT_STATUS_INVALID_PARAMETER);
327                                 return tevent_req_post(req, ev);
328                         }
329                 }
330         }
331
332         for (i=0; i<in_lock_count; i++) {
333                 bool invalid = false;
334
335                 switch (in_locks[i].flags) {
336                 case SMB2_LOCK_FLAG_SHARED:
337                 case SMB2_LOCK_FLAG_EXCLUSIVE:
338                         if (isunlock) {
339                                 invalid = true;
340                                 break;
341                         }
342                         break;
343
344                 case SMB2_LOCK_FLAG_SHARED|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
345                 case SMB2_LOCK_FLAG_EXCLUSIVE|SMB2_LOCK_FLAG_FAIL_IMMEDIATELY:
346                         if (isunlock) {
347                                 invalid = true;
348                         }
349                         break;
350
351                 case SMB2_LOCK_FLAG_UNLOCK:
352                         if (!isunlock) {
353                                 tevent_req_nterror(req,
354                                                    NT_STATUS_INVALID_PARAMETER);
355                                 return tevent_req_post(req, ev);
356                         }
357                         break;
358
359                 default:
360                         if (isunlock) {
361                                 /*
362                                  * If the first element was a UNLOCK
363                                  * we need to defer the error response
364                                  * to the backend, because we need to process
365                                  * all unlock elements before
366                                  */
367                                 invalid = true;
368                                 break;
369                         }
370                         tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
371                         return tevent_req_post(req, ev);
372                 }
373
374                 locks[i].smblctx = fsp->op->global->open_persistent_id;
375                 locks[i].offset = in_locks[i].offset;
376                 locks[i].count  = in_locks[i].length;
377
378                 if (in_locks[i].flags & SMB2_LOCK_FLAG_EXCLUSIVE) {
379                         locks[i].brltype = WRITE_LOCK;
380                 } else if (in_locks[i].flags & SMB2_LOCK_FLAG_SHARED) {
381                         locks[i].brltype = READ_LOCK;
382                 } else if (invalid) {
383                         /*
384                          * this is an invalid UNLOCK element
385                          * and the backend needs to test for
386                          * brltype != UNLOCK_LOCK and return
387                          * NT_STATUS_INVALID_PARAMETER
388                          */
389                         locks[i].brltype = READ_LOCK;
390                 } else {
391                         locks[i].brltype = UNLOCK_LOCK;
392                 }
393
394                 DEBUG(10,("smbd_smb2_lock_send: index %d offset=%llu, count=%llu, "
395                         "smblctx = %llu type %d\n",
396                         i,
397                         (unsigned long long)locks[i].offset,
398                         (unsigned long long)locks[i].count,
399                         (unsigned long long)locks[i].smblctx,
400                         (int)locks[i].brltype ));
401         }
402
403         state->locks = locks;
404         state->lock_count = in_lock_count;
405
406         if (isunlock) {
407                 status = smbd_do_unlocking(smb1req, fsp,
408                                            in_lock_count, locks);
409                 async = false;
410         } else {
411                 status = smbd_do_locking(smb1req, fsp,
412                                          0,
413                                          timeout,
414                                          in_lock_count,
415                                          locks,
416                                          &async);
417         }
418         if (!NT_STATUS_IS_OK(status)) {
419                 if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
420                        status = NT_STATUS_LOCK_NOT_GRANTED;
421                 }
422                 tevent_req_nterror(req, status);
423                 return tevent_req_post(req, ev);
424         }
425
426         if (async) {
427                 tevent_req_defer_callback(req, smb2req->ev_ctx);
428                 SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
429                 return req;
430         }
431
432         if (state->lock_sequence_element != NULL) {
433                 *state->lock_sequence_element = state->lock_sequence_value;
434         }
435
436         tevent_req_done(req);
437         return tevent_req_post(req, ev);
438 }
439
440 static NTSTATUS smbd_smb2_lock_recv(struct tevent_req *req)
441 {
442         NTSTATUS status;
443
444         if (tevent_req_is_nterror(req, &status)) {
445                 tevent_req_received(req);
446                 return status;
447         }
448
449         tevent_req_received(req);
450         return NT_STATUS_OK;
451 }
452
453 /****************************************************************
454  Cancel an outstanding blocking lock request.
455 *****************************************************************/
456
457 static bool smbd_smb2_lock_cancel(struct tevent_req *req)
458 {
459         struct smbd_smb2_request *smb2req = NULL;
460         struct smbd_smb2_lock_state *state = tevent_req_data(req,
461                                 struct smbd_smb2_lock_state);
462         if (!state) {
463                 return false;
464         }
465
466         if (!state->smb2req) {
467                 return false;
468         }
469
470         smb2req = state->smb2req;
471
472         remove_pending_lock(state, state->blr);
473
474         /*
475          * If the request is canceled because of logoff, tdis or close
476          * the status is NT_STATUS_RANGE_NOT_LOCKED instead of
477          * NT_STATUS_CANCELLED.
478          *
479          * Note that the close case is handled in
480          * cancel_pending_lock_requests_by_fid_smb2(SHUTDOWN_CLOSE)
481          * for now.
482          */
483         if (!NT_STATUS_IS_OK(smb2req->session->status)) {
484                 tevent_req_nterror(req, NT_STATUS_RANGE_NOT_LOCKED);
485                 return true;
486         }
487
488         if (!NT_STATUS_IS_OK(smb2req->tcon->status)) {
489                 tevent_req_nterror(req, NT_STATUS_RANGE_NOT_LOCKED);
490                 return true;
491         }
492
493         tevent_req_nterror(req, NT_STATUS_CANCELLED);
494         return true;
495 }
496
497 /****************************************************************
498  Got a message saying someone unlocked a file. Re-schedule all
499  blocking lock requests as we don't know if anything overlapped.
500 *****************************************************************/
501
502 static void received_unlock_msg(struct messaging_context *msg,
503                                 void *private_data,
504                                 uint32_t msg_type,
505                                 struct server_id server_id,
506                                 DATA_BLOB *data)
507 {
508         struct smbd_server_connection *sconn =
509                 talloc_get_type_abort(private_data,
510                 struct smbd_server_connection);
511
512         DEBUG(10,("received_unlock_msg (SMB2)\n"));
513
514         process_blocking_lock_queue_smb2(sconn, timeval_current());
515 }
516
517 /****************************************************************
518  Function to get the blr on a pending record.
519 *****************************************************************/
520
521 struct blocking_lock_record *get_pending_smb2req_blr(struct smbd_smb2_request *smb2req)
522 {
523         struct smbd_smb2_lock_state *state = NULL;
524         const uint8_t *inhdr;
525
526         if (!smb2req) {
527                 return NULL;
528         }
529         if (smb2req->subreq == NULL) {
530                 return NULL;
531         }
532         if (!tevent_req_is_in_progress(smb2req->subreq)) {
533                 return NULL;
534         }
535         inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
536         if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
537                 return NULL;
538         }
539         state = tevent_req_data(smb2req->subreq,
540                         struct smbd_smb2_lock_state);
541         if (!state) {
542                 return NULL;
543         }
544         return state->blr;
545 }
546 /****************************************************************
547  Set up the next brl timeout.
548 *****************************************************************/
549
550 static bool recalc_smb2_brl_timeout(struct smbd_server_connection *sconn)
551 {
552         struct smbXsrv_connection *xconn = NULL;
553         struct timeval next_timeout = timeval_zero();
554         int max_brl_timeout = lp_parm_int(-1, "brl", "recalctime", 5);
555
556         TALLOC_FREE(sconn->smb2.locks.brl_timeout);
557
558         if (sconn->client != NULL) {
559                 xconn = sconn->client->connections;
560         }
561
562         for (; xconn != NULL; xconn = xconn->next) {
563                 struct smbd_smb2_request *smb2req, *nextreq;
564
565                 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
566                         struct blocking_lock_record *blr =
567                                 get_pending_smb2req_blr(smb2req);
568
569                         nextreq = smb2req->next;
570
571                         if (blr == NULL) {
572                                 continue;
573                         }
574
575                         if (!timeval_is_zero(&blr->expire_time)) {
576                                 next_timeout = timeval_brl_min(&next_timeout,
577                                                         &blr->expire_time);
578                                 continue;
579                         }
580
581                         /*
582                          * If we're blocked on pid 0xFFFFFFFFFFFFFFFFLL this is
583                          * a POSIX lock, so calculate a timeout of
584                          * 10 seconds into the future.
585                          */
586                         if (blr->blocking_smblctx == 0xFFFFFFFFFFFFFFFFLL) {
587                                 struct timeval psx_to;
588
589                                 psx_to = timeval_current_ofs(10, 0);
590                                 next_timeout = timeval_brl_min(&next_timeout,
591                                                                &psx_to);
592                         }
593                 }
594         }
595
596         if (timeval_is_zero(&next_timeout)) {
597                 DEBUG(10, ("recalc_smb2_brl_timeout:Next "
598                         "timeout = Infinite.\n"));
599                 return true;
600         }
601
602         /*
603          * To account for unclean shutdowns by clients we need a
604          * maximum timeout that we use for checking pending locks. If
605          * we have any pending locks at all, then check if the pending
606          * lock can continue at least every brl:recalctime seconds
607          * (default 5 seconds).
608          *
609          * This saves us needing to do a message_send_all() in the
610          * SIGCHLD handler in the parent daemon. That
611          * message_send_all() caused O(n^2) work to be done when IP
612          * failovers happened in clustered Samba, which could make the
613          * entire system unusable for many minutes.
614          */
615
616         if (max_brl_timeout > 0) {
617                 struct timeval min_to = timeval_current_ofs(max_brl_timeout, 0);
618                 next_timeout = timeval_brl_min(&next_timeout, &min_to);
619         }
620
621         if (DEBUGLVL(10)) {
622                 struct timeval cur, from_now;
623
624                 cur = timeval_current();
625                 from_now = timeval_until(&cur, &next_timeout);
626                 DEBUG(10, ("recalc_smb2_brl_timeout: Next "
627                         "timeout = %d.%d seconds from now.\n",
628                         (int)from_now.tv_sec, (int)from_now.tv_usec));
629         }
630
631         /*
632          * brl_timeout_fn() calls change_to_root_user()
633          * so we can use sconn->root_ev_ctx.
634          */
635         sconn->smb2.locks.brl_timeout = tevent_add_timer(
636                                 sconn->root_ev_ctx,
637                                 NULL,
638                                 next_timeout,
639                                 brl_timeout_fn,
640                                 sconn);
641         if (!sconn->smb2.locks.brl_timeout) {
642                 return false;
643         }
644         return true;
645 }
646
647 /****************************************************************
648  Get an SMB2 lock request to go async. lock_timeout should
649  always be -1 here.
650 *****************************************************************/
651
652 bool push_blocking_lock_request_smb2( struct byte_range_lock *br_lck,
653                                 struct smb_request *smb1req,
654                                 files_struct *fsp,
655                                 int lock_timeout,
656                                 int lock_num,
657                                 uint64_t smblctx,
658                                 enum brl_type lock_type,
659                                 enum brl_flavour lock_flav,
660                                 uint64_t offset,
661                                 uint64_t count,
662                                 uint64_t blocking_smblctx)
663 {
664         struct smbd_server_connection *sconn = smb1req->sconn;
665         struct smbd_smb2_request *smb2req = smb1req->smb2req;
666         struct tevent_req *req = NULL;
667         struct smbd_smb2_lock_state *state = NULL;
668         struct blocking_lock_record *blr = NULL;
669         NTSTATUS status = NT_STATUS_OK;
670
671         if (!smb2req) {
672                 return false;
673         }
674         req = smb2req->subreq;
675         if (!req) {
676                 return false;
677         }
678         if (!tevent_req_is_in_progress(smb2req->subreq)) {
679                 return false;
680         }
681         state = tevent_req_data(req, struct smbd_smb2_lock_state);
682         if (!state) {
683                 return false;
684         }
685
686         blr = talloc_zero(state, struct blocking_lock_record);
687         if (!blr) {
688                 return false;
689         }
690         blr->fsp = fsp;
691
692         if (lock_timeout == -1) {
693                 blr->expire_time.tv_sec = 0;
694                 blr->expire_time.tv_usec = 0; /* Never expire. */
695         } else {
696                 blr->expire_time = timeval_current_ofs_msec(lock_timeout);
697         }
698
699         blr->lock_num = lock_num;
700         blr->smblctx = smblctx;
701         blr->blocking_smblctx = blocking_smblctx;
702         blr->lock_flav = lock_flav;
703         blr->lock_type = lock_type;
704         blr->offset = offset;
705         blr->count = count;
706
707         /* Specific brl_lock() implementations can fill this in. */
708         blr->blr_private = NULL;
709
710         /* Add a pending lock record for this. */
711         status = brl_lock(sconn->msg_ctx,
712                         br_lck,
713                         smblctx,
714                         messaging_server_id(sconn->msg_ctx),
715                         offset,
716                         count,
717                         lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
718                         blr->lock_flav,
719                         true,
720                         NULL);
721
722         if (!NT_STATUS_IS_OK(status)) {
723                 DEBUG(0,("push_blocking_lock_request_smb2: "
724                         "failed to add PENDING_LOCK record.\n"));
725                 TALLOC_FREE(blr);
726                 return false;
727         }
728         state->blr = blr;
729
730         DEBUG(10,("push_blocking_lock_request_smb2: file %s timeout %d\n",
731                 fsp_str_dbg(fsp),
732                 lock_timeout ));
733
734         recalc_smb2_brl_timeout(sconn);
735
736         /* Ensure we'll receive messages when this is unlocked. */
737         if (!sconn->smb2.locks.blocking_lock_unlock_state) {
738                 messaging_register(sconn->msg_ctx, sconn,
739                                 MSG_SMB_UNLOCK, received_unlock_msg);
740                 sconn->smb2.locks.blocking_lock_unlock_state = true;
741         }
742
743         /* allow this request to be canceled */
744         tevent_req_set_cancel_fn(req, smbd_smb2_lock_cancel);
745
746         return true;
747 }
748
749 /****************************************************************
750  Remove a pending lock record under lock.
751 *****************************************************************/
752
753 static void remove_pending_lock(struct smbd_smb2_lock_state *state,
754                         struct blocking_lock_record *blr)
755 {
756         struct byte_range_lock *br_lck = brl_get_locks(
757                                 state, blr->fsp);
758
759         DEBUG(10, ("remove_pending_lock: BLR = %p\n", blr));
760
761         if (br_lck) {
762                 brl_lock_cancel(br_lck,
763                                 blr->smblctx,
764                                 messaging_server_id(blr->fsp->conn->sconn->msg_ctx),
765                                 blr->offset,
766                                 blr->count,
767                                 blr->lock_flav);
768                 TALLOC_FREE(br_lck);
769         }
770 }
771
772 /****************************************************************
773  Re-proccess a blocking lock request.
774  This is equivalent to process_lockingX() inside smbd/blocking.c
775 *****************************************************************/
776
777 static void reprocess_blocked_smb2_lock(struct smbd_smb2_request *smb2req,
778                                 struct timeval tv_curr)
779 {
780         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
781         struct blocking_lock_record *blr = NULL;
782         struct smbd_smb2_lock_state *state = NULL;
783         struct byte_range_lock *br_lck = NULL;
784         struct smbd_lock_element *e = NULL;
785         files_struct *fsp = NULL;
786
787         if (!smb2req->subreq) {
788                 return;
789         }
790         SMBPROFILE_IOBYTES_ASYNC_SET_BUSY(smb2req->profile);
791
792         state = tevent_req_data(smb2req->subreq, struct smbd_smb2_lock_state);
793         if (!state) {
794                 return;
795         }
796
797         blr = state->blr;
798         fsp = blr->fsp;
799
800         /* We can only have one blocked lock in SMB2. */
801         SMB_ASSERT(state->lock_count == 1);
802         SMB_ASSERT(blr->lock_num == 0);
803
804         /* Try and get the outstanding lock. */
805         e = &state->locks[blr->lock_num];
806
807         br_lck = do_lock(fsp->conn->sconn->msg_ctx,
808                         fsp,
809                         e->smblctx,
810                         e->count,
811                         e->offset,
812                         e->brltype,
813                         WINDOWS_LOCK,
814                         true,
815                         &status,
816                         &blr->blocking_smblctx);
817
818         TALLOC_FREE(br_lck);
819
820         if (NT_STATUS_IS_OK(status)) {
821                 /*
822                  * Success - we got the lock.
823                  */
824
825                 DEBUG(3,("reprocess_blocked_smb2_lock SUCCESS file = %s, "
826                         "%s, num_locks=%d\n",
827                         fsp_str_dbg(fsp),
828                         fsp_fnum_dbg(fsp),
829                         (int)state->lock_count));
830
831                 if (state->lock_sequence_element != NULL) {
832                         *state->lock_sequence_element = state->lock_sequence_value;
833                 }
834
835                 remove_pending_lock(state, blr);
836                 tevent_req_done(smb2req->subreq);
837                 return;
838         }
839
840         if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
841                         !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
842                 /*
843                  * We have other than a "can't get lock"
844                  * error. Return an error.
845                  */
846                 remove_pending_lock(state, blr);
847                 tevent_req_nterror(smb2req->subreq, status);
848                 return;
849         }
850
851         /*
852          * We couldn't get the lock for this record.
853          * If the time has expired, return a lock error.
854          */
855
856         if (!timeval_is_zero(&blr->expire_time) &&
857                         timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
858                 remove_pending_lock(state, blr);
859                 tevent_req_nterror(smb2req->subreq, NT_STATUS_LOCK_NOT_GRANTED);
860                 return;
861         }
862
863         /*
864          * Still can't get the lock - keep waiting.
865          */
866
867         DEBUG(10,("reprocess_blocked_smb2_lock: failed to get lock "
868                 "for file %s, %s. Still waiting....\n",
869                 fsp_str_dbg(fsp),
870                 fsp_fnum_dbg(fsp)));
871
872         SMBPROFILE_IOBYTES_ASYNC_SET_IDLE(smb2req->profile);
873         return;
874 }
875
876 /****************************************************************
877  Attempt to proccess all outstanding blocking locks pending on
878  the request queue.
879 *****************************************************************/
880
881 void process_blocking_lock_queue_smb2(
882         struct smbd_server_connection *sconn, struct timeval tv_curr)
883 {
884         struct smbXsrv_connection *xconn = NULL;
885
886         if (sconn != NULL && sconn->client != NULL) {
887                 xconn = sconn->client->connections;
888         }
889
890         for (; xconn != NULL; xconn = xconn->next) {
891                 struct smbd_smb2_request *smb2req, *nextreq;
892
893                 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
894                         const uint8_t *inhdr;
895
896                         nextreq = smb2req->next;
897
898                         if (smb2req->subreq == NULL) {
899                                 /* This message has been processed. */
900                                 continue;
901                         }
902                         if (!tevent_req_is_in_progress(smb2req->subreq)) {
903                                 /* This message has been processed. */
904                                 continue;
905                         }
906
907                         inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
908                         if (SVAL(inhdr, SMB2_HDR_OPCODE) == SMB2_OP_LOCK) {
909                                 reprocess_blocked_smb2_lock(smb2req, tv_curr);
910                         }
911                 }
912         }
913
914         recalc_smb2_brl_timeout(sconn);
915 }
916
917 /****************************************************************************
918  Remove any locks on this fd. Called from file_close().
919 ****************************************************************************/
920
921 void cancel_pending_lock_requests_by_fid_smb2(files_struct *fsp,
922                         struct byte_range_lock *br_lck,
923                         enum file_close_type close_type)
924 {
925         struct smbd_server_connection *sconn = fsp->conn->sconn;
926         struct smbXsrv_connection *xconn = NULL;
927
928         if (sconn != NULL && sconn->client != NULL) {
929                 xconn = sconn->client->connections;
930         }
931
932         for (; xconn != NULL; xconn = xconn->next) {
933                 struct smbd_smb2_request *smb2req, *nextreq;
934
935                 for (smb2req = xconn->smb2.requests; smb2req; smb2req = nextreq) {
936                         struct smbd_smb2_lock_state *state = NULL;
937                         files_struct *fsp_curr = NULL;
938                         struct blocking_lock_record *blr = NULL;
939                         const uint8_t *inhdr;
940
941                         nextreq = smb2req->next;
942
943                         if (smb2req->subreq == NULL) {
944                                 /* This message has been processed. */
945                                 continue;
946                         }
947                         if (!tevent_req_is_in_progress(smb2req->subreq)) {
948                                 /* This message has been processed. */
949                                 continue;
950                         }
951
952                         inhdr = SMBD_SMB2_IN_HDR_PTR(smb2req);
953                         if (SVAL(inhdr, SMB2_HDR_OPCODE) != SMB2_OP_LOCK) {
954                                 /* Not a lock call. */
955                                 continue;
956                         }
957
958                         state = tevent_req_data(smb2req->subreq,
959                                         struct smbd_smb2_lock_state);
960                         if (!state) {
961                                 /* Strange - is this even possible ? */
962                                 continue;
963                         }
964
965                         fsp_curr = smb2req->compat_chain_fsp;
966                         if (fsp_curr == NULL) {
967                                 /* Strange - is this even possible ? */
968                                 continue;
969                         }
970
971                         if (fsp_curr != fsp) {
972                                 /* It's not our fid */
973                                 continue;
974                         }
975
976                         blr = state->blr;
977
978                         /* Remove the entries from the lock db. */
979                         brl_lock_cancel(br_lck,
980                                         blr->smblctx,
981                                         messaging_server_id(sconn->msg_ctx),
982                                         blr->offset,
983                                         blr->count,
984                                         blr->lock_flav);
985
986                         /* Finally end the request. */
987                         if (close_type == SHUTDOWN_CLOSE) {
988                                 tevent_req_done(smb2req->subreq);
989                         } else {
990                                 tevent_req_nterror(smb2req->subreq,
991                                         NT_STATUS_RANGE_NOT_LOCKED);
992                         }
993                 }
994         }
995 }