Put back lots of missing calls to dos_to_unix(). Thanks to
[ira/wip.git] / source / smbd / open.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    file opening and share modes
5    Copyright (C) Andrew Tridgell 1992-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
24 extern int DEBUGLEVEL;
25
26 extern pstring sesssetup_user;
27 extern uint16 global_oplock_port;
28 extern BOOL global_client_failed_oplock_break;
29
30 /****************************************************************************
31 fd support routines - attempt to do a dos_open
32 ****************************************************************************/
33 static int fd_attempt_open(struct connection_struct *conn, char *fname, 
34                            int flags, mode_t mode)
35 {
36   int fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
37
38   /* Fix for files ending in '.' */
39   if((fd == -1) && (errno == ENOENT) &&
40      (strchr(fname,'.')==NULL))
41     {
42       pstrcat(fname,".");
43       fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
44     }
45
46 #if (defined(ENAMETOOLONG) && defined(HAVE_PATHCONF))
47   if ((fd == -1) && (errno == ENAMETOOLONG))
48     {
49       int max_len;
50       char *p = strrchr(fname, '/');
51
52       if (p == fname)   /* name is "/xxx" */
53         {
54           max_len = pathconf("/", _PC_NAME_MAX);
55           p++;
56         }
57       else if ((p == NULL) || (p == fname))
58         {
59           p = fname;
60           max_len = pathconf(".", _PC_NAME_MAX);
61         }
62       else
63         {
64           *p = '\0';
65           max_len = pathconf(fname, _PC_NAME_MAX);
66           *p = '/';
67           p++;
68         }
69       if (strlen(p) > max_len)
70         {
71           char tmp = p[max_len];
72
73           p[max_len] = '\0';
74           if ((fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode)) == -1)
75             p[max_len] = tmp;
76         }
77     }
78 #endif
79   return fd;
80 }
81
82 /****************************************************************************
83 Cache a uid_t currently with this file open. This is an optimization only
84 used when multiple sessionsetup's have been done to one smbd.
85 ****************************************************************************/
86
87 void fd_add_to_uid_cache(file_fd_struct *fd_ptr, uid_t u)
88 {
89   if(fd_ptr->uid_cache_count >= sizeof(fd_ptr->uid_users_cache)/sizeof(uid_t))
90     return;
91   fd_ptr->uid_users_cache[fd_ptr->uid_cache_count++] = u;
92 }
93
94 /****************************************************************************
95 Remove a uid_t that currently has this file open. This is an optimization only
96 used when multiple sessionsetup's have been done to one smbd.
97 ****************************************************************************/
98
99 static void fd_remove_from_uid_cache(file_fd_struct *fd_ptr, uid_t u)
100 {
101   int i;
102   for(i = 0; i < fd_ptr->uid_cache_count; i++)
103     if(fd_ptr->uid_users_cache[i] == u) {
104       if(i < (fd_ptr->uid_cache_count-1))
105         memmove((char *)&fd_ptr->uid_users_cache[i], (char *)&fd_ptr->uid_users_cache[i+1],
106                sizeof(uid_t)*(fd_ptr->uid_cache_count-1-i) );
107       fd_ptr->uid_cache_count--;
108     }
109   return;
110 }
111
112 /****************************************************************************
113 Check if a uid_t that currently has this file open is present. This is an
114 optimization only used when multiple sessionsetup's have been done to one smbd.
115 ****************************************************************************/
116
117 static BOOL fd_is_in_uid_cache(file_fd_struct *fd_ptr, uid_t u)
118 {
119   int i;
120   for(i = 0; i < fd_ptr->uid_cache_count; i++)
121     if(fd_ptr->uid_users_cache[i] == u)
122       return True;
123   return False;
124 }
125
126 /****************************************************************************
127 fd support routines - attempt to re-open an already open fd as O_RDWR.
128 Save the already open fd (we cannot close due to POSIX file locking braindamage.
129 ****************************************************************************/
130
131 static void fd_attempt_reopen(char *fname, mode_t mode, files_struct *fsp)
132 {
133   int fd = fsp->conn->vfs_ops.open(dos_to_unix(fname, False), O_RDWR, mode);
134   file_fd_struct *fd_ptr = fsp->fd_ptr;
135
136   if(fd == -1)
137     return;
138
139   if(fd_ptr->real_open_flags == O_RDONLY)
140     fd_ptr->fd_readonly = fd_ptr->fd;
141   if(fd_ptr->real_open_flags == O_WRONLY)
142     fd_ptr->fd_writeonly = fd_ptr->fd;
143
144   fd_ptr->fd = fd;
145   fd_ptr->real_open_flags = O_RDWR;
146 }
147
148 /****************************************************************************
149 fd support routines - attempt to close the file referenced by this fd.
150 Decrements the ref_count and returns it.
151 ****************************************************************************/
152 uint16 fd_attempt_close(files_struct *fsp, int *err_ret)
153 {
154   extern struct current_user current_user;
155   file_fd_struct *fd_ptr = fsp->fd_ptr;
156   uint16 ret_ref = fd_ptr->ref_count;
157
158   *err_ret = 0;
159
160   DEBUG(3,("fd_attempt_close fd = %d, dev = %x, inode = %.0f, open_flags = %d, ref_count = %d.\n",
161           fd_ptr->fd, (unsigned int)fd_ptr->dev, (double)fd_ptr->inode,
162           fd_ptr->real_open_flags,
163           fd_ptr->ref_count));
164
165   SMB_ASSERT(fd_ptr->ref_count != 0);
166
167   fd_ptr->ref_count--;
168   ret_ref = fd_ptr->ref_count;
169
170   if(fd_ptr->ref_count == 0) {
171
172     if(fd_ptr->fd != -1) {
173       if(fsp->conn->vfs_ops.close(fd_ptr->fd) < 0)
174         *err_ret = errno;
175         }
176
177     if(fd_ptr->fd_readonly != -1) {
178       if(fsp->conn->vfs_ops.close(fd_ptr->fd_readonly) < 0) {
179         if(*err_ret == 0)
180           *err_ret = errno;
181       }
182         }
183
184     if(fd_ptr->fd_writeonly != -1) {
185       if(fsp->conn->vfs_ops.close(fd_ptr->fd_writeonly) < 0) {
186         if(*err_ret == 0)
187           *err_ret = errno;
188       }
189         }
190
191     /*
192      * Delete this fd_ptr.
193      */
194     fd_ptr_free(fd_ptr);
195   } else {
196     fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
197   }
198
199   return ret_ref;
200 }
201
202 /****************************************************************************
203 fd support routines - check that current user has permissions
204 to open this file. Used when uid not found in optimization cache.
205 This is really ugly code, as due to POSIX locking braindamage we must
206 fork and then attempt to open the file, and return success or failure
207 via an exit code.
208 ****************************************************************************/
209 static BOOL check_access_allowed_for_current_user(struct connection_struct
210                                                   *conn, char *fname, 
211                                                   int accmode )
212 {
213   pid_t child_pid;
214
215   /*
216    * We need to temporarily stop CatchChild from eating 
217    * SIGCLD signals as it also eats the exit status code. JRA.
218    */
219
220   CatchChildLeaveStatus();
221
222   if((child_pid = fork()) < 0) {
223     DEBUG(0,("check_access_allowed_for_current_user: fork failed.\n"));
224     CatchChild();
225     return False;
226   }
227
228   if(child_pid) {
229     /*
230      * Parent.
231      */
232     pid_t wpid;
233     int status_code;
234
235     while ((wpid = sys_waitpid(child_pid, &status_code, 0)) < 0) {
236       if(errno == EINTR) {
237         errno = 0;
238         continue;
239       }
240       DEBUG(0,("check_access_allowed_for_current_user: The process \
241 is no longer waiting ! Error = %s\n", strerror(errno) ));
242       CatchChild();
243       return(False);
244     }
245
246         /*
247          * Go back to ignoring children.
248          */
249         CatchChild();
250
251     if (child_pid != wpid) {
252       DEBUG(0,("check_access_allowed_for_current_user: We were waiting for the wrong process ID\n"));
253       return(False);
254     }
255 #if defined(WIFEXITED) && defined(WEXITSTATUS)
256     if (WIFEXITED(status_code) == 0) {
257       DEBUG(0,("check_access_allowed_for_current_user: The process exited while we were waiting\n"));
258       return(False);
259     }
260     if (WEXITSTATUS(status_code) != 0) {
261       DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access denied.\n", status_code));
262       return(False);
263     }
264 #else /* defined(WIFEXITED) && defined(WEXITSTATUS) */
265     if(status_code != 0) {
266       DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access denied.\n", status_code));
267       return(False);
268     }
269 #endif /* defined(WIFEXITED) && defined(WEXITSTATUS) */
270
271     /*
272      * Success - the child could open the file.
273      */
274     DEBUG(9,("check_access_allowed_for_current_user: The status of the process exiting was %d. Returning access allowed.\n", status_code));
275     return True;
276   } else {
277     /*
278      * Child.
279      */
280     int fd;
281     DEBUG(9,("check_access_allowed_for_current_user: Child - attempting to open %s with mode %d.\n", fname, accmode ));
282     if((fd = fd_attempt_open(conn, fname, accmode, 0)) < 0) {
283       /* Access denied. */
284       _exit(EACCES);
285     }
286     conn->vfs_ops.close(fd);
287     DEBUG(9,("check_access_allowed_for_current_user: Child - returning ok.\n"));
288     _exit(0);
289   }
290
291   return False;
292 }
293
294 /****************************************************************************
295 check a filename for the pipe string
296 ****************************************************************************/
297
298 static void check_for_pipe(char *fname)
299 {
300         /* special case of pipe opens */
301         char s[10];
302         StrnCpy(s,fname,sizeof(s)-1);
303         strlower(s);
304         if (strstr(s,"pipe/")) {
305                 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
306                 unix_ERR_class = ERRSRV;
307                 unix_ERR_code = ERRaccess;
308         }
309 }
310
311 /****************************************************************************
312 open a file
313 ****************************************************************************/
314
315 static void open_file(files_struct *fsp,connection_struct *conn,
316                       char *fname1,int flags,mode_t mode, SMB_STRUCT_STAT *sbuf)
317 {
318   extern struct current_user current_user;
319   pstring fname;
320   SMB_STRUCT_STAT statbuf;
321   file_fd_struct *fd_ptr;
322   int accmode = (flags & O_ACCMODE);
323
324   fsp->open = False;
325   fsp->fd_ptr = 0;
326   fsp->oplock_type = NO_OPLOCK;
327   errno = EPERM;
328
329   pstrcpy(fname,fname1);
330
331   /* check permissions */
332
333   /*
334    * This code was changed after seeing a client open request 
335    * containing the open mode of (DENY_WRITE/read-only) with
336    * the 'create if not exist' bit set. The previous code
337    * would fail to open the file read only on a read-only share
338    * as it was checking the flags parameter  directly against O_RDONLY,
339    * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
340    * JRA.
341    */
342
343   if (!CAN_WRITE(conn) && !conn->printer) {
344     /* It's a read-only share - fail if we wanted to write. */
345     if(accmode != O_RDONLY) {
346       DEBUG(3,("Permission denied opening %s\n",fname));
347       check_for_pipe(fname);
348       return;
349     } else if(flags & O_CREAT) {
350       /* We don't want to write - but we must make sure that O_CREAT
351          doesn't create the file if we have write access into the
352          directory.
353        */
354       flags &= ~O_CREAT;
355     }
356   }
357
358   /* this handles a bug in Win95 - it doesn't say to create the file when it 
359      should */
360   if (conn->printer) {
361           flags |= (O_CREAT|O_EXCL);
362   }
363
364 /*
365   if (flags == O_WRONLY)
366     DEBUG(3,("Bug in client? Set O_WRONLY without O_CREAT\n"));
367 */
368
369   /*
370    * Ensure we have a valid struct stat so we can search the
371    * open fd table.
372    */
373   if(sbuf == 0) {
374     if(conn->vfs_ops.stat(dos_to_unix(fname,False), &statbuf) < 0) {
375       if(errno != ENOENT) {
376         DEBUG(3,("Error doing stat on file %s (%s)\n",
377                  fname,strerror(errno)));
378
379         check_for_pipe(fname);
380         return;
381       }
382       sbuf = 0;
383     } else {
384       sbuf = &statbuf;
385     }
386   }
387
388   /*
389    * Check to see if we have this file already
390    * open. If we do, just use the already open fd and increment the
391    * reference count (fd_get_already_open increments the ref_count).
392    */
393   if((fd_ptr = fd_get_already_open(sbuf))!= 0) {
394     /*
395      * File was already open.
396      */
397
398     /* 
399      * Check it wasn't open for exclusive use.
400      */
401     if((flags & O_CREAT) && (flags & O_EXCL)) {
402       fd_ptr->ref_count--;
403       errno = EEXIST;
404       return;
405     }
406
407     /*
408      * Ensure that the user attempting to open
409      * this file has permissions to do so, if
410      * the user who originally opened the file wasn't
411      * the same as the current user.
412      */
413
414     if(!fd_is_in_uid_cache(fd_ptr, (uid_t)current_user.uid)) {
415       if(!check_access_allowed_for_current_user(conn, fname, accmode )) {
416         /* Error - permission denied. */
417         DEBUG(3,("Permission denied opening file %s (flags=%d, accmode = %d)\n",
418               fname, flags, accmode));
419         /* Ensure the ref_count is decremented. */
420         fd_ptr->ref_count--;
421         fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
422         errno = EACCES;
423         return;
424       }
425     }
426
427     fd_add_to_uid_cache(fd_ptr, (uid_t)current_user.uid);
428
429     /* 
430      * If not opened O_RDWR try
431      * and do that here - a chmod may have been done
432      * between the last open and now. 
433      */
434     if(fd_ptr->real_open_flags != O_RDWR)
435       fd_attempt_reopen(fname, mode, fsp);
436
437     /*
438      * Ensure that if we wanted write access
439      * it has been opened for write, and if we wanted read it
440      * was open for read. 
441      */
442     if(((accmode == O_WRONLY) && (fd_ptr->real_open_flags == O_RDONLY)) ||
443        ((accmode == O_RDONLY) && (fd_ptr->real_open_flags == O_WRONLY)) ||
444        ((accmode == O_RDWR) && (fd_ptr->real_open_flags != O_RDWR))) {
445       DEBUG(3,("Error opening (already open for flags=%d) file %s (%s) (flags=%d)\n",
446                fd_ptr->real_open_flags, fname,strerror(EACCES),flags));
447       check_for_pipe(fname);
448       fd_remove_from_uid_cache(fd_ptr, (uid_t)current_user.uid);
449       fd_ptr->ref_count--;
450       return;
451     }
452
453   } else {
454     int open_flags;
455     /* We need to allocate a new file_fd_struct (this increments the
456        ref_count). */
457     if((fd_ptr = fd_get_new()) == 0)
458       return;
459     /*
460      * Whatever the requested flags, attempt read/write access,
461      * as we don't know what flags future file opens may require.
462      * If this fails, try again with the required flags. 
463      * Even if we open read/write when only read access was 
464      * requested the setting of the can_write flag in
465      * the file_struct will protect us from errant
466      * write requests. We never need to worry about O_APPEND
467      * as this is not set anywhere in Samba.
468      */
469     fd_ptr->real_open_flags = O_RDWR;
470     /* Set the flags as needed without the read/write modes. */
471     open_flags = flags & ~(O_RDWR|O_WRONLY|O_RDONLY);
472     fd_ptr->fd = fd_attempt_open(conn, fname, open_flags|O_RDWR, mode);
473     /*
474      * On some systems opening a file for R/W access on a read only
475      * filesystems sets errno to EROFS.
476      */
477 #ifdef EROFS
478     if((fd_ptr->fd == -1) && ((errno == EACCES) || (errno == EROFS))) {
479 #else /* No EROFS */
480     if((fd_ptr->fd == -1) && (errno == EACCES)) {
481 #endif /* EROFS */
482       if(accmode != O_RDWR) {
483         fd_ptr->fd = fd_attempt_open(conn, fname, open_flags|accmode, mode);
484         fd_ptr->real_open_flags = accmode;
485       }
486     }
487   }
488
489   if ((fd_ptr->fd >=0) && 
490       conn->printer && lp_minprintspace(SNUM(conn))) {
491     pstring dname;
492     SMB_BIG_UINT dum1,dum2,dum3;
493     char *p;
494     pstrcpy(dname,fname);
495     p = strrchr(dname,'/');
496     if (p) *p = 0;
497     if (conn->vfs_ops.disk_free(dos_to_unix(dname,False),False,&dum1,&dum2,&dum3) < 
498         (SMB_BIG_UINT)lp_minprintspace(SNUM(conn))) {
499       int err;
500       if(fd_attempt_close(fsp, &err) == 0)
501         conn->vfs_ops.unlink(dos_to_unix(fname, False));
502       fsp->fd_ptr = 0;
503       errno = ENOSPC;
504       return;
505     }
506   }
507     
508   if (fd_ptr->fd < 0)
509   {
510     int err;
511     DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
512       fname,strerror(errno),flags));
513     /* Ensure the ref_count is decremented. */
514     fd_attempt_close(fsp,&err);
515     check_for_pipe(fname);
516     return;
517   }
518
519   if (fd_ptr->fd >= 0)
520   {
521     if(sbuf == 0) {
522       /* Do the fstat */
523       if(conn->vfs_ops.fstat(fd_ptr->fd, &statbuf) == -1) {
524           int err;
525         /* Error - backout !! */
526         DEBUG(3,("Error doing fstat on fd %d, file %s (%s)\n",
527                  fd_ptr->fd, fname,strerror(errno)));
528         /* Ensure the ref_count is decremented. */
529         fd_attempt_close(fsp,&err);
530         return;
531       }
532       sbuf = &statbuf;
533     }
534
535     /* Set the correct entries in fd_ptr. */
536     fd_ptr->dev = sbuf->st_dev;
537     fd_ptr->inode = sbuf->st_ino;
538
539     fsp->fd_ptr = fd_ptr;
540     conn->num_files_open++;
541     fsp->mode = sbuf->st_mode;
542     GetTimeOfDay(&fsp->open_time);
543     fsp->vuid = current_user.vuid;
544     fsp->size = 0;
545     fsp->pos = -1;
546     fsp->open = True;
547     fsp->can_lock = True;
548     fsp->can_read = ((flags & O_WRONLY)==0);
549     fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
550     fsp->share_mode = 0;
551     fsp->print_file = conn->printer;
552     fsp->modified = False;
553     fsp->oplock_type = NO_OPLOCK;
554     fsp->sent_oplock_break = NO_BREAK_SENT;
555     fsp->is_directory = False;
556     fsp->stat_open = False;
557     fsp->directory_delete_on_close = False;
558     fsp->conn = conn;
559     /*
560      * Note that the file name here is the *untranslated* name
561      * ie. it is still in the DOS codepage sent from the client.
562      * All use of this filename will pass though the sys_xxxx
563      * functions which will do the dos_to_unix translation before
564      * mapping into a UNIX filename. JRA.
565      */
566     string_set(&fsp->fsp_name,fname);
567     fsp->wbmpx_ptr = NULL;      
568     fsp->wcp = NULL; /* Write cache pointer. */
569
570     /*
571      * If the printer is marked as postscript output a leading
572      * file identifier to ensure the file is treated as a raw
573      * postscript file.
574      * This has a similar effect as CtrlD=0 in WIN.INI file.
575      * tim@fsg.com 09/06/94
576      */
577     if (fsp->print_file && lp_postscript(SNUM(conn)) && fsp->can_write) {
578             DEBUG(3,("Writing postscript line\n"));
579             conn->vfs_ops.write(fsp->fd_ptr->fd,"%!\n",3);
580     }
581       
582     DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
583              *sesssetup_user ? sesssetup_user : conn->user,fsp->fsp_name,
584              BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
585              conn->num_files_open));
586
587   }
588 }
589
590 /****************************************************************************
591   C. Hoch 11/22/95
592   Helper for open_file_shared. 
593   Truncate a file after checking locking; close file if locked.
594   **************************************************************************/
595
596 static void truncate_unless_locked(files_struct *fsp, connection_struct *conn, int token, 
597                                    BOOL *share_locked)
598 {
599         if (fsp->can_write){
600                 SMB_OFF_T mask2 = ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
601                 SMB_OFF_T mask = (mask2<<2);
602                 
603                 if (is_locked(fsp,conn,~mask,0,WRITE_LOCK)){
604                         /* If share modes are in force for this connection we
605                            have the share entry locked. Unlock it before closing. */
606                         if (*share_locked && lp_share_modes(SNUM(conn)))
607                                 unlock_share_entry( conn, fsp->fd_ptr->dev, 
608                                                     fsp->fd_ptr->inode);
609                         close_file(fsp,False);   
610                         /* Share mode no longer locked. */
611                         *share_locked = False;
612                         errno = EACCES;
613                         unix_ERR_class = ERRDOS;
614                   unix_ERR_code = ERRlock;
615                 } else {
616                         sys_ftruncate(fsp->fd_ptr->fd,0); 
617                 }
618         }
619 }
620
621
622 /*******************************************************************
623 return True if the filename is one of the special executable types
624 ********************************************************************/
625 static BOOL is_executable(char *fname)
626 {
627         if ((fname = strrchr(fname,'.'))) {
628                 if (strequal(fname,".com") ||
629                     strequal(fname,".dll") ||
630                     strequal(fname,".exe") ||
631                     strequal(fname,".sym")) {
632                         return True;
633                 }
634         }
635         return False;
636 }
637
638 enum {AFAIL,AREAD,AWRITE,AALL};
639
640 /*******************************************************************
641 reproduce the share mode access table
642 this is horrendoously complex, and really can't be justified on any
643 rational grounds except that this is _exactly_ what NT does. See
644 the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
645 test routines.
646 ********************************************************************/
647 static int access_table(int new_deny,int old_deny,int old_mode,
648                         BOOL same_pid, BOOL isexe)
649 {
650           if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
651
652           if (same_pid) {
653                   if (isexe && old_mode == DOS_OPEN_RDONLY && 
654                       old_deny == DENY_DOS && new_deny == DENY_READ) {
655                           return AFAIL;
656                   }
657                   if (!isexe && old_mode == DOS_OPEN_RDONLY && 
658                       old_deny == DENY_DOS && new_deny == DENY_DOS) {
659                           return AREAD;
660                   }
661                   if (new_deny == DENY_FCB && old_deny == DENY_DOS) {
662                           if (isexe) return AFAIL;
663                           if (old_mode == DOS_OPEN_RDONLY) return AFAIL;
664                           return AALL;
665                   }
666                   if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) {
667                           if (new_deny == DENY_FCB || new_deny == DENY_READ) {
668                                   if (isexe) return AREAD;
669                                   return AFAIL;
670                           }
671                   }
672                   if (old_deny == DENY_FCB) {
673                           if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL;
674                           return AFAIL;
675                   }
676           }
677
678           if (old_deny == DENY_DOS || new_deny == DENY_DOS || 
679               old_deny == DENY_FCB || new_deny == DENY_FCB) {
680                   if (isexe) {
681                           if (old_deny == DENY_FCB || new_deny == DENY_FCB) {
682                                   return AFAIL;
683                           }
684                           if (old_deny == DENY_DOS) {
685                                   if (new_deny == DENY_READ && 
686                                       (old_mode == DOS_OPEN_RDONLY || 
687                                        old_mode == DOS_OPEN_RDWR)) {
688                                           return AFAIL;
689                                   }
690                                   if (new_deny == DENY_WRITE && 
691                                       (old_mode == DOS_OPEN_WRONLY || 
692                                        old_mode == DOS_OPEN_RDWR)) {
693                                           return AFAIL;
694                                   }
695                                   return AALL;
696                           }
697                           if (old_deny == DENY_NONE) return AALL;
698                           if (old_deny == DENY_READ) return AWRITE;
699                           if (old_deny == DENY_WRITE) return AREAD;
700                   }
701                   /* it isn't a exe, dll, sym or com file */
702                   if (old_deny == new_deny && same_pid)
703                           return(AALL);    
704
705                   if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL;
706                   if (old_mode == DOS_OPEN_RDONLY) return(AREAD);
707                   
708                   return(AFAIL);
709           }
710           
711           switch (new_deny) 
712                   {
713                   case DENY_WRITE:
714                           if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD);
715                           if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE);
716                           if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL);
717                           return(AFAIL);
718                   case DENY_READ:
719                           if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD);
720                           if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE);
721                           if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL);
722                           return(AFAIL);
723                   case DENY_NONE:
724                           if (old_deny==DENY_WRITE) return(AREAD);
725                           if (old_deny==DENY_READ) return(AWRITE);
726                           if (old_deny==DENY_NONE) return(AALL);
727                           return(AFAIL);      
728                   }
729           return(AFAIL);      
730 }
731
732
733 /****************************************************************************
734 check if we can open a file with a share mode
735 ****************************************************************************/
736
737 static int check_share_mode( share_mode_entry *share, int deny_mode, 
738                              char *fname,
739                              BOOL fcbopen, int *flags)
740 {
741   int old_open_mode = GET_OPEN_MODE(share->share_mode);
742   int old_deny_mode = GET_DENY_MODE(share->share_mode);
743
744   /*
745    * Don't allow any open once the delete on close flag has been
746    * set.
747    */
748
749   if(GET_DELETE_ON_CLOSE_FLAG(share->share_mode))
750   {
751     DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
752           fname ));
753     unix_ERR_class = ERRDOS;
754     unix_ERR_code = ERRnoaccess;
755     return False;
756   }
757
758   {
759     int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
760                                       (share->pid == getpid()),is_executable(fname));
761
762     if ((access_allowed == AFAIL) ||
763         (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
764         (access_allowed == AREAD && *flags != O_RDONLY) ||
765         (access_allowed == AWRITE && *flags != O_WRONLY))
766     {
767       DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
768                 deny_mode,old_deny_mode,old_open_mode,
769                 (int)share->pid,fname, fcbopen, *flags, access_allowed));
770
771       unix_ERR_class = ERRDOS;
772       unix_ERR_code = ERRbadshare;
773
774       return False;
775     }
776
777     if (access_allowed == AREAD)
778       *flags = O_RDONLY;
779
780     if (access_allowed == AWRITE)
781       *flags = O_WRONLY;
782
783   }
784
785   return True;
786 }
787
788 /****************************************************************************
789 open a file with a share mode
790 ****************************************************************************/
791
792 void open_file_shared(files_struct *fsp,connection_struct *conn,char *fname,int share_mode,int ofun,
793                       mode_t mode,int oplock_request, int *Access,int *action)
794 {
795   int flags=0;
796   int flags2=0;
797   int deny_mode = GET_DENY_MODE(share_mode);
798   BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode);
799   SMB_STRUCT_STAT sbuf;
800   BOOL file_existed = vfs_file_exist(conn, fname, &sbuf);
801   BOOL share_locked = False;
802   BOOL fcbopen = False;
803   int token = 0;
804   SMB_DEV_T dev = 0;
805   SMB_INO_T inode = 0;
806   int num_share_modes = 0;
807   int oplock_contention_count = 0;
808   BOOL all_current_opens_are_level_II = False;
809   fsp->open = False;
810   fsp->fd_ptr = 0;
811
812   DEBUG(10,("open_file_shared: fname = %s, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
813         fname, share_mode, ofun, (int)mode,  oplock_request ));
814
815
816   /* ignore any oplock requests if oplocks are disabled */
817   if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
818           oplock_request = 0;
819   }
820
821   /* this is for OS/2 EAs - try and say we don't support them */
822   if (strstr(fname,".+,;=[].")) 
823   {
824     unix_ERR_class = ERRDOS;
825     /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
826 #if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
827     unix_ERR_code = ERRcannotopen;
828 #else /* OS2_WPS_FIX */
829     unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
830 #endif /* OS2_WPS_FIX */
831
832     DEBUG(5,("open_file_shared: OS/2 EA's are not supported.\n"));
833     return;
834   }
835
836   if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed)  
837   {
838     DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
839           fname ));
840     errno = EEXIST;
841     return;
842   }
843       
844   if (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST)
845     flags2 |= O_CREAT;
846
847   if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)
848     flags2 |= O_TRUNC;
849
850   if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL)
851     flags2 |= O_EXCL;
852
853   /* note that we ignore the append flag as 
854      append does not mean the same thing under dos and unix */
855
856   switch (GET_OPEN_MODE(share_mode))
857   {
858     case DOS_OPEN_WRONLY: 
859       flags = O_WRONLY; 
860       break;
861     case DOS_OPEN_FCB: 
862       fcbopen = True;
863       flags = O_RDWR; 
864       break;
865     case DOS_OPEN_RDWR: 
866       flags = O_RDWR; 
867       break;
868     default:
869       flags = O_RDONLY;
870       break;
871   }
872
873 #if defined(O_SYNC)
874   if (GET_FILE_SYNC_OPENMODE(share_mode)) {
875           flags2 |= O_SYNC;
876   }
877 #endif /* O_SYNC */
878   
879   if (flags != O_RDONLY && file_existed && 
880       (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,&sbuf)))) 
881   {
882     if (!fcbopen) 
883     {
884       DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
885             fname, !CAN_WRITE(conn) ? "share" : "file" ));
886       errno = EACCES;
887       return;
888     }
889     flags = O_RDONLY;
890   }
891
892   if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) 
893   {
894     DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
895     errno = EINVAL;
896     return;
897   }
898
899   if (lp_share_modes(SNUM(conn))) 
900   {
901     int i;
902     share_mode_entry *old_shares = 0;
903
904     if (file_existed)
905     {
906       dev = sbuf.st_dev;
907       inode = sbuf.st_ino;
908       lock_share_entry(conn, dev, inode);
909       share_locked = True;
910       num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
911     }
912
913     /*
914      * Check if the share modes will give us access.
915      */
916
917     if(share_locked && (num_share_modes != 0))
918     {
919       BOOL broke_oplock;
920
921       do
922       {
923
924         broke_oplock = False;
925         all_current_opens_are_level_II = True;
926
927         for(i = 0; i < num_share_modes; i++)
928         {
929           share_mode_entry *share_entry = &old_shares[i];
930
931           /* 
932            * By observation of NetBench, oplocks are broken *before* share
933            * modes are checked. This allows a file to be closed by the client
934            * if the share mode would deny access and the client has an oplock. 
935            * Check if someone has an oplock on this file. If so we must break 
936            * it before continuing. 
937            */
938           if((oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
939              (!oplock_request && (share_entry->op_type != NO_OPLOCK)))
940           {
941
942             DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
943 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
944
945             /* Oplock break.... */
946             unlock_share_entry(conn, dev, inode);
947             if(request_oplock_break(share_entry, dev, inode) == False)
948             {
949               free((char *)old_shares);
950
951               DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
952 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
953
954               errno = EACCES;
955               unix_ERR_class = ERRDOS;
956               unix_ERR_code = ERRbadshare;
957               return;
958             }
959             lock_share_entry(conn, dev, inode);
960             broke_oplock = True;
961             all_current_opens_are_level_II = False;
962             break;
963           } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
964             all_current_opens_are_level_II = False;
965           }
966
967           /* someone else has a share lock on it, check to see 
968              if we can too */
969           if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
970           {
971             free((char *)old_shares);
972             unlock_share_entry(conn, dev, inode);
973             errno = EACCES;
974             return;
975           }
976
977         } /* end for */
978
979         if(broke_oplock)
980         {
981           free((char *)old_shares);
982           num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
983           oplock_contention_count++;
984         }
985       } while(broke_oplock);
986     }
987
988     if(old_shares != 0)
989       free((char *)old_shares);
990   }
991
992   /*
993    * Refuse to grant an oplock in case the contention limit is
994    * reached when going through the lock list multiple times.
995    */
996
997   if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn)))
998   {
999     oplock_request = 0;
1000     DEBUG(4,("open_file_shared: oplock contention = %d. Not granting oplock.\n",
1001           oplock_contention_count ));
1002   }
1003
1004   DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
1005            flags,flags2,(int)mode));
1006
1007   open_file(fsp,conn,fname,flags|(flags2&~(O_TRUNC)),mode,file_existed ? &sbuf : 0);
1008   if (!fsp->open && flags==O_RDWR && errno!=ENOENT && fcbopen) 
1009   {
1010     flags = O_RDONLY;
1011     open_file(fsp,conn,fname,flags,mode,file_existed ? &sbuf : 0 );
1012   }
1013
1014   if (fsp->open) 
1015   {
1016     int open_mode=0;
1017
1018     if((share_locked == False) && lp_share_modes(SNUM(conn)))
1019     {
1020       /* We created the file - thus we must now lock the share entry before creating it. */
1021       dev = fsp->fd_ptr->dev;
1022       inode = fsp->fd_ptr->inode;
1023       lock_share_entry(conn, dev, inode);
1024       share_locked = True;
1025     }
1026
1027     switch (flags) 
1028     {
1029       case O_RDONLY:
1030         open_mode = DOS_OPEN_RDONLY;
1031         break;
1032       case O_RDWR:
1033         open_mode = DOS_OPEN_RDWR;
1034         break;
1035       case O_WRONLY:
1036         open_mode = DOS_OPEN_WRONLY;
1037         break;
1038     }
1039
1040     fsp->share_mode = SET_DENY_MODE(deny_mode) | 
1041                       SET_OPEN_MODE(open_mode) | 
1042                       SET_ALLOW_SHARE_DELETE(allow_share_delete);
1043
1044     if (Access)
1045       (*Access) = open_mode;
1046
1047     if (action) 
1048     {
1049       if (file_existed && !(flags2 & O_TRUNC)) *action = FILE_WAS_OPENED;
1050       if (!file_existed) *action = FILE_WAS_CREATED;
1051       if (file_existed && (flags2 & O_TRUNC)) *action = FILE_WAS_OVERWRITTEN;
1052     }
1053     /* We must create the share mode entry before truncate as
1054        truncate can fail due to locking and have to close the
1055        file (which expects the share_mode_entry to be there).
1056      */
1057     if (lp_share_modes(SNUM(conn)))
1058     {
1059       uint16 port = 0;
1060
1061       /* 
1062        * Setup the oplock info in both the shared memory and
1063        * file structs.
1064        */
1065
1066       if(oplock_request && (num_share_modes == 0) && 
1067               !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) {
1068         port = global_oplock_port;
1069       } else if (oplock_request && all_current_opens_are_level_II) {
1070         port = global_oplock_port;
1071         oplock_request = LEVEL_II_OPLOCK;
1072         set_file_oplock(fsp, oplock_request);
1073       } else {
1074         port = 0;
1075         oplock_request = 0;
1076       }
1077
1078       set_share_mode(fsp, port, oplock_request);
1079     }
1080
1081     if ((flags2&O_TRUNC) && file_existed)
1082       truncate_unless_locked(fsp,conn,token,&share_locked);
1083   }
1084
1085   if (share_locked && lp_share_modes(SNUM(conn)))
1086     unlock_share_entry( conn, dev, inode);
1087 }
1088
1089 /****************************************************************************
1090  Open a file for permissions read only. Return a pseudo file entry
1091  with the 'stat_open' flag set and a fd_ptr of NULL.
1092 ****************************************************************************/
1093
1094 int open_file_stat(files_struct *fsp,connection_struct *conn,
1095                    char *fname, int smb_ofun, SMB_STRUCT_STAT *pst, int *action)
1096 {
1097         extern struct current_user current_user;
1098
1099         if(conn->vfs_ops.stat(dos_to_unix(fname, False), pst) < 0) {
1100                 DEBUG(0,("open_file_stat: unable to stat name = %s. Error was %s\n",
1101                          fname, strerror(errno) ));
1102                 return -1;
1103         }
1104
1105         if(S_ISDIR(pst->st_mode)) {
1106                 DEBUG(0,("open_file_stat: %s is a directory !\n", fname ));
1107                 return -1;
1108         }
1109
1110         *action = FILE_WAS_OPENED;
1111         
1112         DEBUG(5,("open_file_stat: opening file %s as a stat entry\n", fname));
1113
1114         /*
1115          * Setup the files_struct for it.
1116          */
1117         
1118         fsp->fd_ptr = NULL;
1119         conn->num_files_open++;
1120         fsp->mode = 0;
1121         GetTimeOfDay(&fsp->open_time);
1122         fsp->vuid = current_user.vuid;
1123         fsp->size = 0;
1124         fsp->pos = -1;
1125         fsp->open = True;
1126         fsp->can_lock = False;
1127         fsp->can_read = False;
1128         fsp->can_write = False;
1129         fsp->share_mode = 0;
1130         fsp->print_file = False;
1131         fsp->modified = False;
1132         fsp->oplock_type = NO_OPLOCK;
1133         fsp->sent_oplock_break = NO_BREAK_SENT;
1134         fsp->is_directory = False;
1135         fsp->stat_open = True;
1136         fsp->directory_delete_on_close = False;
1137         fsp->conn = conn;
1138         /*
1139          * Note that the file name here is the *untranslated* name
1140          * ie. it is still in the DOS codepage sent from the client.
1141          * All use of this filename will pass though the sys_xxxx
1142          * functions which will do the dos_to_unix translation before
1143          * mapping into a UNIX filename. JRA.
1144          */
1145         string_set(&fsp->fsp_name,fname);
1146         fsp->wbmpx_ptr = NULL;
1147     fsp->wcp = NULL; /* Write cache pointer. */
1148
1149         return 0;
1150 }
1151
1152 /****************************************************************************
1153  Open a directory from an NT SMB call.
1154 ****************************************************************************/
1155
1156 int open_directory(files_struct *fsp,connection_struct *conn,
1157                    char *fname, int smb_ofun, mode_t unixmode, int *action)
1158 {
1159         extern struct current_user current_user;
1160         SMB_STRUCT_STAT st;
1161         BOOL got_stat = False;
1162
1163         if(conn->vfs_ops.stat(dos_to_unix(fname, False), &st) == 0) {
1164                 got_stat = True;
1165         }
1166
1167         if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) {
1168                 errno = EEXIST; /* Setup so correct error is returned to client. */
1169                 return -1;
1170         }
1171
1172         if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) {
1173
1174                 if (got_stat) {
1175
1176                         if(!S_ISDIR(st.st_mode)) {
1177                                 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1178                                 errno = EACCES;
1179                                 return -1;
1180                         }
1181                         *action = FILE_WAS_OPENED;
1182
1183                 } else {
1184
1185                         /*
1186                          * Try and create the directory.
1187                          */
1188
1189                         if(!CAN_WRITE(conn)) {
1190                                 DEBUG(2,("open_directory: failing create on read-only share\n"));
1191                                 errno = EACCES;
1192                                 return -1;
1193                         }
1194
1195                         if(conn->vfs_ops.mkdir(dos_to_unix(fname, False), 
1196                                                unix_mode(conn,aDIR, fname)) < 0) {
1197                                 DEBUG(0,("open_directory: unable to create %s. Error was %s\n",
1198                                          fname, strerror(errno) ));
1199                                 return -1;
1200                         }
1201                         *action = FILE_WAS_CREATED;
1202
1203                 }
1204         } else {
1205
1206                 /*
1207                  * Don't create - just check that it *was* a directory.
1208                  */
1209
1210                 if(!got_stat) {
1211                         DEBUG(0,("open_directory: unable to stat name = %s. Error was %s\n",
1212                                  fname, strerror(errno) ));
1213                         return -1;
1214                 }
1215
1216                 if(!S_ISDIR(st.st_mode)) {
1217                         DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
1218                         return -1;
1219                 }
1220
1221                 *action = FILE_WAS_OPENED;
1222         }
1223         
1224         DEBUG(5,("open_directory: opening directory %s\n",
1225                  fname));
1226
1227         /*
1228          * Setup the files_struct for it.
1229          */
1230         
1231         fsp->fd_ptr = NULL;
1232         conn->num_files_open++;
1233         fsp->mode = 0;
1234         GetTimeOfDay(&fsp->open_time);
1235         fsp->vuid = current_user.vuid;
1236         fsp->size = 0;
1237         fsp->pos = -1;
1238         fsp->open = True;
1239         fsp->can_lock = True;
1240         fsp->can_read = False;
1241         fsp->can_write = False;
1242         fsp->share_mode = 0;
1243         fsp->print_file = False;
1244         fsp->modified = False;
1245         fsp->oplock_type = NO_OPLOCK;
1246         fsp->sent_oplock_break = NO_BREAK_SENT;
1247         fsp->is_directory = True;
1248         fsp->directory_delete_on_close = False;
1249         fsp->conn = conn;
1250         /*
1251          * Note that the file name here is the *untranslated* name
1252          * ie. it is still in the DOS codepage sent from the client.
1253          * All use of this filename will pass though the sys_xxxx
1254          * functions which will do the dos_to_unix translation before
1255          * mapping into a UNIX filename. JRA.
1256          */
1257         string_set(&fsp->fsp_name,fname);
1258         fsp->wbmpx_ptr = NULL;
1259
1260         return 0;
1261 }
1262
1263 /*******************************************************************
1264  Check if the share mode on a file allows it to be deleted or unlinked.
1265  Return True if sharing doesn't prevent the operation.
1266 ********************************************************************/
1267
1268 BOOL check_file_sharing(connection_struct *conn,char *fname, BOOL rename_op)
1269 {
1270   int i;
1271   int ret = False;
1272   share_mode_entry *old_shares = 0;
1273   int num_share_modes;
1274   SMB_STRUCT_STAT sbuf;
1275   pid_t pid = getpid();
1276   SMB_DEV_T dev;
1277   SMB_INO_T inode;
1278
1279   if(!lp_share_modes(SNUM(conn)))
1280     return True;
1281
1282   if (conn->vfs_ops.stat(dos_to_unix(fname,False),&sbuf) == -1) return(True);
1283
1284   dev = sbuf.st_dev;
1285   inode = sbuf.st_ino;
1286
1287   lock_share_entry(conn, dev, inode);
1288   num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
1289
1290   /*
1291    * Check if the share modes will give us access.
1292    */
1293
1294   if(num_share_modes != 0)
1295   {
1296     BOOL broke_oplock;
1297
1298     do
1299     {
1300
1301       broke_oplock = False;
1302       for(i = 0; i < num_share_modes; i++)
1303       {
1304         share_mode_entry *share_entry = &old_shares[i];
1305
1306         /* 
1307          * Break oplocks before checking share modes. See comment in
1308          * open_file_shared for details. 
1309          * Check if someone has an oplock on this file. If so we must 
1310          * break it before continuing. 
1311          */
1312         if(BATCH_OPLOCK_TYPE(share_entry->op_type))
1313         {
1314
1315 #if 0
1316
1317 /* JRA. Try removing this code to see if the new oplock changes
1318    fix the problem. I'm dubious, but Andrew is recommending we
1319    try this....
1320 */
1321
1322           /*
1323            * It appears that the NT redirector may have a bug, in that
1324            * it tries to do an SMBmv on a file that it has open with a
1325            * batch oplock, and then fails to respond to the oplock break
1326            * request. This only seems to occur when the client is doing an
1327            * SMBmv to the smbd it is using - thus we try and detect this
1328            * condition by checking if the file being moved is open and oplocked by
1329            * this smbd process, and then not sending the oplock break in this
1330            * special case. If the file was open with a deny mode that 
1331            * prevents the move the SMBmv will fail anyway with a share
1332            * violation error. JRA.
1333            */
1334           if(rename_op && (share_entry->pid == pid))
1335           {
1336
1337             DEBUG(0,("check_file_sharing: NT redirector workaround - rename attempted on \
1338 batch oplocked file %s, dev = %x, inode = %.0f\n", fname, (unsigned int)dev, (double)inode));
1339
1340             /* 
1341              * This next line is a test that allows the deny-mode
1342              * processing to be skipped. This seems to be needed as
1343              * NT insists on the rename succeeding (in Office 9x no less !).
1344              * This should be removed as soon as (a) MS fix the redirector
1345              * bug or (b) NT SMB support in Samba makes NT not issue the
1346              * call (as is my fervent hope). JRA.
1347              */ 
1348             continue;
1349           }
1350           else
1351 #endif /* 0 */
1352           {
1353
1354             DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
1355 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
1356
1357             /* Oplock break.... */
1358             unlock_share_entry(conn, dev, inode);
1359             if(request_oplock_break(share_entry, dev, inode) == False)
1360             {
1361               free((char *)old_shares);
1362
1363               DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
1364 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
1365
1366               return False;
1367             }
1368             lock_share_entry(conn, dev, inode);
1369             broke_oplock = True;
1370             break;
1371           }
1372         }
1373
1374         /* 
1375          * If this is a delete request and ALLOW_SHARE_DELETE is set then allow 
1376          * this to proceed. This takes precedence over share modes.
1377          */
1378
1379         if(!rename_op && GET_ALLOW_SHARE_DELETE(share_entry->share_mode))
1380           continue;
1381
1382         /* 
1383          * Someone else has a share lock on it, check to see 
1384          * if we can too.
1385          */
1386
1387         if ((GET_DENY_MODE(share_entry->share_mode) != DENY_DOS) || 
1388             (share_entry->pid != pid))
1389           goto free_and_exit;
1390
1391       } /* end for */
1392
1393       if(broke_oplock)
1394       {
1395         free((char *)old_shares);
1396         num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
1397       }
1398     } while(broke_oplock);
1399   }
1400
1401   /* XXXX exactly what share mode combinations should be allowed for
1402      deleting/renaming? */
1403   /* 
1404    * If we got here then either there were no share modes or
1405    * all share modes were DENY_DOS and the pid == getpid() or
1406    * delete access was requested and all share modes had the
1407    * ALLOW_SHARE_DELETE bit set (takes precedence over other
1408    * share modes).
1409    */
1410
1411   ret = True;
1412
1413 free_and_exit:
1414
1415   unlock_share_entry(conn, dev, inode);
1416   if(old_shares != NULL)
1417     free((char *)old_shares);
1418   return(ret);
1419 }