|
| 1 | +#!/bin/bash |
| 2 | +# info: change path for ftp user. |
| 3 | +# options: USER DOMAIN FTP_USER FTP_PATH |
| 4 | +# |
| 5 | +# The function changes ftp user path. |
| 6 | + |
| 7 | + |
| 8 | +#----------------------------------------------------------# |
| 9 | +# Variable&Function # |
| 10 | +#----------------------------------------------------------# |
| 11 | + |
| 12 | +# Argument defenition |
| 13 | +user=$1 |
| 14 | +domain=$(idn -t --quiet -u "$2" ) |
| 15 | +domain_idn=$(idn -t --quiet -a "$domain") |
| 16 | +ftp_user=$3 |
| 17 | +ftp_path=$4 |
| 18 | + |
| 19 | +# Includes |
| 20 | +source $VESTA/func/main.sh |
| 21 | +source $VESTA/func/domain.sh |
| 22 | +source $VESTA/conf/vesta.conf |
| 23 | + |
| 24 | + |
| 25 | +#----------------------------------------------------------# |
| 26 | +# Verifications # |
| 27 | +#----------------------------------------------------------# |
| 28 | + |
| 29 | +check_args '4' "$#" 'USER DOMAIN FTP_USER FTP_PATH' |
| 30 | +validate_format 'user' 'domain' 'ftp_user' |
| 31 | +is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM' |
| 32 | +is_object_valid 'user' 'USER' "$user" |
| 33 | +is_object_unsuspended 'user' 'USER' "$user" |
| 34 | +is_object_valid 'web' 'DOMAIN' "$domain" |
| 35 | +is_object_unsuspended 'web' 'DOMAIN' "$domain" |
| 36 | +get_domain_values 'web' |
| 37 | +if [ -z "$(echo $FTP_USER | tr ':' '\n' | grep ^$ftp_user$)" ]; then |
| 38 | + echo "Error: account $ftp_user doesn't exist" |
| 39 | + log_event "$E_NOTEXIST" "$EVENT" |
| 40 | + exit $E_NOTEXIST |
| 41 | +fi |
| 42 | +ftp_path_a=$(readlink -f "$HOMEDIR/$user/web/$domain/$ftp_path") |
| 43 | +if [ -z "$(echo $ftp_path_a |grep $HOMEDIR/$user/web/$domain)" ]; then |
| 44 | + echo "Error: absolute path $ftp_path_a is invalid" |
| 45 | + log_event "$E_INVALID" "$EVENT" |
| 46 | + exit $E_INVALID |
| 47 | +fi |
| 48 | + |
| 49 | + |
| 50 | +#----------------------------------------------------------# |
| 51 | +# Action # |
| 52 | +#----------------------------------------------------------# |
| 53 | + |
| 54 | +# MKDIR if path doesn't exist |
| 55 | +if [ ! -e "$ftp_path_a" ]; then |
| 56 | + mkdir -p "$ftp_path_a" |
| 57 | + chown $user:$user "$ftp_path_a" |
| 58 | + chmod 751 "$ftp_path_a" |
| 59 | +fi |
| 60 | + |
| 61 | +# Chaning ftp user path |
| 62 | +pw_str=$(grep -n "^$ftp_user:" /etc/passwd) |
| 63 | +str=$(echo "$pw_str" | cut -f 1 -d :) |
| 64 | +old_path=$(echo "$pw_str" | cut -f 7 -d :) |
| 65 | +sed -i "$str s%$old_path%$ftp_path_a%g" /etc/passwd |
| 66 | + |
| 67 | + |
| 68 | +#----------------------------------------------------------# |
| 69 | +# Vesta # |
| 70 | +#----------------------------------------------------------# |
| 71 | + |
| 72 | +# Transforming absolute path to relative |
| 73 | +ftp_path_r=$(echo $ftp_path_a |sed "s%$HOMEDIR/$user/web/$domain%%") |
| 74 | + |
| 75 | +# Rebuilding FTP variables |
| 76 | +position=$(echo $FTP_USER |tr ':' '\n' |grep -n '' |grep ":$ftp_user$" |\ |
| 77 | + cut -f 1 -d:) |
| 78 | +ftp_path=$(echo $FTP_PATH |tr ':' '\n' |grep -n '' |\ |
| 79 | + sed -e "s%^$position:.*%$position:$ftp_path_r%" |\ |
| 80 | + cut -f 2 -d :| sed -e ':a;N;$!ba;s/\n/:/g') |
| 81 | + |
| 82 | +# Updating config |
| 83 | +update_object_value 'web' 'DOMAIN' "$domain" '$FTP_PATH' "$ftp_path" |
| 84 | + |
| 85 | +# Logging |
| 86 | +log_history "changed path to $ftp_path_a for $ftp_user@$domain" |
| 87 | +log_event "$OK" "$EVENT" |
| 88 | + |
| 89 | +exit |
0 commit comments