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)

このスクリプトを実行すると、2つの問題が発生します。

  1. ユーザーを追加すると、すべてが正しく機能しているようです。アカウントは正しいディレクトリに作成されます。デフォルトフォルダが作成され、インストールディレクトリはユーザーのホームディレクトリにROをインストールします。ユーザーは正しいグループに追加されます。

質問"chown root:root /home/chroot/home/$uid":グループに「users」または「sftpusers」を選択すると、コマンドが実行されていないかのようにホームフォルダに正しい権限がありません。

  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

ベストアンサー1

おすすめ記事