s3: smbd: Change open_streams_for_delete() to take a struct smb_filename *.
[samba.git] / source3 / smbd / vfs.c
index 0f443eee0b17775774008053d191088b722877f9..a1154aee784d6b242534429a6b0207a59393b459 100644 (file)
@@ -1076,15 +1076,32 @@ NTSTATUS check_reduced_name_with_privilege(connection_struct *conn,
        }
 
        rootdir_len = strlen(conn_rootdir);
-       if (strncmp(conn_rootdir, resolved_name, rootdir_len) != 0) {
-               DEBUG(2, ("check_reduced_name_with_privilege: Bad access "
-                       "attempt: %s is a symlink outside the "
-                       "share path\n",
-                       dir_name));
-               DEBUGADD(2, ("conn_rootdir =%s\n", conn_rootdir));
-               DEBUGADD(2, ("resolved_name=%s\n", resolved_name));
-               status = NT_STATUS_ACCESS_DENIED;
-               goto err;
+
+       /*
+        * In the case of rootdir_len == 1, we know that conn_rootdir is
+        * "/", and we also know that resolved_name starts with a slash.
+        * So, in this corner case, resolved_name is automatically a
+        * sub-directory of the conn_rootdir. Thus we can skip the string
+        * comparison and the next character checks (which are even
+        * wrong in this case).
+        */
+       if (rootdir_len != 1) {
+               bool matched;
+
+               matched = (strncmp(conn_rootdir, resolved_name,
+                               rootdir_len) == 0);
+
+               if (!matched || (resolved_name[rootdir_len] != '/' &&
+                                resolved_name[rootdir_len] != '\0')) {
+                       DEBUG(2, ("check_reduced_name_with_privilege: Bad "
+                               "access attempt: %s is a symlink outside the "
+                               "share path\n",
+                               dir_name));
+                       DEBUGADD(2, ("conn_rootdir =%s\n", conn_rootdir));
+                       DEBUGADD(2, ("resolved_name=%s\n", resolved_name));
+                       status = NT_STATUS_ACCESS_DENIED;
+                       goto err;
+               }
        }
 
        /* Now ensure that the last component either doesn't
@@ -1226,15 +1243,33 @@ NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
                }
 
                rootdir_len = strlen(conn_rootdir);
-               if (strncmp(conn_rootdir, resolved_name,
-                               rootdir_len) != 0) {
-                       DEBUG(2, ("check_reduced_name: Bad access "
-                               "attempt: %s is a symlink outside the "
-                               "share path\n", fname));
-                       DEBUGADD(2, ("conn_rootdir =%s\n", conn_rootdir));
-                       DEBUGADD(2, ("resolved_name=%s\n", resolved_name));
-                       SAFE_FREE(resolved_name);
-                       return NT_STATUS_ACCESS_DENIED;
+
+               /*
+                * In the case of rootdir_len == 1, we know that
+                * conn_rootdir is "/", and we also know that
+                * resolved_name starts with a slash.  So, in this
+                * corner case, resolved_name is automatically a
+                * sub-directory of the conn_rootdir. Thus we can skip
+                * the string comparison and the next character checks
+                * (which are even wrong in this case).
+                */
+               if (rootdir_len != 1) {
+                       bool matched;
+
+                       matched = (strncmp(conn_rootdir, resolved_name,
+                                       rootdir_len) == 0);
+                       if (!matched || (resolved_name[rootdir_len] != '/' &&
+                                        resolved_name[rootdir_len] != '\0')) {
+                               DEBUG(2, ("check_reduced_name: Bad access "
+                                       "attempt: %s is a symlink outside the "
+                                       "share path\n", fname));
+                               DEBUGADD(2, ("conn_rootdir =%s\n",
+                                            conn_rootdir));
+                               DEBUGADD(2, ("resolved_name=%s\n",
+                                            resolved_name));
+                               SAFE_FREE(resolved_name);
+                               return NT_STATUS_ACCESS_DENIED;
+                       }
                }
 
                /* Extra checks if all symlinks are disallowed. */
@@ -1269,8 +1304,7 @@ NTSTATUS check_reduced_name(connection_struct *conn, const char *fname)
 
   out:
 
-       DEBUG(3,("check_reduced_name: %s reduced to %s\n", fname,
-                resolved_name));
+       DBG_INFO("%s reduced to %s\n", fname, resolved_name);
        SAFE_FREE(resolved_name);
        return NT_STATUS_OK;
 }
