main.sh 32 KB

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