I was trying to use pam_exec.so on Ubuntu to call a script, prompt for user input and allow the user to log in through SSH if the script exits with an exit code of 0. I was unable to get this to work. So I wrote a simple script as follows to test pam_exec.so and see if the problem was with my original script. However I am getting the same issues even with this simple script.
#!/bin/bash echo -n "Please enter your name:" read name echo "Hello $name"
I called it by including the following line after
@include common-auth in /etc/pam.d/sshd
auth required pam_exec.so stdout /usr/local/bin/test.sh
Then I changed the above script as follows to exit with an exit code of 1 if no input is given.
#!/bin/bash echo -n "Please enter your name:" read name if [ -z "$name" ] then exit 1 else echo "Hello $name" exit 0 fi
Then I am unable to SSH into the system (screenshot below) even when I enter the correct password and I do not see any output of the script.
So what I want to know is how can I correct this problem and use pam_exec.so to run a script, get user input, and allow ssh authorization only if that script exits with an exit code of 0?