如果 ssh 服务器在 root 上下文中运行,SFTP 如何只能访问特定用户文件?

如果 ssh 服务器在 root 上下文中运行,SFTP 如何只能访问特定用户文件?

这个问题可能听起来很具体,但那是因为我正在制作自己的 ssh 服务器(公司需要一些特定的东西)并且它具有 SFTP,但因为服务器以 root 身份运行,所以它允许使用 SFTP 服务的任何用户访问所有内容。

对于 bash 会话,这不是问题,因为我像这样运行它们

sudo -H -u $USER bash

但我使用的 SFTP 没有用户身份验证。

我想知道 SSH 如何处理这个问题,因为我想象默认的 ssh 服务器也以 root 身份运行,但也许 SSH 使用的 SFTP 服务允许进行身份验证。

我在想,每次用户登录时,我都可以生成另一个服务器,但仅限于像这样的 SFTP

sudo -u $USER bash -c "my_server -sftponly

然后 SFTP 服务器将只能访问该用户的文件,但我认为这是多余的。

图书馆

golang.org/x/crypto/ssh
github.com/pkg/sftp

答案1

我猜想将 root 特权放弃给另一个用户的工具只是使用为此目的提供的系统调用。

$ man setuid
SETUID(2)                 FreeBSD System Calls Manual                SETUID(2)

NAME
     setuid, seteuid, setgid, setegid - set user and group ID

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <unistd.h>

     int
     setuid(uid_t uid);

     int
     seteuid(uid_t euid);

     int
     setgid(gid_t gid);

     int
     setegid(gid_t egid);

DESCRIPTION
     The setuid() system call sets the real and effective user IDs and the
     saved set-user-ID of the current process to the specified value.  The
     setuid() system call is permitted if the specified ID is equal to the
     real user ID or the effective user ID of the process, or if the effective
     user ID is that of the super user.

     The setgid() system call sets the real and effective group IDs and the
     saved set-group-ID of the current process to the specified value.  The
     setgid() system call is permitted if the specified ID is equal to the
     real group ID or the effective group ID of the process, or if the
     effective user ID is that of the super user.

     The seteuid() system call (setegid()) sets the effective user ID (group
     ID) of the current process.  The effective user ID may be set to the
     value of the real user ID or the saved set-user-ID (see intro(2) and
     execve(2)); in this way, the effective user ID of a set-user-ID
     executable may be toggled by switching to the real user ID, then re-
     enabled by reverting to the set-user-ID value.  Similarly, the effective
     group ID may be set to the value of the real group ID or the saved set-
     group-ID.

RETURN VALUES
     Upon successful completion, the value 0 is returned; otherwise the
     value -1 is returned and the global variable errno is set to indicate the
     error.

ERRORS
     The system calls will fail if:

     [EPERM]            The user is not the super user and the ID specified is
                        not the real, effective ID, or saved ID.

相关内容