Skip to content

Commit 8827512

Browse files
committed
fix: use both dirs ssh checks for config files
1 parent 74f9e13 commit 8827512

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

command-ctrl.sh

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/bin/sh
22

3+
add_ssh_entry() {
4+
ENTRY=$1
5+
SSH_FILE=$2
6+
#Add the entry in both .ssh dirs since ssh checks different locations based on env variables and how it is launched, e.g. on boot vs. manually
7+
for DIR in ~root/.ssh $HOME/.ssh
8+
do
9+
if [ ! -f "$DIR/$SSH_FILE" ] || ! grep -Fxq "$ENTRY" "$DIR/$SSH_FILE"; then
10+
mkdir -p $DIR
11+
echo "$ENTRY" >> "$DIR/$SSH_FILE"
12+
fi
13+
done
14+
}
315

416
remote_ctrl() {
517
if [ -z "$SSH_HOST" ]; then
@@ -40,22 +52,14 @@ remote_ctrl() {
4052
if [ -z "$SSH_HOST_KEY" ]; then
4153
echo "\$SSH_HOST_KEY is empty"
4254
else
43-
KNOWN_HOSTS_FILE=~root/.ssh/known_hosts
4455
KNOWN_HOST="$SSH_HOST $SSH_HOST_KEY"
45-
if [ ! -f "$KNOWN_HOSTS_FILE" ] || ! grep -Fxq "$KNOWN_HOST" "$KNOWN_HOSTS_FILE"; then
46-
mkdir -p ~root/.ssh
47-
echo "$KNOWN_HOST" >> "$KNOWN_HOSTS_FILE"
48-
fi
56+
add_ssh_entry "$KNOWN_HOST" known_hosts
4957
fi
5058

5159
if [ -z "$SSH_AUTH_KEY" ]; then
5260
echo "\$SSH_AUTH_KEY is empty"
5361
else
54-
AUTHORIZED_KEYS_FILE=~root/.ssh/authorized_keys
55-
if [ ! -f "$AUTHORIZED_KEYS_FILE" ] || ! grep -Fxq "$SSH_AUTH_KEY" "$AUTHORIZED_KEYS_FILE"; then
56-
mkdir -p ~root/.ssh
57-
echo "$SSH_AUTH_KEY" >> "$AUTHORIZED_KEYS_FILE"
58-
fi
62+
add_ssh_entry "$SSH_AUTH_KEY" authorized_keys
5963
fi
6064

6165
if [ "$SSHD_FORCE_PUBKEY_AUTH" == "true" ]; then

0 commit comments

Comments
 (0)