Removed version number from file header.
[ira/wip.git] / source3 / smbd / blocking.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Blocking Locking functions
4    Copyright (C) Jeremy Allison 1998
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 extern char *OutBuffer;
24
25 /****************************************************************************
26  This is the structure to queue to implement blocking locks.
27  notify. It consists of the requesting SMB and the expiry time.
28 *****************************************************************************/
29
30 typedef struct {
31   ubi_slNode msg_next;
32   int com_type;
33   files_struct *fsp;
34   time_t expire_time;
35   int lock_num;
36   char *inbuf;
37   int length;
38 } blocking_lock_record;
39
40 static ubi_slList blocking_lock_queue = { NULL, (ubi_slNodePtr)&blocking_lock_queue, 0};
41
42 /****************************************************************************
43  Destructor for the above structure.
44 ****************************************************************************/
45
46 static void free_blocking_lock_record(blocking_lock_record *blr)
47 {
48   SAFE_FREE(blr->inbuf);
49   SAFE_FREE(blr);
50 }
51
52 /****************************************************************************
53  Get the files_struct given a particular queued SMB.
54 *****************************************************************************/
55
56 static files_struct *get_fsp_from_pkt(char *inbuf)
57 {
58   switch(CVAL(inbuf,smb_com)) {
59   case SMBlock:
60   case SMBlockread:
61     return file_fsp(inbuf,smb_vwv0);
62   case SMBlockingX:
63     return file_fsp(inbuf,smb_vwv2);
64   default:
65     DEBUG(0,("get_fsp_from_pkt: PANIC - unknown type on blocking lock queue - exiting.!\n"));
66     exit_server("PANIC - unknown type on blocking lock queue");
67   }
68   return NULL; /* Keep compiler happy. */
69 }
70
71 /****************************************************************************
72  Determine if this is a secondary element of a chained SMB.
73   **************************************************************************/
74
75 static BOOL in_chained_smb(void)
76 {
77   return (chain_size != 0);
78 }
79
80 /****************************************************************************
81  Function to push a blocking lock request onto the lock queue.
82 ****************************************************************************/
83
84 BOOL push_blocking_lock_request( char *inbuf, int length, int lock_timeout, int lock_num)
85 {
86   blocking_lock_record *blr;
87
88   if(in_chained_smb() ) {
89     DEBUG(0,("push_blocking_lock_request: cannot queue a chained request (currently).\n"));
90     return False;
91   }
92
93   /*
94    * Now queue an entry on the blocking lock queue. We setup
95    * the expiration time here.
96    */
97
98   if((blr = (blocking_lock_record *)malloc(sizeof(blocking_lock_record))) == NULL) {
99     DEBUG(0,("push_blocking_lock_request: Malloc fail !\n" ));
100     return False;
101   }
102
103   if((blr->inbuf = (char *)malloc(length)) == NULL) {
104     DEBUG(0,("push_blocking_lock_request: Malloc fail (2)!\n" ));
105     SAFE_FREE(blr);
106     return False;
107   }
108
109   blr->com_type = CVAL(inbuf,smb_com);
110   blr->fsp = get_fsp_from_pkt(inbuf);
111   blr->expire_time = (lock_timeout == -1) ? (time_t)-1 : time(NULL) + (time_t)lock_timeout;
112   blr->lock_num = lock_num;
113   memcpy(blr->inbuf, inbuf, length);
114   blr->length = length;
115
116   ubi_slAddTail(&blocking_lock_queue, blr);
117
118
119   DEBUG(3,("push_blocking_lock_request: lock request length=%d blocked with expiry time %d (+%d) \
120 for fnum = %d, name = %s\n", length, (int)blr->expire_time, lock_timeout,
121         blr->fsp->fnum, blr->fsp->fsp_name ));
122
123   return True;
124 }
125
126 /****************************************************************************
127  Return a smd with a given size.
128 *****************************************************************************/
129
130 static void send_blocking_reply(char *outbuf, int outsize)
131 {
132         if(outsize > 4)
133                 smb_setlen(outbuf,outsize - 4);
134
135         if (!send_smb(smbd_server_fd(),outbuf))
136                 exit_server("send_blocking_reply: send_smb failed.");
137 }
138
139 /****************************************************************************
140  Return a lockingX success SMB.
141 *****************************************************************************/
142
143 static void reply_lockingX_success(blocking_lock_record *blr)
144 {
145   char *outbuf = OutBuffer;
146   int bufsize = BUFFER_SIZE;
147   char *inbuf = blr->inbuf;
148   int outsize = 0;
149
150   construct_reply_common(inbuf, outbuf);
151   set_message(outbuf,2,0,True);
152
153   /*
154    * As this message is a lockingX call we must handle
155    * any following chained message correctly.
156    * This is normally handled in construct_reply(),
157    * but as that calls switch_message, we can't use
158    * that here and must set up the chain info manually.
159    */
160
161   outsize = chain_reply(inbuf,outbuf,blr->length,bufsize);
162
163   outsize += chain_size;
164
165   send_blocking_reply(outbuf,outsize);
166 }
167
168 /****************************************************************************
169  Return a generic lock fail error blocking call.
170 *****************************************************************************/
171
172 static void generic_blocking_lock_error(blocking_lock_record *blr, NTSTATUS status)
173 {
174         char *outbuf = OutBuffer;
175         char *inbuf = blr->inbuf;
176         construct_reply_common(inbuf, outbuf);
177
178         ERROR_NT(status);
179         if (!send_smb(smbd_server_fd(),outbuf))
180                 exit_server("generic_blocking_lock_error: send_smb failed.");
181 }
182
183 /****************************************************************************
184  Return a lock fail error for a lockingX call. Undo all the locks we have 
185  obtained first.
186 *****************************************************************************/
187
188 static void reply_lockingX_error(blocking_lock_record *blr, NTSTATUS status)
189 {
190         char *inbuf = blr->inbuf;
191         files_struct *fsp = blr->fsp;
192         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
193         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
194         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT) 0;
195         uint16 lock_pid;
196         unsigned char locktype = CVAL(inbuf,smb_vwv3);
197         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
198         char *data;
199         int i;
200
201         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
202         
203         /* 
204          * Data now points at the beginning of the list
205          * of smb_lkrng structs.
206          */
207
208         /*
209          * Ensure we don't do a remove on the lock that just failed,
210          * as under POSIX rules, if we have a lock already there, we
211          * will delete it (and we shouldn't) .....
212          */
213         
214         for(i = blr->lock_num - 1; i >= 0; i--) {
215                 BOOL err;
216                 
217                 lock_pid = get_lock_pid( data, i, large_file_format);
218                 count = get_lock_count( data, i, large_file_format);
219                 offset = get_lock_offset( data, i, large_file_format, &err);
220                 
221                 /*
222                  * We know err cannot be set as if it was the lock
223                  * request would never have been queued. JRA.
224                  */
225                 
226                 do_unlock(fsp,conn,lock_pid,count,offset);
227         }
228         
229         generic_blocking_lock_error(blr, status);
230 }
231
232 /****************************************************************************
233  Return a lock fail error.
234 *****************************************************************************/
235
236 static void blocking_lock_reply_error(blocking_lock_record *blr, NTSTATUS status)
237 {
238         switch(blr->com_type) {
239         case SMBlock:
240         case SMBlockread:
241                 generic_blocking_lock_error(blr, status);
242                 break;
243         case SMBlockingX:
244                 reply_lockingX_error(blr, status);
245                 break;
246         default:
247                 DEBUG(0,("blocking_lock_reply_error: PANIC - unknown type on blocking lock queue - exiting.!\n"));
248                 exit_server("PANIC - unknown type on blocking lock queue");
249         }
250 }
251
252 /****************************************************************************
253  Attempt to finish off getting all pending blocking locks for a lockread call.
254  Returns True if we want to be removed from the list.
255 *****************************************************************************/
256
257 static BOOL process_lockread(blocking_lock_record *blr)
258 {
259         char *outbuf = OutBuffer;
260         char *inbuf = blr->inbuf;
261         ssize_t nread = -1;
262         char *data, *p;
263         int outsize = 0;
264         SMB_OFF_T startpos;
265         size_t numtoread;
266         NTSTATUS status;
267         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
268         files_struct *fsp = blr->fsp;
269
270         numtoread = SVAL(inbuf,smb_vwv1);
271         startpos = IVAL(inbuf,smb_vwv2);
272         
273         numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
274         data = smb_buf(outbuf) + 3;
275  
276         status = do_lock( fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtoread, 
277                           (SMB_BIG_UINT)startpos, READ_LOCK);
278         if (NT_STATUS_V(status)) {
279                 if ((errno != EACCES) && (errno != EAGAIN)) {
280                         /*
281                          * We have other than a "can't get lock" POSIX
282                          * error. Send an error.
283                          * Return True so we get dequeued.
284                          */
285                         generic_blocking_lock_error(blr, status);
286                         return True;
287                 }
288
289                 /*
290                  * Still waiting for lock....
291                  */
292                 
293                 DEBUG(10,("process_lockread: failed to get lock for file = %s. Still waiting....\n",
294                           fsp->fsp_name));
295                 return False;
296         }
297
298         nread = read_file(fsp,data,startpos,numtoread);
299
300         if (nread < 0) {
301                 generic_blocking_lock_error(blr,NT_STATUS_ACCESS_DENIED);
302                 return True;
303         }
304         
305         construct_reply_common(inbuf, outbuf);
306         outsize = set_message(outbuf,5,0,True);
307         
308         outsize += nread;
309         SSVAL(outbuf,smb_vwv0,nread);
310         SSVAL(outbuf,smb_vwv5,nread+3);
311         p = smb_buf(outbuf);
312         *p++ = 1;
313         SSVAL(p,0,nread); p += 2;
314         set_message_end(outbuf, p+nread);
315         
316         DEBUG(3, ( "process_lockread file = %s, fnum=%d num=%d nread=%d\n",
317                    fsp->fsp_name, fsp->fnum, (int)numtoread, (int)nread ) );
318         
319         send_blocking_reply(outbuf,outsize);
320         return True;
321 }
322
323 /****************************************************************************
324  Attempt to finish off getting all pending blocking locks for a lock call.
325  Returns True if we want to be removed from the list.
326 *****************************************************************************/
327
328 static BOOL process_lock(blocking_lock_record *blr)
329 {
330         char *outbuf = OutBuffer;
331         char *inbuf = blr->inbuf;
332         int outsize;
333         SMB_OFF_T count = 0, offset = 0;
334         NTSTATUS status;
335         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
336         files_struct *fsp = blr->fsp;
337
338         count = IVAL(inbuf,smb_vwv1);
339         offset = IVAL(inbuf,smb_vwv3);
340
341         errno = 0;
342         status = do_lock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)count, 
343                          (SMB_BIG_UINT)offset, WRITE_LOCK);
344         if (NT_STATUS_IS_ERR(status)) {
345                 if((errno != EACCES) && (errno != EAGAIN)) {
346                         /*
347                          * We have other than a "can't get lock" POSIX
348                          * error. Send an error.
349                          * Return True so we get dequeued.
350                          */
351                         
352                         blocking_lock_reply_error(blr, status);
353                         return True;
354                 }
355                 /*
356                  * Still can't get the lock - keep waiting.
357                  */
358                 DEBUG(10,("process_lock: failed to get lock for file = %s. Still waiting....\n",
359                           fsp->fsp_name));
360                 return False;
361         }
362
363         /*
364          * Success - we got the lock.
365          */
366         
367         DEBUG(3,("process_lock : file=%s fnum=%d offset=%.0f count=%.0f\n",
368                  fsp->fsp_name, fsp->fnum, (double)offset, (double)count));
369         
370         construct_reply_common(inbuf, outbuf);
371         outsize = set_message(outbuf,0,0,True);
372         send_blocking_reply(outbuf,outsize);
373         return True;
374 }
375
376 /****************************************************************************
377  Attempt to finish off getting all pending blocking locks for a lockingX call.
378  Returns True if we want to be removed from the list.
379 *****************************************************************************/
380
381 static BOOL process_lockingX(blocking_lock_record *blr)
382 {
383         char *inbuf = blr->inbuf;
384         unsigned char locktype = CVAL(inbuf,smb_vwv3);
385         files_struct *fsp = blr->fsp;
386         connection_struct *conn = conn_find(SVAL(inbuf,smb_tid));
387         uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
388         uint16 num_locks = SVAL(inbuf,smb_vwv7);
389         SMB_BIG_UINT count = (SMB_BIG_UINT)0, offset = (SMB_BIG_UINT)0;
390         uint16 lock_pid;
391         BOOL large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES);
392         char *data;
393         NTSTATUS status = NT_STATUS_OK;
394
395         data = smb_buf(inbuf) + ((large_file_format ? 20 : 10)*num_ulocks);
396
397         /* 
398          * Data now points at the beginning of the list
399          * of smb_lkrng structs.
400          */
401         
402         for(; blr->lock_num < num_locks; blr->lock_num++) {
403                 BOOL err;
404
405                 lock_pid = get_lock_pid( data, blr->lock_num, large_file_format);
406                 count = get_lock_count( data, blr->lock_num, large_file_format);
407                 offset = get_lock_offset( data, blr->lock_num, large_file_format, &err);
408                 
409                 /*
410                  * We know err cannot be set as if it was the lock
411                  * request would never have been queued. JRA.
412                  */
413                 errno = 0;
414                 status = do_lock(fsp,conn,lock_pid,count,offset, 
415                                  ((locktype & 1) ? READ_LOCK : WRITE_LOCK));
416                 if (NT_STATUS_IS_ERR(status)) break;
417         }
418
419         if(blr->lock_num == num_locks) {
420                 /*
421                  * Success - we got all the locks.
422                  */
423                 
424                 DEBUG(3,("process_lockingX file = %s, fnum=%d type=%d num_locks=%d\n",
425                          fsp->fsp_name, fsp->fnum, (unsigned int)locktype, num_locks) );
426
427                 reply_lockingX_success(blr);
428                 return True;
429         } else if ((errno != EACCES) && (errno != EAGAIN)) {
430                 /*
431                  * We have other than a "can't get lock" POSIX
432                  * error. Free any locks we had and return an error.
433                  * Return True so we get dequeued.
434                  */
435                 
436                 blocking_lock_reply_error(blr, status);
437                 return True;
438         }
439
440         /*
441          * Still can't get all the locks - keep waiting.
442          */
443         
444         DEBUG(10,("process_lockingX: only got %d locks of %d needed for file %s, fnum = %d. \
445 Waiting....\n", 
446                   blr->lock_num, num_locks, fsp->fsp_name, fsp->fnum));
447         
448         return False;
449 }
450
451 /****************************************************************************
452  Process a blocking lock SMB.
453  Returns True if we want to be removed from the list.
454 *****************************************************************************/
455
456 static BOOL blocking_lock_record_process(blocking_lock_record *blr)
457 {
458   switch(blr->com_type) {
459   case SMBlock:
460     return process_lock(blr);
461   case SMBlockread:
462     return process_lockread(blr);
463   case SMBlockingX:
464     return process_lockingX(blr);
465   default:
466     DEBUG(0,("blocking_lock_record_process: PANIC - unknown type on blocking lock queue - exiting.!\n"));
467     exit_server("PANIC - unknown type on blocking lock queue");
468   }
469   return False; /* Keep compiler happy. */
470 }
471
472 /****************************************************************************
473  Delete entries by fnum from the blocking lock pending queue.
474 *****************************************************************************/
475
476 void remove_pending_lock_requests_by_fid(files_struct *fsp)
477 {
478   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
479   blocking_lock_record *prev = NULL;
480
481   while(blr != NULL) {
482     if(blr->fsp->fnum == fsp->fnum) {
483
484       DEBUG(10,("remove_pending_lock_requests_by_fid - removing request type %d for \
485 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
486
487       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
488       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
489       continue;
490     }
491
492     prev = blr;
493     blr = (blocking_lock_record *)ubi_slNext(blr);
494   }
495 }
496
497 /****************************************************************************
498  Delete entries by mid from the blocking lock pending queue. Always send reply.
499 *****************************************************************************/
500
501 void remove_pending_lock_requests_by_mid(int mid)
502 {
503   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
504   blocking_lock_record *prev = NULL;
505
506   while(blr != NULL) {
507     if(SVAL(blr->inbuf,smb_mid) == mid) {
508       files_struct *fsp = blr->fsp;
509
510       DEBUG(10,("remove_pending_lock_requests_by_mid - removing request type %d for \
511 file %s fnum = %d\n", blr->com_type, fsp->fsp_name, fsp->fnum ));
512
513       blocking_lock_reply_error(blr,NT_STATUS_CANCELLED);
514       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
515       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
516       continue;
517     }
518
519     prev = blr;
520     blr = (blocking_lock_record *)ubi_slNext(blr);
521   }
522 }
523
524 /****************************************************************************
525  Return True if the blocking lock queue has entries.
526 *****************************************************************************/
527
528 BOOL blocking_locks_pending(void)
529 {
530   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
531   return (blr == NULL ? False : True);
532 }
533
534 /****************************************************************************
535  Process the blocking lock queue. Note that this is only called as root.
536 *****************************************************************************/
537
538 void process_blocking_lock_queue(time_t t)
539 {
540   blocking_lock_record *blr = (blocking_lock_record *)ubi_slFirst( &blocking_lock_queue );
541   blocking_lock_record *prev = NULL;
542
543   if(blr == NULL)
544     return;
545
546   /*
547    * Go through the queue and see if we can get any of the locks.
548    */
549
550   while(blr != NULL) {
551     connection_struct *conn = NULL;
552     uint16 vuid;
553     files_struct *fsp = NULL;
554
555     /*
556      * Ensure we don't have any old chain_fsp values
557      * sitting around....
558      */
559     chain_size = 0;
560     file_chain_reset();
561     fsp = blr->fsp;
562
563     conn = conn_find(SVAL(blr->inbuf,smb_tid));
564     vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID :
565                   SVAL(blr->inbuf,smb_uid);
566
567     DEBUG(5,("process_blocking_lock_queue: examining pending lock fnum = %d for file %s\n",
568           fsp->fnum, fsp->fsp_name ));
569
570     if((blr->expire_time != -1) && (blr->expire_time > t)) {
571       /*
572        * Lock expired - throw away all previously
573        * obtained locks and return lock error.
574        */
575       DEBUG(5,("process_blocking_lock_queue: pending lock fnum = %d for file %s timed out.\n",
576           fsp->fnum, fsp->fsp_name ));
577
578       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
579       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
580       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
581       continue;
582     }
583
584     if(!change_to_user(conn,vuid)) {
585       DEBUG(0,("process_blocking_lock_queue: Unable to become user vuid=%d.\n",
586             vuid ));
587       /*
588        * Remove the entry and return an error to the client.
589        */
590       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
591       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
592       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
593       continue;
594     }
595
596     if(!set_current_service(conn,True)) {
597       DEBUG(0,("process_blocking_lock_queue: Unable to become service Error was %s.\n", strerror(errno) ));
598       /*
599        * Remove the entry and return an error to the client.
600        */
601       blocking_lock_reply_error(blr,NT_STATUS_ACCESS_DENIED);
602       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
603       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
604       change_to_root_user();
605       continue;
606     }
607
608     /*
609      * Go through the remaining locks and try and obtain them.
610      * The call returns True if all locks were obtained successfully
611      * and False if we still need to wait.
612      */
613
614     if(blocking_lock_record_process(blr)) {
615       free_blocking_lock_record((blocking_lock_record *)ubi_slRemNext( &blocking_lock_queue, prev));
616       blr = (blocking_lock_record *)(prev ? ubi_slNext(prev) : ubi_slFirst(&blocking_lock_queue));
617       change_to_root_user();
618       continue;
619     }
620
621     change_to_root_user();
622
623     /*
624      * Move to the next in the list.
625      */
626     prev = blr;
627     blr = (blocking_lock_record *)ubi_slNext(blr);
628   }
629 }