main.sh 30 KB

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