• About
  • Manuals
  • Network
Confessions of a FreeBSD hacker Various tips & tricks about computers in general and FreeBSD in particular
Confessions of a FreeBSD hacker

Chrooting FTP users

February 19, 2013 23:07 / Leave a Comment / Magnus Strahlert

The builtin ftp daemon in FreeBSD is able to lock users in secured directories by using chroot and is really simple to setup.

There is a standardgroup called ftp with the groupid 14. Let’s make another group called ftpchroot as well.

pw groupadd ftpchroot -g 15

Then create a filearea where the user should be restricted to.

mkdir /usr/local/ftpchroot
chgrp ftpchroot /usr/local/ftpchroot

Create the user. Note the dot in the user’s homedirectory. This separates the chroot from the dir where to cd into upon login. By setting the directory permissions to 700, other ftpchroot users cannot peek into eachother directories.

# adduser
Username: ftpuser
Full name: Ftpuser
Uid (Leave empty for default): 
Login group [ftpuser]: ftpchroot
Login group is ftpchroot. Invite ftpuser into other groups? []:
Login class [default]:
Shell (sh csh tcsh bash rbash nologin) [sh]: nologin
Home directory [/home/ftpuser]: /usr/local/ftpchroot/./ftpuser
Home directory permissions (Leave empty for default): 700
Use password-based authentication? [yes]:
Use an empty password? (yes/no) [no]:
Use a random password? (yes/no) [no]:
Enter password:
Enter password again:
Lock out the account after creation? [no]:
Username   : ftpuser
Password   : *****
Full Name  : Ftpuser
Uid        : 1001
Class      :
Groups     : ftpchroot
Home       : /usr/local/ftpchroot/./ftpuser
Home Mode  : 700
Shell      : /usr/sbin/nologin
Locked     : no
OK? (yes/no): yes
adduser: INFO: Successfully added (ftpuser) to the user database.
Add another user? (yes/no): no
Goodbye!

In order for the user to be able to login through ftp, /usr/sbin/nologin must be added to /etc/shells. The file /etc/ftpchroot determines which users should be allowed restricted ftpaccess. It contains a list of users and/or groups where groups are written by prefixing it with an @ character.

@ftpchroot

To properly map uid’s to usernames a couple of steps still remains.

mkdir /usr/local/ftpchroot/etc

Create a dummy passwd file for all ftpchroot users and generate a passwd database from it and copy over the system group file to the chroot etc

while IFS=: read user x uid gid x; do
  if [ "$gid" = "15" ]; then
    echo $user:*:$uid:$gid:::::: >> /tmp/passwd
  fi
done < /etc/passwd

pwd_mkdb -d /usr/local/ftpchroot/etc /tmp/passwd
rm /usr/local/etc/ftpchroot/etc/master.passwd
rm /usr/local/etc/ftpchroot/etc/spwd.db
cp /etc/group /usr/local/etc/ftpchroot/etc
chmod 555 /usr/local/etc/ftpchroot/etc
chmod 444 /usr/local/etc/ftpchroot/etc/*

Having added the following to /etc/rc.conf you’re ready to fire it up by /etc/rc.d/ftpd start.

ftpd_enable="YES"
ftpd_flags="-llD"
Posted in: FreeBSD / Tagged: chroot, freebsd, ftp, ftpchroot

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

Post Navigation

← Previous Post
Next Post →

Recent Posts

  • Takeaways utilising VFIO in a KVM virtual guest
  • Converting VM from ESXi to KVM with iSCSI storage
  • Deploying oVirt in self-hosted mode
  • Ansible playbook for handling perl on FreeBSD
  • iSCSI: Migrating from istgt to ctld

Tags

ansible backup carp chroot cluster cross-server esxi freebsd ftp ftpchroot gpt grub hast install iscsi kvm linux lun lvm mirror network nfs nfsroot ovirt pxe raid ramdrive raspberrypi snapshot ssd ssh terminalserver usb vfio virtio virtualbox vlan vmware volumes winxp zfs zpool zroot

Archives

  • July 2019
  • March 2018
  • February 2018
  • July 2017
  • February 2016
  • January 2015
  • August 2014
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • September 2012
  • July 2012
  • April 2012
  • March 2012
  • November 2011
  • August 2011

Recent Comments

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org
    © Copyright 2013-2016 - Magnus Strahlert
    Infinity Theme by DesignCoral / WordPress