1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Blocking Locking functions
|
---|
4 | Copyright (C) Jeremy Allison 1998-2003
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #undef DBGC_CLASS
|
---|
22 | #define DBGC_CLASS DBGC_LOCKING
|
---|
23 |
|
---|
24 | /****************************************************************************
|
---|
25 | This is the structure to queue to implement blocking locks.
|
---|
26 | notify. It consists of the requesting SMB and the expiry time.
|
---|
27 | *****************************************************************************/
|
---|
28 |
|
---|
29 | typedef struct _blocking_lock_record {
|
---|
30 | struct _blocking_lock_record *next;
|
---|
31 | struct _blocking_lock_record *prev;
|
---|
32 | int com_type;
|
---|
33 | files_struct *fsp;
|
---|
34 | struct timeval expire_time;
|
---|
35 | int lock_num;
|
---|
36 | SMB_BIG_UINT offset;
|
---|
37 | SMB_BIG_UINT count;
|
---|
38 | uint32 lock_pid;
|
---|
39 | uint32 blocking_pid; /* PID that blocks us. */
|
---|
40 | enum brl_flavour lock_flav;
|
---|
41 | enum brl_type lock_type;
|
---|
42 | char *inbuf;
|
---|
43 | int length;
|
---|
44 | bool encrypted;
|
---|
45 | } blocking_lock_record;
|
---|
46 |
|
---|
47 | /* dlink list we store pending lock records on. */
|
---|
48 | static blocking_lock_record *blocking_lock_queue;
|
---|
49 |
|
---|
50 | /* dlink list we move cancelled lock records onto. */
|
---|
51 | static blocking_lock_record *blocking_lock_cancelled_queue;
|
---|
52 |
|
---|
53 | /* The event that makes us process our blocking lock queue */
|
---|
54 | static struct timed_event *brl_timeout;
|
---|
55 |
|
---|
56 | /****************************************************************************
|
---|
57 | Destructor for the above structure.
|
---|
58 | ****************************************************************************/
|
---|
59 |
|
---|
60 | static void free_blocking_lock_record(blocking_lock_record *blr)
|
---|
61 | {
|
---|
62 | SAFE_FREE(blr->inbuf);
|
---|
63 | SAFE_FREE(blr);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /****************************************************************************
|
---|
67 | Determine if this is a secondary element of a chained SMB.
|
---|
68 | **************************************************************************/
|
---|
69 |
|
---|
70 | static bool in_chained_smb(void)
|
---|
71 | {
|
---|
72 | return (chain_size != 0);
|
---|
73 | }
|
---|
74 |
|
---|
75 | static void received_unlock_msg(struct messaging_context *msg,
|
---|
76 | void *private_data,
|
---|
77 | uint32_t msg_type,
|
---|
78 | struct server_id server_id,
|
---|
79 | DATA_BLOB *data);
|
---|
80 | static void process_blocking_lock_queue(void);
|
---|
81 |
|
---|
82 | static void brl_timeout_fn(struct event_context *event_ctx,
|
---|
83 | struct timed_event *te,
|
---|
84 | struct timeval now,
|
---|
85 | void *private_data)
|
---|
86 | {
|
---|
87 | SMB_ASSERT(brl_timeout == te);
|
---|
88 | TALLOC_FREE(brl_timeout);
|
---|
89 |
|
---|
90 | change_to_root_user(); /* TODO: Possibly run all timed events as
|
---|
91 | * root */
|
---|
92 |
|
---|
93 | process_blocking_lock_queue();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /****************************************************************************
|
---|
97 | After a change to blocking_lock_queue, recalculate the timed_event for the
|
---|
98 | next processing.
|
---|
99 | ****************************************************************************/
|
---|
100 |
|
---|
101 | static bool recalc_brl_timeout(void)
|
---|
102 | {
|
---|
103 | blocking_lock_record *brl;
|
---|
104 | struct timeval next_timeout;
|
---|
105 |
|
---|
106 | TALLOC_FREE(brl_timeout);
|
---|
107 |
|
---|
108 | next_timeout = timeval_zero();
|
---|
109 |
|
---|
110 | for (brl = blocking_lock_queue; brl; brl = brl->next) {
|
---|
111 | if (timeval_is_zero(&brl->expire_time)) {
|
---|
112 | /*
|
---|
113 | * If we're blocked on pid 0xFFFFFFFF this is
|
---|
114 | * a POSIX lock, so calculate a timeout of
|
---|
115 | * 10 seconds into the future.
|
---|
116 | */
|
---|
117 | if (brl->blocking_pid == 0xFFFFFFFF) {
|
---|
118 | struct timeval psx_to = timeval_current_ofs(10, 0);
|
---|
119 | next_timeout = timeval_min(&next_timeout, &psx_to);
|
---|
120 | }
|
---|
121 |
|
---|
122 | continue;
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (timeval_is_zero(&next_timeout)) {
|
---|
126 | next_timeout = brl->expire_time;
|
---|
127 | }
|
---|
128 | else {
|
---|
129 | next_timeout = timeval_min(&next_timeout,
|
---|
130 | &brl->expire_time);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (timeval_is_zero(&next_timeout)) {
|
---|
135 | return True;
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (!(brl_timeout = event_add_timed(smbd_event_context(), NULL,
|
---|
139 | next_timeout,
|
---|
140 | brl_timeout_fn, NULL))) {
|
---|
141 | return False;
|
---|
142 | }
|
---|
143 |
|
---|
144 | return True;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /****************************************************************************
|
---|
149 | Function to push a blocking lock request onto the lock queue.
|
---|
150 | ****************************************************************************/
|
---|
151 |
|
---|
152 | bool push_blocking_lock_request( struct byte_range_lock *br_lck,
|
---|
153 | const struct smb_request *req,
|
---|
154 | files_struct *fsp,
|
---|
155 | int lock_timeout,
|
---|
156 | int lock_num,
|
---|
157 | uint32 lock_pid,
|
---|
158 | enum brl_type lock_type,
|
---|
159 | enum brl_flavour lock_flav,
|
---|
160 | SMB_BIG_UINT offset,
|
---|
161 | SMB_BIG_UINT count,
|
---|
162 | uint32 blocking_pid)
|
---|
163 | {
|
---|
164 | static bool set_lock_msg;
|
---|
165 | size_t length = smb_len(req->inbuf)+4;
|
---|
166 | blocking_lock_record *blr;
|
---|
167 | NTSTATUS status;
|
---|
168 |
|
---|
169 | if(in_chained_smb() ) {
|
---|
170 | DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
|
---|
171 | return False;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Now queue an entry on the blocking lock queue. We setup
|
---|
176 | * the expiration time here.
|
---|
177 | */
|
---|
178 |
|
---|
179 | if((blr = SMB_MALLOC_P(blocking_lock_record)) == NULL) {
|
---|
180 | DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
|
---|
181 | return False;
|
---|
182 | }
|
---|
183 |
|
---|
184 | blr->next = NULL;
|
---|
185 | blr->prev = NULL;
|
---|
186 |
|
---|
187 | if((blr->inbuf = (char *)SMB_MALLOC(length)) == NULL) {
|
---|
188 | DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
|
---|
189 | SAFE_FREE(blr);
|
---|
190 | return False;
|
---|
191 | }
|
---|
192 |
|
---|
193 | blr->com_type = CVAL(req->inbuf,smb_com);
|
---|
194 | blr->fsp = fsp;
|
---|
195 | if (lock_timeout == -1) {
|
---|
196 | blr->expire_time.tv_sec = 0;
|
---|
197 | blr->expire_time.tv_usec = 0; /* Never expire. */
|
---|
198 | } else {
|
---|
199 | blr->expire_time = timeval_current_ofs(lock_timeout/1000,
|
---|
200 | (lock_timeout % 1000) * 1000);
|
---|
201 | }
|
---|
202 | blr->lock_num = lock_num;
|
---|
203 | blr->lock_pid = lock_pid;
|
---|
204 | blr->blocking_pid = blocking_pid;
|
---|
205 | blr->lock_flav = lock_flav;
|
---|
206 | blr->lock_type = lock_type;
|
---|
207 | blr->offset = offset;
|
---|
208 | blr->count = count;
|
---|
209 | memcpy(blr->inbuf, req->inbuf, length);
|
---|
210 | blr->length = length;
|
---|
211 | blr->encrypted = req->encrypted;
|
---|
212 |
|
---|
213 | /* Add a pending lock record for this. */
|
---|
214 | status = brl_lock(smbd_messaging_context(), br_lck,
|
---|
215 | lock_pid,
|
---|
216 | procid_self(),
|
---|
217 | offset,
|
---|
218 | count,
|
---|
219 | lock_type == READ_LOCK ? PENDING_READ_LOCK : PENDING_WRITE_LOCK,
|
---|
220 | blr->lock_flav,
|
---|
221 | lock_timeout ? True : False, /* blocking_lock. */
|
---|
222 | NULL);
|
---|
223 |
|
---|
224 | if (!NT_STATUS_IS_OK(status)) {
|
---|
225 | DEBUG(0,("push_blocking_lock_request: failed to add PENDING_LOCK record.\n"));
|
---|
226 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
227 | free_blocking_lock_record(blr);
|
---|
228 | return False;
|
---|
229 | }
|
---|
230 |
|
---|
231 | DLIST_ADD_END(blocking_lock_queue, blr, blocking_lock_record *);
|
---|
232 | recalc_brl_timeout();
|
---|
233 |
|
---|
234 | /* Ensure we'll receive messages when this is unlocked. */
|
---|
235 | if (!set_lock_msg) {
|
---|
236 | messaging_register(smbd_messaging_context(), NULL,
|
---|
237 | MSG_SMB_UNLOCK, received_unlock_msg);
|
---|
238 | set_lock_msg = True;
|
---|
239 | }
|
---|
240 |
|
---|
241 | DEBUG(3,("push_blocking_lock_request: lock request length=%u blocked with "
|
---|
242 | "expiry time (%u sec. %u usec) (+%d msec) for fnum = %d, name = %s\n",
|
---|
243 | (unsigned int)length, (unsigned int)blr->expire_time.tv_sec,
|
---|
244 | (unsigned int)blr->expire_time.tv_usec, lock_timeout,
|
---|
245 | blr->fsp->fnum, blr->fsp->fsp_name ));
|
---|
246 |
|
---|
247 | /* Push the MID of this packet on the signing queue. */
|
---|
248 | srv_defer_sign_response(SVAL(req->inbuf,smb_mid));
|
---|
249 |
|
---|
250 | return True;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /****************************************************************************
|
---|
254 | Return a lockingX success SMB.
|
---|
255 | *****************************************************************************/
|
---|
256 |
|
---|
257 | static void reply_lockingX_success(blocking_lock_record *blr)
|
---|
258 | {
|
---|
259 | struct smb_request *req;
|
---|
260 |
|
---|
261 | if (!(req = talloc(talloc_tos(), struct smb_request))) {
|
---|
262 | smb_panic("Could not allocate smb_request");
|
---|
263 | }
|
---|
264 |
|
---|
265 | init_smb_request(req, (uint8 *)blr->inbuf, 0, blr->encrypted);
|
---|
266 | reply_outbuf(req, 2, 0);
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * As this message is a lockingX call we must handle
|
---|
270 | * any following chained message correctly.
|
---|
271 | * This is normally handled in construct_reply(),
|
---|
272 | * but as that calls switch_message, we can't use
|
---|
273 | * that here and must set up the chain info manually.
|
---|
274 | */
|
---|
275 |
|
---|
276 | chain_reply(req);
|
---|
277 |
|
---|
278 | if (!srv_send_smb(smbd_server_fd(),
|
---|
279 | (char *)req->outbuf,
|
---|
280 | IS_CONN_ENCRYPTED(blr->fsp->conn))) {
|
---|
281 | exit_server_cleanly("send_blocking_reply: srv_send_smb failed.");
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | /****************************************************************************
|
---|
286 | Return a generic lock fail error blocking call.
|
---|
287 | *****************************************************************************/
|
---|
288 |
|
---|
289 | static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
|
---|
290 | {
|
---|
291 | char outbuf[smb_size];
|
---|
292 | char *inbuf = blr->inbuf;
|
---|
293 |
|
---|
294 | construct_reply_common(inbuf, outbuf);
|
---|
295 |
|
---|
296 | /* whenever a timeout is given w2k maps LOCK_NOT_GRANTED to
|
---|
297 | FILE_LOCK_CONFLICT! (tridge) */
|
---|
298 | if (NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
|
---|
299 | status = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_LOCK_CONFLICT)) {
|
---|
303 | /* Store the last lock error. */
|
---|
304 | files_struct *fsp = blr->fsp;
|
---|
305 |
|
---|
306 | if (fsp) {
|
---|
307 | fsp->last_lock_failure.context.smbpid = blr->lock_pid;
|
---|
308 | fsp->last_lock_failure.context.tid = fsp->conn->cnum;
|
---|
309 | fsp->last_lock_failure.context.pid = procid_self();
|
---|
310 | fsp->last_lock_failure.start = blr->offset;
|
---|
311 | fsp->last_lock_failure.size = blr->count;
|
---|
312 | fsp->last_lock_failure.fnum = fsp->fnum;
|
---|
313 | fsp->last_lock_failure.lock_type = READ_LOCK; /* Don't care. */
|
---|
314 | fsp->last_lock_failure.lock_flav = blr->lock_flav;
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | ERROR_NT(status);
|
---|
319 | if (!srv_send_smb(smbd_server_fd(),outbuf, blr->encrypted)) {
|
---|
320 | exit_server_cleanly("generic_blocking_lock_error: srv_send_smb failed.");
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | /****************************************************************************
|
---|
325 | Return a lock fail error for a lockingX call. Undo all the locks we have
|
---|
326 | obtained first.
|
---|
327 | *****************************************************************************/
|
---|
328 |
|
---|
329 | static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
|
---|
330 | {
|
---|
331 | char *inbuf = blr->inbuf;
|
---|
332 | files_struct *fsp = blr->fsp;
|
---|
333 | uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
|
---|
334 | SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
|
---|
335 | uint32 lock_pid;
|
---|
336 | unsigned char locktype = CVAL(inbuf,smb_vwv3);
|
---|
337 | bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
|
---|
338 | char *data;
|
---|
339 | int i;
|
---|
340 |
|
---|
341 | data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
|
---|
342 |
|
---|
343 | /*
|
---|
344 | * Data now points at the beginning of the list
|
---|
345 | * of smb_lkrng structs.
|
---|
346 | */
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Ensure we don't do a remove on the lock that just failed,
|
---|
350 | * as under POSIX rules, if we have a lock already there, we
|
---|
351 | * will delete it (and we shouldn't) .....
|
---|
352 | */
|
---|
353 |
|
---|
354 | for(i = blr->lock_num - 1; i >= 0; i--) {
|
---|
355 | bool err;
|
---|
356 |
|
---|
357 | lock_pid = get_lock_pid( data, i, large_file_format);
|
---|
358 | count = get_lock_count( data, i, large_file_format);
|
---|
359 | offset = get_lock_offset( data, i, large_file_format, &err);
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * We know err cannot be set as if it was the lock
|
---|
363 | * request would never have been queued. JRA.
|
---|
364 | */
|
---|
365 |
|
---|
366 | do_unlock(smbd_messaging_context(),
|
---|
367 | fsp,
|
---|
368 | lock_pid,
|
---|
369 | count,
|
---|
370 | offset,
|
---|
371 | WINDOWS_LOCK);
|
---|
372 | }
|
---|
373 |
|
---|
374 | generic_blocking_lock_error(blr, status);
|
---|
375 | }
|
---|
376 |
|
---|
377 | /****************************************************************************
|
---|
378 | Return a lock fail error.
|
---|
379 | *****************************************************************************/
|
---|
380 |
|
---|
381 | static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
|
---|
382 | {
|
---|
383 | switch(blr->com_type) {
|
---|
384 | case SMBlockingX:
|
---|
385 | reply_lockingX_error(blr, status);
|
---|
386 | break;
|
---|
387 | case SMBtrans2:
|
---|
388 | case SMBtranss2:
|
---|
389 | {
|
---|
390 | char outbuf[smb_size];
|
---|
391 | char *inbuf = blr->inbuf;
|
---|
392 | construct_reply_common(inbuf, outbuf);
|
---|
393 | /* construct_reply_common has done us the favor to pre-fill the
|
---|
394 | * command field with SMBtranss2 which is wrong :-)
|
---|
395 | */
|
---|
396 | SCVAL(outbuf,smb_com,SMBtrans2);
|
---|
397 | ERROR_NT(status);
|
---|
398 | if (!srv_send_smb(smbd_server_fd(),
|
---|
399 | outbuf,
|
---|
400 | IS_CONN_ENCRYPTED(blr->fsp->conn))) {
|
---|
401 | exit_server_cleanly("blocking_lock_reply_error: srv_send_smb failed.");
|
---|
402 | }
|
---|
403 | break;
|
---|
404 | }
|
---|
405 | default:
|
---|
406 | DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
|
---|
407 | exit_server("PANIC - unknown type on blocking lock queue");
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | /****************************************************************************
|
---|
412 | Attempt to finish off getting all pending blocking locks for a lockingX call.
|
---|
413 | Returns True if we want to be removed from the list.
|
---|
414 | *****************************************************************************/
|
---|
415 |
|
---|
416 | static bool process_lockingX(blocking_lock_record *blr)
|
---|
417 | {
|
---|
418 | char *inbuf = blr->inbuf;
|
---|
419 | unsigned char locktype = CVAL(inbuf,smb_vwv3);
|
---|
420 | files_struct *fsp = blr->fsp;
|
---|
421 | uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
|
---|
422 | uint16 num_locks = SVAL(inbuf,smb_vwv7);
|
---|
423 | SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
|
---|
424 | uint32 lock_pid;
|
---|
425 | bool large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
|
---|
426 | char *data;
|
---|
427 | NTSTATUS status = NT_STATUS_OK;
|
---|
428 |
|
---|
429 | data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * Data now points at the beginning of the list
|
---|
433 | * of smb_lkrng structs.
|
---|
434 | */
|
---|
435 |
|
---|
436 | for(; blr->lock_num < num_locks; blr->lock_num++) {
|
---|
437 | struct byte_range_lock *br_lck = NULL;
|
---|
438 | bool err;
|
---|
439 |
|
---|
440 | lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
|
---|
441 | count = get_lock_count( data, blr->lock_num, large_file_format);
|
---|
442 | offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * We know err cannot be set as if it was the lock
|
---|
446 | * request would never have been queued. JRA.
|
---|
447 | */
|
---|
448 | errno = 0;
|
---|
449 | br_lck = do_lock(smbd_messaging_context(),
|
---|
450 | fsp,
|
---|
451 | lock_pid,
|
---|
452 | count,
|
---|
453 | offset,
|
---|
454 | ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
|
---|
455 | READ_LOCK : WRITE_LOCK),
|
---|
456 | WINDOWS_LOCK,
|
---|
457 | True,
|
---|
458 | &status,
|
---|
459 | &blr->blocking_pid);
|
---|
460 |
|
---|
461 | TALLOC_FREE(br_lck);
|
---|
462 |
|
---|
463 | if (NT_STATUS_IS_ERR(status)) {
|
---|
464 | break;
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | if(blr->lock_num == num_locks) {
|
---|
469 | /*
|
---|
470 | * Success - we got all the locks.
|
---|
471 | */
|
---|
472 |
|
---|
473 | DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
|
---|
474 | fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
|
---|
475 |
|
---|
476 | reply_lockingX_success(blr);
|
---|
477 | return True;
|
---|
478 | } else if (!NT_STATUS_EQUAL(status,NT_STATUS_LOCK_NOT_GRANTED) &&
|
---|
479 | !NT_STATUS_EQUAL(status,NT_STATUS_FILE_LOCK_CONFLICT)) {
|
---|
480 | /*
|
---|
481 | * We have other than a "can't get lock"
|
---|
482 | * error. Free any locks we had and return an error.
|
---|
483 | * Return True so we get dequeued.
|
---|
484 | */
|
---|
485 |
|
---|
486 | blocking_lock_reply_error(blr, status);
|
---|
487 | return True;
|
---|
488 | }
|
---|
489 |
|
---|
490 | /*
|
---|
491 | * Still can't get all the locks - keep waiting.
|
---|
492 | */
|
---|
493 |
|
---|
494 | DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
|
---|
495 | Waiting....\n",
|
---|
496 | blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
|
---|
497 |
|
---|
498 | return False;
|
---|
499 | }
|
---|
500 |
|
---|
501 | /****************************************************************************
|
---|
502 | Attempt to get the posix lock request from a SMBtrans2 call.
|
---|
503 | Returns True if we want to be removed from the list.
|
---|
504 | *****************************************************************************/
|
---|
505 |
|
---|
506 | static bool process_trans2(blocking_lock_record *blr)
|
---|
507 | {
|
---|
508 | struct smb_request *req;
|
---|
509 | char params[2];
|
---|
510 | NTSTATUS status;
|
---|
511 | struct byte_range_lock *br_lck = do_lock(smbd_messaging_context(),
|
---|
512 | blr->fsp,
|
---|
513 | blr->lock_pid,
|
---|
514 | blr->count,
|
---|
515 | blr->offset,
|
---|
516 | blr->lock_type,
|
---|
517 | blr->lock_flav,
|
---|
518 | True,
|
---|
519 | &status,
|
---|
520 | &blr->blocking_pid);
|
---|
521 | TALLOC_FREE(br_lck);
|
---|
522 |
|
---|
523 | if (!NT_STATUS_IS_OK(status)) {
|
---|
524 | if (ERROR_WAS_LOCK_DENIED(status)) {
|
---|
525 | /* Still can't get the lock, just keep waiting. */
|
---|
526 | return False;
|
---|
527 | }
|
---|
528 | /*
|
---|
529 | * We have other than a "can't get lock"
|
---|
530 | * error. Send an error and return True so we get dequeued.
|
---|
531 | */
|
---|
532 | blocking_lock_reply_error(blr, status);
|
---|
533 | return True;
|
---|
534 | }
|
---|
535 |
|
---|
536 | /* We finally got the lock, return success. */
|
---|
537 |
|
---|
538 | if (!(req = talloc(talloc_tos(), struct smb_request))) {
|
---|
539 | blocking_lock_reply_error(blr, NT_STATUS_NO_MEMORY);
|
---|
540 | return True;
|
---|
541 | }
|
---|
542 |
|
---|
543 | init_smb_request(req, (uint8 *)blr->inbuf, 0, blr->encrypted);
|
---|
544 |
|
---|
545 | SCVAL(req->inbuf, smb_com, SMBtrans2);
|
---|
546 | SSVAL(params,0,0);
|
---|
547 | /* Fake up max_data_bytes here - we know it fits. */
|
---|
548 | send_trans2_replies(blr->fsp->conn, req, params, 2, NULL, 0, 0xffff);
|
---|
549 | return True;
|
---|
550 | }
|
---|
551 |
|
---|
552 |
|
---|
553 | /****************************************************************************
|
---|
554 | Process a blocking lock SMB.
|
---|
555 | Returns True if we want to be removed from the list.
|
---|
556 | *****************************************************************************/
|
---|
557 |
|
---|
558 | static bool blocking_lock_record_process(blocking_lock_record *blr)
|
---|
559 | {
|
---|
560 | switch(blr->com_type) {
|
---|
561 | case SMBlockingX:
|
---|
562 | return process_lockingX(blr);
|
---|
563 | case SMBtrans2:
|
---|
564 | case SMBtranss2:
|
---|
565 | return process_trans2(blr);
|
---|
566 | default:
|
---|
567 | DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
|
---|
568 | exit_server("PANIC - unknown type on blocking lock queue");
|
---|
569 | }
|
---|
570 | return False; /* Keep compiler happy. */
|
---|
571 | }
|
---|
572 |
|
---|
573 | /****************************************************************************
|
---|
574 | Cancel entries by fnum from the blocking lock pending queue.
|
---|
575 | *****************************************************************************/
|
---|
576 |
|
---|
577 | void cancel_pending_lock_requests_by_fid(files_struct *fsp, struct byte_range_lock *br_lck)
|
---|
578 | {
|
---|
579 | blocking_lock_record *blr, *next = NULL;
|
---|
580 |
|
---|
581 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
582 | next = blr->next;
|
---|
583 | if(blr->fsp->fnum == fsp->fnum) {
|
---|
584 | unsigned char locktype = 0;
|
---|
585 |
|
---|
586 | if (blr->com_type == SMBlockingX) {
|
---|
587 | locktype = CVAL(blr->inbuf,smb_vwv3);
|
---|
588 | }
|
---|
589 |
|
---|
590 | if (br_lck) {
|
---|
591 | DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
|
---|
592 | file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
|
---|
593 |
|
---|
594 | brl_lock_cancel(br_lck,
|
---|
595 | blr->lock_pid,
|
---|
596 | procid_self(),
|
---|
597 | blr->offset,
|
---|
598 | blr->count,
|
---|
599 | blr->lock_flav);
|
---|
600 |
|
---|
601 | blocking_lock_cancel(fsp,
|
---|
602 | blr->lock_pid,
|
---|
603 | blr->offset,
|
---|
604 | blr->count,
|
---|
605 | blr->lock_flav,
|
---|
606 | locktype,
|
---|
607 | NT_STATUS_RANGE_NOT_LOCKED);
|
---|
608 | }
|
---|
609 | /* We're closing the file fsp here, so ensure
|
---|
610 | * we don't have a dangling pointer. */
|
---|
611 | blr->fsp = NULL;
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | /****************************************************************************
|
---|
617 | Delete entries by mid from the blocking lock pending queue. Always send reply.
|
---|
618 | *****************************************************************************/
|
---|
619 |
|
---|
620 | void remove_pending_lock_requests_by_mid(int mid)
|
---|
621 | {
|
---|
622 | blocking_lock_record *blr, *next = NULL;
|
---|
623 |
|
---|
624 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
625 | next = blr->next;
|
---|
626 | if(SVAL(blr->inbuf,smb_mid) == mid) {
|
---|
627 | files_struct *fsp = blr->fsp;
|
---|
628 | struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
629 |
|
---|
630 | if (br_lck) {
|
---|
631 | DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
|
---|
632 | file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
|
---|
633 |
|
---|
634 | brl_lock_cancel(br_lck,
|
---|
635 | blr->lock_pid,
|
---|
636 | procid_self(),
|
---|
637 | blr->offset,
|
---|
638 | blr->count,
|
---|
639 | blr->lock_flav);
|
---|
640 | TALLOC_FREE(br_lck);
|
---|
641 | }
|
---|
642 |
|
---|
643 | blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
|
---|
644 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
645 | free_blocking_lock_record(blr);
|
---|
646 | }
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | /****************************************************************************
|
---|
651 | Is this mid a blocking lock request on the queue ?
|
---|
652 | *****************************************************************************/
|
---|
653 |
|
---|
654 | bool blocking_lock_was_deferred(int mid)
|
---|
655 | {
|
---|
656 | blocking_lock_record *blr, *next = NULL;
|
---|
657 |
|
---|
658 | for(blr = blocking_lock_queue; blr; blr = next) {
|
---|
659 | next = blr->next;
|
---|
660 | if(SVAL(blr->inbuf,smb_mid) == mid) {
|
---|
661 | return True;
|
---|
662 | }
|
---|
663 | }
|
---|
664 | return False;
|
---|
665 | }
|
---|
666 |
|
---|
667 | /****************************************************************************
|
---|
668 | Set a flag as an unlock request affects one of our pending locks.
|
---|
669 | *****************************************************************************/
|
---|
670 |
|
---|
671 | static void received_unlock_msg(struct messaging_context *msg,
|
---|
672 | void *private_data,
|
---|
673 | uint32_t msg_type,
|
---|
674 | struct server_id server_id,
|
---|
675 | DATA_BLOB *data)
|
---|
676 | {
|
---|
677 | DEBUG(10,("received_unlock_msg\n"));
|
---|
678 | process_blocking_lock_queue();
|
---|
679 | }
|
---|
680 |
|
---|
681 | /****************************************************************************
|
---|
682 | Process the blocking lock queue. Note that this is only called as root.
|
---|
683 | *****************************************************************************/
|
---|
684 |
|
---|
685 | static void process_blocking_lock_queue(void)
|
---|
686 | {
|
---|
687 | struct timeval tv_curr = timeval_current();
|
---|
688 | blocking_lock_record *blr, *next = NULL;
|
---|
689 | bool recalc_timeout = False;
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Go through the queue and see if we can get any of the locks.
|
---|
693 | */
|
---|
694 |
|
---|
695 | for (blr = blocking_lock_queue; blr; blr = next) {
|
---|
696 | connection_struct *conn = NULL;
|
---|
697 | uint16 vuid;
|
---|
698 | files_struct *fsp = NULL;
|
---|
699 |
|
---|
700 | next = blr->next;
|
---|
701 |
|
---|
702 | /*
|
---|
703 | * Ensure we don't have any old chain_fsp values
|
---|
704 | * sitting around....
|
---|
705 | */
|
---|
706 | chain_size = 0;
|
---|
707 | file_chain_reset();
|
---|
708 | fsp = blr->fsp;
|
---|
709 |
|
---|
710 | conn = conn_find(SVAL(blr->inbuf,smb_tid));
|
---|
711 | vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
|
---|
712 | SVAL(blr->inbuf,smb_uid);
|
---|
713 |
|
---|
714 | DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
|
---|
715 | fsp->fnum, fsp->fsp_name ));
|
---|
716 |
|
---|
717 | if(!change_to_user(conn,vuid)) {
|
---|
718 | struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
719 |
|
---|
720 | /*
|
---|
721 | * Remove the entry and return an error to the client.
|
---|
722 | */
|
---|
723 |
|
---|
724 | if (br_lck) {
|
---|
725 | brl_lock_cancel(br_lck,
|
---|
726 | blr->lock_pid,
|
---|
727 | procid_self(),
|
---|
728 | blr->offset,
|
---|
729 | blr->count,
|
---|
730 | blr->lock_flav);
|
---|
731 | TALLOC_FREE(br_lck);
|
---|
732 | }
|
---|
733 |
|
---|
734 | DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
|
---|
735 | vuid ));
|
---|
736 | blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
|
---|
737 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
738 | free_blocking_lock_record(blr);
|
---|
739 | recalc_timeout = True;
|
---|
740 | continue;
|
---|
741 | }
|
---|
742 |
|
---|
743 | if(!set_current_service(conn,SVAL(blr->inbuf,smb_flg),True)) {
|
---|
744 | struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
745 |
|
---|
746 | /*
|
---|
747 | * Remove the entry and return an error to the client.
|
---|
748 | */
|
---|
749 |
|
---|
750 | if (br_lck) {
|
---|
751 | brl_lock_cancel(br_lck,
|
---|
752 | blr->lock_pid,
|
---|
753 | procid_self(),
|
---|
754 | blr->offset,
|
---|
755 | blr->count,
|
---|
756 | blr->lock_flav);
|
---|
757 | TALLOC_FREE(br_lck);
|
---|
758 | }
|
---|
759 |
|
---|
760 | DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
|
---|
761 | blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
|
---|
762 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
763 | free_blocking_lock_record(blr);
|
---|
764 | recalc_timeout = True;
|
---|
765 | change_to_root_user();
|
---|
766 | continue;
|
---|
767 | }
|
---|
768 |
|
---|
769 | /*
|
---|
770 | * Go through the remaining locks and try and obtain them.
|
---|
771 | * The call returns True if all locks were obtained successfully
|
---|
772 | * and False if we still need to wait.
|
---|
773 | */
|
---|
774 |
|
---|
775 | if(blocking_lock_record_process(blr)) {
|
---|
776 | struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
777 |
|
---|
778 | if (br_lck) {
|
---|
779 | brl_lock_cancel(br_lck,
|
---|
780 | blr->lock_pid,
|
---|
781 | procid_self(),
|
---|
782 | blr->offset,
|
---|
783 | blr->count,
|
---|
784 | blr->lock_flav);
|
---|
785 | TALLOC_FREE(br_lck);
|
---|
786 | }
|
---|
787 |
|
---|
788 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
789 | free_blocking_lock_record(blr);
|
---|
790 | recalc_timeout = True;
|
---|
791 | change_to_root_user();
|
---|
792 | continue;
|
---|
793 | }
|
---|
794 |
|
---|
795 | change_to_root_user();
|
---|
796 |
|
---|
797 | /*
|
---|
798 | * We couldn't get the locks for this record on the list.
|
---|
799 | * If the time has expired, return a lock error.
|
---|
800 | */
|
---|
801 |
|
---|
802 | if (!timeval_is_zero(&blr->expire_time) && timeval_compare(&blr->expire_time, &tv_curr) <= 0) {
|
---|
803 | struct byte_range_lock *br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
804 |
|
---|
805 | /*
|
---|
806 | * Lock expired - throw away all previously
|
---|
807 | * obtained locks and return lock error.
|
---|
808 | */
|
---|
809 |
|
---|
810 | if (br_lck) {
|
---|
811 | DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
|
---|
812 | fsp->fnum, fsp->fsp_name ));
|
---|
813 |
|
---|
814 | brl_lock_cancel(br_lck,
|
---|
815 | blr->lock_pid,
|
---|
816 | procid_self(),
|
---|
817 | blr->offset,
|
---|
818 | blr->count,
|
---|
819 | blr->lock_flav);
|
---|
820 | TALLOC_FREE(br_lck);
|
---|
821 | }
|
---|
822 |
|
---|
823 | blocking_lock_reply_error(blr,NT_STATUS_FILE_LOCK_CONFLICT);
|
---|
824 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
825 | free_blocking_lock_record(blr);
|
---|
826 | recalc_timeout = True;
|
---|
827 | }
|
---|
828 | }
|
---|
829 |
|
---|
830 | if (recalc_timeout) {
|
---|
831 | recalc_brl_timeout();
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | /****************************************************************************
|
---|
836 | Handle a cancel message. Lock already moved onto the cancel queue.
|
---|
837 | *****************************************************************************/
|
---|
838 |
|
---|
839 | #define MSG_BLOCKING_LOCK_CANCEL_SIZE (sizeof(blocking_lock_record *) + sizeof(NTSTATUS))
|
---|
840 |
|
---|
841 | static void process_blocking_lock_cancel_message(struct messaging_context *ctx,
|
---|
842 | void *private_data,
|
---|
843 | uint32_t msg_type,
|
---|
844 | struct server_id server_id,
|
---|
845 | DATA_BLOB *data)
|
---|
846 | {
|
---|
847 | NTSTATUS err;
|
---|
848 | const char *msg = (const char *)data->data;
|
---|
849 | blocking_lock_record *blr;
|
---|
850 |
|
---|
851 | if (data->data == NULL) {
|
---|
852 | smb_panic("process_blocking_lock_cancel_message: null msg");
|
---|
853 | }
|
---|
854 |
|
---|
855 | if (data->length != MSG_BLOCKING_LOCK_CANCEL_SIZE) {
|
---|
856 | DEBUG(0, ("process_blocking_lock_cancel_message: "
|
---|
857 | "Got invalid msg len %d\n", (int)data->length));
|
---|
858 | smb_panic("process_blocking_lock_cancel_message: bad msg");
|
---|
859 | }
|
---|
860 |
|
---|
861 | memcpy(&blr, msg, sizeof(blr));
|
---|
862 | memcpy(&err, &msg[sizeof(blr)], sizeof(NTSTATUS));
|
---|
863 |
|
---|
864 | DEBUG(10,("process_blocking_lock_cancel_message: returning error %s\n",
|
---|
865 | nt_errstr(err) ));
|
---|
866 |
|
---|
867 | blocking_lock_reply_error(blr, err);
|
---|
868 | DLIST_REMOVE(blocking_lock_cancelled_queue, blr);
|
---|
869 | free_blocking_lock_record(blr);
|
---|
870 | }
|
---|
871 |
|
---|
872 | /****************************************************************************
|
---|
873 | Send ourselves a blocking lock cancelled message. Handled asynchronously above.
|
---|
874 | *****************************************************************************/
|
---|
875 |
|
---|
876 | bool blocking_lock_cancel(files_struct *fsp,
|
---|
877 | uint32 lock_pid,
|
---|
878 | SMB_BIG_UINT offset,
|
---|
879 | SMB_BIG_UINT count,
|
---|
880 | enum brl_flavour lock_flav,
|
---|
881 | unsigned char locktype,
|
---|
882 | NTSTATUS err)
|
---|
883 | {
|
---|
884 | static bool initialized;
|
---|
885 | char msg[MSG_BLOCKING_LOCK_CANCEL_SIZE];
|
---|
886 | blocking_lock_record *blr;
|
---|
887 |
|
---|
888 | if (!initialized) {
|
---|
889 | /* Register our message. */
|
---|
890 | messaging_register(smbd_messaging_context(), NULL,
|
---|
891 | MSG_SMB_BLOCKING_LOCK_CANCEL,
|
---|
892 | process_blocking_lock_cancel_message);
|
---|
893 |
|
---|
894 | initialized = True;
|
---|
895 | }
|
---|
896 |
|
---|
897 | for (blr = blocking_lock_queue; blr; blr = blr->next) {
|
---|
898 | if (fsp == blr->fsp &&
|
---|
899 | lock_pid == blr->lock_pid &&
|
---|
900 | offset == blr->offset &&
|
---|
901 | count == blr->count &&
|
---|
902 | lock_flav == blr->lock_flav) {
|
---|
903 | break;
|
---|
904 | }
|
---|
905 | }
|
---|
906 |
|
---|
907 | if (!blr) {
|
---|
908 | return False;
|
---|
909 | }
|
---|
910 |
|
---|
911 | /* Check the flags are right. */
|
---|
912 | if (blr->com_type == SMBlockingX &&
|
---|
913 | (locktype & LOCKING_ANDX_LARGE_FILES) !=
|
---|
914 | (CVAL(blr->inbuf,smb_vwv3) & LOCKING_ANDX_LARGE_FILES)) {
|
---|
915 | return False;
|
---|
916 | }
|
---|
917 |
|
---|
918 | /* Move to cancelled queue. */
|
---|
919 | DLIST_REMOVE(blocking_lock_queue, blr);
|
---|
920 | DLIST_ADD(blocking_lock_cancelled_queue, blr);
|
---|
921 |
|
---|
922 | /* Create the message. */
|
---|
923 | memcpy(msg, &blr, sizeof(blr));
|
---|
924 | memcpy(&msg[sizeof(blr)], &err, sizeof(NTSTATUS));
|
---|
925 |
|
---|
926 | messaging_send_buf(smbd_messaging_context(), procid_self(),
|
---|
927 | MSG_SMB_BLOCKING_LOCK_CANCEL,
|
---|
928 | (uint8 *)&msg, sizeof(msg));
|
---|
929 |
|
---|
930 | return True;
|
---|
931 | }
|
---|