lib: Make sid_parse return the parsed length
[samba.git] / source3 / modules / vfs_default.c
index b1c4acc482dd52a9ce20bff19a6ba76b881d2fa8..7ce3775e54f0aa7b841a4f6e9a7cd89e398f2b6f 100644 (file)
@@ -686,7 +686,7 @@ static struct tevent_req *vfswrap_pread_send(struct vfs_handle_struct *handle,
        SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
 
        subreq = pthreadpool_tevent_job_send(
-               state, ev, handle->conn->sconn->raw_thread_pool,
+               state, ev, handle->conn->sconn->pool,
                vfs_pread_do, state);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -742,8 +742,18 @@ static void vfs_pread_done(struct tevent_req *subreq)
        TALLOC_FREE(subreq);
        SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
        talloc_set_destructor(state, NULL);
-       if (tevent_req_error(req, ret)) {
-               return;
+       if (ret != 0) {
+               if (ret != EAGAIN) {
+                       tevent_req_error(req, ret);
+                       return;
+               }
+               /*
+                * If we get EAGAIN from pthreadpool_tevent_job_recv() this
+                * means the lower level pthreadpool failed to create a new
+                * thread. Fallback to sync processing in that case to allow
+                * some progress for the client.
+                */
+               vfs_pread_do(state);
        }
 
        tevent_req_done(req);
@@ -804,7 +814,7 @@ static struct tevent_req *vfswrap_pwrite_send(struct vfs_handle_struct *handle,
        SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
 
        subreq = pthreadpool_tevent_job_send(
-               state, ev, handle->conn->sconn->raw_thread_pool,
+               state, ev, handle->conn->sconn->pool,
                vfs_pwrite_do, state);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
@@ -860,8 +870,18 @@ static void vfs_pwrite_done(struct tevent_req *subreq)
        TALLOC_FREE(subreq);
        SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
        talloc_set_destructor(state, NULL);
-       if (tevent_req_error(req, ret)) {
-               return;
+       if (ret != 0) {
+               if (ret != EAGAIN) {
+                       tevent_req_error(req, ret);
+                       return;
+               }
+               /*
+                * If we get EAGAIN from pthreadpool_tevent_job_recv() this
+                * means the lower level pthreadpool failed to create a new
+                * thread. Fallback to sync processing in that case to allow
+                * some progress for the client.
+                */
+               vfs_pwrite_do(state);
        }
 
        tevent_req_done(req);
@@ -914,8 +934,7 @@ static struct tevent_req *vfswrap_fsync_send(struct vfs_handle_struct *handle,
        SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
 
        subreq = pthreadpool_tevent_job_send(
-               state, ev, handle->conn->sconn->raw_thread_pool,
-               vfs_fsync_do, state);
+               state, ev, handle->conn->sconn->pool, vfs_fsync_do, state);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -969,8 +988,18 @@ static void vfs_fsync_done(struct tevent_req *subreq)
        TALLOC_FREE(subreq);
        SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
        talloc_set_destructor(state, NULL);
-       if (tevent_req_error(req, ret)) {
-               return;
+       if (ret != 0) {
+               if (ret != EAGAIN) {
+                       tevent_req_error(req, ret);
+                       return;
+               }
+               /*
+                * If we get EAGAIN from pthreadpool_tevent_job_recv() this
+                * means the lower level pthreadpool failed to create a new
+                * thread. Fallback to sync processing in that case to allow
+                * some progress for the client.
+                */
+               vfs_fsync_do(state);
        }
 
        tevent_req_done(req);
@@ -996,10 +1025,7 @@ static off_t vfswrap_lseek(vfs_handle_struct *handle, files_struct *fsp, off_t o
 
        START_PROFILE(syscall_lseek);
 
-       /* Cope with 'stat' file opens. */
-       if (fsp->fh->fd != -1)
-               result = lseek(fsp->fh->fd, offset, whence);
-
+       result = lseek(fsp->fh->fd, offset, whence);
        /*
         * We want to maintain the fiction that we can seek
         * on a fifo for file system purposes. This allows
@@ -1329,6 +1355,7 @@ static NTSTATUS vfswrap_fsctl(struct vfs_handle_struct *handle,
                 *
                 * but I have to check that --metze
                 */
+               struct sid_parse_ret ret;
                struct dom_sid sid;
                struct dom_sid_buf buf;
                uid_t uid;
@@ -1347,7 +1374,8 @@ static NTSTATUS vfswrap_fsctl(struct vfs_handle_struct *handle,
                /* unknown 4 bytes: this is not the length of the sid :-(  */
                /*unknown = IVAL(pdata,0);*/
 
-               if (!sid_parse(_in_data + 4, sid_len, &sid)) {
+               ret = sid_parse(_in_data + 4, sid_len, &sid);
+               if (ret.len == -1) {
                        return NT_STATUS_INVALID_PARAMETER;
                }
                DEBUGADD(10, ("for SID: %s\n",
@@ -1493,7 +1521,7 @@ struct vfswrap_get_dos_attributes_state {
        struct vfs_aio_state aio_state;
        connection_struct *conn;
        TALLOC_CTX *mem_ctx;
-       const struct smb_vfs_ev_glue *evg;
+       struct tevent_context *ev;
        files_struct *dir_fsp;
        struct smb_filename *smb_fname;
        uint32_t dosmode;
@@ -1504,12 +1532,11 @@ static void vfswrap_get_dos_attributes_getxattr_done(struct tevent_req *subreq);
 
 static struct tevent_req *vfswrap_get_dos_attributes_send(
                        TALLOC_CTX *mem_ctx,
-                       const struct smb_vfs_ev_glue *evg,
+                       struct tevent_context *ev,
                        struct vfs_handle_struct *handle,
                        files_struct *dir_fsp,
                        struct smb_filename *smb_fname)
 {
-       struct tevent_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
        struct tevent_req *req = NULL;
        struct tevent_req *subreq = NULL;
        struct vfswrap_get_dos_attributes_state *state = NULL;
@@ -1523,13 +1550,13 @@ static struct tevent_req *vfswrap_get_dos_attributes_send(
        *state = (struct vfswrap_get_dos_attributes_state) {
                .conn = dir_fsp->conn,
                .mem_ctx = mem_ctx,
-               .evg = evg,
+               .ev = ev,
                .dir_fsp = dir_fsp,
                .smb_fname = smb_fname,
        };
 
        subreq = SMB_VFS_GETXATTRAT_SEND(state,
-                                        evg,
+                                        ev,
                                         dir_fsp,
                                         smb_fname,
                                         SAMBA_XATTR_DOS_ATTRIB,
@@ -1562,8 +1589,6 @@ static void vfswrap_get_dos_attributes_getxattr_done(struct tevent_req *subreq)
                                             &blob.data);
        TALLOC_FREE(subreq);
        if (xattr_size == -1) {
-               const struct smb_vfs_ev_glue *root_evg = NULL;
-
                status = map_nt_error_from_unix(state->aio_state.error);
 
                if (state->as_root) {
@@ -1576,14 +1601,15 @@ static void vfswrap_get_dos_attributes_getxattr_done(struct tevent_req *subreq)
                }
 
                state->as_root = true;
-               root_evg = smb_vfs_ev_glue_get_root_glue(state->evg);
 
+               become_root();
                subreq = SMB_VFS_GETXATTRAT_SEND(state,
-                                                root_evg,
+                                                state->ev,
                                                 state->dir_fsp,
                                                 state->smb_fname,
                                                 SAMBA_XATTR_DOS_ATTRIB,
                                                 sizeof(fstring));
+               unbecome_root();
                if (tevent_req_nomem(subreq, req)) {
                        return;
                }
@@ -1847,7 +1873,7 @@ static struct tevent_req *vfswrap_offload_write_send(
                return tevent_req_post(req, ev);
        }
 
-       state->src_ev = src_fsp->conn->user_ev_ctx;
+       state->src_ev = src_fsp->conn->sconn->ev_ctx;
        state->src_fsp = src_fsp;
 
        state->buf = talloc_array(state, uint8_t, num);
@@ -2526,11 +2552,8 @@ static bool vfswrap_lock(vfs_handle_struct *handle, files_struct *fsp, int op, o
 
        START_PROFILE(syscall_fcntl_lock);
 
-       if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
-                                               "smbd",
-                                               "force process locks",
-                                               false)) {
-               op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
+       if (fsp->use_ofd_locks) {
+               op = map_process_lock_to_ofd_lock(op);
        }
 
        result =  fcntl_lock(fsp->fh->fd, op, offset, count, type);
@@ -2554,11 +2577,8 @@ static bool vfswrap_getlock(vfs_handle_struct *handle, files_struct *fsp, off_t
 
        START_PROFILE(syscall_fcntl_getlock);
 
-       if (fsp->use_ofd_locks || !lp_parm_bool(SNUM(fsp->conn),
-                                               "smbd",
-                                               "force process locks",
-                                               false)) {
-               op = map_process_lock_to_ofd_lock(op, &fsp->use_ofd_locks);
+       if (fsp->use_ofd_locks) {
+               op = map_process_lock_to_ofd_lock(op);
        }
 
        result = fcntl_getlock(fsp->fh->fd, op, poffset, pcount, ptype, ppid);
@@ -2903,13 +2923,22 @@ static ssize_t vfswrap_getxattr(struct vfs_handle_struct *handle,
 }
 
 struct vfswrap_getxattrat_state {
-       int dirfd;
+       struct tevent_context *ev;
+       files_struct *dir_fsp;
+       const struct smb_filename *smb_fname;
+       struct tevent_req *req;
+
+       /*
+        * The following variables are talloced off "state" which is protected
+        * by a destructor and thus are guaranteed to be safe to be used in the
+        * job function in the worker thread.
+        */
        char *name;
-       size_t xattr_bufsize;
        const char *xattr_name;
-       ssize_t xattr_size;
        uint8_t *xattr_value;
+       struct security_unix_token *token;
 
+       ssize_t xattr_size;
        struct vfs_aio_state vfs_aio_state;
        SMBPROFILE_BYTES_ASYNC_STATE(profile_bytes);
 };
@@ -2920,23 +2949,26 @@ static int vfswrap_getxattrat_state_destructor(
        return -1;
 }
 
-static void vfswrap_getxattrat_do(void *private_data);
+static void vfswrap_getxattrat_do_sync(struct tevent_req *req);
+static void vfswrap_getxattrat_do_async(void *private_data);
 static void vfswrap_getxattrat_done(struct tevent_req *subreq);
 
 static struct tevent_req *vfswrap_getxattrat_send(
                        TALLOC_CTX *mem_ctx,
-                       const struct smb_vfs_ev_glue *evg,
+                       struct tevent_context *ev,
                        struct vfs_handle_struct *handle,
                        files_struct *dir_fsp,
                        const struct smb_filename *smb_fname,
                        const char *xattr_name,
                        size_t alloc_hint)
 {
-       struct tevent_context *ev = smb_vfs_ev_glue_ev_ctx(evg);
-       struct pthreadpool_tevent *tp = smb_vfs_ev_glue_tp_chdir_safe(evg);
        struct tevent_req *req = NULL;
        struct tevent_req *subreq = NULL;
        struct vfswrap_getxattrat_state *state = NULL;
+       size_t max_threads = 0;
+       bool have_per_thread_cwd = false;
+       bool have_per_thread_creds = false;
+       bool do_async = false;
 
        req = tevent_req_create(mem_ctx, &state,
                                struct vfswrap_getxattrat_state);
@@ -2944,19 +2976,49 @@ static struct tevent_req *vfswrap_getxattrat_send(
                return NULL;
        }
        *state = (struct vfswrap_getxattrat_state) {
-               .dirfd = dir_fsp->fh->fd,
-               .xattr_bufsize = alloc_hint,
+               .ev = ev,
+               .dir_fsp = dir_fsp,
+               .smb_fname = smb_fname,
+               .req = req,
        };
 
+       max_threads = pthreadpool_tevent_max_threads(dir_fsp->conn->sconn->pool);
+       if (max_threads >= 1) {
+               /*
+                * We need a non sync threadpool!
+                */
+               have_per_thread_cwd = per_thread_cwd_supported();
+       }
+#ifdef HAVE_LINUX_THREAD_CREDENTIALS
+       have_per_thread_creds = true;
+#endif
+       if (have_per_thread_cwd && have_per_thread_creds) {
+               do_async = true;
+       }
+
        SMBPROFILE_BYTES_ASYNC_START(syscall_asys_getxattrat, profile_p,
                                     state->profile_bytes, 0);
 
-       if (state->dirfd == -1) {
+       if (dir_fsp->fh->fd == -1) {
                DBG_ERR("Need a valid directory fd\n");
                tevent_req_error(req, EINVAL);
                return tevent_req_post(req, ev);
        }
 
+       if (alloc_hint > 0) {
+               state->xattr_value = talloc_zero_array(state,
+                                                      uint8_t,
+                                                      alloc_hint);
+               if (tevent_req_nomem(state->xattr_value, req)) {
+                       return tevent_req_post(req, ev);
+               }
+       }
+
+       if (!do_async) {
+               vfswrap_getxattrat_do_sync(req);
+               return tevent_req_post(req, ev);
+       }
+
        /*
         * Now allocate all parameters from a memory context that won't go away
         * no matter what. These paremeters will get used in threads and we
@@ -2974,22 +3036,32 @@ static struct tevent_req *vfswrap_getxattrat_send(
                return tevent_req_post(req, ev);
        }
 
-       if (state->xattr_bufsize > 0) {
-               state->xattr_value = talloc_zero_array(state,
-                                                      uint8_t,
-                                                      state->xattr_bufsize);
-               if (tevent_req_nomem(state->xattr_value, req)) {
-                       return tevent_req_post(req, ev);
-               }
+       /*
+        * This is a hot codepath so at first glance one might think we should
+        * somehow optimize away the token allocation and do a
+        * talloc_reference() or similar black magic instead. But due to the
+        * talloc_stackframe pool per SMB2 request this should be a simple copy
+        * without a malloc in most cases.
+        */
+       if (geteuid() == sec_initial_uid()) {
+               state->token = root_unix_token(state);
+       } else {
+               state->token = copy_unix_token(
+                                       state,
+                                       dir_fsp->conn->session_info->unix_token);
+       }
+       if (tevent_req_nomem(state->token, req)) {
+               return tevent_req_post(req, ev);
        }
 
        SMBPROFILE_BYTES_ASYNC_SET_IDLE(state->profile_bytes);
 
-       subreq = pthreadpool_tevent_job_send(state,
-                                            ev,
-                                            tp,
-                                            vfswrap_getxattrat_do,
-                                            state);
+       subreq = pthreadpool_tevent_job_send(
+                       state,
+                       ev,
+                       dir_fsp->conn->sconn->pool,
+                       vfswrap_getxattrat_do_async,
+                       state);
        if (tevent_req_nomem(subreq, req)) {
                return tevent_req_post(req, ev);
        }
@@ -3000,7 +3072,43 @@ static struct tevent_req *vfswrap_getxattrat_send(
        return req;
 }
 
-static void vfswrap_getxattrat_do(void *private_data)
+static void vfswrap_getxattrat_do_sync(struct tevent_req *req)
+{
+       struct vfswrap_getxattrat_state *state = talloc_get_type_abort(
+               req, struct vfswrap_getxattrat_state);
+       char *path = NULL;
+       char *tofree = NULL;
+       char pathbuf[PATH_MAX+1];
+       size_t pathlen;
+       int err;
+
+       pathlen = full_path_tos(state->dir_fsp->fsp_name->base_name,
+                               state->smb_fname->base_name,
+                               pathbuf,
+                               sizeof(pathbuf),
+                               &path,
+                               &tofree);
+       if (pathlen == -1) {
+               tevent_req_error(req, ENOMEM);
+               return;
+       }
+
+       state->xattr_size = getxattr(path,
+                                    state->xattr_name,
+                                    state->xattr_value,
+                                    talloc_array_length(state->xattr_value));
+       err = errno;
+       TALLOC_FREE(tofree);
+       if (state->xattr_size == -1) {
+               tevent_req_error(req, err);
+               return;
+       }
+
+       tevent_req_done(req);
+       return;
+}
+
+static void vfswrap_getxattrat_do_async(void *private_data)
 {
        struct vfswrap_getxattrat_state *state = talloc_get_type_abort(
                private_data, struct vfswrap_getxattrat_state);
@@ -3014,14 +3122,22 @@ static void vfswrap_getxattrat_do(void *private_data)
        /*
         * Here we simulate a getxattrat()
         * call using fchdir();getxattr()
-        *
-        * We don't need to revert the directory
-        * change as pthreadpool_tevent wrapper
-        * handlers that.
         */
-       SMB_ASSERT(pthreadpool_tevent_current_job_per_thread_cwd());
 
-       ret = fchdir(state->dirfd);
+       per_thread_cwd_activate();
+
+       /* Become the correct credential on this thread. */
+       ret = set_thread_credentials(state->token->uid,
+                                    state->token->gid,
+                                    (size_t)state->token->ngroups,
+                                    state->token->groups);
+       if (ret != 0) {
+               state->xattr_size = -1;
+               state->vfs_aio_state.error = errno;
+               goto end_profile;
+       }
+
+       ret = fchdir(state->dir_fsp->fh->fd);
        if (ret == -1) {
                state->xattr_size = -1;
                state->vfs_aio_state.error = errno;
@@ -3031,7 +3147,7 @@ static void vfswrap_getxattrat_do(void *private_data)
        state->xattr_size = getxattr(state->name,
                                     state->xattr_name,
                                     state->xattr_value,
-                                    state->xattr_bufsize);
+                                    talloc_array_length(state->xattr_value));
        if (state->xattr_size == -1) {
                state->vfs_aio_state.error = errno;
        }
@@ -3049,12 +3165,30 @@ static void vfswrap_getxattrat_done(struct tevent_req *subreq)
        struct vfswrap_getxattrat_state *state = tevent_req_data(
                req, struct vfswrap_getxattrat_state);
        int ret;
+       bool ok;
+
+       /*
+        * Make sure we run as the user again
+        */
+       ok = change_to_user_by_fsp(state->dir_fsp);
+       SMB_ASSERT(ok);
 
        ret = pthreadpool_tevent_job_recv(subreq);
        TALLOC_FREE(subreq);
        SMBPROFILE_BYTES_ASYNC_END(state->profile_bytes);
        talloc_set_destructor(state, NULL);
-       if (tevent_req_error(req, ret)) {
+       if (ret != 0) {
+               if (ret != EAGAIN) {
+                       tevent_req_error(req, ret);
+                       return;
+               }
+               /*
+                * If we get EAGAIN from pthreadpool_tevent_job_recv() this
+                * means the lower level pthreadpool failed to create a new
+                * thread. Fallback to sync processing in that case to allow
+                * some progress for the client.
+                */
+               vfswrap_getxattrat_do_sync(req);
                return;
        }