main.sh 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. #!/usr/bin/env bash
  2. # Internal variables
  3. HOMEDIR='/home'
  4. BACKUP='/backup'
  5. BACKUP_GZIP=9
  6. BACKUP_DISK_LIMIT=95
  7. BACKUP_LA_LIMIT=`cat /proc/cpuinfo | grep processor | wc -l`
  8. RRD_STEP=300
  9. BIN=$HESTIA/bin
  10. if [ "$OS_BASE" = 'rhel' ]; then
  11. HESTIA_INSTALL_DIR=$HESTIA/install/rhel
  12. else
  13. HESTIA_INSTALL_DIR=$HESTIA/install/deb
  14. fi
  15. HESTIA_BACKUP="/root/hst_backups/$(date +%d%m%Y%H%M)"
  16. USER_DATA=$HESTIA/data/users/$user
  17. WEBTPL=$HESTIA/data/templates/web
  18. MAILTPL=$HESTIA/data/templates/mail
  19. DNSTPL=$HESTIA/data/templates/dns
  20. RRD=$HESTIA/web/rrd
  21. SENDMAIL="$HESTIA/web/inc/mail-wrapper.php"
  22. HESTIA_GIT_REPO="https://raw.githubusercontent.com/hestiacp/hestiacp"
  23. HESTIA_THEMES="$HESTIA_INSTALL_DIR/themes"
  24. HESTIA_THEMES_CUSTOM="$HESTIA/data/templates/themes"
  25. SCRIPT="$(basename $0)"
  26. # Return codes
  27. OK=0
  28. E_ARGS=1
  29. E_INVALID=2
  30. E_NOTEXIST=3
  31. E_EXISTS=4
  32. E_SUSPENDED=5
  33. E_UNSUSPENDED=6
  34. E_INUSE=7
  35. E_LIMIT=8
  36. E_PASSWORD=9
  37. E_FORBIDEN=10
  38. E_DISABLED=11
  39. E_PARSING=12
  40. E_DISK=13
  41. E_LA=14
  42. E_CONNECT=15
  43. E_FTP=16
  44. E_DB=17
  45. E_RRD=18
  46. E_UPDATE=19
  47. E_RESTART=20
  48. # Detect operating system
  49. detect_os() {
  50. if [ -e "/etc/os-release" ]; then
  51. get_os_type=$(grep "^ID=" /etc/os-release | cut -f 2 -d '=')
  52. if [ "$get_os_type" = "ubuntu" ]; then
  53. if [ -e '/usr/bin/lsb_release' ]; then
  54. OS_VERSION="$(lsb_release -s -r)"
  55. OS_TYPE='Ubuntu'
  56. fi
  57. elif [ "$get_os_type" = "debian" ]; then
  58. OS_TYPE='Debian'
  59. OS_VERSION=$(cat /etc/debian_version|grep -o "[0-9]\{1,2\}"|head -n1)
  60. fi
  61. else
  62. OS_TYPE="Unsupported OS"
  63. OS_VERSION="Unknown"
  64. fi
  65. }
  66. # Generate time stamp
  67. new_timestamp() {
  68. time_n_date=$(date +'%T %F')
  69. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  70. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  71. }
  72. # Event string for logger
  73. ARGS=("$@")
  74. for ((I=1; I <= $# ; I++)); do
  75. if [[ "$HIDE" != $I ]]; then
  76. ARGUMENTS="$ARGUMENTS '${ARGS[${I}-1]}'"
  77. else
  78. ARGUMENTS="$ARGUMENTS '******'"
  79. fi
  80. done
  81. # Log event function
  82. log_event() {
  83. if [ -z "$time" ]; then
  84. LOG_TIME="$(date +'%F %T') $(basename $0)"
  85. else
  86. LOG_TIME="$date $time $(basename $0)"
  87. fi
  88. if [ "$1" -eq 0 ]; then
  89. echo "$LOG_TIME $2" >> $HESTIA/log/system.log
  90. else
  91. echo "$LOG_TIME $2 [Error $1]" >> $HESTIA/log/error.log
  92. fi
  93. }
  94. # Log user history
  95. log_history() {
  96. cmd=$1
  97. undo=${2-no}
  98. log_user=${3-$user}
  99. if ! $BIN/v-list-user "$log_user" >/dev/null; then
  100. return $E_NOTEXIST
  101. fi
  102. log=$HESTIA/data/users/$log_user/history.log
  103. touch $log
  104. if [ '99' -lt "$(wc -l $log |cut -f 1 -d ' ')" ]; then
  105. tail -n 49 $log > $log.moved
  106. mv -f $log.moved $log
  107. chmod 660 $log
  108. fi
  109. if [ -z "$date" ]; then
  110. time_n_date=$(date +'%T %F')
  111. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  112. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  113. fi
  114. curr_str=$(grep "ID=" $log | cut -f 2 -d \' | sort -n | tail -n1)
  115. id="$((curr_str +1))"
  116. echo "ID='$id' DATE='$date' TIME='$time' CMD='$cmd' UNDO='$undo'" >> $log
  117. }
  118. # Result checker
  119. check_result() {
  120. if [ $1 -ne 0 ]; then
  121. echo "Error: $2"
  122. if [ ! -z "$3" ]; then
  123. log_event "$3" "$ARGUMENTS"
  124. exit $3
  125. else
  126. log_event "$1" "$ARGUMENTS"
  127. exit $1
  128. fi
  129. fi
  130. }
  131. # Argument list checker
  132. check_args() {
  133. if [ "$1" -gt "$2" ]; then
  134. echo "Usage: $(basename $0) $3"
  135. check_result $E_ARGS "not enought arguments" >/dev/null
  136. fi
  137. }
  138. # Subsystem checker
  139. is_system_enabled() {
  140. if [ -z "$1" ] || [ "$1" = no ]; then
  141. check_result $E_DISABLED "$2 is not enabled"
  142. fi
  143. }
  144. # User package check
  145. is_package_full() {
  146. case "$1" in
  147. WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf);;
  148. WEB_ALIASES) used=$(echo $aliases |tr ',' '\n' |wc -l);;
  149. DNS_DOMAINS) used=$(wc -l $USER_DATA/dns.conf);;
  150. DNS_RECORDS) used=$(wc -l $USER_DATA/dns/$domain.conf);;
  151. MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf);;
  152. MAIL_ACCOUNTS) used=$(wc -l $USER_DATA/mail/$domain.conf);;
  153. DATABASES) used=$(wc -l $USER_DATA/db.conf);;
  154. CRON_JOBS) used=$(wc -l $USER_DATA/cron.conf);;
  155. esac
  156. used=$(echo "$used"| cut -f 1 -d \ )
  157. limit=$(grep "^$1=" $USER_DATA/user.conf |cut -f 2 -d \')
  158. if [ "$limit" != 'unlimited' ] && [[ "$used" -ge "$limit" ]]; then
  159. check_result $E_LIMIT "$1 limit is reached :: upgrade user package"
  160. fi
  161. }
  162. # User owner for reseller plugin
  163. get_user_owner() {
  164. if [ -z "$RESELLER_KEY" ]; then
  165. owner='admin'
  166. else
  167. owner=$(grep "^OWNER" $USER_DATA/user.conf| cut -f 2 -d \')
  168. if [ -z "$owner" ]; then
  169. owner='admin'
  170. fi
  171. fi
  172. }
  173. # Random password generator
  174. generate_password() {
  175. matrix=$1
  176. length=$2
  177. if [ -z "$matrix" ]; then
  178. matrix="A-Za-z0-9"
  179. fi
  180. if [ -z "$length" ]; then
  181. length=16
  182. fi
  183. head /dev/urandom | tr -dc $matrix | head -c$length
  184. }
  185. # Package existence check
  186. is_package_valid() {
  187. if [ -z "$1" ]; then
  188. pkg_dir="$HESTIA/data/packages"
  189. fi
  190. if [ ! -e "$pkg_dir/$package.pkg" ]; then
  191. check_result $E_NOTEXIST "package $package doesn't exist"
  192. fi
  193. }
  194. # Validate system type
  195. is_type_valid() {
  196. if [ -z "$(echo $1 | grep -w $2)" ]; then
  197. check_result $E_INVALID "$2 type is invalid"
  198. fi
  199. }
  200. # Check user backup settings
  201. is_backup_enabled() {
  202. BACKUPS=$(grep "^BACKUPS=" $USER_DATA/user.conf | cut -f2 -d \')
  203. if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then
  204. check_result $E_DISABLED "user backup is disabled"
  205. fi
  206. }
  207. # Check user backup settings
  208. is_backup_scheduled() {
  209. if [ -e "$HESTIA/data/queue/backup.pipe" ]; then
  210. check_q=$(grep " $user " $HESTIA/data/queue/backup.pipe | grep $1)
  211. if [ ! -z "$check_q" ]; then
  212. check_result $E_EXISTS "$1 is already scheduled"
  213. fi
  214. fi
  215. }
  216. # Check if object is new
  217. is_object_new() {
  218. if [ $2 = 'USER' ]; then
  219. if [ -d "$USER_DATA" ]; then
  220. object="OK"
  221. fi
  222. else
  223. object=$(grep "$2='$3'" $USER_DATA/$1.conf)
  224. fi
  225. if [ ! -z "$object" ]; then
  226. check_result $E_EXISTS "$2=$3 is already exists"
  227. fi
  228. }
  229. # Check if object is valid
  230. is_object_valid() {
  231. if [ $2 = 'USER' ]; then
  232. tstpath="$(readlink -f "$HESTIA/data/users/$3")"
  233. if [ "$(dirname "$tstpath")" != "$(readlink -f "$HESTIA/data/users")" ] || [ ! -d "$HESTIA/data/users/$3" ]; then
  234. check_result $E_NOTEXIST "$1 $3 doesn't exist"
  235. fi
  236. else
  237. object=$(grep "$2='$3'" $HESTIA/data/users/$user/$1.conf)
  238. if [ -z "$object" ]; then
  239. arg1=$(basename $1)
  240. arg2=$(echo $2 |tr '[:upper:]' '[:lower:]')
  241. check_result $E_NOTEXIST "$arg1 $arg2 $3 doesn't exist"
  242. fi
  243. fi
  244. }
  245. # Check if a object string with key values pairs has the correct format and load it afterwards
  246. parse_object_kv_list_non_eval() {
  247. local str
  248. local objkv obj_key obj_val
  249. local OLD_IFS="$IFS"
  250. str=${@//$'\n'/ }
  251. str=${str//\"/\\\"}
  252. str=${str//$/\\$}
  253. IFS=$'\n'
  254. # Extract and loop trough each key-value pair. (Regex test: https://regex101.com/r/eiMufk/5)
  255. for objkv in $( echo "$str" | perl -n -e "while(/\b([a-zA-Z]+[\w]*)='(.*?)'(\s|\$)/g) {print \$1.'='.\$2 . \"\n\" }" ); do
  256. if ! [[ "$objkv" =~ ^([[:alnum:]][_[:alnum:]]{0,64}[[:alnum:]])=(\'?[^\']+?\'?)?$ ]]; then
  257. check_result $E_INVALID "Invalid key value format [$objkv]"
  258. fi
  259. obj_key=${objkv%%=*} # strip everything after first '=' char
  260. obj_val=${objkv#*=} # strip everything before first '=' char
  261. declare -g $obj_key="$obj_val"
  262. done
  263. IFS="$OLD_IFS"
  264. }
  265. # Check if a object string with key values pairs has the correct format and load it afterwards
  266. parse_object_kv_list() {
  267. local str
  268. local objkv
  269. local suboutput
  270. local OLD_IFS="$IFS"
  271. str=${@//$'\n'/ }
  272. str=${str//\"/\\\"}
  273. str=${str//$/\\$}
  274. IFS=$'\n'
  275. suboutput=$(setpriv --clear-groups --reuid nobody --regid nogroup bash -c "PS4=''; set -xe; eval \"${str}\"" 2>&1)
  276. check_result $? "Invalid object format: ${str}" $E_INVALID
  277. for objkv in $suboutput; do
  278. if [[ "$objkv" =~ ^'eval ' ]]; then
  279. continue
  280. fi
  281. if ! [[ "$objkv" =~ ^([[:alnum:]][_[:alnum:]]{0,64}[[:alnum:]])=(\'?[^\']+?\'?)?$ ]]; then
  282. check_result $E_INVALID "Invalid key value format [$objkv]"
  283. fi
  284. eval "$objkv"
  285. done
  286. IFS="$OLD_IFS"
  287. }
  288. # Check if object is supended
  289. is_object_suspended() {
  290. if [ $2 = 'USER' ]; then
  291. spnd=$(cat $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  292. else
  293. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  294. fi
  295. if [ -z "$spnd" ]; then
  296. check_result $E_UNSUSPENDED "$(basename $1) $3 is not suspended"
  297. fi
  298. }
  299. # Check if object is unsupended
  300. is_object_unsuspended() {
  301. if [ $2 = 'USER' ]; then
  302. spnd=$(cat $USER_DATA/$1.conf |grep "SUSPENDED='yes'")
  303. else
  304. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf |grep "SUSPENDED='yes'")
  305. fi
  306. if [ ! -z "$spnd" ]; then
  307. check_result $E_SUSPENDED "$(basename $1) $3 is suspended"
  308. fi
  309. }
  310. # Check if object value is empty
  311. is_object_value_empty() {
  312. str=$(grep "$2='$3'" $USER_DATA/$1.conf)
  313. parse_object_kv_list "$str"
  314. eval value=$4
  315. if [ ! -z "$value" ] && [ "$value" != 'no' ]; then
  316. check_result $E_EXISTS "${4//$}=$value is already exists"
  317. fi
  318. }
  319. # Check if object value is empty
  320. is_object_value_exist() {
  321. str=$(grep "$2='$3'" $USER_DATA/$1.conf)
  322. parse_object_kv_list "$str"
  323. eval value=$4
  324. if [ -z "$value" ] || [ "$value" = 'no' ]; then
  325. check_result $E_NOTEXIST "${4//$}=$value doesn't exist"
  326. fi
  327. }
  328. # Check if password is transmitted via file
  329. is_password_valid() {
  330. if [[ "$password" =~ ^/tmp/ ]]; then
  331. if [ -f "$password" ]; then
  332. password="$(head -n1 $password)"
  333. fi
  334. fi
  335. }
  336. # Check if hash is transmitted via file
  337. is_hash_valid() {
  338. if [[ "$hash" =~ ^/tmp/ ]]; then
  339. if [ -f "$hash" ]; then
  340. hash="$(head -n1 $hash)"
  341. fi
  342. fi
  343. }
  344. # Check if directory is a symlink
  345. is_dir_symlink() {
  346. if [[ -L "$1" ]]; then
  347. check_result $E_FORBIDEN "$1 directory is a symlink"
  348. fi
  349. }
  350. # Get object value
  351. get_object_value() {
  352. object=$(grep "$2='$3'" $USER_DATA/$1.conf)
  353. parse_object_kv_list "$object"
  354. eval echo $4
  355. }
  356. # Update object value
  357. update_object_value() {
  358. row=$(grep -nF "$2='$3'" $USER_DATA/$1.conf)
  359. lnr=$(echo $row | cut -f 1 -d ':')
  360. object=$(echo $row | sed "s/^$lnr://")
  361. parse_object_kv_list "$object"
  362. eval old="$4"
  363. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  364. new=$(echo "$5" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  365. sed -i "$lnr s/${4//$/}='${old//\*/\\*}'/${4//$/}='${new//\*/\\*}'/g" \
  366. $USER_DATA/$1.conf
  367. }
  368. # Add object key
  369. add_object_key() {
  370. row=$(grep -n "$2='$3'" $USER_DATA/$1.conf)
  371. lnr=$(echo $row | cut -f 1 -d ':')
  372. object=$(echo $row | sed "s/^$lnr://")
  373. if [ -z "$(echo $object |grep $4=)" ]; then
  374. eval old="$4"
  375. sed -i "$lnr s/$5='/$4='' $5='/" $USER_DATA/$1.conf
  376. fi
  377. }
  378. # Search objects
  379. search_objects() {
  380. OLD_IFS="$IFS"
  381. IFS=$'\n'
  382. for line in $(grep $2=\'$3\' $USER_DATA/$1.conf); do
  383. parse_object_kv_list "$line"
  384. eval echo \$$4
  385. done
  386. IFS="$OLD_IFS"
  387. }
  388. # Get user value
  389. get_user_value() {
  390. grep "^${1//$/}=" $USER_DATA/user.conf | head -1 | awk -F "'" '{print $2}'
  391. }
  392. # Update user value in user.conf
  393. update_user_value() {
  394. key="${2//$}"
  395. lnr=$(grep -n "^$key='" $HESTIA/data/users/$1/user.conf |cut -f 1 -d ':')
  396. if [ ! -z "$lnr" ]; then
  397. sed -i "$lnr d" $HESTIA/data/users/$1/user.conf
  398. sed -i "$lnr i\\$key='${3}'" $HESTIA/data/users/$1/user.conf
  399. fi
  400. }
  401. # Increase user counter
  402. increase_user_value() {
  403. key="${2//$}"
  404. factor="${3-1}"
  405. conf="$HESTIA/data/users/$1/user.conf"
  406. old=$(grep "$key=" $conf | cut -f 2 -d \')
  407. if [ -z "$old" ]; then
  408. old=0
  409. fi
  410. new=$((old + factor))
  411. sed -i "s/$key='$old'/$key='$new'/g" $conf
  412. }
  413. # Decrease user counter
  414. decrease_user_value() {
  415. key="${2//$}"
  416. factor="${3-1}"
  417. conf="$HESTIA/data/users/$1/user.conf"
  418. old=$(grep "$key=" $conf | cut -f 2 -d \')
  419. if [ -z "$old" ]; then
  420. old=0
  421. fi
  422. if [ "$old" -le 1 ]; then
  423. new=0
  424. else
  425. new=$((old - factor))
  426. fi
  427. if [ "$new" -lt 0 ]; then
  428. new=0
  429. fi
  430. sed -i "s/$key='$old'/$key='$new'/g" $conf
  431. }
  432. # Notify user
  433. send_notice() {
  434. topic=$1
  435. notice=$2
  436. if [ "$notify" = 'yes' ]; then
  437. touch $USER_DATA/notifications.conf
  438. chmod 660 $USER_DATA/notifications.conf
  439. time_n_date=$(date +'%T %F')
  440. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  441. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  442. nid=$(grep "NID=" $USER_DATA/notifications.conf |cut -f 2 -d \')
  443. nid=$(echo "$nid" |sort -n |tail -n1)
  444. if [ ! -z "$nid" ]; then
  445. nid="$((nid +1))"
  446. else
  447. nid=1
  448. fi
  449. str="NID='$nid' TOPIC='$topic' NOTICE='$notice' TYPE='$type'"
  450. str="$str ACK='no' TIME='$time' DATE='$date'"
  451. echo "$str" >> $USER_DATA/notifications.conf
  452. if [ -z "$(grep NOTIFICATIONS $USER_DATA/user.conf)" ]; then
  453. sed -i "s/^TIME/NOTIFICATIONS='yes'\nTIME/g" $USER_DATA/user.conf
  454. else
  455. update_user_value "$user" '$NOTIFICATIONS' "yes"
  456. fi
  457. fi
  458. }
  459. # Recalculate U_DISK value
  460. recalc_user_disk_usage() {
  461. u_usage=0
  462. if [ -f "$USER_DATA/web.conf" ]; then
  463. usage=0
  464. dusage=$(grep 'U_DISK=' $USER_DATA/web.conf |\
  465. awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  466. for disk_usage in $dusage; do
  467. usage=$((usage + disk_usage))
  468. done
  469. d=$(grep "U_DISK_WEB='" $USER_DATA/user.conf | cut -f 2 -d \')
  470. sed -i "s/U_DISK_WEB='$d'/U_DISK_WEB='$usage'/g" $USER_DATA/user.conf
  471. u_usage=$((u_usage + usage))
  472. fi
  473. if [ -f "$USER_DATA/mail.conf" ]; then
  474. usage=0
  475. dusage=$(grep 'U_DISK=' $USER_DATA/mail.conf |\
  476. awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  477. for disk_usage in $dusage; do
  478. usage=$((usage + disk_usage))
  479. done
  480. d=$(grep "U_DISK_MAIL='" $USER_DATA/user.conf | cut -f 2 -d \')
  481. sed -i "s/U_DISK_MAIL='$d'/U_DISK_MAIL='$usage'/g" $USER_DATA/user.conf
  482. u_usage=$((u_usage + usage))
  483. fi
  484. if [ -f "$USER_DATA/db.conf" ]; then
  485. usage=0
  486. dusage=$(grep 'U_DISK=' $USER_DATA/db.conf |\
  487. awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  488. for disk_usage in $dusage; do
  489. usage=$((usage + disk_usage))
  490. done
  491. d=$(grep "U_DISK_DB='" $USER_DATA/user.conf | cut -f 2 -d \')
  492. sed -i "s/U_DISK_DB='$d'/U_DISK_DB='$usage'/g" $USER_DATA/user.conf
  493. u_usage=$((u_usage + usage))
  494. fi
  495. usage=$(grep 'U_DISK_DIRS=' $USER_DATA/user.conf | cut -f 2 -d "'")
  496. u_usage=$((u_usage + usage))
  497. old=$(grep "U_DISK='" $USER_DATA/user.conf | cut -f 2 -d \')
  498. sed -i "s/U_DISK='$old'/U_DISK='$u_usage'/g" $USER_DATA/user.conf
  499. }
  500. # Recalculate U_BANDWIDTH value
  501. recalc_user_bandwidth_usage() {
  502. usage=0
  503. bandwidth_usage=$(grep 'U_BANDWIDTH=' $USER_DATA/web.conf |\
  504. awk -F "U_BANDWIDTH='" '{print $2}'|cut -f 1 -d \')
  505. for bandwidth in $bandwidth_usage; do
  506. usage=$((usage + bandwidth))
  507. done
  508. old=$(grep "U_BANDWIDTH='" $USER_DATA/user.conf | cut -f 2 -d \')
  509. sed -i "s/U_BANDWIDTH='$old'/U_BANDWIDTH='$usage'/g" $USER_DATA/user.conf
  510. }
  511. # Get next cron job id
  512. get_next_cronjob() {
  513. if [ -z "$job" ]; then
  514. curr_str=$(grep "JOB=" $USER_DATA/cron.conf|cut -f 2 -d \'|\
  515. sort -n|tail -n1)
  516. job="$((curr_str +1))"
  517. fi
  518. }
  519. # Sort cron jobs by id
  520. sort_cron_jobs() {
  521. cat $USER_DATA/cron.conf |sort -n -k 2 -t \' > $USER_DATA/cron.tmp
  522. mv -f $USER_DATA/cron.tmp $USER_DATA/cron.conf
  523. }
  524. # Sync cronjobs with system cron
  525. sync_cron_jobs() {
  526. source $USER_DATA/user.conf
  527. if [ -e "/var/spool/cron/crontabs" ]; then
  528. crontab="/var/spool/cron/crontabs/$user"
  529. else
  530. crontab="/var/spool/cron/$user"
  531. fi
  532. rm -f $crontab
  533. if [ "$CRON_REPORTS" = 'yes' ]; then
  534. echo "MAILTO=$CONTACT" > $crontab
  535. echo 'CONTENT_TYPE="text/plain; charset=utf-8"' >> $crontab
  536. fi
  537. while read line; do
  538. parse_object_kv_list "$line"
  539. if [ "$SUSPENDED" = 'no' ]; then
  540. echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" |\
  541. sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" \
  542. >> $crontab
  543. fi
  544. done < $USER_DATA/cron.conf
  545. chown $user:$user $crontab
  546. chmod 600 $crontab
  547. }
  548. # User format validator
  549. is_user_format_valid() {
  550. if [ ${#1} -eq 1 ]; then
  551. if ! [[ "$1" =~ ^^[[:alnum:]]$ ]]; then
  552. check_result $E_INVALID "invalid $2 format :: $1"
  553. fi
  554. else
  555. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,28}[[:alnum:]]$ ]]
  556. then
  557. check_result $E_INVALID "invalid $2 format :: $1"
  558. fi
  559. fi
  560. }
  561. # Domain format validator
  562. is_domain_format_valid() {
  563. object_name=${2-domain}
  564. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%|\`| ]"
  565. if [[ $1 =~ $exclude ]] || [[ $1 =~ ^[0-9]+$ ]] || [[ $1 =~ "\.\." ]] || [[ $1 =~ "$(printf '\t')" ]]; then
  566. check_result $E_INVALID "invalid $object_name format :: $1"
  567. fi
  568. }
  569. # Alias forman validator
  570. is_alias_format_valid() {
  571. for object in ${1//,/ }; do
  572. exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|<|>|?|_|/|\|\"|'|;|%|\`| ]"
  573. if [[ "$object" =~ $exclude ]]; then
  574. check_result $E_INVALID "invalid alias format :: $object"
  575. fi
  576. if [[ "$object" =~ [*] ]] && ! [[ "$object" =~ ^[*]\..* ]]; then
  577. check_result $E_INVALID "invalid alias format :: $object"
  578. fi
  579. done
  580. }
  581. # IP format validator
  582. is_ip_format_valid() {
  583. object_name=${2-ip}
  584. ip_regex='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
  585. ip_clean=$(echo "${1%/*}")
  586. if ! [[ $ip_clean =~ ^$ip_regex\.$ip_regex\.$ip_regex\.$ip_regex$ ]]; then
  587. check_result $E_INVALID "invalid $object_name format :: $1"
  588. fi
  589. if [ $1 != "$ip_clean" ]; then
  590. ip_cidr="$ip_clean/"
  591. ip_cidr=$(echo "${1#$ip_cidr}")
  592. if [[ "$ip_cidr" -gt 32 ]] || [[ "$ip_cidr" =~ [:alnum:] ]]; then
  593. check_result $E_INVALID "invalid $object_name format :: $1"
  594. fi
  595. fi
  596. }
  597. # Proxy extention format validator
  598. is_extention_format_valid() {
  599. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|@|<|>|?|/|\|\"|'|;|%|\`| ]"
  600. if [[ "$1" =~ $exclude ]]; then
  601. check_result $E_INVALID "invalid proxy extention format :: $1"
  602. fi
  603. }
  604. # Number format validator
  605. is_number_format_valid() {
  606. object_name=${2-number}
  607. if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
  608. check_result $E_INVALID "invalid $object_name format :: $1"
  609. fi
  610. }
  611. # Autoreply format validator
  612. is_autoreply_format_valid() {
  613. if [[ "$1" =~ [$|\`] ]] || [ 10240 -le ${#1} ]; then
  614. check_result $E_INVALID "invalid autoreply format :: $1"
  615. fi
  616. }
  617. # Boolean format validator
  618. is_boolean_format_valid() {
  619. if [ "$1" != 'yes' ] && [ "$1" != 'no' ]; then
  620. check_result $E_INVALID "invalid $2 format :: $1"
  621. fi
  622. }
  623. # Common format validator
  624. is_common_format_valid() {
  625. exclude="[!|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%|\`| ]"
  626. if [[ "$1" =~ $exclude ]]; then
  627. check_result $E_INVALID "invalid $2 format :: $1"
  628. fi
  629. if [ 400 -le ${#1} ]; then
  630. check_result $E_INVALID "invalid $2 format :: $1"
  631. fi
  632. if [[ "$1" =~ @ ]] && [ ${#1} -gt 1 ] ; then
  633. check_result $E_INVALID "invalid $2 format :: $1"
  634. fi
  635. if [[ $1 =~ \* ]]; then
  636. if [[ "$(echo $1 | grep -o '\*\.' |wc -l)" -eq 0 ]] && [[ $1 != '*' ]] ; then
  637. check_result $E_INVALID "invalid $2 format :: $1"
  638. fi
  639. fi
  640. if [[ $(echo -n "$1" | tail -c 1) =~ [^a-zA-Z0-9_*@] ]]; then
  641. check_result $E_INVALID "invalid $2 format :: $1"
  642. fi
  643. if [[ $(echo -n "$1" | grep -c '\.\.') -gt 0 ]];then
  644. check_result $E_INVALID "invalid $2 format :: $1"
  645. fi
  646. if [[ $(echo -n "$1" | head -c 1) =~ [^a-zA-Z0-9_*@] ]]; then
  647. check_result $E_INVALID "invalid $2 format :: $1"
  648. fi
  649. if [[ $(echo -n "$1" | grep -c '\-\-') -gt 0 ]]; then
  650. check_result $E_INVALID "invalid $2 format :: $1"
  651. fi
  652. if [[ $(echo -n "$1" | grep -c '\_\_') -gt 0 ]]; then
  653. check_result $E_INVALID "invalid $2 format :: $1"
  654. fi
  655. }
  656. # Database format validator
  657. is_database_format_valid() {
  658. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  659. if [[ "$1" =~ $exclude ]] || [ 64 -le ${#1} ]; then
  660. check_result $E_INVALID "invalid $2 format :: $1"
  661. fi
  662. }
  663. # Date format validator
  664. is_date_format_valid() {
  665. if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
  666. check_result $E_INVALID "invalid date format :: $1"
  667. fi
  668. }
  669. # Database user validator
  670. is_dbuser_format_valid() {
  671. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|/|\|\"|'|;|%|\`| ]"
  672. if [ 33 -le ${#1} ]; then
  673. check_result $E_INVALID "mysql username can be up to 32 characters long"
  674. fi
  675. if [[ "$1" =~ $exclude ]]; then
  676. check_result $E_INVALID "invalid $2 format :: $1"
  677. fi
  678. }
  679. # DNS record type validator
  680. is_dns_type_format_valid() {
  681. known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF,TLSA,CAA'
  682. if [ -z "$(echo $known_dnstype |grep -w $1)" ]; then
  683. check_result $E_INVALID "invalid dns record type format :: $1"
  684. fi
  685. }
  686. # DNS record validator
  687. is_dns_record_format_valid() {
  688. if [ "$rtype" = 'A' ]; then
  689. is_ip_format_valid "$1"
  690. fi
  691. if [ "$rtype" = 'NS' ]; then
  692. is_domain_format_valid "${1::-1}" 'ns_record'
  693. fi
  694. if [ "$rtype" = 'MX' ]; then
  695. is_domain_format_valid "${1::-1}" 'mx_record'
  696. is_int_format_valid "$priority" 'priority_record'
  697. fi
  698. }
  699. # Email format validator
  700. is_email_format_valid() {
  701. if [[ ! "$1" =~ ^[A-Za-z0-9._%+-]+@[[:alnum:].-]+\.[A-Za-z]{2,63}$ ]] ; then
  702. check_result $E_INVALID "invalid email format :: $1"
  703. fi
  704. }
  705. # Firewall action validator
  706. is_fw_action_format_valid() {
  707. if [ "$1" != "ACCEPT" ] && [ "$1" != 'DROP' ] ; then
  708. check_result $E_INVALID "invalid action format :: $1"
  709. fi
  710. }
  711. # Firewall protocol validator
  712. is_fw_protocol_format_valid() {
  713. if [ "$1" != "ICMP" ] && [ "$1" != 'UDP' ] && [ "$1" != 'TCP' ] ; then
  714. check_result $E_INVALID "invalid protocol format :: $1"
  715. fi
  716. }
  717. # Firewall port validator
  718. is_fw_port_format_valid() {
  719. if [ "${#1}" -eq 1 ]; then
  720. if ! [[ "$1" =~ [0-9] ]]; then
  721. check_result $E_INVALID "invalid port format :: $1"
  722. fi
  723. else
  724. if ! [[ "$1" =~ ^[0-9][-|,|:|0-9]{0,30}[0-9]$ ]]
  725. then
  726. check_result $E_INVALID "invalid port format :: $1"
  727. fi
  728. fi
  729. }
  730. # Integer validator
  731. is_int_format_valid() {
  732. if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
  733. check_result $E_INVALID "invalid $2 format :: $1"
  734. fi
  735. }
  736. # Interface validator
  737. is_interface_format_valid() {
  738. netdevices=$(cat /proc/net/dev |grep : |cut -f 1 -d : |tr -d ' ')
  739. if [ -z $(echo "$netdevices" |grep -x $1) ]; then
  740. check_result $E_INVALID "invalid interface format :: $1"
  741. fi
  742. }
  743. # IP status validator
  744. is_ip_status_format_valid() {
  745. if [ -z "$(echo shared,dedicated | grep -w $1 )" ]; then
  746. check_result $E_INVALID "invalid status format :: $1"
  747. fi
  748. }
  749. # Cron validator
  750. is_cron_format_valid() {
  751. limit=59
  752. check_format=''
  753. if [ "$2" = 'hour' ]; then
  754. limit=23
  755. fi
  756. if [ "$2" = 'day' ]; then
  757. limit=31
  758. fi
  759. if [ "$2" = 'month' ]; then
  760. limit=12
  761. fi
  762. if [ "$2" = 'wday' ]; then
  763. limit=7
  764. fi
  765. if [ "$1" = '*' ]; then
  766. check_format='ok'
  767. fi
  768. if [[ "$1" =~ ^[\*]+[/]+[0-9] ]]; then
  769. if [ "$(echo $1 |cut -f 2 -d /)" -lt $limit ]; then
  770. check_format='ok'
  771. fi
  772. fi
  773. if [[ "$1" =~ ^[0-9][-|,|0-9]{0,70}[\/][0-9]$ ]]; then
  774. check_format='ok'
  775. crn_values=${1//,/ }
  776. crn_values=${crn_values//-/ }
  777. crn_values=${crn_values//\// }
  778. for crn_vl in $crn_values; do
  779. if [ "$crn_vl" -gt $limit ]; then
  780. check_format='invalid'
  781. fi
  782. done
  783. fi
  784. crn_values=$(echo $1 |tr "," " " | tr "-" " ")
  785. for crn_vl in $crn_values
  786. do
  787. if [[ "$crn_vl" =~ ^[0-9]+$ ]] && [ "$crn_vl" -le $limit ]; then
  788. check_format='ok'
  789. fi
  790. done
  791. if [ "$check_format" != 'ok' ]; then
  792. check_result $E_INVALID "invalid $2 format :: $1"
  793. fi
  794. }
  795. # Name validator
  796. is_name_format_valid() {
  797. if ! [[ "$1" =~ ^[[:alnum:]][-|\ |\.|_[:alnum:]]{0,28}[[:alnum:]]$ ]]; then
  798. check_result $E_INVALID "invalid $2 format :: $1"
  799. fi
  800. }
  801. # Object validator
  802. is_object_format_valid() {
  803. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}[[:alnum:]]$ ]]; then
  804. check_result $E_INVALID "invalid $2 format :: $1"
  805. fi
  806. }
  807. # Role validator
  808. is_role_valid (){
  809. if ! [[ "$1" =~ ^admin|user$ ]]; then
  810. check_result $E_INVALID "invalid $2 format :: $1"
  811. fi
  812. }
  813. # Password validator
  814. is_password_format_valid() {
  815. if [ "${#1}" -lt '6' ]; then
  816. check_result $E_INVALID "invalid password format :: $1"
  817. fi
  818. }
  819. # Missing function -
  820. # Before: validate_format_shell
  821. # After: is_format_valid_shell
  822. is_format_valid_shell() {
  823. if [ -z "$(grep -w $1 /etc/shells)" ]; then
  824. echo "Error: shell $1 is not valid"
  825. log_event "$E_INVALID" "$EVENT"
  826. exit $E_INVALID
  827. fi
  828. }
  829. # Service name validator
  830. is_service_format_valid() {
  831. if ! [[ "$1" =~ ^[[:alnum:]][-|\.|_[:alnum:]]{0,64}$ ]]; then
  832. check_result $E_INVALID "invalid $2 format :: $1"
  833. fi
  834. }
  835. is_hash_format_valid() {
  836. if ! [[ "$1" =~ ^[_A-Za-z0-9]{1,32}$ ]]; then
  837. check_result $E_INVALID "invalid $2 format :: $1"
  838. fi
  839. }
  840. # Format validation controller
  841. is_format_valid() {
  842. for arg_name in $*; do
  843. eval arg=\$$arg_name
  844. if [ ! -z "$arg" ]; then
  845. case $arg_name in
  846. account) is_user_format_valid "$arg" "$arg_name";;
  847. action) is_fw_action_format_valid "$arg";;
  848. active) is_boolean_format_valid "$arg" 'active' ;;
  849. aliases) is_alias_format_valid "$arg" ;;
  850. antispam) is_boolean_format_valid "$arg" 'antispam' ;;
  851. antivirus) is_boolean_format_valid "$arg" 'antivirus' ;;
  852. autoreply) is_autoreply_format_valid "$arg" ;;
  853. backup) is_object_format_valid "$arg" 'backup' ;;
  854. charset) is_object_format_valid "$arg" "$arg_name" ;;
  855. charsets) is_common_format_valid "$arg" 'charsets' ;;
  856. comment) is_object_format_valid "$arg" 'comment' ;;
  857. database) is_database_format_valid "$arg" 'database';;
  858. day) is_cron_format_valid "$arg" $arg_name ;;
  859. dbpass) is_password_format_valid "$arg" ;;
  860. dbuser) is_dbuser_format_valid "$arg" 'dbuser';;
  861. dkim) is_boolean_format_valid "$arg" 'dkim' ;;
  862. dkim_size) is_int_format_valid "$arg" ;;
  863. domain) is_domain_format_valid "$arg" ;;
  864. dvalue) is_dns_record_format_valid "$arg";;
  865. email) is_email_format_valid "$arg" ;;
  866. email_forward) is_email_format_valid "$arg" ;;
  867. exp) is_date_format_valid "$arg" ;;
  868. extentions) is_common_format_valid "$arg" 'extentions' ;;
  869. ftp_password) is_password_format_valid "$arg" ;;
  870. ftp_user) is_user_format_valid "$arg" "$arg_name" ;;
  871. hash) is_hash_format_valid "$arg" "$arg_name" ;;
  872. host) is_object_format_valid "$arg" "$arg_name" ;;
  873. hour) is_cron_format_valid "$arg" $arg_name ;;
  874. id) is_int_format_valid "$arg" 'id' ;;
  875. iface) is_interface_format_valid "$arg" ;;
  876. ip) is_ip_format_valid "$arg" ;;
  877. ip_name) is_domain_format_valid "$arg" 'IP name';;
  878. ip_status) is_ip_status_format_valid "$arg" ;;
  879. job) is_int_format_valid "$arg" 'job' ;;
  880. key) is_user_format_valid "$arg" "$arg_name" ;;
  881. malias) is_user_format_valid "$arg" "$arg_name" ;;
  882. max_db) is_int_format_valid "$arg" 'max db';;
  883. min) is_cron_format_valid "$arg" $arg_name ;;
  884. month) is_cron_format_valid "$arg" $arg_name ;;
  885. name) is_name_format_valid "$arg" "name" ;;
  886. nat_ip) is_ip_format_valid "$arg" ;;
  887. netmask) is_ip_format_valid "$arg" 'netmask' ;;
  888. newid) is_int_format_valid "$arg" 'id' ;;
  889. ns1) is_domain_format_valid "$arg" 'ns1' ;;
  890. ns2) is_domain_format_valid "$arg" 'ns2' ;;
  891. ns3) is_domain_format_valid "$arg" 'ns3' ;;
  892. ns4) is_domain_format_valid "$arg" 'ns4' ;;
  893. ns5) is_domain_format_valid "$arg" 'ns5' ;;
  894. ns6) is_domain_format_valid "$arg" 'ns6' ;;
  895. ns7) is_domain_format_valid "$arg" 'ns7' ;;
  896. ns8) is_domain_format_valid "$arg" 'ns8' ;;
  897. object) is_name_format_valid "$arg" 'object';;
  898. package) is_object_format_valid "$arg" "$arg_name" ;;
  899. password) is_password_format_valid "$arg" ;;
  900. port) is_int_format_valid "$arg" 'port' ;;
  901. port_ext) is_fw_port_format_valid "$arg";;
  902. protocol) is_fw_protocol_format_valid "$arg" ;;
  903. proxy_ext) is_extention_format_valid "$arg" ;;
  904. quota) is_int_format_valid "$arg" 'quota' ;;
  905. record) is_common_format_valid "$arg" 'record';;
  906. restart) is_boolean_format_valid "$arg" 'restart' ;;
  907. role) is_role_valid "$arg" 'role' ;;
  908. rtype) is_dns_type_format_valid "$arg" ;;
  909. rule) is_int_format_valid "$arg" "rule id" ;;
  910. service) is_service_format_valid "$arg" "$arg_name" ;;
  911. soa) is_domain_format_valid "$arg" 'SOA' ;;
  912. #missing command: is_format_valid_shell
  913. shell) is_format_valid_shell "$arg" ;;
  914. stats_pass) is_password_format_valid "$arg" ;;
  915. stats_user) is_user_format_valid "$arg" "$arg_name" ;;
  916. template) is_object_format_valid "$arg" "$arg_name" ;;
  917. ttl) is_int_format_valid "$arg" 'ttl';;
  918. user) is_user_format_valid "$arg" $arg_name;;
  919. wday) is_cron_format_valid "$arg" $arg_name ;;
  920. esac
  921. fi
  922. done
  923. }
  924. # Domain argument formatting
  925. format_domain() {
  926. if [[ "$domain" = *[![:ascii:]]* ]]; then
  927. if [[ "$domain" =~ [[:upper:]] ]]; then
  928. domain=$(echo "$domain" |sed 's/[[:upper:]].*/\L&/')
  929. fi
  930. else
  931. if [[ "$domain" =~ [[:upper:]] ]]; then
  932. domain=$(echo "$domain" |tr '[:upper:]' '[:lower:]')
  933. fi
  934. fi
  935. if [[ "$domain" =~ ^www\..* ]]; then
  936. domain=$(echo "$domain" |sed -e "s/^www.//")
  937. fi
  938. if [[ "$domain" =~ .*\.$ ]]; then
  939. domain=$(echo "$domain" |sed -e "s/[.]*$//g")
  940. fi
  941. if [[ "$domain" =~ ^\. ]]; then
  942. domain=$(echo "$domain" |sed -e "s/^[.]*//")
  943. fi
  944. }
  945. format_domain_idn() {
  946. if [ -z "$domain_idn" ]; then
  947. domain_idn=$domain
  948. fi
  949. if [[ "$domain_idn" = *[![:ascii:]]* ]]; then
  950. domain_idn=$(idn -t --quiet -a $domain_idn)
  951. fi
  952. }
  953. format_aliases() {
  954. if [ ! -z "$aliases" ] && [ "$aliases" != 'none' ]; then
  955. aliases=$(echo $aliases |tr '[:upper:]' '[:lower:]' |tr ',' '\n')
  956. aliases=$(echo "$aliases" |sed -e "s/\.$//" |sort -u)
  957. aliases=$(echo "$aliases" |tr -s '.')
  958. aliases=$(echo "$aliases" |sed -e "s/[.]*$//g")
  959. aliases=$(echo "$aliases" |sed -e "s/^[.]*//")
  960. aliases=$(echo "$aliases" |sed -e "/^$/d")
  961. aliases=$(echo "$aliases" |tr '\n' ',' |sed -e "s/,$//")
  962. fi
  963. }
  964. check_backup_conditions() {
  965. # Checking load average
  966. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  967. # i=0
  968. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  969. echo -e "$(date "+%F %T") Load Average $la"
  970. sleep 60
  971. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  972. done
  973. }
  974. # Define download function
  975. download_file() {
  976. local url=$1
  977. local destination=$2
  978. local force=$3
  979. # Default destination is the curent working directory
  980. local dstopt=""
  981. if [ ! -z "$(echo "$url" | grep -E "\.(gz|gzip|bz2|zip|xz)$")" ]; then
  982. # When an archive file is downloaded it will be first saved localy
  983. dstopt="--directory-prefix=$ARCHIVE_DIR"
  984. local is_archive="true"
  985. local filename="${url##*/}"
  986. if [ -z "$filename" ]; then
  987. >&2 echo "[!] No filename was found in url, exiting ($url)"
  988. exit 1
  989. fi
  990. if [ ! -z "$force" ] && [ -f "$ARCHIVE_DIR/$filename" ]; then
  991. rm -f $ARCHIVE_DIR/$filename
  992. fi
  993. elif [ ! -z "$destination" ]; then
  994. # Plain files will be written to specified location
  995. dstopt="-O $destination"
  996. fi
  997. # check for corrupted archive
  998. if [ -f "$ARCHIVE_DIR/$filename" ] && [ "$is_archive" = "true" ]; then
  999. tar -tzf "$ARCHIVE_DIR/$filename" > /dev/null 2>&1
  1000. if [ $? -ne 0 ]; then
  1001. >&2 echo "[!] Archive $ARCHIVE_DIR/$filename is corrupted, redownloading"
  1002. rm -f $ARCHIVE_DIR/$filename
  1003. fi
  1004. fi
  1005. if [ ! -f "$ARCHIVE_DIR/$filename" ]; then
  1006. wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  1007. fi
  1008. if [ ! -z "$destination" ] && [ "$is_archive" = "true" ]; then
  1009. if [ "$destination" = "-" ]; then
  1010. cat "$ARCHIVE_DIR/$filename"
  1011. elif [ -d "$(dirname $destination)" ]; then
  1012. cp "$ARCHIVE_DIR/$filename" "$destination"
  1013. fi
  1014. fi
  1015. }
  1016. check_hestia_demo_mode() {
  1017. demo_mode=$(grep DEMO_MODE /usr/local/hestia/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")
  1018. if [ ! -z "$demo_mode" ] && [ "$demo_mode" = "yes" ]; then
  1019. echo "ERROR: Unable to perform operation due to security restrictions that are in place."
  1020. exit 1
  1021. fi
  1022. }
  1023. multiphp_count() {
  1024. echo $(ls -d /etc/php/*/fpm/pool.d 2>/dev/null |wc -l)
  1025. }
  1026. multiphp_versions() {
  1027. local -a php_versions_list;
  1028. local php_ver;
  1029. if [ "$(multiphp_count)" -gt 0 ] ; then
  1030. for php_ver in $(ls -v /etc/php/); do
  1031. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  1032. php_versions_list+=($php_ver)
  1033. done
  1034. echo "${php_versions_list[@]}"
  1035. fi
  1036. }
  1037. multiphp_default_version() {
  1038. # Get system wide default php version (set by update-alternatives)
  1039. local sys_phpversion=$(php -r "echo (float)phpversion();")
  1040. # Check if the system php also has php-fpm enabled, otherwise return
  1041. # the most recent php version which does have it installed.
  1042. if [ ! -d "/etc/php/$sys_phpversion/fpm/pool.d/" ]; then
  1043. local all_versions="$(multiphp_versions)"
  1044. if [ ! -z "$all_versions" ]; then
  1045. sys_phpversion="${all_versions##*\ }";
  1046. fi
  1047. fi
  1048. echo "$sys_phpversion"
  1049. }
  1050. # Run arbitrary cli commands with dropped privileges
  1051. # Note: setpriv --init-groups is not available on debian9 (util-linux 2.29.2)
  1052. # Input:
  1053. # - $user : Vaild hestia user
  1054. user_exec() {
  1055. is_object_valid 'user' 'USER' "$user"
  1056. local user_groups=$(id -G "$user")
  1057. user_groups=${user_groups//\ /,}
  1058. setpriv --groups "$user_groups" --reuid "$user" --regid "$user" -- $@
  1059. }
  1060. # Simple chmod wrapper that skips symlink files after glob expand
  1061. no_symlink_chmod() {
  1062. local filemode=$1; shift;
  1063. for i in "$@"; do
  1064. [[ -L ${i} ]] && continue
  1065. chmod "${filemode}" "${i}"
  1066. done
  1067. }