shared.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. # Internal variables
  2. DATE=$(date +%F)
  3. TIME=$(date +%T)
  4. SCRIPT=$(basename $0)
  5. EVENT="DATE='$DATE' TIME='$TIME' COMMAND='$SCRIPT' ARGUMENTS='$*'"
  6. BACKUP_GZIP=5
  7. BACKUP_DISK_LIMIT=95
  8. BACKUP_LA_LIMIT=5
  9. RRD_STEP=300
  10. RRD_IFACE_EXCLUDE=lo
  11. BIN=$VESTA/bin
  12. USER_DATA=$VESTA/data/users/$user
  13. WEBTPL=$VESTA/data/templates/web
  14. DNSTPL=$VESTA/data/templates/dns
  15. RRD=$VESTA/web/rrd
  16. # Return codes
  17. OK=0
  18. E_ARGS=1
  19. E_INVALID=2
  20. E_NOTEXIST=3
  21. E_EXISTS=4
  22. E_SUSPENDED=5
  23. E_UNSUSPENDED=6
  24. E_INUSE=7
  25. E_LIMIT=8
  26. E_PASSWORD=9
  27. E_FORBIDEN=10
  28. E_DISABLED=11
  29. E_PARSING=12
  30. E_DISK=13
  31. E_LA=14
  32. E_FTP=15
  33. E_SSH=16
  34. E_DB=17
  35. E_RRD=18
  36. E_UPDATE=19
  37. E_RESTART=20
  38. # Log event function
  39. log_event() {
  40. echo "$1 $2" >> $VESTA/log/system.log
  41. }
  42. # Log user history
  43. log_history() {
  44. echo "$1" >> $USER_DATA/history.log
  45. }
  46. # Argument list checker
  47. check_args() {
  48. if [ "$1" -gt "$2" ]; then
  49. echo "Error: bad args"
  50. echo "Usage: $SCRIPT $3"
  51. log_event "$E_ARGS" "$EVENT"
  52. exit $E_ARGS
  53. fi
  54. }
  55. # Subsystem checker
  56. is_system_enabled() {
  57. if [ -z "$1" ] || [ "$1" = no ]; then
  58. echo "Error: subsystem disabled"
  59. log_event "$E_DISABLED" "$EVENT"
  60. exit $E_DISABLED
  61. fi
  62. }
  63. # User package check
  64. is_package_full() {
  65. case "$1" in
  66. WEB_DOMAINS) used=$(wc -l $USER_DATA/web.conf|cut -f1 -d \ );;
  67. WEB_ALIASES) used=$(grep "DOMAIN='$domain'" $USER_DATA/web.conf |\
  68. awk -F "ALIAS='" '{print $2}' | cut -f 1 -d \' | tr ',' '\n' |\
  69. wc -l );;
  70. DNS_DOMAINS) used=$(wc -l $USER_DATA/dns.conf |cut -f1 -d \ );;
  71. DNS_RECORDS) used=$(wc -l $USER_DATA/dns/$domain |cut -f1 -d \ );;
  72. MAIL_DOMAINS) used=$(wc -l $USER_DATA/mail.conf |cut -f1 -d \ );;
  73. MAIL_ACCOUNTS) used=$(wc -l $USER_DATA/mail/$domain |\
  74. cut -f1 -d \ );;
  75. DATABASES) used=$(wc -l $USER_DATA/db.conf |cut -f1 -d \ );;
  76. CRON_JOBS) used=$(wc -l $USER_DATA/cron.conf |cut -f1 -d \ );;
  77. esac
  78. limit=$(grep "^$1=" $USER_DATA/user.conf | cut -f 2 -d \' )
  79. if [ "$used" -ge "$limit" ]; then
  80. echo "Error: Upgrade package"
  81. log_event "$E_LIMIT" "$EVENT"
  82. exit $E_LIMIT
  83. fi
  84. }
  85. # Random password generator
  86. gen_password() {
  87. matrix='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  88. lenght=10
  89. while [ ${n:=1} -le $lenght ]; do
  90. pass="$pass${matrix:$(($RANDOM%${#matrix})):1}"
  91. let n+=1
  92. done
  93. echo "$pass"
  94. }
  95. # Web template check
  96. is_apache_template_valid() {
  97. c=$(echo "$templates" | grep -w "$template")
  98. t="$WEBTPL/apache_$template.tpl"
  99. d="$WEBTPL/apache_$template.descr"
  100. s="$WEBTPL/apache_$template.stpl"
  101. if [ -z "$c" ] || [ ! -e $t ] || [ ! -e $d ] || [ ! -e $s ]; then
  102. echo "Error: $template not found"
  103. log_event "$E_NOTEXIST" "$EVENT"
  104. exit $E_NOTEXIST
  105. fi
  106. }
  107. # Nginx template check
  108. is_nginx_template_valid() {
  109. t="$WEBTPL/ngingx_vhost_$template.tpl"
  110. d="$WEBTPL/ngingx_vhost_$template.descr"
  111. s="$WEBTPL/ngingx_vhost_$template.stpl"
  112. if [ ! -e $t ] || [ ! -e $d ] || [ ! -e $s ]; then
  113. echo "Error: $template not found"
  114. log_event "$E_NOTEXIST" "$EVENT"
  115. exit $E_NOTEXIST
  116. fi
  117. }
  118. # DNS template check
  119. is_dns_template_valid() {
  120. tpl="$DNSTPL/$template.tpl"
  121. descr="$DNSTPL/$template.descr"
  122. if [ ! -e $tpl ] || [ ! -e $descr ]; then
  123. echo "Error: template not found"
  124. log_event "$E_NOTEXIST" "$EVENT"
  125. exit $E_NOTEXIST
  126. fi
  127. }
  128. # Package existance check
  129. is_package_valid() {
  130. if [ ! -e "$VESTA/data/packages/$package.pkg" ]; then
  131. echo "Error: $package is not exist"
  132. log_event "$E_NOTEXIST $EVENT"
  133. exit $E_NOTEXIST
  134. fi
  135. }
  136. # Validate system type
  137. is_type_valid() {
  138. if [ -z "$(echo $1 | grep -w $2)" ]; then
  139. echo "Error: unknown type"
  140. log_event "$E_INVALID" "$EVENT"
  141. exit $E_INVALID
  142. fi
  143. }
  144. # Checkk user backup settings
  145. is_backup_enabled() {
  146. BACKUPS=$(grep "BACKUPS='" $USER_DATA/user.conf | cut -f 2 -d \')
  147. if [ -z "$BACKUPS" ] || [[ "$BACKUPS" -le '0' ]]; then
  148. echo "Error: user backup disabled"
  149. log_event "$E_DISABLED" "$EVENT"
  150. exit $E_DISABLED
  151. fi
  152. }
  153. # Check if object is free and can be created
  154. is_object_free() {
  155. if [ $2 = 'USER' ]; then
  156. if [ -d "$USER_DATA" ]; then
  157. object="OK"
  158. fi
  159. else
  160. object=$(grep "$2='$3'" $USER_DATA/$1.conf)
  161. fi
  162. if [ ! -z "$object" ]; then
  163. echo "Error: $3 exists"
  164. log_event "$E_EXISTS" "$EVENT"
  165. exit $E_EXISTS
  166. fi
  167. }
  168. # Check if object exists and can be used
  169. is_object_valid() {
  170. if [ $2 = 'USER' ]; then
  171. if [ -d "$VESTA/data/users/$user" ]; then
  172. object="OK"
  173. fi
  174. else
  175. if [ $2 = 'DBHOST' ]; then
  176. object=$(grep "HOST='$host'" $VESTA/conf/$type.conf)
  177. else
  178. object=$(grep "$2='$3'" $VESTA/data/users/$user/$1.conf)
  179. fi
  180. fi
  181. if [ -z "$object" ]; then
  182. echo "Error: $3 not exist"
  183. log_event "$E_NOTEXIST" "$EVENT"
  184. exit $E_NOTEXIST
  185. fi
  186. }
  187. # Check if object is supended
  188. is_object_suspended() {
  189. if [ $2 = 'USER' ]; then
  190. spnd=$(cat $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  191. else
  192. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  193. fi
  194. if [ ! -z "$spnd" ]; then
  195. echo "Error: $3 is suspended"
  196. log_event "$E_SUSPENDED" "$EVENT"
  197. exit $E_SUSPENDED
  198. fi
  199. }
  200. # Check if object is unsupended
  201. is_object_unsuspended() {
  202. if [ $2 = 'USER' ]; then
  203. spnd=$(cat $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  204. else
  205. spnd=$(grep "$2='$3'" $USER_DATA/$1.conf|grep "SUSPENDED='yes'")
  206. fi
  207. if [ -z "$spnd" ]; then
  208. echo "Error: $3 is not suspended"
  209. log_event "$E_UNSUSPENDED" "$EVENT"
  210. exit $E_UNSUSPENDED
  211. fi
  212. }
  213. # Update object value
  214. update_object_value() {
  215. row=$(grep -n "$2='$3'" $USER_DATA/$1.conf)
  216. lnr=$(echo $row | cut -f 1 -d ':')
  217. object=$(echo $row | sed -e "s/^$lnr://")
  218. eval "$object"
  219. eval old="$4"
  220. old=$(echo "$old" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  221. new=$(echo "$5" | sed -e 's/\\/\\\\/g' -e 's/&/\\&/g' -e 's/\//\\\//g')
  222. sed -i "$lnr s/${4//$/}='${old//\*/\\*}'/${4//$/}='${new//\*/\\*}'/g" \
  223. $USER_DATA/$1.conf
  224. }
  225. # Search objects
  226. search_objects() {
  227. OLD_IFS="$IFS"
  228. IFS=$'\n'
  229. for line in $(grep $2=\'$3\' $USER_DATA/$1.conf); do
  230. eval $line
  231. eval echo \$$4
  232. done
  233. IFS="$OLD_IFS"
  234. }
  235. # Get user value
  236. get_user_value() {
  237. grep "^$1=" $USER_DATA/user.conf| cut -f 2 -d \'
  238. }
  239. # Update user value in user.conf
  240. update_user_value() {
  241. key="${2//$}"
  242. conf="$VESTA/data/users/$1/user.conf"
  243. old=$(grep "$key=" $conf | cut -f 2 -d \')
  244. sed -i "s/$key='$old'/$key='$3'/g" $conf
  245. }
  246. # Increase user counter
  247. increase_user_value() {
  248. key="${2//$}"
  249. factor="${3-1}"
  250. conf="$VESTA/data/users/$1/user.conf"
  251. old=$(grep "$key=" $conf | cut -f 2 -d \')
  252. if [ -z "$old" ]; then
  253. old=0
  254. fi
  255. new=$((old + factor))
  256. sed -i "s/$key='$old'/$key='$new'/g" $conf
  257. }
  258. # Decrease user counter
  259. decrease_user_value() {
  260. key="${2//$}"
  261. factor="${3-1}"
  262. conf="$VESTA/data/users/$1/user.conf"
  263. old=$(grep "$key=" $conf | cut -f 2 -d \')
  264. if [ -z "$old" ]; then
  265. old=0
  266. fi
  267. if [ "$old" -le 1 ]; then
  268. new=0
  269. else
  270. new=$((old - factor))
  271. fi
  272. sed -i "s/$key='$old'/$key='$new'/g" $conf
  273. }
  274. # Json listing function
  275. json_list() {
  276. echo '{'
  277. fileds_count=$(echo $fields| wc -w )
  278. while read line; do
  279. eval $line
  280. if [ -n "$data_output" ]; then
  281. echo -e ' },'
  282. fi
  283. i=1
  284. for field in $fields; do
  285. eval value=$field
  286. if [ $i -eq 1 ]; then
  287. (( ++i))
  288. echo -e "\t\"$value\": {"
  289. else
  290. if [ $i -lt $fileds_count ]; then
  291. (( ++i))
  292. echo -e "\t\t\"${field//$/}\": \"$value\","
  293. else
  294. echo -e "\t\t\"${field//$/}\": \"$value\""
  295. data_output=yes
  296. fi
  297. fi
  298. done
  299. done < $conf
  300. if [ "$data_output" = 'yes' ]; then
  301. echo -e ' }'
  302. fi
  303. echo -e '}'
  304. }
  305. # Shell listing function
  306. shell_list() {
  307. if [ -z "$nohead" ] ; then
  308. echo "${fields//$/}"
  309. for a in $fields; do
  310. echo -e "------ \c"
  311. done
  312. echo
  313. fi
  314. while read line ; do
  315. eval $line
  316. for field in $fields; do
  317. eval value=$field
  318. if [ -z "$value" ]; then
  319. value='NULL'
  320. fi
  321. echo -n "$value "
  322. done
  323. echo
  324. done < $conf
  325. }
  326. # Recalculate U_DISK value
  327. recalc_user_disk_usage() {
  328. usage=$(grep 'U_DIR_DISK=' $USER_DATA/user.conf | cut -f 2 -d "'")
  329. for conf_type in mail db web; do
  330. if [ -f "$USER_DATA/$conf_type.conf" ]; then
  331. dusage=$(grep 'U_DISK=' $USER_DATA/$conf_type.conf |\
  332. awk -F "U_DISK='" '{print $2}' | cut -f 1 -d \')
  333. for disk in $dusage; do
  334. usage=$((usage + disk))
  335. done
  336. fi
  337. done
  338. old=$(grep "U_DISK='" $USER_DATA/user.conf | cut -f 2 -d \')
  339. sed -i "s/U_DISK='$old'/U_DISK='$usage'/g" $USER_DATA/user.conf
  340. }
  341. # Recalculate U_BANDWIDTH value
  342. recalc_user_bandwidth_usage() {
  343. usage=0
  344. bandwidth_usage=$(grep 'U_BANDWIDTH=' $USER_DATA/web.conf |\
  345. awk -F "U_BANDWIDTH='" '{print $2}'|cut -f 1 -d \')
  346. for bandwidth in $bandwidth_usage; do
  347. usage=$((usage + bandwidth))
  348. done
  349. old=$(grep "U_BANDWIDTH='" $USER_DATA/user.conf | cut -f 2 -d \')
  350. sed -i "s/U_BANDWIDTH='$old'/U_BANDWIDTH='$usage'/g" $USER_DATA/user.conf
  351. }
  352. # Get next cron job id
  353. get_next_cronjob() {
  354. if [ -z "$job" ]; then
  355. curr_str=$(grep "JOB=" $USER_DATA/cron.conf|cut -f 2 -d \'|\
  356. sort -n|tail -n1)
  357. job="$((curr_str +1))"
  358. fi
  359. }
  360. # Sort cron jobs by id
  361. sort_cron_jobs() {
  362. cat $USER_DATA/cron.conf |sort -n -k 2 -t \' > $USER_DATA/cron.tmp
  363. mv -f $USER_DATA/cron.tmp $USER_DATA/cron.conf
  364. }
  365. # Sync cronjobs with system cron
  366. sync_cron_jobs() {
  367. source $USER_DATA/user.conf
  368. rm -f /var/spool/cron/$user
  369. if [ "$CRON_REPORTS" = 'yes' ]; then
  370. echo "MAILTO=$CONTACT" > /var/spool/cron/$user
  371. fi
  372. while read line ; do
  373. eval $line
  374. if [ "$SUSPENDED" = 'no' ] ; then
  375. echo "$MIN $HOUR $DAY $MONTH $WDAY $CMD" |\
  376. sed -e "s/%quote%/'/g" -e "s/%dots%/:/g" |\
  377. >> /var/spool/cron/$user
  378. fi
  379. done < $USER_DATA/cron.conf
  380. }
  381. ### Format Validators ###
  382. # URL
  383. validate_format_url() {
  384. check_http=$(echo "$1" | grep "^http://" )
  385. needed_chars=$(echo "$1" | cut -f 2 -d \.)
  386. if [ -z "$check_http" ] || [ -z "$needed_chars" ]; then
  387. echo "Error: url $1 is not valid"
  388. log_event "$E_INVALID" "$EVENT"
  389. exit $E_INVALID
  390. fi
  391. }
  392. # Shell
  393. validate_format_shell() {
  394. if [ -z "$(grep -x $1 /etc/shells)" ]; then
  395. echo "Error: shell $1 is not valid"
  396. log_event "$E_INVALID" "$EVENT"
  397. exit $E_INVALID
  398. fi
  399. }
  400. # Password
  401. validate_format_password() {
  402. if [ "${#1}" -lt '6' ]; then
  403. echo "Error: password is too short"
  404. log_event "$E_INVALID" "$EVENT"
  405. exit $E_INVALID
  406. fi
  407. }
  408. # Integer
  409. validate_format_int() {
  410. if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
  411. echo "Error: int $1 is not valid"
  412. log_event "$E_INVALID" "$EVENT"
  413. exit $E_INVALID
  414. fi
  415. }
  416. # Boolean
  417. validate_format_boolean() {
  418. if [ "$1" != 'yes' ] || [ "$1" != 'no' ]; then
  419. echo "Error: boolean $1 is not valid"
  420. log_event "$E_INVALID" "$EVENT"
  421. exit $E_INVALID
  422. fi
  423. }
  424. # Network interface
  425. validate_format_interface() {
  426. netdevices=$(cat /proc/net/dev | grep : | cut -f 1 -d : | tr -d ' ')
  427. if [ -z $(echo "$netdevices"| grep -x $1) ]; then
  428. echo "Error: intreface $1 is not valid"
  429. log_event "$E_INVALID" "$EVENT"
  430. exit $E_INVALID
  431. fi
  432. }
  433. # IP address
  434. validate_format_ip() {
  435. valid_octets=0
  436. for octet in ${1//./ }; do
  437. if [[ $octet =~ ^[0-9]{1,3}$ ]] && [[ $octet -le 255 ]]; then
  438. ((++valid_octets))
  439. fi
  440. done
  441. if [ "$valid_octets" -lt 4 ]; then
  442. echo "Error: ip $1 is not valid"
  443. log_event "$E_INVALID" "$EVENT"
  444. exit $E_INVALID
  445. fi
  446. }
  447. # IP address status
  448. validate_format_ip_status() {
  449. if [ -z "$(echo shared,dedicated | grep -w $1 )" ]; then
  450. echo "Error: ip_status $1 is not valid"
  451. log_event "$E_INVALID" "$EVENT"
  452. exit $E_INVALID
  453. fi
  454. }
  455. # Email address
  456. validate_format_email() {
  457. local_part=$(echo $1 | cut -s -f1 -d\@)
  458. remote_host=$(echo $1 | cut -s -f2 -d\@)
  459. mx_failed=1
  460. if [ ! -z "$remote_host" ] && [ ! -z "$local_part" ]; then
  461. /usr/bin/host -t mx "$remote_host" &> /dev/null
  462. mx_failed="$?"
  463. fi
  464. if [ "$mx_failed" -eq 1 ]; then
  465. echo "Error: email $1 is not valid"
  466. log_event "$E_INVALID" "$EVENT"
  467. exit $E_INVALID
  468. fi
  469. }
  470. # Username
  471. validate_format_username() {
  472. if ! [[ "$1" =~ ^[0-Z]+(\.[0-Z]+)?$ ]] || [[ "${#1}" -gt 28 ]]; then
  473. echo "Error: usernmae $1 is not valid"
  474. log_event "$E_INVALID" "$EVENT"
  475. exit $E_INVALID
  476. fi
  477. }
  478. # Domain
  479. validate_format_domain() {
  480. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|<|>|?|_|/|\|\"|'|;|%| ]"
  481. dpart1=$(echo $1 | cut -f 1 -d .)
  482. if [[ "$1" =~ $exclude ]] || [ -z "$dpart1" ]; then
  483. echo "Error: domain $1 is not valid"
  484. log_event "$E_INVALID" "$EVENT"
  485. exit $E_INVALID
  486. fi
  487. }
  488. # Database
  489. validate_format_database() {
  490. exclude="[!|@|#|$|^|&|*|(|)|+|=|{|}|:|,|.|<|>|?|/|\|\"|'|;|%| ]"
  491. if [[ "$1" =~ $exclude ]] || [ 17 -le ${#1} ]; then
  492. echo "Error: database $1 is not valid"
  493. log_event "$E_INVALID" "$EVENT"
  494. exit $E_INVALID
  495. fi
  496. }
  497. # DNS type
  498. validate_format_dns_type() {
  499. known_dnstype='A,AAAA,NS,CNAME,MX,TXT,SRV,DNSKEY,KEY,IPSECKEY,PTR,SPF'
  500. if [ -z "$(echo $known_dnstype | grep -w $1)" ]; then
  501. echo "Error: dnstype $1 is not valid"
  502. log_event "$E_INVALID" "$EVENT"
  503. exit $E_INVALID
  504. fi
  505. }
  506. # DKIM key size
  507. validate_format_key_size() {
  508. known_size='128,256,512,768,1024,2048'
  509. if [ -z "$(echo $known_size | grep -w $1)" ]; then
  510. echo "Error: key_size $1 is not valid"
  511. log_event "$E_INVALID" "$EVENT"
  512. exit $E_INVALID
  513. fi
  514. }
  515. # Minute / Hour / Day / Month / Day of Week
  516. validate_format_mhdmw() {
  517. limit=60
  518. check_format=''
  519. if [ "$2" = 'day' ]; then
  520. limit=31
  521. fi
  522. if [ "$2" = 'month' ]; then
  523. limit=12
  524. fi
  525. if [ "$2" = 'wday' ]; then
  526. limit=7
  527. fi
  528. if [ "$1" = '*' ]; then
  529. check_format='ok'
  530. fi
  531. if [[ "$1" =~ ^[\*]+[/]+[0-9] ]]; then
  532. if [ "$(echo $1 |cut -f 2 -d /)" -lt $limit ]; then
  533. check_format='ok'
  534. fi
  535. fi
  536. if [[ "$1" =~ ^[0-9]+$ ]] && [ "$1" -lt $limit ]; then
  537. check_format='ok'
  538. fi
  539. if [ "$check_format" != 'ok' ]; then
  540. echo "Error: $2 $1 is not valid"
  541. log_event "$E_INVALID" "$EVENT"
  542. exit $E_INVALID
  543. fi
  544. }
  545. # Nginx static extention or DNS record
  546. validate_format_extentions() {
  547. exclude="[!|@|#|$|^|&|(|)|+|=|{|}|:|<|>|?|/|\|\"|'|;|%| ]"
  548. if [[ "$1" =~ $exclude ]] || [ 200 -le ${#1} ]; then
  549. echo "Error: extention $1 is not valid"
  550. log_event "$E_INVALID" "$EVENT"
  551. exit $E_INVALID
  552. fi
  553. }
  554. # DNS record value
  555. validate_format_dvalue() {
  556. record_types="$(echo A,AAAA,NS,CNAME | grep -w "$rtype")"
  557. if [[ "$1" =~ [\ ] ]] && [ ! -z "$record_types" ]; then
  558. echo "Error: dvalue $1 is not valid"
  559. log_event "$E_INVALID" "$EVENT"
  560. exit $E_INVALID
  561. fi
  562. if [ "$rtype" = 'A' ]; then
  563. validate_format_ip "$1"
  564. fi
  565. if [ "$rtype" = 'NS' ]; then
  566. validate_format_domain "$1"
  567. fi
  568. }
  569. # Date
  570. validate_format_date() {
  571. if ! [[ "$1" =~ ^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$ ]]; then
  572. echo "Error: date $1 is not valid"
  573. log_event "$E_INVALID" "$EVENT"
  574. exit $E_INVALID
  575. fi
  576. }
  577. # Format validation controller
  578. validate_format(){
  579. for arg_name in $*; do
  580. eval arg=\$$argument_name
  581. case $var in
  582. account) validate_format_username "$arg" ;;
  583. antispam) validate_format_boolean "$arg" ;;
  584. antivirus) validate_format_boolean "$arg" ;;
  585. auth_pass) validate_format_password "$arg" ;;
  586. auth_user) validate_format_username "$arg" ;;
  587. backup) validate_format_date "$arg" ;;
  588. database) validate_format_database "$arg" ;;
  589. day) validate_format_mhdmw "$arg" $arg_name ;;
  590. db_password) validate_format_password "$arg" ;;
  591. db_user) validate_format_database "$arg" ;;
  592. dkim) validate_format_boolean "$arg" ;;
  593. dkim_size) validate_format_key_size "$arg" ;;
  594. domain) validate_format_domain "$arg" ;;
  595. dom_alias) validate_format_domain "$arg" ;;
  596. dvalue) validate_format_dvalue "$arg";;
  597. email) validate_format_email "$arg" ;;
  598. encoding) validate_format_username "$arg" ;;
  599. exp) validate_format_date "$arg" ;;
  600. extentions) validate_format_extentions "$arg" ;;
  601. fname) validate_format_username "$arg" ;;
  602. host) validate_format_username "$arg" ;;
  603. hour) validate_format_mhdmw "$arg" $arg_name ;;
  604. id) validate_format_int "$arg" ;;
  605. interface) validate_format_interface "$arg" ;;
  606. ip) validate_format_ip "$arg" ;;
  607. ip_name) validate_format_domain "$arg" ;;
  608. ip_status) validate_format_ip_status "$arg" ;;
  609. job) validate_format_int "$arg" ;;
  610. key) validate_format_username "$arg" ;;
  611. lname) validate_format_username "$arg" ;;
  612. malias) validate_format_username "$arg" ;;
  613. mask) validate_format_ip "$arg" ;;
  614. max_db) validate_format_int "$arg" ;;
  615. min) validate_format_mhdmw "$arg" $arg_name ;;
  616. month) validate_format_mhdmw "$arg" $arg_name ;;
  617. ns1) validate_format_domain "$arg" ;;
  618. ns2) validate_format_domain "$arg" ;;
  619. ns3) validate_format_domain "$arg" ;;
  620. ns4) validate_format_domain "$arg" ;;
  621. ns5) validate_format_domain "$arg" ;;
  622. ns6) validate_format_domain "$arg" ;;
  623. ns7) validate_format_domain "$arg" ;;
  624. ns8) validate_format_domain "$arg" ;;
  625. package) validate_format_username "$arg" ;;
  626. password) validate_format_password "$arg" ;;
  627. port) validate_format_int "$arg" ;;
  628. quota) validate_format_int "$arg" ;;
  629. restart) validate_format_boolean "$arg" ;;
  630. record) validate_format_extentions "$arg" ;;
  631. rtype) validate_format_dns_type "$arg" ;;
  632. shell) validate_format_shell "$arg" ;;
  633. soa) validate_format_domain "$arg" ;;
  634. template) validate_format_username "$arg" ;;
  635. ttl) validate_format_int "$arg" ;;
  636. url) validate_format_url "$arg" ;;
  637. user) validate_format_username "$arg" ;;
  638. wday) validate_format_mhdmw "$arg" $arg_name ;;
  639. esac
  640. done
  641. }