@@ -1310,7 +1344,7 @@ NTSTATUS vfs_stat_fsp(files_struct *fsp)
        int ret;
 
        if(fsp->fh->fd == -1) {
-               if (fsp->posix_open) {
+               if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
                        ret = SMB_VFS_LSTAT(fsp->conn, fsp->fsp_name);
                } else {
                        ret = SMB_VFS_STAT(fsp->conn, fsp->fsp_name);
@@ -1331,14 +1365,19 @@ NTSTATUS vfs_stat_fsp(files_struct *fsp)
  */
 NTSTATUS vfs_streaminfo(connection_struct *conn,
                        struct files_struct *fsp,
-                       const char *fname,
+                       const struct smb_filename *smb_fname,
                        TALLOC_CTX *mem_ctx,
                        unsigned int *num_streams,
                        struct stream_struct **streams)
 {
        *num_streams = 0;
        *streams = NULL;
-       return SMB_VFS_STREAMINFO(conn, fsp, fname, mem_ctx, num_streams, streams);
+       return SMB_VFS_STREAMINFO(conn,
+                       fsp,
+                       smb_fname,
+                       mem_ctx,
+                       num_streams,
+                       streams);
 }
 
 /*
@@ -1370,12 +1409,12 @@ uint64_t smb_vfs_call_disk_free(struct vfs_handle_struct *handle,
        return handle->fns->disk_free_fn(handle, path, bsize, dfree, dsize);
 }
 
-int smb_vfs_call_get_quota(struct vfs_handle_struct *handle,
+int smb_vfs_call_get_quota(struct vfs_handle_struct *handle, const char *path,
                           enum SMB_QUOTA_TYPE qtype, unid_t id,
                           SMB_DISK_QUOTA *qt)
 {
        VFS_FIND(get_quota);
-       return handle->fns->get_quota_fn(handle, qtype, id, qt);
+       return handle->fns->get_quota_fn(handle, path, qtype, id, qt);
 }
 
 int smb_vfs_call_set_quota(struct vfs_handle_struct *handle,
@@ -1418,11 +1457,12 @@ NTSTATUS smb_vfs_call_get_dfs_referrals(struct vfs_handle_struct *handle,
 }
 
 DIR *smb_vfs_call_opendir(struct vfs_handle_struct *handle,
-                                    const char *fname, const char *mask,
-                                    uint32_t attributes)
+                                       const struct smb_filename *smb_fname,
+                                       const char *mask,
+                                       uint32_t attributes)
 {
        VFS_FIND(opendir);
-       return handle->fns->opendir_fn(handle, fname, mask, attributes);
+       return handle->fns->opendir_fn(handle, smb_fname, mask, attributes);
 }
 
 DIR *smb_vfs_call_fdopendir(struct vfs_handle_struct *handle,
@@ -1463,17 +1503,19 @@ void smb_vfs_call_rewind_dir(struct vfs_handle_struct *handle,
        handle->fns->rewind_dir_fn(handle, dirp);
 }
 
-int smb_vfs_call_mkdir(struct vfs_handle_struct *handle, const char *path,
-                      mode_t mode)
+int smb_vfs_call_mkdir(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       mode_t mode)
 {
        VFS_FIND(mkdir);
-       return handle->fns->mkdir_fn(handle, path, mode);
+       return handle->fns->mkdir_fn(handle, smb_fname, mode);
 }
 
-int smb_vfs_call_rmdir(struct vfs_handle_struct *handle, const char *path)
+int smb_vfs_call_rmdir(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname)
 {
        VFS_FIND(rmdir);
-       return handle->fns->rmdir_fn(handle, path);
+       return handle->fns->rmdir_fn(handle, smb_fname);
 }
 
 int smb_vfs_call_closedir(struct vfs_handle_struct *handle,
@@ -1550,8 +1592,9 @@ ssize_t smb_vfs_call_pread(struct vfs_handle_struct *handle,
 }
 
 struct smb_vfs_call_pread_state {
-       ssize_t (*recv_fn)(struct tevent_req *req, int *err);
+       ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
        ssize_t retval;
+       struct vfs_aio_state vfs_aio_state;
 };
 
 static void smb_vfs_call_pread_done(struct tevent_req *subreq);
@@ -1589,27 +1632,26 @@ static void smb_vfs_call_pread_done(struct tevent_req *subreq)
                subreq, struct tevent_req);
        struct smb_vfs_call_pread_state *state = tevent_req_data(
                req, struct smb_vfs_call_pread_state);
-       int err;
 
-       state->retval = state->recv_fn(subreq, &err);
+       state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
        TALLOC_FREE(subreq);
        if (state->retval == -1) {
-               tevent_req_error(req, err);
+               tevent_req_error(req, state->vfs_aio_state.error);
                return;
        }
        tevent_req_done(req);
 }
 
-ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req, int *perrno)
+ssize_t SMB_VFS_PREAD_RECV(struct tevent_req *req,
+                          struct vfs_aio_state *vfs_aio_state)
 {
        struct smb_vfs_call_pread_state *state = tevent_req_data(
                req, struct smb_vfs_call_pread_state);
-       int err;
 
-       if (tevent_req_is_unix_error(req, &err)) {
-               *perrno = err;
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
                return -1;
        }
+       *vfs_aio_state = state->vfs_aio_state;
        return state->retval;
 }
 
@@ -1630,8 +1672,9 @@ ssize_t smb_vfs_call_pwrite(struct vfs_handle_struct *handle,
 }
 
 struct smb_vfs_call_pwrite_state {
-       ssize_t (*recv_fn)(struct tevent_req *req, int *err);
+       ssize_t (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
        ssize_t retval;
+       struct vfs_aio_state vfs_aio_state;
 };
 
 static void smb_vfs_call_pwrite_done(struct tevent_req *subreq);
@@ -1669,27 +1712,26 @@ static void smb_vfs_call_pwrite_done(struct tevent_req *subreq)
                subreq, struct tevent_req);
        struct smb_vfs_call_pwrite_state *state = tevent_req_data(
                req, struct smb_vfs_call_pwrite_state);
-       int err;
 
-       state->retval = state->recv_fn(subreq, &err);
+       state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
        TALLOC_FREE(subreq);
        if (state->retval == -1) {
-               tevent_req_error(req, err);
+               tevent_req_error(req, state->vfs_aio_state.error);
                return;
        }
        tevent_req_done(req);
 }
 
-ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req, int *perrno)
+ssize_t SMB_VFS_PWRITE_RECV(struct tevent_req *req,
+                           struct vfs_aio_state *vfs_aio_state)
 {
        struct smb_vfs_call_pwrite_state *state = tevent_req_data(
                req, struct smb_vfs_call_pwrite_state);
-       int err;
 
-       if (tevent_req_is_unix_error(req, &err)) {
-               *perrno = err;
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
                return -1;
        }
+       *vfs_aio_state = state->vfs_aio_state;
        return state->retval;
 }
 
@@ -1734,8 +1776,9 @@ int smb_vfs_call_fsync(struct vfs_handle_struct *handle,
 }
 
 struct smb_vfs_call_fsync_state {
-       int (*recv_fn)(struct tevent_req *req, int *err);
+       int (*recv_fn)(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state);
        int retval;
+       struct vfs_aio_state vfs_aio_state;
 };
 
 static void smb_vfs_call_fsync_done(struct tevent_req *subreq);
@@ -1770,27 +1813,25 @@ static void smb_vfs_call_fsync_done(struct tevent_req *subreq)
                subreq, struct tevent_req);
        struct smb_vfs_call_fsync_state *state = tevent_req_data(
                req, struct smb_vfs_call_fsync_state);
-       int err;
 
-       state->retval = state->recv_fn(subreq, &err);
+       state->retval = state->recv_fn(subreq, &state->vfs_aio_state);
        TALLOC_FREE(subreq);
        if (state->retval == -1) {
-               tevent_req_error(req, err);
+               tevent_req_error(req, state->vfs_aio_state.error);
                return;
        }
        tevent_req_done(req);
 }
 
-int SMB_VFS_FSYNC_RECV(struct tevent_req *req, int *perrno)
+int SMB_VFS_FSYNC_RECV(struct tevent_req *req, struct vfs_aio_state *vfs_aio_state)
 {
        struct smb_vfs_call_fsync_state *state = tevent_req_data(
                req, struct smb_vfs_call_fsync_state);
-       int err;
 
-       if (tevent_req_is_unix_error(req, &err)) {
-               *perrno = err;
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
                return -1;
        }
+       *vfs_aio_state = state->vfs_aio_state;
        return state->retval;
 }
 
@@ -1831,11 +1872,12 @@ int smb_vfs_call_unlink(struct vfs_handle_struct *handle,
        return handle->fns->unlink_fn(handle, smb_fname);
 }
 
-int smb_vfs_call_chmod(struct vfs_handle_struct *handle, const char *path,
-                      mode_t mode)
+int smb_vfs_call_chmod(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       mode_t mode)
 {
        VFS_FIND(chmod);
-       return handle->fns->chmod_fn(handle, path, mode);
+       return handle->fns->chmod_fn(handle, smb_fname, mode);
 }
 
 int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
@@ -1845,11 +1887,13 @@ int smb_vfs_call_fchmod(struct vfs_handle_struct *handle,
        return handle->fns->fchmod_fn(handle, fsp, mode);
 }
 
-int smb_vfs_call_chown(struct vfs_handle_struct *handle, const char *path,
-                      uid_t uid, gid_t gid)
+int smb_vfs_call_chown(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       uid_t uid,
+                       gid_t gid)
 {
        VFS_FIND(chown);
-       return handle->fns->chown_fn(handle, path, uid, gid);
+       return handle->fns->chown_fn(handle, smb_fname, uid, gid);
 }
 
 int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
@@ -1859,20 +1903,19 @@ int smb_vfs_call_fchown(struct vfs_handle_struct *handle,
        return handle->fns->fchown_fn(handle, fsp, uid, gid);
 }
 
-int smb_vfs_call_lchown(struct vfs_handle_struct *handle, const char *path,
-                       uid_t uid, gid_t gid)
+int smb_vfs_call_lchown(struct vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       uid_t uid,
+                       gid_t gid)
 {
        VFS_FIND(lchown);
-       return handle->fns->lchown_fn(handle, path, uid, gid);
+       return handle->fns->lchown_fn(handle, smb_fname, uid, gid);
 }
 
 NTSTATUS vfs_chown_fsp(files_struct *fsp, uid_t uid, gid_t gid)
 {
        int ret;
        bool as_root = false;
-       const char *path;
-       char *saved_dir = NULL;
-       char *parent_dir = NULL;
        NTSTATUS status;
 
        if (fsp->fh->fd != -1) {
@@ -1895,8 +1938,10 @@ NTSTATUS vfs_chown_fsp(files_struct *fsp, uid_t uid, gid_t gid)
                 * and always act using lchown to ensure we
                 * don't deref any symbolic links.
                 */
+               char *saved_dir = NULL;
+               char *parent_dir = NULL;
                const char *final_component = NULL;
-               struct smb_filename local_fname;
+               struct smb_filename *local_smb_fname = NULL;
 
                saved_dir = vfs_GetWd(talloc_tos(),fsp->conn);
                if (!saved_dir) {
@@ -1920,33 +1965,56 @@ NTSTATUS vfs_chown_fsp(files_struct *fsp, uid_t uid, gid_t gid)
                        return map_nt_error_from_unix(errno);
                }
 
-               ZERO_STRUCT(local_fname);
-               local_fname.base_name = discard_const_p(char, final_component);
+               local_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       final_component,
+                                       NULL,
+                                       NULL);
+               if (local_smb_fname == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
 
                /* Must use lstat here. */
-               ret = SMB_VFS_LSTAT(fsp->conn, &local_fname);
+               ret = SMB_VFS_LSTAT(fsp->conn, local_smb_fname);
                if (ret == -1) {
                        status = map_nt_error_from_unix(errno);
                        goto out;
                }
 
                /* Ensure it matches the fsp stat. */
-               if (!check_same_stat(&local_fname.st, &fsp->fsp_name->st)) {
+               if (!check_same_stat(&local_smb_fname->st,
+                               &fsp->fsp_name->st)) {
                         status = NT_STATUS_ACCESS_DENIED;
                        goto out;
                 }
-                path = final_component;
-        } else {
-                path = fsp->fsp_name->base_name;
-        }
 
-       if (fsp->posix_open || as_root) {
                ret = SMB_VFS_LCHOWN(fsp->conn,
-                       path,
+                       local_smb_fname,
+                       uid, gid);
+
+               if (ret == 0) {
+                       status = NT_STATUS_OK;
+               } else {
+                       status = map_nt_error_from_unix(errno);
+               }
+
+  out:
+
+               vfs_ChDir(fsp->conn,saved_dir);
+               TALLOC_FREE(local_smb_fname);
+               TALLOC_FREE(saved_dir);
+               TALLOC_FREE(parent_dir);
+
+               return status;
+       }
+
+       if (fsp->posix_flags & FSP_POSIX_FLAGS_OPEN) {
+               ret = SMB_VFS_LCHOWN(fsp->conn,
+                       fsp->fsp_name,
                        uid, gid);
        } else {
                ret = SMB_VFS_CHOWN(fsp->conn,
-                       path,
+                       fsp->fsp_name,
                        uid, gid);
        }
 
@@ -1955,14 +2023,6 @@ NTSTATUS vfs_chown_fsp(files_struct *fsp, uid_t uid, gid_t gid)
        } else {
                status = map_nt_error_from_unix(errno);
        }
-
-  out:
-
-       if (as_root) {
-               vfs_ChDir(fsp->conn,saved_dir);
-               TALLOC_FREE(saved_dir);
-               TALLOC_FREE(parent_dir);
-       }
        return status;
 }
 
