-
Notifications
You must be signed in to change notification settings - Fork 18
/
turnkey-sudoadmin
executable file
·261 lines (231 loc) · 6.85 KB
/
turnkey-sudoadmin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/bin/bash -e
#
# Copyright (c) 2015 Alon Swartz <[email protected]>
#
# This file is part of InitHooks.
#
# InitHooks is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
SUDOADMIN_STATE=/etc/sudoadmin/state
fatal() { echo "FATAL [$(basename $0)]: $@" 1>&2; exit 1; }
warn() { echo "WARN [$(basename $0)]: $@"; }
info() { echo "INFO [$(basename $0)]: $@"; }
usage() {
cat<<EOF
Syntax: $(basename $0) on|off|status [--disable-setpass]
Configure system to use admin user with sudo, or root
On:
- installs sudo package
- creates admin user and sets random password
- configures passwordless sudo for admin user
- configures inithooks and init-fence for admin user
- updates confconsole services for admin user
- merges root .ssh/authorized_keys with admin
- locks the root user
- disables root ssh access
- sets root ssh login banner
- restarts ssh daemon, if it is running
- sets admin password interactively (unless --disable-setpass)
Off:
- configures inithooks and init-fence for root
- updates confconsole services for root
- merges admin .ssh/authorized_keys with root
- enables root ssh access
- locks admin user
- unsets root ssh login banner
- restarts ssh daemon, if it is running
- sets root password interactively (unless --disable-setpass)
Status:
- checks turnkey-sudoadmin status, returns one of the following:
- unconfigured
- on
- off
EOF
exit 1
}
install_sudo() {
info $FUNCNAME $@
which sudo >/dev/null && return
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get -y install sudo
}
create_user() {
info $FUNCNAME $@
username=$1
grep -q ^${username}: /etc/passwd && return
useradd --create-home --skel /etc/skel --shell /bin/bash ${username}
echo ${username}:$(mcookie) | chpasswd
}
passwordless_sudo() {
info $FUNCNAME $@
username=$1
cfg="/etc/sudoers.d/99_${username}"
str="${username} ALL=(ALL) NOPASSWD:ALL"
touch $cfg
grep -q "^${str}$" $cfg && return
echo "$str" >> $cfg
chmod 0440 $cfg
}
inithooks_sudoadmin() {
info $FUNCNAME $@
val=$1
key="SUDOADMIN"
cfg="/etc/default/inithooks"
[ -e $cfg ] || return 0
grep -q ^${key}= $cfg || (echo "$key=$val" >> $cfg; return)
sed -i "s/^${key}=.*/$key=$val/" $cfg
}
setup_initfence() {
info $FUNCNAME $@
username=$1
root_profiled=/root/.profile.d/turnkey-init-fence
user_profiled=/home/${username}/.profile.d/turnkey-init-fence
if [ -e $root_profiled ]; then
if [ $username != "root" ]; then
mkdir -p $(dirname $user_profiled)
cp $root_profiled $user_profiled
sed -i "s|^|sudo |" $user_profiled
sed -i "s|/root|/home\/${username}|g" $user_profiled
chown -R $username:$username $(dirname $user_profiled)
fi
fi
return 0
}
update_confconsole_services() {
info $FUNCNAME $@
new_uname=$1
old_uname=$2
cfg="/etc/confconsole/services.txt"
[ -e $cfg ] || return 0
sed -i "s|${old_uname}@|${new_uname}@|g" $cfg
}
ssh_authorizedkeys_inithook() {
info $FUNCNAME $@
username=$1
sshkeys_inithook=/usr/lib/inithooks/firstboot.d/40ec2-sshkeys
[ -e $sshkeys_inithook ] || return 0
sed -i "s|^USERNAME.*|USERNAME = \'${username}\'|" $sshkeys_inithook
}
ssh_authorizedkeys_merge() {
info $FUNCNAME $@
user1=$1
user2=$2
grep -q ^${user1}: /etc/passwd || return 0
grep -q ^${user2}: /etc/passwd || return 0
auth1="$(eval printf ~$user1)/.ssh/authorized_keys"
mkdir -p $(dirname $auth1)
chmod 0700 $(dirname $auth1)
touch $auth1
auth2="$(eval printf ~$user2)/.ssh/authorized_keys"
mkdir -p $(dirname $auth2)
chmod 0700 $(dirname $auth2)
touch $auth2
cat $auth1 $auth2 | sort | uniq > $auth1.new
echo $auth1 $auth2 | xargs -n 1 cp $auth1.new
rm $auth1.new
chown -R $user1:$user1 $(dirname $auth1)
chown -R $user2:$user2 $(dirname $auth2)
}
permitrootlogin_ssh() {
info $FUNCNAME $@
val=$1
key="PermitRootLogin"
cfg="/etc/ssh/sshd_config"
grep -q ^${key} $cfg || (echo "$key $val" >> $cfg; return)
sed -i "s/^${key} .*/$key $val/" $cfg
}
set_sshrootbanner() {
info $FUNCNAME $@
content=$1
banner_path="/root/.ssh/banner"
mkdir -p $(dirname $banner_path)
echo $content > $banner_path
}
user_state() {
info $FUNCNAME $@
username=$1
action=$2
grep -q ^${username}: /etc/passwd || return 0
passwd --${action} $username
if [ $username != "root" ]; then
[ $action == "lock" ] && usermod --expiredate "1" $username
[ $action == "unlock" ] && usermod --expiredate "" $username
fi
return 0
}
restart_sshd() {
info $FUNCNAME $@
if [ -e /var/run/sshd.pid ]; then
/etc/init.d/ssh restart
fi
}
setpass() {
info $FUNCNAME $@
username=$1
script=/usr/lib/inithooks/bin/setpass.py
if [ -x $script ]; then
$script $username
else
echo "Set password for $username"
passwd $username
fi
}
set_status() {
if [ ! -d $(dirname $SUDOADMIN_STATE) ]; then
mkdir $(dirname $SUDOADMIN_STATE)
fi
cat > $SUDOADMIN_STATE <<EOF
# This is an automatically generated file, do not edit manually
state=$1
EOF
}
check_status() {
if [ ! -f $SUDOADMIN_STATE ]; then
echo 'unconfigured'
else
sed -rn 's/state=(.*)/\1/p' "$SUDOADMIN_STATE"
fi
}
case $1 in
on)
[ "$(id -u)" != "0" ] && fatal "must be run with root permissions"
install_sudo;
create_user "admin";
passwordless_sudo "admin";
inithooks_sudoadmin "true";
setup_initfence "admin";
update_confconsole_services "admin" "root";
ssh_authorizedkeys_inithook "admin";
ssh_authorizedkeys_merge "admin" "root";
user_state "admin" "unlock";
user_state "root" "lock";
permitrootlogin_ssh "no";
set_sshrootbanner 'Please login as user "admin" rather than user "root".';
restart_sshd;
set_status "on"
[ "$2" == "--disable-setpass" ] || setpass "admin";
;;
off)
[ "$(id -u)" != "0" ] && fatal "must be run with root permissions"
inithooks_sudoadmin "false";
setup_initfence "root";
update_confconsole_services "root" "admin";
ssh_authorizedkeys_inithook "root";
ssh_authorizedkeys_merge "root" "admin";
permitrootlogin_ssh "yes";
user_state "root" "unlock";
user_state "admin" "lock";
set_sshrootbanner "";
restart_sshd;
set_status "off"
[ "$2" == "--disable-setpass" ] || setpass "root";
;;
status)
check_status;
;;
*)
usage;;
esac