Skip to content

Commit

Permalink
SFTP
Browse files Browse the repository at this point in the history
  • Loading branch information
cjmalloy committed Jan 27, 2024
1 parent 3375429 commit 2f7cc32
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion 40-setup-users.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ PasswordAuthentication no
AllowTcpForwarding yes
X11Forwarding no
AllowAgentForwarding no
ForceCommand /bin/false
UseDNS no
PermitOpen none
ForceCommand internal-sftp
Subsystem sftp internal-sftp
ChrootDirectory /opt/chrooted-sftp/%u
"
echo "$sshdConfig" > "$sshd_config"

Expand Down Expand Up @@ -83,6 +86,26 @@ setup_user() {

chown -R $user:$user "$home_dir"

# Chroot User
user_chroot="/opt/chrooted-sftp/${user_origin:-default}"
mkdir -p "$user_chroot"

# Ensure the base structure of the chroot environment exists
for dir in /usr /bin /lib; do
mkdir -p "$user_chroot$dir"
done

# Copy the SFTP server binary
cp /usr/bin/sftp-server "$user_chroot/usr/bin/"

# Use scanelf to list needed shared libraries and copy them
scanelf -e /usr/bin/sftp-server | awk '{print $3}' | \
xargs -I {} cp {} "$user_chroot/lib/"

# Ensure correct permissions
chown root:root "$user_chroot"
chmod 755 "$user_chroot"

# Append to SSHD Config for user-specific PermitOpen
echo "Match User $user" >> "$sshd_config"
echo " PermitOpen localhost:$port" >> "$sshd_config"
Expand Down

0 comments on commit 2f7cc32

Please sign in to comment.