smbd: Use has_other_nonposix_opens in smb_posix_unlink
[kai/samba-autobuild/.git] / source3 / smbd / trans2.c
index 8fd238ae96c92cd450aa21275e9d997b53b33a9d..0003c3682e3cce33866d30dbc94159ed130c755d 100644 (file)
@@ -32,7 +32,6 @@
 #include "../libcli/auth/libcli_auth.h"
 #include "../librpc/gen_ndr/xattr.h"
 #include "../librpc/gen_ndr/ndr_security.h"
-#include "../librpc/gen_ndr/open_files.h"
 #include "libcli/security/security.h"
 #include "trans2.h"
 #include "auth.h"
@@ -41,6 +40,7 @@
 #include "printing.h"
 #include "lib/util_ea.h"
 #include "lib/readdir_attr.h"
+#include "messages.h"
 
 #define DIR_ENTRY_SAFETY_MARGIN 4096
 
@@ -203,9 +203,12 @@ bool samba_private_attr_name(const char *unix_ea_name)
  Get one EA value. Fill in a struct ea_struct.
 ****************************************************************************/
 
-NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
-                     files_struct *fsp, const char *fname,
-                     const char *ea_name, struct ea_struct *pea)
+NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx,
+                       connection_struct *conn,
+                       files_struct *fsp,
+                       const struct smb_filename *smb_fname,
+                       const char *ea_name,
+                       struct ea_struct *pea)
 {
        /* Get the value of this xattr. Max size is 64k. */
        size_t attr_size = 256;
@@ -222,7 +225,8 @@ NTSTATUS get_ea_value(TALLOC_CTX *mem_ctx, connection_struct *conn,
        if (fsp && fsp->fh->fd != -1) {
                sizeret = SMB_VFS_FGETXATTR(fsp, ea_name, val, attr_size);
        } else {
-               sizeret = SMB_VFS_GETXATTR(conn, fname, ea_name, val, attr_size);
+               sizeret = SMB_VFS_GETXATTR(conn, smb_fname,
+                               ea_name, val, attr_size);
        }
 
        if (sizeret == -1 && errno == ERANGE && attr_size != 65536) {
@@ -289,7 +293,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx,
                                             ea_namelist_size);
        } else {
                sizeret = SMB_VFS_LISTXATTR(conn,
-                                           smb_fname->base_name,
+                                           smb_fname,
                                            ea_namelist,
                                            ea_namelist_size);
        }
@@ -307,7 +311,7 @@ NTSTATUS get_ea_names_from_file(TALLOC_CTX *mem_ctx,
                                                     ea_namelist_size);
                } else {
                        sizeret = SMB_VFS_LISTXATTR(conn,
-                                                   smb_fname->base_name,
+                                                   smb_fname,
                                                    ea_namelist,
                                                    ea_namelist_size);
                }
@@ -458,7 +462,7 @@ static NTSTATUS get_ea_list_from_file_path(TALLOC_CTX *mem_ctx,
                status = get_ea_value(listp,
                                        conn,
                                        fsp,
-                                       smb_fname->base_name,
+                                       smb_fname,
                                        names[i],
                                        &listp->ea);
 
@@ -800,7 +804,7 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
                                DEBUG(10,("set_ea: deleting ea name %s on file %s.\n",
                                        unix_ea_name, smb_fname->base_name));
                                ret = SMB_VFS_REMOVEXATTR(conn,
-                                               smb_fname->base_name,
+                                               smb_fname,
                                                unix_ea_name);
                        }
 #ifdef ENOATTR
