在 BASH 脚本中运行命令时出现问题

在 BASH 脚本中运行命令时出现问题

我已经很长时间没有编写 BASH 脚本了,所以我有点缺乏实践,所以请原谅写得不好的结构:-)

我编写了一个基本的“提示”脚本,用于添加/删除 SFTP 用户帐户。该脚本在添加/删除帐户时提示用户输入信息。添加时,它会在“CHROOT”监狱中创建帐户并添加包含图像文件的根安装 RO 文件夹。

有 3 种类型的组:

'staff' = 这些用户基本上是“管理员”并且被允许访问服务器。

他们对服务器有物理访问权限并有权对其进行更改,可以创建用户、检查日志、允许运行 SCP/SFTP 等。(主文件夹:/home/chroot/home/$uid)(文件夹权限: $uid:$gid) (外壳: /bin/bash)

'users' = 允许登录但不允许通过其'CHROOT'主页的用户。 (即技术)

他们被允许运行“SFTP”仅有的到/从他们自己的主目录。他们不允许访问其主目录之外的文件。 (主文件夹:/home/chroot/home/$uid)(文件夹权限:root:root)(外壳:/bin/bash)

'sftpusers' = 用户仅有的有 SFTP 使用。

他们根本无权登录服务器。他们只允许 SFTP 并且只能在其主目录中进行。 (主文件夹:/home/chroot/home/$uid)(文件夹权限:root:root)(外壳:/bin/nologin)

当我运行这个脚本时,出现了两个问题:

  1. 添加用户时,一切似乎都工作正常。在正确的目录中创建帐户;创建基本文件夹并安装目录在用户的主目录中安装 RO;用户的“sftp”文件夹具有适当的权限;用户被添加到所选的正确组中。

问题:为组选择“users”或“sftpusers”时,主文件夹没有正确的权限,就像命令"chown root:root /home/chroot/home/$uid"未执行一样。

  1. 删除用户时,一切似乎都工作正常。帐户被删除;用户的“sftp”文件夹被删除。

问题:安装的 RO 文件夹不会被卸载,就好像该"umount -f"命令永远不会执行,并且用户的主目录永远不会被删除。

谁能指出我哪里出错了? (评论请温柔一点,哈哈)

这是代码:

