main.sh 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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. fname) is_name_format_valid "$arg" "first name" ;;
  842. ftp_password) is_password_format_valid "$arg" ;;
  843. ftp_user) is_user_format_valid "$arg" "$arg_name" ;;
  844. host) is_object_format_valid "$arg" "$arg_name" ;;
  845. hour) is_cron_format_valid "$arg" $arg_name ;;
  846. id) is_int_format_valid "$arg" 'id' ;;
  847. ip) is_ip_format_valid "$arg" ;;
  848. ip_name) is_domain_format_valid "$arg" 'IP name';;
  849. ip_status) is_ip_status_format_valid "$arg" ;;
  850. job) is_int_format_valid "$arg" 'job' ;;
  851. key) is_user_format_valid "$arg" "$arg_name" ;;
  852. lname) is_name_format_valid "$arg" "last name" ;;
  853. malias) is_user_format_valid "$arg" "$arg_name" ;;
  854. max_db) is_int_format_valid "$arg" 'max db';;
  855. min) is_cron_format_valid "$arg" $arg_name ;;
  856. month) is_cron_format_valid "$arg" $arg_name ;;
  857. nat_ip) is_ip_format_valid "$arg" ;;
  858. netmask) is_ip_format_valid "$arg" 'netmask' ;;
  859. newid) is_int_format_valid "$arg" 'id' ;;
  860. ns1) is_domain_format_valid "$arg" 'ns1' ;;
  861. ns2) is_domain_format_valid "$arg" 'ns2' ;;
  862. ns3) is_domain_format_valid "$arg" 'ns3' ;;
  863. ns4) is_domain_format_valid "$arg" 'ns4' ;;
  864. ns5) is_domain_format_valid "$arg" 'ns5' ;;
  865. ns6) is_domain_format_valid "$arg" 'ns6' ;;
  866. ns7) is_domain_format_valid "$arg" 'ns7' ;;
  867. ns8) is_domain_format_valid "$arg" 'ns8' ;;
  868. object) is_name_format_valid "$arg" 'object';;
  869. package) is_object_format_valid "$arg" "$arg_name" ;;
  870. password) is_password_format_valid "$arg" ;;
  871. port) is_int_format_valid "$arg" 'port' ;;
  872. port_ext) is_fw_port_format_valid "$arg";;
  873. protocol) is_fw_protocol_format_valid "$arg" ;;
  874. proxy_ext) is_extention_format_valid "$arg" ;;
  875. quota) is_int_format_valid "$arg" 'quota' ;;
  876. record) is_common_format_valid "$arg" 'record';;
  877. restart) is_boolean_format_valid "$arg" 'restart' ;;
  878. rtype) is_dns_type_format_valid "$arg" ;;
  879. rule) is_int_format_valid "$arg" "rule id" ;;
  880. service) is_service_format_valid "$arg" "$arg_name" ;;
  881. soa) is_domain_format_valid "$arg" 'SOA' ;;
  882. #missing command: is_format_valid_shell
  883. shell) is_format_valid_shell "$arg" ;;
  884. stats_pass) is_password_format_valid "$arg" ;;
  885. stats_user) is_user_format_valid "$arg" "$arg_name" ;;
  886. template) is_object_format_valid "$arg" "$arg_name" ;;
  887. ttl) is_int_format_valid "$arg" 'ttl';;
  888. user) is_user_format_valid "$arg" $arg_name;;
  889. wday) is_cron_format_valid "$arg" $arg_name ;;
  890. esac
  891. fi
  892. done
  893. }
  894. # Domain argument formatting
  895. format_domain() {
  896. if [[ "$domain" = *[![:ascii:]]* ]]; then
  897. if [[ "$domain" =~ [[:upper:]] ]]; then
  898. domain=$(echo "$domain" |sed 's/[[:upper:]].*/\L&/')
  899. fi
  900. else
  901. if [[ "$domain" =~ [[:upper:]] ]]; then
  902. domain=$(echo "$domain" |tr '[:upper:]' '[:lower:]')
  903. fi
  904. fi
  905. if [[ "$domain" =~ ^www\..* ]]; then
  906. domain=$(echo "$domain" |sed -e "s/^www.//")
  907. fi
  908. if [[ "$domain" =~ .*\.$ ]]; then
  909. domain=$(echo "$domain" |sed -e "s/[.]*$//g")
  910. fi
  911. if [[ "$domain" =~ ^\. ]]; then
  912. domain=$(echo "$domain" |sed -e "s/^[.]*//")
  913. fi
  914. }
  915. format_domain_idn() {
  916. if [ -z "$domain_idn" ]; then
  917. domain_idn=$domain
  918. fi
  919. if [[ "$domain_idn" = *[![:ascii:]]* ]]; then
  920. domain_idn=$(idn -t --quiet -a $domain_idn)
  921. fi
  922. }
  923. format_aliases() {
  924. if [ ! -z "$aliases" ] && [ "$aliases" != 'none' ]; then
  925. aliases=$(echo $aliases |tr '[:upper:]' '[:lower:]' |tr ',' '\n')
  926. aliases=$(echo "$aliases" |sed -e "s/\.$//" |sort -u)
  927. aliases=$(echo "$aliases" |tr -s '.')
  928. aliases=$(echo "$aliases" |sed -e "s/[.]*$//g")
  929. aliases=$(echo "$aliases" |sed -e "s/^[.]*//")
  930. aliases=$(echo "$aliases" |sed -e "/^$/d")
  931. aliases=$(echo "$aliases" |tr '\n' ',' |sed -e "s/,$//")
  932. fi
  933. }
  934. check_backup_conditions() {
  935. # Checking load average
  936. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  937. # i=0
  938. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  939. echo -e "$(date "+%F %T") Load Average $la"
  940. sleep 60
  941. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  942. done
  943. }
  944. # Define download function
  945. download_file() {
  946. local url=$1
  947. local destination=$2
  948. local force=$3
  949. # Default destination is the curent working directory
  950. local dstopt=""
  951. if [ ! -z "$(echo "$url" | grep -E "\.(gz|gzip|bz2|zip|xz)$")" ]; then
  952. # When an archive file is downloaded it will be first saved localy
  953. dstopt="--directory-prefix=$ARCHIVE_DIR"
  954. local is_archive="true"
  955. local filename="${url##*/}"
  956. if [ -z "$filename" ]; then
  957. >&2 echo "[!] No filename was found in url, exiting ($url)"
  958. exit 1
  959. fi
  960. if [ ! -z "$force" ] && [ -f "$ARCHIVE_DIR/$filename" ]; then
  961. rm -f $ARCHIVE_DIR/$filename
  962. fi
  963. elif [ ! -z "$destination" ]; then
  964. # Plain files will be written to specified location
  965. dstopt="-O $destination"
  966. fi
  967. # check for corrupted archive
  968. if [ -f "$ARCHIVE_DIR/$filename" ] && [ "$is_archive" = "true" ]; then
  969. tar -tzf "$ARCHIVE_DIR/$filename" > /dev/null 2>&1
  970. if [ $? -ne 0 ]; then
  971. >&2 echo "[!] Archive $ARCHIVE_DIR/$filename is corrupted, redownloading"
  972. rm -f $ARCHIVE_DIR/$filename
  973. fi
  974. fi
  975. if [ ! -f "$ARCHIVE_DIR/$filename" ]; then
  976. wget $url -q $dstopt --show-progress --progress=bar:force --limit-rate=3m
  977. fi
  978. if [ ! -z "$destination" ] && [ "$is_archive" = "true" ]; then
  979. if [ "$destination" = "-" ]; then
  980. cat "$ARCHIVE_DIR/$filename"
  981. elif [ -d "$(dirname $destination)" ]; then
  982. cp "$ARCHIVE_DIR/$filename" "$destination"
  983. fi
  984. fi
  985. }
  986. check_hestia_demo_mode() {
  987. demo_mode=$(grep DEMO_MODE /usr/local/hestia/conf/hestia.conf | cut -d '=' -f2 | sed "s|'||g")
  988. if [ ! -z "$demo_mode" ] && [ "$demo_mode" = "yes" ]; then
  989. echo "ERROR: Unable to perform operation due to security restrictions that are in place."
  990. exit 1
  991. fi
  992. }
  993. multiphp_count() {
  994. echo $(ls -d /etc/php/*/fpm/pool.d 2>/dev/null |wc -l)
  995. }
  996. multiphp_versions() {
  997. local -a php_versions_list;
  998. local php_ver;
  999. if [ "$(multiphp_count)" -gt 0 ] ; then
  1000. for php_ver in $(ls -v /etc/php/); do
  1001. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  1002. php_versions_list+=($php_ver)
  1003. done
  1004. echo "${php_versions_list[@]}"
  1005. fi
  1006. }
  1007. multiphp_default_version() {
  1008. # Get system wide default php version (set by update-alternatives)
  1009. local sys_phpversion=$(php -r "echo (float)phpversion();")
  1010. # Check if the system php also has php-fpm enabled, otherwise return
  1011. # the most recent php version which does have it installed.
  1012. if [ ! -d "/etc/php/$sys_phpversion/fpm/pool.d/" ]; then
  1013. local all_versions="$(multiphp_versions)"
  1014. if [ ! -z "$all_versions" ]; then
  1015. sys_phpversion="${all_versions##*\ }";
  1016. fi
  1017. fi
  1018. echo "$sys_phpversion"
  1019. }
  1020. # Run arbitrary cli commands with dropped privileges
  1021. # Note: setpriv --init-groups is not available on debian9 (util-linux 2.29.2)
  1022. # Input:
  1023. # - $user : Vaild hestia user
  1024. user_exec() {
  1025. is_object_valid 'user' 'USER' "$user"
  1026. local user_groups=$(id -G "$user")
  1027. user_groups=${user_groups//\ /,}
  1028. setpriv --groups "$user_groups" --reuid "$user" --regid "$user" -- $@
  1029. }