Revised fixes on ssh

main
Anthony 2025-07-30 00:47:55 +08:00
parent 7154d32ec8
commit 7dc59911ed
3 changed files with 39 additions and 2 deletions

View File

@ -119,18 +119,25 @@ if [ ! -d "/home/sftpusers" ]; then
fi fi
log_success "Directory setup completed" log_success "Directory setup completed"
# Determine shell path
if [ "$SSH_ENABLED" = "true" ]; then
USER_SHELL="/shell/bin/bash"
else
USER_SHELL="/sbin/nologin"
fi
# Create the user account # Create the user account
log "Phase 6: Creating user account" log "Phase 6: Creating user account"
if [ "$SSH_ENABLED" = "true" ]; then if [ "$SSH_ENABLED" = "true" ]; then
log "Creating user with SSH access" log "Creating user with SSH access"
if ! log_cmd "useradd -d $USER_HOME -m -s /bin/bash $USERNAME" "Creating user with bash shell"; then if ! log_cmd "useradd -d $USER_HOME -m -s $USER_SHELL $USERNAME" "Creating user with SSH shell inside jail"; then
log_error "Failed to create user account with SSH access" log_error "Failed to create user account with SSH access"
echo "ERROR: Failed to create user account with SSH access" >&2 echo "ERROR: Failed to create user account with SSH access" >&2
exit 1 exit 1
fi fi
else else
log "Creating user with SFTP-only access" log "Creating user with SFTP-only access"
if ! log_cmd "useradd -d $USER_HOME -m -s /sbin/nologin $USERNAME" "Creating user with nologin shell"; then if ! log_cmd "useradd -d $USER_HOME -m -s $USER_SHELL $USERNAME" "Creating user with nologin shell"; then
log_error "Failed to create user account with SFTP-only access" log_error "Failed to create user account with SFTP-only access"
echo "ERROR: Failed to create user account with SFTP-only access" >&2 echo "ERROR: Failed to create user account with SFTP-only access" >&2
exit 1 exit 1

View File

@ -335,6 +335,18 @@ actions:
sed -i "\|/home/sftpusers/${settings.manage_username}/data/ROOT|d" /etc/fstab sed -i "\|/home/sftpusers/${settings.manage_username}/data/ROOT|d" /etc/fstab
fi fi
# Unmount shell template if mounted
if mount | grep -q "/home/sftpusers/${settings.manage_username}/shell"; then
log "Unmounting shell bind mount for user: ${settings.manage_username}"
umount /home/sftpusers/${settings.manage_username}/shell
fi
# Remove shell mount from fstab
if grep -q "/home/sftpusers/${settings.manage_username}/shell" /etc/fstab; then
log "Removing shell bind mount from fstab for user: ${settings.manage_username}"
sed -i "\|/home/sftpusers/${settings.manage_username}/shell|d" /etc/fstab
fi
# Delete user account # Delete user account
if userdel ${settings.manage_username}; then if userdel ${settings.manage_username}; then
log_success "User account deleted: ${settings.manage_username}" log_success "User account deleted: ${settings.manage_username}"

View File

@ -101,6 +101,24 @@ EOF
log_cmd "chown root:root /home/sftpusers" "Setting /home/sftpusers ownership to root:root" log_cmd "chown root:root /home/sftpusers" "Setting /home/sftpusers ownership to root:root"
log_cmd "chmod 755 /home/sftpusers" "Setting /home/sftpusers permissions to 755" log_cmd "chmod 755 /home/sftpusers" "Setting /home/sftpusers permissions to 755"
# --------------------------------------------------------------------------
# Step 5: Build read-only shell template for jailed SSH users
# --------------------------------------------------------------------------
local shell_tmpl="/home/sftp-shell"
if [ ! -x "$shell_tmpl/bin/bash" ]; then
log "Creating minimal shell environment at $shell_tmpl"
log_cmd "mkdir -p $shell_tmpl/bin $shell_tmpl/lib64" "Creating template directories"
log_cmd "cp /bin/bash $shell_tmpl/bin/" "Copying bash binary"
# Copy required shared libraries for bash
for lib in $(ldd /bin/bash | awk '{print $3}' | grep -E '^/'); do
log_cmd "cp --dereference $lib $shell_tmpl/lib64/" "Copying $(basename $lib)"
done
log_cmd "chmod -R 755 $shell_tmpl" "Setting permissions on shell template"
log_success "Shell template created for jailed SSH users"
else
log_debug "Shell template already exists, skipping rebuild."
fi
log_success "System preparation complete." log_success "System preparation complete."
return 0 return 0
} }