| 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-2004 | 
|---|
| 6 | Copyright (C) Volker Lendecke 2005 | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 11 | (at your option) any later version. | 
|---|
| 12 |  | 
|---|
| 13 | This program is distributed in the hope that it will be useful, | 
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | GNU General Public License for more details. | 
|---|
| 17 |  | 
|---|
| 18 | You should have received a copy of the GNU General Public License | 
|---|
| 19 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 20 | */ | 
|---|
| 21 |  | 
|---|
| 22 | #include "includes.h" | 
|---|
| 23 | #include "system/filesys.h" | 
|---|
| 24 | #include "printing.h" | 
|---|
| 25 | #include "smbd/smbd.h" | 
|---|
| 26 | #include "smbd/globals.h" | 
|---|
| 27 | #include "fake_file.h" | 
|---|
| 28 | #include "../libcli/security/security.h" | 
|---|
| 29 | #include "../librpc/gen_ndr/ndr_security.h" | 
|---|
| 30 | #include "auth.h" | 
|---|
| 31 | #include "messages.h" | 
|---|
| 32 |  | 
|---|
| 33 | extern const struct generic_mapping file_generic_mapping; | 
|---|
| 34 |  | 
|---|
| 35 | struct deferred_open_record { | 
|---|
| 36 | bool delayed_for_oplocks; | 
|---|
| 37 | struct file_id id; | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | /**************************************************************************** | 
|---|
| 41 | SMB1 file varient of se_access_check. Never test FILE_READ_ATTRIBUTES. | 
|---|
| 42 | ****************************************************************************/ | 
|---|
| 43 |  | 
|---|
| 44 | NTSTATUS smb1_file_se_access_check(struct connection_struct *conn, | 
|---|
| 45 | const struct security_descriptor *sd, | 
|---|
| 46 | const struct security_token *token, | 
|---|
| 47 | uint32_t access_desired, | 
|---|
| 48 | uint32_t *access_granted) | 
|---|
| 49 | { | 
|---|
| 50 | *access_granted = 0; | 
|---|
| 51 |  | 
|---|
| 52 | if (get_current_uid(conn) == (uid_t)0) { | 
|---|
| 53 | /* I'm sorry sir, I didn't know you were root... */ | 
|---|
| 54 | *access_granted = access_desired; | 
|---|
| 55 | if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) { | 
|---|
| 56 | *access_granted |= FILE_GENERIC_ALL; | 
|---|
| 57 | } | 
|---|
| 58 | return NT_STATUS_OK; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | return se_access_check(sd, | 
|---|
| 62 | token, | 
|---|
| 63 | (access_desired & ~FILE_READ_ATTRIBUTES), | 
|---|
| 64 | access_granted); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | /**************************************************************************** | 
|---|
| 68 | Check if we have open rights. | 
|---|
| 69 | ****************************************************************************/ | 
|---|
| 70 |  | 
|---|
| 71 | NTSTATUS smbd_check_open_rights(struct connection_struct *conn, | 
|---|
| 72 | const struct smb_filename *smb_fname, | 
|---|
| 73 | uint32_t access_mask, | 
|---|
| 74 | uint32_t *access_granted) | 
|---|
| 75 | { | 
|---|
| 76 | /* Check if we have rights to open. */ | 
|---|
| 77 | NTSTATUS status; | 
|---|
| 78 | struct security_descriptor *sd = NULL; | 
|---|
| 79 | uint32_t rejected_share_access; | 
|---|
| 80 |  | 
|---|
| 81 | rejected_share_access = access_mask & ~(conn->share_access); | 
|---|
| 82 |  | 
|---|
| 83 | if (rejected_share_access) { | 
|---|
| 84 | *access_granted = rejected_share_access; | 
|---|
| 85 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | if ((access_mask & DELETE_ACCESS) && !lp_acl_check_permissions(SNUM(conn))) { | 
|---|
| 89 | *access_granted = access_mask; | 
|---|
| 90 |  | 
|---|
| 91 | DEBUG(10,("smbd_check_open_rights: not checking ACL " | 
|---|
| 92 | "on DELETE_ACCESS on file %s. Granting 0x%x\n", | 
|---|
| 93 | smb_fname_str_dbg(smb_fname), | 
|---|
| 94 | (unsigned int)*access_granted )); | 
|---|
| 95 | return NT_STATUS_OK; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | if (access_mask == DELETE_ACCESS && | 
|---|
| 99 | VALID_STAT(smb_fname->st) && | 
|---|
| 100 | S_ISLNK(smb_fname->st.st_ex_mode)) { | 
|---|
| 101 | /* We can always delete a symlink. */ | 
|---|
| 102 | DEBUG(10,("smbd_check_open_rights: not checking ACL " | 
|---|
| 103 | "on DELETE_ACCESS on symlink %s.\n", | 
|---|
| 104 | smb_fname_str_dbg(smb_fname) )); | 
|---|
| 105 | return NT_STATUS_OK; | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name, | 
|---|
| 109 | (SECINFO_OWNER | | 
|---|
| 110 | SECINFO_GROUP | | 
|---|
| 111 | SECINFO_DACL),&sd); | 
|---|
| 112 |  | 
|---|
| 113 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 114 | DEBUG(10, ("smbd_check_open_rights: Could not get acl " | 
|---|
| 115 | "on %s: %s\n", | 
|---|
| 116 | smb_fname_str_dbg(smb_fname), | 
|---|
| 117 | nt_errstr(status))); | 
|---|
| 118 | return status; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | status = smb1_file_se_access_check(conn, | 
|---|
| 122 | sd, | 
|---|
| 123 | get_current_nttok(conn), | 
|---|
| 124 | access_mask, | 
|---|
| 125 | access_granted); | 
|---|
| 126 |  | 
|---|
| 127 | DEBUG(10,("smbd_check_open_rights: file %s requesting " | 
|---|
| 128 | "0x%x returning 0x%x (%s)\n", | 
|---|
| 129 | smb_fname_str_dbg(smb_fname), | 
|---|
| 130 | (unsigned int)access_mask, | 
|---|
| 131 | (unsigned int)*access_granted, | 
|---|
| 132 | nt_errstr(status) )); | 
|---|
| 133 |  | 
|---|
| 134 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 135 | if (DEBUGLEVEL >= 10) { | 
|---|
| 136 | DEBUG(10,("smbd_check_open_rights: acl for %s is:\n", | 
|---|
| 137 | smb_fname_str_dbg(smb_fname) )); | 
|---|
| 138 | NDR_PRINT_DEBUG(security_descriptor, sd); | 
|---|
| 139 | } | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | TALLOC_FREE(sd); | 
|---|
| 143 |  | 
|---|
| 144 | return status; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /**************************************************************************** | 
|---|
| 148 | fd support routines - attempt to do a dos_open. | 
|---|
| 149 | ****************************************************************************/ | 
|---|
| 150 |  | 
|---|
| 151 | static NTSTATUS fd_open(struct connection_struct *conn, | 
|---|
| 152 | files_struct *fsp, | 
|---|
| 153 | int flags, | 
|---|
| 154 | mode_t mode) | 
|---|
| 155 | { | 
|---|
| 156 | struct smb_filename *smb_fname = fsp->fsp_name; | 
|---|
| 157 | NTSTATUS status = NT_STATUS_OK; | 
|---|
| 158 |  | 
|---|
| 159 | #ifdef O_NOFOLLOW | 
|---|
| 160 | /* | 
|---|
| 161 | * Never follow symlinks on a POSIX client. The | 
|---|
| 162 | * client should be doing this. | 
|---|
| 163 | */ | 
|---|
| 164 |  | 
|---|
| 165 | if (fsp->posix_open || !lp_symlinks(SNUM(conn))) { | 
|---|
| 166 | flags |= O_NOFOLLOW; | 
|---|
| 167 | } | 
|---|
| 168 | #endif | 
|---|
| 169 |  | 
|---|
| 170 | fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, mode); | 
|---|
| 171 | if (fsp->fh->fd == -1) { | 
|---|
| 172 | status = map_nt_error_from_unix(errno); | 
|---|
| 173 | if (errno == EMFILE) { | 
|---|
| 174 | static time_t last_warned = 0L; | 
|---|
| 175 |  | 
|---|
| 176 | if (time((time_t *) NULL) > last_warned) { | 
|---|
| 177 | DEBUG(0,("Too many open files, unable " | 
|---|
| 178 | "to open more!  smbd's max " | 
|---|
| 179 | "open files = %d\n", | 
|---|
| 180 | lp_max_open_files())); | 
|---|
| 181 | last_warned = time((time_t *) NULL); | 
|---|
| 182 | } | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | DEBUG(10,("fd_open: name %s, flags = 0%o mode = 0%o, fd = %d. %s\n", | 
|---|
| 188 | smb_fname_str_dbg(smb_fname), flags, (int)mode, fsp->fh->fd, | 
|---|
| 189 | (fsp->fh->fd == -1) ? strerror(errno) : "" )); | 
|---|
| 190 |  | 
|---|
| 191 | return status; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | /**************************************************************************** | 
|---|
| 195 | Close the file associated with a fsp. | 
|---|
| 196 | ****************************************************************************/ | 
|---|
| 197 |  | 
|---|
| 198 | NTSTATUS fd_close(files_struct *fsp) | 
|---|
| 199 | { | 
|---|
| 200 | int ret; | 
|---|
| 201 |  | 
|---|
| 202 | if (fsp->dptr) { | 
|---|
| 203 | dptr_CloseDir(fsp); | 
|---|
| 204 | } | 
|---|
| 205 | if (fsp->fh->fd == -1) { | 
|---|
| 206 | return NT_STATUS_OK; /* What we used to call a stat open. */ | 
|---|
| 207 | } | 
|---|
| 208 | if (fsp->fh->ref_count > 1) { | 
|---|
| 209 | return NT_STATUS_OK; /* Shared handle. Only close last reference. */ | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | ret = SMB_VFS_CLOSE(fsp); | 
|---|
| 213 | fsp->fh->fd = -1; | 
|---|
| 214 | if (ret == -1) { | 
|---|
| 215 | return map_nt_error_from_unix(errno); | 
|---|
| 216 | } | 
|---|
| 217 | return NT_STATUS_OK; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | /**************************************************************************** | 
|---|
| 221 | Change the ownership of a file to that of the parent directory. | 
|---|
| 222 | Do this by fd if possible. | 
|---|
| 223 | ****************************************************************************/ | 
|---|
| 224 |  | 
|---|
| 225 | void change_file_owner_to_parent(connection_struct *conn, | 
|---|
| 226 | const char *inherit_from_dir, | 
|---|
| 227 | files_struct *fsp) | 
|---|
| 228 | { | 
|---|
| 229 | struct smb_filename *smb_fname_parent = NULL; | 
|---|
| 230 | NTSTATUS status; | 
|---|
| 231 | int ret; | 
|---|
| 232 |  | 
|---|
| 233 | status = create_synthetic_smb_fname(talloc_tos(), inherit_from_dir, | 
|---|
| 234 | NULL, NULL, &smb_fname_parent); | 
|---|
| 235 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 236 | return; | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | ret = SMB_VFS_STAT(conn, smb_fname_parent); | 
|---|
| 240 | if (ret == -1) { | 
|---|
| 241 | DEBUG(0,("change_file_owner_to_parent: failed to stat parent " | 
|---|
| 242 | "directory %s. Error was %s\n", | 
|---|
| 243 | smb_fname_str_dbg(smb_fname_parent), | 
|---|
| 244 | strerror(errno))); | 
|---|
| 245 | TALLOC_FREE(smb_fname_parent); | 
|---|
| 246 | return; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | if (smb_fname_parent->st.st_ex_uid == fsp->fsp_name->st.st_ex_uid) { | 
|---|
| 250 | /* Already this uid - no need to change. */ | 
|---|
| 251 | DEBUG(10,("change_file_owner_to_parent: file %s " | 
|---|
| 252 | "is already owned by uid %d\n", | 
|---|
| 253 | fsp_str_dbg(fsp), | 
|---|
| 254 | (int)fsp->fsp_name->st.st_ex_uid )); | 
|---|
| 255 | TALLOC_FREE(smb_fname_parent); | 
|---|
| 256 | return; | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | become_root(); | 
|---|
| 260 | ret = SMB_VFS_FCHOWN(fsp, smb_fname_parent->st.st_ex_uid, (gid_t)-1); | 
|---|
| 261 | unbecome_root(); | 
|---|
| 262 | if (ret == -1) { | 
|---|
| 263 | DEBUG(0,("change_file_owner_to_parent: failed to fchown " | 
|---|
| 264 | "file %s to parent directory uid %u. Error " | 
|---|
| 265 | "was %s\n", fsp_str_dbg(fsp), | 
|---|
| 266 | (unsigned int)smb_fname_parent->st.st_ex_uid, | 
|---|
| 267 | strerror(errno) )); | 
|---|
| 268 | } else { | 
|---|
| 269 | DEBUG(10,("change_file_owner_to_parent: changed new file %s to " | 
|---|
| 270 | "parent directory uid %u.\n", fsp_str_dbg(fsp), | 
|---|
| 271 | (unsigned int)smb_fname_parent->st.st_ex_uid)); | 
|---|
| 272 | /* Ensure the uid entry is updated. */ | 
|---|
| 273 | fsp->fsp_name->st.st_ex_uid = smb_fname_parent->st.st_ex_uid; | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | TALLOC_FREE(smb_fname_parent); | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | NTSTATUS change_dir_owner_to_parent(connection_struct *conn, | 
|---|
| 280 | const char *inherit_from_dir, | 
|---|
| 281 | const char *fname, | 
|---|
| 282 | SMB_STRUCT_STAT *psbuf) | 
|---|
| 283 | { | 
|---|
| 284 | struct smb_filename *smb_fname_parent = NULL; | 
|---|
| 285 | struct smb_filename *smb_fname_cwd = NULL; | 
|---|
| 286 | char *saved_dir = NULL; | 
|---|
| 287 | TALLOC_CTX *ctx = talloc_tos(); | 
|---|
| 288 | NTSTATUS status = NT_STATUS_OK; | 
|---|
| 289 | int ret; | 
|---|
| 290 |  | 
|---|
| 291 | status = create_synthetic_smb_fname(ctx, inherit_from_dir, NULL, NULL, | 
|---|
| 292 | &smb_fname_parent); | 
|---|
| 293 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 294 | return status; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | ret = SMB_VFS_STAT(conn, smb_fname_parent); | 
|---|
| 298 | if (ret == -1) { | 
|---|
| 299 | status = map_nt_error_from_unix(errno); | 
|---|
| 300 | DEBUG(0,("change_dir_owner_to_parent: failed to stat parent " | 
|---|
| 301 | "directory %s. Error was %s\n", | 
|---|
| 302 | smb_fname_str_dbg(smb_fname_parent), | 
|---|
| 303 | strerror(errno))); | 
|---|
| 304 | goto out; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | /* We've already done an lstat into psbuf, and we know it's a | 
|---|
| 308 | directory. If we can cd into the directory and the dev/ino | 
|---|
| 309 | are the same then we can safely chown without races as | 
|---|
| 310 | we're locking the directory in place by being in it.  This | 
|---|
| 311 | should work on any UNIX (thanks tridge :-). JRA. | 
|---|
| 312 | */ | 
|---|
| 313 |  | 
|---|
| 314 | saved_dir = vfs_GetWd(ctx,conn); | 
|---|
| 315 | if (!saved_dir) { | 
|---|
| 316 | status = map_nt_error_from_unix(errno); | 
|---|
| 317 | DEBUG(0,("change_dir_owner_to_parent: failed to get " | 
|---|
| 318 | "current working directory. Error was %s\n", | 
|---|
| 319 | strerror(errno))); | 
|---|
| 320 | goto out; | 
|---|
| 321 | } | 
|---|
| 322 |  | 
|---|
| 323 | /* Chdir into the new path. */ | 
|---|
| 324 | if (vfs_ChDir(conn, fname) == -1) { | 
|---|
| 325 | status = map_nt_error_from_unix(errno); | 
|---|
| 326 | DEBUG(0,("change_dir_owner_to_parent: failed to change " | 
|---|
| 327 | "current working directory to %s. Error " | 
|---|
| 328 | "was %s\n", fname, strerror(errno) )); | 
|---|
| 329 | goto chdir; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | status = create_synthetic_smb_fname(ctx, ".", NULL, NULL, | 
|---|
| 333 | &smb_fname_cwd); | 
|---|
| 334 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 335 | return status; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | ret = SMB_VFS_STAT(conn, smb_fname_cwd); | 
|---|
| 339 | if (ret == -1) { | 
|---|
| 340 | status = map_nt_error_from_unix(errno); | 
|---|
| 341 | DEBUG(0,("change_dir_owner_to_parent: failed to stat " | 
|---|
| 342 | "directory '.' (%s) Error was %s\n", | 
|---|
| 343 | fname, strerror(errno))); | 
|---|
| 344 | goto chdir; | 
|---|
| 345 | } | 
|---|
| 346 |  | 
|---|
| 347 | /* Ensure we're pointing at the same place. */ | 
|---|
| 348 | if (smb_fname_cwd->st.st_ex_dev != psbuf->st_ex_dev || | 
|---|
| 349 | smb_fname_cwd->st.st_ex_ino != psbuf->st_ex_ino) { | 
|---|
| 350 | DEBUG(0,("change_dir_owner_to_parent: " | 
|---|
| 351 | "device/inode on directory %s changed. " | 
|---|
| 352 | "Refusing to chown !\n", fname )); | 
|---|
| 353 | status = NT_STATUS_ACCESS_DENIED; | 
|---|
| 354 | goto chdir; | 
|---|
| 355 | } | 
|---|
| 356 |  | 
|---|
| 357 | if (smb_fname_parent->st.st_ex_uid == smb_fname_cwd->st.st_ex_uid) { | 
|---|
| 358 | /* Already this uid - no need to change. */ | 
|---|
| 359 | DEBUG(10,("change_dir_owner_to_parent: directory %s " | 
|---|
| 360 | "is already owned by uid %d\n", | 
|---|
| 361 | fname, | 
|---|
| 362 | (int)smb_fname_cwd->st.st_ex_uid )); | 
|---|
| 363 | status = NT_STATUS_OK; | 
|---|
| 364 | goto chdir; | 
|---|
| 365 | } | 
|---|
| 366 |  | 
|---|
| 367 | become_root(); | 
|---|
| 368 | ret = SMB_VFS_LCHOWN(conn, ".", smb_fname_parent->st.st_ex_uid, | 
|---|
| 369 | (gid_t)-1); | 
|---|
| 370 | unbecome_root(); | 
|---|
| 371 | if (ret == -1) { | 
|---|
| 372 | status = map_nt_error_from_unix(errno); | 
|---|
| 373 | DEBUG(10,("change_dir_owner_to_parent: failed to chown " | 
|---|
| 374 | "directory %s to parent directory uid %u. " | 
|---|
| 375 | "Error was %s\n", fname, | 
|---|
| 376 | (unsigned int)smb_fname_parent->st.st_ex_uid, | 
|---|
| 377 | strerror(errno) )); | 
|---|
| 378 | goto chdir; | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | DEBUG(10,("change_dir_owner_to_parent: changed ownership of new " | 
|---|
| 382 | "directory %s to parent directory uid %u.\n", | 
|---|
| 383 | fname, (unsigned int)smb_fname_parent->st.st_ex_uid )); | 
|---|
| 384 |  | 
|---|
| 385 | chdir: | 
|---|
| 386 | vfs_ChDir(conn,saved_dir); | 
|---|
| 387 | out: | 
|---|
| 388 | TALLOC_FREE(smb_fname_parent); | 
|---|
| 389 | TALLOC_FREE(smb_fname_cwd); | 
|---|
| 390 | return status; | 
|---|
| 391 | } | 
|---|
| 392 |  | 
|---|
| 393 | /**************************************************************************** | 
|---|
| 394 | Open a file. | 
|---|
| 395 | ****************************************************************************/ | 
|---|
| 396 |  | 
|---|
| 397 | static NTSTATUS open_file(files_struct *fsp, | 
|---|
| 398 | connection_struct *conn, | 
|---|
| 399 | struct smb_request *req, | 
|---|
| 400 | const char *parent_dir, | 
|---|
| 401 | int flags, | 
|---|
| 402 | mode_t unx_mode, | 
|---|
| 403 | uint32 access_mask, /* client requested access mask. */ | 
|---|
| 404 | uint32 open_access_mask) /* what we're actually using in the open. */ | 
|---|
| 405 | { | 
|---|
| 406 | struct smb_filename *smb_fname = fsp->fsp_name; | 
|---|
| 407 | NTSTATUS status = NT_STATUS_OK; | 
|---|
| 408 | int accmode = (flags & O_ACCMODE); | 
|---|
| 409 | int local_flags = flags; | 
|---|
| 410 | bool file_existed = VALID_STAT(fsp->fsp_name->st); | 
|---|
| 411 | bool file_created = false; | 
|---|
| 412 |  | 
|---|
| 413 | fsp->fh->fd = -1; | 
|---|
| 414 | errno = EPERM; | 
|---|
| 415 |  | 
|---|
| 416 | /* Check permissions */ | 
|---|
| 417 |  | 
|---|
| 418 | /* | 
|---|
| 419 | * This code was changed after seeing a client open request | 
|---|
| 420 | * containing the open mode of (DENY_WRITE/read-only) with | 
|---|
| 421 | * the 'create if not exist' bit set. The previous code | 
|---|
| 422 | * would fail to open the file read only on a read-only share | 
|---|
| 423 | * as it was checking the flags parameter  directly against O_RDONLY, | 
|---|
| 424 | * this was failing as the flags parameter was set to O_RDONLY|O_CREAT. | 
|---|
| 425 | * JRA. | 
|---|
| 426 | */ | 
|---|
| 427 |  | 
|---|
| 428 | if (!CAN_WRITE(conn)) { | 
|---|
| 429 | /* It's a read-only share - fail if we wanted to write. */ | 
|---|
| 430 | if(accmode != O_RDONLY || (flags & O_TRUNC) || (flags & O_APPEND)) { | 
|---|
| 431 | DEBUG(3,("Permission denied opening %s\n", | 
|---|
| 432 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 433 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 434 | } else if(flags & O_CREAT) { | 
|---|
| 435 | /* We don't want to write - but we must make sure that | 
|---|
| 436 | O_CREAT doesn't create the file if we have write | 
|---|
| 437 | access into the directory. | 
|---|
| 438 | */ | 
|---|
| 439 | flags &= ~(O_CREAT|O_EXCL); | 
|---|
| 440 | local_flags &= ~(O_CREAT|O_EXCL); | 
|---|
| 441 | } | 
|---|
| 442 | } | 
|---|
| 443 |  | 
|---|
| 444 | /* | 
|---|
| 445 | * This little piece of insanity is inspired by the | 
|---|
| 446 | * fact that an NT client can open a file for O_RDONLY, | 
|---|
| 447 | * but set the create disposition to FILE_EXISTS_TRUNCATE. | 
|---|
| 448 | * If the client *can* write to the file, then it expects to | 
|---|
| 449 | * truncate the file, even though it is opening for readonly. | 
|---|
| 450 | * Quicken uses this stupid trick in backup file creation... | 
|---|
| 451 | * Thanks *greatly* to "David W. Chapman Jr." <dwcjr@inethouston.net> | 
|---|
| 452 | * for helping track this one down. It didn't bite us in 2.0.x | 
|---|
| 453 | * as we always opened files read-write in that release. JRA. | 
|---|
| 454 | */ | 
|---|
| 455 |  | 
|---|
| 456 | if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) { | 
|---|
| 457 | DEBUG(10,("open_file: truncate requested on read-only open " | 
|---|
| 458 | "for file %s\n", smb_fname_str_dbg(smb_fname))); | 
|---|
| 459 | local_flags = (flags & ~O_ACCMODE)|O_RDWR; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 | if ((open_access_mask & (FILE_READ_DATA|FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_EXECUTE)) || | 
|---|
| 463 | (!file_existed && (local_flags & O_CREAT)) || | 
|---|
| 464 | ((local_flags & O_TRUNC) == O_TRUNC) ) { | 
|---|
| 465 | const char *wild; | 
|---|
| 466 |  | 
|---|
| 467 | /* | 
|---|
| 468 | * We can't actually truncate here as the file may be locked. | 
|---|
| 469 | * open_file_ntcreate will take care of the truncate later. JRA. | 
|---|
| 470 | */ | 
|---|
| 471 |  | 
|---|
| 472 | local_flags &= ~O_TRUNC; | 
|---|
| 473 |  | 
|---|
| 474 | #if defined(O_NONBLOCK) && defined(S_ISFIFO) | 
|---|
| 475 | /* | 
|---|
| 476 | * We would block on opening a FIFO with no one else on the | 
|---|
| 477 | * other end. Do what we used to do and add O_NONBLOCK to the | 
|---|
| 478 | * open flags. JRA. | 
|---|
| 479 | */ | 
|---|
| 480 |  | 
|---|
| 481 | if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) { | 
|---|
| 482 | local_flags |= O_NONBLOCK; | 
|---|
| 483 | } | 
|---|
| 484 | #endif | 
|---|
| 485 |  | 
|---|
| 486 | /* Don't create files with Microsoft wildcard characters. */ | 
|---|
| 487 | if (fsp->base_fsp) { | 
|---|
| 488 | /* | 
|---|
| 489 | * wildcard characters are allowed in stream names | 
|---|
| 490 | * only test the basefilename | 
|---|
| 491 | */ | 
|---|
| 492 | wild = fsp->base_fsp->fsp_name->base_name; | 
|---|
| 493 | } else { | 
|---|
| 494 | wild = smb_fname->base_name; | 
|---|
| 495 | } | 
|---|
| 496 | if ((local_flags & O_CREAT) && !file_existed && | 
|---|
| 497 | ms_has_wild(wild))  { | 
|---|
| 498 | return NT_STATUS_OBJECT_NAME_INVALID; | 
|---|
| 499 | } | 
|---|
| 500 |  | 
|---|
| 501 | /* Actually do the open */ | 
|---|
| 502 | status = fd_open(conn, fsp, local_flags, unx_mode); | 
|---|
| 503 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 504 | DEBUG(3,("Error opening file %s (%s) (local_flags=%d) " | 
|---|
| 505 | "(flags=%d)\n", smb_fname_str_dbg(smb_fname), | 
|---|
| 506 | nt_errstr(status),local_flags,flags)); | 
|---|
| 507 | return status; | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | if ((local_flags & O_CREAT) && !file_existed) { | 
|---|
| 511 | file_created = true; | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | } else { | 
|---|
| 515 | fsp->fh->fd = -1; /* What we used to call a stat open. */ | 
|---|
| 516 | if (file_existed) { | 
|---|
| 517 | uint32_t access_granted = 0; | 
|---|
| 518 |  | 
|---|
| 519 | status = smbd_check_open_rights(conn, | 
|---|
| 520 | smb_fname, | 
|---|
| 521 | access_mask, | 
|---|
| 522 | &access_granted); | 
|---|
| 523 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 524 | if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) { | 
|---|
| 525 | /* | 
|---|
| 526 | * On NT_STATUS_ACCESS_DENIED, access_granted | 
|---|
| 527 | * contains the denied bits. | 
|---|
| 528 | */ | 
|---|
| 529 |  | 
|---|
| 530 | if ((access_mask & FILE_WRITE_ATTRIBUTES) && | 
|---|
| 531 | (access_granted & FILE_WRITE_ATTRIBUTES) && | 
|---|
| 532 | (lp_map_readonly(SNUM(conn)) || | 
|---|
| 533 | lp_map_archive(SNUM(conn)) || | 
|---|
| 534 | lp_map_hidden(SNUM(conn)) || | 
|---|
| 535 | lp_map_system(SNUM(conn)))) { | 
|---|
| 536 | access_granted &= ~FILE_WRITE_ATTRIBUTES; | 
|---|
| 537 |  | 
|---|
| 538 | DEBUG(10,("open_file: " | 
|---|
| 539 | "overrode " | 
|---|
| 540 | "FILE_WRITE_" | 
|---|
| 541 | "ATTRIBUTES " | 
|---|
| 542 | "on file %s\n", | 
|---|
| 543 | smb_fname_str_dbg( | 
|---|
| 544 | smb_fname))); | 
|---|
| 545 | } | 
|---|
| 546 |  | 
|---|
| 547 | if ((access_mask & DELETE_ACCESS) && | 
|---|
| 548 | (access_granted & DELETE_ACCESS) && | 
|---|
| 549 | can_delete_file_in_directory(conn, | 
|---|
| 550 | smb_fname)) { | 
|---|
| 551 | /* Were we trying to do a stat open | 
|---|
| 552 | * for delete and didn't get DELETE | 
|---|
| 553 | * access (only) ? Check if the | 
|---|
| 554 | * directory allows DELETE_CHILD. | 
|---|
| 555 | * See here: | 
|---|
| 556 | * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx | 
|---|
| 557 | * for details. */ | 
|---|
| 558 |  | 
|---|
| 559 | access_granted &= ~DELETE_ACCESS; | 
|---|
| 560 |  | 
|---|
| 561 | DEBUG(10,("open_file: " | 
|---|
| 562 | "overrode " | 
|---|
| 563 | "DELETE_ACCESS on " | 
|---|
| 564 | "file %s\n", | 
|---|
| 565 | smb_fname_str_dbg( | 
|---|
| 566 | smb_fname))); | 
|---|
| 567 | } | 
|---|
| 568 |  | 
|---|
| 569 | if (access_granted != 0) { | 
|---|
| 570 | DEBUG(10,("open_file: Access " | 
|---|
| 571 | "denied on file " | 
|---|
| 572 | "%s\n", | 
|---|
| 573 | smb_fname_str_dbg( | 
|---|
| 574 | smb_fname))); | 
|---|
| 575 | return status; | 
|---|
| 576 | } | 
|---|
| 577 | } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) && | 
|---|
| 578 | fsp->posix_open && | 
|---|
| 579 | S_ISLNK(smb_fname->st.st_ex_mode)) { | 
|---|
| 580 | /* This is a POSIX stat open for delete | 
|---|
| 581 | * or rename on a symlink that points | 
|---|
| 582 | * nowhere. Allow. */ | 
|---|
| 583 | DEBUG(10,("open_file: allowing POSIX " | 
|---|
| 584 | "open on bad symlink %s\n", | 
|---|
| 585 | smb_fname_str_dbg( | 
|---|
| 586 | smb_fname))); | 
|---|
| 587 | } else { | 
|---|
| 588 | DEBUG(10,("open_file: " | 
|---|
| 589 | "smbd_check_open_rights on file " | 
|---|
| 590 | "%s returned %s\n", | 
|---|
| 591 | smb_fname_str_dbg(smb_fname), | 
|---|
| 592 | nt_errstr(status) )); | 
|---|
| 593 | return status; | 
|---|
| 594 | } | 
|---|
| 595 | } | 
|---|
| 596 | } | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | if (!file_existed) { | 
|---|
| 600 | int ret; | 
|---|
| 601 |  | 
|---|
| 602 | if (fsp->fh->fd == -1) { | 
|---|
| 603 | ret = SMB_VFS_STAT(conn, smb_fname); | 
|---|
| 604 | } else { | 
|---|
| 605 | ret = SMB_VFS_FSTAT(fsp, &smb_fname->st); | 
|---|
| 606 | /* If we have an fd, this stat should succeed. */ | 
|---|
| 607 | if (ret == -1) { | 
|---|
| 608 | DEBUG(0,("Error doing fstat on open file %s " | 
|---|
| 609 | "(%s)\n", | 
|---|
| 610 | smb_fname_str_dbg(smb_fname), | 
|---|
| 611 | strerror(errno) )); | 
|---|
| 612 | } | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | /* For a non-io open, this stat failing means file not found. JRA */ | 
|---|
| 616 | if (ret == -1) { | 
|---|
| 617 | status = map_nt_error_from_unix(errno); | 
|---|
| 618 | fd_close(fsp); | 
|---|
| 619 | return status; | 
|---|
| 620 | } | 
|---|
| 621 |  | 
|---|
| 622 | if (file_created) { | 
|---|
| 623 | bool need_re_stat = false; | 
|---|
| 624 | /* Do all inheritance work after we've | 
|---|
| 625 | done a successful stat call and filled | 
|---|
| 626 | in the stat struct in fsp->fsp_name. */ | 
|---|
| 627 |  | 
|---|
| 628 | /* Inherit the ACL if required */ | 
|---|
| 629 | if (lp_inherit_perms(SNUM(conn))) { | 
|---|
| 630 | inherit_access_posix_acl(conn, parent_dir, | 
|---|
| 631 | smb_fname->base_name, | 
|---|
| 632 | unx_mode); | 
|---|
| 633 | need_re_stat = true; | 
|---|
| 634 | } | 
|---|
| 635 |  | 
|---|
| 636 | /* Change the owner if required. */ | 
|---|
| 637 | if (lp_inherit_owner(SNUM(conn))) { | 
|---|
| 638 | change_file_owner_to_parent(conn, parent_dir, | 
|---|
| 639 | fsp); | 
|---|
| 640 | need_re_stat = true; | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | if (need_re_stat) { | 
|---|
| 644 | if (fsp->fh->fd == -1) { | 
|---|
| 645 | ret = SMB_VFS_STAT(conn, smb_fname); | 
|---|
| 646 | } else { | 
|---|
| 647 | ret = SMB_VFS_FSTAT(fsp, &smb_fname->st); | 
|---|
| 648 | /* If we have an fd, this stat should succeed. */ | 
|---|
| 649 | if (ret == -1) { | 
|---|
| 650 | DEBUG(0,("Error doing fstat on open file %s " | 
|---|
| 651 | "(%s)\n", | 
|---|
| 652 | smb_fname_str_dbg(smb_fname), | 
|---|
| 653 | strerror(errno) )); | 
|---|
| 654 | } | 
|---|
| 655 | } | 
|---|
| 656 | } | 
|---|
| 657 |  | 
|---|
| 658 | notify_fname(conn, NOTIFY_ACTION_ADDED, | 
|---|
| 659 | FILE_NOTIFY_CHANGE_FILE_NAME, | 
|---|
| 660 | smb_fname->base_name); | 
|---|
| 661 | } | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | /* | 
|---|
| 665 | * POSIX allows read-only opens of directories. We don't | 
|---|
| 666 | * want to do this (we use a different code path for this) | 
|---|
| 667 | * so catch a directory open and return an EISDIR. JRA. | 
|---|
| 668 | */ | 
|---|
| 669 |  | 
|---|
| 670 | if(S_ISDIR(smb_fname->st.st_ex_mode)) { | 
|---|
| 671 | fd_close(fsp); | 
|---|
| 672 | errno = EISDIR; | 
|---|
| 673 | return NT_STATUS_FILE_IS_A_DIRECTORY; | 
|---|
| 674 | } | 
|---|
| 675 |  | 
|---|
| 676 | fsp->mode = smb_fname->st.st_ex_mode; | 
|---|
| 677 | fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st); | 
|---|
| 678 | fsp->vuid = req ? req->vuid : UID_FIELD_INVALID; | 
|---|
| 679 | fsp->file_pid = req ? req->smbpid : 0; | 
|---|
| 680 | fsp->can_lock = True; | 
|---|
| 681 | fsp->can_read = (access_mask & (FILE_READ_DATA)) ? True : False; | 
|---|
| 682 | if (!CAN_WRITE(conn)) { | 
|---|
| 683 | fsp->can_write = False; | 
|---|
| 684 | } else { | 
|---|
| 685 | fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ? | 
|---|
| 686 | True : False; | 
|---|
| 687 | } | 
|---|
| 688 | fsp->print_file = NULL; | 
|---|
| 689 | fsp->modified = False; | 
|---|
| 690 | fsp->sent_oplock_break = NO_BREAK_SENT; | 
|---|
| 691 | fsp->is_directory = False; | 
|---|
| 692 | if (conn->aio_write_behind_list && | 
|---|
| 693 | is_in_path(smb_fname->base_name, conn->aio_write_behind_list, | 
|---|
| 694 | conn->case_sensitive)) { | 
|---|
| 695 | fsp->aio_write_behind = True; | 
|---|
| 696 | } | 
|---|
| 697 |  | 
|---|
| 698 | fsp->wcp = NULL; /* Write cache pointer. */ | 
|---|
| 699 |  | 
|---|
| 700 | DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n", | 
|---|
| 701 | conn->session_info->unix_name, | 
|---|
| 702 | smb_fname_str_dbg(smb_fname), | 
|---|
| 703 | BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write), | 
|---|
| 704 | conn->num_files_open)); | 
|---|
| 705 |  | 
|---|
| 706 | errno = 0; | 
|---|
| 707 | return NT_STATUS_OK; | 
|---|
| 708 | } | 
|---|
| 709 |  | 
|---|
| 710 | /**************************************************************************** | 
|---|
| 711 | Check if we can open a file with a share mode. | 
|---|
| 712 | Returns True if conflict, False if not. | 
|---|
| 713 | ****************************************************************************/ | 
|---|
| 714 |  | 
|---|
| 715 | static bool share_conflict(struct share_mode_entry *entry, | 
|---|
| 716 | uint32 access_mask, | 
|---|
| 717 | uint32 share_access) | 
|---|
| 718 | { | 
|---|
| 719 | DEBUG(10,("share_conflict: entry->access_mask = 0x%x, " | 
|---|
| 720 | "entry->share_access = 0x%x, " | 
|---|
| 721 | "entry->private_options = 0x%x\n", | 
|---|
| 722 | (unsigned int)entry->access_mask, | 
|---|
| 723 | (unsigned int)entry->share_access, | 
|---|
| 724 | (unsigned int)entry->private_options)); | 
|---|
| 725 |  | 
|---|
| 726 | DEBUG(10,("share_conflict: access_mask = 0x%x, share_access = 0x%x\n", | 
|---|
| 727 | (unsigned int)access_mask, (unsigned int)share_access)); | 
|---|
| 728 |  | 
|---|
| 729 | if ((entry->access_mask & (FILE_WRITE_DATA| | 
|---|
| 730 | FILE_APPEND_DATA| | 
|---|
| 731 | FILE_READ_DATA| | 
|---|
| 732 | FILE_EXECUTE| | 
|---|
| 733 | DELETE_ACCESS)) == 0) { | 
|---|
| 734 | DEBUG(10,("share_conflict: No conflict due to " | 
|---|
| 735 | "entry->access_mask = 0x%x\n", | 
|---|
| 736 | (unsigned int)entry->access_mask )); | 
|---|
| 737 | return False; | 
|---|
| 738 | } | 
|---|
| 739 |  | 
|---|
| 740 | if ((access_mask & (FILE_WRITE_DATA| | 
|---|
| 741 | FILE_APPEND_DATA| | 
|---|
| 742 | FILE_READ_DATA| | 
|---|
| 743 | FILE_EXECUTE| | 
|---|
| 744 | DELETE_ACCESS)) == 0) { | 
|---|
| 745 | DEBUG(10,("share_conflict: No conflict due to " | 
|---|
| 746 | "access_mask = 0x%x\n", | 
|---|
| 747 | (unsigned int)access_mask )); | 
|---|
| 748 | return False; | 
|---|
| 749 | } | 
|---|
| 750 |  | 
|---|
| 751 | #if 1 /* JRA TEST - Superdebug. */ | 
|---|
| 752 | #define CHECK_MASK(num, am, right, sa, share) \ | 
|---|
| 753 | DEBUG(10,("share_conflict: [%d] am (0x%x) & right (0x%x) = 0x%x\n", \ | 
|---|
| 754 | (unsigned int)(num), (unsigned int)(am), \ | 
|---|
| 755 | (unsigned int)(right), (unsigned int)(am)&(right) )); \ | 
|---|
| 756 | DEBUG(10,("share_conflict: [%d] sa (0x%x) & share (0x%x) = 0x%x\n", \ | 
|---|
| 757 | (unsigned int)(num), (unsigned int)(sa), \ | 
|---|
| 758 | (unsigned int)(share), (unsigned int)(sa)&(share) )); \ | 
|---|
| 759 | if (((am) & (right)) && !((sa) & (share))) { \ | 
|---|
| 760 | DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \ | 
|---|
| 761 | sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \ | 
|---|
| 762 | (unsigned int)(share) )); \ | 
|---|
| 763 | return True; \ | 
|---|
| 764 | } | 
|---|
| 765 | #else | 
|---|
| 766 | #define CHECK_MASK(num, am, right, sa, share) \ | 
|---|
| 767 | if (((am) & (right)) && !((sa) & (share))) { \ | 
|---|
| 768 | DEBUG(10,("share_conflict: check %d conflict am = 0x%x, right = 0x%x, \ | 
|---|
| 769 | sa = 0x%x, share = 0x%x\n", (num), (unsigned int)(am), (unsigned int)(right), (unsigned int)(sa), \ | 
|---|
| 770 | (unsigned int)(share) )); \ | 
|---|
| 771 | return True; \ | 
|---|
| 772 | } | 
|---|
| 773 | #endif | 
|---|
| 774 |  | 
|---|
| 775 | CHECK_MASK(1, entry->access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA, | 
|---|
| 776 | share_access, FILE_SHARE_WRITE); | 
|---|
| 777 | CHECK_MASK(2, access_mask, FILE_WRITE_DATA | FILE_APPEND_DATA, | 
|---|
| 778 | entry->share_access, FILE_SHARE_WRITE); | 
|---|
| 779 |  | 
|---|
| 780 | CHECK_MASK(3, entry->access_mask, FILE_READ_DATA | FILE_EXECUTE, | 
|---|
| 781 | share_access, FILE_SHARE_READ); | 
|---|
| 782 | CHECK_MASK(4, access_mask, FILE_READ_DATA | FILE_EXECUTE, | 
|---|
| 783 | entry->share_access, FILE_SHARE_READ); | 
|---|
| 784 |  | 
|---|
| 785 | CHECK_MASK(5, entry->access_mask, DELETE_ACCESS, | 
|---|
| 786 | share_access, FILE_SHARE_DELETE); | 
|---|
| 787 | CHECK_MASK(6, access_mask, DELETE_ACCESS, | 
|---|
| 788 | entry->share_access, FILE_SHARE_DELETE); | 
|---|
| 789 |  | 
|---|
| 790 | DEBUG(10,("share_conflict: No conflict.\n")); | 
|---|
| 791 | return False; | 
|---|
| 792 | } | 
|---|
| 793 |  | 
|---|
| 794 | #if defined(DEVELOPER) | 
|---|
| 795 | static void validate_my_share_entries(struct smbd_server_connection *sconn, | 
|---|
| 796 | int num, | 
|---|
| 797 | struct share_mode_entry *share_entry) | 
|---|
| 798 | { | 
|---|
| 799 | files_struct *fsp; | 
|---|
| 800 |  | 
|---|
| 801 | if (!procid_is_me(&share_entry->pid)) { | 
|---|
| 802 | return; | 
|---|
| 803 | } | 
|---|
| 804 |  | 
|---|
| 805 | if (is_deferred_open_entry(share_entry) && | 
|---|
| 806 | !open_was_deferred(share_entry->op_mid)) { | 
|---|
| 807 | char *str = talloc_asprintf(talloc_tos(), | 
|---|
| 808 | "Got a deferred entry without a request: " | 
|---|
| 809 | "PANIC: %s\n", | 
|---|
| 810 | share_mode_str(talloc_tos(), num, share_entry)); | 
|---|
| 811 | smb_panic(str); | 
|---|
| 812 | } | 
|---|
| 813 |  | 
|---|
| 814 | if (!is_valid_share_mode_entry(share_entry)) { | 
|---|
| 815 | return; | 
|---|
| 816 | } | 
|---|
| 817 |  | 
|---|
| 818 | fsp = file_find_dif(sconn, share_entry->id, | 
|---|
| 819 | share_entry->share_file_id); | 
|---|
| 820 | if (!fsp) { | 
|---|
| 821 | DEBUG(0,("validate_my_share_entries: PANIC : %s\n", | 
|---|
| 822 | share_mode_str(talloc_tos(), num, share_entry) )); | 
|---|
| 823 | smb_panic("validate_my_share_entries: Cannot match a " | 
|---|
| 824 | "share entry with an open file\n"); | 
|---|
| 825 | } | 
|---|
| 826 |  | 
|---|
| 827 | if (is_deferred_open_entry(share_entry) || | 
|---|
| 828 | is_unused_share_mode_entry(share_entry)) { | 
|---|
| 829 | goto panic; | 
|---|
| 830 | } | 
|---|
| 831 |  | 
|---|
| 832 | if ((share_entry->op_type == NO_OPLOCK) && | 
|---|
| 833 | (fsp->oplock_type == FAKE_LEVEL_II_OPLOCK)) { | 
|---|
| 834 | /* Someone has already written to it, but I haven't yet | 
|---|
| 835 | * noticed */ | 
|---|
| 836 | return; | 
|---|
| 837 | } | 
|---|
| 838 |  | 
|---|
| 839 | if (((uint16)fsp->oplock_type) != share_entry->op_type) { | 
|---|
| 840 | goto panic; | 
|---|
| 841 | } | 
|---|
| 842 |  | 
|---|
| 843 | return; | 
|---|
| 844 |  | 
|---|
| 845 | panic: | 
|---|
| 846 | { | 
|---|
| 847 | char *str; | 
|---|
| 848 | DEBUG(0,("validate_my_share_entries: PANIC : %s\n", | 
|---|
| 849 | share_mode_str(talloc_tos(), num, share_entry) )); | 
|---|
| 850 | str = talloc_asprintf(talloc_tos(), | 
|---|
| 851 | "validate_my_share_entries: " | 
|---|
| 852 | "file %s, oplock_type = 0x%x, op_type = 0x%x\n", | 
|---|
| 853 | fsp->fsp_name->base_name, | 
|---|
| 854 | (unsigned int)fsp->oplock_type, | 
|---|
| 855 | (unsigned int)share_entry->op_type ); | 
|---|
| 856 | smb_panic(str); | 
|---|
| 857 | } | 
|---|
| 858 | } | 
|---|
| 859 | #endif | 
|---|
| 860 |  | 
|---|
| 861 | bool is_stat_open(uint32 access_mask) | 
|---|
| 862 | { | 
|---|
| 863 | return (access_mask && | 
|---|
| 864 | ((access_mask & ~(SYNCHRONIZE_ACCESS| FILE_READ_ATTRIBUTES| | 
|---|
| 865 | FILE_WRITE_ATTRIBUTES))==0) && | 
|---|
| 866 | ((access_mask & (SYNCHRONIZE_ACCESS|FILE_READ_ATTRIBUTES| | 
|---|
| 867 | FILE_WRITE_ATTRIBUTES)) != 0)); | 
|---|
| 868 | } | 
|---|
| 869 |  | 
|---|
| 870 | /**************************************************************************** | 
|---|
| 871 | Deal with share modes | 
|---|
| 872 | Invarient: Share mode must be locked on entry and exit. | 
|---|
| 873 | Returns -1 on error, or number of share modes on success (may be zero). | 
|---|
| 874 | ****************************************************************************/ | 
|---|
| 875 |  | 
|---|
| 876 | static NTSTATUS open_mode_check(connection_struct *conn, | 
|---|
| 877 | struct share_mode_lock *lck, | 
|---|
| 878 | uint32_t name_hash, | 
|---|
| 879 | uint32 access_mask, | 
|---|
| 880 | uint32 share_access, | 
|---|
| 881 | uint32 create_options, | 
|---|
| 882 | bool *file_existed) | 
|---|
| 883 | { | 
|---|
| 884 | int i; | 
|---|
| 885 |  | 
|---|
| 886 | if(lck->num_share_modes == 0) { | 
|---|
| 887 | return NT_STATUS_OK; | 
|---|
| 888 | } | 
|---|
| 889 |  | 
|---|
| 890 | *file_existed = True; | 
|---|
| 891 |  | 
|---|
| 892 | /* A delete on close prohibits everything */ | 
|---|
| 893 |  | 
|---|
| 894 | if (is_delete_on_close_set(lck, name_hash)) { | 
|---|
| 895 | return NT_STATUS_DELETE_PENDING; | 
|---|
| 896 | } | 
|---|
| 897 |  | 
|---|
| 898 | if (is_stat_open(access_mask)) { | 
|---|
| 899 | /* Stat open that doesn't trigger oplock breaks or share mode | 
|---|
| 900 | * checks... ! JRA. */ | 
|---|
| 901 | return NT_STATUS_OK; | 
|---|
| 902 | } | 
|---|
| 903 |  | 
|---|
| 904 | /* | 
|---|
| 905 | * Check if the share modes will give us access. | 
|---|
| 906 | */ | 
|---|
| 907 |  | 
|---|
| 908 | #if defined(DEVELOPER) | 
|---|
| 909 | for(i = 0; i < lck->num_share_modes; i++) { | 
|---|
| 910 | validate_my_share_entries(conn->sconn, i, | 
|---|
| 911 | &lck->share_modes[i]); | 
|---|
| 912 | } | 
|---|
| 913 | #endif | 
|---|
| 914 |  | 
|---|
| 915 | if (!lp_share_modes(SNUM(conn))) { | 
|---|
| 916 | return NT_STATUS_OK; | 
|---|
| 917 | } | 
|---|
| 918 |  | 
|---|
| 919 | /* Now we check the share modes, after any oplock breaks. */ | 
|---|
| 920 | for(i = 0; i < lck->num_share_modes; i++) { | 
|---|
| 921 |  | 
|---|
| 922 | if (!is_valid_share_mode_entry(&lck->share_modes[i])) { | 
|---|
| 923 | continue; | 
|---|
| 924 | } | 
|---|
| 925 |  | 
|---|
| 926 | /* someone else has a share lock on it, check to see if we can | 
|---|
| 927 | * too */ | 
|---|
| 928 | if (share_conflict(&lck->share_modes[i], | 
|---|
| 929 | access_mask, share_access)) { | 
|---|
| 930 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 931 | } | 
|---|
| 932 | } | 
|---|
| 933 |  | 
|---|
| 934 | return NT_STATUS_OK; | 
|---|
| 935 | } | 
|---|
| 936 |  | 
|---|
| 937 | /* | 
|---|
| 938 | * Send a break message to the oplock holder and delay the open for | 
|---|
| 939 | * our client. | 
|---|
| 940 | */ | 
|---|
| 941 |  | 
|---|
| 942 | static NTSTATUS send_break_message(files_struct *fsp, | 
|---|
| 943 | struct share_mode_entry *exclusive, | 
|---|
| 944 | uint64_t mid, | 
|---|
| 945 | int oplock_request) | 
|---|
| 946 | { | 
|---|
| 947 | NTSTATUS status; | 
|---|
| 948 | char msg[MSG_SMB_SHARE_MODE_ENTRY_SIZE]; | 
|---|
| 949 |  | 
|---|
| 950 | DEBUG(10, ("Sending break request to PID %s\n", | 
|---|
| 951 | procid_str_static(&exclusive->pid))); | 
|---|
| 952 | exclusive->op_mid = mid; | 
|---|
| 953 |  | 
|---|
| 954 | /* Create the message. */ | 
|---|
| 955 | share_mode_entry_to_message(msg, exclusive); | 
|---|
| 956 |  | 
|---|
| 957 | /* Add in the FORCE_OPLOCK_BREAK_TO_NONE bit in the message if set. We | 
|---|
| 958 | don't want this set in the share mode struct pointed to by lck. */ | 
|---|
| 959 |  | 
|---|
| 960 | if (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE) { | 
|---|
| 961 | SSVAL(msg,OP_BREAK_MSG_OP_TYPE_OFFSET, | 
|---|
| 962 | exclusive->op_type | FORCE_OPLOCK_BREAK_TO_NONE); | 
|---|
| 963 | } | 
|---|
| 964 |  | 
|---|
| 965 | status = messaging_send_buf(fsp->conn->sconn->msg_ctx, exclusive->pid, | 
|---|
| 966 | MSG_SMB_BREAK_REQUEST, | 
|---|
| 967 | (uint8 *)msg, | 
|---|
| 968 | MSG_SMB_SHARE_MODE_ENTRY_SIZE); | 
|---|
| 969 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 970 | DEBUG(3, ("Could not send oplock break message: %s\n", | 
|---|
| 971 | nt_errstr(status))); | 
|---|
| 972 | } | 
|---|
| 973 |  | 
|---|
| 974 | return status; | 
|---|
| 975 | } | 
|---|
| 976 |  | 
|---|
| 977 | /* | 
|---|
| 978 | * Return share_mode_entry pointers for : | 
|---|
| 979 | * 1). Batch oplock entry. | 
|---|
| 980 | * 2). Batch or exclusive oplock entry (may be identical to #1). | 
|---|
| 981 | * bool have_level2_oplock | 
|---|
| 982 | * bool have_no_oplock. | 
|---|
| 983 | * Do internal consistency checks on the share mode for a file. | 
|---|
| 984 | */ | 
|---|
| 985 |  | 
|---|
| 986 | static void find_oplock_types(files_struct *fsp, | 
|---|
| 987 | int oplock_request, | 
|---|
| 988 | struct share_mode_lock *lck, | 
|---|
| 989 | struct share_mode_entry **pp_batch, | 
|---|
| 990 | struct share_mode_entry **pp_ex_or_batch, | 
|---|
| 991 | bool *got_level2, | 
|---|
| 992 | bool *got_no_oplock) | 
|---|
| 993 | { | 
|---|
| 994 | int i; | 
|---|
| 995 |  | 
|---|
| 996 | *pp_batch = NULL; | 
|---|
| 997 | *pp_ex_or_batch = NULL; | 
|---|
| 998 | *got_level2 = false; | 
|---|
| 999 | *got_no_oplock = false; | 
|---|
| 1000 |  | 
|---|
| 1001 | /* Ignore stat or internal opens, as is done in | 
|---|
| 1002 | delay_for_batch_oplocks() and | 
|---|
| 1003 | delay_for_exclusive_oplocks(). | 
|---|
| 1004 | */ | 
|---|
| 1005 | if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) { | 
|---|
| 1006 | return; | 
|---|
| 1007 | } | 
|---|
| 1008 |  | 
|---|
| 1009 | for (i=0; i<lck->num_share_modes; i++) { | 
|---|
| 1010 | if (!is_valid_share_mode_entry(&lck->share_modes[i])) { | 
|---|
| 1011 | continue; | 
|---|
| 1012 | } | 
|---|
| 1013 |  | 
|---|
| 1014 | if (lck->share_modes[i].op_type == NO_OPLOCK && | 
|---|
| 1015 | is_stat_open(lck->share_modes[i].access_mask)) { | 
|---|
| 1016 | /* We ignore stat opens in the table - they | 
|---|
| 1017 | always have NO_OPLOCK and never get or | 
|---|
| 1018 | cause breaks. JRA. */ | 
|---|
| 1019 | continue; | 
|---|
| 1020 | } | 
|---|
| 1021 |  | 
|---|
| 1022 | if (BATCH_OPLOCK_TYPE(lck->share_modes[i].op_type)) { | 
|---|
| 1023 | /* batch - can only be one. */ | 
|---|
| 1024 | if (*pp_ex_or_batch || *pp_batch || *got_level2 || *got_no_oplock) { | 
|---|
| 1025 | smb_panic("Bad batch oplock entry."); | 
|---|
| 1026 | } | 
|---|
| 1027 | *pp_batch = &lck->share_modes[i]; | 
|---|
| 1028 | } | 
|---|
| 1029 |  | 
|---|
| 1030 | if (EXCLUSIVE_OPLOCK_TYPE(lck->share_modes[i].op_type)) { | 
|---|
| 1031 | /* Exclusive or batch - can only be one. */ | 
|---|
| 1032 | if (*pp_ex_or_batch || *got_level2 || *got_no_oplock) { | 
|---|
| 1033 | smb_panic("Bad exclusive or batch oplock entry."); | 
|---|
| 1034 | } | 
|---|
| 1035 | *pp_ex_or_batch = &lck->share_modes[i]; | 
|---|
| 1036 | } | 
|---|
| 1037 |  | 
|---|
| 1038 | if (LEVEL_II_OPLOCK_TYPE(lck->share_modes[i].op_type)) { | 
|---|
| 1039 | if (*pp_batch || *pp_ex_or_batch) { | 
|---|
| 1040 | smb_panic("Bad levelII oplock entry."); | 
|---|
| 1041 | } | 
|---|
| 1042 | *got_level2 = true; | 
|---|
| 1043 | } | 
|---|
| 1044 |  | 
|---|
| 1045 | if (lck->share_modes[i].op_type == NO_OPLOCK) { | 
|---|
| 1046 | if (*pp_batch || *pp_ex_or_batch) { | 
|---|
| 1047 | smb_panic("Bad no oplock entry."); | 
|---|
| 1048 | } | 
|---|
| 1049 | *got_no_oplock = true; | 
|---|
| 1050 | } | 
|---|
| 1051 | } | 
|---|
| 1052 | } | 
|---|
| 1053 |  | 
|---|
| 1054 | static bool delay_for_batch_oplocks(files_struct *fsp, | 
|---|
| 1055 | uint64_t mid, | 
|---|
| 1056 | int oplock_request, | 
|---|
| 1057 | struct share_mode_entry *batch_entry) | 
|---|
| 1058 | { | 
|---|
| 1059 | if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) { | 
|---|
| 1060 | return false; | 
|---|
| 1061 | } | 
|---|
| 1062 |  | 
|---|
| 1063 | if (batch_entry != NULL) { | 
|---|
| 1064 | /* Found a batch oplock */ | 
|---|
| 1065 | send_break_message(fsp, batch_entry, mid, oplock_request); | 
|---|
| 1066 | return true; | 
|---|
| 1067 | } | 
|---|
| 1068 | return false; | 
|---|
| 1069 | } | 
|---|
| 1070 |  | 
|---|
| 1071 | static bool delay_for_exclusive_oplocks(files_struct *fsp, | 
|---|
| 1072 | uint64_t mid, | 
|---|
| 1073 | int oplock_request, | 
|---|
| 1074 | struct share_mode_entry *ex_entry) | 
|---|
| 1075 | { | 
|---|
| 1076 | if ((oplock_request & INTERNAL_OPEN_ONLY) || is_stat_open(fsp->access_mask)) { | 
|---|
| 1077 | return false; | 
|---|
| 1078 | } | 
|---|
| 1079 |  | 
|---|
| 1080 | if (ex_entry != NULL) { | 
|---|
| 1081 | send_break_message(fsp, ex_entry, mid, oplock_request); | 
|---|
| 1082 | return true; | 
|---|
| 1083 | } | 
|---|
| 1084 | return false; | 
|---|
| 1085 | } | 
|---|
| 1086 |  | 
|---|
| 1087 | static void grant_fsp_oplock_type(files_struct *fsp, | 
|---|
| 1088 | const struct byte_range_lock *br_lck, | 
|---|
| 1089 | int oplock_request, | 
|---|
| 1090 | bool got_level2_oplock, | 
|---|
| 1091 | bool got_a_none_oplock) | 
|---|
| 1092 | { | 
|---|
| 1093 | bool allow_level2 = (global_client_caps & CAP_LEVEL_II_OPLOCKS) && | 
|---|
| 1094 | lp_level2_oplocks(SNUM(fsp->conn)); | 
|---|
| 1095 |  | 
|---|
| 1096 | /* Start by granting what the client asked for, | 
|---|
| 1097 | but ensure no SAMBA_PRIVATE bits can be set. */ | 
|---|
| 1098 | fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK); | 
|---|
| 1099 |  | 
|---|
| 1100 | if (oplock_request & INTERNAL_OPEN_ONLY) { | 
|---|
| 1101 | /* No oplocks on internal open. */ | 
|---|
| 1102 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 1103 | DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n", | 
|---|
| 1104 | fsp->oplock_type, fsp_str_dbg(fsp))); | 
|---|
| 1105 | return; | 
|---|
| 1106 | } else if (br_lck && br_lck->num_locks > 0) { | 
|---|
| 1107 | DEBUG(10,("grant_fsp_oplock_type: file %s has byte range locks\n", | 
|---|
| 1108 | fsp_str_dbg(fsp))); | 
|---|
| 1109 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 1110 | } | 
|---|
| 1111 |  | 
|---|
| 1112 | if (is_stat_open(fsp->access_mask)) { | 
|---|
| 1113 | /* Leave the value already set. */ | 
|---|
| 1114 | DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n", | 
|---|
| 1115 | fsp->oplock_type, fsp_str_dbg(fsp))); | 
|---|
| 1116 | return; | 
|---|
| 1117 | } | 
|---|
| 1118 |  | 
|---|
| 1119 | /* | 
|---|
| 1120 | * Match what was requested (fsp->oplock_type) with | 
|---|
| 1121 | * what was found in the existing share modes. | 
|---|
| 1122 | */ | 
|---|
| 1123 |  | 
|---|
| 1124 | if (got_a_none_oplock) { | 
|---|
| 1125 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 1126 | } else if (got_level2_oplock) { | 
|---|
| 1127 | if (fsp->oplock_type == NO_OPLOCK || | 
|---|
| 1128 | fsp->oplock_type == FAKE_LEVEL_II_OPLOCK) { | 
|---|
| 1129 | /* Store a level2 oplock, but don't tell the client */ | 
|---|
| 1130 | fsp->oplock_type = FAKE_LEVEL_II_OPLOCK; | 
|---|
| 1131 | } else { | 
|---|
| 1132 | fsp->oplock_type = LEVEL_II_OPLOCK; | 
|---|
| 1133 | } | 
|---|
| 1134 | } else { | 
|---|
| 1135 | /* All share_mode_entries are placeholders or deferred. | 
|---|
| 1136 | * Silently upgrade to fake levelII if the client didn't | 
|---|
| 1137 | * ask for an oplock. */ | 
|---|
| 1138 | if (fsp->oplock_type == NO_OPLOCK) { | 
|---|
| 1139 | /* Store a level2 oplock, but don't tell the client */ | 
|---|
| 1140 | fsp->oplock_type = FAKE_LEVEL_II_OPLOCK; | 
|---|
| 1141 | } | 
|---|
| 1142 | } | 
|---|
| 1143 |  | 
|---|
| 1144 | /* | 
|---|
| 1145 | * Don't grant level2 to clients that don't want them | 
|---|
| 1146 | * or if we've turned them off. | 
|---|
| 1147 | */ | 
|---|
| 1148 | if (fsp->oplock_type == LEVEL_II_OPLOCK && !allow_level2) { | 
|---|
| 1149 | fsp->oplock_type = FAKE_LEVEL_II_OPLOCK; | 
|---|
| 1150 | } | 
|---|
| 1151 |  | 
|---|
| 1152 | DEBUG(10,("grant_fsp_oplock_type: oplock type 0x%x on file %s\n", | 
|---|
| 1153 | fsp->oplock_type, fsp_str_dbg(fsp))); | 
|---|
| 1154 | } | 
|---|
| 1155 |  | 
|---|
| 1156 | bool request_timed_out(struct timeval request_time, | 
|---|
| 1157 | struct timeval timeout) | 
|---|
| 1158 | { | 
|---|
| 1159 | struct timeval now, end_time; | 
|---|
| 1160 | GetTimeOfDay(&now); | 
|---|
| 1161 | end_time = timeval_sum(&request_time, &timeout); | 
|---|
| 1162 | return (timeval_compare(&end_time, &now) < 0); | 
|---|
| 1163 | } | 
|---|
| 1164 |  | 
|---|
| 1165 | /**************************************************************************** | 
|---|
| 1166 | Handle the 1 second delay in returning a SHARING_VIOLATION error. | 
|---|
| 1167 | ****************************************************************************/ | 
|---|
| 1168 |  | 
|---|
| 1169 | static void defer_open(struct share_mode_lock *lck, | 
|---|
| 1170 | struct timeval request_time, | 
|---|
| 1171 | struct timeval timeout, | 
|---|
| 1172 | struct smb_request *req, | 
|---|
| 1173 | struct deferred_open_record *state) | 
|---|
| 1174 | { | 
|---|
| 1175 | int i; | 
|---|
| 1176 |  | 
|---|
| 1177 | /* Paranoia check */ | 
|---|
| 1178 |  | 
|---|
| 1179 | for (i=0; i<lck->num_share_modes; i++) { | 
|---|
| 1180 | struct share_mode_entry *e = &lck->share_modes[i]; | 
|---|
| 1181 |  | 
|---|
| 1182 | if (!is_deferred_open_entry(e)) { | 
|---|
| 1183 | continue; | 
|---|
| 1184 | } | 
|---|
| 1185 |  | 
|---|
| 1186 | if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) { | 
|---|
| 1187 | DEBUG(0, ("Trying to defer an already deferred " | 
|---|
| 1188 | "request: mid=%llu, exiting\n", | 
|---|
| 1189 | (unsigned long long)req->mid)); | 
|---|
| 1190 | exit_server("attempt to defer a deferred request"); | 
|---|
| 1191 | } | 
|---|
| 1192 | } | 
|---|
| 1193 |  | 
|---|
| 1194 | /* End paranoia check */ | 
|---|
| 1195 |  | 
|---|
| 1196 | DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred " | 
|---|
| 1197 | "open entry for mid %llu\n", | 
|---|
| 1198 | (unsigned int)request_time.tv_sec, | 
|---|
| 1199 | (unsigned int)request_time.tv_usec, | 
|---|
| 1200 | (unsigned long long)req->mid)); | 
|---|
| 1201 |  | 
|---|
| 1202 | if (!push_deferred_open_message_smb(req, request_time, timeout, | 
|---|
| 1203 | state->id, (char *)state, sizeof(*state))) { | 
|---|
| 1204 | exit_server("push_deferred_open_message_smb failed"); | 
|---|
| 1205 | } | 
|---|
| 1206 | add_deferred_open(lck, req->mid, request_time, | 
|---|
| 1207 | sconn_server_id(req->sconn), state->id); | 
|---|
| 1208 | } | 
|---|
| 1209 |  | 
|---|
| 1210 |  | 
|---|
| 1211 | /**************************************************************************** | 
|---|
| 1212 | On overwrite open ensure that the attributes match. | 
|---|
| 1213 | ****************************************************************************/ | 
|---|
| 1214 |  | 
|---|
| 1215 | bool open_match_attributes(connection_struct *conn, | 
|---|
| 1216 | uint32 old_dos_attr, | 
|---|
| 1217 | uint32 new_dos_attr, | 
|---|
| 1218 | mode_t existing_unx_mode, | 
|---|
| 1219 | mode_t new_unx_mode, | 
|---|
| 1220 | mode_t *returned_unx_mode) | 
|---|
| 1221 | { | 
|---|
| 1222 | uint32 noarch_old_dos_attr, noarch_new_dos_attr; | 
|---|
| 1223 |  | 
|---|
| 1224 | noarch_old_dos_attr = (old_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE); | 
|---|
| 1225 | noarch_new_dos_attr = (new_dos_attr & ~FILE_ATTRIBUTE_ARCHIVE); | 
|---|
| 1226 |  | 
|---|
| 1227 | if((noarch_old_dos_attr == 0 && noarch_new_dos_attr != 0) || | 
|---|
| 1228 | (noarch_old_dos_attr != 0 && ((noarch_old_dos_attr & noarch_new_dos_attr) == noarch_old_dos_attr))) { | 
|---|
| 1229 | *returned_unx_mode = new_unx_mode; | 
|---|
| 1230 | } else { | 
|---|
| 1231 | *returned_unx_mode = (mode_t)0; | 
|---|
| 1232 | } | 
|---|
| 1233 |  | 
|---|
| 1234 | DEBUG(10,("open_match_attributes: old_dos_attr = 0x%x, " | 
|---|
| 1235 | "existing_unx_mode = 0%o, new_dos_attr = 0x%x " | 
|---|
| 1236 | "returned_unx_mode = 0%o\n", | 
|---|
| 1237 | (unsigned int)old_dos_attr, | 
|---|
| 1238 | (unsigned int)existing_unx_mode, | 
|---|
| 1239 | (unsigned int)new_dos_attr, | 
|---|
| 1240 | (unsigned int)*returned_unx_mode )); | 
|---|
| 1241 |  | 
|---|
| 1242 | /* If we're mapping SYSTEM and HIDDEN ensure they match. */ | 
|---|
| 1243 | if (lp_map_system(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) { | 
|---|
| 1244 | if ((old_dos_attr & FILE_ATTRIBUTE_SYSTEM) && | 
|---|
| 1245 | !(new_dos_attr & FILE_ATTRIBUTE_SYSTEM)) { | 
|---|
| 1246 | return False; | 
|---|
| 1247 | } | 
|---|
| 1248 | } | 
|---|
| 1249 | if (lp_map_hidden(SNUM(conn)) || lp_store_dos_attributes(SNUM(conn))) { | 
|---|
| 1250 | if ((old_dos_attr & FILE_ATTRIBUTE_HIDDEN) && | 
|---|
| 1251 | !(new_dos_attr & FILE_ATTRIBUTE_HIDDEN)) { | 
|---|
| 1252 | return False; | 
|---|
| 1253 | } | 
|---|
| 1254 | } | 
|---|
| 1255 | return True; | 
|---|
| 1256 | } | 
|---|
| 1257 |  | 
|---|
| 1258 | /**************************************************************************** | 
|---|
| 1259 | Special FCB or DOS processing in the case of a sharing violation. | 
|---|
| 1260 | Try and find a duplicated file handle. | 
|---|
| 1261 | ****************************************************************************/ | 
|---|
| 1262 |  | 
|---|
| 1263 | NTSTATUS fcb_or_dos_open(struct smb_request *req, | 
|---|
| 1264 | connection_struct *conn, | 
|---|
| 1265 | files_struct *fsp_to_dup_into, | 
|---|
| 1266 | const struct smb_filename *smb_fname, | 
|---|
| 1267 | struct file_id id, | 
|---|
| 1268 | uint16 file_pid, | 
|---|
| 1269 | uint16 vuid, | 
|---|
| 1270 | uint32 access_mask, | 
|---|
| 1271 | uint32 share_access, | 
|---|
| 1272 | uint32 create_options) | 
|---|
| 1273 | { | 
|---|
| 1274 | files_struct *fsp; | 
|---|
| 1275 |  | 
|---|
| 1276 | DEBUG(5,("fcb_or_dos_open: attempting old open semantics for " | 
|---|
| 1277 | "file %s.\n", smb_fname_str_dbg(smb_fname))); | 
|---|
| 1278 |  | 
|---|
| 1279 | for(fsp = file_find_di_first(conn->sconn, id); fsp; | 
|---|
| 1280 | fsp = file_find_di_next(fsp)) { | 
|---|
| 1281 |  | 
|---|
| 1282 | DEBUG(10,("fcb_or_dos_open: checking file %s, fd = %d, " | 
|---|
| 1283 | "vuid = %u, file_pid = %u, private_options = 0x%x " | 
|---|
| 1284 | "access_mask = 0x%x\n", fsp_str_dbg(fsp), | 
|---|
| 1285 | fsp->fh->fd, (unsigned int)fsp->vuid, | 
|---|
| 1286 | (unsigned int)fsp->file_pid, | 
|---|
| 1287 | (unsigned int)fsp->fh->private_options, | 
|---|
| 1288 | (unsigned int)fsp->access_mask )); | 
|---|
| 1289 |  | 
|---|
| 1290 | if (fsp->fh->fd != -1 && | 
|---|
| 1291 | fsp->vuid == vuid && | 
|---|
| 1292 | fsp->file_pid == file_pid && | 
|---|
| 1293 | (fsp->fh->private_options & (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS | | 
|---|
| 1294 | NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) && | 
|---|
| 1295 | (fsp->access_mask & FILE_WRITE_DATA) && | 
|---|
| 1296 | strequal(fsp->fsp_name->base_name, smb_fname->base_name) && | 
|---|
| 1297 | strequal(fsp->fsp_name->stream_name, | 
|---|
| 1298 | smb_fname->stream_name)) { | 
|---|
| 1299 | DEBUG(10,("fcb_or_dos_open: file match\n")); | 
|---|
| 1300 | break; | 
|---|
| 1301 | } | 
|---|
| 1302 | } | 
|---|
| 1303 |  | 
|---|
| 1304 | if (!fsp) { | 
|---|
| 1305 | return NT_STATUS_NOT_FOUND; | 
|---|
| 1306 | } | 
|---|
| 1307 |  | 
|---|
| 1308 | /* quite an insane set of semantics ... */ | 
|---|
| 1309 | if (is_executable(smb_fname->base_name) && | 
|---|
| 1310 | (fsp->fh->private_options & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS)) { | 
|---|
| 1311 | DEBUG(10,("fcb_or_dos_open: file fail due to is_executable.\n")); | 
|---|
| 1312 | return NT_STATUS_INVALID_PARAMETER; | 
|---|
| 1313 | } | 
|---|
| 1314 |  | 
|---|
| 1315 | /* We need to duplicate this fsp. */ | 
|---|
| 1316 | return dup_file_fsp(req, fsp, access_mask, share_access, | 
|---|
| 1317 | create_options, fsp_to_dup_into); | 
|---|
| 1318 | } | 
|---|
| 1319 |  | 
|---|
| 1320 | static void schedule_defer_open(struct share_mode_lock *lck, | 
|---|
| 1321 | struct timeval request_time, | 
|---|
| 1322 | struct smb_request *req) | 
|---|
| 1323 | { | 
|---|
| 1324 | struct deferred_open_record state; | 
|---|
| 1325 |  | 
|---|
| 1326 | /* This is a relative time, added to the absolute | 
|---|
| 1327 | request_time value to get the absolute timeout time. | 
|---|
| 1328 | Note that if this is the second or greater time we enter | 
|---|
| 1329 | this codepath for this particular request mid then | 
|---|
| 1330 | request_time is left as the absolute time of the *first* | 
|---|
| 1331 | time this request mid was processed. This is what allows | 
|---|
| 1332 | the request to eventually time out. */ | 
|---|
| 1333 |  | 
|---|
| 1334 | struct timeval timeout; | 
|---|
| 1335 |  | 
|---|
| 1336 | /* Normally the smbd we asked should respond within | 
|---|
| 1337 | * OPLOCK_BREAK_TIMEOUT seconds regardless of whether | 
|---|
| 1338 | * the client did, give twice the timeout as a safety | 
|---|
| 1339 | * measure here in case the other smbd is stuck | 
|---|
| 1340 | * somewhere else. */ | 
|---|
| 1341 |  | 
|---|
| 1342 | timeout = timeval_set(OPLOCK_BREAK_TIMEOUT*2, 0); | 
|---|
| 1343 |  | 
|---|
| 1344 | /* Nothing actually uses state.delayed_for_oplocks | 
|---|
| 1345 | but it's handy to differentiate in debug messages | 
|---|
| 1346 | between a 30 second delay due to oplock break, and | 
|---|
| 1347 | a 1 second delay for share mode conflicts. */ | 
|---|
| 1348 |  | 
|---|
| 1349 | state.delayed_for_oplocks = True; | 
|---|
| 1350 | state.id = lck->id; | 
|---|
| 1351 |  | 
|---|
| 1352 | if (!request_timed_out(request_time, timeout)) { | 
|---|
| 1353 | defer_open(lck, request_time, timeout, req, &state); | 
|---|
| 1354 | } | 
|---|
| 1355 | } | 
|---|
| 1356 |  | 
|---|
| 1357 | /**************************************************************************** | 
|---|
| 1358 | Work out what access_mask to use from what the client sent us. | 
|---|
| 1359 | ****************************************************************************/ | 
|---|
| 1360 |  | 
|---|
| 1361 | NTSTATUS smbd_calculate_access_mask(connection_struct *conn, | 
|---|
| 1362 | const struct smb_filename *smb_fname, | 
|---|
| 1363 | bool file_existed, | 
|---|
| 1364 | uint32_t access_mask, | 
|---|
| 1365 | uint32_t *access_mask_out) | 
|---|
| 1366 | { | 
|---|
| 1367 | NTSTATUS status; | 
|---|
| 1368 | uint32_t orig_access_mask = access_mask; | 
|---|
| 1369 | uint32_t rejected_share_access; | 
|---|
| 1370 |  | 
|---|
| 1371 | /* | 
|---|
| 1372 | * Convert GENERIC bits to specific bits. | 
|---|
| 1373 | */ | 
|---|
| 1374 |  | 
|---|
| 1375 | se_map_generic(&access_mask, &file_generic_mapping); | 
|---|
| 1376 |  | 
|---|
| 1377 | /* Calculate MAXIMUM_ALLOWED_ACCESS if requested. */ | 
|---|
| 1378 | if (access_mask & MAXIMUM_ALLOWED_ACCESS) { | 
|---|
| 1379 | if (file_existed) { | 
|---|
| 1380 |  | 
|---|
| 1381 | struct security_descriptor *sd; | 
|---|
| 1382 | uint32_t access_granted = 0; | 
|---|
| 1383 |  | 
|---|
| 1384 | status = SMB_VFS_GET_NT_ACL(conn, smb_fname->base_name, | 
|---|
| 1385 | (SECINFO_OWNER | | 
|---|
| 1386 | SECINFO_GROUP | | 
|---|
| 1387 | SECINFO_DACL),&sd); | 
|---|
| 1388 |  | 
|---|
| 1389 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 1390 | DEBUG(10,("smbd_calculate_access_mask: " | 
|---|
| 1391 | "Could not get acl on file %s: %s\n", | 
|---|
| 1392 | smb_fname_str_dbg(smb_fname), | 
|---|
| 1393 | nt_errstr(status))); | 
|---|
| 1394 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 1395 | } | 
|---|
| 1396 |  | 
|---|
| 1397 | status = smb1_file_se_access_check(conn, | 
|---|
| 1398 | sd, | 
|---|
| 1399 | get_current_nttok(conn), | 
|---|
| 1400 | access_mask, | 
|---|
| 1401 | &access_granted); | 
|---|
| 1402 |  | 
|---|
| 1403 | TALLOC_FREE(sd); | 
|---|
| 1404 |  | 
|---|
| 1405 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 1406 | DEBUG(10, ("smbd_calculate_access_mask: " | 
|---|
| 1407 | "Access denied on file %s: " | 
|---|
| 1408 | "when calculating maximum access\n", | 
|---|
| 1409 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 1410 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 1411 | } | 
|---|
| 1412 |  | 
|---|
| 1413 | if (!(access_granted & DELETE_ACCESS)) { | 
|---|
| 1414 | if (can_delete_file_in_directory(conn, smb_fname)) { | 
|---|
| 1415 | access_granted |= DELETE_ACCESS; | 
|---|
| 1416 | } | 
|---|
| 1417 | } | 
|---|
| 1418 |  | 
|---|
| 1419 | access_mask = access_granted; | 
|---|
| 1420 | } else { | 
|---|
| 1421 | access_mask = FILE_GENERIC_ALL; | 
|---|
| 1422 | } | 
|---|
| 1423 |  | 
|---|
| 1424 | access_mask &= conn->share_access; | 
|---|
| 1425 | } | 
|---|
| 1426 |  | 
|---|
| 1427 | rejected_share_access = access_mask & ~(conn->share_access); | 
|---|
| 1428 |  | 
|---|
| 1429 | if (rejected_share_access) { | 
|---|
| 1430 | DEBUG(10, ("smbd_calculate_access_mask: Access denied on " | 
|---|
| 1431 | "file %s: rejected by share access mask[0x%08X] " | 
|---|
| 1432 | "orig[0x%08X] mapped[0x%08X] reject[0x%08X]\n", | 
|---|
| 1433 | smb_fname_str_dbg(smb_fname), | 
|---|
| 1434 | conn->share_access, | 
|---|
| 1435 | orig_access_mask, access_mask, | 
|---|
| 1436 | rejected_share_access)); | 
|---|
| 1437 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 1438 | } | 
|---|
| 1439 |  | 
|---|
| 1440 | *access_mask_out = access_mask; | 
|---|
| 1441 | return NT_STATUS_OK; | 
|---|
| 1442 | } | 
|---|
| 1443 |  | 
|---|
| 1444 | /**************************************************************************** | 
|---|
| 1445 | Remove the deferred open entry under lock. | 
|---|
| 1446 | ****************************************************************************/ | 
|---|
| 1447 |  | 
|---|
| 1448 | void remove_deferred_open_entry(struct file_id id, uint64_t mid, | 
|---|
| 1449 | struct server_id pid) | 
|---|
| 1450 | { | 
|---|
| 1451 | struct share_mode_lock *lck = get_share_mode_lock(talloc_tos(), id, | 
|---|
| 1452 | NULL, NULL, NULL); | 
|---|
| 1453 | if (lck == NULL) { | 
|---|
| 1454 | DEBUG(0, ("could not get share mode lock\n")); | 
|---|
| 1455 | } else { | 
|---|
| 1456 | del_deferred_open_entry(lck, mid, pid); | 
|---|
| 1457 | TALLOC_FREE(lck); | 
|---|
| 1458 | } | 
|---|
| 1459 | } | 
|---|
| 1460 |  | 
|---|
| 1461 | /**************************************************************** | 
|---|
| 1462 | Ensure we get the brlock lock followed by the share mode lock | 
|---|
| 1463 | in the correct order to prevent deadlocks if other smbd's are | 
|---|
| 1464 | using the brlock database on this file simultaneously with this open | 
|---|
| 1465 | (that code also gets the locks in brlock -> share mode lock order). | 
|---|
| 1466 | ****************************************************************/ | 
|---|
| 1467 |  | 
|---|
| 1468 | static bool acquire_ordered_locks(TALLOC_CTX *mem_ctx, | 
|---|
| 1469 | files_struct *fsp, | 
|---|
| 1470 | const struct file_id id, | 
|---|
| 1471 | const char *connectpath, | 
|---|
| 1472 | const struct smb_filename *smb_fname, | 
|---|
| 1473 | const struct timespec *p_old_write_time, | 
|---|
| 1474 | struct share_mode_lock **p_lck, | 
|---|
| 1475 | struct byte_range_lock **p_br_lck) | 
|---|
| 1476 | { | 
|---|
| 1477 | /* Ordering - we must get the br_lck for this | 
|---|
| 1478 | file before the share mode. */ | 
|---|
| 1479 | if (lp_locking(fsp->conn->params)) { | 
|---|
| 1480 | *p_br_lck = brl_get_locks_readonly(fsp); | 
|---|
| 1481 | if (*p_br_lck == NULL) { | 
|---|
| 1482 | DEBUG(0, ("Could not get br_lock\n")); | 
|---|
| 1483 | return false; | 
|---|
| 1484 | } | 
|---|
| 1485 | /* Note - we don't need to free the returned | 
|---|
| 1486 | br_lck explicitly as it was allocated on talloc_tos() | 
|---|
| 1487 | and so will be autofreed (and release the lock) | 
|---|
| 1488 | once the frame context disappears. | 
|---|
| 1489 |  | 
|---|
| 1490 | If it was set to fsp->brlock_rec then it was | 
|---|
| 1491 | talloc_move'd to hang off the fsp pointer and | 
|---|
| 1492 | in this case is guarenteed to not be holding the | 
|---|
| 1493 | lock on the brlock database. */ | 
|---|
| 1494 | } | 
|---|
| 1495 |  | 
|---|
| 1496 | *p_lck = get_share_mode_lock(mem_ctx, | 
|---|
| 1497 | id, | 
|---|
| 1498 | connectpath, | 
|---|
| 1499 | smb_fname, | 
|---|
| 1500 | p_old_write_time); | 
|---|
| 1501 |  | 
|---|
| 1502 | if (*p_lck == NULL) { | 
|---|
| 1503 | DEBUG(0, ("Could not get share mode lock\n")); | 
|---|
| 1504 | TALLOC_FREE(*p_br_lck); | 
|---|
| 1505 | return false; | 
|---|
| 1506 | } | 
|---|
| 1507 | return true; | 
|---|
| 1508 | } | 
|---|
| 1509 |  | 
|---|
| 1510 | /**************************************************************************** | 
|---|
| 1511 | Open a file with a share mode. Passed in an already created files_struct *. | 
|---|
| 1512 | ****************************************************************************/ | 
|---|
| 1513 |  | 
|---|
| 1514 | static NTSTATUS open_file_ntcreate(connection_struct *conn, | 
|---|
| 1515 | struct smb_request *req, | 
|---|
| 1516 | uint32 access_mask,         /* access bits (FILE_READ_DATA etc.) */ | 
|---|
| 1517 | uint32 share_access,        /* share constants (FILE_SHARE_READ etc) */ | 
|---|
| 1518 | uint32 create_disposition,  /* FILE_OPEN_IF etc. */ | 
|---|
| 1519 | uint32 create_options,      /* options such as delete on close. */ | 
|---|
| 1520 | uint32 new_dos_attributes,  /* attributes used for new file. */ | 
|---|
| 1521 | int oplock_request,         /* internal Samba oplock codes. */ | 
|---|
| 1522 | /* Information (FILE_EXISTS etc.) */ | 
|---|
| 1523 | uint32_t private_flags,     /* Samba specific flags. */ | 
|---|
| 1524 | int *pinfo, | 
|---|
| 1525 | files_struct *fsp) | 
|---|
| 1526 | { | 
|---|
| 1527 | struct smb_filename *smb_fname = fsp->fsp_name; | 
|---|
| 1528 | int flags=0; | 
|---|
| 1529 | int flags2=0; | 
|---|
| 1530 | bool file_existed = VALID_STAT(smb_fname->st); | 
|---|
| 1531 | bool def_acl = False; | 
|---|
| 1532 | bool posix_open = False; | 
|---|
| 1533 | bool new_file_created = False; | 
|---|
| 1534 | bool clear_ads = false; | 
|---|
| 1535 | struct file_id id; | 
|---|
| 1536 | NTSTATUS fsp_open = NT_STATUS_ACCESS_DENIED; | 
|---|
| 1537 | mode_t new_unx_mode = (mode_t)0; | 
|---|
| 1538 | mode_t unx_mode = (mode_t)0; | 
|---|
| 1539 | int info; | 
|---|
| 1540 | uint32 existing_dos_attributes = 0; | 
|---|
| 1541 | struct timeval request_time = timeval_zero(); | 
|---|
| 1542 | struct share_mode_lock *lck = NULL; | 
|---|
| 1543 | uint32 open_access_mask = access_mask; | 
|---|
| 1544 | NTSTATUS status; | 
|---|
| 1545 | char *parent_dir; | 
|---|
| 1546 |  | 
|---|
| 1547 | ZERO_STRUCT(id); | 
|---|
| 1548 |  | 
|---|
| 1549 | if (conn->printer) { | 
|---|
| 1550 | /* | 
|---|
| 1551 | * Printers are handled completely differently. | 
|---|
| 1552 | * Most of the passed parameters are ignored. | 
|---|
| 1553 | */ | 
|---|
| 1554 |  | 
|---|
| 1555 | if (pinfo) { | 
|---|
| 1556 | *pinfo = FILE_WAS_CREATED; | 
|---|
| 1557 | } | 
|---|
| 1558 |  | 
|---|
| 1559 | DEBUG(10, ("open_file_ntcreate: printer open fname=%s\n", | 
|---|
| 1560 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 1561 |  | 
|---|
| 1562 | if (!req) { | 
|---|
| 1563 | DEBUG(0,("open_file_ntcreate: printer open without " | 
|---|
| 1564 | "an SMB request!\n")); | 
|---|
| 1565 | return NT_STATUS_INTERNAL_ERROR; | 
|---|
| 1566 | } | 
|---|
| 1567 |  | 
|---|
| 1568 | return print_spool_open(fsp, smb_fname->base_name, | 
|---|
| 1569 | req->vuid); | 
|---|
| 1570 | } | 
|---|
| 1571 |  | 
|---|
| 1572 | if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir, | 
|---|
| 1573 | NULL)) { | 
|---|
| 1574 | return NT_STATUS_NO_MEMORY; | 
|---|
| 1575 | } | 
|---|
| 1576 |  | 
|---|
| 1577 | if (new_dos_attributes & FILE_FLAG_POSIX_SEMANTICS) { | 
|---|
| 1578 | posix_open = True; | 
|---|
| 1579 | unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS); | 
|---|
| 1580 | new_dos_attributes = 0; | 
|---|
| 1581 | } else { | 
|---|
| 1582 | /* Windows allows a new file to be created and | 
|---|
| 1583 | silently removes a FILE_ATTRIBUTE_DIRECTORY | 
|---|
| 1584 | sent by the client. Do the same. */ | 
|---|
| 1585 |  | 
|---|
| 1586 | new_dos_attributes &= ~FILE_ATTRIBUTE_DIRECTORY; | 
|---|
| 1587 |  | 
|---|
| 1588 | /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is | 
|---|
| 1589 | * created new. */ | 
|---|
| 1590 | unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE, | 
|---|
| 1591 | smb_fname, parent_dir); | 
|---|
| 1592 | } | 
|---|
| 1593 |  | 
|---|
| 1594 | DEBUG(10, ("open_file_ntcreate: fname=%s, dos_attrs=0x%x " | 
|---|
| 1595 | "access_mask=0x%x share_access=0x%x " | 
|---|
| 1596 | "create_disposition = 0x%x create_options=0x%x " | 
|---|
| 1597 | "unix mode=0%o oplock_request=%d private_flags = 0x%x\n", | 
|---|
| 1598 | smb_fname_str_dbg(smb_fname), new_dos_attributes, | 
|---|
| 1599 | access_mask, share_access, create_disposition, | 
|---|
| 1600 | create_options, (unsigned int)unx_mode, oplock_request, | 
|---|
| 1601 | (unsigned int)private_flags)); | 
|---|
| 1602 |  | 
|---|
| 1603 | if ((req == NULL) && ((oplock_request & INTERNAL_OPEN_ONLY) == 0)) { | 
|---|
| 1604 | DEBUG(0, ("No smb request but not an internal only open!\n")); | 
|---|
| 1605 | return NT_STATUS_INTERNAL_ERROR; | 
|---|
| 1606 | } | 
|---|
| 1607 |  | 
|---|
| 1608 | /* | 
|---|
| 1609 | * Only non-internal opens can be deferred at all | 
|---|
| 1610 | */ | 
|---|
| 1611 |  | 
|---|
| 1612 | if (req) { | 
|---|
| 1613 | void *ptr; | 
|---|
| 1614 | if (get_deferred_open_message_state(req, | 
|---|
| 1615 | &request_time, | 
|---|
| 1616 | &ptr)) { | 
|---|
| 1617 |  | 
|---|
| 1618 | struct deferred_open_record *state = (struct deferred_open_record *)ptr; | 
|---|
| 1619 | /* Remember the absolute time of the original | 
|---|
| 1620 | request with this mid. We'll use it later to | 
|---|
| 1621 | see if this has timed out. */ | 
|---|
| 1622 |  | 
|---|
| 1623 | /* Remove the deferred open entry under lock. */ | 
|---|
| 1624 | remove_deferred_open_entry( | 
|---|
| 1625 | state->id, req->mid, | 
|---|
| 1626 | sconn_server_id(req->sconn)); | 
|---|
| 1627 |  | 
|---|
| 1628 | /* Ensure we don't reprocess this message. */ | 
|---|
| 1629 | remove_deferred_open_message_smb(req->mid); | 
|---|
| 1630 | } | 
|---|
| 1631 | } | 
|---|
| 1632 |  | 
|---|
| 1633 | if (!posix_open) { | 
|---|
| 1634 | new_dos_attributes &= SAMBA_ATTRIBUTES_MASK; | 
|---|
| 1635 | if (file_existed) { | 
|---|
| 1636 | existing_dos_attributes = dos_mode(conn, smb_fname); | 
|---|
| 1637 | } | 
|---|
| 1638 | } | 
|---|
| 1639 |  | 
|---|
| 1640 | /* ignore any oplock requests if oplocks are disabled */ | 
|---|
| 1641 | if (!lp_oplocks(SNUM(conn)) || | 
|---|
| 1642 | IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) { | 
|---|
| 1643 | /* Mask off everything except the private Samba bits. */ | 
|---|
| 1644 | oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK; | 
|---|
| 1645 | } | 
|---|
| 1646 |  | 
|---|
| 1647 | /* this is for OS/2 long file names - say we don't support them */ | 
|---|
| 1648 | if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) { | 
|---|
| 1649 | /* OS/2 Workplace shell fix may be main code stream in a later | 
|---|
| 1650 | * release. */ | 
|---|
| 1651 | DEBUG(5,("open_file_ntcreate: OS/2 long filenames are not " | 
|---|
| 1652 | "supported.\n")); | 
|---|
| 1653 | if (use_nt_status()) { | 
|---|
| 1654 | return NT_STATUS_OBJECT_NAME_NOT_FOUND; | 
|---|
| 1655 | } | 
|---|
| 1656 | return NT_STATUS_DOS(ERRDOS, ERRcannotopen); | 
|---|
| 1657 | } | 
|---|
| 1658 |  | 
|---|
| 1659 | switch( create_disposition ) { | 
|---|
| 1660 | /* | 
|---|
| 1661 | * Currently we're using FILE_SUPERSEDE as the same as | 
|---|
| 1662 | * FILE_OVERWRITE_IF but they really are | 
|---|
| 1663 | * different. FILE_SUPERSEDE deletes an existing file | 
|---|
| 1664 | * (requiring delete access) then recreates it. | 
|---|
| 1665 | */ | 
|---|
| 1666 | case FILE_SUPERSEDE: | 
|---|
| 1667 | /* If file exists replace/overwrite. If file doesn't | 
|---|
| 1668 | * exist create. */ | 
|---|
| 1669 | flags2 |= (O_CREAT | O_TRUNC); | 
|---|
| 1670 | clear_ads = true; | 
|---|
| 1671 | break; | 
|---|
| 1672 |  | 
|---|
| 1673 | case FILE_OVERWRITE_IF: | 
|---|
| 1674 | /* If file exists replace/overwrite. If file doesn't | 
|---|
| 1675 | * exist create. */ | 
|---|
| 1676 | flags2 |= (O_CREAT | O_TRUNC); | 
|---|
| 1677 | clear_ads = true; | 
|---|
| 1678 | break; | 
|---|
| 1679 |  | 
|---|
| 1680 | case FILE_OPEN: | 
|---|
| 1681 | /* If file exists open. If file doesn't exist error. */ | 
|---|
| 1682 | if (!file_existed) { | 
|---|
| 1683 | DEBUG(5,("open_file_ntcreate: FILE_OPEN " | 
|---|
| 1684 | "requested for file %s and file " | 
|---|
| 1685 | "doesn't exist.\n", | 
|---|
| 1686 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 1687 | errno = ENOENT; | 
|---|
| 1688 | return NT_STATUS_OBJECT_NAME_NOT_FOUND; | 
|---|
| 1689 | } | 
|---|
| 1690 | break; | 
|---|
| 1691 |  | 
|---|
| 1692 | case FILE_OVERWRITE: | 
|---|
| 1693 | /* If file exists overwrite. If file doesn't exist | 
|---|
| 1694 | * error. */ | 
|---|
| 1695 | if (!file_existed) { | 
|---|
| 1696 | DEBUG(5,("open_file_ntcreate: FILE_OVERWRITE " | 
|---|
| 1697 | "requested for file %s and file " | 
|---|
| 1698 | "doesn't exist.\n", | 
|---|
| 1699 | smb_fname_str_dbg(smb_fname) )); | 
|---|
| 1700 | errno = ENOENT; | 
|---|
| 1701 | return NT_STATUS_OBJECT_NAME_NOT_FOUND; | 
|---|
| 1702 | } | 
|---|
| 1703 | flags2 |= O_TRUNC; | 
|---|
| 1704 | clear_ads = true; | 
|---|
| 1705 | break; | 
|---|
| 1706 |  | 
|---|
| 1707 | case FILE_CREATE: | 
|---|
| 1708 | /* If file exists error. If file doesn't exist | 
|---|
| 1709 | * create. */ | 
|---|
| 1710 | if (file_existed) { | 
|---|
| 1711 | DEBUG(5,("open_file_ntcreate: FILE_CREATE " | 
|---|
| 1712 | "requested for file %s and file " | 
|---|
| 1713 | "already exists.\n", | 
|---|
| 1714 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 1715 | if (S_ISDIR(smb_fname->st.st_ex_mode)) { | 
|---|
| 1716 | errno = EISDIR; | 
|---|
| 1717 | } else { | 
|---|
| 1718 | errno = EEXIST; | 
|---|
| 1719 | } | 
|---|
| 1720 | return map_nt_error_from_unix(errno); | 
|---|
| 1721 | } | 
|---|
| 1722 | flags2 |= (O_CREAT|O_EXCL); | 
|---|
| 1723 | break; | 
|---|
| 1724 |  | 
|---|
| 1725 | case FILE_OPEN_IF: | 
|---|
| 1726 | /* If file exists open. If file doesn't exist | 
|---|
| 1727 | * create. */ | 
|---|
| 1728 | flags2 |= O_CREAT; | 
|---|
| 1729 | break; | 
|---|
| 1730 |  | 
|---|
| 1731 | default: | 
|---|
| 1732 | return NT_STATUS_INVALID_PARAMETER; | 
|---|
| 1733 | } | 
|---|
| 1734 |  | 
|---|
| 1735 | /* We only care about matching attributes on file exists and | 
|---|
| 1736 | * overwrite. */ | 
|---|
| 1737 |  | 
|---|
| 1738 | if (!posix_open && file_existed && ((create_disposition == FILE_OVERWRITE) || | 
|---|
| 1739 | (create_disposition == FILE_OVERWRITE_IF))) { | 
|---|
| 1740 | if (!open_match_attributes(conn, existing_dos_attributes, | 
|---|
| 1741 | new_dos_attributes, | 
|---|
| 1742 | smb_fname->st.st_ex_mode, | 
|---|
| 1743 | unx_mode, &new_unx_mode)) { | 
|---|
| 1744 | DEBUG(5,("open_file_ntcreate: attributes missmatch " | 
|---|
| 1745 | "for file %s (%x %x) (0%o, 0%o)\n", | 
|---|
| 1746 | smb_fname_str_dbg(smb_fname), | 
|---|
| 1747 | existing_dos_attributes, | 
|---|
| 1748 | new_dos_attributes, | 
|---|
| 1749 | (unsigned int)smb_fname->st.st_ex_mode, | 
|---|
| 1750 | (unsigned int)unx_mode )); | 
|---|
| 1751 | errno = EACCES; | 
|---|
| 1752 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 1753 | } | 
|---|
| 1754 | } | 
|---|
| 1755 |  | 
|---|
| 1756 | status = smbd_calculate_access_mask(conn, smb_fname, file_existed, | 
|---|
| 1757 | access_mask, | 
|---|
| 1758 | &access_mask); | 
|---|
| 1759 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 1760 | DEBUG(10, ("open_file_ntcreate: smbd_calculate_access_mask " | 
|---|
| 1761 | "on file %s returned %s\n", | 
|---|
| 1762 | smb_fname_str_dbg(smb_fname), nt_errstr(status))); | 
|---|
| 1763 | return status; | 
|---|
| 1764 | } | 
|---|
| 1765 |  | 
|---|
| 1766 | open_access_mask = access_mask; | 
|---|
| 1767 |  | 
|---|
| 1768 | if ((flags2 & O_TRUNC) || (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) { | 
|---|
| 1769 | open_access_mask |= FILE_WRITE_DATA; /* This will cause oplock breaks. */ | 
|---|
| 1770 | } | 
|---|
| 1771 |  | 
|---|
| 1772 | DEBUG(10, ("open_file_ntcreate: fname=%s, after mapping " | 
|---|
| 1773 | "access_mask=0x%x\n", smb_fname_str_dbg(smb_fname), | 
|---|
| 1774 | access_mask)); | 
|---|
| 1775 |  | 
|---|
| 1776 | /* | 
|---|
| 1777 | * Note that we ignore the append flag as append does not | 
|---|
| 1778 | * mean the same thing under DOS and Unix. | 
|---|
| 1779 | */ | 
|---|
| 1780 |  | 
|---|
| 1781 | if ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) || | 
|---|
| 1782 | (oplock_request & FORCE_OPLOCK_BREAK_TO_NONE)) { | 
|---|
| 1783 | /* DENY_DOS opens are always underlying read-write on the | 
|---|
| 1784 | file handle, no matter what the requested access mask | 
|---|
| 1785 | says. */ | 
|---|
| 1786 | if ((private_flags & NTCREATEX_OPTIONS_PRIVATE_DENY_DOS) || | 
|---|
| 1787 | access_mask & (FILE_READ_ATTRIBUTES|FILE_READ_DATA|FILE_READ_EA|FILE_EXECUTE)) { | 
|---|
| 1788 | flags = O_RDWR; | 
|---|
| 1789 | } else { | 
|---|
| 1790 | flags = O_WRONLY; | 
|---|
| 1791 | } | 
|---|
| 1792 | } else { | 
|---|
| 1793 | flags = O_RDONLY; | 
|---|
| 1794 | } | 
|---|
| 1795 |  | 
|---|
| 1796 | /* | 
|---|
| 1797 | * Currently we only look at FILE_WRITE_THROUGH for create options. | 
|---|
| 1798 | */ | 
|---|
| 1799 |  | 
|---|
| 1800 | #if defined(O_SYNC) | 
|---|
| 1801 | if ((create_options & FILE_WRITE_THROUGH) && lp_strict_sync(SNUM(conn))) { | 
|---|
| 1802 | flags2 |= O_SYNC; | 
|---|
| 1803 | } | 
|---|
| 1804 | #endif /* O_SYNC */ | 
|---|
| 1805 |  | 
|---|
| 1806 | if (posix_open && (access_mask & FILE_APPEND_DATA)) { | 
|---|
| 1807 | flags2 |= O_APPEND; | 
|---|
| 1808 | } | 
|---|
| 1809 |  | 
|---|
| 1810 | if (!posix_open && !CAN_WRITE(conn)) { | 
|---|
| 1811 | /* | 
|---|
| 1812 | * We should really return a permission denied error if either | 
|---|
| 1813 | * O_CREAT or O_TRUNC are set, but for compatibility with | 
|---|
| 1814 | * older versions of Samba we just AND them out. | 
|---|
| 1815 | */ | 
|---|
| 1816 | flags2 &= ~(O_CREAT|O_TRUNC); | 
|---|
| 1817 | } | 
|---|
| 1818 |  | 
|---|
| 1819 | /* | 
|---|
| 1820 | * Ensure we can't write on a read-only share or file. | 
|---|
| 1821 | */ | 
|---|
| 1822 |  | 
|---|
| 1823 | if (flags != O_RDONLY && file_existed && | 
|---|
| 1824 | (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) { | 
|---|
| 1825 | DEBUG(5,("open_file_ntcreate: write access requested for " | 
|---|
| 1826 | "file %s on read only %s\n", | 
|---|
| 1827 | smb_fname_str_dbg(smb_fname), | 
|---|
| 1828 | !CAN_WRITE(conn) ? "share" : "file" )); | 
|---|
| 1829 | errno = EACCES; | 
|---|
| 1830 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 1831 | } | 
|---|
| 1832 |  | 
|---|
| 1833 | fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st); | 
|---|
| 1834 | fsp->share_access = share_access; | 
|---|
| 1835 | fsp->fh->private_options = private_flags; | 
|---|
| 1836 | fsp->access_mask = open_access_mask; /* We change this to the | 
|---|
| 1837 | * requested access_mask after | 
|---|
| 1838 | * the open is done. */ | 
|---|
| 1839 | fsp->posix_open = posix_open; | 
|---|
| 1840 |  | 
|---|
| 1841 | /* Ensure no SAMBA_PRIVATE bits can be set. */ | 
|---|
| 1842 | fsp->oplock_type = (oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK); | 
|---|
| 1843 |  | 
|---|
| 1844 | if (timeval_is_zero(&request_time)) { | 
|---|
| 1845 | request_time = fsp->open_time; | 
|---|
| 1846 | } | 
|---|
| 1847 |  | 
|---|
| 1848 | if (file_existed) { | 
|---|
| 1849 | struct byte_range_lock *br_lck = NULL; | 
|---|
| 1850 | struct share_mode_entry *batch_entry = NULL; | 
|---|
| 1851 | struct share_mode_entry *exclusive_entry = NULL; | 
|---|
| 1852 | bool got_level2_oplock = false; | 
|---|
| 1853 | bool got_a_none_oplock = false; | 
|---|
| 1854 |  | 
|---|
| 1855 | struct timespec old_write_time = smb_fname->st.st_ex_mtime; | 
|---|
| 1856 | id = vfs_file_id_from_sbuf(conn, &smb_fname->st); | 
|---|
| 1857 |  | 
|---|
| 1858 | if (!acquire_ordered_locks(talloc_tos(), | 
|---|
| 1859 | fsp, | 
|---|
| 1860 | id, | 
|---|
| 1861 | conn->connectpath, | 
|---|
| 1862 | smb_fname, | 
|---|
| 1863 | &old_write_time, | 
|---|
| 1864 | &lck, | 
|---|
| 1865 | &br_lck)) { | 
|---|
| 1866 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 1867 | } | 
|---|
| 1868 |  | 
|---|
| 1869 | /* Get the types we need to examine. */ | 
|---|
| 1870 | find_oplock_types(fsp, | 
|---|
| 1871 | oplock_request, | 
|---|
| 1872 | lck, | 
|---|
| 1873 | &batch_entry, | 
|---|
| 1874 | &exclusive_entry, | 
|---|
| 1875 | &got_level2_oplock, | 
|---|
| 1876 | &got_a_none_oplock); | 
|---|
| 1877 |  | 
|---|
| 1878 | /* First pass - send break only on batch oplocks. */ | 
|---|
| 1879 | if ((req != NULL) && | 
|---|
| 1880 | delay_for_batch_oplocks(fsp, | 
|---|
| 1881 | req->mid, | 
|---|
| 1882 | oplock_request, | 
|---|
| 1883 | batch_entry)) { | 
|---|
| 1884 | schedule_defer_open(lck, request_time, req); | 
|---|
| 1885 | TALLOC_FREE(lck); | 
|---|
| 1886 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 1887 | } | 
|---|
| 1888 |  | 
|---|
| 1889 | /* Use the client requested access mask here, not the one we | 
|---|
| 1890 | * open with. */ | 
|---|
| 1891 | status = open_mode_check(conn, lck, fsp->name_hash, | 
|---|
| 1892 | access_mask, share_access, | 
|---|
| 1893 | create_options, &file_existed); | 
|---|
| 1894 |  | 
|---|
| 1895 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 1896 | /* We might be going to allow this open. Check oplock | 
|---|
| 1897 | * status again. */ | 
|---|
| 1898 | /* Second pass - send break for both batch or | 
|---|
| 1899 | * exclusive oplocks. */ | 
|---|
| 1900 | if ((req != NULL) && | 
|---|
| 1901 | delay_for_exclusive_oplocks( | 
|---|
| 1902 | fsp, | 
|---|
| 1903 | req->mid, | 
|---|
| 1904 | oplock_request, | 
|---|
| 1905 | exclusive_entry)) { | 
|---|
| 1906 | schedule_defer_open(lck, request_time, req); | 
|---|
| 1907 | TALLOC_FREE(lck); | 
|---|
| 1908 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 1909 | } | 
|---|
| 1910 | } | 
|---|
| 1911 |  | 
|---|
| 1912 | if (NT_STATUS_EQUAL(status, NT_STATUS_DELETE_PENDING)) { | 
|---|
| 1913 | /* DELETE_PENDING is not deferred for a second */ | 
|---|
| 1914 | TALLOC_FREE(lck); | 
|---|
| 1915 | return status; | 
|---|
| 1916 | } | 
|---|
| 1917 |  | 
|---|
| 1918 | grant_fsp_oplock_type(fsp, | 
|---|
| 1919 | br_lck, | 
|---|
| 1920 | oplock_request, | 
|---|
| 1921 | got_level2_oplock, | 
|---|
| 1922 | got_a_none_oplock); | 
|---|
| 1923 |  | 
|---|
| 1924 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 1925 | uint32 can_access_mask; | 
|---|
| 1926 | bool can_access = True; | 
|---|
| 1927 |  | 
|---|
| 1928 | SMB_ASSERT(NT_STATUS_EQUAL(status, NT_STATUS_SHARING_VIOLATION)); | 
|---|
| 1929 |  | 
|---|
| 1930 | /* Check if this can be done with the deny_dos and fcb | 
|---|
| 1931 | * calls. */ | 
|---|
| 1932 | if (private_flags & | 
|---|
| 1933 | (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS| | 
|---|
| 1934 | NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) { | 
|---|
| 1935 | if (req == NULL) { | 
|---|
| 1936 | DEBUG(0, ("DOS open without an SMB " | 
|---|
| 1937 | "request!\n")); | 
|---|
| 1938 | TALLOC_FREE(lck); | 
|---|
| 1939 | return NT_STATUS_INTERNAL_ERROR; | 
|---|
| 1940 | } | 
|---|
| 1941 |  | 
|---|
| 1942 | /* Use the client requested access mask here, | 
|---|
| 1943 | * not the one we open with. */ | 
|---|
| 1944 | status = fcb_or_dos_open(req, | 
|---|
| 1945 | conn, | 
|---|
| 1946 | fsp, | 
|---|
| 1947 | smb_fname, | 
|---|
| 1948 | id, | 
|---|
| 1949 | req->smbpid, | 
|---|
| 1950 | req->vuid, | 
|---|
| 1951 | access_mask, | 
|---|
| 1952 | share_access, | 
|---|
| 1953 | create_options); | 
|---|
| 1954 |  | 
|---|
| 1955 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 1956 | TALLOC_FREE(lck); | 
|---|
| 1957 | if (pinfo) { | 
|---|
| 1958 | *pinfo = FILE_WAS_OPENED; | 
|---|
| 1959 | } | 
|---|
| 1960 | return NT_STATUS_OK; | 
|---|
| 1961 | } | 
|---|
| 1962 | } | 
|---|
| 1963 |  | 
|---|
| 1964 | /* | 
|---|
| 1965 | * This next line is a subtlety we need for | 
|---|
| 1966 | * MS-Access. If a file open will fail due to share | 
|---|
| 1967 | * permissions and also for security (access) reasons, | 
|---|
| 1968 | * we need to return the access failed error, not the | 
|---|
| 1969 | * share error. We can't open the file due to kernel | 
|---|
| 1970 | * oplock deadlock (it's possible we failed above on | 
|---|
| 1971 | * the open_mode_check()) so use a userspace check. | 
|---|
| 1972 | */ | 
|---|
| 1973 |  | 
|---|
| 1974 | if (flags & O_RDWR) { | 
|---|
| 1975 | can_access_mask = FILE_READ_DATA|FILE_WRITE_DATA; | 
|---|
| 1976 | } else if (flags & O_WRONLY) { | 
|---|
| 1977 | can_access_mask = FILE_WRITE_DATA; | 
|---|
| 1978 | } else { | 
|---|
| 1979 | can_access_mask = FILE_READ_DATA; | 
|---|
| 1980 | } | 
|---|
| 1981 |  | 
|---|
| 1982 | if (((can_access_mask & FILE_WRITE_DATA) && | 
|---|
| 1983 | !CAN_WRITE(conn)) || | 
|---|
| 1984 | !can_access_file_data(conn, smb_fname, | 
|---|
| 1985 | can_access_mask)) { | 
|---|
| 1986 | can_access = False; | 
|---|
| 1987 | } | 
|---|
| 1988 |  | 
|---|
| 1989 | /* | 
|---|
| 1990 | * If we're returning a share violation, ensure we | 
|---|
| 1991 | * cope with the braindead 1 second delay. | 
|---|
| 1992 | */ | 
|---|
| 1993 |  | 
|---|
| 1994 | if (!(oplock_request & INTERNAL_OPEN_ONLY) && | 
|---|
| 1995 | lp_defer_sharing_violations()) { | 
|---|
| 1996 | struct timeval timeout; | 
|---|
| 1997 | struct deferred_open_record state; | 
|---|
| 1998 | int timeout_usecs; | 
|---|
| 1999 |  | 
|---|
| 2000 | /* this is a hack to speed up torture tests | 
|---|
| 2001 | in 'make test' */ | 
|---|
| 2002 | timeout_usecs = lp_parm_int(SNUM(conn), | 
|---|
| 2003 | "smbd","sharedelay", | 
|---|
| 2004 | SHARING_VIOLATION_USEC_WAIT); | 
|---|
| 2005 |  | 
|---|
| 2006 | /* This is a relative time, added to the absolute | 
|---|
| 2007 | request_time value to get the absolute timeout time. | 
|---|
| 2008 | Note that if this is the second or greater time we enter | 
|---|
| 2009 | this codepath for this particular request mid then | 
|---|
| 2010 | request_time is left as the absolute time of the *first* | 
|---|
| 2011 | time this request mid was processed. This is what allows | 
|---|
| 2012 | the request to eventually time out. */ | 
|---|
| 2013 |  | 
|---|
| 2014 | timeout = timeval_set(0, timeout_usecs); | 
|---|
| 2015 |  | 
|---|
| 2016 | /* Nothing actually uses state.delayed_for_oplocks | 
|---|
| 2017 | but it's handy to differentiate in debug messages | 
|---|
| 2018 | between a 30 second delay due to oplock break, and | 
|---|
| 2019 | a 1 second delay for share mode conflicts. */ | 
|---|
| 2020 |  | 
|---|
| 2021 | state.delayed_for_oplocks = False; | 
|---|
| 2022 | state.id = id; | 
|---|
| 2023 |  | 
|---|
| 2024 | if ((req != NULL) | 
|---|
| 2025 | && !request_timed_out(request_time, | 
|---|
| 2026 | timeout)) { | 
|---|
| 2027 | defer_open(lck, request_time, timeout, | 
|---|
| 2028 | req, &state); | 
|---|
| 2029 | } | 
|---|
| 2030 | } | 
|---|
| 2031 |  | 
|---|
| 2032 | TALLOC_FREE(lck); | 
|---|
| 2033 | if (can_access) { | 
|---|
| 2034 | /* | 
|---|
| 2035 | * We have detected a sharing violation here | 
|---|
| 2036 | * so return the correct error code | 
|---|
| 2037 | */ | 
|---|
| 2038 | status = NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2039 | } else { | 
|---|
| 2040 | status = NT_STATUS_ACCESS_DENIED; | 
|---|
| 2041 | } | 
|---|
| 2042 | return status; | 
|---|
| 2043 | } | 
|---|
| 2044 |  | 
|---|
| 2045 | /* | 
|---|
| 2046 | * We exit this block with the share entry *locked*..... | 
|---|
| 2047 | */ | 
|---|
| 2048 | } | 
|---|
| 2049 |  | 
|---|
| 2050 | SMB_ASSERT(!file_existed || (lck != NULL)); | 
|---|
| 2051 |  | 
|---|
| 2052 | /* | 
|---|
| 2053 | * Ensure we pay attention to default ACLs on directories if required. | 
|---|
| 2054 | */ | 
|---|
| 2055 |  | 
|---|
| 2056 | if ((flags2 & O_CREAT) && lp_inherit_acls(SNUM(conn)) && | 
|---|
| 2057 | (def_acl = directory_has_default_acl(conn, parent_dir))) { | 
|---|
| 2058 | unx_mode = (0777 & lp_create_mask(SNUM(conn))); | 
|---|
| 2059 | } | 
|---|
| 2060 |  | 
|---|
| 2061 | DEBUG(4,("calling open_file with flags=0x%X flags2=0x%X mode=0%o, " | 
|---|
| 2062 | "access_mask = 0x%x, open_access_mask = 0x%x\n", | 
|---|
| 2063 | (unsigned int)flags, (unsigned int)flags2, | 
|---|
| 2064 | (unsigned int)unx_mode, (unsigned int)access_mask, | 
|---|
| 2065 | (unsigned int)open_access_mask)); | 
|---|
| 2066 |  | 
|---|
| 2067 | /* | 
|---|
| 2068 | * open_file strips any O_TRUNC flags itself. | 
|---|
| 2069 | */ | 
|---|
| 2070 |  | 
|---|
| 2071 | fsp_open = open_file(fsp, conn, req, parent_dir, | 
|---|
| 2072 | flags|flags2, unx_mode, access_mask, | 
|---|
| 2073 | open_access_mask); | 
|---|
| 2074 |  | 
|---|
| 2075 | if (!NT_STATUS_IS_OK(fsp_open)) { | 
|---|
| 2076 | if (lck != NULL) { | 
|---|
| 2077 | TALLOC_FREE(lck); | 
|---|
| 2078 | } | 
|---|
| 2079 | return fsp_open; | 
|---|
| 2080 | } | 
|---|
| 2081 |  | 
|---|
| 2082 | if (!file_existed) { | 
|---|
| 2083 | struct byte_range_lock *br_lck = NULL; | 
|---|
| 2084 | struct share_mode_entry *batch_entry = NULL; | 
|---|
| 2085 | struct share_mode_entry *exclusive_entry = NULL; | 
|---|
| 2086 | bool got_level2_oplock = false; | 
|---|
| 2087 | bool got_a_none_oplock = false; | 
|---|
| 2088 | struct timespec old_write_time = smb_fname->st.st_ex_mtime; | 
|---|
| 2089 | /* | 
|---|
| 2090 | * Deal with the race condition where two smbd's detect the | 
|---|
| 2091 | * file doesn't exist and do the create at the same time. One | 
|---|
| 2092 | * of them will win and set a share mode, the other (ie. this | 
|---|
| 2093 | * one) should check if the requested share mode for this | 
|---|
| 2094 | * create is allowed. | 
|---|
| 2095 | */ | 
|---|
| 2096 |  | 
|---|
| 2097 | /* | 
|---|
| 2098 | * Now the file exists and fsp is successfully opened, | 
|---|
| 2099 | * fsp->dev and fsp->inode are valid and should replace the | 
|---|
| 2100 | * dev=0,inode=0 from a non existent file. Spotted by | 
|---|
| 2101 | * Nadav Danieli <nadavd@exanet.com>. JRA. | 
|---|
| 2102 | */ | 
|---|
| 2103 |  | 
|---|
| 2104 | id = fsp->file_id; | 
|---|
| 2105 |  | 
|---|
| 2106 | if (!acquire_ordered_locks(talloc_tos(), | 
|---|
| 2107 | fsp, | 
|---|
| 2108 | id, | 
|---|
| 2109 | conn->connectpath, | 
|---|
| 2110 | smb_fname, | 
|---|
| 2111 | &old_write_time, | 
|---|
| 2112 | &lck, | 
|---|
| 2113 | &br_lck)) { | 
|---|
| 2114 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2115 | } | 
|---|
| 2116 |  | 
|---|
| 2117 | /* Get the types we need to examine. */ | 
|---|
| 2118 | find_oplock_types(fsp, | 
|---|
| 2119 | oplock_request, | 
|---|
| 2120 | lck, | 
|---|
| 2121 | &batch_entry, | 
|---|
| 2122 | &exclusive_entry, | 
|---|
| 2123 | &got_level2_oplock, | 
|---|
| 2124 | &got_a_none_oplock); | 
|---|
| 2125 |  | 
|---|
| 2126 | /* First pass - send break only on batch oplocks. */ | 
|---|
| 2127 | if ((req != NULL) && | 
|---|
| 2128 | delay_for_batch_oplocks(fsp, | 
|---|
| 2129 | req->mid, | 
|---|
| 2130 | oplock_request, | 
|---|
| 2131 | batch_entry)) { | 
|---|
| 2132 | schedule_defer_open(lck, request_time, req); | 
|---|
| 2133 | TALLOC_FREE(lck); | 
|---|
| 2134 | fd_close(fsp); | 
|---|
| 2135 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2136 | } | 
|---|
| 2137 |  | 
|---|
| 2138 | status = open_mode_check(conn, lck, fsp->name_hash, | 
|---|
| 2139 | access_mask, share_access, | 
|---|
| 2140 | create_options, &file_existed); | 
|---|
| 2141 |  | 
|---|
| 2142 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 2143 | /* We might be going to allow this open. Check oplock | 
|---|
| 2144 | * status again. */ | 
|---|
| 2145 | /* Second pass - send break for both batch or | 
|---|
| 2146 | * exclusive oplocks. */ | 
|---|
| 2147 | if ((req != NULL) && | 
|---|
| 2148 | delay_for_exclusive_oplocks( | 
|---|
| 2149 | fsp, | 
|---|
| 2150 | req->mid, | 
|---|
| 2151 | oplock_request, | 
|---|
| 2152 | exclusive_entry)) { | 
|---|
| 2153 | schedule_defer_open(lck, request_time, req); | 
|---|
| 2154 | TALLOC_FREE(lck); | 
|---|
| 2155 | fd_close(fsp); | 
|---|
| 2156 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2157 | } | 
|---|
| 2158 | } | 
|---|
| 2159 |  | 
|---|
| 2160 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2161 | struct deferred_open_record state; | 
|---|
| 2162 |  | 
|---|
| 2163 | fd_close(fsp); | 
|---|
| 2164 |  | 
|---|
| 2165 | state.delayed_for_oplocks = False; | 
|---|
| 2166 | state.id = id; | 
|---|
| 2167 |  | 
|---|
| 2168 | /* Do it all over again immediately. In the second | 
|---|
| 2169 | * round we will find that the file existed and handle | 
|---|
| 2170 | * the DELETE_PENDING and FCB cases correctly. No need | 
|---|
| 2171 | * to duplicate the code here. Essentially this is a | 
|---|
| 2172 | * "goto top of this function", but don't tell | 
|---|
| 2173 | * anybody... */ | 
|---|
| 2174 |  | 
|---|
| 2175 | if (req != NULL) { | 
|---|
| 2176 | defer_open(lck, request_time, timeval_zero(), | 
|---|
| 2177 | req, &state); | 
|---|
| 2178 | } | 
|---|
| 2179 | TALLOC_FREE(lck); | 
|---|
| 2180 | return status; | 
|---|
| 2181 | } | 
|---|
| 2182 |  | 
|---|
| 2183 | grant_fsp_oplock_type(fsp, | 
|---|
| 2184 | br_lck, | 
|---|
| 2185 | oplock_request, | 
|---|
| 2186 | got_level2_oplock, | 
|---|
| 2187 | got_a_none_oplock); | 
|---|
| 2188 |  | 
|---|
| 2189 | /* | 
|---|
| 2190 | * We exit this block with the share entry *locked*..... | 
|---|
| 2191 | */ | 
|---|
| 2192 |  | 
|---|
| 2193 | } | 
|---|
| 2194 |  | 
|---|
| 2195 | SMB_ASSERT(lck != NULL); | 
|---|
| 2196 |  | 
|---|
| 2197 | /* Delete streams if create_disposition requires it */ | 
|---|
| 2198 | if (file_existed && clear_ads && | 
|---|
| 2199 | !is_ntfs_stream_smb_fname(smb_fname)) { | 
|---|
| 2200 | status = delete_all_streams(conn, smb_fname->base_name); | 
|---|
| 2201 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2202 | TALLOC_FREE(lck); | 
|---|
| 2203 | fd_close(fsp); | 
|---|
| 2204 | return status; | 
|---|
| 2205 | } | 
|---|
| 2206 | } | 
|---|
| 2207 |  | 
|---|
| 2208 | /* note that we ignore failure for the following. It is | 
|---|
| 2209 | basically a hack for NFS, and NFS will never set one of | 
|---|
| 2210 | these only read them. Nobody but Samba can ever set a deny | 
|---|
| 2211 | mode and we have already checked our more authoritative | 
|---|
| 2212 | locking database for permission to set this deny mode. If | 
|---|
| 2213 | the kernel refuses the operations then the kernel is wrong. | 
|---|
| 2214 | note that GPFS supports it as well - jmcd */ | 
|---|
| 2215 |  | 
|---|
| 2216 | if (fsp->fh->fd != -1) { | 
|---|
| 2217 | int ret_flock; | 
|---|
| 2218 | ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask); | 
|---|
| 2219 | if(ret_flock == -1 ){ | 
|---|
| 2220 |  | 
|---|
| 2221 | TALLOC_FREE(lck); | 
|---|
| 2222 | fd_close(fsp); | 
|---|
| 2223 |  | 
|---|
| 2224 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2225 | } | 
|---|
| 2226 | } | 
|---|
| 2227 |  | 
|---|
| 2228 | /* | 
|---|
| 2229 | * At this point onwards, we can guarentee that the share entry | 
|---|
| 2230 | * is locked, whether we created the file or not, and that the | 
|---|
| 2231 | * deny mode is compatible with all current opens. | 
|---|
| 2232 | */ | 
|---|
| 2233 |  | 
|---|
| 2234 | /* | 
|---|
| 2235 | * If requested, truncate the file. | 
|---|
| 2236 | */ | 
|---|
| 2237 |  | 
|---|
| 2238 | if (file_existed && (flags2&O_TRUNC)) { | 
|---|
| 2239 | /* | 
|---|
| 2240 | * We are modifing the file after open - update the stat | 
|---|
| 2241 | * struct.. | 
|---|
| 2242 | */ | 
|---|
| 2243 | if ((SMB_VFS_FTRUNCATE(fsp, 0) == -1) || | 
|---|
| 2244 | (SMB_VFS_FSTAT(fsp, &smb_fname->st)==-1)) { | 
|---|
| 2245 | status = map_nt_error_from_unix(errno); | 
|---|
| 2246 | TALLOC_FREE(lck); | 
|---|
| 2247 | fd_close(fsp); | 
|---|
| 2248 | return status; | 
|---|
| 2249 | } | 
|---|
| 2250 | } | 
|---|
| 2251 |  | 
|---|
| 2252 | /* | 
|---|
| 2253 | * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted, | 
|---|
| 2254 | * but we don't have to store this - just ignore it on access check. | 
|---|
| 2255 | */ | 
|---|
| 2256 | if (conn->sconn->using_smb2) { | 
|---|
| 2257 | /* | 
|---|
| 2258 | * SMB2 doesn't return it (according to Microsoft tests). | 
|---|
| 2259 | * Test Case: TestSuite_ScenarioNo009GrantedAccessTestS0 | 
|---|
| 2260 | * File created with access = 0x7 (Read, Write, Delete) | 
|---|
| 2261 | * Query Info on file returns 0x87 (Read, Write, Delete, Read Attributes) | 
|---|
| 2262 | */ | 
|---|
| 2263 | fsp->access_mask = access_mask; | 
|---|
| 2264 | } else { | 
|---|
| 2265 | /* But SMB1 does. */ | 
|---|
| 2266 | fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES; | 
|---|
| 2267 | } | 
|---|
| 2268 |  | 
|---|
| 2269 | if (file_existed) { | 
|---|
| 2270 | /* stat opens on existing files don't get oplocks. */ | 
|---|
| 2271 | if (is_stat_open(open_access_mask)) { | 
|---|
| 2272 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 2273 | } | 
|---|
| 2274 |  | 
|---|
| 2275 | if (!(flags2 & O_TRUNC)) { | 
|---|
| 2276 | info = FILE_WAS_OPENED; | 
|---|
| 2277 | } else { | 
|---|
| 2278 | info = FILE_WAS_OVERWRITTEN; | 
|---|
| 2279 | } | 
|---|
| 2280 | } else { | 
|---|
| 2281 | info = FILE_WAS_CREATED; | 
|---|
| 2282 | } | 
|---|
| 2283 |  | 
|---|
| 2284 | if (pinfo) { | 
|---|
| 2285 | *pinfo = info; | 
|---|
| 2286 | } | 
|---|
| 2287 |  | 
|---|
| 2288 | /* | 
|---|
| 2289 | * Setup the oplock info in both the shared memory and | 
|---|
| 2290 | * file structs. | 
|---|
| 2291 | */ | 
|---|
| 2292 |  | 
|---|
| 2293 | if (!set_file_oplock(fsp, fsp->oplock_type)) { | 
|---|
| 2294 | /* | 
|---|
| 2295 | * Could not get the kernel oplock or there are byte-range | 
|---|
| 2296 | * locks on the file. | 
|---|
| 2297 | */ | 
|---|
| 2298 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 2299 | } | 
|---|
| 2300 |  | 
|---|
| 2301 | if (info == FILE_WAS_OVERWRITTEN || info == FILE_WAS_CREATED || info == FILE_WAS_SUPERSEDED) { | 
|---|
| 2302 | new_file_created = True; | 
|---|
| 2303 | } | 
|---|
| 2304 |  | 
|---|
| 2305 | set_share_mode(lck, fsp, get_current_uid(conn), | 
|---|
| 2306 | req ? req->mid : 0, | 
|---|
| 2307 | fsp->oplock_type); | 
|---|
| 2308 |  | 
|---|
| 2309 | /* Handle strange delete on close create semantics. */ | 
|---|
| 2310 | if (create_options & FILE_DELETE_ON_CLOSE) { | 
|---|
| 2311 |  | 
|---|
| 2312 | status = can_set_delete_on_close(fsp, new_dos_attributes); | 
|---|
| 2313 |  | 
|---|
| 2314 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2315 | /* Remember to delete the mode we just added. */ | 
|---|
| 2316 | del_share_mode(lck, fsp); | 
|---|
| 2317 | TALLOC_FREE(lck); | 
|---|
| 2318 | fd_close(fsp); | 
|---|
| 2319 | return status; | 
|---|
| 2320 | } | 
|---|
| 2321 | /* Note that here we set the *inital* delete on close flag, | 
|---|
| 2322 | not the regular one. The magic gets handled in close. */ | 
|---|
| 2323 | fsp->initial_delete_on_close = True; | 
|---|
| 2324 | } | 
|---|
| 2325 |  | 
|---|
| 2326 | if (new_file_created) { | 
|---|
| 2327 | /* Files should be initially set as archive */ | 
|---|
| 2328 | if (lp_map_archive(SNUM(conn)) || | 
|---|
| 2329 | lp_store_dos_attributes(SNUM(conn))) { | 
|---|
| 2330 | if (!posix_open) { | 
|---|
| 2331 | if (file_set_dosmode(conn, smb_fname, | 
|---|
| 2332 | new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE, | 
|---|
| 2333 | parent_dir, true) == 0) { | 
|---|
| 2334 | unx_mode = smb_fname->st.st_ex_mode; | 
|---|
| 2335 | } | 
|---|
| 2336 | } | 
|---|
| 2337 | } | 
|---|
| 2338 | } | 
|---|
| 2339 |  | 
|---|
| 2340 | /* Determine sparse flag. */ | 
|---|
| 2341 | if (posix_open) { | 
|---|
| 2342 | /* POSIX opens are sparse by default. */ | 
|---|
| 2343 | fsp->is_sparse = true; | 
|---|
| 2344 | } else { | 
|---|
| 2345 | fsp->is_sparse = (file_existed && | 
|---|
| 2346 | (existing_dos_attributes & FILE_ATTRIBUTE_SPARSE)); | 
|---|
| 2347 | } | 
|---|
| 2348 |  | 
|---|
| 2349 | /* | 
|---|
| 2350 | * Take care of inherited ACLs on created files - if default ACL not | 
|---|
| 2351 | * selected. | 
|---|
| 2352 | */ | 
|---|
| 2353 |  | 
|---|
| 2354 | if (!posix_open && !file_existed && !def_acl) { | 
|---|
| 2355 |  | 
|---|
| 2356 | int saved_errno = errno; /* We might get ENOSYS in the next | 
|---|
| 2357 | * call.. */ | 
|---|
| 2358 |  | 
|---|
| 2359 | if (SMB_VFS_FCHMOD_ACL(fsp, unx_mode) == -1 && | 
|---|
| 2360 | errno == ENOSYS) { | 
|---|
| 2361 | errno = saved_errno; /* Ignore ENOSYS */ | 
|---|
| 2362 | } | 
|---|
| 2363 |  | 
|---|
| 2364 | } else if (new_unx_mode) { | 
|---|
| 2365 |  | 
|---|
| 2366 | int ret = -1; | 
|---|
| 2367 |  | 
|---|
| 2368 | /* Attributes need changing. File already existed. */ | 
|---|
| 2369 |  | 
|---|
| 2370 | { | 
|---|
| 2371 | int saved_errno = errno; /* We might get ENOSYS in the | 
|---|
| 2372 | * next call.. */ | 
|---|
| 2373 | ret = SMB_VFS_FCHMOD_ACL(fsp, new_unx_mode); | 
|---|
| 2374 |  | 
|---|
| 2375 | if (ret == -1 && errno == ENOSYS) { | 
|---|
| 2376 | errno = saved_errno; /* Ignore ENOSYS */ | 
|---|
| 2377 | } else { | 
|---|
| 2378 | DEBUG(5, ("open_file_ntcreate: reset " | 
|---|
| 2379 | "attributes of file %s to 0%o\n", | 
|---|
| 2380 | smb_fname_str_dbg(smb_fname), | 
|---|
| 2381 | (unsigned int)new_unx_mode)); | 
|---|
| 2382 | ret = 0; /* Don't do the fchmod below. */ | 
|---|
| 2383 | } | 
|---|
| 2384 | } | 
|---|
| 2385 |  | 
|---|
| 2386 | if ((ret == -1) && | 
|---|
| 2387 | (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1)) | 
|---|
| 2388 | DEBUG(5, ("open_file_ntcreate: failed to reset " | 
|---|
| 2389 | "attributes of file %s to 0%o\n", | 
|---|
| 2390 | smb_fname_str_dbg(smb_fname), | 
|---|
| 2391 | (unsigned int)new_unx_mode)); | 
|---|
| 2392 | } | 
|---|
| 2393 |  | 
|---|
| 2394 | /* If this is a successful open, we must remove any deferred open | 
|---|
| 2395 | * records. */ | 
|---|
| 2396 | if (req != NULL) { | 
|---|
| 2397 | del_deferred_open_entry(lck, req->mid, | 
|---|
| 2398 | sconn_server_id(req->sconn)); | 
|---|
| 2399 | } | 
|---|
| 2400 | TALLOC_FREE(lck); | 
|---|
| 2401 |  | 
|---|
| 2402 | return NT_STATUS_OK; | 
|---|
| 2403 | } | 
|---|
| 2404 |  | 
|---|
| 2405 |  | 
|---|
| 2406 | /**************************************************************************** | 
|---|
| 2407 | Open a file for for write to ensure that we can fchmod it. | 
|---|
| 2408 | ****************************************************************************/ | 
|---|
| 2409 |  | 
|---|
| 2410 | NTSTATUS open_file_fchmod(connection_struct *conn, | 
|---|
| 2411 | struct smb_filename *smb_fname, | 
|---|
| 2412 | files_struct **result) | 
|---|
| 2413 | { | 
|---|
| 2414 | if (!VALID_STAT(smb_fname->st)) { | 
|---|
| 2415 | return NT_STATUS_INVALID_PARAMETER; | 
|---|
| 2416 | } | 
|---|
| 2417 |  | 
|---|
| 2418 | return SMB_VFS_CREATE_FILE( | 
|---|
| 2419 | conn,                                   /* conn */ | 
|---|
| 2420 | NULL,                                   /* req */ | 
|---|
| 2421 | 0,                                      /* root_dir_fid */ | 
|---|
| 2422 | smb_fname,                              /* fname */ | 
|---|
| 2423 | FILE_WRITE_DATA,                        /* access_mask */ | 
|---|
| 2424 | (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */ | 
|---|
| 2425 | FILE_SHARE_DELETE), | 
|---|
| 2426 | FILE_OPEN,                              /* create_disposition*/ | 
|---|
| 2427 | 0,                                      /* create_options */ | 
|---|
| 2428 | 0,                                      /* file_attributes */ | 
|---|
| 2429 | INTERNAL_OPEN_ONLY,                     /* oplock_request */ | 
|---|
| 2430 | 0,                                      /* allocation_size */ | 
|---|
| 2431 | 0,                                      /* private_flags */ | 
|---|
| 2432 | NULL,                                   /* sd */ | 
|---|
| 2433 | NULL,                                   /* ea_list */ | 
|---|
| 2434 | result,                                 /* result */ | 
|---|
| 2435 | NULL);                                  /* pinfo */ | 
|---|
| 2436 | } | 
|---|
| 2437 |  | 
|---|
| 2438 | static NTSTATUS mkdir_internal(connection_struct *conn, | 
|---|
| 2439 | struct smb_filename *smb_dname, | 
|---|
| 2440 | uint32 file_attributes) | 
|---|
| 2441 | { | 
|---|
| 2442 | mode_t mode; | 
|---|
| 2443 | char *parent_dir; | 
|---|
| 2444 | NTSTATUS status; | 
|---|
| 2445 | bool posix_open = false; | 
|---|
| 2446 | bool need_re_stat = false; | 
|---|
| 2447 |  | 
|---|
| 2448 | if(!CAN_WRITE(conn)) { | 
|---|
| 2449 | DEBUG(5,("mkdir_internal: failing create on read-only share " | 
|---|
| 2450 | "%s\n", lp_servicename(SNUM(conn)))); | 
|---|
| 2451 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 2452 | } | 
|---|
| 2453 |  | 
|---|
| 2454 | status = check_name(conn, smb_dname->base_name); | 
|---|
| 2455 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2456 | return status; | 
|---|
| 2457 | } | 
|---|
| 2458 |  | 
|---|
| 2459 | if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir, | 
|---|
| 2460 | NULL)) { | 
|---|
| 2461 | return NT_STATUS_NO_MEMORY; | 
|---|
| 2462 | } | 
|---|
| 2463 |  | 
|---|
| 2464 | if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) { | 
|---|
| 2465 | posix_open = true; | 
|---|
| 2466 | mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS); | 
|---|
| 2467 | } else { | 
|---|
| 2468 | mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir); | 
|---|
| 2469 | } | 
|---|
| 2470 |  | 
|---|
| 2471 | if (SMB_VFS_MKDIR(conn, smb_dname->base_name, mode) != 0) { | 
|---|
| 2472 | return map_nt_error_from_unix(errno); | 
|---|
| 2473 | } | 
|---|
| 2474 |  | 
|---|
| 2475 | /* Ensure we're checking for a symlink here.... */ | 
|---|
| 2476 | /* We don't want to get caught by a symlink racer. */ | 
|---|
| 2477 |  | 
|---|
| 2478 | if (SMB_VFS_LSTAT(conn, smb_dname) == -1) { | 
|---|
| 2479 | DEBUG(2, ("Could not stat directory '%s' just created: %s\n", | 
|---|
| 2480 | smb_fname_str_dbg(smb_dname), strerror(errno))); | 
|---|
| 2481 | return map_nt_error_from_unix(errno); | 
|---|
| 2482 | } | 
|---|
| 2483 |  | 
|---|
| 2484 | if (!S_ISDIR(smb_dname->st.st_ex_mode)) { | 
|---|
| 2485 | DEBUG(0, ("Directory just '%s' created is not a directory\n", | 
|---|
| 2486 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2487 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 2488 | } | 
|---|
| 2489 |  | 
|---|
| 2490 | if (lp_store_dos_attributes(SNUM(conn))) { | 
|---|
| 2491 | if (!posix_open) { | 
|---|
| 2492 | file_set_dosmode(conn, smb_dname, | 
|---|
| 2493 | file_attributes | FILE_ATTRIBUTE_DIRECTORY, | 
|---|
| 2494 | parent_dir, true); | 
|---|
| 2495 | } | 
|---|
| 2496 | } | 
|---|
| 2497 |  | 
|---|
| 2498 | if (lp_inherit_perms(SNUM(conn))) { | 
|---|
| 2499 | inherit_access_posix_acl(conn, parent_dir, | 
|---|
| 2500 | smb_dname->base_name, mode); | 
|---|
| 2501 | need_re_stat = true; | 
|---|
| 2502 | } | 
|---|
| 2503 |  | 
|---|
| 2504 | if (!posix_open) { | 
|---|
| 2505 | /* | 
|---|
| 2506 | * Check if high bits should have been set, | 
|---|
| 2507 | * then (if bits are missing): add them. | 
|---|
| 2508 | * Consider bits automagically set by UNIX, i.e. SGID bit from parent | 
|---|
| 2509 | * dir. | 
|---|
| 2510 | */ | 
|---|
| 2511 | if ((mode & ~(S_IRWXU|S_IRWXG|S_IRWXO)) && | 
|---|
| 2512 | (mode & ~smb_dname->st.st_ex_mode)) { | 
|---|
| 2513 | SMB_VFS_CHMOD(conn, smb_dname->base_name, | 
|---|
| 2514 | (smb_dname->st.st_ex_mode | | 
|---|
| 2515 | (mode & ~smb_dname->st.st_ex_mode))); | 
|---|
| 2516 | need_re_stat = true; | 
|---|
| 2517 | } | 
|---|
| 2518 | } | 
|---|
| 2519 |  | 
|---|
| 2520 | /* Change the owner if required. */ | 
|---|
| 2521 | if (lp_inherit_owner(SNUM(conn))) { | 
|---|
| 2522 | change_dir_owner_to_parent(conn, parent_dir, | 
|---|
| 2523 | smb_dname->base_name, | 
|---|
| 2524 | &smb_dname->st); | 
|---|
| 2525 | need_re_stat = true; | 
|---|
| 2526 | } | 
|---|
| 2527 |  | 
|---|
| 2528 | if (need_re_stat) { | 
|---|
| 2529 | if (SMB_VFS_LSTAT(conn, smb_dname) == -1) { | 
|---|
| 2530 | DEBUG(2, ("Could not stat directory '%s' just created: %s\n", | 
|---|
| 2531 | smb_fname_str_dbg(smb_dname), strerror(errno))); | 
|---|
| 2532 | return map_nt_error_from_unix(errno); | 
|---|
| 2533 | } | 
|---|
| 2534 | } | 
|---|
| 2535 |  | 
|---|
| 2536 | notify_fname(conn, NOTIFY_ACTION_ADDED, FILE_NOTIFY_CHANGE_DIR_NAME, | 
|---|
| 2537 | smb_dname->base_name); | 
|---|
| 2538 |  | 
|---|
| 2539 | return NT_STATUS_OK; | 
|---|
| 2540 | } | 
|---|
| 2541 |  | 
|---|
| 2542 | /**************************************************************************** | 
|---|
| 2543 | Ensure we didn't get symlink raced on opening a directory. | 
|---|
| 2544 | ****************************************************************************/ | 
|---|
| 2545 |  | 
|---|
| 2546 | bool check_same_stat(const SMB_STRUCT_STAT *sbuf1, | 
|---|
| 2547 | const SMB_STRUCT_STAT *sbuf2) | 
|---|
| 2548 | { | 
|---|
| 2549 | if (sbuf1->st_ex_uid != sbuf2->st_ex_uid || | 
|---|
| 2550 | sbuf1->st_ex_gid != sbuf2->st_ex_gid || | 
|---|
| 2551 | sbuf1->st_ex_dev != sbuf2->st_ex_dev || | 
|---|
| 2552 | sbuf1->st_ex_ino != sbuf2->st_ex_ino) { | 
|---|
| 2553 | return false; | 
|---|
| 2554 | } | 
|---|
| 2555 | return true; | 
|---|
| 2556 | } | 
|---|
| 2557 |  | 
|---|
| 2558 | /**************************************************************************** | 
|---|
| 2559 | Open a directory from an NT SMB call. | 
|---|
| 2560 | ****************************************************************************/ | 
|---|
| 2561 |  | 
|---|
| 2562 | static NTSTATUS open_directory(connection_struct *conn, | 
|---|
| 2563 | struct smb_request *req, | 
|---|
| 2564 | struct smb_filename *smb_dname, | 
|---|
| 2565 | uint32 access_mask, | 
|---|
| 2566 | uint32 share_access, | 
|---|
| 2567 | uint32 create_disposition, | 
|---|
| 2568 | uint32 create_options, | 
|---|
| 2569 | uint32 file_attributes, | 
|---|
| 2570 | int *pinfo, | 
|---|
| 2571 | files_struct **result) | 
|---|
| 2572 | { | 
|---|
| 2573 | files_struct *fsp = NULL; | 
|---|
| 2574 | bool dir_existed = VALID_STAT(smb_dname->st) ? True : False; | 
|---|
| 2575 | struct share_mode_lock *lck = NULL; | 
|---|
| 2576 | NTSTATUS status; | 
|---|
| 2577 | struct timespec mtimespec; | 
|---|
| 2578 | int info = 0; | 
|---|
| 2579 |  | 
|---|
| 2580 | SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname)); | 
|---|
| 2581 |  | 
|---|
| 2582 | if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS)) { | 
|---|
| 2583 | /* Ensure we have a directory attribute. */ | 
|---|
| 2584 | file_attributes |= FILE_ATTRIBUTE_DIRECTORY; | 
|---|
| 2585 | } | 
|---|
| 2586 |  | 
|---|
| 2587 | DEBUG(5,("open_directory: opening directory %s, access_mask = 0x%x, " | 
|---|
| 2588 | "share_access = 0x%x create_options = 0x%x, " | 
|---|
| 2589 | "create_disposition = 0x%x, file_attributes = 0x%x\n", | 
|---|
| 2590 | smb_fname_str_dbg(smb_dname), | 
|---|
| 2591 | (unsigned int)access_mask, | 
|---|
| 2592 | (unsigned int)share_access, | 
|---|
| 2593 | (unsigned int)create_options, | 
|---|
| 2594 | (unsigned int)create_disposition, | 
|---|
| 2595 | (unsigned int)file_attributes)); | 
|---|
| 2596 |  | 
|---|
| 2597 | if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) && | 
|---|
| 2598 | (conn->fs_capabilities & FILE_NAMED_STREAMS) && | 
|---|
| 2599 | is_ntfs_stream_smb_fname(smb_dname)) { | 
|---|
| 2600 | DEBUG(2, ("open_directory: %s is a stream name!\n", | 
|---|
| 2601 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2602 | return NT_STATUS_NOT_A_DIRECTORY; | 
|---|
| 2603 | } | 
|---|
| 2604 |  | 
|---|
| 2605 | status = smbd_calculate_access_mask(conn, smb_dname, dir_existed, | 
|---|
| 2606 | access_mask, &access_mask); | 
|---|
| 2607 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2608 | DEBUG(10, ("open_directory: smbd_calculate_access_mask " | 
|---|
| 2609 | "on file %s returned %s\n", | 
|---|
| 2610 | smb_fname_str_dbg(smb_dname), | 
|---|
| 2611 | nt_errstr(status))); | 
|---|
| 2612 | return status; | 
|---|
| 2613 | } | 
|---|
| 2614 |  | 
|---|
| 2615 | if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) && | 
|---|
| 2616 | !security_token_has_privilege(get_current_nttok(conn), | 
|---|
| 2617 | SEC_PRIV_SECURITY)) { | 
|---|
| 2618 | DEBUG(10, ("open_directory: open on %s " | 
|---|
| 2619 | "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n", | 
|---|
| 2620 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2621 | return NT_STATUS_PRIVILEGE_NOT_HELD; | 
|---|
| 2622 | } | 
|---|
| 2623 |  | 
|---|
| 2624 | switch( create_disposition ) { | 
|---|
| 2625 | case FILE_OPEN: | 
|---|
| 2626 |  | 
|---|
| 2627 | if (!dir_existed) { | 
|---|
| 2628 | return NT_STATUS_OBJECT_NAME_NOT_FOUND; | 
|---|
| 2629 | } | 
|---|
| 2630 |  | 
|---|
| 2631 | info = FILE_WAS_OPENED; | 
|---|
| 2632 | break; | 
|---|
| 2633 |  | 
|---|
| 2634 | case FILE_CREATE: | 
|---|
| 2635 |  | 
|---|
| 2636 | /* If directory exists error. If directory doesn't | 
|---|
| 2637 | * exist create. */ | 
|---|
| 2638 |  | 
|---|
| 2639 | status = mkdir_internal(conn, smb_dname, | 
|---|
| 2640 | file_attributes); | 
|---|
| 2641 |  | 
|---|
| 2642 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2643 | DEBUG(2, ("open_directory: unable to create " | 
|---|
| 2644 | "%s. Error was %s\n", | 
|---|
| 2645 | smb_fname_str_dbg(smb_dname), | 
|---|
| 2646 | nt_errstr(status))); | 
|---|
| 2647 | return status; | 
|---|
| 2648 | } | 
|---|
| 2649 |  | 
|---|
| 2650 | info = FILE_WAS_CREATED; | 
|---|
| 2651 | break; | 
|---|
| 2652 |  | 
|---|
| 2653 | case FILE_OPEN_IF: | 
|---|
| 2654 | /* | 
|---|
| 2655 | * If directory exists open. If directory doesn't | 
|---|
| 2656 | * exist create. | 
|---|
| 2657 | */ | 
|---|
| 2658 |  | 
|---|
| 2659 | status = mkdir_internal(conn, smb_dname, | 
|---|
| 2660 | file_attributes); | 
|---|
| 2661 |  | 
|---|
| 2662 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 2663 | info = FILE_WAS_CREATED; | 
|---|
| 2664 | } | 
|---|
| 2665 |  | 
|---|
| 2666 | if (NT_STATUS_EQUAL(status, | 
|---|
| 2667 | NT_STATUS_OBJECT_NAME_COLLISION)) { | 
|---|
| 2668 | info = FILE_WAS_OPENED; | 
|---|
| 2669 | status = NT_STATUS_OK; | 
|---|
| 2670 | } | 
|---|
| 2671 | break; | 
|---|
| 2672 |  | 
|---|
| 2673 | case FILE_SUPERSEDE: | 
|---|
| 2674 | case FILE_OVERWRITE: | 
|---|
| 2675 | case FILE_OVERWRITE_IF: | 
|---|
| 2676 | default: | 
|---|
| 2677 | DEBUG(5,("open_directory: invalid create_disposition " | 
|---|
| 2678 | "0x%x for directory %s\n", | 
|---|
| 2679 | (unsigned int)create_disposition, | 
|---|
| 2680 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2681 | return NT_STATUS_INVALID_PARAMETER; | 
|---|
| 2682 | } | 
|---|
| 2683 |  | 
|---|
| 2684 | if(!S_ISDIR(smb_dname->st.st_ex_mode)) { | 
|---|
| 2685 | DEBUG(5,("open_directory: %s is not a directory !\n", | 
|---|
| 2686 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2687 | return NT_STATUS_NOT_A_DIRECTORY; | 
|---|
| 2688 | } | 
|---|
| 2689 |  | 
|---|
| 2690 | if (info == FILE_WAS_OPENED) { | 
|---|
| 2691 | uint32_t access_granted = 0; | 
|---|
| 2692 | status = smbd_check_open_rights(conn, smb_dname, access_mask, | 
|---|
| 2693 | &access_granted); | 
|---|
| 2694 |  | 
|---|
| 2695 | /* Were we trying to do a directory open | 
|---|
| 2696 | * for delete and didn't get DELETE | 
|---|
| 2697 | * access (only) ? Check if the | 
|---|
| 2698 | * directory allows DELETE_CHILD. | 
|---|
| 2699 | * See here: | 
|---|
| 2700 | * http://blogs.msdn.com/oldnewthing/archive/2004/06/04/148426.aspx | 
|---|
| 2701 | * for details. */ | 
|---|
| 2702 |  | 
|---|
| 2703 | if ((NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) && | 
|---|
| 2704 | (access_mask & DELETE_ACCESS) && | 
|---|
| 2705 | (access_granted == DELETE_ACCESS) && | 
|---|
| 2706 | can_delete_file_in_directory(conn, smb_dname))) { | 
|---|
| 2707 | DEBUG(10,("open_directory: overrode ACCESS_DENIED " | 
|---|
| 2708 | "on directory %s\n", | 
|---|
| 2709 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2710 | status = NT_STATUS_OK; | 
|---|
| 2711 | } | 
|---|
| 2712 |  | 
|---|
| 2713 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2714 | DEBUG(10, ("open_directory: smbd_check_open_rights on " | 
|---|
| 2715 | "file %s failed with %s\n", | 
|---|
| 2716 | smb_fname_str_dbg(smb_dname), | 
|---|
| 2717 | nt_errstr(status))); | 
|---|
| 2718 | return status; | 
|---|
| 2719 | } | 
|---|
| 2720 | } | 
|---|
| 2721 |  | 
|---|
| 2722 | status = file_new(req, conn, &fsp); | 
|---|
| 2723 | if(!NT_STATUS_IS_OK(status)) { | 
|---|
| 2724 | return status; | 
|---|
| 2725 | } | 
|---|
| 2726 |  | 
|---|
| 2727 | /* | 
|---|
| 2728 | * Setup the files_struct for it. | 
|---|
| 2729 | */ | 
|---|
| 2730 |  | 
|---|
| 2731 | fsp->mode = smb_dname->st.st_ex_mode; | 
|---|
| 2732 | fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st); | 
|---|
| 2733 | fsp->vuid = req ? req->vuid : UID_FIELD_INVALID; | 
|---|
| 2734 | fsp->file_pid = req ? req->smbpid : 0; | 
|---|
| 2735 | fsp->can_lock = False; | 
|---|
| 2736 | fsp->can_read = False; | 
|---|
| 2737 | fsp->can_write = False; | 
|---|
| 2738 |  | 
|---|
| 2739 | fsp->share_access = share_access; | 
|---|
| 2740 | fsp->fh->private_options = 0; | 
|---|
| 2741 | fsp->print_file = NULL; | 
|---|
| 2742 | fsp->modified = False; | 
|---|
| 2743 | fsp->oplock_type = NO_OPLOCK; | 
|---|
| 2744 | fsp->sent_oplock_break = NO_BREAK_SENT; | 
|---|
| 2745 | fsp->is_directory = True; | 
|---|
| 2746 | fsp->posix_open = (file_attributes & FILE_FLAG_POSIX_SEMANTICS) ? True : False; | 
|---|
| 2747 | status = fsp_set_smb_fname(fsp, smb_dname); | 
|---|
| 2748 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2749 | file_free(req, fsp); | 
|---|
| 2750 | return status; | 
|---|
| 2751 | } | 
|---|
| 2752 |  | 
|---|
| 2753 | mtimespec = smb_dname->st.st_ex_mtime; | 
|---|
| 2754 |  | 
|---|
| 2755 | /* Temporary access mask used to open the directory fd. */ | 
|---|
| 2756 | fsp->access_mask = FILE_READ_DATA | FILE_READ_ATTRIBUTES; | 
|---|
| 2757 | #ifdef O_DIRECTORY | 
|---|
| 2758 | status = fd_open(conn, fsp, O_RDONLY|O_DIRECTORY, 0); | 
|---|
| 2759 | #else | 
|---|
| 2760 | /* POSIX allows us to open a directory with O_RDONLY. */ | 
|---|
| 2761 | status = fd_open(conn, fsp, O_RDONLY, 0); | 
|---|
| 2762 | #endif | 
|---|
| 2763 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2764 | DEBUG(5, ("open_directory: Could not open fd for " | 
|---|
| 2765 | "%s (%s)\n", | 
|---|
| 2766 | smb_fname_str_dbg(smb_dname), | 
|---|
| 2767 | nt_errstr(status))); | 
|---|
| 2768 | file_free(req, fsp); | 
|---|
| 2769 | return status; | 
|---|
| 2770 | } | 
|---|
| 2771 |  | 
|---|
| 2772 | /* | 
|---|
| 2773 | * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted, | 
|---|
| 2774 | * Set the real access mask for later access (possibly delete). | 
|---|
| 2775 | */ | 
|---|
| 2776 | fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES; | 
|---|
| 2777 |  | 
|---|
| 2778 | status = vfs_stat_fsp(fsp); | 
|---|
| 2779 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2780 | fd_close(fsp); | 
|---|
| 2781 | file_free(req, fsp); | 
|---|
| 2782 | return status; | 
|---|
| 2783 | } | 
|---|
| 2784 |  | 
|---|
| 2785 | /* Ensure there was no race condition. */ | 
|---|
| 2786 | if (!check_same_stat(&smb_dname->st, &fsp->fsp_name->st)) { | 
|---|
| 2787 | DEBUG(5,("open_directory: stat struct differs for " | 
|---|
| 2788 | "directory %s.\n", | 
|---|
| 2789 | smb_fname_str_dbg(smb_dname))); | 
|---|
| 2790 | fd_close(fsp); | 
|---|
| 2791 | file_free(req, fsp); | 
|---|
| 2792 | return NT_STATUS_ACCESS_DENIED; | 
|---|
| 2793 | } | 
|---|
| 2794 |  | 
|---|
| 2795 | lck = get_share_mode_lock(talloc_tos(), fsp->file_id, | 
|---|
| 2796 | conn->connectpath, smb_dname, &mtimespec); | 
|---|
| 2797 |  | 
|---|
| 2798 | if (lck == NULL) { | 
|---|
| 2799 | DEBUG(0, ("open_directory: Could not get share mode lock for " | 
|---|
| 2800 | "%s\n", smb_fname_str_dbg(smb_dname))); | 
|---|
| 2801 | fd_close(fsp); | 
|---|
| 2802 | file_free(req, fsp); | 
|---|
| 2803 | return NT_STATUS_SHARING_VIOLATION; | 
|---|
| 2804 | } | 
|---|
| 2805 |  | 
|---|
| 2806 | status = open_mode_check(conn, lck, fsp->name_hash, | 
|---|
| 2807 | access_mask, share_access, | 
|---|
| 2808 | create_options, &dir_existed); | 
|---|
| 2809 |  | 
|---|
| 2810 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2811 | TALLOC_FREE(lck); | 
|---|
| 2812 | fd_close(fsp); | 
|---|
| 2813 | file_free(req, fsp); | 
|---|
| 2814 | return status; | 
|---|
| 2815 | } | 
|---|
| 2816 |  | 
|---|
| 2817 | set_share_mode(lck, fsp, get_current_uid(conn), | 
|---|
| 2818 | req ? req->mid : 0, NO_OPLOCK); | 
|---|
| 2819 |  | 
|---|
| 2820 | /* For directories the delete on close bit at open time seems | 
|---|
| 2821 | always to be honored on close... See test 19 in Samba4 BASE-DELETE. */ | 
|---|
| 2822 | if (create_options & FILE_DELETE_ON_CLOSE) { | 
|---|
| 2823 | status = can_set_delete_on_close(fsp, 0); | 
|---|
| 2824 | if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) { | 
|---|
| 2825 | TALLOC_FREE(lck); | 
|---|
| 2826 | fd_close(fsp); | 
|---|
| 2827 | file_free(req, fsp); | 
|---|
| 2828 | return status; | 
|---|
| 2829 | } | 
|---|
| 2830 |  | 
|---|
| 2831 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 2832 | /* Note that here we set the *inital* delete on close flag, | 
|---|
| 2833 | not the regular one. The magic gets handled in close. */ | 
|---|
| 2834 | fsp->initial_delete_on_close = True; | 
|---|
| 2835 | } | 
|---|
| 2836 | } | 
|---|
| 2837 |  | 
|---|
| 2838 | TALLOC_FREE(lck); | 
|---|
| 2839 |  | 
|---|
| 2840 | if (pinfo) { | 
|---|
| 2841 | *pinfo = info; | 
|---|
| 2842 | } | 
|---|
| 2843 |  | 
|---|
| 2844 | *result = fsp; | 
|---|
| 2845 | return NT_STATUS_OK; | 
|---|
| 2846 | } | 
|---|
| 2847 |  | 
|---|
| 2848 | NTSTATUS create_directory(connection_struct *conn, struct smb_request *req, | 
|---|
| 2849 | struct smb_filename *smb_dname) | 
|---|
| 2850 | { | 
|---|
| 2851 | NTSTATUS status; | 
|---|
| 2852 | files_struct *fsp; | 
|---|
| 2853 |  | 
|---|
| 2854 | status = SMB_VFS_CREATE_FILE( | 
|---|
| 2855 | conn,                                   /* conn */ | 
|---|
| 2856 | req,                                    /* req */ | 
|---|
| 2857 | 0,                                      /* root_dir_fid */ | 
|---|
| 2858 | smb_dname,                              /* fname */ | 
|---|
| 2859 | FILE_READ_ATTRIBUTES,                   /* access_mask */ | 
|---|
| 2860 | FILE_SHARE_NONE,                        /* share_access */ | 
|---|
| 2861 | FILE_CREATE,                            /* create_disposition*/ | 
|---|
| 2862 | FILE_DIRECTORY_FILE,                    /* create_options */ | 
|---|
| 2863 | FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */ | 
|---|
| 2864 | 0,                                      /* oplock_request */ | 
|---|
| 2865 | 0,                                      /* allocation_size */ | 
|---|
| 2866 | 0,                                      /* private_flags */ | 
|---|
| 2867 | NULL,                                   /* sd */ | 
|---|
| 2868 | NULL,                                   /* ea_list */ | 
|---|
| 2869 | &fsp,                                   /* result */ | 
|---|
| 2870 | NULL);                                  /* pinfo */ | 
|---|
| 2871 |  | 
|---|
| 2872 | if (NT_STATUS_IS_OK(status)) { | 
|---|
| 2873 | close_file(req, fsp, NORMAL_CLOSE); | 
|---|
| 2874 | } | 
|---|
| 2875 |  | 
|---|
| 2876 | return status; | 
|---|
| 2877 | } | 
|---|
| 2878 |  | 
|---|
| 2879 | /**************************************************************************** | 
|---|
| 2880 | Receive notification that one of our open files has been renamed by another | 
|---|
| 2881 | smbd process. | 
|---|
| 2882 | ****************************************************************************/ | 
|---|
| 2883 |  | 
|---|
| 2884 | void msg_file_was_renamed(struct messaging_context *msg, | 
|---|
| 2885 | void *private_data, | 
|---|
| 2886 | uint32_t msg_type, | 
|---|
| 2887 | struct server_id server_id, | 
|---|
| 2888 | DATA_BLOB *data) | 
|---|
| 2889 | { | 
|---|
| 2890 | struct smbd_server_connection *sconn; | 
|---|
| 2891 | files_struct *fsp; | 
|---|
| 2892 | char *frm = (char *)data->data; | 
|---|
| 2893 | struct file_id id; | 
|---|
| 2894 | const char *sharepath; | 
|---|
| 2895 | const char *base_name; | 
|---|
| 2896 | const char *stream_name; | 
|---|
| 2897 | struct smb_filename *smb_fname = NULL; | 
|---|
| 2898 | size_t sp_len, bn_len; | 
|---|
| 2899 | NTSTATUS status; | 
|---|
| 2900 |  | 
|---|
| 2901 | sconn = msg_ctx_to_sconn(msg); | 
|---|
| 2902 | if (sconn == NULL) { | 
|---|
| 2903 | DEBUG(1, ("could not find sconn\n")); | 
|---|
| 2904 | return; | 
|---|
| 2905 | } | 
|---|
| 2906 |  | 
|---|
| 2907 | if (data->data == NULL | 
|---|
| 2908 | || data->length < MSG_FILE_RENAMED_MIN_SIZE + 2) { | 
|---|
| 2909 | DEBUG(0, ("msg_file_was_renamed: Got invalid msg len %d\n", | 
|---|
| 2910 | (int)data->length)); | 
|---|
| 2911 | return; | 
|---|
| 2912 | } | 
|---|
| 2913 |  | 
|---|
| 2914 | /* Unpack the message. */ | 
|---|
| 2915 | pull_file_id_24(frm, &id); | 
|---|
| 2916 | sharepath = &frm[24]; | 
|---|
| 2917 | sp_len = strlen(sharepath); | 
|---|
| 2918 | base_name = sharepath + sp_len + 1; | 
|---|
| 2919 | bn_len = strlen(base_name); | 
|---|
| 2920 | stream_name = sharepath + sp_len + 1 + bn_len + 1; | 
|---|
| 2921 |  | 
|---|
| 2922 | /* stream_name must always be NULL if there is no stream. */ | 
|---|
| 2923 | if (stream_name[0] == '\0') { | 
|---|
| 2924 | stream_name = NULL; | 
|---|
| 2925 | } | 
|---|
| 2926 |  | 
|---|
| 2927 | status = create_synthetic_smb_fname(talloc_tos(), base_name, | 
|---|
| 2928 | stream_name, NULL, &smb_fname); | 
|---|
| 2929 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2930 | return; | 
|---|
| 2931 | } | 
|---|
| 2932 |  | 
|---|
| 2933 | DEBUG(10,("msg_file_was_renamed: Got rename message for sharepath %s, new name %s, " | 
|---|
| 2934 | "file_id %s\n", | 
|---|
| 2935 | sharepath, smb_fname_str_dbg(smb_fname), | 
|---|
| 2936 | file_id_string_tos(&id))); | 
|---|
| 2937 |  | 
|---|
| 2938 | for(fsp = file_find_di_first(sconn, id); fsp; | 
|---|
| 2939 | fsp = file_find_di_next(fsp)) { | 
|---|
| 2940 | if (memcmp(fsp->conn->connectpath, sharepath, sp_len) == 0) { | 
|---|
| 2941 |  | 
|---|
| 2942 | DEBUG(10,("msg_file_was_renamed: renaming file fnum %d from %s -> %s\n", | 
|---|
| 2943 | fsp->fnum, fsp_str_dbg(fsp), | 
|---|
| 2944 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 2945 | status = fsp_set_smb_fname(fsp, smb_fname); | 
|---|
| 2946 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2947 | goto out; | 
|---|
| 2948 | } | 
|---|
| 2949 | } else { | 
|---|
| 2950 | /* TODO. JRA. */ | 
|---|
| 2951 | /* Now we have the complete path we can work out if this is | 
|---|
| 2952 | actually within this share and adjust newname accordingly. */ | 
|---|
| 2953 | DEBUG(10,("msg_file_was_renamed: share mismatch (sharepath %s " | 
|---|
| 2954 | "not sharepath %s) " | 
|---|
| 2955 | "fnum %d from %s -> %s\n", | 
|---|
| 2956 | fsp->conn->connectpath, | 
|---|
| 2957 | sharepath, | 
|---|
| 2958 | fsp->fnum, | 
|---|
| 2959 | fsp_str_dbg(fsp), | 
|---|
| 2960 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 2961 | } | 
|---|
| 2962 | } | 
|---|
| 2963 | out: | 
|---|
| 2964 | TALLOC_FREE(smb_fname); | 
|---|
| 2965 | return; | 
|---|
| 2966 | } | 
|---|
| 2967 |  | 
|---|
| 2968 | /* | 
|---|
| 2969 | * If a main file is opened for delete, all streams need to be checked for | 
|---|
| 2970 | * !FILE_SHARE_DELETE. Do this by opening with DELETE_ACCESS. | 
|---|
| 2971 | * If that works, delete them all by setting the delete on close and close. | 
|---|
| 2972 | */ | 
|---|
| 2973 |  | 
|---|
| 2974 | NTSTATUS open_streams_for_delete(connection_struct *conn, | 
|---|
| 2975 | const char *fname) | 
|---|
| 2976 | { | 
|---|
| 2977 | struct stream_struct *stream_info = NULL; | 
|---|
| 2978 | files_struct **streams = NULL; | 
|---|
| 2979 | int i; | 
|---|
| 2980 | unsigned int num_streams = 0; | 
|---|
| 2981 | TALLOC_CTX *frame = talloc_stackframe(); | 
|---|
| 2982 | NTSTATUS status; | 
|---|
| 2983 |  | 
|---|
| 2984 | status = vfs_streaminfo(conn, NULL, fname, talloc_tos(), | 
|---|
| 2985 | &num_streams, &stream_info); | 
|---|
| 2986 |  | 
|---|
| 2987 | if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_IMPLEMENTED) | 
|---|
| 2988 | || NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) { | 
|---|
| 2989 | DEBUG(10, ("no streams around\n")); | 
|---|
| 2990 | TALLOC_FREE(frame); | 
|---|
| 2991 | return NT_STATUS_OK; | 
|---|
| 2992 | } | 
|---|
| 2993 |  | 
|---|
| 2994 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 2995 | DEBUG(10, ("vfs_streaminfo failed: %s\n", | 
|---|
| 2996 | nt_errstr(status))); | 
|---|
| 2997 | goto fail; | 
|---|
| 2998 | } | 
|---|
| 2999 |  | 
|---|
| 3000 | DEBUG(10, ("open_streams_for_delete found %d streams\n", | 
|---|
| 3001 | num_streams)); | 
|---|
| 3002 |  | 
|---|
| 3003 | if (num_streams == 0) { | 
|---|
| 3004 | TALLOC_FREE(frame); | 
|---|
| 3005 | return NT_STATUS_OK; | 
|---|
| 3006 | } | 
|---|
| 3007 |  | 
|---|
| 3008 | streams = TALLOC_ARRAY(talloc_tos(), files_struct *, num_streams); | 
|---|
| 3009 | if (streams == NULL) { | 
|---|
| 3010 | DEBUG(0, ("talloc failed\n")); | 
|---|
| 3011 | status = NT_STATUS_NO_MEMORY; | 
|---|
| 3012 | goto fail; | 
|---|
| 3013 | } | 
|---|
| 3014 |  | 
|---|
| 3015 | for (i=0; i<num_streams; i++) { | 
|---|
| 3016 | struct smb_filename *smb_fname = NULL; | 
|---|
| 3017 |  | 
|---|
| 3018 | if (strequal(stream_info[i].name, "::$DATA")) { | 
|---|
| 3019 | streams[i] = NULL; | 
|---|
| 3020 | continue; | 
|---|
| 3021 | } | 
|---|
| 3022 |  | 
|---|
| 3023 | status = create_synthetic_smb_fname(talloc_tos(), fname, | 
|---|
| 3024 | stream_info[i].name, | 
|---|
| 3025 | NULL, &smb_fname); | 
|---|
| 3026 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3027 | goto fail; | 
|---|
| 3028 | } | 
|---|
| 3029 |  | 
|---|
| 3030 | if (SMB_VFS_STAT(conn, smb_fname) == -1) { | 
|---|
| 3031 | DEBUG(10, ("Unable to stat stream: %s\n", | 
|---|
| 3032 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 3033 | } | 
|---|
| 3034 |  | 
|---|
| 3035 | status = SMB_VFS_CREATE_FILE( | 
|---|
| 3036 | conn,                  /* conn */ | 
|---|
| 3037 | NULL,                  /* req */ | 
|---|
| 3038 | 0,                     /* root_dir_fid */ | 
|---|
| 3039 | smb_fname,             /* fname */ | 
|---|
| 3040 | DELETE_ACCESS,         /* access_mask */ | 
|---|
| 3041 | (FILE_SHARE_READ |     /* share_access */ | 
|---|
| 3042 | FILE_SHARE_WRITE | FILE_SHARE_DELETE), | 
|---|
| 3043 | FILE_OPEN,             /* create_disposition*/ | 
|---|
| 3044 | 0,                     /* create_options */ | 
|---|
| 3045 | FILE_ATTRIBUTE_NORMAL, /* file_attributes */ | 
|---|
| 3046 | 0,                     /* oplock_request */ | 
|---|
| 3047 | 0,                     /* allocation_size */ | 
|---|
| 3048 | NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE, /* private_flags */ | 
|---|
| 3049 | NULL,                  /* sd */ | 
|---|
| 3050 | NULL,                  /* ea_list */ | 
|---|
| 3051 | &streams[i],           /* result */ | 
|---|
| 3052 | NULL);                 /* pinfo */ | 
|---|
| 3053 |  | 
|---|
| 3054 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3055 | DEBUG(10, ("Could not open stream %s: %s\n", | 
|---|
| 3056 | smb_fname_str_dbg(smb_fname), | 
|---|
| 3057 | nt_errstr(status))); | 
|---|
| 3058 |  | 
|---|
| 3059 | TALLOC_FREE(smb_fname); | 
|---|
| 3060 | break; | 
|---|
| 3061 | } | 
|---|
| 3062 | TALLOC_FREE(smb_fname); | 
|---|
| 3063 | } | 
|---|
| 3064 |  | 
|---|
| 3065 | /* | 
|---|
| 3066 | * don't touch the variable "status" beyond this point :-) | 
|---|
| 3067 | */ | 
|---|
| 3068 |  | 
|---|
| 3069 | for (i -= 1 ; i >= 0; i--) { | 
|---|
| 3070 | if (streams[i] == NULL) { | 
|---|
| 3071 | continue; | 
|---|
| 3072 | } | 
|---|
| 3073 |  | 
|---|
| 3074 | DEBUG(10, ("Closing stream # %d, %s\n", i, | 
|---|
| 3075 | fsp_str_dbg(streams[i]))); | 
|---|
| 3076 | close_file(NULL, streams[i], NORMAL_CLOSE); | 
|---|
| 3077 | } | 
|---|
| 3078 |  | 
|---|
| 3079 | fail: | 
|---|
| 3080 | TALLOC_FREE(frame); | 
|---|
| 3081 | return status; | 
|---|
| 3082 | } | 
|---|
| 3083 |  | 
|---|
| 3084 | /* | 
|---|
| 3085 | * Wrapper around open_file_ntcreate and open_directory | 
|---|
| 3086 | */ | 
|---|
| 3087 |  | 
|---|
| 3088 | static NTSTATUS create_file_unixpath(connection_struct *conn, | 
|---|
| 3089 | struct smb_request *req, | 
|---|
| 3090 | struct smb_filename *smb_fname, | 
|---|
| 3091 | uint32_t access_mask, | 
|---|
| 3092 | uint32_t share_access, | 
|---|
| 3093 | uint32_t create_disposition, | 
|---|
| 3094 | uint32_t create_options, | 
|---|
| 3095 | uint32_t file_attributes, | 
|---|
| 3096 | uint32_t oplock_request, | 
|---|
| 3097 | uint64_t allocation_size, | 
|---|
| 3098 | uint32_t private_flags, | 
|---|
| 3099 | struct security_descriptor *sd, | 
|---|
| 3100 | struct ea_list *ea_list, | 
|---|
| 3101 |  | 
|---|
| 3102 | files_struct **result, | 
|---|
| 3103 | int *pinfo) | 
|---|
| 3104 | { | 
|---|
| 3105 | int info = FILE_WAS_OPENED; | 
|---|
| 3106 | files_struct *base_fsp = NULL; | 
|---|
| 3107 | files_struct *fsp = NULL; | 
|---|
| 3108 | NTSTATUS status; | 
|---|
| 3109 |  | 
|---|
| 3110 | DEBUG(10,("create_file_unixpath: access_mask = 0x%x " | 
|---|
| 3111 | "file_attributes = 0x%x, share_access = 0x%x, " | 
|---|
| 3112 | "create_disposition = 0x%x create_options = 0x%x " | 
|---|
| 3113 | "oplock_request = 0x%x private_flags = 0x%x " | 
|---|
| 3114 | "ea_list = 0x%p, sd = 0x%p, " | 
|---|
| 3115 | "fname = %s\n", | 
|---|
| 3116 | (unsigned int)access_mask, | 
|---|
| 3117 | (unsigned int)file_attributes, | 
|---|
| 3118 | (unsigned int)share_access, | 
|---|
| 3119 | (unsigned int)create_disposition, | 
|---|
| 3120 | (unsigned int)create_options, | 
|---|
| 3121 | (unsigned int)oplock_request, | 
|---|
| 3122 | (unsigned int)private_flags, | 
|---|
| 3123 | ea_list, sd, smb_fname_str_dbg(smb_fname))); | 
|---|
| 3124 |  | 
|---|
| 3125 | if (create_options & FILE_OPEN_BY_FILE_ID) { | 
|---|
| 3126 | status = NT_STATUS_NOT_SUPPORTED; | 
|---|
| 3127 | goto fail; | 
|---|
| 3128 | } | 
|---|
| 3129 |  | 
|---|
| 3130 | if (create_options & NTCREATEX_OPTIONS_INVALID_PARAM_MASK) { | 
|---|
| 3131 | status = NT_STATUS_INVALID_PARAMETER; | 
|---|
| 3132 | goto fail; | 
|---|
| 3133 | } | 
|---|
| 3134 |  | 
|---|
| 3135 | if (req == NULL) { | 
|---|
| 3136 | oplock_request |= INTERNAL_OPEN_ONLY; | 
|---|
| 3137 | } | 
|---|
| 3138 |  | 
|---|
| 3139 | if ((conn->fs_capabilities & FILE_NAMED_STREAMS) | 
|---|
| 3140 | && (access_mask & DELETE_ACCESS) | 
|---|
| 3141 | && !is_ntfs_stream_smb_fname(smb_fname)) { | 
|---|
| 3142 | /* | 
|---|
| 3143 | * We can't open a file with DELETE access if any of the | 
|---|
| 3144 | * streams is open without FILE_SHARE_DELETE | 
|---|
| 3145 | */ | 
|---|
| 3146 | status = open_streams_for_delete(conn, smb_fname->base_name); | 
|---|
| 3147 |  | 
|---|
| 3148 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3149 | goto fail; | 
|---|
| 3150 | } | 
|---|
| 3151 | } | 
|---|
| 3152 |  | 
|---|
| 3153 | /* This is the correct thing to do (check every time) but can_delete | 
|---|
| 3154 | * is expensive (it may have to read the parent directory | 
|---|
| 3155 | * permissions). So for now we're not doing it unless we have a strong | 
|---|
| 3156 | * hint the client is really going to delete this file. If the client | 
|---|
| 3157 | * is forcing FILE_CREATE let the filesystem take care of the | 
|---|
| 3158 | * permissions. */ | 
|---|
| 3159 |  | 
|---|
| 3160 | /* Setting FILE_SHARE_DELETE is the hint. */ | 
|---|
| 3161 |  | 
|---|
| 3162 | if ((create_disposition != FILE_CREATE) | 
|---|
| 3163 | && (access_mask & DELETE_ACCESS) | 
|---|
| 3164 | && (!(can_delete_file_in_directory(conn, smb_fname) || | 
|---|
| 3165 | can_access_file_acl(conn, smb_fname, DELETE_ACCESS)))) { | 
|---|
| 3166 | status = NT_STATUS_ACCESS_DENIED; | 
|---|
| 3167 | DEBUG(10,("create_file_unixpath: open file %s " | 
|---|
| 3168 | "for delete ACCESS_DENIED\n", | 
|---|
| 3169 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 3170 | goto fail; | 
|---|
| 3171 | } | 
|---|
| 3172 |  | 
|---|
| 3173 | if ((access_mask & SEC_FLAG_SYSTEM_SECURITY) && | 
|---|
| 3174 | !security_token_has_privilege(get_current_nttok(conn), | 
|---|
| 3175 | SEC_PRIV_SECURITY)) { | 
|---|
| 3176 | DEBUG(10, ("create_file_unixpath: open on %s " | 
|---|
| 3177 | "failed - SEC_FLAG_SYSTEM_SECURITY denied.\n", | 
|---|
| 3178 | smb_fname_str_dbg(smb_fname))); | 
|---|
| 3179 | status = NT_STATUS_PRIVILEGE_NOT_HELD; | 
|---|
| 3180 | goto fail; | 
|---|
| 3181 | } | 
|---|
| 3182 |  | 
|---|
| 3183 | if ((conn->fs_capabilities & FILE_NAMED_STREAMS) | 
|---|
| 3184 | && is_ntfs_stream_smb_fname(smb_fname) | 
|---|
| 3185 | && (!(private_flags & NTCREATEX_OPTIONS_PRIVATE_STREAM_DELETE))) { | 
|---|
| 3186 | uint32 base_create_disposition; | 
|---|
| 3187 | struct smb_filename *smb_fname_base = NULL; | 
|---|
| 3188 |  | 
|---|
| 3189 | if (create_options & FILE_DIRECTORY_FILE) { | 
|---|
| 3190 | status = NT_STATUS_NOT_A_DIRECTORY; | 
|---|
| 3191 | goto fail; | 
|---|
| 3192 | } | 
|---|
| 3193 |  | 
|---|
| 3194 | switch (create_disposition) { | 
|---|
| 3195 | case FILE_OPEN: | 
|---|
| 3196 | base_create_disposition = FILE_OPEN; | 
|---|
| 3197 | break; | 
|---|
| 3198 | default: | 
|---|
| 3199 | base_create_disposition = FILE_OPEN_IF; | 
|---|
| 3200 | break; | 
|---|
| 3201 | } | 
|---|
| 3202 |  | 
|---|
| 3203 | /* Create an smb_filename with stream_name == NULL. */ | 
|---|
| 3204 | status = create_synthetic_smb_fname(talloc_tos(), | 
|---|
| 3205 | smb_fname->base_name, | 
|---|
| 3206 | NULL, NULL, | 
|---|
| 3207 | &smb_fname_base); | 
|---|
| 3208 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3209 | goto fail; | 
|---|
| 3210 | } | 
|---|
| 3211 |  | 
|---|
| 3212 | if (SMB_VFS_STAT(conn, smb_fname_base) == -1) { | 
|---|
| 3213 | DEBUG(10, ("Unable to stat stream: %s\n", | 
|---|
| 3214 | smb_fname_str_dbg(smb_fname_base))); | 
|---|
| 3215 | } | 
|---|
| 3216 |  | 
|---|
| 3217 | /* Open the base file. */ | 
|---|
| 3218 | status = create_file_unixpath(conn, NULL, smb_fname_base, 0, | 
|---|
| 3219 | FILE_SHARE_READ | 
|---|
| 3220 | | FILE_SHARE_WRITE | 
|---|
| 3221 | | FILE_SHARE_DELETE, | 
|---|
| 3222 | base_create_disposition, | 
|---|
| 3223 | 0, 0, 0, 0, 0, NULL, NULL, | 
|---|
| 3224 | &base_fsp, NULL); | 
|---|
| 3225 | TALLOC_FREE(smb_fname_base); | 
|---|
| 3226 |  | 
|---|
| 3227 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3228 | DEBUG(10, ("create_file_unixpath for base %s failed: " | 
|---|
| 3229 | "%s\n", smb_fname->base_name, | 
|---|
| 3230 | nt_errstr(status))); | 
|---|
| 3231 | goto fail; | 
|---|
| 3232 | } | 
|---|
| 3233 | /* we don't need to low level fd */ | 
|---|
| 3234 | fd_close(base_fsp); | 
|---|
| 3235 | } | 
|---|
| 3236 |  | 
|---|
| 3237 | /* | 
|---|
| 3238 | * If it's a request for a directory open, deal with it separately. | 
|---|
| 3239 | */ | 
|---|
| 3240 |  | 
|---|
| 3241 | if (create_options & FILE_DIRECTORY_FILE) { | 
|---|
| 3242 |  | 
|---|
| 3243 | if (create_options & FILE_NON_DIRECTORY_FILE) { | 
|---|
| 3244 | status = NT_STATUS_INVALID_PARAMETER; | 
|---|
| 3245 | goto fail; | 
|---|
| 3246 | } | 
|---|
| 3247 |  | 
|---|
| 3248 | /* Can't open a temp directory. IFS kit test. */ | 
|---|
| 3249 | if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) && | 
|---|
| 3250 | (file_attributes & FILE_ATTRIBUTE_TEMPORARY)) { | 
|---|
| 3251 | status = NT_STATUS_INVALID_PARAMETER; | 
|---|
| 3252 | goto fail; | 
|---|
| 3253 | } | 
|---|
| 3254 |  | 
|---|
| 3255 | /* | 
|---|
| 3256 | * We will get a create directory here if the Win32 | 
|---|
| 3257 | * app specified a security descriptor in the | 
|---|
| 3258 | * CreateDirectory() call. | 
|---|
| 3259 | */ | 
|---|
| 3260 |  | 
|---|
| 3261 | oplock_request = 0; | 
|---|
| 3262 | status = open_directory( | 
|---|
| 3263 | conn, req, smb_fname, access_mask, share_access, | 
|---|
| 3264 | create_disposition, create_options, file_attributes, | 
|---|
| 3265 | &info, &fsp); | 
|---|
| 3266 | } else { | 
|---|
| 3267 |  | 
|---|
| 3268 | /* | 
|---|
| 3269 | * Ordinary file case. | 
|---|
| 3270 | */ | 
|---|
| 3271 |  | 
|---|
| 3272 | status = file_new(req, conn, &fsp); | 
|---|
| 3273 | if(!NT_STATUS_IS_OK(status)) { | 
|---|
| 3274 | goto fail; | 
|---|
| 3275 | } | 
|---|
| 3276 |  | 
|---|
| 3277 | status = fsp_set_smb_fname(fsp, smb_fname); | 
|---|
| 3278 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3279 | goto fail; | 
|---|
| 3280 | } | 
|---|
| 3281 |  | 
|---|
| 3282 | /* | 
|---|
| 3283 | * We're opening the stream element of a base_fsp | 
|---|
| 3284 | * we already opened. Set up the base_fsp pointer. | 
|---|
| 3285 | */ | 
|---|
| 3286 | if (base_fsp) { | 
|---|
| 3287 | fsp->base_fsp = base_fsp; | 
|---|
| 3288 | } | 
|---|
| 3289 |  | 
|---|
| 3290 | status = open_file_ntcreate(conn, | 
|---|
| 3291 | req, | 
|---|
| 3292 | access_mask, | 
|---|
| 3293 | share_access, | 
|---|
| 3294 | create_disposition, | 
|---|
| 3295 | create_options, | 
|---|
| 3296 | file_attributes, | 
|---|
| 3297 | oplock_request, | 
|---|
| 3298 | private_flags, | 
|---|
| 3299 | &info, | 
|---|
| 3300 | fsp); | 
|---|
| 3301 |  | 
|---|
| 3302 | if(!NT_STATUS_IS_OK(status)) { | 
|---|
| 3303 | file_free(req, fsp); | 
|---|
| 3304 | fsp = NULL; | 
|---|
| 3305 | } | 
|---|
| 3306 |  | 
|---|
| 3307 | if (NT_STATUS_EQUAL(status, NT_STATUS_FILE_IS_A_DIRECTORY)) { | 
|---|
| 3308 |  | 
|---|
| 3309 | /* A stream open never opens a directory */ | 
|---|
| 3310 |  | 
|---|
| 3311 | if (base_fsp) { | 
|---|
| 3312 | status = NT_STATUS_FILE_IS_A_DIRECTORY; | 
|---|
| 3313 | goto fail; | 
|---|
| 3314 | } | 
|---|
| 3315 |  | 
|---|
| 3316 | /* | 
|---|
| 3317 | * Fail the open if it was explicitly a non-directory | 
|---|
| 3318 | * file. | 
|---|
| 3319 | */ | 
|---|
| 3320 |  | 
|---|
| 3321 | if (create_options & FILE_NON_DIRECTORY_FILE) { | 
|---|
| 3322 | status = NT_STATUS_FILE_IS_A_DIRECTORY; | 
|---|
| 3323 | goto fail; | 
|---|
| 3324 | } | 
|---|
| 3325 |  | 
|---|
| 3326 | oplock_request = 0; | 
|---|
| 3327 | status = open_directory( | 
|---|
| 3328 | conn, req, smb_fname, access_mask, | 
|---|
| 3329 | share_access, create_disposition, | 
|---|
| 3330 | create_options, file_attributes, | 
|---|
| 3331 | &info, &fsp); | 
|---|
| 3332 | } | 
|---|
| 3333 | } | 
|---|
| 3334 |  | 
|---|
| 3335 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3336 | goto fail; | 
|---|
| 3337 | } | 
|---|
| 3338 |  | 
|---|
| 3339 | fsp->base_fsp = base_fsp; | 
|---|
| 3340 |  | 
|---|
| 3341 | /* | 
|---|
| 3342 | * According to the MS documentation, the only time the security | 
|---|
| 3343 | * descriptor is applied to the opened file is iff we *created* the | 
|---|
| 3344 | * file; an existing file stays the same. | 
|---|
| 3345 | * | 
|---|
| 3346 | * Also, it seems (from observation) that you can open the file with | 
|---|
| 3347 | * any access mask but you can still write the sd. We need to override | 
|---|
| 3348 | * the granted access before we call set_sd | 
|---|
| 3349 | * Patch for bug #2242 from Tom Lackemann <cessnatomny@yahoo.com>. | 
|---|
| 3350 | */ | 
|---|
| 3351 |  | 
|---|
| 3352 | if ((sd != NULL) && (info == FILE_WAS_CREATED) | 
|---|
| 3353 | && lp_nt_acl_support(SNUM(conn))) { | 
|---|
| 3354 |  | 
|---|
| 3355 | uint32_t sec_info_sent; | 
|---|
| 3356 | uint32_t saved_access_mask = fsp->access_mask; | 
|---|
| 3357 |  | 
|---|
| 3358 | sec_info_sent = get_sec_info(sd); | 
|---|
| 3359 |  | 
|---|
| 3360 | fsp->access_mask = FILE_GENERIC_ALL; | 
|---|
| 3361 |  | 
|---|
| 3362 | if (sec_info_sent & (SECINFO_OWNER| | 
|---|
| 3363 | SECINFO_GROUP| | 
|---|
| 3364 | SECINFO_DACL| | 
|---|
| 3365 | SECINFO_SACL)) { | 
|---|
| 3366 | status = set_sd(fsp, sd, sec_info_sent); | 
|---|
| 3367 | } | 
|---|
| 3368 |  | 
|---|
| 3369 | fsp->access_mask = saved_access_mask; | 
|---|
| 3370 |  | 
|---|
| 3371 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3372 | goto fail; | 
|---|
| 3373 | } | 
|---|
| 3374 | } | 
|---|
| 3375 |  | 
|---|
| 3376 | if ((ea_list != NULL) && | 
|---|
| 3377 | ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN))) { | 
|---|
| 3378 | status = set_ea(conn, fsp, fsp->fsp_name, ea_list); | 
|---|
| 3379 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3380 | goto fail; | 
|---|
| 3381 | } | 
|---|
| 3382 | } | 
|---|
| 3383 |  | 
|---|
| 3384 | if (!fsp->is_directory && S_ISDIR(fsp->fsp_name->st.st_ex_mode)) { | 
|---|
| 3385 | status = NT_STATUS_ACCESS_DENIED; | 
|---|
| 3386 | goto fail; | 
|---|
| 3387 | } | 
|---|
| 3388 |  | 
|---|
| 3389 | /* Save the requested allocation size. */ | 
|---|
| 3390 | if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) { | 
|---|
| 3391 | if (allocation_size | 
|---|
| 3392 | && (allocation_size > fsp->fsp_name->st.st_ex_size)) { | 
|---|
| 3393 | fsp->initial_allocation_size = smb_roundup( | 
|---|
| 3394 | fsp->conn, allocation_size); | 
|---|
| 3395 | if (fsp->is_directory) { | 
|---|
| 3396 | /* Can't set allocation size on a directory. */ | 
|---|
| 3397 | status = NT_STATUS_ACCESS_DENIED; | 
|---|
| 3398 | goto fail; | 
|---|
| 3399 | } | 
|---|
| 3400 | if (vfs_allocate_file_space( | 
|---|
| 3401 | fsp, fsp->initial_allocation_size) == -1) { | 
|---|
| 3402 | status = NT_STATUS_DISK_FULL; | 
|---|
| 3403 | goto fail; | 
|---|
| 3404 | } | 
|---|
| 3405 | } else { | 
|---|
| 3406 | fsp->initial_allocation_size = smb_roundup( | 
|---|
| 3407 | fsp->conn, (uint64_t)fsp->fsp_name->st.st_ex_size); | 
|---|
| 3408 | } | 
|---|
| 3409 | } | 
|---|
| 3410 |  | 
|---|
| 3411 | DEBUG(10, ("create_file_unixpath: info=%d\n", info)); | 
|---|
| 3412 |  | 
|---|
| 3413 | *result = fsp; | 
|---|
| 3414 | if (pinfo != NULL) { | 
|---|
| 3415 | *pinfo = info; | 
|---|
| 3416 | } | 
|---|
| 3417 |  | 
|---|
| 3418 | smb_fname->st = fsp->fsp_name->st; | 
|---|
| 3419 |  | 
|---|
| 3420 | return NT_STATUS_OK; | 
|---|
| 3421 |  | 
|---|
| 3422 | fail: | 
|---|
| 3423 | DEBUG(10, ("create_file_unixpath: %s\n", nt_errstr(status))); | 
|---|
| 3424 |  | 
|---|
| 3425 | if (fsp != NULL) { | 
|---|
| 3426 | if (base_fsp && fsp->base_fsp == base_fsp) { | 
|---|
| 3427 | /* | 
|---|
| 3428 | * The close_file below will close | 
|---|
| 3429 | * fsp->base_fsp. | 
|---|
| 3430 | */ | 
|---|
| 3431 | base_fsp = NULL; | 
|---|
| 3432 | } | 
|---|
| 3433 | close_file(req, fsp, ERROR_CLOSE); | 
|---|
| 3434 | fsp = NULL; | 
|---|
| 3435 | } | 
|---|
| 3436 | if (base_fsp != NULL) { | 
|---|
| 3437 | close_file(req, base_fsp, ERROR_CLOSE); | 
|---|
| 3438 | base_fsp = NULL; | 
|---|
| 3439 | } | 
|---|
| 3440 | return status; | 
|---|
| 3441 | } | 
|---|
| 3442 |  | 
|---|
| 3443 | /* | 
|---|
| 3444 | * Calculate the full path name given a relative fid. | 
|---|
| 3445 | */ | 
|---|
| 3446 | NTSTATUS get_relative_fid_filename(connection_struct *conn, | 
|---|
| 3447 | struct smb_request *req, | 
|---|
| 3448 | uint16_t root_dir_fid, | 
|---|
| 3449 | const struct smb_filename *smb_fname, | 
|---|
| 3450 | struct smb_filename **smb_fname_out) | 
|---|
| 3451 | { | 
|---|
| 3452 | files_struct *dir_fsp; | 
|---|
| 3453 | char *parent_fname = NULL; | 
|---|
| 3454 | char *new_base_name = NULL; | 
|---|
| 3455 | NTSTATUS status; | 
|---|
| 3456 |  | 
|---|
| 3457 | if (root_dir_fid == 0 || !smb_fname) { | 
|---|
| 3458 | status = NT_STATUS_INTERNAL_ERROR; | 
|---|
| 3459 | goto out; | 
|---|
| 3460 | } | 
|---|
| 3461 |  | 
|---|
| 3462 | dir_fsp = file_fsp(req, root_dir_fid); | 
|---|
| 3463 |  | 
|---|
| 3464 | if (dir_fsp == NULL) { | 
|---|
| 3465 | status = NT_STATUS_INVALID_HANDLE; | 
|---|
| 3466 | goto out; | 
|---|
| 3467 | } | 
|---|
| 3468 |  | 
|---|
| 3469 | if (is_ntfs_stream_smb_fname(dir_fsp->fsp_name)) { | 
|---|
| 3470 | status = NT_STATUS_INVALID_HANDLE; | 
|---|
| 3471 | goto out; | 
|---|
| 3472 | } | 
|---|
| 3473 |  | 
|---|
| 3474 | if (!dir_fsp->is_directory) { | 
|---|
| 3475 |  | 
|---|
| 3476 | /* | 
|---|
| 3477 | * Check to see if this is a mac fork of some kind. | 
|---|
| 3478 | */ | 
|---|
| 3479 |  | 
|---|
| 3480 | if ((conn->fs_capabilities & FILE_NAMED_STREAMS) && | 
|---|
| 3481 | is_ntfs_stream_smb_fname(smb_fname)) { | 
|---|
| 3482 | status = NT_STATUS_OBJECT_PATH_NOT_FOUND; | 
|---|
| 3483 | goto out; | 
|---|
| 3484 | } | 
|---|
| 3485 |  | 
|---|
| 3486 | /* | 
|---|
| 3487 | we need to handle the case when we get a | 
|---|
| 3488 | relative open relative to a file and the | 
|---|
| 3489 | pathname is blank - this is a reopen! | 
|---|
| 3490 | (hint from demyn plantenberg) | 
|---|
| 3491 | */ | 
|---|
| 3492 |  | 
|---|
| 3493 | status = NT_STATUS_INVALID_HANDLE; | 
|---|
| 3494 | goto out; | 
|---|
| 3495 | } | 
|---|
| 3496 |  | 
|---|
| 3497 | if (ISDOT(dir_fsp->fsp_name->base_name)) { | 
|---|
| 3498 | /* | 
|---|
| 3499 | * We're at the toplevel dir, the final file name | 
|---|
| 3500 | * must not contain ./, as this is filtered out | 
|---|
| 3501 | * normally by srvstr_get_path and unix_convert | 
|---|
| 3502 | * explicitly rejects paths containing ./. | 
|---|
| 3503 | */ | 
|---|
| 3504 | parent_fname = talloc_strdup(talloc_tos(), ""); | 
|---|
| 3505 | if (parent_fname == NULL) { | 
|---|
| 3506 | status = NT_STATUS_NO_MEMORY; | 
|---|
| 3507 | goto out; | 
|---|
| 3508 | } | 
|---|
| 3509 | } else { | 
|---|
| 3510 | size_t dir_name_len = strlen(dir_fsp->fsp_name->base_name); | 
|---|
| 3511 |  | 
|---|
| 3512 | /* | 
|---|
| 3513 | * Copy in the base directory name. | 
|---|
| 3514 | */ | 
|---|
| 3515 |  | 
|---|
| 3516 | parent_fname = TALLOC_ARRAY(talloc_tos(), char, | 
|---|
| 3517 | dir_name_len+2); | 
|---|
| 3518 | if (parent_fname == NULL) { | 
|---|
| 3519 | status = NT_STATUS_NO_MEMORY; | 
|---|
| 3520 | goto out; | 
|---|
| 3521 | } | 
|---|
| 3522 | memcpy(parent_fname, dir_fsp->fsp_name->base_name, | 
|---|
| 3523 | dir_name_len+1); | 
|---|
| 3524 |  | 
|---|
| 3525 | /* | 
|---|
| 3526 | * Ensure it ends in a '/'. | 
|---|
| 3527 | * We used TALLOC_SIZE +2 to add space for the '/'. | 
|---|
| 3528 | */ | 
|---|
| 3529 |  | 
|---|
| 3530 | if(dir_name_len | 
|---|
| 3531 | && (parent_fname[dir_name_len-1] != '\\') | 
|---|
| 3532 | && (parent_fname[dir_name_len-1] != '/')) { | 
|---|
| 3533 | parent_fname[dir_name_len] = '/'; | 
|---|
| 3534 | parent_fname[dir_name_len+1] = '\0'; | 
|---|
| 3535 | } | 
|---|
| 3536 | } | 
|---|
| 3537 |  | 
|---|
| 3538 | new_base_name = talloc_asprintf(talloc_tos(), "%s%s", parent_fname, | 
|---|
| 3539 | smb_fname->base_name); | 
|---|
| 3540 | if (new_base_name == NULL) { | 
|---|
| 3541 | status = NT_STATUS_NO_MEMORY; | 
|---|
| 3542 | goto out; | 
|---|
| 3543 | } | 
|---|
| 3544 |  | 
|---|
| 3545 | status = filename_convert(req, | 
|---|
| 3546 | conn, | 
|---|
| 3547 | req->flags2 & FLAGS2_DFS_PATHNAMES, | 
|---|
| 3548 | new_base_name, | 
|---|
| 3549 | 0, | 
|---|
| 3550 | NULL, | 
|---|
| 3551 | smb_fname_out); | 
|---|
| 3552 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3553 | goto out; | 
|---|
| 3554 | } | 
|---|
| 3555 |  | 
|---|
| 3556 | out: | 
|---|
| 3557 | TALLOC_FREE(parent_fname); | 
|---|
| 3558 | TALLOC_FREE(new_base_name); | 
|---|
| 3559 | return status; | 
|---|
| 3560 | } | 
|---|
| 3561 |  | 
|---|
| 3562 | NTSTATUS create_file_default(connection_struct *conn, | 
|---|
| 3563 | struct smb_request *req, | 
|---|
| 3564 | uint16_t root_dir_fid, | 
|---|
| 3565 | struct smb_filename *smb_fname, | 
|---|
| 3566 | uint32_t access_mask, | 
|---|
| 3567 | uint32_t share_access, | 
|---|
| 3568 | uint32_t create_disposition, | 
|---|
| 3569 | uint32_t create_options, | 
|---|
| 3570 | uint32_t file_attributes, | 
|---|
| 3571 | uint32_t oplock_request, | 
|---|
| 3572 | uint64_t allocation_size, | 
|---|
| 3573 | uint32_t private_flags, | 
|---|
| 3574 | struct security_descriptor *sd, | 
|---|
| 3575 | struct ea_list *ea_list, | 
|---|
| 3576 | files_struct **result, | 
|---|
| 3577 | int *pinfo) | 
|---|
| 3578 | { | 
|---|
| 3579 | int info = FILE_WAS_OPENED; | 
|---|
| 3580 | files_struct *fsp = NULL; | 
|---|
| 3581 | NTSTATUS status; | 
|---|
| 3582 | bool stream_name = false; | 
|---|
| 3583 |  | 
|---|
| 3584 | DEBUG(10,("create_file: access_mask = 0x%x " | 
|---|
| 3585 | "file_attributes = 0x%x, share_access = 0x%x, " | 
|---|
| 3586 | "create_disposition = 0x%x create_options = 0x%x " | 
|---|
| 3587 | "oplock_request = 0x%x " | 
|---|
| 3588 | "private_flags = 0x%x " | 
|---|
| 3589 | "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, " | 
|---|
| 3590 | "fname = %s\n", | 
|---|
| 3591 | (unsigned int)access_mask, | 
|---|
| 3592 | (unsigned int)file_attributes, | 
|---|
| 3593 | (unsigned int)share_access, | 
|---|
| 3594 | (unsigned int)create_disposition, | 
|---|
| 3595 | (unsigned int)create_options, | 
|---|
| 3596 | (unsigned int)oplock_request, | 
|---|
| 3597 | (unsigned int)private_flags, | 
|---|
| 3598 | (unsigned int)root_dir_fid, | 
|---|
| 3599 | ea_list, sd, smb_fname_str_dbg(smb_fname))); | 
|---|
| 3600 |  | 
|---|
| 3601 | /* | 
|---|
| 3602 | * Calculate the filename from the root_dir_if if necessary. | 
|---|
| 3603 | */ | 
|---|
| 3604 |  | 
|---|
| 3605 | if (root_dir_fid != 0) { | 
|---|
| 3606 | struct smb_filename *smb_fname_out = NULL; | 
|---|
| 3607 | status = get_relative_fid_filename(conn, req, root_dir_fid, | 
|---|
| 3608 | smb_fname, &smb_fname_out); | 
|---|
| 3609 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3610 | goto fail; | 
|---|
| 3611 | } | 
|---|
| 3612 | smb_fname = smb_fname_out; | 
|---|
| 3613 | } | 
|---|
| 3614 |  | 
|---|
| 3615 | /* | 
|---|
| 3616 | * Check to see if this is a mac fork of some kind. | 
|---|
| 3617 | */ | 
|---|
| 3618 |  | 
|---|
| 3619 | stream_name = is_ntfs_stream_smb_fname(smb_fname); | 
|---|
| 3620 | if (stream_name) { | 
|---|
| 3621 | enum FAKE_FILE_TYPE fake_file_type; | 
|---|
| 3622 |  | 
|---|
| 3623 | fake_file_type = is_fake_file(smb_fname); | 
|---|
| 3624 |  | 
|---|
| 3625 | if (fake_file_type != FAKE_FILE_TYPE_NONE) { | 
|---|
| 3626 |  | 
|---|
| 3627 | /* | 
|---|
| 3628 | * Here we go! support for changing the disk quotas | 
|---|
| 3629 | * --metze | 
|---|
| 3630 | * | 
|---|
| 3631 | * We need to fake up to open this MAGIC QUOTA file | 
|---|
| 3632 | * and return a valid FID. | 
|---|
| 3633 | * | 
|---|
| 3634 | * w2k close this file directly after openening xp | 
|---|
| 3635 | * also tries a QUERY_FILE_INFO on the file and then | 
|---|
| 3636 | * close it | 
|---|
| 3637 | */ | 
|---|
| 3638 | status = open_fake_file(req, conn, req->vuid, | 
|---|
| 3639 | fake_file_type, smb_fname, | 
|---|
| 3640 | access_mask, &fsp); | 
|---|
| 3641 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3642 | goto fail; | 
|---|
| 3643 | } | 
|---|
| 3644 |  | 
|---|
| 3645 | ZERO_STRUCT(smb_fname->st); | 
|---|
| 3646 | goto done; | 
|---|
| 3647 | } | 
|---|
| 3648 |  | 
|---|
| 3649 | if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) { | 
|---|
| 3650 | status = NT_STATUS_OBJECT_NAME_NOT_FOUND; | 
|---|
| 3651 | goto fail; | 
|---|
| 3652 | } | 
|---|
| 3653 | } | 
|---|
| 3654 |  | 
|---|
| 3655 | if (stream_name && is_ntfs_default_stream_smb_fname(smb_fname)) { | 
|---|
| 3656 | int ret; | 
|---|
| 3657 | smb_fname->stream_name = NULL; | 
|---|
| 3658 | /* We have to handle this error here. */ | 
|---|
| 3659 | if (create_options & FILE_DIRECTORY_FILE) { | 
|---|
| 3660 | status = NT_STATUS_NOT_A_DIRECTORY; | 
|---|
| 3661 | goto fail; | 
|---|
| 3662 | } | 
|---|
| 3663 | if (lp_posix_pathnames()) { | 
|---|
| 3664 | ret = SMB_VFS_LSTAT(conn, smb_fname); | 
|---|
| 3665 | } else { | 
|---|
| 3666 | ret = SMB_VFS_STAT(conn, smb_fname); | 
|---|
| 3667 | } | 
|---|
| 3668 |  | 
|---|
| 3669 | if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) { | 
|---|
| 3670 | status = NT_STATUS_FILE_IS_A_DIRECTORY; | 
|---|
| 3671 | goto fail; | 
|---|
| 3672 | } | 
|---|
| 3673 | } | 
|---|
| 3674 |  | 
|---|
| 3675 | status = create_file_unixpath( | 
|---|
| 3676 | conn, req, smb_fname, access_mask, share_access, | 
|---|
| 3677 | create_disposition, create_options, file_attributes, | 
|---|
| 3678 | oplock_request, allocation_size, private_flags, | 
|---|
| 3679 | sd, ea_list, | 
|---|
| 3680 | &fsp, &info); | 
|---|
| 3681 |  | 
|---|
| 3682 | if (!NT_STATUS_IS_OK(status)) { | 
|---|
| 3683 | goto fail; | 
|---|
| 3684 | } | 
|---|
| 3685 |  | 
|---|
| 3686 | done: | 
|---|
| 3687 | DEBUG(10, ("create_file: info=%d\n", info)); | 
|---|
| 3688 |  | 
|---|
| 3689 | *result = fsp; | 
|---|
| 3690 | if (pinfo != NULL) { | 
|---|
| 3691 | *pinfo = info; | 
|---|
| 3692 | } | 
|---|
| 3693 | return NT_STATUS_OK; | 
|---|
| 3694 |  | 
|---|
| 3695 | fail: | 
|---|
| 3696 | DEBUG(10, ("create_file: %s\n", nt_errstr(status))); | 
|---|
| 3697 |  | 
|---|
| 3698 | if (fsp != NULL) { | 
|---|
| 3699 | close_file(req, fsp, ERROR_CLOSE); | 
|---|
| 3700 | fsp = NULL; | 
|---|
| 3701 | } | 
|---|
| 3702 | return status; | 
|---|
| 3703 | } | 
|---|