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