Skip to content

Instantly share code, notes, and snippets.

@stemid
Created November 9, 2016 10:34

Revisions

  1. stemid created this gist Nov 9, 2016.
    29 changes: 29 additions & 0 deletions bootstrap_sftp.bash
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env bash

    home_base=/var/sftp

    test $(id -u $PAM_USER) -gt 500 || exit 1
    grep ^{{chroot_openssh_group}} /etc/group | grep -q $PAM_USER || exit 1

    _HOME=$(getent passwd $PAM_USER 2>/dev/null | cut -d: -f6 2>/dev/null)

    if [ -z "$_HOME" ]; then
    _HOME="/home/$PAM_USER"
    fi

    test -d $_HOME || exit 1

    if [ ! -d "$home_base/$PAM_USER" ]; then
    echo "Creating home $home_base/$PAM_USER"
    mkdir -p "$home_base/$PAM_USER"
    fi

    if [ ! -d "$home_base/$PAM_USER/ftp_home" ]; then
    echo "Creating ftp_home mount point for $PAM_USER"
    mkdir "$home_base/$PAM_USER/ftp_home"
    fi

    if ! mount | grep "$home_base/$PAM_USER/ftp_home type none (rw,bind)" &>/dev/null; then
    echo "Mounting ftp_home in users ($PAM_USER) sftp-dir "
    mount -B "$_HOME" "$home_base/$PAM_USER/ftp_home"
    fi