main.sh 35 KB

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