@@ -2069,13 +2129,13 @@ struct file_id smb_vfs_call_file_id_create(struct vfs_handle_struct *handle,
 
 NTSTATUS smb_vfs_call_streaminfo(struct vfs_handle_struct *handle,
                                 struct files_struct *fsp,
-                                const char *fname,
+                                const struct smb_filename *smb_fname,
                                 TALLOC_CTX *mem_ctx,
                                 unsigned int *num_streams,
                                 struct stream_struct **streams)
 {
        VFS_FIND(streaminfo);
-       return handle->fns->streaminfo_fn(handle, fsp, fname, mem_ctx,
+       return handle->fns->streaminfo_fn(handle, fsp, smb_fname, mem_ctx,
                                          num_streams, streams);
 }
 
@@ -2227,13 +2287,17 @@ NTSTATUS smb_vfs_call_fget_nt_acl(struct vfs_handle_struct *handle,
 }
 
 NTSTATUS smb_vfs_call_get_nt_acl(struct vfs_handle_struct *handle,
-                                const char *name,
+                                const struct smb_filename *smb_fname,
                                 uint32_t security_info,
                                 TALLOC_CTX *mem_ctx,
                                 struct security_descriptor **ppdesc)
 {
        VFS_FIND(get_nt_acl);
-       return handle->fns->get_nt_acl_fn(handle, name, security_info, mem_ctx, ppdesc);
+       return handle->fns->get_nt_acl_fn(handle,
+                               smb_fname,
+                               security_info,
+                               mem_ctx,
+                               ppdesc);
 }
 
 NTSTATUS smb_vfs_call_fset_nt_acl(struct vfs_handle_struct *handle,
@@ -2260,11 +2324,12 @@ NTSTATUS smb_vfs_call_audit_file(struct vfs_handle_struct *handle,
                                          access_denied);
 }
 
-int smb_vfs_call_chmod_acl(struct vfs_handle_struct *handle, const char *name,
-                          mode_t mode)
+int smb_vfs_call_chmod_acl(struct vfs_handle_struct *handle,
+               const struct smb_filename *smb_fname,
+               mode_t mode)
 {
        VFS_FIND(chmod_acl);
-       return handle->fns->chmod_acl_fn(handle, name, mode);
+       return handle->fns->chmod_acl_fn(handle, smb_fname, mode);
 }
 
 int smb_vfs_call_fchmod_acl(struct vfs_handle_struct *handle,