The latest open() code changes broke the NT directory opens. Detect if a
[sfrench/samba-autobuild/.git] / source3 / 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
34 static int fd_open(struct connection_struct *conn, char *fname, 
35                    int flags, mode_t mode)
36 {
37         int fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
38
39         /* Fix for files ending in '.' */
40         if((fd == -1) && (errno == ENOENT) &&
41            (strchr(fname,'.')==NULL)) {
42                 pstrcat(fname,".");
43                 fd = conn->vfs_ops.open(dos_to_unix(fname,False),flags,mode);
44         }
45
46         DEBUG(10,("fd_open: name %s, mode = %d, fd = %d. %s\n", fname, (int)mode, fd,
47                 (fd == -1) ? strerror(errno) : "" ));
48
49         return fd;
50 }
51
52 /****************************************************************************
53  Close the file associated with a fsp.
54 ****************************************************************************/
55
56 int fd_close(struct connection_struct *conn, files_struct *fsp)
57 {
58         int ret = conn->vfs_ops.close(fsp->fd);
59         fsp->fd = -1;
60         return ret;
61 }
62
63
64 /****************************************************************************
65  Check a filename for the pipe string.
66 ****************************************************************************/
67
68 static void check_for_pipe(char *fname)
69 {
70         /* special case of pipe opens */
71         char s[10];
72         StrnCpy(s,fname,sizeof(s)-1);
73         strlower(s);
74         if (strstr(s,"pipe/")) {
75                 DEBUG(3,("Rejecting named pipe open for %s\n",fname));
76                 unix_ERR_class = ERRSRV;
77                 unix_ERR_code = ERRaccess;
78         }
79 }
80
81 /****************************************************************************
82  Open a file.
83 ****************************************************************************/
84
85 static void open_file(files_struct *fsp,connection_struct *conn,
86                       char *fname1,int flags,mode_t mode)
87 {
88         extern struct current_user current_user;
89         pstring fname;
90         int accmode = (flags & O_ACCMODE);
91         SMB_STRUCT_STAT sbuf;
92
93         fsp->open = False;
94         fsp->fd = 0;
95         fsp->oplock_type = NO_OPLOCK;
96         errno = EPERM;
97
98         pstrcpy(fname,fname1);
99
100         /* Check permissions */
101
102         /*
103          * This code was changed after seeing a client open request 
104          * containing the open mode of (DENY_WRITE/read-only) with
105          * the 'create if not exist' bit set. The previous code
106          * would fail to open the file read only on a read-only share
107          * as it was checking the flags parameter  directly against O_RDONLY,
108          * this was failing as the flags parameter was set to O_RDONLY|O_CREAT.
109          * JRA.
110          */
111
112         if (!CAN_WRITE(conn)) {
113                 /* It's a read-only share - fail if we wanted to write. */
114                 if(accmode != O_RDONLY) {
115                         DEBUG(3,("Permission denied opening %s\n",fname));
116                         check_for_pipe(fname);
117                         return;
118                 } else if(flags & O_CREAT) {
119                         /* We don't want to write - but we must make sure that O_CREAT
120                            doesn't create the file if we have write access into the
121                            directory.
122                         */
123                         flags &= ~O_CREAT;
124                 }
125         }
126
127         /* actually do the open */
128         fsp->fd = fd_open(conn, fname, flags, mode);
129
130         if (fsp->fd == -1)  {
131                 DEBUG(3,("Error opening file %s (%s) (flags=%d)\n",
132                          fname,strerror(errno),flags));
133                 check_for_pipe(fname);
134                 return;
135         }
136
137         conn->vfs_ops.fstat(fsp->fd, &sbuf);
138
139         /*
140          * POSIX allows read-only opens of directories. We don't
141          * want to do this (we use a different code path for this)
142          * so catch a directory open and return an EISDIR. JRA.
143          */
144
145         if(S_ISDIR(sbuf.st_mode)) {
146                 fd_close(conn, fsp);
147                 errno = EISDIR;
148                 return;
149         }
150
151         conn->num_files_open++;
152         fsp->mode = sbuf.st_mode;
153         fsp->inode = sbuf.st_ino;
154         fsp->dev = sbuf.st_dev;
155         GetTimeOfDay(&fsp->open_time);
156         fsp->vuid = current_user.vuid;
157         fsp->size = 0;
158         fsp->pos = -1;
159         fsp->open = True;
160         fsp->can_lock = True;
161         fsp->can_read = ((flags & O_WRONLY)==0);
162         fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0);
163         fsp->share_mode = 0;
164         fsp->print_file = False;
165         fsp->modified = False;
166         fsp->oplock_type = NO_OPLOCK;
167         fsp->sent_oplock_break = NO_BREAK_SENT;
168         fsp->is_directory = False;
169         fsp->stat_open = False;
170         fsp->directory_delete_on_close = False;
171         fsp->conn = conn;
172         /*
173          * Note that the file name here is the *untranslated* name
174          * ie. it is still in the DOS codepage sent from the client.
175          * All use of this filename will pass though the sys_xxxx
176          * functions which will do the dos_to_unix translation before
177          * mapping into a UNIX filename. JRA.
178          */
179         string_set(&fsp->fsp_name,fname);
180         fsp->wbmpx_ptr = NULL;      
181         fsp->wcp = NULL; /* Write cache pointer. */
182
183         DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
184                  *sesssetup_user ? sesssetup_user : conn->user,fsp->fsp_name,
185                  BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
186                  conn->num_files_open));
187 }
188
189 /****************************************************************************
190   C. Hoch 11/22/95
191   Helper for open_file_shared. 
192   Truncate a file after checking locking; close file if locked.
193   **************************************************************************/
194
195 static void truncate_unless_locked(files_struct *fsp, connection_struct *conn, int token, 
196                                    BOOL *share_locked)
197 {
198         if (fsp->can_write){
199                 SMB_OFF_T mask2 = ((SMB_OFF_T)0x3) << (SMB_OFF_T_BITS-4);
200                 SMB_OFF_T mask = (mask2<<2);
201                 
202                 if (is_locked(fsp,conn,~mask,0,WRITE_LOCK)){
203                         /* If share modes are in force for this connection we
204                            have the share entry locked. Unlock it before closing. */
205                         if (*share_locked && lp_share_modes(SNUM(conn)))
206                                 unlock_share_entry_fsp(fsp);
207                         close_file(fsp,False);   
208                         /* Share mode no longer locked. */
209                         *share_locked = False;
210                         errno = EACCES;
211                         unix_ERR_class = ERRDOS;
212                   unix_ERR_code = ERRlock;
213                 } else {
214                         sys_ftruncate(fsp->fd,0); 
215                 }
216         }
217 }
218
219
220 /*******************************************************************
221 return True if the filename is one of the special executable types
222 ********************************************************************/
223 static BOOL is_executable(char *fname)
224 {
225         if ((fname = strrchr(fname,'.'))) {
226                 if (strequal(fname,".com") ||
227                     strequal(fname,".dll") ||
228                     strequal(fname,".exe") ||
229                     strequal(fname,".sym")) {
230                         return True;
231                 }
232         }
233         return False;
234 }
235
236 enum {AFAIL,AREAD,AWRITE,AALL};
237
238 /*******************************************************************
239 reproduce the share mode access table
240 this is horrendoously complex, and really can't be justified on any
241 rational grounds except that this is _exactly_ what NT does. See
242 the DENY1 and DENY2 tests in smbtorture for a comprehensive set of
243 test routines.
244 ********************************************************************/
245 static int access_table(int new_deny,int old_deny,int old_mode,
246                         BOOL same_pid, BOOL isexe)
247 {
248           if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL);
249
250           if (same_pid) {
251                   if (isexe && old_mode == DOS_OPEN_RDONLY && 
252                       old_deny == DENY_DOS && new_deny == DENY_READ) {
253                           return AFAIL;
254                   }
255                   if (!isexe && old_mode == DOS_OPEN_RDONLY && 
256                       old_deny == DENY_DOS && new_deny == DENY_DOS) {
257                           return AREAD;
258                   }
259                   if (new_deny == DENY_FCB && old_deny == DENY_DOS) {
260                           if (isexe) return AFAIL;
261                           if (old_mode == DOS_OPEN_RDONLY) return AFAIL;
262                           return AALL;
263                   }
264                   if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) {
265                           if (new_deny == DENY_FCB || new_deny == DENY_READ) {
266                                   if (isexe) return AREAD;
267                                   return AFAIL;
268                           }
269                   }
270                   if (old_deny == DENY_FCB) {
271                           if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL;
272                           return AFAIL;
273                   }
274           }
275
276           if (old_deny == DENY_DOS || new_deny == DENY_DOS || 
277               old_deny == DENY_FCB || new_deny == DENY_FCB) {
278                   if (isexe) {
279                           if (old_deny == DENY_FCB || new_deny == DENY_FCB) {
280                                   return AFAIL;
281                           }
282                           if (old_deny == DENY_DOS) {
283                                   if (new_deny == DENY_READ && 
284                                       (old_mode == DOS_OPEN_RDONLY || 
285                                        old_mode == DOS_OPEN_RDWR)) {
286                                           return AFAIL;
287                                   }
288                                   if (new_deny == DENY_WRITE && 
289                                       (old_mode == DOS_OPEN_WRONLY || 
290                                        old_mode == DOS_OPEN_RDWR)) {
291                                           return AFAIL;
292                                   }
293                                   return AALL;
294                           }
295                           if (old_deny == DENY_NONE) return AALL;
296                           if (old_deny == DENY_READ) return AWRITE;
297                           if (old_deny == DENY_WRITE) return AREAD;
298                   }
299                   /* it isn't a exe, dll, sym or com file */
300                   if (old_deny == new_deny && same_pid)
301                           return(AALL);    
302
303                   if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL;
304                   if (old_mode == DOS_OPEN_RDONLY) return(AREAD);
305                   
306                   return(AFAIL);
307           }
308           
309           switch (new_deny) 
310                   {
311                   case DENY_WRITE:
312                           if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD);
313                           if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE);
314                           if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL);
315                           return(AFAIL);
316                   case DENY_READ:
317                           if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD);
318                           if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE);
319                           if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL);
320                           return(AFAIL);
321                   case DENY_NONE:
322                           if (old_deny==DENY_WRITE) return(AREAD);
323                           if (old_deny==DENY_READ) return(AWRITE);
324                           if (old_deny==DENY_NONE) return(AALL);
325                           return(AFAIL);      
326                   }
327           return(AFAIL);      
328 }
329
330
331 /****************************************************************************
332 check if we can open a file with a share mode
333 ****************************************************************************/
334
335 static int check_share_mode( share_mode_entry *share, int deny_mode, 
336                              char *fname,
337                              BOOL fcbopen, int *flags)
338 {
339   int old_open_mode = GET_OPEN_MODE(share->share_mode);
340   int old_deny_mode = GET_DENY_MODE(share->share_mode);
341
342   /*
343    * Don't allow any open once the delete on close flag has been
344    * set.
345    */
346
347   if(GET_DELETE_ON_CLOSE_FLAG(share->share_mode))
348   {
349     DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n",
350           fname ));
351     unix_ERR_class = ERRDOS;
352     unix_ERR_code = ERRnoaccess;
353     return False;
354   }
355
356   {
357     int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode,
358                                       (share->pid == getpid()),is_executable(fname));
359
360     if ((access_allowed == AFAIL) ||
361         (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) ||
362         (access_allowed == AREAD && *flags != O_RDONLY) ||
363         (access_allowed == AWRITE && *flags != O_WRONLY))
364     {
365       DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n",
366                 deny_mode,old_deny_mode,old_open_mode,
367                 (int)share->pid,fname, fcbopen, *flags, access_allowed));
368
369       unix_ERR_class = ERRDOS;
370       unix_ERR_code = ERRbadshare;
371
372       return False;
373     }
374
375     if (access_allowed == AREAD)
376       *flags = O_RDONLY;
377
378     if (access_allowed == AWRITE)
379       *flags = O_WRONLY;
380
381   }
382
383   return True;
384 }
385
386 /****************************************************************************
387 open a file with a share mode
388 ****************************************************************************/
389
390 void open_file_shared(files_struct *fsp,connection_struct *conn,char *fname,int share_mode,int ofun,
391                       mode_t mode,int oplock_request, int *Access,int *action)
392 {
393   int flags=0;
394   int flags2=0;
395   int deny_mode = GET_DENY_MODE(share_mode);
396   BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode);
397   SMB_STRUCT_STAT sbuf;
398   BOOL file_existed = vfs_file_exist(conn, fname, &sbuf);
399   BOOL share_locked = False;
400   BOOL fcbopen = False;
401   int token = 0;
402   SMB_DEV_T dev = 0;
403   SMB_INO_T inode = 0;
404   int num_share_modes = 0;
405   int oplock_contention_count = 0;
406   BOOL all_current_opens_are_level_II = False;
407   fsp->open = False;
408   fsp->fd = -1;
409
410   if (conn->printer) {
411           /* printers are handled completely differently. Most of the passed parameters are
412              ignored */
413           *Access = DOS_OPEN_WRONLY;
414           *action = FILE_WAS_CREATED;
415           print_open_file(fsp, conn, fname);
416           return;
417   }
418
419   DEBUG(10,("open_file_shared: fname = %s, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n",
420         fname, share_mode, ofun, (int)mode,  oplock_request ));
421
422   if (!check_name(fname,conn)) {
423           return;
424   } 
425
426   /* ignore any oplock requests if oplocks are disabled */
427   if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) {
428           oplock_request = 0;
429   }
430
431   /* this is for OS/2 EAs - try and say we don't support them */
432   if (strstr(fname,".+,;=[].")) 
433   {
434     unix_ERR_class = ERRDOS;
435     /* OS/2 Workplace shell fix may be main code stream in a later release. */ 
436 #if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */
437     unix_ERR_code = ERRcannotopen;
438 #else /* OS2_WPS_FIX */
439     unix_ERR_code = ERROR_EAS_NOT_SUPPORTED;
440 #endif /* OS2_WPS_FIX */
441
442     DEBUG(5,("open_file_shared: OS/2 EA's are not supported.\n"));
443     return;
444   }
445
446   if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed)  
447   {
448     DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n",
449           fname ));
450     errno = EEXIST;
451     return;
452   }
453       
454   if (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST)
455     flags2 |= O_CREAT;
456
457   if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)
458     flags2 |= O_TRUNC;
459
460   if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL)
461     flags2 |= O_EXCL;
462
463   /* note that we ignore the append flag as 
464      append does not mean the same thing under dos and unix */
465
466   switch (GET_OPEN_MODE(share_mode))
467   {
468     case DOS_OPEN_WRONLY: 
469       flags = O_WRONLY; 
470       break;
471     case DOS_OPEN_FCB: 
472       fcbopen = True;
473       flags = O_RDWR; 
474       break;
475     case DOS_OPEN_RDWR: 
476       flags = O_RDWR; 
477       break;
478     default:
479       flags = O_RDONLY;
480       break;
481   }
482
483 #if defined(O_SYNC)
484   if (GET_FILE_SYNC_OPENMODE(share_mode)) {
485           flags2 |= O_SYNC;
486   }
487 #endif /* O_SYNC */
488   
489   if (flags != O_RDONLY && file_existed && 
490       (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,&sbuf)))) 
491   {
492     if (!fcbopen) 
493     {
494       DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n",
495             fname, !CAN_WRITE(conn) ? "share" : "file" ));
496       errno = EACCES;
497       return;
498     }
499     flags = O_RDONLY;
500   }
501
502   if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) 
503   {
504     DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname));
505     errno = EINVAL;
506     return;
507   }
508
509   if (lp_share_modes(SNUM(conn))) 
510   {
511     int i;
512     share_mode_entry *old_shares = 0;
513
514     if (file_existed)
515     {
516       dev = sbuf.st_dev;
517       inode = sbuf.st_ino;
518       lock_share_entry(conn, dev, inode);
519       share_locked = True;
520       num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
521     }
522
523     /*
524      * Check if the share modes will give us access.
525      */
526
527     if(share_locked && (num_share_modes != 0))
528     {
529       BOOL broke_oplock;
530
531       do
532       {
533
534         broke_oplock = False;
535         all_current_opens_are_level_II = True;
536
537         for(i = 0; i < num_share_modes; i++)
538         {
539           share_mode_entry *share_entry = &old_shares[i];
540
541           /* 
542            * By observation of NetBench, oplocks are broken *before* share
543            * modes are checked. This allows a file to be closed by the client
544            * if the share mode would deny access and the client has an oplock. 
545            * Check if someone has an oplock on this file. If so we must break 
546            * it before continuing. 
547            */
548           if((oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) ||
549              (!oplock_request && (share_entry->op_type != NO_OPLOCK)))
550           {
551
552             DEBUG(5,("open_file_shared: breaking oplock (%x) on file %s, \
553 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
554
555             /* Oplock break.... */
556             unlock_share_entry(conn, dev, inode);
557             if(request_oplock_break(share_entry, dev, inode) == False)
558             {
559               free((char *)old_shares);
560
561               DEBUG(0,("open_file_shared: FAILED when breaking oplock (%x) on file %s, \
562 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
563
564               errno = EACCES;
565               unix_ERR_class = ERRDOS;
566               unix_ERR_code = ERRbadshare;
567               return;
568             }
569             lock_share_entry(conn, dev, inode);
570             broke_oplock = True;
571             all_current_opens_are_level_II = False;
572             break;
573           } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) {
574             all_current_opens_are_level_II = False;
575           }
576
577           /* someone else has a share lock on it, check to see 
578              if we can too */
579           if(check_share_mode(share_entry, deny_mode, fname, fcbopen, &flags) == False)
580           {
581             free((char *)old_shares);
582             unlock_share_entry(conn, dev, inode);
583             errno = EACCES;
584             return;
585           }
586
587         } /* end for */
588
589         if(broke_oplock)
590         {
591           free((char *)old_shares);
592           num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
593           oplock_contention_count++;
594         }
595       } while(broke_oplock);
596     }
597
598     if(old_shares != 0)
599       free((char *)old_shares);
600   }
601
602   /*
603    * Refuse to grant an oplock in case the contention limit is
604    * reached when going through the lock list multiple times.
605    */
606
607   if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn)))
608   {
609     oplock_request = 0;
610     DEBUG(4,("open_file_shared: oplock contention = %d. Not granting oplock.\n",
611           oplock_contention_count ));
612   }
613
614   DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n",
615            flags,flags2,(int)mode));
616
617   open_file(fsp,conn,fname,flags|(flags2&~(O_TRUNC)),mode);
618   if (!fsp->open && flags==O_RDWR && errno != ENOENT && fcbopen) {
619           flags = O_RDONLY;
620           open_file(fsp,conn,fname,flags,mode);
621   }
622
623   if (fsp->open) 
624   {
625     int open_mode=0;
626
627     if((share_locked == False) && lp_share_modes(SNUM(conn)))
628     {
629       /* We created the file - thus we must now lock the share entry before creating it. */
630       lock_share_entry_fsp(fsp);
631       share_locked = True;
632     }
633
634     switch (flags) 
635     {
636       case O_RDONLY:
637         open_mode = DOS_OPEN_RDONLY;
638         break;
639       case O_RDWR:
640         open_mode = DOS_OPEN_RDWR;
641         break;
642       case O_WRONLY:
643         open_mode = DOS_OPEN_WRONLY;
644         break;
645     }
646
647     fsp->share_mode = SET_DENY_MODE(deny_mode) | 
648                       SET_OPEN_MODE(open_mode) | 
649                       SET_ALLOW_SHARE_DELETE(allow_share_delete);
650
651     if (Access)
652       (*Access) = open_mode;
653
654     if (action) 
655     {
656       if (file_existed && !(flags2 & O_TRUNC)) *action = FILE_WAS_OPENED;
657       if (!file_existed) *action = FILE_WAS_CREATED;
658       if (file_existed && (flags2 & O_TRUNC)) *action = FILE_WAS_OVERWRITTEN;
659     }
660     /* We must create the share mode entry before truncate as
661        truncate can fail due to locking and have to close the
662        file (which expects the share_mode_entry to be there).
663      */
664     if (lp_share_modes(SNUM(conn)))
665     {
666       uint16 port = 0;
667
668       /* 
669        * Setup the oplock info in both the shared memory and
670        * file structs.
671        */
672
673       if(oplock_request && (num_share_modes == 0) && 
674               !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) {
675         port = global_oplock_port;
676       } else if (oplock_request && all_current_opens_are_level_II) {
677         port = global_oplock_port;
678         oplock_request = LEVEL_II_OPLOCK;
679         set_file_oplock(fsp, oplock_request);
680       } else {
681         port = 0;
682         oplock_request = 0;
683       }
684
685       set_share_mode(fsp, port, oplock_request);
686     }
687
688     if ((flags2&O_TRUNC) && file_existed)
689       truncate_unless_locked(fsp,conn,token,&share_locked);
690   }
691
692   if (share_locked && lp_share_modes(SNUM(conn)))
693     unlock_share_entry_fsp(fsp);
694 }
695
696 /****************************************************************************
697  Open a file for permissions read only. Return a pseudo file entry
698  with the 'stat_open' flag set 
699 ****************************************************************************/
700
701 int open_file_stat(files_struct *fsp,connection_struct *conn,
702                    char *fname, int smb_ofun, SMB_STRUCT_STAT *pst, int *action)
703 {
704         extern struct current_user current_user;
705
706         if(conn->vfs_ops.stat(dos_to_unix(fname, False), pst) < 0) {
707                 DEBUG(0,("open_file_stat: unable to stat name = %s. Error was %s\n",
708                          fname, strerror(errno) ));
709                 return -1;
710         }
711
712         if(S_ISDIR(pst->st_mode)) {
713                 DEBUG(0,("open_file_stat: %s is a directory !\n", fname ));
714                 return -1;
715         }
716
717         *action = FILE_WAS_OPENED;
718         
719         DEBUG(5,("open_file_stat: opening file %s as a stat entry\n", fname));
720
721         /*
722          * Setup the files_struct for it.
723          */
724         
725         fsp->fd = -1;
726         conn->num_files_open++;
727         fsp->mode = 0;
728         GetTimeOfDay(&fsp->open_time);
729         fsp->vuid = current_user.vuid;
730         fsp->size = 0;
731         fsp->pos = -1;
732         fsp->open = True;
733         fsp->can_lock = False;
734         fsp->can_read = False;
735         fsp->can_write = False;
736         fsp->share_mode = 0;
737         fsp->print_file = False;
738         fsp->modified = False;
739         fsp->oplock_type = NO_OPLOCK;
740         fsp->sent_oplock_break = NO_BREAK_SENT;
741         fsp->is_directory = False;
742         fsp->stat_open = True;
743         fsp->directory_delete_on_close = False;
744         fsp->conn = conn;
745         /*
746          * Note that the file name here is the *untranslated* name
747          * ie. it is still in the DOS codepage sent from the client.
748          * All use of this filename will pass though the sys_xxxx
749          * functions which will do the dos_to_unix translation before
750          * mapping into a UNIX filename. JRA.
751          */
752         string_set(&fsp->fsp_name,fname);
753         fsp->wbmpx_ptr = NULL;
754     fsp->wcp = NULL; /* Write cache pointer. */
755
756         return 0;
757 }
758
759 /****************************************************************************
760  Open a directory from an NT SMB call.
761 ****************************************************************************/
762
763 int open_directory(files_struct *fsp,connection_struct *conn,
764                    char *fname, int smb_ofun, mode_t unixmode, int *action)
765 {
766         extern struct current_user current_user;
767         SMB_STRUCT_STAT st;
768         BOOL got_stat = False;
769
770         if(conn->vfs_ops.stat(dos_to_unix(fname, False), &st) == 0) {
771                 got_stat = True;
772         }
773
774         if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) {
775                 errno = EEXIST; /* Setup so correct error is returned to client. */
776                 return -1;
777         }
778
779         if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) {
780
781                 if (got_stat) {
782
783                         if(!S_ISDIR(st.st_mode)) {
784                                 DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
785                                 errno = EACCES;
786                                 return -1;
787                         }
788                         *action = FILE_WAS_OPENED;
789
790                 } else {
791
792                         /*
793                          * Try and create the directory.
794                          */
795
796                         if(!CAN_WRITE(conn)) {
797                                 DEBUG(2,("open_directory: failing create on read-only share\n"));
798                                 errno = EACCES;
799                                 return -1;
800                         }
801
802                         if(conn->vfs_ops.mkdir(dos_to_unix(fname, False), 
803                                                unix_mode(conn,aDIR, fname)) < 0) {
804                                 DEBUG(0,("open_directory: unable to create %s. Error was %s\n",
805                                          fname, strerror(errno) ));
806                                 return -1;
807                         }
808                         *action = FILE_WAS_CREATED;
809
810                 }
811         } else {
812
813                 /*
814                  * Don't create - just check that it *was* a directory.
815                  */
816
817                 if(!got_stat) {
818                         DEBUG(0,("open_directory: unable to stat name = %s. Error was %s\n",
819                                  fname, strerror(errno) ));
820                         return -1;
821                 }
822
823                 if(!S_ISDIR(st.st_mode)) {
824                         DEBUG(0,("open_directory: %s is not a directory !\n", fname ));
825                         return -1;
826                 }
827
828                 *action = FILE_WAS_OPENED;
829         }
830         
831         DEBUG(5,("open_directory: opening directory %s\n",
832                  fname));
833
834         /*
835          * Setup the files_struct for it.
836          */
837         
838         fsp->fd = -1;
839         conn->num_files_open++;
840         fsp->mode = 0;
841         GetTimeOfDay(&fsp->open_time);
842         fsp->vuid = current_user.vuid;
843         fsp->size = 0;
844         fsp->pos = -1;
845         fsp->open = True;
846         fsp->can_lock = True;
847         fsp->can_read = False;
848         fsp->can_write = False;
849         fsp->share_mode = 0;
850         fsp->print_file = False;
851         fsp->modified = False;
852         fsp->oplock_type = NO_OPLOCK;
853         fsp->sent_oplock_break = NO_BREAK_SENT;
854         fsp->is_directory = True;
855         fsp->directory_delete_on_close = False;
856         fsp->conn = conn;
857         /*
858          * Note that the file name here is the *untranslated* name
859          * ie. it is still in the DOS codepage sent from the client.
860          * All use of this filename will pass though the sys_xxxx
861          * functions which will do the dos_to_unix translation before
862          * mapping into a UNIX filename. JRA.
863          */
864         string_set(&fsp->fsp_name,fname);
865         fsp->wbmpx_ptr = NULL;
866
867         return 0;
868 }
869
870 /*******************************************************************
871  Check if the share mode on a file allows it to be deleted or unlinked.
872  Return True if sharing doesn't prevent the operation.
873 ********************************************************************/
874
875 BOOL check_file_sharing(connection_struct *conn,char *fname, BOOL rename_op)
876 {
877   int i;
878   int ret = False;
879   share_mode_entry *old_shares = 0;
880   int num_share_modes;
881   SMB_STRUCT_STAT sbuf;
882   pid_t pid = getpid();
883   SMB_DEV_T dev;
884   SMB_INO_T inode;
885
886   if(!lp_share_modes(SNUM(conn)))
887     return True;
888
889   if (conn->vfs_ops.stat(dos_to_unix(fname,False),&sbuf) == -1) return(True);
890
891   dev = sbuf.st_dev;
892   inode = sbuf.st_ino;
893
894   lock_share_entry(conn, dev, inode);
895   num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
896
897   /*
898    * Check if the share modes will give us access.
899    */
900
901   if(num_share_modes != 0)
902   {
903     BOOL broke_oplock;
904
905     do
906     {
907
908       broke_oplock = False;
909       for(i = 0; i < num_share_modes; i++)
910       {
911         share_mode_entry *share_entry = &old_shares[i];
912
913         /* 
914          * Break oplocks before checking share modes. See comment in
915          * open_file_shared for details. 
916          * Check if someone has an oplock on this file. If so we must 
917          * break it before continuing. 
918          */
919         if(BATCH_OPLOCK_TYPE(share_entry->op_type))
920         {
921
922 #if 0
923
924 /* JRA. Try removing this code to see if the new oplock changes
925    fix the problem. I'm dubious, but Andrew is recommending we
926    try this....
927 */
928
929           /*
930            * It appears that the NT redirector may have a bug, in that
931            * it tries to do an SMBmv on a file that it has open with a
932            * batch oplock, and then fails to respond to the oplock break
933            * request. This only seems to occur when the client is doing an
934            * SMBmv to the smbd it is using - thus we try and detect this
935            * condition by checking if the file being moved is open and oplocked by
936            * this smbd process, and then not sending the oplock break in this
937            * special case. If the file was open with a deny mode that 
938            * prevents the move the SMBmv will fail anyway with a share
939            * violation error. JRA.
940            */
941           if(rename_op && (share_entry->pid == pid))
942           {
943
944             DEBUG(0,("check_file_sharing: NT redirector workaround - rename attempted on \
945 batch oplocked file %s, dev = %x, inode = %.0f\n", fname, (unsigned int)dev, (double)inode));
946
947             /* 
948              * This next line is a test that allows the deny-mode
949              * processing to be skipped. This seems to be needed as
950              * NT insists on the rename succeeding (in Office 9x no less !).
951              * This should be removed as soon as (a) MS fix the redirector
952              * bug or (b) NT SMB support in Samba makes NT not issue the
953              * call (as is my fervent hope). JRA.
954              */ 
955             continue;
956           }
957           else
958 #endif /* 0 */
959           {
960
961             DEBUG(5,("check_file_sharing: breaking oplock (%x) on file %s, \
962 dev = %x, inode = %.0f\n", share_entry->op_type, fname, (unsigned int)dev, (double)inode));
963
964             /* Oplock break.... */
965             unlock_share_entry(conn, dev, inode);
966             if(request_oplock_break(share_entry, dev, inode) == False)
967             {
968               free((char *)old_shares);
969
970               DEBUG(0,("check_file_sharing: FAILED when breaking oplock (%x) on file %s, \
971 dev = %x, inode = %.0f\n", old_shares[i].op_type, fname, (unsigned int)dev, (double)inode));
972
973               return False;
974             }
975             lock_share_entry(conn, dev, inode);
976             broke_oplock = True;
977             break;
978           }
979         }
980
981         /* 
982          * If this is a delete request and ALLOW_SHARE_DELETE is set then allow 
983          * this to proceed. This takes precedence over share modes.
984          */
985
986         if(!rename_op && GET_ALLOW_SHARE_DELETE(share_entry->share_mode))
987           continue;
988
989         /* 
990          * Someone else has a share lock on it, check to see 
991          * if we can too.
992          */
993
994         if ((GET_DENY_MODE(share_entry->share_mode) != DENY_DOS) || 
995             (share_entry->pid != pid))
996           goto free_and_exit;
997
998       } /* end for */
999
1000       if(broke_oplock)
1001       {
1002         free((char *)old_shares);
1003         num_share_modes = get_share_modes(conn, dev, inode, &old_shares);
1004       }
1005     } while(broke_oplock);
1006   }
1007
1008   /* XXXX exactly what share mode combinations should be allowed for
1009      deleting/renaming? */
1010   /* 
1011    * If we got here then either there were no share modes or
1012    * all share modes were DENY_DOS and the pid == getpid() or
1013    * delete access was requested and all share modes had the
1014    * ALLOW_SHARE_DELETE bit set (takes precedence over other
1015    * share modes).
1016    */
1017
1018   ret = True;
1019
1020 free_and_exit:
1021
1022   unlock_share_entry(conn, dev, inode);
1023   if(old_shares != NULL)
1024     free((char *)old_shares);
1025   return(ret);
1026 }