main.sh 31 KB

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