main.sh 34 KB

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