From 1ef1a7825ba787dca3a0b1f50466010bba684e44 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 17 Mar 2024 15:48:33 +0100 Subject: [PATCH] Fix more bugs --- .visudo | 8 ++++---- cbattery | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/.visudo b/.visudo index 742151b..976908c 100644 --- a/.visudo +++ b/.visudo @@ -1,7 +1,7 @@ -Cmnd_Alias BATTERYOFF = /usr/local/bin/smc -k CH0B -w 02, /usr/local/bin/smc -k CH0C -w 02, /usr/local/bin/smc -k CH0B -r, /usr/local/bin/smc -k CH0C -r -Cmnd_Alias BATTERYON = /usr/local/bin/smc -k CH0B -w 00, /usr/local/bin/smc -k CH0C -w 00 -Cmnd_Alias DISCHARGEOFF = /usr/local/bin/smc -k CH0I -w 00, /usr/local/bin/smc -k CH0I -r -Cmnd_Alias DISCHARGEON = /usr/local/bin/smc -k CH0I -w 01 +Cmnd_Alias BATTERYOFF = smc -k CH0B -w 02, smc -k CH0C -w 02, smc -k CH0B -r, smc -k CH0C -r +Cmnd_Alias BATTERYON = smc -k CH0B -w 00, smc -k CH0C -w 00 +Cmnd_Alias DISCHARGEOFF = smc -k CH0I -w 00, smc -k CH0I -r +Cmnd_Alias DISCHARGEON = smc -k CH0I -w 01 ALL ALL = NOPASSWD: BATTERYOFF ALL ALL = NOPASSWD: BATTERYON diff --git a/cbattery b/cbattery index 1300a97..d491fd2 100755 --- a/cbattery +++ b/cbattery @@ -56,8 +56,17 @@ function get_battery_percentage() { } function install_visudo_file() { - sudoers_file=/private/etc/sudoers.d/cbattery - sudo cp ".visudo" "$sudoers_file" + sudoers_file="/private/etc/sudoers.d/cbattery" + if ! smc_loc="$(command -v smc)"; then + echo "'smc' is required but not found." + exit 1 + fi + sed "s:smc:$smc_loc:g" ".visudo" | sudo tee "$sudoers_file" > /dev/null + if ! sudo visudo -c -f "$sudoers_file"; then + echo "Failed to check sudoers file syntax." + sudo rm "$sudoers_file" + exit 1 + fi sudo chmod 440 "$sudoers_file" } @@ -105,7 +114,7 @@ if [[ "$action" == "status" ]]; then fi if [[ "$action" == "visudo" ]]; then - install_visudo + install_visudo_file exit 0 fi