Skip to content

Commit 95e33f2

Browse files
bootstrap: all installation commands run with passwordless sudo
1 parent 3e50ca8 commit 95e33f2

4 files changed

Lines changed: 65 additions & 22 deletions

File tree

install/bootstrap_archlinux.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ else
7373
exit 1
7474
fi
7575
if is_profile_dev_single; then
76-
sudoers_add_user "$(whoami)"
76+
sudoers_add_profile "dotfiles_dev" "$(whoami)"
7777
fi
78+
sudoers_add_profile "dotfiles_bootstrap" "$(whoami)" "bootstrap_permission"
7879

7980
onexit() {
8081
# FIXME remove config file with passwords ...
@@ -84,6 +85,7 @@ onexit() {
8485
unset SUDO_OPTIONS
8586

8687
ramdisk_umount_and_destroy_storage "secrets"
88+
sudoers_remove_profile "dotfiles_bootstrap" "$(whoami)"
8789
}
8890

8991
trap onexit EXIT

install/bootstrap_fedora.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ else
7474
exit 1
7575
fi
7676
if is_profile_dev_single; then
77-
sudoers_add_user "$(whoami)"
77+
sudoers_add_profile "dotfiles_dev" "$(whoami)"
7878
fi
79+
sudoers_add_profile "dotfiles_bootstrap" "$(whoami)" "bootstrap_permission"
7980

8081
onexit() {
8182
# FIXME remove config file with passwords ...
@@ -85,6 +86,7 @@ onexit() {
8586
unset SUDO_OPTIONS
8687

8788
ramdisk_umount_and_destroy_storage "secrets"
89+
sudoers_remove_profile "dotfiles_bootstrap" "$(whoami)"
8890
}
8991

9092
trap onexit EXIT

install/bootstrap_macos.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ else
8484
exit 1
8585
fi
8686
if is_profile_dev_single; then
87-
sudoers_add_user "$(whoami)"
87+
sudoers_add_profile "dotfiles_dev" "$(whoami)"
8888
fi
89+
sudoers_add_profile "dotfiles_bootstrap" "$(whoami)" "bootstrap_permission"
90+
8991

9092
onexit() {
9193
# FIXME remove config file with passwords ...
@@ -95,6 +97,7 @@ onexit() {
9597
unset SUDO_OPTIONS
9698

9799
ramdisk_umount_and_destroy_storage "secrets"
100+
sudoers_remove_profile "dotfiles_bootstrap" "$(whoami)"
98101
}
99102

100103
trap onexit EXIT

install/common/shell/sudoers.sh

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
1-
function sudoers_add_user() {
2-
local current_user="$1"
3-
local stderr_file="/tmp/bootstrap.$$.sudoers.log"
1+
ETC_SUDOERS_D_DIR="/etc/sudoers.d"
42

5-
if sudo visudo -c 1>${stderr_file} 2>&1; then
6-
:
7-
else
8-
if grep "bad permission" "${stderr_file}"; then
9-
rm -fv "$stderr_file"
10-
else
11-
cat "$stderr_file"
12-
exit 1
13-
fi
3+
function sudoers_add_profile() {
4+
local profile_name="$1"
5+
local current_user="$2"
6+
local capability="${3:-default}"
7+
8+
if test "$capability" = "default"; then
9+
capability="$current_user ALL=(ALL) ALL"
10+
elif test "$capability" = "bootstrap_permission"; then
11+
capability="$current_user ALL=(ALL) NOPASSWD: ALL"
12+
fi
13+
14+
sudoers_add_capability "${profile_name}_${current_user}" "$capability"
15+
}
16+
17+
function sudoers_remove_profile() {
18+
local profile_name="$1"
19+
local current_user="$2"
20+
21+
sudoers_remove_capability "${profile_name}_${current_user}"
22+
}
23+
24+
function sudoers_remove_capability() {
25+
local profile_file="$1"
26+
27+
local sudoers_file="$ETC_SUDOERS_D_DIR/$profile_file"
28+
if sudo test -f "$sudoers_file"; then
29+
sudo rm -v "$sudoers_file"
30+
fi
31+
}
32+
33+
function sudoers_add_capability() {
34+
local profile_file="$1"
35+
local capability="$2"
36+
local stderr_file="/tmp/bootstrap.$$.sudoers.add_capability.$profile_file.log"
37+
38+
if ! sudoers_verify "${stderr_file}"; then
39+
exit 1
1440
fi
1541

16-
local sudoersFile="/etc/sudoers.d/bootstrap-machine" # file name must not contain '.' or '~'
42+
# file name must not contain '.' or '~'
43+
local sudoers_file="$ETC_SUDOERS_D_DIR/$profile_file"
1744

18-
sudo cat <<-EOF > "$sudoersFile"
19-
# Added by dotfiles's boostrap.sh script
20-
$current_user ALL=(ALL) ALL
45+
sudo tee "$sudoers_file" > /dev/null <<-EOF
46+
# Added by dotfiles's bootstrap.sh script
47+
$capability
2148
EOF
22-
sudo chmod 0440 "$sudoersFile"
49+
sudo chmod 0440 "$sudoers_file"
50+
51+
if ! sudoers_verify "${stderr_file}"; then
52+
exit 1
53+
fi
54+
}
55+
56+
function sudoers_verify() {
57+
local stderr_file="$1"
2358

2459
if sudo visudo -c 1>${stderr_file} 2>&1; then
25-
:
60+
return 0
2661
else
2762
if grep "bad permission" "${stderr_file}"; then
2863
rm -fv "$stderr_file"
64+
return 1
2965
else
3066
cat "$stderr_file"
31-
exit 1
67+
return 1
3268
fi
3369
fi
3470
}

0 commit comments

Comments
 (0)