#!/bin/bash
# Notify user of incorrect selection
incorrect_selection() {
    echo "Option doesn't exist. Please try again."
}
menu_add_user() {
    clear;echo ""
    echo -e "\tADDING NEW USERS FOR SFTP"
    echo -e "\t========================="
    echo ""
    read -p " 1) What is the user's UID? " uid
    read -p " 2) What is the user's full name? " fname
    read -p " 3) What is the user's room number/cube? " room
    read -p " 4) What is the user's work phone #? " work_ph
    read -p " 5) What is the user's cell phone #? " cell_ph
    echo ""
    echo -e "\tPlease choose one of the following:"
    echo -e "\t==================================="
    echo -e "\t1) The user is a contractor"
    echo -e "\t2) The user will only be a SFTP user"
    echo -e "\t3) The user is local LAN MANAGEMENT"
    echo ""
    read -p "Enter your selection [1-3]: " member
    case $member in
        1 ) read -p "Enter the contractor's company name: " other
            other="(CONTRACTOR - ${other})"
            read -p "Will the user manage SFTP user access [y/n]? " mgr
            case $mgr in
                [Yy]* ) group='staff';;
                [Nn]* ) group='users';;
                * ) incorrect_selection;;
            esac;;
        2 ) group='sftpusers'
            other="(SFTP ONLY USER)";;
        3 ) read -p "Will the user manage SFTP user access [y/n]? " mgr
            other="(LAN MANAGEMENT)"
            case $mgr in
                [Yy]* ) group='staff';;
                [Nn]* ) group='users';;
                * ) incorrect_selection;;
            esac;;
        * ) group='sftpusers';;
    esac
    echo ""
    echo -e "\tPLEASE VERIFY"
    echo -e "\t============="
    echo -e "\tUser's UID: $uid"
    echo -e "\tUser's Name: $fname"
    echo -e "\tRoom / cubical location: $room"
    echo -e "\tUser's Work Ph: $work_ph"
    echo -e "\tUser's Work Cell Ph: $cell_ph"
    echo -e "\tUser will be placed into group: $group"
    echo -e "\tOther user information: $other"
    read -p "Is the above correct? " correct
    room=`echo "$room" | sed 's/,/|/g'`
    case $correct in
        [Yy]* ) echo -e "\tCREATING USER ACCOUNT..."
            if [[ $group -eq 'staff' ]] ; then
                echo ""
                echo -e "\tASSIGNING USER TO GROUP: [$group]"
                echo -e "\tCREATING USER ACCOUNT [$uid]..."
                sudo su -c "adduser --home /home/chroot/home/$uid --ingroup $group --disabled-password --gecos \"$fname,$room,$work_ph,$cell_ph,$other\" $uid"
                echo -e "\tCREATING USER DEFAULT DIRECTORIES - [sftp,Cisco]..."
                sudo su -c "mkdir /home/chroot/home/$uid/{sftp,Cisco}"
            elif [ $group -eq 'users' ] || [ $group -eq 'sftpusers' ] ; then
                echo ""
                echo -e "\tASSIGNING USER TO GROUP: [$group]"
                echo -e "\tCREATING USER ACCOUNT [$uid]..."
                # Create the user with the proper login shell based on group assignment.
                if [[ $group -eq 'sftpusers' ]]; then
                    sudo su -c "adduser --home /home/chroot/home/$uid --ingroup $group --disabled-password --gecos \"$fname,$room,$work_ph,$cell_ph,$other\" --shell /bin/nologin $uid"
                else
                    sudo su -c "adduser --home /home/chroot/home/$uid --ingroup $group --disabled-password --gecos \"$fname,$room,$work_ph,$cell_ph,$other\" $uid"
                fi
                echo -e "\tCREATING USER DEFAULT DIRECTORIES - [sftp,Cisco]..."
                sudo su -c "mkdir /home/chroot/home/$uid/{sftp,Cisco}"
                # THIS COMMAND IS NEEDED IN ORDER TO MAKE USER A SFTPUSER ONLY #
                sudo su -c "chown root:root /home/chroot/home/$uid"
            else
                echo ""
                echo -e "\tGROUP ($group) DOESN'T EXIST"
            fi
            echo -e "\tSETTING USER FOLDER PERMISSIONS..."
            sudo su -c "chown $uid:$group /home/chroot/home/$uid/sftp"
            sudo su -c "mount --bind /home/chroot/home/sftpuser/Cisco /home/chroot/home/$uid/Cisco"
            sudo su -c "mount -o remount,ro,bind /home/chroot/home/sftpuser/Cisco /home/chroot/home/$uid/Cisco"
            echo ""
            echo -e "\tCREATED USER ACCOUNT: [$uid]"
            sed "/END: $group/i $fname $other: $(printf '\t%.0s' {2})$uid <---- ACTIVE" SFTP_User_List.txt
            echo -e "\t============================";;
        [Nn]* ) echo "Re-enter the information..."
                menu_add_user;;
        * ) incorrect_selection;;
    esac
}
menu_delete_user() {
    clear;echo ""
    echo -e "\tDELETING USERS FROM SFTP ACCESS"
    echo -e "\t==============================="
    echo ""
    read -p " 1) What is the user's UID to be deleted? " uid
    read -p " 2) Do you want to save any files in the user's folder? " sfiles
    echo ""
    echo -e "\tPLEASE VERIFY"
    echo -e "\t============="
    echo -e "\tDelete user [UID]: $uid"
    echo -e "\tElected to save files: $sfiles"
    read -p "Is the above correct? " correct
    case $correct in
        [Yy]* ) case $sfiles in
            [Yy]* ) sudo su -c "umount -f /home/chroot/home/$uid/Cisco $uid"
                    sudo su -c "deluser --force --backup-to /home/chroot/home/sftpuser/$uid --remove-all-files $uid" 2>/dev/null ;;
            [Nn]* ) sudo su -c "deluser --quiet --force --remove-all-files $uid" 2>/dev/null ;;
                esac;;
        [Nn]* ) menu_delete_user;;
    esac
    # MARK USER REMOVED IN LIST FILE
    echo ""
    echo -e "\tDELETED USER ACCOUNT: [$uid]"
    sed -i "/$uid/ s/ACTIVE/REMOVED/" ./SFTP_User_List.txt
    echo -e "\t============================"
}
press_enter() {
    echo ""
    read -p "Press enter to continue..."
    clear
}
until [ "$selection" = '0' ]; do
    clear;
    echo ""
    echo -e "\tCREATE or DELETE SFTP USER MENU"
    echo -e "\t==============================="
    echo -e "\t 1 - Add a new user"
    echo -e "\t 2 - Delete an SFTP user"
    echo -e "\t 0 - Exit"
    echo ""
    read -p "Enter selection: " selection
    case $selection in
        1 ) clear; menu_add_user; press_enter;;
        2 ) clear; menu_delete_user; press_enter;;
        0 ) exit;;
        * ) clear; incorrect_selection; press_enter;;
    esac
done

相关内容