pam_exec.so スクリプトからディレクトリのファイルのリストを取得できません。

pam_exec.so スクリプトからディレクトリのファイルのリストを取得できません。

私はすべてのSSHログインでカスタムスクリプトを実行し、カスタムホワイトリストに追加されていない未知のIPアドレスのログインがある場合に通知します。私はそれを使用していますpam_exec.soユーザーがログインするたびに確認してください。これまではうまくいっています。ログインするたびに希望の通知を受け取りますが、私のIPは現在私が定義したホワイトリストにはありません。

唯一の問題は、サーバーにどのWebサイト構成があるかを示すforループがスクリプトにあることです。これを行うには、すべてのファイルをインポートしようとします。/etc/apache2/sites-enabled。現在説明していないホスト名を持つサーバーがたくさんあるので、通知を受けたときにそのサーバーで何が実行されているのかを知るのが便利です。スクリプトを手動で実行しても問題はありませんが、pamを使用して自動化すると、構成名を含む文字列が空であることがわかります。

ファイル名を知る方法はありますか?

以下は、スクリプト、その権限の設定、および機能するようにpam構成に追加した行です。

/root/sh/login 通知.sh:

-rws--x--x 1 ルート ルート 1296 6月14日 07:45 login-notify.sh*

#!/bin/sh

# Change sender depending on where the notification should go

sender="[email protected]"
target="[email protected]"
client_ip="${PAM_RHOST%% *}"
user=$PAM_USER

# Check if the session is open

if [ "$PAM_TYPE" != "close_session" ]; then
        # Check if client ip is on the whitelist
        if grep -Fxq "$client_ip" /etc/ssh_ip_whitelist; then
        :
        else
        host="`hostname`"

        # Get all active website configurations on the server
        host_websites=""
        for entry in /etc/apache2/sites-enabled/*
        do
                config_name="$(basename $entry)"
                host_websites+="\n${config_name}"
        done

        subject="SSH Login: user $user from $client_ip on $host"
        # Message to send, e.g. the current environment variables.
        message="A new ssh login has been detected:\nhost - $host\nuser - $user\nip - $client_ip\n\nThe following website configurations exist on this server: $host_websites\n\nIf you trust this IP, please ask your server administrator to add it to the file /etc/ssh_ip_whitelist on this server. To quickly add an IP, connect to the server, change to root user and use:\n\necho \"<trusted ip>\" >> /etc/ssh_ip_whitelist"

        # Send the email
        echo "Subject: $subject\n$message"|/usr/sbin/sendmail -r $sender -t $target
        fi
fi

/etc/pam.d/sshd:

session optional pam_exec.so seteuid /root/sh/login-notify.sh

ベストアンサー1

おすすめ記事