|
| 1 | +#!/bin/bash |
| 2 | +# info: add user sftp key |
| 3 | +# options: USER |
| 4 | +# |
| 5 | +# The script creates and updates ssh key for filemanager usage |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument definition |
| 13 | +user=$1 |
| 14 | + |
| 15 | +# Includes |
| 16 | +source $HESTIA/func/main.sh |
| 17 | +source $HESTIA/conf/hestia.conf |
| 18 | + |
| 19 | + |
| 20 | +#----------------------------------------------------------# |
| 21 | +# Verifications # |
| 22 | +#----------------------------------------------------------# |
| 23 | + |
| 24 | +check_args '1' "$#" 'USER' |
| 25 | +is_format_valid 'user' |
| 26 | +is_object_valid 'user' 'USER' "$user" |
| 27 | + |
| 28 | + |
| 29 | +#----------------------------------------------------------# |
| 30 | +# Action # |
| 31 | +#----------------------------------------------------------# |
| 32 | + |
| 33 | +PRVKEY_FILE="$HOMEDIR/$user/.ssh/hst-filemanager-key" |
| 34 | +PUBKEY_FILE="$HOMEDIR/$user/.ssh/hst-filemanager-key.pub" |
| 35 | +AUTHKEY_FILE="$HOMEDIR/$user/.ssh/authorized_keys" |
| 36 | + |
| 37 | +[ -L "$PRVKEY_FILE" ] && check_result $E_FORBIDEN "Private key file cannot be a symlink" |
| 38 | +[ -L "$PUBKEY_FILE" ] && check_result $E_FORBIDEN "Public key file cannot be a symlink" |
| 39 | +[ -L "$AUTHKEY_FILE" ] && check_result $E_FORBIDEN "Authorized keys file cannot be a symlink" |
| 40 | + |
| 41 | +if [ ! -f "${PRVKEY_FILE}" ]; then |
| 42 | + |
| 43 | + ssh-keygen -q -b 1024 -t rsa -f "${PRVKEY_FILE}" -N "" |
| 44 | + new_privkey=true |
| 45 | + |
| 46 | +fi |
| 47 | + |
| 48 | +if [ ! -f "${PUBKEY_FILE}" ] || [ "$new_privkey" = true ]; then |
| 49 | + |
| 50 | + ssh-keygen -y -f "${PRVKEY_FILE}" > "${PUBKEY_FILE}" |
| 51 | + new_pubkey=true |
| 52 | + |
| 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 -F "[${user}]filemanager.ssh.key" "${AUTHKEY_FILE}"; then |
| 62 | + echo "remove old pub key from authkeys file" |
| 63 | + sed -i "/ \[${user}\]filemanager\.ssh\.key\$/d" "${AUTHKEY_FILE}" |
| 64 | + fi |
| 65 | + |
| 66 | + echo "from=\"127.0.0.1\",command=\"internal-sftp\",restrict ${pubkey_str} TS:${now} ${pubkey_desc}" >> "${AUTHKEY_FILE}" |
| 67 | + |
| 68 | +fi |
| 69 | + |
| 70 | +# |
| 71 | +chown ${user}: "${AUTHKEY_FILE}" |
| 72 | +chown ${user}: "${PUBKEY_FILE}" |
| 73 | +chown admin: "${PRVKEY_FILE}" |
| 74 | + |
| 75 | + |
| 76 | +#----------------------------------------------------------# |
| 77 | +# Hestia # |
| 78 | +#----------------------------------------------------------# |
| 79 | + |
| 80 | +# Logging |
| 81 | +log_event "$OK" "$ARGUMENTS" |
| 82 | + |
| 83 | +exit |
0 commit comments