Posted by niol on Tue 1 Apr 2008 at 10:49
The upcoming version of OpenSSH (4.8p1 for the GNU/Linux port) features a new configuration option : ChrootDirectory. This has been made possible by a new SFTP subsystem statically linked to sshd.
This makes it easy to replace a basic FTP service without the hassle of configuring encryption and/or bothering with FTP passive and active modes when operating through a NAT router. This is also simpler than packages such as rssh, scponly or other patches because it does not require setting up and maintaining (i.e. security updates) a chroot environment.
To enable it, you obviously need the new version 4.8p1. I personaly use the cvs version and the debian/ directory of the sid package to build a well integrated Debian package 4.8p1~cvs-1.
In /etc/ssh/sshd_config :
You need to configure OpenSSH to use its internal SFTP subsystem.
Subsystem sftp internal-sftp
Then, I configured chroot()ing in a match rule.
Match group sftponly
ChrootDirectory /home/%u
X11Forwarding no
AllowTcpForwarding no
ForceCommand internal-sftp
The directory in which to chroot() must be owned by root. After the call to chroot(), sshd changes directory to the home directory relative to the new root directory. That is why I use / as home directory.
# chown root.root /home/user # usermod -d / user # adduser user sftponly
This seems to work as expected :
$ sftp user@host Connecting to host... user@host's password: sftp> ls build cowbuildinall incoming johnbuilderclean sftp> pwd Remote working directory: / sftp> cd .. sftp> ls build cowbuildinall incoming johnbuilderclean
The only thing I miss is file transfers logging, but I did not investigate this at all. More on this whenever I find some time to do so.
References :
[ Parent ]
[ Parent ]
#define SSHCFG_GLOBAL 0x01 /* allowed in main section of sshd_config */
#define SSHCFG_MATCH 0x02 /* allowed inside a Match section */
#define SSHCFG_ALL (SSHCFG_GLOBAL|SSHCFG_MATCH)
/* Textual representation of the tokens. */
static struct {
const char *name;
ServerOpCodes opcode;
u_int flags;
} keywords[] = {
// [...]
{ "chrootdirectory", sChrootDirectory, SSHCFG_ALL },
// [...]
};
HTH[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
# usermod -d / useruserdel -rf user[ Parent ]
[ Parent ]
chown root /path/to/destination
chown root /path/to
chown root /path
chmod g-w,o-w /path/to/destination
chmod g-w,o-w /path/to
chmod g-w,o-w /path
chown root /path/to/destination
ln -s /path/to/destination /destination
chown root /destination
chmod g-w,o-w /path/to/destination
chmod g-w,o-w /destination
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
chown root:root /home/user1
chmod 750 /home/user1
setfacl -m user:user1:rwx /home/user1
setfacl -m mask::rwx /home/user1[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
[ Parent ]
After the call to chroot(), sshd changes directory to the home directory relative to the new root directory. That is why I use / as home directory.
If you don't change the home directory to / it seems to work anyway (ie leave as default /home/user).
It also allows public key authentication to work (~/.ssh/authorized_keys)
[ Parent ]
But I've learned not to trust anything posted on this day...
[ Parent ]