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
1111
1212# Argument definition
1313user=$1
14+ ttl=$2
1415
1516# Includes
1617source $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 '
2627is_object_valid ' user' ' USER' " $user "
2728
2829
@@ -41,37 +42,34 @@ AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys"
4142if [ ! -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
4648fi
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
7069fi
7170
7271#
7372chown ${user} : " ${AUTHKEY_FILE} "
74- chown ${user} : " ${PUBKEY_FILE} "
7573chown admin: " ${PRVKEY_FILE} "
7674
7775
0 commit comments