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