main.sh 35 KB

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