-
Notifications
You must be signed in to change notification settings - Fork 0
/
sshd.sh
executable file
·309 lines (257 loc) · 5.44 KB
/
sshd.sh
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env sh
GITHUB_USER="${GITHUB_USER:-pschmitt}"
KEYS_URL="https://github.com/$GITHUB_USER.keys"
user_homedir() {
TARGET_USER="$1"
USER_HOME=$(eval echo "~${TARGET_USER}")
if [ -n "$USER_HOME" ]
then
echo "$USER_HOME"
return 0
fi
USER_HOME=$(awk -v u="$TARGET_USER" -v FS=':' '$1==u {print $6}' /etc/passwd)
if [ -n "$USER_HOME" ]
then
echo "$USER_HOME"
return 0
fi
USER_HOME=$(getent passwd "$TARGET_USER" | cut -d: -f6)
if [ -n "$USER_HOME" ]
then
echo "$USER_HOME"
return 0
fi
if [ "$TARGET_USER" = "root" ] && [ -d "/root" ]
then
echo "/root"
return 0
fi
if [ -d "/home/${TARGET_USER}" ]
then
echo "/home/${TARGET_USER}"
return 0
fi
return 1
}
fetch() {
if command -v curl >/dev/null 2>&1
then
command curl -fsSL "$@"
return "$?"
fi
if command -v wget >/dev/null 2>&1
then
command wget -q -O- "$@"
return "$?"
fi
echo "Neither curl nor wget is found. Cannot download." >&2
return 1
}
fetch_keys() {
fetch "$KEYS_URL"
}
# Function to elevate a command (directly if root, via sudo or doas otherwise)
sudo() {
COMMAND="$*"
# Running as root
if [ "$(id -u)" -eq 0 ]
then
eval "$COMMAND"
return $?
fi
if command -v sudo >/dev/null 2>&1
then
if ! command sudo -n true
then
echo "sudo requires a password or permission is denied." >&2
return 1
fi
# shellcheck disable=SC2086
command sudo -n $COMMAND
return "$?"
fi
if command -v doas >/dev/null 2>&1
then
if ! doas true
then
echo "doas requires a password or permission is denied." >&2
return 1
fi
# shellcheck disable=SC2086
doas $COMMAND
return "$?"
fi
echo "Neither sudo nor doas found. Cannot elevate command." >&2
return 1
}
update_authorized_keys() {
TARGET_FILE="$1"
KEYS="$2"
while IFS= read -r key
do
# Skip empty lines
if [ -z "$key" ]
then
continue
fi
if ! sudo cat "$TARGET_FILE" 2>/dev/null | grep -qxF "$key"
then
echo "$key" | sudo tee -a "$TARGET_FILE" >/dev/null
fi
done <<EOF
$KEYS
EOF
sudo chmod 600 "$TARGET_FILE"
}
install_sshd() {
if command -v sshd >/dev/null 2>&1
then
echo "sshd command found. OpenSSH server is likely installed."
return 0
fi
if grep -iq alpine /etc/os-release
then
if ! sudo apk add --no-cache openssh-server
then
echo "Failed to install OpenSSH (apk)." >&2
return 1
fi
return 0
fi
if grep -iqE 'ubuntu|debian' /etc/os-release
then
if ! sudo apt-get update
then
echo "Failed to update package list." >&2
return 1
fi
if ! sudo apt-get install -y --no-install-recommends openssh-server
then
echo "Failed to install OpenSSH (apt)." >&2
return 1
fi
return 0
fi
if grep -iqE 'centos|redhat|fedora|oracle' /etc/os-release
then
if command -v dnf >/dev/null 2>&1
then
if ! sudo dnf install -y openssh-server
then
echo "Failed to install OpenSSH (dnf)." >&2
return 1
fi
return 0
fi
if ! sudo yum install -y openssh-server
then
echo "Failed to install OpenSSH (yum/dnf)." >&2
return 1
fi
return 0
fi
echo "Unsupported OS. Please install OpenSSH server manually." >&2
return 1
}
sshd_running() {
SSHD_PATH="$(command -v sshd)"
if [ -z "$SSHD_PATH" ]
then
echo "sshd command not found." >&2
echo "Please install OpenSSH server" >&2
return 1
fi
if command -v pgrep >/dev/null 2>&1
then
pgrep -af "$SSHD_PATH" >/dev/null
return "$?"
fi
# shellcheck disable=SC2009
ps -e | grep -v "$$" | grep -q "$SSHD_PATH"
}
start_sshd() {
install_sshd
# Create host keys if they do not exist
sudo ssh-keygen -A
if sshd_running
then
echo "sshd is already running."
return 0
fi
if command -v systemctl >/dev/null 2>&1
then
if sudo "systemctl start sshd"
then
echo "sshd started via systemctl."
return 0
else
echo "Failed to start sshd via systemctl." >&2
fi
fi
if command -v service >/dev/null 2>&1
then
if sudo "service ssh start"
then
echo "sshd started via service."
return 0
else
echo "Failed to start sshd via service." >&2
fi
fi
# Attempt to start sshd directly
SSHD_PATH=$(command -v sshd)
if [ -z "$SSHD_PATH" ]
then
echo "Unable to start sshd. Please ensure sshd is installed." >&2
return 1
fi
if sudo "$SSHD_PATH"
then
echo "sshd started directly."
return 0
fi
echo "Failed to start sshd directly." >&2
return 1
}
setup_keys() {
TARGET_USER="${1:-$(whoami)}"
if [ -z "$TARGET_USER" ]
then
echo "No user specified." >&2
return 1
fi
USER_HOME=$(user_homedir "$TARGET_USER")
if [ -z "$USER_HOME" ]
then
echo "Failed to determine home directory for $TARGET_USER." >&2
return 1
fi
SSH_DIR="${USER_HOME}/.ssh"
sudo mkdir -p "$SSH_DIR"
sudo chown -R "$TARGET_USER" "$SSH_DIR"
sudo chmod 700 "$SSH_DIR"
update_authorized_keys "${SSH_DIR}/authorized_keys" "$KEYS"
sudo chown "$TARGET_USER" "${SSH_DIR}/authorized_keys"
}
while [ "$#" -gt 0 ]
do
case "$1" in
-u|--user)
GITHUB_USER="$2"
shift 2
;;
*)
echo "Unknown option: $1" >&2
exit 2
;;
esac
done
trap start_sshd EXIT
KEYS=$(fetch_keys)
if [ -z "$KEYS" ]
then
echo "Failed to fetch SSH keys for $GITHUB_USER." >&2
exit 1
fi
setup_keys "$USER"
setup_keys root