@@ -822,7 +826,7 @@ NTSTATUS set_ea(connection_struct *conn, files_struct *fsp,
                                DEBUG(10,("set_ea: setting ea name %s on file %s.\n",
                                        unix_ea_name, smb_fname->base_name));
                                ret = SMB_VFS_SETXATTR(conn,
-                                               smb_fname->base_name,
+                                               smb_fname,
                                                unix_ea_name,
                                                ea_list->ea.value.data,
                                                ea_list->ea.value.length,
@@ -1573,18 +1577,18 @@ static NTSTATUS unix_perms_from_wire( connection_struct *conn,
 ****************************************************************************/
 
 static bool check_msdfs_link(connection_struct *conn,
-                               const char *pathname,
-                               SMB_STRUCT_STAT *psbuf)
+                               struct smb_filename *smb_fname)
 {
        int saved_errno = errno;
        if(lp_host_msdfs() &&
                lp_msdfs_root(SNUM(conn)) &&
-               is_msdfs_link(conn, pathname, psbuf)) {
+               is_msdfs_link(conn, smb_fname)) {
 
                DEBUG(5,("check_msdfs_link: Masquerading msdfs link %s "
                        "as a directory\n",
-                       pathname));
-               psbuf->st_ex_mode = (psbuf->st_ex_mode & 0xFFF) | S_IFDIR;
+                       smb_fname->base_name));
+               smb_fname->st.st_ex_mode =
+                       (smb_fname->st.st_ex_mode & 0xFFF) | S_IFDIR;
                errno = saved_errno;
                return true;
        }
@@ -1704,6 +1708,7 @@ static bool smbd_dirptr_lanman2_match_fn(TALLOC_CTX *ctx,
 static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
                                        void *private_data,
                                        struct smb_filename *smb_fname,
+                                       bool get_dosmode,
                                        uint32_t *_mode)
 {
        struct smbd_dirptr_lanman2_state *state =
@@ -1725,8 +1730,7 @@ static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
                 * directories */
 
                ms_dfs_link = check_msdfs_link(state->conn,
-                                              smb_fname->base_name,
-                                              &smb_fname->st);
+                                              smb_fname);
                if (!ms_dfs_link) {
                        DEBUG(5,("smbd_dirptr_lanman2_mode_fn: "
                                 "Couldn't stat [%s] (%s)\n",
@@ -1738,7 +1742,7 @@ static bool smbd_dirptr_lanman2_mode_fn(TALLOC_CTX *ctx,
 
        if (ms_dfs_link) {
                mode = dos_mode_msdfs(state->conn, smb_fname);
-       } else {
+       } else if (get_dosmode) {
                mode = dos_mode(state->conn, smb_fname);
        }
 
@@ -2447,12 +2451,14 @@ NTSTATUS smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
                               int requires_resume_key,
                               bool dont_descend,
                               bool ask_sharemode,
+                              bool get_dosmode,
                               uint8_t align,
                               bool do_pad,
                               char **ppdata,
                               char *base_data,
                               char *end_data,
                               int space_remaining,
+                              struct smb_filename **_smb_fname,
                               bool *got_exact_match,
                               int *_last_entry_off,
                               struct ea_list *name_list,
@@ -2501,6 +2507,7 @@ NTSTATUS smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
                                   dirtype,
                                   dont_descend,
                                   ask_sharemode,
+                                  get_dosmode,
                                   smbd_dirptr_lanman2_match_fn,
                                   smbd_dirptr_lanman2_mode_fn,
                                   &state,
@@ -2542,15 +2549,33 @@ NTSTATUS smbd_dirptr_lanman2_entry(TALLOC_CTX *ctx,
                *file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
        }
 
-       TALLOC_FREE(fname);
+       if (!NT_STATUS_IS_OK(status) &&
+           !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES))
+       {
+               TALLOC_FREE(smb_fname);
+               TALLOC_FREE(fname);
+               return status;
+       }
+
+       if (_smb_fname != NULL) {
+               struct smb_filename *name = NULL;
+
+               name = synthetic_smb_fname(ctx, fname, NULL, &smb_fname->st, 0);
+               if (name == NULL) {
+                       TALLOC_FREE(smb_fname);
+                       TALLOC_FREE(fname);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               *_smb_fname = name;
+       }
+
        TALLOC_FREE(smb_fname);
+       TALLOC_FREE(fname);
+
        if (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
                dptr_SeekDir(dirptr, prev_dirpos);
                return status;
        }
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
 
        *_last_entry_off = last_entry_off;
        return NT_STATUS_OK;
@@ -2585,9 +2610,10 @@ static NTSTATUS get_lanman2_dir_entry(TALLOC_CTX *ctx,
        return smbd_dirptr_lanman2_entry(ctx, conn, dirptr, flags2,
                                         path_mask, dirtype, info_level,
                                         requires_resume_key, dont_descend, ask_sharemode,
-                                        align, do_pad,
+                                        true, align, do_pad,
                                         ppdata, base_data, end_data,
                                         space_remaining,
+                                        NULL,
                                         got_exact_match,
                                         last_entry_off, name_list, NULL);
 }
@@ -2864,9 +2890,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
        dptr_num = dptr_dnum(dirptr);
        DEBUG(4,("dptr_num is %d, wcard = %s, attr = %d\n", dptr_num, mask, dirtype));
 
-       /* Initialize per TRANS2_FIND_FIRST operation data */
-       dptr_init_search_op(dirptr);
-
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
@@ -3234,9 +3257,6 @@ total_data=%u (should be %u)\n", (unsigned int)total_data, (unsigned int)IVAL(pd
                dptr_TellDir(dirptr),
                (int)backup_priv));
 
-       /* Initialize per TRANS2_FIND_NEXT operation data */
-       dptr_init_search_op(dirptr);
-
        /* We don't need to check for VOL here as this is returned by
                a different TRANS2 call. */
 
@@ -3455,7 +3475,8 @@ NTSTATUS smbd_do_qfsinfo(struct smbXsrv_connection *xconn,
        ZERO_STRUCT(smb_fname);
        smb_fname.base_name = discard_const_p(char, filename);
 
-       if(SMB_VFS_STAT(conn, &smb_fname) != 0) {
+       if(info_level != SMB_FS_QUOTA_INFORMATION
+          && SMB_VFS_STAT(conn, &smb_fname) != 0) {
                DEBUG(2,("stat of . failed (%s)\n", strerror(errno)));
                return map_nt_error_from_unix(errno);
        }
@@ -3901,7 +3922,7 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                                return NT_STATUS_INVALID_LEVEL;
                        }
 
-                       rc = SMB_VFS_STATVFS(conn, filename, &svfs);
+                       rc = SMB_VFS_STATVFS(conn, &smb_fname, &svfs);
 
                        if (!rc) {
                                data_len = 56;
@@ -4035,7 +4056,8 @@ cBytesSector=%u, cUnitTotal=%u, cUnitAvail=%d\n", (unsigned int)bsize, (unsigned
                                SIVAL(pdata,84,0x100); /* Don't support mac... */
                                break;
                        }
-                       /* drop through */
+
+                       FALL_THROUGH;
                default:
                        return NT_STATUS_INVALID_LEVEL;
        }
@@ -5479,7 +5501,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                return NT_STATUS_DOS(ERRDOS, ERRbadlink);
 #endif
                                link_len = SMB_VFS_READLINK(conn,
-                                                      smb_fname->base_name,
+                                                      smb_fname,
                                                       buffer, PATH_MAX);
                                if (link_len == -1) {
                                        return map_nt_error_from_unix(errno);
@@ -5519,7 +5541,7 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                } else {
                                        file_acl =
                                            SMB_VFS_SYS_ACL_GET_FILE(conn,
-                                               smb_fname->base_name,
+                                               smb_fname,
                                                SMB_ACL_TYPE_ACCESS,
                                                talloc_tos());
                                }
@@ -5537,14 +5559,14 @@ NTSTATUS smbd_do_qfilepathinfo(connection_struct *conn,
                                                def_acl =
                                                    SMB_VFS_SYS_ACL_GET_FILE(
                                                            conn,
-                                                           fsp->fsp_name->base_name,
+                                                           fsp->fsp_name,
                                                            SMB_ACL_TYPE_DEFAULT,
                                                            talloc_tos());
                                        } else {
                                                def_acl =
                                                    SMB_VFS_SYS_ACL_GET_FILE(
                                                            conn,
-                                                           smb_fname->base_name,
+                                                           smb_fname,
                                                            SMB_ACL_TYPE_DEFAULT,
                                                            talloc_tos());
                                        }
@@ -6145,8 +6167,7 @@ NTSTATUS hardlink_internals(TALLOC_CTX *ctx,
        DEBUG(10,("hardlink_internals: doing hard link %s -> %s\n",
                  smb_fname_old->base_name, smb_fname_new->base_name));
 
-       if (SMB_VFS_LINK(conn, smb_fname_old->base_name,
-                        smb_fname_new->base_name) != 0) {
+       if (SMB_VFS_LINK(conn, smb_fname_old, smb_fname_new) != 0) {
                status = map_nt_error_from_unix(errno);
                DEBUG(3,("hardlink_internals: Error %s hard link %s -> %s\n",
                         nt_errstr(status), smb_fname_old->base_name,
@@ -6334,7 +6355,9 @@ static NTSTATUS smb_set_file_size(connection_struct *conn,
                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
        }
 
-       DEBUG(6,("smb_set_file_size: size: %.0f ", (double)size));
+       DBG_INFO("size: %"PRIu64", file_size_stat=%"PRIu64"\n",
+                (uint64_t)size,
+                get_file_size_stat(psbuf));
 
        if (size == get_file_size_stat(psbuf)) {
                return NT_STATUS_OK;
@@ -6605,10 +6628,9 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
                                       struct smb_request *req,
                                       const char *pdata,
                                       int total_data,
-                                      const struct smb_filename *smb_fname)
+                                      const struct smb_filename *new_smb_fname)
 {
        char *link_target = NULL;
-       const char *newname = smb_fname->base_name;
        TALLOC_CTX *ctx = talloc_tos();
 
        /* Set a symbolic link. */
@@ -6630,9 +6652,9 @@ static NTSTATUS smb_set_file_unix_link(connection_struct *conn,
        }
 
        DEBUG(10,("smb_set_file_unix_link: SMB_SET_FILE_UNIX_LINK doing symlink %s -> %s\n",
-                       newname, link_target ));
+                       new_smb_fname->base_name, link_target ));
 
-       if (SMB_VFS_SYMLINK(conn,link_target,newname) != 0) {
+       if (SMB_VFS_SYMLINK(conn,link_target,new_smb_fname) != 0) {
                return map_nt_error_from_unix(errno);
        }
 
@@ -7013,6 +7035,12 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                 * the newname instead.
                 */
                char *base_name = NULL;
+               uint32_t ucf_flags = UCF_SAVE_LCOMP |
+                       ucf_flags_from_smb_request(req);
+
+               if (dest_has_wcard) {
+                       ucf_flags |= UCF_ALWAYS_ALLOW_WCARD_LCOMP;
+               }
 
                /* newname must *not* be a stream name. */
                if (newname[0] == ':') {
@@ -7045,10 +7073,7 @@ static NTSTATUS smb_file_rename_information(connection_struct *conn,
                }
 
                status = unix_convert(ctx, conn, base_name, &smb_fname_dst,
-                                     (UCF_SAVE_LCOMP |
-                                         (dest_has_wcard ?
-                                             UCF_ALWAYS_ALLOW_WCARD_LCOMP :
-                                             0)));
+                                       ucf_flags);
 
                /* If an error we expect this to be
                 * NT_STATUS_OBJECT_PATH_NOT_FOUND */
@@ -7148,13 +7173,13 @@ static NTSTATUS smb_set_posix_acl(connection_struct *conn,
                (unsigned int)num_def_acls));
 
        if (valid_file_acls && !set_unix_posix_acl(conn, fsp,
-               smb_fname->base_name, num_file_acls,
+               smb_fname, num_file_acls,
                pdata + SMB_POSIX_ACL_HEADER_SIZE)) {
                return map_nt_error_from_unix(errno);
        }
 
        if (valid_def_acls && !set_unix_posix_default_acl(conn,
-               smb_fname->base_name, &smb_fname->st, num_def_acls,
+               smb_fname, num_def_acls,
                pdata + SMB_POSIX_ACL_HEADER_SIZE +
                (num_file_acls*SMB_POSIX_ACL_ENTRY_SIZE))) {
                return map_nt_error_from_unix(errno);
@@ -7573,7 +7598,7 @@ static NTSTATUS smb_unix_mknod(connection_struct *conn,
                  (unsigned int)unixmode, smb_fname_str_dbg(smb_fname)));
 
        /* Ok - do the mknod. */
-       if (SMB_VFS_MKNOD(conn, smb_fname->base_name, unixmode, dev) != 0) {
+       if (SMB_VFS_MKNOD(conn, smb_fname, unixmode, dev) != 0) {
                return map_nt_error_from_unix(errno);
        }
 
@@ -7587,7 +7612,7 @@ static NTSTATUS smb_unix_mknod(connection_struct *conn,
                                    &parent, NULL)) {
                        return NT_STATUS_NO_MEMORY;
                }
-               inherit_access_posix_acl(conn, parent, smb_fname->base_name,
+               inherit_access_posix_acl(conn, parent, smb_fname,
                                         unixmode);
                TALLOC_FREE(parent);
        }
@@ -7779,10 +7804,10 @@ static NTSTATUS smb_set_file_unix_basic(connection_struct *conn,
 
                DEBUG(10,("smb_set_file_unix_basic: SMB_SET_FILE_UNIX_BASIC "
                          "changing group %u for file %s\n",
-                         (unsigned int)set_owner,
+                         (unsigned int)set_grp,
                          smb_fname_str_dbg(smb_fname)));
                if (fsp && fsp->fh->fd != -1) {
-                       ret = SMB_VFS_FCHOWN(fsp, set_owner, (gid_t)-1);
+                       ret = SMB_VFS_FCHOWN(fsp, (uid_t)-1, set_grp);
                } else {
                        /*
                         * UNIX extensions calls must always operate
@@ -7898,7 +7923,7 @@ static NTSTATUS smb_set_file_unix_info2(connection_struct *conn,
                        /* XXX: we should be  using SMB_VFS_FCHFLAGS here. */
                        return NT_STATUS_NOT_SUPPORTED;
                } else {
-                       if (SMB_VFS_CHFLAGS(conn, smb_fname->base_name,
+                       if (SMB_VFS_CHFLAGS(conn, smb_fname,
                                            stat_fflags) != 0) {
                                return map_nt_error_from_unix(errno);
                        }
@@ -8284,14 +8309,15 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                                int total_data,
                                struct smb_filename *smb_fname)
 {
+       struct server_id self = messaging_server_id(conn->sconn->msg_ctx);
        NTSTATUS status = NT_STATUS_OK;
        files_struct *fsp = NULL;
        uint16_t flags = 0;
        char del = 1;
        int info = 0;
        int create_options = 0;
-       int i;
        struct share_mode_lock *lck = NULL;
+       bool other_nonposix_opens;
 
        if (total_data < 2) {
                return NT_STATUS_INVALID_PARAMETER;
@@ -8354,25 +8380,12 @@ static NTSTATUS smb_posix_unlink(connection_struct *conn,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       /*
-        * See if others still have the file open. If this is the case, then
-        * don't delete. If all opens are POSIX delete we can set the delete
-        * on close disposition.
-        */
-       for (i=0; i<lck->data->num_share_modes; i++) {
-               struct share_mode_entry *e = &lck->data->share_modes[i];
-               if (is_valid_share_mode_entry(e)) {
-                       if (e->flags & SHARE_MODE_FLAG_POSIX_OPEN) {
-                               continue;
-                       }
-                       if (share_mode_stale_pid(lck->data, i)) {
-                               continue;
-                       }
-                       /* Fail with sharing violation. */
-                       TALLOC_FREE(lck);
-                       close_file(req, fsp, NORMAL_CLOSE);
-                       return NT_STATUS_SHARING_VIOLATION;
-               }
+       other_nonposix_opens = has_other_nonposix_opens(lck, fsp, self);
+       if (other_nonposix_opens) {
+               /* Fail with sharing violation. */
+               TALLOC_FREE(lck);
+               close_file(req, fsp, NORMAL_CLOSE);
+               return NT_STATUS_SHARING_VIOLATION;
        }
 
        /*
@@ -8543,6 +8556,11 @@ NTSTATUS smbd_do_setfilepathinfo(connection_struct *conn,
                        break;
                }
 
+               /* [MS-SMB2] 3.3.5.21.1 states we MUST fail with STATUS_NOT_SUPPORTED. */
+               case SMB_FILE_VALID_DATA_LENGTH_INFORMATION:
+               case SMB_FILE_SHORT_NAME_INFORMATION:
+                       return NT_STATUS_NOT_SUPPORTED;
+
                /*
                 * CIFS UNIX extensions.
                 */