Skip to content

Instantly share code, notes, and snippets.

@cgudea
Last active August 29, 2015 14:07
Show Gist options
  • Save cgudea/718a0f98e935140c139a to your computer and use it in GitHub Desktop.
Save cgudea/718a0f98e935140c139a to your computer and use it in GitHub Desktop.
Automates the process of creating a google-authenticator-apache-module user token
#!/bin/bash
GAUTH=ubuntu
unset user
unset password
unset secret
function getPass {
getpass=true
while [ "$getpass" = true ]
do
unset pass1
unset pass2
prompt="Enter Password: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
pass1+="$char"
done
echo
prompt="Confirm Password: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
pass2+="$char"
done
echo
if [ "$pass1" = "$pass2" ]; then
echo "Passwords match!"
password=$pass1
getpass=false
else
clear
echo "Passwords do not match dummy"
fi
done
}
if [ "$(id -u)" != "0" ]; then
echo -e "Must run as root! Try:\nsudo $0"
exit 1
fi
# Get username and santize
clear
echo -n "Enter username: "
read -e user
CLEAN=${user//_/}
# next, replace spaces with underscores
CLEAN=${CLEAN// /_}
# now, clean out anything that's not alphanumeric or an underscore
CLEAN=${CLEAN//[^a-zA-Z0-9_]/}
# finally, lowercase with TR
CLEAN=`echo -n $CLEAN | tr A-Z a-z`
user=$CLEAN
getPass
echo -e "\n\nCreating new user $user..."
# Create a new gauth token and copy to two-factor dir
google-authenticator < response 1>/dev/null
cd /etc/apache2/two-factor
cp /home/$GAUTH/.google_authenticator $user
sudo chmod 777 $user
echo "\" PASSWORD=$password" >> $user
chown -R :www-data /etc/apache2/two-factor/$user
chmod 440 $user
echo -e "\nYour apache two-factor account is ready to go! Your QR code is located at:"
read -r secret <$user
echo "https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/$user@ACEServer%3Fsecret%3D$secret"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment