1/* 2 Unix SMB/CIFS implementation. 3 file opening and share modes 4 Copyright (C) Andrew Tridgell 1992-1998 5 Copyright (C) Jeremy Allison 2001 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 24extern userdom_struct current_user_info; 25extern uint16 global_oplock_port; 26extern uint16 global_smbpid; 27extern BOOL global_client_failed_oplock_break; 28extern BOOL qosEnabled; 29 30 31/**************************************************************************** 32 fd support routines - attempt to do a dos_open. 33****************************************************************************/ 34 35static int fd_open(struct connection_struct *conn, const char *fname, 36 int flags, mode_t mode) 37{ 38 int fd; 39#ifdef O_NOFOLLOW 40 if (!lp_symlinks(SNUM(conn))) 41 flags |= O_NOFOLLOW; 42#endif 43 44 fd = SMB_VFS_OPEN(conn,fname,flags,mode); 45 46 DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", fname, 47 flags, (int)mode, fd, (fd == -1) ? strerror(errno) : "" )); 48 49 return fd; 50} 51 52/**************************************************************************** 53 Close the file associated with a fsp. 54****************************************************************************/ 55 56int fd_close(struct connection_struct *conn, files_struct *fsp) 57{ 58 if (fsp->fd == -1) 59 return 0; /* what we used to call a stat open. */ 60 return fd_close_posix(conn, fsp); 61} 62 63 64/**************************************************************************** 65 Check a filename for the pipe string. 66****************************************************************************/ 67 68static void check_for_pipe(const char *fname) 69{ 70 /* special case of pipe opens */ 71 char s[10]; 72 StrnCpy(s,fname,sizeof(s)-1); 73 strlower_m(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 unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED; 79 } 80} 81 82/**************************************************************************** 83 Open a file. 84****************************************************************************/ 85 86static BOOL open_file(files_struct *fsp,connection_struct *conn, 87 const char *fname,SMB_STRUCT_STAT *psbuf,int flags,mode_t mode, uint32 desired_access) 88{ 89 extern struct current_user current_user; 90 int accmode = (flags & O_ACCMODE); 91 int local_flags = flags; 92 93 fsp->fd = -1; 94 fsp->oplock_type = NO_OPLOCK; 95 errno = EPERM; 96 97 /* Check permissions */ 98 99 /* 100 * This code was changed after seeing a client open request 101 * containing the open mode of (DENY_WRITE/read-only) with 102 * the 'create if not exist' bit set. The previous code 103 * would fail to open the file read only on a read-only share 104 * as it was checking the flags parameter directly against O_RDONLY, 105 * this was failing as the flags parameter was set to O_RDONLY|O_CREAT. 106 * JRA. 107 */ 108 109 if (!CAN_WRITE(conn)) { 110 /* It's a read-only share - fail if we wanted to write. */ 111 if(accmode != O_RDONLY) { 112 DEBUG(3,("Permission denied opening %s\n",fname)); 113 check_for_pipe(fname); 114 return False; 115 } else if(flags & O_CREAT) { 116 /* We don't want to write - but we must make sure that O_CREAT 117 doesn't create the file if we have write access into the 118 directory. 119 */ 120 flags &= ~O_CREAT; 121 local_flags &= ~O_CREAT; 122 } 123 } 124 125 /* 126 * This little piece of insanity is inspired by the 127 * fact that an NT client can open a file for O_RDONLY, 128 * but set the create disposition to FILE_EXISTS_TRUNCATE. 129 * If the client *can* write to the file, then it expects to 130 * truncate the file, even though it is opening for readonly. 131 * Quicken uses this stupid trick in backup file creation... 132 * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net> 133 * for helping track this one down. It didn't bite us in 2.0.x 134 * as we always opened files read-write in that release. JRA. 135 */ 136 137 if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) { 138 DEBUG(10,("open_file: truncate requested on read-only open for file %s\n",fname )); 139 local_flags = (flags & ~O_ACCMODE)|O_RDWR; 140 } 141 142 if ((desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) || 143 (local_flags & O_CREAT) || ((local_flags & O_TRUNC) == O_TRUNC) ) { 144 145 /* 146 * We can't actually truncate here as the file may be locked. 147 * open_file_shared will take care of the truncate later. JRA. 148 */ 149 150 local_flags &= ~O_TRUNC; 151 152#if defined(O_NONBLOCK) && defined(S_ISFIFO) 153 /* 154 * We would block on opening a FIFO with no one else on the 155 * other end. Do what we used to do and add O_NONBLOCK to the 156 * open flags. JRA. 157 */ 158 159 if (VALID_STAT(*psbuf) && S_ISFIFO(psbuf->st_mode)) 160 local_flags |= O_NONBLOCK; 161#endif 162 163 /* Don't create files with Microsoft wildcard characters. */ 164 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf) && ms_has_wild(fname)) { 165 unix_ERR_class = ERRDOS; 166 unix_ERR_code = ERRinvalidname; 167 unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID; 168 return False; 169 } 170 171 /* Actually do the open */ 172 fsp->fd = fd_open(conn, fname, local_flags, mode); 173 if (fsp->fd == -1) { 174 DEBUG(3,("Error opening file %s (%s) (local_flags=%d) (flags=%d)\n", 175 fname,strerror(errno),local_flags,flags)); 176 check_for_pipe(fname); 177 return False; 178 } 179 180 /* Inherit the ACL if the file was created. */ 181 if ((local_flags & O_CREAT) && !VALID_STAT(*psbuf)) 182 inherit_access_acl(conn, fname, mode); 183 184 } else 185 fsp->fd = -1; /* What we used to call a stat open. */ 186 187 if (!VALID_STAT(*psbuf)) { 188 int ret; 189 190 if (fsp->fd == -1) 191 ret = SMB_VFS_STAT(conn, fname, psbuf); 192 else { 193 ret = SMB_VFS_FSTAT(fsp,fsp->fd,psbuf); 194 /* If we have an fd, this stat should succeed. */ 195 if (ret == -1) 196 DEBUG(0,("Error doing fstat on open file %s (%s)\n", fname,strerror(errno) )); 197 } 198 199 /* For a non-io open, this stat failing means file not found. JRA */ 200 if (ret == -1) { 201 fd_close(conn, fsp); 202 return False; 203 } 204 } 205 206 /* 207 * POSIX allows read-only opens of directories. We don't 208 * want to do this (we use a different code path for this) 209 * so catch a directory open and return an EISDIR. JRA. 210 */ 211 212 if(S_ISDIR(psbuf->st_mode)) { 213 fd_close(conn, fsp); 214 errno = EISDIR; 215 return False; 216 } 217 218 fsp->mode = psbuf->st_mode; 219 fsp->inode = psbuf->st_ino; 220 fsp->dev = psbuf->st_dev; 221 fsp->vuid = current_user.vuid; 222 fsp->file_pid = global_smbpid; 223 fsp->size = psbuf->st_size; 224 fsp->can_lock = True; 225 fsp->can_read = ((flags & O_WRONLY)==0); 226 fsp->can_write = ((flags & (O_WRONLY|O_RDWR))!=0); 227 fsp->share_mode = 0; 228 fsp->desired_access = desired_access; 229 fsp->print_file = False; 230 fsp->modified = False; 231 fsp->oplock_type = NO_OPLOCK; 232 fsp->sent_oplock_break = NO_BREAK_SENT; 233 fsp->is_directory = False; 234 fsp->is_stat = False; 235 fsp->directory_delete_on_close = False; 236 string_set(&fsp->fsp_name,fname); 237 fsp->wcp = NULL; /* Write cache pointer. */ 238 239 DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n", 240 *current_user_info.smb_name ? current_user_info.smb_name : conn->user,fsp->fsp_name, 241 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write), 242 conn->num_files_open + 1)); 243 244 return True; 245} 246 247/**************************************************************************** 248 C. Hoch 11/22/95 249 Helper for open_file_shared. 250 Truncate a file after checking locking; close file if locked. 251 **************************************************************************/ 252 253static int truncate_unless_locked(struct connection_struct *conn, files_struct *fsp) 254{ 255 SMB_BIG_UINT mask = (SMB_BIG_UINT)-1; 256 257 if (is_locked(fsp,fsp->conn,mask,0,WRITE_LOCK,True)){ 258 errno = EACCES; 259 unix_ERR_class = ERRDOS; 260 unix_ERR_code = ERRlock; 261 unix_ERR_ntstatus = dos_to_ntstatus(ERRDOS, ERRlock); 262 return -1; 263 } else { 264 return SMB_VFS_FTRUNCATE(fsp,fsp->fd,0); 265 } 266} 267 268/******************************************************************* 269return True if the filename is one of the special executable types 270********************************************************************/ 271static BOOL is_executable(const char *fname) 272{ 273 if ((fname = strrchr_m(fname,'.'))) { 274 if (strequal(fname,".com") || 275 strequal(fname,".dll") || 276 strequal(fname,".exe") || 277 strequal(fname,".sym")) { 278 return True; 279 } 280 } 281 return False; 282} 283 284enum {AFAIL,AREAD,AWRITE,AALL}; 285 286/******************************************************************* 287reproduce the share mode access table 288this is horrendoously complex, and really can't be justified on any 289rational grounds except that this is _exactly_ what NT does. See 290the DENY1 and DENY2 tests in smbtorture for a comprehensive set of 291test routines. 292********************************************************************/ 293static int access_table(int new_deny,int old_deny,int old_mode, 294 BOOL same_pid, BOOL isexe) 295{ 296 if (new_deny == DENY_ALL || old_deny == DENY_ALL) return(AFAIL); 297 298 if (same_pid) { 299 if (isexe && old_mode == DOS_OPEN_RDONLY && 300 old_deny == DENY_DOS && new_deny == DENY_READ) { 301 return AFAIL; 302 } 303 if (!isexe && old_mode == DOS_OPEN_RDONLY && 304 old_deny == DENY_DOS && new_deny == DENY_DOS) { 305 return AREAD; 306 } 307 if (new_deny == DENY_FCB && old_deny == DENY_DOS) { 308 if (isexe) return AFAIL; 309 if (old_mode == DOS_OPEN_RDONLY) return AFAIL; 310 return AALL; 311 } 312 if (old_mode == DOS_OPEN_RDONLY && old_deny == DENY_DOS) { 313 if (new_deny == DENY_FCB || new_deny == DENY_READ) { 314 if (isexe) return AREAD; 315 return AFAIL; 316 } 317 } 318 if (old_deny == DENY_FCB) { 319 if (new_deny == DENY_DOS || new_deny == DENY_FCB) return AALL; 320 return AFAIL; 321 } 322 } 323 324 if (old_deny == DENY_DOS || new_deny == DENY_DOS || 325 old_deny == DENY_FCB || new_deny == DENY_FCB) { 326 if (isexe) { 327 if (old_deny == DENY_FCB || new_deny == DENY_FCB) { 328 return AFAIL; 329 } 330 if (old_deny == DENY_DOS) { 331 if (new_deny == DENY_READ && 332 (old_mode == DOS_OPEN_RDONLY || 333 old_mode == DOS_OPEN_RDWR)) { 334 return AFAIL; 335 } 336 if (new_deny == DENY_WRITE && 337 (old_mode == DOS_OPEN_WRONLY || 338 old_mode == DOS_OPEN_RDWR)) { 339 return AFAIL; 340 } 341 return AALL; 342 } 343 if (old_deny == DENY_NONE) return AALL; 344 if (old_deny == DENY_READ) return AWRITE; 345 if (old_deny == DENY_WRITE) return AREAD; 346 } 347 /* it isn't a exe, dll, sym or com file */ 348 if (old_deny == new_deny && same_pid) 349 return(AALL); 350 351 if (old_deny == DENY_READ || new_deny == DENY_READ) return AFAIL; 352 if (old_mode == DOS_OPEN_RDONLY) return(AREAD); 353 354 return(AFAIL); 355 } 356 357 switch (new_deny) 358 { 359 case DENY_WRITE: 360 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_RDONLY) return(AREAD); 361 if (old_deny==DENY_READ && old_mode==DOS_OPEN_RDONLY) return(AWRITE); 362 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_RDONLY) return(AALL); 363 return(AFAIL); 364 case DENY_READ: 365 if (old_deny==DENY_WRITE && old_mode==DOS_OPEN_WRONLY) return(AREAD); 366 if (old_deny==DENY_READ && old_mode==DOS_OPEN_WRONLY) return(AWRITE); 367 if (old_deny==DENY_NONE && old_mode==DOS_OPEN_WRONLY) return(AALL); 368 return(AFAIL); 369 case DENY_NONE: 370 if (old_deny==DENY_WRITE) return(AREAD); 371 if (old_deny==DENY_READ) return(AWRITE); 372 if (old_deny==DENY_NONE) return(AALL); 373 return(AFAIL); 374 } 375 return(AFAIL); 376} 377 378 379/**************************************************************************** 380check if we can open a file with a share mode 381****************************************************************************/ 382 383static BOOL check_share_mode(connection_struct *conn, share_mode_entry *share, int share_mode, uint32 desired_access, 384 const char *fname, BOOL fcbopen, int *flags) 385{ 386 int deny_mode = GET_DENY_MODE(share_mode); 387 int old_open_mode = GET_OPEN_MODE(share->share_mode); 388 int old_deny_mode = GET_DENY_MODE(share->share_mode); 389 390 /* 391 * share modes = false means don't bother to check for 392 * DENY mode conflict. This is a *really* bad idea :-). JRA. 393 */ 394 395 if(!lp_share_modes(SNUM(conn))) 396 return True; 397 398 /* 399 * Don't allow any opens once the delete on close flag has been 400 * set. 401 */ 402 403 if (GET_DELETE_ON_CLOSE_FLAG(share->share_mode)) { 404 DEBUG(5,("check_share_mode: Failing open on file %s as delete on close flag is set.\n", 405 fname )); 406 /* Use errno to map to correct error. */ 407 unix_ERR_class = SMB_SUCCESS; 408 unix_ERR_code = 0; 409 unix_ERR_ntstatus = NT_STATUS_OK; 410 return False; 411 } 412 413 /* this is a nasty hack, but necessary until we rewrite our open 414 handling to use a NTCreateX call as the basic call. 415 NT may open a file with neither read nor write access, and in 416 this case it expects the open not to conflict with any 417 existing deny modes. This happens (for example) during a 418 "xcopy /o" where the second file descriptor is used for 419 ACL sets 420 (tridge) 421 */ 422 423 /* 424 * This is a bit wierd - the test for desired access not having the 425 * critical bits seems seems odd. Firstly, if both opens have no 426 * critical bits then always ignore. Then check the "allow delete" 427 * then check for either. This probably isn't quite right yet but 428 * gets us much closer. JRA. 429 */ 430 431 /* 432 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE 433 * and the existing desired_acces then share modes don't conflict. 434 */ 435 436 if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) && 437 !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) { 438 439 /* 440 * Wrinkle discovered by smbtorture.... 441 * If both are non-io open and requester is asking for delete and current open has delete access 442 * but neither open has allowed file share delete then deny.... this is very strange and 443 * seems to be the only case in which non-io opens conflict. JRA. 444 */ 445 446 if ((desired_access & DELETE_ACCESS) && (share->desired_access & DELETE_ACCESS) && 447 (!GET_ALLOW_SHARE_DELETE(share->share_mode) || !GET_ALLOW_SHARE_DELETE(share_mode))) { 448 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requests conflict.\n", 449 fname )); 450 unix_ERR_class = ERRDOS; 451 unix_ERR_code = ERRbadshare; 452 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 453 454 return False; 455 } 456 457 DEBUG(5,("check_share_mode: Allowing open on file %s as both desired access (0x%x) \ 458and existing desired access (0x%x) are non-data opens\n", 459 fname, (unsigned int)desired_access, (unsigned int)share->desired_access )); 460 return True; 461 } 462 463 /* 464 * If delete access was requested and the existing share mode doesn't have 465 * ALLOW_SHARE_DELETE then deny. 466 */ 467 468 if ((desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share->share_mode)) { 469 DEBUG(5,("check_share_mode: Failing open on file %s as delete access requested and allow share delete not set.\n", 470 fname )); 471 unix_ERR_class = ERRDOS; 472 unix_ERR_code = ERRbadshare; 473 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 474 475 return False; 476 } 477 478 /* 479 * The inverse of the above. 480 * If delete access was granted and the new share mode doesn't have 481 * ALLOW_SHARE_DELETE then deny. 482 */ 483 484 if ((share->desired_access & DELETE_ACCESS) && !GET_ALLOW_SHARE_DELETE(share_mode)) { 485 DEBUG(5,("check_share_mode: Failing open on file %s as delete access granted and allow share delete not requested.\n", 486 fname )); 487 unix_ERR_class = ERRDOS; 488 unix_ERR_code = ERRbadshare; 489 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 490 491 return False; 492 } 493 494 /* 495 * If desired_access doesn't contain READ_DATA,WRITE_DATA,APPEND_DATA or EXECUTE 496 * then share modes don't conflict. Likewise with existing desired access. 497 */ 498 499 if ( !(desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) || 500 !(share->desired_access & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) ) { 501 DEBUG(5,("check_share_mode: Allowing open on file %s as desired access (0x%x) doesn't conflict with\ 502existing desired access (0x%x).\n", fname, (unsigned int)desired_access, (unsigned int)share->desired_access )); 503 return True; 504 } 505 506 { 507 int access_allowed = access_table(deny_mode,old_deny_mode,old_open_mode, 508 (share->pid == sys_getpid()),is_executable(fname)); 509//Yen 510 DEBUG(0, ("Debug:::deny_mode = %d, old_deny_mode=%d, old_open_mode=%d, getpid=%d, exe=%d\n", deny_mode,old_deny_mode,old_open_mode, (share->pid == sys_getpid()),is_executable(fname))); 511 512 if ((access_allowed == AFAIL) || 513 (!fcbopen && (access_allowed == AREAD && *flags == O_RDWR)) || 514 (access_allowed == AREAD && *flags != O_RDONLY) || 515 (access_allowed == AWRITE && *flags != O_WRONLY)) { 516 517 DEBUG(2,("Share violation on file (%d,%d,%d,%d,%s,fcbopen = %d, flags = %d) = %d\n", 518 deny_mode,old_deny_mode,old_open_mode, 519 (int)share->pid,fname, fcbopen, *flags, access_allowed)); 520 521 unix_ERR_class = ERRDOS; 522 unix_ERR_code = ERRbadshare; 523 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 524 525 526 return False; 527 } 528 529 if (access_allowed == AREAD) 530 *flags = O_RDONLY; 531 532 if (access_allowed == AWRITE) 533 *flags = O_WRONLY; 534 535 } 536 537 DEBUG(0, ("Debug:::deny_mode = %d, old_deny_mode=%d, old_open_mode=%d, getpid=%d, exe=%d\n", deny_mode,old_deny_mode,old_open_mode, (share->pid == sys_getpid()),is_executable(fname))); 538 539 return True; 540} 541 542 543#if defined(DEVELOPER) 544static void validate_my_share_entries(int num, share_mode_entry *share_entry) 545{ 546 files_struct *fsp; 547 548 if (share_entry->pid != sys_getpid()) 549 return; 550 551 fsp = file_find_dif(share_entry->dev, share_entry->inode, share_entry->share_file_id); 552 if (!fsp) { 553 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) )); 554 smb_panic("validate_my_share_entries: Cannot match a share entry with an open file\n"); 555 } 556 557 if (((uint16)fsp->oplock_type) != share_entry->op_type) { 558 pstring str; 559 DEBUG(0,("validate_my_share_entries: PANIC : %s\n", share_mode_str(num, share_entry) )); 560 slprintf(str, sizeof(str)-1, "validate_my_share_entries: file %s, oplock_type = 0x%x, op_type = 0x%x\n", 561 fsp->fsp_name, (unsigned int)fsp->oplock_type, (unsigned int)share_entry->op_type ); 562 smb_panic(str); 563 } 564} 565#endif 566 567/**************************************************************************** 568 Deal with open deny mode and oplock break processing. 569 Invarient: Share mode must be locked on entry and exit. 570 Returns -1 on error, or number of share modes on success (may be zero). 571****************************************************************************/ 572 573static int open_mode_check(connection_struct *conn, const char *fname, SMB_DEV_T dev, 574 SMB_INO_T inode, 575 uint32 desired_access, 576 int share_mode, int *p_flags, int *p_oplock_request, 577 BOOL *p_all_current_opens_are_level_II) 578{ 579 int i; 580 int num_share_modes; 581 int oplock_contention_count = 0; 582 share_mode_entry *old_shares = 0; 583 BOOL fcbopen = False; 584 BOOL broke_oplock; 585 586 if(GET_OPEN_MODE(share_mode) == DOS_OPEN_FCB) 587 fcbopen = True; 588 589 num_share_modes = get_share_modes(conn, dev, inode, &old_shares); 590 591 if(num_share_modes == 0) 592 return 0; 593 594 /* 595 * Check if the share modes will give us access. 596 */ 597 598 do { 599 share_mode_entry broken_entry; 600 601 broke_oplock = False; 602 *p_all_current_opens_are_level_II = True; 603 604 for(i = 0; i < num_share_modes; i++) { 605 share_mode_entry *share_entry = &old_shares[i]; 606 607#if defined(DEVELOPER) 608 validate_my_share_entries(i, share_entry); 609#endif 610 611 /* 612 * By observation of NetBench, oplocks are broken *before* share 613 * modes are checked. This allows a file to be closed by the client 614 * if the share mode would deny access and the client has an oplock. 615 * Check if someone has an oplock on this file. If so we must break 616 * it before continuing. 617 */ 618 619 if((*p_oplock_request && EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type)) || 620 (!*p_oplock_request && (share_entry->op_type != NO_OPLOCK))) { 621 622 BOOL opb_ret; 623 624 DEBUG(5,("open_mode_check: oplock_request = %d, breaking oplock (%x) on file %s, \ 625dev = %x, inode = %llu\n", *p_oplock_request, share_entry->op_type, fname, (unsigned int)dev, inode)); 626 627 /* Ensure the reply for the open uses the correct sequence number. */ 628 /* This isn't a real deferred packet as it's response will also increment 629 * the sequence. 630 */ 631 srv_defer_sign_response(get_current_mid()); 632 633 /* Oplock break - unlock to request it. */ 634 unlock_share_entry(conn, dev, inode); 635 636 opb_ret = request_oplock_break(share_entry, False); 637 638 /* Now relock. */ 639 lock_share_entry(conn, dev, inode); 640 641 if(opb_ret == False) { 642 DEBUG(0,("open_mode_check: FAILED when breaking oplock (%x) on file %s, \ 643dev = %x, inode = %llu\n", old_shares[i].op_type, fname, (unsigned int)dev, inode)); 644 SAFE_FREE(old_shares); 645 errno = EACCES; 646 unix_ERR_class = ERRDOS; 647 unix_ERR_code = ERRbadshare; 648 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 649 return -1; 650 } 651 652 broke_oplock = True; 653 broken_entry = *share_entry; 654 break; 655 656 } else if (!LEVEL_II_OPLOCK_TYPE(share_entry->op_type)) { 657 *p_all_current_opens_are_level_II = False; 658 } 659 660 /* someone else has a share lock on it, check to see if we can too */ 661 if (!check_share_mode(conn, share_entry, share_mode, desired_access, 662 fname, fcbopen, p_flags)) { 663 SAFE_FREE(old_shares); 664 errno = EACCES; 665 return -1; 666 } 667 668 } /* end for */ 669 670 if(broke_oplock) { 671 SAFE_FREE(old_shares); 672 num_share_modes = get_share_modes(conn, dev, inode, &old_shares); 673 oplock_contention_count++; 674 675 /* Paranoia check that this is no longer an exlusive entry. */ 676 for(i = 0; i < num_share_modes; i++) { 677 share_mode_entry *share_entry = &old_shares[i]; 678 679 if (share_modes_identical(&broken_entry, share_entry) && 680 EXCLUSIVE_OPLOCK_TYPE(share_entry->op_type) ) { 681 682 /* 683 * This should not happen. The target left this oplock 684 * as exlusive.... The process *must* be dead.... 685 */ 686 687 DEBUG(0,("open_mode_check: exlusive oplock left by process %d after break ! For file %s, \ 688dev = %x, inode = %llu. Deleting it to continue...\n", (int)broken_entry.pid, fname, (unsigned int)dev, inode)); 689 690 if (process_exists(broken_entry.pid)) { 691 DEBUG(0,("open_mode_check: Existent process %lu left active oplock.\n", 692 (unsigned long)broken_entry.pid )); 693 } 694 695 if (del_share_entry(dev, inode, &broken_entry, NULL) == -1) { 696 errno = EACCES; 697 unix_ERR_class = ERRDOS; 698 unix_ERR_code = ERRbadshare; 699 unix_ERR_ntstatus = NT_STATUS_SHARING_VIOLATION; 700 return -1; 701 } 702 703 /* 704 * We must reload the share modes after deleting the 705 * other process's entry. 706 */ 707 708 SAFE_FREE(old_shares); 709 num_share_modes = get_share_modes(conn, dev, inode, &old_shares); 710 break; 711 } 712 } /* end for paranoia... */ 713 } /* end if broke_oplock */ 714 715 } while(broke_oplock); 716 717 if(old_shares != 0) 718 SAFE_FREE(old_shares); 719 720 /* 721 * Refuse to grant an oplock in case the contention limit is 722 * reached when going through the lock list multiple times. 723 */ 724 725 if(oplock_contention_count >= lp_oplock_contention_limit(SNUM(conn))) { 726 *p_oplock_request = 0; 727 DEBUG(4,("open_mode_check: oplock contention = %d. Not granting oplock.\n", 728 oplock_contention_count )); 729 } 730 731 return num_share_modes; 732} 733 734/**************************************************************************** 735set a kernel flock on a file for NFS interoperability 736this requires a patch to Linux 737****************************************************************************/ 738static void kernel_flock(files_struct *fsp, int deny_mode) 739{ 740#if HAVE_KERNEL_SHARE_MODES 741 int kernel_mode = 0; 742 if (deny_mode == DENY_READ) kernel_mode = LOCK_MAND|LOCK_WRITE; 743 else if (deny_mode == DENY_WRITE) kernel_mode = LOCK_MAND|LOCK_READ; 744 else if (deny_mode == DENY_ALL) kernel_mode = LOCK_MAND; 745 if (kernel_mode) flock(fsp->fd, kernel_mode); 746#endif 747 ;; 748} 749 750 751static BOOL open_match_attributes(connection_struct *conn, char *path, mode_t existing_mode, 752 mode_t new_mode, mode_t *returned_mode) 753{ 754 uint32 old_dos_mode, new_dos_mode; 755 uint32 noarch_old_dos_mode, noarch_new_dos_mode; 756 SMB_STRUCT_STAT sbuf; 757 758 ZERO_STRUCT(sbuf); 759 760 sbuf.st_mode = existing_mode; 761 old_dos_mode = dos_mode(conn, path, &sbuf); 762 763 sbuf.st_mode = new_mode; 764 new_dos_mode = dos_mode(conn, path, &sbuf); 765 766 noarch_old_dos_mode = (old_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE); 767 noarch_new_dos_mode = (new_dos_mode & ~FILE_ATTRIBUTE_ARCHIVE); 768 769 if((noarch_old_dos_mode == 0 && noarch_new_dos_mode != 0) || 770 (noarch_old_dos_mode != 0 && ((noarch_old_dos_mode & noarch_new_dos_mode) == noarch_old_dos_mode))) 771 *returned_mode = new_mode; 772 else 773 *returned_mode = (mode_t)0; 774 775 DEBUG(10,("open_match_attributes: file %s old_dos_mode = 0x%x, existing_mode = 0%o, new_dos_mode = 0x%x returned_mode = 0%o\n", 776 path, 777 old_dos_mode, (unsigned int)existing_mode, new_dos_mode, (unsigned int)*returned_mode )); 778 779 /* If we're mapping SYSTEM and HIDDEN ensure they match. */ 780 if (lp_map_system(SNUM(conn))) { 781 if ((old_dos_mode & FILE_ATTRIBUTE_SYSTEM) && !(new_dos_mode & FILE_ATTRIBUTE_SYSTEM)) 782 return False; 783 } 784 if (lp_map_hidden(SNUM(conn))) { 785 if ((old_dos_mode & FILE_ATTRIBUTE_HIDDEN) && !(new_dos_mode & FILE_ATTRIBUTE_HIDDEN)) 786 return False; 787 } 788 return True; 789} 790 791/**************************************************************************** 792 Open a file with a share mode. 793****************************************************************************/ 794 795files_struct *open_file_shared(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf, 796 int share_mode,int ofun, mode_t mode,int oplock_request, 797 int *Access,int *action) 798{ 799 return open_file_shared1(conn, fname, psbuf, 0, share_mode, ofun, mode, 800 oplock_request, Access, action); 801} 802 803/**************************************************************************** 804 Open a file with a share mode. 805****************************************************************************/ 806 807files_struct *open_file_shared1(connection_struct *conn,char *fname, SMB_STRUCT_STAT *psbuf, 808 uint32 desired_access, 809 int share_mode,int ofun, mode_t mode,int oplock_request, 810 int *Access,int *action) 811{ 812 int flags=0; 813 int flags2=0; 814 int deny_mode = GET_DENY_MODE(share_mode); 815 BOOL allow_share_delete = GET_ALLOW_SHARE_DELETE(share_mode); 816 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode); 817 BOOL file_existed = VALID_STAT(*psbuf); 818 BOOL fcbopen = False; 819 BOOL def_acl = False; 820 SMB_DEV_T dev = 0; 821 SMB_INO_T inode = 0; 822 int num_share_modes = 0; 823 BOOL all_current_opens_are_level_II = False; 824 BOOL fsp_open = False; 825 files_struct *fsp = NULL; 826 int open_mode=0; 827 uint16 port = 0; 828 mode_t new_mode = (mode_t)0; 829 830 if (conn->printer) { 831 /* printers are handled completely differently. Most of the passed parameters are 832 ignored */ 833 if (Access) 834 *Access = DOS_OPEN_WRONLY; 835 if (action) 836 *action = FILE_WAS_CREATED; 837 return print_fsp_open(conn, fname); 838 } 839 840 fsp = file_new(conn); 841 if(!fsp) 842 return NULL; 843 844 DEBUG(10,("open_file_shared: fname = %s, share_mode = %x, ofun = %x, mode = %o, oplock request = %d\n", 845 fname, share_mode, ofun, (int)mode, oplock_request )); 846 847 if (!check_name(fname,conn)) { 848 file_free(fsp); 849 return NULL; 850 } 851 852 /* ignore any oplock requests if oplocks are disabled */ 853 if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break) { 854 oplock_request = 0; 855 } 856 857 /* this is for OS/2 EAs - try and say we don't support them */ 858 if (strstr(fname,".+,;=[].")) { 859 unix_ERR_class = ERRDOS; 860 /* OS/2 Workplace shell fix may be main code stream in a later release. */ 861#if 1 /* OS2_WPS_FIX - Recent versions of OS/2 need this. */ 862 unix_ERR_code = ERRcannotopen; 863#else /* OS2_WPS_FIX */ 864 unix_ERR_code = ERROR_EAS_NOT_SUPPORTED; 865#endif /* OS2_WPS_FIX */ 866 867 DEBUG(5,("open_file_shared: OS/2 EA's are not supported.\n")); 868 file_free(fsp); 869 return NULL; 870 } 871 872 if ((GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) && file_existed) { 873 DEBUG(5,("open_file_shared: create new requested for file %s and file already exists.\n", 874 fname )); 875 file_free(fsp); 876 errno = EEXIST; 877 return NULL; 878 } 879 880 if (CAN_WRITE(conn) && (GET_FILE_CREATE_DISPOSITION(ofun) == FILE_CREATE_IF_NOT_EXIST)) 881 flags2 |= O_CREAT; 882 883 if (CAN_WRITE(conn) && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)) 884 flags2 |= O_TRUNC; 885 886 /* We only care about matching attributes on file exists and truncate. */ 887 if (file_existed && (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_TRUNCATE)) { 888 if (!open_match_attributes(conn, fname, psbuf->st_mode, mode, &new_mode)) { 889 DEBUG(5,("open_file_shared: attributes missmatch for file %s (0%o, 0%o)\n", 890 fname, (int)psbuf->st_mode, (int)mode )); 891 file_free(fsp); 892 errno = EACCES; 893 return NULL; 894 } 895 } 896 897 if (GET_FILE_OPEN_DISPOSITION(ofun) == FILE_EXISTS_FAIL) 898 flags2 |= O_EXCL; 899 900 /* note that we ignore the append flag as 901 append does not mean the same thing under dos and unix */ 902 903 switch (GET_OPEN_MODE(share_mode)) { 904 case DOS_OPEN_WRONLY: 905 flags = O_WRONLY; 906 if (desired_access == 0) 907 desired_access = FILE_WRITE_DATA; 908 break; 909 case DOS_OPEN_FCB: 910 fcbopen = True; 911 flags = O_RDWR; 912 if (desired_access == 0) 913 desired_access = FILE_READ_DATA|FILE_WRITE_DATA; 914 break; 915 case DOS_OPEN_RDWR: 916 flags = O_RDWR; 917 if (desired_access == 0) 918 desired_access = FILE_READ_DATA|FILE_WRITE_DATA; 919 break; 920 default: 921 flags = O_RDONLY; 922 if (desired_access == 0) 923 desired_access = FILE_READ_DATA; 924 break; 925 } 926 927#if defined(O_SYNC) 928 if (GET_FILE_SYNC_OPENMODE(share_mode)) { 929 flags2 |= O_SYNC; 930 } 931#endif /* O_SYNC */ 932 933 if (flags != O_RDONLY && file_existed && 934 (!CAN_WRITE(conn) || IS_DOS_READONLY(dos_mode(conn,fname,psbuf)))) { 935 if (!fcbopen) { 936 DEBUG(5,("open_file_shared: read/write access requested for file %s on read only %s\n", 937 fname, !CAN_WRITE(conn) ? "share" : "file" )); 938 file_free(fsp); 939 errno = EACCES; 940 return NULL; 941 } 942 flags = O_RDONLY; 943 } 944 945 if (deny_mode > DENY_NONE && deny_mode!=DENY_FCB) { 946 DEBUG(2,("Invalid deny mode %d on file %s\n",deny_mode,fname)); 947 file_free(fsp); 948 errno = EINVAL; 949 return NULL; 950 } 951 952 if (file_existed) { 953 954 dev = psbuf->st_dev; 955 inode = psbuf->st_ino; 956 957 lock_share_entry(conn, dev, inode); 958 959 num_share_modes = open_mode_check(conn, fname, dev, inode, 960 desired_access, 961 share_mode, 962 &flags, &oplock_request, &all_current_opens_are_level_II); 963 if(num_share_modes == -1) { 964 965 /* 966 * This next line is a subtlety we need for MS-Access. If a file open will 967 * fail due to share permissions and also for security (access) 968 * reasons, we need to return the access failed error, not the 969 * share error. This means we must attempt to open the file anyway 970 * in order to get the UNIX access error - even if we're going to 971 * fail the open for share reasons. This is bad, as we're burning 972 * another fd if there are existing locks but there's nothing else 973 * we can do. We also ensure we're not going to create or tuncate 974 * the file as we only want an access decision at this stage. JRA. 975 */ 976 errno = 0; 977 fsp_open = open_file(fsp,conn,fname,psbuf, 978 flags|(flags2&~(O_TRUNC|O_CREAT)),mode,desired_access); 979 980 DEBUG(4,("open_file_shared : share_mode deny - calling open_file with \ 981flags=0x%X flags2=0x%X mode=0%o returned %d\n", 982 flags,(flags2&~(O_TRUNC|O_CREAT)),(int)mode,(int)fsp_open )); 983 984 if (!fsp_open && errno) { 985 unix_ERR_class = ERRDOS; 986 unix_ERR_code = ERRnoaccess; 987 unix_ERR_ntstatus = NT_STATUS_ACCESS_DENIED; 988 } 989 990 unlock_share_entry(conn, dev, inode); 991 if (fsp_open) 992 fd_close(conn, fsp); 993 file_free(fsp); 994 return NULL; 995 } 996 997 /* 998 * We exit this block with the share entry *locked*..... 999 */ 1000 } 1001 1002 /* 1003 * Ensure we pay attention to default ACLs on directories if required. 1004 */ 1005 1006 if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) && 1007 (def_acl = directory_has_default_acl(conn, parent_dirname(fname)))) 1008 mode = 0777; 1009 1010 DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o\n", 1011 flags,flags2,(int)mode)); 1012 1013 /* 1014 * open_file strips any O_TRUNC flags itself. 1015 */ 1016 1017 fsp_open = open_file(fsp,conn,fname,psbuf,flags|flags2,mode,desired_access); 1018 1019 if (!fsp_open && (flags == O_RDWR) && (errno != ENOENT) && fcbopen) { 1020 if((fsp_open = open_file(fsp,conn,fname,psbuf,O_RDONLY,mode,desired_access)) == True) 1021 flags = O_RDONLY; 1022 } 1023 1024 if (!fsp_open) { 1025 if(file_existed) 1026 unlock_share_entry(conn, dev, inode); 1027 file_free(fsp); 1028 return NULL; 1029 } 1030 1031 /* 1032 * Deal with the race condition where two smbd's detect the file doesn't 1033 * exist and do the create at the same time. One of them will win and 1034 * set a share mode, the other (ie. this one) should check if the 1035 * requested share mode for this create is allowed. 1036 */ 1037 1038 if (!file_existed) { 1039 1040 /* 1041 * Now the file exists and fsp is successfully opened, 1042 * fsp->dev and fsp->inode are valid and should replace the 1043 * dev=0,inode=0 from a non existent file. Spotted by 1044 * Nadav Danieli <nadavd@exanet.com>. JRA. 1045 */ 1046 1047 dev = fsp->dev; 1048 inode = fsp->inode; 1049 1050 lock_share_entry_fsp(fsp); 1051 1052 num_share_modes = open_mode_check(conn, fname, dev, inode, 1053 desired_access, 1054 share_mode, 1055 &flags, &oplock_request, &all_current_opens_are_level_II); 1056 1057 if(num_share_modes == -1) { 1058 unlock_share_entry_fsp(fsp); 1059 fd_close(conn,fsp); 1060 file_free(fsp); 1061 return NULL; 1062 } 1063 1064 /* 1065 * If there are any share modes set then the file *did* 1066 * exist. Ensure we return the correct value for action. 1067 */ 1068 1069 if (num_share_modes > 0) 1070 file_existed = True; 1071 1072 /* 1073 * We exit this block with the share entry *locked*..... 1074 */ 1075 } 1076 1077 /* note that we ignore failure for the following. It is 1078 basically a hack for NFS, and NFS will never set one of 1079 these only read them. Nobody but Samba can ever set a deny 1080 mode and we have already checked our more authoritative 1081 locking database for permission to set this deny mode. If 1082 the kernel refuses the operations then the kernel is wrong */ 1083 kernel_flock(fsp, deny_mode); 1084 1085 /* 1086 * At this point onwards, we can guarentee that the share entry 1087 * is locked, whether we created the file or not, and that the 1088 * deny mode is compatible with all current opens. 1089 */ 1090 1091 /* 1092 * If requested, truncate the file. 1093 */ 1094 1095 if (flags2&O_TRUNC) { 1096 /* 1097 * We are modifing the file after open - update the stat struct.. 1098 */ 1099 if ((truncate_unless_locked(conn,fsp) == -1) || (SMB_VFS_FSTAT(fsp,fsp->fd,psbuf)==-1)) { 1100 unlock_share_entry_fsp(fsp); 1101 fd_close(conn,fsp); 1102 file_free(fsp); 1103 return NULL; 1104 } 1105 } 1106 1107 switch (flags) { 1108 case O_RDONLY: 1109 open_mode = DOS_OPEN_RDONLY; 1110 break; 1111 case O_RDWR: 1112 open_mode = DOS_OPEN_RDWR; 1113 break; 1114 case O_WRONLY: 1115 open_mode = DOS_OPEN_WRONLY; 1116 break; 1117 } 1118 1119 fsp->share_mode = SET_DENY_MODE(deny_mode) | 1120 SET_OPEN_MODE(open_mode) | 1121 SET_ALLOW_SHARE_DELETE(allow_share_delete); 1122 1123 DEBUG(10,("open_file_shared : share_mode = %x\n", fsp->share_mode )); 1124 1125 if (Access) 1126 (*Access) = open_mode; 1127 1128 if (action) { 1129 if (file_existed && !(flags2 & O_TRUNC)) 1130 *action = FILE_WAS_OPENED; 1131 if (!file_existed) 1132 *action = FILE_WAS_CREATED; 1133 if (file_existed && (flags2 & O_TRUNC)) 1134 *action = FILE_WAS_OVERWRITTEN; 1135 } 1136 1137 /* 1138 * Setup the oplock info in both the shared memory and 1139 * file structs. 1140 */ 1141 1142 if(oplock_request && (num_share_modes == 0) && 1143 !IS_VETO_OPLOCK_PATH(conn,fname) && set_file_oplock(fsp, oplock_request) ) { 1144 port = global_oplock_port; 1145 } else if (oplock_request && all_current_opens_are_level_II) { 1146 port = global_oplock_port; 1147 oplock_request = LEVEL_II_OPLOCK; 1148 set_file_oplock(fsp, oplock_request); 1149 } else { 1150 port = 0; 1151 oplock_request = 0; 1152 } 1153 1154 set_share_mode(fsp, port, oplock_request); 1155 1156 if (delete_on_close) { 1157 NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close); 1158 1159 if (NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_OK)) { 1160 /* Remember to delete the mode we just added. */ 1161 del_share_mode(fsp, NULL); 1162 unlock_share_entry_fsp(fsp); 1163 fd_close(conn,fsp); 1164 file_free(fsp); 1165 return NULL; 1166 } 1167 } 1168 1169 /* 1170 * Take care of inherited ACLs on created files - if default ACL not 1171 * selected. 1172 */ 1173 1174 if (!file_existed && !def_acl) { 1175 1176 int saved_errno = errno; /* We might get ENOSYS in the next call.. */ 1177 1178 if (SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, mode) == -1 && errno == ENOSYS) 1179 errno = saved_errno; /* Ignore ENOSYS */ 1180 1181 } else if (new_mode) { 1182 1183 int ret = -1; 1184 1185 /* Attributes need changing. File already existed. */ 1186 1187 { 1188 int saved_errno = errno; /* We might get ENOSYS in the next call.. */ 1189 ret = SMB_VFS_FCHMOD_ACL(fsp, fsp->fd, new_mode); 1190 1191 if (ret == -1 && errno == ENOSYS) { 1192 errno = saved_errno; /* Ignore ENOSYS */ 1193 } else { 1194 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n", 1195 fname, (int)new_mode)); 1196 ret = 0; /* Don't do the fchmod below. */ 1197 } 1198 } 1199 1200 if ((ret == -1) && (SMB_VFS_FCHMOD(fsp, fsp->fd, new_mode) == -1)) 1201 DEBUG(5, ("open_file_shared: failed to reset attributes of file %s to 0%o\n", 1202 fname, (int)new_mode)); 1203 } 1204 1205 unlock_share_entry_fsp(fsp); 1206 1207 conn->num_files_open++; 1208 1209 if (qosEnabled) 1210 { 1211 qos_increment_file_count(fsp->fsp_name, conn); 1212 } 1213 1214 return fsp; 1215} 1216 1217/**************************************************************************** 1218 Open a file for for write to ensure that we can fchmod it. 1219****************************************************************************/ 1220 1221files_struct *open_file_fchmod(connection_struct *conn, const char *fname, SMB_STRUCT_STAT *psbuf) 1222{ 1223 files_struct *fsp = NULL; 1224 BOOL fsp_open; 1225 1226 if (!VALID_STAT(*psbuf)) 1227 return NULL; 1228 1229 fsp = file_new(conn); 1230 if(!fsp) 1231 return NULL; 1232 1233 /* note! we must use a non-zero desired access or we don't get 1234 a real file descriptor. Oh what a twisted web we weave. */ 1235 fsp_open = open_file(fsp,conn,fname,psbuf,O_WRONLY,0,FILE_WRITE_DATA); 1236 1237 /* 1238 * This is not a user visible file open. 1239 * Don't set a share mode and don't increment 1240 * the conn->num_files_open. 1241 */ 1242 1243 if (!fsp_open) { 1244 file_free(fsp); 1245 return NULL; 1246 } 1247 1248 return fsp; 1249} 1250 1251/**************************************************************************** 1252 Close the fchmod file fd - ensure no locks are lost. 1253****************************************************************************/ 1254 1255int close_file_fchmod(files_struct *fsp) 1256{ 1257 int ret = fd_close(fsp->conn, fsp); 1258 file_free(fsp); 1259 return ret; 1260} 1261 1262/**************************************************************************** 1263 Open a directory from an NT SMB call. 1264****************************************************************************/ 1265 1266files_struct *open_directory(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf, 1267 uint32 desired_access, int share_mode, int smb_ofun, mode_t unixmode, int *action) 1268{ 1269 extern struct current_user current_user; 1270 BOOL got_stat = False; 1271 files_struct *fsp = file_new(conn); 1272 BOOL delete_on_close = GET_DELETE_ON_CLOSE_FLAG(share_mode); 1273 1274 if(!fsp) 1275 return NULL; 1276 1277 if (VALID_STAT(*psbuf)) 1278 got_stat = True; 1279 1280 if (got_stat && (GET_FILE_OPEN_DISPOSITION(smb_ofun) == FILE_EXISTS_FAIL)) { 1281 file_free(fsp); 1282 errno = EEXIST; /* Setup so correct error is returned to client. */ 1283 return NULL; 1284 } 1285 1286 if (GET_FILE_CREATE_DISPOSITION(smb_ofun) == FILE_CREATE_IF_NOT_EXIST) { 1287 1288 if (got_stat) { 1289 1290 if(!S_ISDIR(psbuf->st_mode)) { 1291 DEBUG(0,("open_directory: %s is not a directory !\n", fname )); 1292 file_free(fsp); 1293 errno = EACCES; 1294 return NULL; 1295 } 1296 *action = FILE_WAS_OPENED; 1297 1298 } else { 1299 1300 /* 1301 * Try and create the directory. 1302 */ 1303 1304 if(!CAN_WRITE(conn)) { 1305 DEBUG(2,("open_directory: failing create on read-only share\n")); 1306 file_free(fsp); 1307 errno = EACCES; 1308 return NULL; 1309 } 1310 1311 if (ms_has_wild(fname)) { 1312 file_free(fsp); 1313 DEBUG(5,("open_directory: failing create on filename %s with wildcards\n", fname)); 1314 unix_ERR_class = ERRDOS; 1315 unix_ERR_code = ERRinvalidname; 1316 unix_ERR_ntstatus = NT_STATUS_OBJECT_NAME_INVALID; 1317 return NULL; 1318 } 1319 1320 if(vfs_MkDir(conn,fname, unix_mode(conn,aDIR, fname)) < 0) { 1321 DEBUG(2,("open_directory: unable to create %s. Error was %s\n", 1322 fname, strerror(errno) )); 1323 file_free(fsp); 1324 return NULL; 1325 } 1326 1327 if(SMB_VFS_STAT(conn,fname, psbuf) != 0) { 1328 file_free(fsp); 1329 return NULL; 1330 } 1331 1332 *action = FILE_WAS_CREATED; 1333 1334 } 1335 } else { 1336 1337 /* 1338 * Don't create - just check that it *was* a directory. 1339 */ 1340 1341 if(!got_stat) { 1342 DEBUG(3,("open_directory: unable to stat name = %s. Error was %s\n", 1343 fname, strerror(errno) )); 1344 file_free(fsp); 1345 return NULL; 1346 } 1347 1348 if(!S_ISDIR(psbuf->st_mode)) { 1349 DEBUG(0,("open_directory: %s is not a directory !\n", fname )); 1350 file_free(fsp); 1351 return NULL; 1352 } 1353 1354 *action = FILE_WAS_OPENED; 1355 } 1356 1357 DEBUG(5,("open_directory: opening directory %s\n", fname)); 1358 1359 /* 1360 * Setup the files_struct for it. 1361 */ 1362 1363 fsp->mode = psbuf->st_mode; 1364 fsp->inode = psbuf->st_ino; 1365 fsp->dev = psbuf->st_dev; 1366 fsp->size = psbuf->st_size; 1367 fsp->vuid = current_user.vuid; 1368 fsp->file_pid = global_smbpid; 1369 fsp->can_lock = True; 1370 fsp->can_read = False; 1371 fsp->can_write = False; 1372 fsp->share_mode = share_mode; 1373 fsp->desired_access = desired_access; 1374 fsp->print_file = False; 1375 fsp->modified = False; 1376 fsp->oplock_type = NO_OPLOCK; 1377 fsp->sent_oplock_break = NO_BREAK_SENT; 1378 fsp->is_directory = True; 1379 fsp->is_stat = False; 1380 fsp->directory_delete_on_close = False; 1381 string_set(&fsp->fsp_name,fname); 1382 1383 if (delete_on_close) { 1384 NTSTATUS result = set_delete_on_close_internal(fsp, delete_on_close); 1385 1386 if (NT_STATUS_V(result) != NT_STATUS_V(NT_STATUS_OK)) { 1387 file_free(fsp); 1388 return NULL; 1389 } 1390 } 1391 conn->num_files_open++; 1392 1393 if (qosEnabled) 1394 { 1395 qos_increment_file_count(fsp->fsp_name, conn); 1396 } 1397 1398 return fsp; 1399} 1400 1401/**************************************************************************** 1402 Open a pseudo-file (no locking checks - a 'stat' open). 1403****************************************************************************/ 1404 1405files_struct *open_file_stat(connection_struct *conn, char *fname, SMB_STRUCT_STAT *psbuf) 1406{ 1407 extern struct current_user current_user; 1408 files_struct *fsp = NULL; 1409 1410 if (!VALID_STAT(*psbuf)) 1411 return NULL; 1412 1413 /* Can't 'stat' open directories. */ 1414 if(S_ISDIR(psbuf->st_mode)) 1415 return NULL; 1416 1417 fsp = file_new(conn); 1418 if(!fsp) 1419 return NULL; 1420 1421 DEBUG(5,("open_file_stat: 'opening' file %s\n", fname)); 1422 1423 /* 1424 * Setup the files_struct for it. 1425 */ 1426 1427 fsp->mode = psbuf->st_mode; 1428 /* 1429 * Don't store dev or inode, we don't want any iterator 1430 * to see this. 1431 */ 1432 fsp->inode = (SMB_INO_T)0; 1433 fsp->dev = (SMB_DEV_T)0; 1434 fsp->size = psbuf->st_size; 1435 fsp->vuid = current_user.vuid; 1436 fsp->file_pid = global_smbpid; 1437 fsp->can_lock = False; 1438 fsp->can_read = False; 1439 fsp->can_write = False; 1440 fsp->share_mode = 0; 1441 fsp->desired_access = 0; 1442 fsp->print_file = False; 1443 fsp->modified = False; 1444 fsp->oplock_type = NO_OPLOCK; 1445 fsp->sent_oplock_break = NO_BREAK_SENT; 1446 fsp->is_directory = False; 1447 fsp->is_stat = True; 1448 fsp->directory_delete_on_close = False; 1449 string_set(&fsp->fsp_name,fname); 1450 1451 conn->num_files_open++; 1452 1453 if (qosEnabled) 1454 { 1455 qos_increment_file_count(fsp->fsp_name, conn); 1456 } 1457 1458 return fsp; 1459} 1460