domain.sh 12 KB

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