main.sh 31 KB

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