domain.sh 13 KB

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