domain.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. # Web template check
  2. is_apache_template_valid() {
  3. t="$WEBTPL/apache_$template.tpl"
  4. s="$WEBTPL/apache_$template.stpl"
  5. if [ ! -e $t ] || [ ! -e $s ]; then
  6. template='default'
  7. t="$WEBTPL/apache_$template.tpl"
  8. s="$WEBTPL/apache_$template.stpl"
  9. if [ ! -e $t ] || [ ! -e $s ]; then
  10. echo "Error: template $template not found"
  11. log_event "$E_NOTEXIST" "$EVENT"
  12. exit $E_NOTEXIST
  13. fi
  14. fi
  15. }
  16. # Nginx template check
  17. is_nginx_template_valid() {
  18. t="$WEBTPL/nginx_$template.tpl"
  19. s="$WEBTPL/nginx_$template.stpl"
  20. if [ ! -e $t ] || [ ! -e $s ]; then
  21. template='default'
  22. t="$WEBTPL/nginx_$template.tpl"
  23. s="$WEBTPL/nginx_$template.stpl"
  24. if [ ! -e $t ] || [ ! -e $s ]; then
  25. echo "Error: nginx $template not found"
  26. log_event "$E_NOTEXIST" "$EVENT"
  27. exit $E_NOTEXIST
  28. fi
  29. fi
  30. }
  31. # DNS template check
  32. is_dns_template_valid() {
  33. tpl="$DNSTPL/$template.tpl"
  34. if [ ! -e $tpl ]; then
  35. template='default'
  36. tpl="$DNSTPL/$template.tpl"
  37. if [ ! -e $tpl ]; then
  38. echo "Error: template not found"
  39. log_event "$E_NOTEXIST" "$EVENT"
  40. exit $E_NOTEXIST
  41. fi
  42. fi
  43. }
  44. # Checking domain existance
  45. is_domain_new() {
  46. type="$1"
  47. dom=${2-$domain}
  48. web="$(grep -w $dom $VESTA/data/users/*/web.conf)"
  49. dns="$(grep DOMAIN='$dom' $VESTA/data/users/*/dns.conf)"
  50. mail="$(grep DOMAIN='$dom' $VESTA/data/users/*/mail.conf)"
  51. if [ -n "$web" ] || [ -n "$dns" ] || [ -n "$mail" ]; then
  52. if [ ! -z "$(grep -w $dom $USER_DATA/*.conf)" ]; then
  53. c1=$(grep "'$dom'" $USER_DATA/$type.conf)
  54. c2=$(grep "'$dom," $USER_DATA/$type.conf)
  55. c3=$(grep ",$dom," $USER_DATA/$type.conf)
  56. c4=$(grep ",$dom'" $USER_DATA/$type.conf)
  57. if [ -n "$c1" ] || [ -n "$c2" ] || [ -n "$c3" ] || [ -n "$c4" ]
  58. then
  59. echo "Error: domain $dom exist"
  60. log_event "$E_EXISTS" "$EVENT"
  61. exit $E_EXISTS
  62. fi
  63. else
  64. echo "Error: domain $dom exist"
  65. log_event "$E_EXISTS" "$EVENT"
  66. exit $E_EXISTS
  67. fi
  68. fi
  69. }
  70. # Checking mail account existance
  71. is_mail_new() {
  72. check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
  73. if [ ! -z "$check_acc" ]; then
  74. echo "Error: mail account $1 exist"
  75. log_event "$E_EXISTS" "$EVENT"
  76. exit
  77. fi
  78. check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf )
  79. check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
  80. if [ ! -z "$check_als" ]; then
  81. echo "Error: mail alias $1 exist"
  82. log_event "$E_EXISTS" "$EVENT"
  83. exit
  84. fi
  85. }
  86. # Update domain zone
  87. update_domain_zone() {
  88. conf="$HOMEDIR/$user/conf/dns/$domain.db"
  89. line=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
  90. fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
  91. if [ -e $conf ]; then
  92. zn_serial=$(head $conf|grep 'SOA' -A1|tail -n 1|sed -e "s/ //g")
  93. s_date=$(echo ${zn_serial:0:8})
  94. c_date=$(date +'%Y%m%d')
  95. if [ "$s_date" == "$c_date" ]; then
  96. cur_value=$(echo ${zn_serial:8} )
  97. new_value=$(expr $cur_value + 1 )
  98. len_value=$(expr length $new_value)
  99. if [ 1 -eq "$len_value" ]; then
  100. new_value='0'$new_value
  101. fi
  102. serial="$c_date""$new_value"
  103. else
  104. serial="$(date +'%Y%m%d01')"
  105. fi
  106. else
  107. serial="$(date +'%Y%m%d01')"
  108. fi
  109. eval $line
  110. SOA=$(idn --quiet -a -t "$SOA")
  111. echo "\$TTL $TTL
  112. @ IN SOA $SOA. root.$domain_idn. (
  113. $serial
  114. 7200
  115. 3600
  116. 1209600
  117. 180 )
  118. " > $conf
  119. while read line ; do
  120. IFS=$'\n'
  121. for key in $(echo $line|sed -e "s/' /'\n/g"); do
  122. eval ${key%%=*}="${key#*=}"
  123. done
  124. RECORD=$(idn --quiet -a -t "$RECORD")
  125. if [ "$SUSPENDED" != 'yes' ]; then
  126. eval echo -e "\"$fields\""|sed -e "s/%quote%/'/g" >> $conf
  127. fi
  128. done < $USER_DATA/dns/$domain.conf
  129. }
  130. # Get next DNS record ID
  131. get_next_dnsrecord(){
  132. if [ -z "$id" ]; then
  133. curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' |\
  134. sort -n|tail -n1)
  135. id="$((curr_str +1))"
  136. fi
  137. }
  138. # Sort DNS records
  139. sort_dns_records() {
  140. conf="$USER_DATA/dns/$domain.conf"
  141. cat $conf |sort -n -k 2 -t \' >$conf.tmp
  142. mv -f $conf.tmp $conf
  143. }
  144. # Add web config
  145. add_web_config() {
  146. cat $tpl_file | \
  147. sed -e "s/%ip%/$ip/g" \
  148. -e "s/%web_port%/$WEB_PORT/g" \
  149. -e "s/%web_ssl_port%/$WEB_SSL_PORT/g" \
  150. -e "s/%proxy_port%/$PROXY_PORT/g" \
  151. -e "s/%proxy_ssl_port%/$PROXY_SSL_PORT/g" \
  152. -e "s/%domain_idn%/$domain_idn/g" \
  153. -e "s/%domain%/$domain/g" \
  154. -e "s/%user%/$user/g" \
  155. -e "s/%group%/$group/g" \
  156. -e "s/%home%/${HOMEDIR////\/}/g" \
  157. -e "s/%docroot%/${docroot////\/}/g" \
  158. -e "s/%sdocroot%/${sdocroot////\/}/g" \
  159. -e "s/%email%/$email/g" \
  160. -e "s/%alias_string%/$alias_string/g" \
  161. -e "s/%alias_idn%/${aliases_idn//,/ }/g" \
  162. -e "s/%alias%/${aliases//,/ }/g" \
  163. -e "s/%ssl_crt%/${ssl_crt////\/}/g" \
  164. -e "s/%ssl_key%/${ssl_key////\/}/g" \
  165. -e "s/%ssl_pem%/${ssl_pem////\/}/g" \
  166. -e "s/%ssl_ca_str%/${ssl_ca_str////\/}/g" \
  167. -e "s/%ssl_ca%/${ssl_ca////\/}/g" \
  168. -e "s/%nginx_extentions%/${NGINX_EXT//,/|}/g" \
  169. -e "s/%elog%/$elog/g" \
  170. -e "s/%cgi%/$cgi/g" \
  171. -e "s/%cgi_option%/$cgi_option/g" \
  172. >> $conf
  173. }
  174. # Get config top and bottom line numbers
  175. get_web_config_brds() {
  176. serv_line=$(grep -ni 'Name %domain_idn%' "$tpl_file" |cut -f 1 -d :)
  177. if [ -z "$serv_line" ]; then
  178. log_event "$E_PARSING" "$EVENT"
  179. return $E_PARSING
  180. fi
  181. last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
  182. bfr_line=$((serv_line - 1))
  183. aftr_line=$((last_line - serv_line - 1))
  184. str=$(grep -ni "Name $domain_idn" $conf | cut -f 1 -d :)
  185. top_line=$((str - serv_line + 1))
  186. bottom_line=$((top_line + last_line -1))
  187. multi=$(sed -n "$top_line,$bottom_line p" $conf |grep ServerAlias |wc -l)
  188. if [ "$multi" -ge 2 ]; then
  189. bottom_line=$((bottom_line + multi -1))
  190. fi
  191. }
  192. # Change web config
  193. change_web_config() {
  194. get_web_config_brds || exit $?
  195. vhost=$(grep -A $aftr_line -B $bfr_line -ni "Name $domain_idn" $conf)
  196. str=$(echo "$vhost" | grep -F "$search_phrase" | head -n 1)
  197. str_numb=$(echo "$str" | sed -e "s/-/=/" | cut -f 1 -d '=')
  198. str_cont=$(echo "$str" | sed -e "s/-/=/" | cut -f 2 -d '=')
  199. str_repl=$(echo "$str_repl" | sed \
  200. -e 's/\\/\\\\/g' \
  201. -e 's/&/\\&/g' \
  202. -e 's/\//\\\//g')
  203. if [ ! -z "$str" ]; then
  204. sed -i "$str_numb s/.*/$str_repl/" $conf
  205. fi
  206. }
  207. # Replace web config
  208. replace_web_config() {
  209. get_web_config_brds || exit $?
  210. clean_new=$(echo "$new" | sed \
  211. -e 's/\\/\\\\/g' \
  212. -e 's/&/\\&/g' \
  213. -e 's/\//\\\//g')
  214. clean_old=$(echo "$old" | sed \
  215. -e 's/\\/\\\\/g' \
  216. -e 's/&/\\&/g' \
  217. -e 's/\//\\\//g')
  218. sed -i "$top_line,$bottom_line s/$clean_old/$clean_new/" $conf
  219. }
  220. # Get domain variables
  221. get_domain_values() {
  222. for line in $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf); do
  223. eval $line
  224. done
  225. }
  226. # SSL certificate verification
  227. is_web_domain_cert_valid() {
  228. if [ ! -e "$ssl_dir/$domain.crt" ] || [ ! -e "$ssl_dir/$domain.key" ]; then
  229. echo "Error: $ssl_dir/$domain.[crt|key] not found"
  230. log_event "$E_NOTEXIST" "$EVENT"
  231. exit $E_NOTEXIST
  232. fi
  233. if [ ! -e "$ssl_dir/$domain.ca" ]; then
  234. crt=$(openssl verify $ssl_dir/$domain.crt 2>/dev/null |grep 'OK')
  235. else
  236. crt=$(openssl verify -untrusted $ssl_dir/$domain.ca \
  237. $ssl_dir/$domain.crt 2>/dev/null |grep 'OK')
  238. fi
  239. if [ -z "$crt" ]; then
  240. echo "Error: certificate is not valid"
  241. log_event "$E_INVALID" "$EVENT"
  242. exit $E_INVALID
  243. fi
  244. openssl rsa -in "$ssl_dir/$domain.key" -check &>/dev/null
  245. if [ "$?" -ne 0 ]; then
  246. echo "Error: ssl key is not valid"
  247. log_event "$E_INVALID" "$EVENT"
  248. exit $E_INVALID
  249. fi
  250. if [ -e "$ssl_dir/$domain.ca" ]; then
  251. ca=$(openssl verify $ssl_dir/$domain.ca 2>/dev/null |grep 'OK')
  252. if [ -z "$ca" ]; then
  253. echo "Error: ssl certificate authority is not valid"
  254. log_event "$E_INVALID" "$EVENT"
  255. exit $E_INVALID
  256. fi
  257. fi
  258. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  259. -key $ssl_dir/$domain.key &
  260. pid=$!
  261. sleep 0.5
  262. disown &> /dev/null
  263. kill $pid &> /dev/null
  264. if [ "$?" -ne '0' ]; then
  265. echo "Error: ssl certificate key pair is not valid"
  266. log_event "$E_INVALID" "$EVENT"
  267. exit $E_INVALID
  268. fi
  269. }
  270. # Delete web configuartion
  271. del_web_config() {
  272. get_web_config_brds || exit $?
  273. sed -i "$top_line,$bottom_line d" $conf
  274. }
  275. # Add ip virtual hosting support
  276. namehost_ip_support() {
  277. if [ "$WEB_SYSTEM" = 'apache' ]; then
  278. conf_line=$(grep -n "NameVirtual" $conf|tail -n 1|cut -f 1 -d ':')
  279. if [ ! -z "$conf_line" ]; then
  280. conf_ins=$((conf_line + 1))
  281. else
  282. conf_ins='1'
  283. fi
  284. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  285. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_SSL_PORT" $conf
  286. sed -i "$conf_ins i Listen $ip:$WEB_SSL_PORT" $conf
  287. fi
  288. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_PORT" $conf
  289. sed -i "$conf_ins i Listen $ip:$WEB_PORT" $conf
  290. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  291. cat $WEBTPL/ngingx.ip.tpl | sed -e "s/%ip%/$ip/g" \
  292. -e "s/%web_port%/$WEB_PORT/g" \
  293. -e "s/%proxy_port%/$PROXY_PORT/g" >>$nconf
  294. ips=$(grep 'MEFaccept ' $rconf |grep -v '#'| head -n1)
  295. sed -i "s/$ips/$ips $ip/g" $rconf
  296. fi
  297. web_restart='yes'
  298. fi
  299. }
  300. # Disable virtual ip hosting support
  301. namehost_ip_disable() {
  302. if [ "$WEB_SYSTEM" = 'apache' ]; then
  303. sed -i "/NameVirtualHost $ip:/d" $conf
  304. sed -i "/Listen $ip:/d" $conf
  305. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  306. tpl_ln=$(wc -l $WEBTPL/ngingx.ip.tpl | cut -f 1 -d ' ')
  307. ip_line=$(grep -n "%ip%" $WEBTPL/ngingx.ip.tpl |head -n1 |\
  308. cut -f 1 -d :)
  309. conf_line=$(grep -n -w $ip $nconf|head -n1|cut -f 1 -d :)
  310. if [ -z "$tpl_ln" ] || [ -z "$ip_line" ] || [ -z "$conf_line" ]
  311. then
  312. echo "Error: nginx config paring error"
  313. log_event "$E_PARSING" "$EVENT"
  314. exit $E_PARSING
  315. fi
  316. up_line=$((ip_line - 1))
  317. first_line=$((conf_line - up_line))
  318. last_line=$((conf_line - ip_line + tpl_ln))
  319. if [ -z "$first_line" ] || [ -z "$last_line" ]; then
  320. echo "Error: nginx config paring error"
  321. log_event "$E_PARSING" "$EVENT"
  322. exit $E_PARSING
  323. fi
  324. sed -i "$first_line,$last_line d" $nconf
  325. ips=$(grep 'RPAFproxy_ips' $rconf)
  326. new_ips=$(echo "$ips"|sed -e "s/$ip//")
  327. sed -i "s/$ips/$new_ips/g" $rconf
  328. fi
  329. web_restart='yes'
  330. fi
  331. }
  332. # Update web domain values
  333. upd_web_domain_values() {
  334. ip=$IP
  335. group="$user"
  336. email="$user@$domain"
  337. docroot="$HOMEDIR/$user/web/$domain/public_html"
  338. sdocroot=$docroot
  339. if [ "$SSL_HOME" = 'single' ]; then
  340. sdocroot="$HOMEDIR/$user/web/$domain/public_shtml" ;
  341. fi
  342. i=1
  343. j=1
  344. OLD_IFS="$IFS"
  345. IFS=','
  346. server_alias=''
  347. alias_string=''
  348. aliases_idn=''
  349. for dalias in $ALIAS; do
  350. dalias=$(idn -t --quiet -a $dalias)
  351. check_8k="$server_alias $dalias"
  352. if [ "${#check_8k}" -ge '8100' ]; then
  353. if [ "$j" -eq 1 ]; then
  354. alias_string="ServerAlias $server_alias"
  355. else
  356. alias_string="$alias_string\n ServerAlias $server_alias"
  357. fi
  358. j=2
  359. server_alias=''
  360. fi
  361. if [ "$i" -eq 1 ]; then
  362. aliases_idn="$dalias"
  363. server_alias="$dalias"
  364. alias_string="ServerAlias $server_alias"
  365. else
  366. aliases_idn="$aliases_idn,$dalias"
  367. server_alias="$server_alias $dalias"
  368. fi
  369. i=2
  370. done
  371. if [ $j -gt 1 ]; then
  372. alias_string="$alias_string\n ServerAlias $server_alias"
  373. else
  374. alias_string="ServerAlias $server_alias"
  375. fi
  376. IFS=$OLD_IFS
  377. if [ "$ELOG" = 'no' ]; then
  378. elog='#'
  379. else
  380. elog=''
  381. fi
  382. if [ "$CGI" != 'yes' ]; then
  383. cgi='#'
  384. cgi_option='-ExecCGI'
  385. else
  386. cgi=''
  387. cgi_option='+ExecCGI'
  388. fi
  389. ssl_crt="$HOMEDIR/$user/conf/web/ssl.$domain.crt"
  390. ssl_key="$HOMEDIR/$user/conf/web/ssl.$domain.key"
  391. ssl_pem="$HOMEDIR/$user/conf/web/ssl.$domain.pem"
  392. ssl_ca="$HOMEDIR/$user/conf/web/ssl.$domain.ca"
  393. if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
  394. ssl_ca_str='#'
  395. fi
  396. if [ "$SUSPENDED" = 'yes' ]; then
  397. docroot="$VESTA/data/templates/web/suspend"
  398. sdocroot="$VESTA/data/templates/web/suspend"
  399. fi
  400. }