Skip to content

Commit 285f9b0

Browse files
committed
Auto create filemanager sftp key when missing, no need to add it manually before using the filemanager
Add auto-expire functionality to v-add-user-sftp-key and remove unnecesary pubkey
1 parent 45c0532 commit 285f9b0

File tree

4 files changed

+21
-27
lines changed

4 files changed

+21
-27
lines changed

bin/v-add-user

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,6 @@ fi
236236
# Adding jailed sftp env
237237
$BIN/v-add-user-sftp-jail $user
238238

239-
# Adding sftp ssh key
240-
$BIN/v-add-user-sftp-key $user
241-
242239
# Logging
243240
log_history "added system user $user" '' 'admin'
244241
log_event "$OK" "$ARGUMENTS"

bin/v-add-user-sftp-key

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
# info: add user sftp key
3-
# options: USER
3+
# options: USER [TTL]
44
#
55
# The script creates and updates ssh key for filemanager usage
66

@@ -11,6 +11,7 @@
1111

1212
# Argument definition
1313
user=$1
14+
ttl=$2
1415

1516
# Includes
1617
source $HESTIA/func/main.sh
@@ -21,8 +22,8 @@ source $HESTIA/conf/hestia.conf
2122
# Verifications #
2223
#----------------------------------------------------------#
2324

24-
check_args '1' "$#" 'USER'
25-
is_format_valid 'user'
25+
check_args '1' "$#" 'USER [TTL]'
26+
is_format_valid 'user' 'ttl'
2627
is_object_valid 'user' 'USER' "$user"
2728

2829

@@ -41,37 +42,34 @@ AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
4142
if [ ! -f "${PRVKEY_FILE}" ]; then
4243

4344
ssh-keygen -q -b 1024 -t rsa -f "${PRVKEY_FILE}" -N ""
45+
rm "${PUBKEY_FILE}"
4446
new_privkey=true
4547

4648
fi
4749

48-
if [ ! -f "${PUBKEY_FILE}" ] || [ "$new_privkey" = true ]; then
50+
if [ ! -f "${AUTHKEY_FILE}" ] || [ "$new_privkey" = true ]; then
4951

50-
ssh-keygen -y -f "${PRVKEY_FILE}" > "${PUBKEY_FILE}"
51-
new_pubkey=true
52+
pubkey_str="$(ssh-keygen -y -f ${PRVKEY_FILE})"
53+
pubkey_desc="filemanager.ssh.key"
5254

53-
fi
54-
55-
if [ ! -f "${AUTHKEY_FILE}" ] || [ "$new_pubkey" = true ]; then
56-
57-
now=$(date +%s)
58-
pubkey_str=$(cat "${PUBKEY_FILE}")
59-
pubkey_desc="[${user}]filemanager.ssh.key"
60-
61-
if grep --quiet --no-messages -F "[${user}]filemanager.ssh.key" "${AUTHKEY_FILE}"; then
62-
sed -i "/ \[${user}\]filemanager\.ssh\.key\$/d" "${AUTHKEY_FILE}"
55+
if grep --quiet --no-messages -F "$pubkey_desc" "${AUTHKEY_FILE}"; then
56+
sed -i "/filemanager\.ssh\.key\$/d" "${AUTHKEY_FILE}"
6357
fi
6458

6559
# make sure authorized_keys is ending with EOL
6660
[ -f "${AUTHKEY_FILE}" ] && sed -i '$a\' "${AUTHKEY_FILE}"
6761

68-
echo "from=\"127.0.0.1\",command=\"internal-sftp\",restrict ${pubkey_str} TS:${now} ${pubkey_desc}" >> "${AUTHKEY_FILE}"
62+
expire=0
63+
if [[ "$ttl" -gt 0 ]]; then
64+
expire=$(date +%s -d "+${ttl} min")
65+
echo "rm ${PRVKEY_FILE}" | at "now +${ttl} minute" > /dev/null 2>&1
66+
fi
67+
echo "from=\"127.0.0.1\",command=\"internal-sftp\",restrict ${pubkey_str} TS:${expire} ${pubkey_desc}" >> "${AUTHKEY_FILE}"
6968

7069
fi
7170

7271
#
7372
chown ${user}: "${AUTHKEY_FILE}"
74-
chown ${user}: "${PUBKEY_FILE}"
7573
chown admin: "${PRVKEY_FILE}"
7674

7775

install/deb/filemanager/filegator/configuration.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
if (isset($_SESSION['look']) && $_SESSION['look'] != 'admin' && $v_user === 'admin') {
1717
$v_user = $_SESSION['look'];
1818
}
19+
# Create filemanager sftp key if missing and trash it after 30 min
20+
if (! file_exists('/home/'.basename($v_user).'/.ssh/hst-filemanager-key')) {
21+
exec ("sudo /usr/local/hestia/bin/v-add-user-sftp-key " . escapeshellarg(basename($v_user)) . " 30", $output, $return_var);
22+
}
1923

2024
return new \League\Flysystem\Sftp\SftpAdapter([
2125
'host' => '127.0.0.1',
@@ -35,11 +39,6 @@
3539
],
3640
];
3741

38-
$dist_config['services']['Filegator\Services\View\ViewInterface']['config'] = [
39-
'add_to_head' => '',
40-
'add_to_body' => '',
41-
];
42-
4342
$dist_config['services']['Filegator\Services\View\ViewInterface']['config'] = [
4443
'add_to_head' => '',
4544
'add_to_body' => '

install/upgrade/versions/latest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ if [ ! -e "$HESTIA/web/fm/configuration.php" ]; then
8787

8888
# Add sftp key for every user
8989
for user in $(v-list-sys-users plain); do
90-
v-add-user-sftp-key "$user"
90+
[[ -f "/home/${user}/.ssh/hst-filemanager-key" ]] && rm "/home/${user}/.ssh/hst-filemanager-key"
9191
done
9292
fi
9393

0 commit comments

Comments
 (0)