-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from artginzburg/develop
Develop
- Loading branch information
Showing
2 changed files
with
160 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,102 @@ | ||
#!/bin/sh | ||
#!/bin/bash | ||
|
||
VERSION=0.2 | ||
VERSION=0.3 | ||
readable_name='[TouchID for sudo]' | ||
backup_ext='.bak' | ||
|
||
sudo_touchid_disable() { | ||
local touch_pam='auth sufficient pam_tid.so' | ||
local sudo_path='/etc/pam.d/sudo' | ||
touch_pam='auth sufficient pam_tid.so' | ||
sudo_path='/etc/pam.d/sudo' | ||
|
||
if grep -e "^$touch_pam$" "$sudo_path" &> /dev/null; then | ||
echo "The following will be your $sudo_path after disabling:\n" | ||
grep -v "^$touch_pam$" "$sudo_path" | ||
echo | ||
read -p "Are you sure? [y] to confirm " -n 1 -r | ||
# Source: https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh | ||
getc() { | ||
local save_state | ||
save_state="$(/bin/stty -g)" | ||
/bin/stty raw -echo | ||
IFS='' read -r -n 1 -d '' "$@" | ||
/bin/stty "${save_state}" | ||
} | ||
wait_for_user() { | ||
local c | ||
echo | ||
echo "Press RETURN to continue or any other key to abort" | ||
getc c | ||
# we test for \r and \n because some stuff does \r instead | ||
if ! [[ "${c}" == $'\r' || "${c}" == $'\n' ]]; then | ||
exit 1 | ||
fi | ||
} | ||
# Source end. | ||
|
||
display_backup_info() { | ||
echo "Created a backup file at $sudo_path$backup_ext" | ||
echo | ||
} | ||
|
||
display_sudo_without_touch_pam() { | ||
grep -v "^$touch_pam$" "$sudo_path" | ||
} | ||
|
||
touch_pam_at_sudo_path_check_exists() { | ||
grep -q -e "^$touch_pam$" "$sudo_path" | ||
} | ||
|
||
touch_pam_at_sudo_path_insert() { | ||
sudo sed -E -i "$backup_ext" "1s/^(#.*)$/\1\n$touch_pam/" "$sudo_path" | ||
} | ||
|
||
touch_pam_at_sudo_path_remove() { | ||
sudo sed -i "$backup_ext" -e "/^$touch_pam$/d" "$sudo_path" | ||
} | ||
|
||
sudo_touchid_disable() { | ||
if touch_pam_at_sudo_path_check_exists; then | ||
echo "The following will be your $sudo_path after disabling:" | ||
echo | ||
if [[ $REPLY =~ ^[Yy]$ ]]; then | ||
sudo sed -i '.bak' -e "/^$touch_pam$/d" "$sudo_path" | ||
display_sudo_without_touch_pam | ||
wait_for_user | ||
if touch_pam_at_sudo_path_remove; then | ||
display_backup_info | ||
echo "$readable_name has been disabled." | ||
else | ||
echo "$readable_name failed to disable" | ||
fi | ||
else | ||
echo "TouchID for sudo seems not to be enabled" | ||
else | ||
echo "$readable_name seems to be already disabled" | ||
fi | ||
} | ||
|
||
sudo_touchid() { | ||
local touch_pam='auth sufficient pam_tid.so' | ||
local sudo_path='/etc/pam.d/sudo' | ||
sudo_touchid_enable() { | ||
if touch_pam_at_sudo_path_check_exists; then | ||
echo "$readable_name seems to be enabled already" | ||
else | ||
if touch_pam_at_sudo_path_insert; then | ||
display_backup_info | ||
echo "$readable_name enabled successfully." | ||
else | ||
echo "$readable_name failed to execute" | ||
fi | ||
fi | ||
} | ||
|
||
sudo_touchid() { | ||
for opt in "${@}"; do | ||
case "$opt" in | ||
-V|--version) | ||
echo "$VERSION" | ||
return 0 | ||
-v | --version) | ||
echo "v$VERSION" | ||
return 0 | ||
;; | ||
-D|--disable) | ||
sudo_touchid_disable | ||
return 0 | ||
-d | --disable) | ||
sudo_touchid_disable | ||
return 0 | ||
;; | ||
*) | ||
echo "$readable_name Unknown option: $opt" | ||
return 0 | ||
;; | ||
esac | ||
done | ||
|
||
grep -e "^$touch_pam$" "$sudo_path" &> /dev/null | ||
if [ $? -ne 0 ]; then | ||
sudo sed -E -i '.bak' "1s/^(#.*)$/\1\n$touch_pam/" "$sudo_path" | ||
fi | ||
sudo_touchid_enable | ||
} | ||
|
||
sudo_touchid "${@}" |