main.sh 31 KB

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