Also please add this line to your howto in order to make it perfectOmniVector wrote:I found little documentation on this subject, and I'm sure if would be of interest to many people trying to create a secure ftp solution, and this is what I came up with.
Firstly you'll need to emerge the restricted rssh shellTo configure it, you'll need add /usr/bin/rssh to the list of accepted shells:Code: Select all
emerge rsshand you'll want to modify the rssh config and make some minor changes to enable chrooting, scp, and sftp.Code: Select all
echo /usr/bin/rssh >> /etc/shells
/etc/rssh.conf:If you wish to disable scp, or sftp independently, just remove the line or comment it out with a #.Code: Select all
logfacility = LOG_USER allowscp allowsftp umask = 022 chrootpath="/home"
Next, we need to build a chroot environment for rssh to work.
This involves copying a few files to our chrooted folder (/home).though we're not quite done copying files yet. now we need to copy the dependencies of those files. ldd will tell us what files are neededCode: Select all
cd /home mkdir -p usr/bin cp /usr/bin/scp usr/bin cp /usr/bin/rssh usr/bin mkdir -p usr/libexec cp /usr/libexec/rssh_chroot_helper usr/libexec mkdir -p usr/lib/misc cp /usr/lib/misc/sftp-server usr/lib/miscso now we need to make the necessary folders, and copy the libs needed for scpCode: Select all
ldd /usr/bin/scp libutil.so.1 => /lib/libutil.so.1 (0x4001c000) libz.so.1 => /usr/lib/libz.so.1 (0x4001f000) libnsl.so.1 => /lib/libnsl.so.1 (0x4002d000) libcrypto.so.0.9.6 => /usr/lib/libcrypto.so.0.9.6 (0x40042000) libc.so.6 => /lib/libc.so.6 (0x40106000) libdl.so.2 => /lib/libdl.so.2 (0x40235000) /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)now run ldd on the other files we copied into our chroot environmentCode: Select all
cd /home mkdir lib cp /lib/libutil.so.1 lib cp /lib/libnsl.so.1 lib cp /lib/libc.so.6 lib cp /lib/libdl.so.2 lib cp /lib/ld-linux.so.2 lib mkdir -p usr/lib cp /usr/lib/libz.so.1 usr/lib cp /usr/lib/libcrypto.so.0.9.6 usr/libcopy the libraries associated with those files if there are any we didn't already get from scp. note: for me, there were no other dependencies. copying all the dependencies for scp was enough for me. this should be the case for you as well unless your configuration is very different.Code: Select all
ldd /usr/bin/rssh ldd /usr/libexec/rssh_chroot_helper ldd /usr/lib/misc/sftp-server
the only thing left to do now is create a user and change their shell to /usr/bin/rssh. there are a couple of ways to do this. you could run superadduseror simply modify an existing user accountCode: Select all
emerge superadduser superadduser Login name for new user []: testuser User ID ('UID') [ defaults to next available ]: Initial group [ users ]: Additional groups (comma separated) []: Home directory [ /home/testuser ] - Warning: '/home/testuser' already exists ! Do you wish to change the home directory path? (Y/n) n Shell [ /bin/bash ] /usr/bin/rssh Expiry date (YYYY-MM-DD) []:Code: Select all
usermod -s /usr/bin/rssh testuser
finally make sure sshd is runningif not run /etc/init.d/sshd startCode: Select all
/etc/init.d/sshd status * status: started
and try connecting:Viola! sftp with chrooting, and no shell allowed!Code: Select all
sftp testuser@yourip.com Connecting to yourip.com... testuser@yourip.com's password: sftp> ls . .. .bash_profile .bashrc .qmail sftp> pwd Remote working directory: /testuser sftp> exit ssh testuser@yourip.com testuser@yourip.com's password: This account is restricted to scp or sftp. If you believe this is in error, please contact your system administrator. Connection to yourip.com closed.
Code: Select all
# cp /lib/libcrypt.so.1 /home/lib/
For those too lazy to click on the link without knowing what it is, I made a patch for rssh that added cvs support.Cicero wrote:After much research and hard work on this:
http://bugs.gentoo.org/show_bug.cgi?id=33118
Please try it out!
Code: Select all
livius:x:1003:501:Voicu Liviu,507,5881253,6310714,067424004:/liviu:/usr/local/bin/rssh
Code: Select all
[root@ayelet liviu]# cat /usr/local/etc/rssh.conf
# This is the default rssh config file
# set the log facility. "LOG_USER" and "user" are equivalent.
logfacility = LOG_USER # you can use comments at end of line
# Leave these both uncommented to make the default action for rssh to lock
# users out completely...
allowscp
allowsftp
# set the default umask
umask = 022
# If you want to chroot users, use this to set the directory
# if you DO NOT want to chroot users, LEAVE THIS COMMENTED OUT.
# Quotes not required unless path contains a space...
#chrootpath="/usr/local/chroot dir"
##########################################
# EXAMPLES of configuring per-user options
user=livius:077:11:/liviu
Code: Select all
Nov 20 11:35:34 ayelet rssh[23060]: allowing sftp to all users
Nov 20 11:35:34 ayelet rssh[23060]: setting umask to 022
Nov 20 11:35:34 ayelet rssh[23060]: line 21: configuring user livius
Nov 20 11:35:34 ayelet rssh[23060]: setting livius's umask to 077
Nov 20 11:35:34 ayelet rssh[23060]: allowing scp to user livius
Nov 20 11:35:34 ayelet rssh[23060]: allowing sftp to user livius
Nov 20 11:35:34 ayelet rssh[23060]: chrooting livius to /liviu
Nov 20 11:35:34 ayelet rssh[23060]: user livius attempted to execute forbidden commands
Nov 20 11:35:34 ayelet rssh[23060]: command: /usr/libexec/openssh/sftp-server
Nov 20 11:35:34 ayelet sshd(pam_unix)[23059]: session closed for user livius
You only need an entry in passwd for the user. Everything else from your original passwd file can be removed.Steffen wrote: Edit: I forgot to say that I had to copy my /etc/passwd file into the chroot, too. I don't know, but this makes SCP a bit less attractive for me than SFTP...
Code: Select all
sftp testuser@mymachineCode: Select all
Connection has been unexpectedly closed. Server sent command exit status 0.


Code: Select all
info chroot

rojaro wrote:.. scponly ..

First of all, you need scponly-3.11-r2 (unstable but should be stable in one day or so). Previous version do NOT support chrooted SFTP.GurliGebis wrote:emerged scponly, but how do I configure it?

No, this is not possible. The dirs make up the filesystem hierarchy needed for chroot to work and MUST be placed in the chrooted home directory. I don´t see the point why you need this anyway. They are NOT user writeable anyway.GurliGebis wrote:Okay, that works, is there a way to place the folders somewhere so the user only sees the folder I create for him to upload in?

Code: Select all
# ldd /usr/bin/scp
linux-gate.so.1 => (0xffffe000)
libresolv.so.2 => /lib/libresolv.so.2 (0xb7fcf000)
libcrypto.so.0.9.7 => /usr/lib/libcrypto.so.0.9.7 (0xb7ed3000)
libutil.so.1 => /lib/libutil.so.1 (0xb7ed0000)
libz.so.1 => /lib/libz.so.1 (0xb7ebf000)
libnsl.so.1 => /lib/libnsl.so.1 (0xb7eaa000)
libcrypt.so.1 => /lib/libcrypt.so.1 (0xb7e7d000)
libc.so.6 => /lib/libc.so.6 (0xb7d6b000)
libdl.so.2 => /lib/libdl.so.2 (0xb7d68000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7fea000)