domain.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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_$template.tpl"
  16. d="$WEBTPL/ngingx_$template.descr"
  17. s="$WEBTPL/ngingx_$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_port%/$PROXY_PORT/g" \
  136. -e "s/%proxy_ssl_port%/$PROXY_SSL_PORT/g" \
  137. -e "s/%domain_idn%/$domain_idn/g" \
  138. -e "s/%domain%/$domain/g" \
  139. -e "s/%user%/$user/g" \
  140. -e "s/%group%/$group/g" \
  141. -e "s/%home%/${HOMEDIR////\/}/g" \
  142. -e "s/%docroot%/${docroot////\/}/g" \
  143. -e "s/%sdocroot%/${sdocroot////\/}/g" \
  144. -e "s/%email%/$email/g" \
  145. -e "s/%alias_string%/$alias_string/g" \
  146. -e "s/%alias_idn%/${aliases_idn//,/ }/g" \
  147. -e "s/%alias%/${aliases//,/ }/g" \
  148. -e "s/%ssl_crt%/${ssl_crt////\/}/g" \
  149. -e "s/%ssl_key%/${ssl_key////\/}/g" \
  150. -e "s/%ssl_pem%/${ssl_pem////\/}/g" \
  151. -e "s/%ssl_ca_str%/${ssl_ca_str////\/}/g" \
  152. -e "s/%ssl_ca%/${ssl_ca////\/}/g" \
  153. -e "s/%nginx_extentions%/${NGINX_EXT//,/|}/g" \
  154. -e "s/%elog%/$elog/g" \
  155. -e "s/%cgi%/$cgi/g" \
  156. -e "s/%cgi_option%/$cgi_option/g" \
  157. >> $conf
  158. }
  159. # Get config top and bottom line numbers
  160. get_web_config_brds() {
  161. serv_line=$(grep -ni 'Name %domain_idn%' "$tpl_file" |cut -f 1 -d :)
  162. if [ -z "$serv_line" ]; then
  163. log_event "$E_PARSING" "$EVENT"
  164. return $E_PARSING
  165. fi
  166. last_line=$(wc -l $tpl_file|cut -f 1 -d ' ')
  167. bfr_line=$((serv_line - 1))
  168. aftr_line=$((last_line - serv_line - 1))
  169. str=$(grep -ni "Name $domain_idn" $conf | cut -f 1 -d :)
  170. top_line=$((str - serv_line + 1))
  171. bottom_line=$((top_line + last_line -1))
  172. multi=$(sed -n "$top_line,$bottom_line p" $conf |grep ServerAlias |wc -l)
  173. if [ "$multi" -ge 2 ]; then
  174. bottom_line=$((bottom_line + multi -1))
  175. fi
  176. }
  177. # Change web config
  178. change_web_config() {
  179. get_web_config_brds || exit $?
  180. vhost=$(grep -A $aftr_line -B $bfr_line -ni "Name $domain_idn" $conf)
  181. str=$(echo "$vhost" | grep -F "$search_phrase" | head -n 1)
  182. str_numb=$(echo "$str" | sed -e "s/-/=/" | cut -f 1 -d '=')
  183. str_cont=$(echo "$str" | sed -e "s/-/=/" | cut -f 2 -d '=')
  184. str_repl=$(echo "$str_repl" | sed \
  185. -e 's/\\/\\\\/g' \
  186. -e 's/&/\\&/g' \
  187. -e 's/\//\\\//g')
  188. if [ ! -z "$str" ]; then
  189. sed -i "$str_numb s/.*/$str_repl/" $conf
  190. fi
  191. }
  192. # Replace web config
  193. replace_web_config() {
  194. get_web_config_brds || exit $?
  195. clean_new=$(echo "$new" | sed \
  196. -e 's/\\/\\\\/g' \
  197. -e 's/&/\\&/g' \
  198. -e 's/\//\\\//g')
  199. clean_old=$(echo "$old" | sed \
  200. -e 's/\\/\\\\/g' \
  201. -e 's/&/\\&/g' \
  202. -e 's/\//\\\//g')
  203. sed -i "$top_line,$bottom_line s/$clean_old/$clean_new/" $conf
  204. }
  205. # Get domain variables
  206. get_domain_values() {
  207. for line in $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf); do
  208. eval $line
  209. done
  210. }
  211. # SSL certificate verification
  212. is_web_domain_cert_valid() {
  213. if [ ! -e "$ssl_dir/$domain.crt" ] || [ ! -e "$ssl_dir/$domain.key" ]; then
  214. echo "Error: $ssl_dir/$domain.[crt|key] not found"
  215. log_event "$E_NOTEXIST" "$EVENT"
  216. exit $E_NOTEXIST
  217. fi
  218. if [ ! -e "$ssl_dir/$domain.ca" ]; then
  219. crt=$(openssl verify $ssl_dir/$domain.crt 2>/dev/null |grep 'OK')
  220. else
  221. crt=$(openssl verify -untrusted $ssl_dir/$domain.ca \
  222. $ssl_dir/$domain.crt 2>/dev/null |grep 'OK')
  223. fi
  224. if [ -z "$crt" ]; then
  225. echo "Error: certificate is not valid"
  226. log_event "$E_INVALID" "$EVENT"
  227. exit $E_INVALID
  228. fi
  229. openssl rsa -in "$ssl_dir/$domain.key" -check &>/dev/null
  230. if [ "$?" -ne 0 ]; then
  231. echo "Error: ssl key is not valid"
  232. log_event "$E_INVALID" "$EVENT"
  233. exit $E_INVALID
  234. fi
  235. if [ -e "$ssl_dir/$domain.ca" ]; then
  236. ca=$(openssl verify $ssl_dir/$domain.ca 2>/dev/null |grep 'OK')
  237. if [ -z "$ca" ]; then
  238. echo "Error: ssl certificate authority is not valid"
  239. log_event "$E_INVALID" "$EVENT"
  240. exit $E_INVALID
  241. fi
  242. fi
  243. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  244. -key $ssl_dir/$domain.key &
  245. pid=$!
  246. sleep 0.5
  247. disown &> /dev/null
  248. kill $pid &> /dev/null
  249. if [ "$?" -ne '0' ]; then
  250. echo "Error: ssl certificate key pair is not valid"
  251. log_event "$E_INVALID" "$EVENT"
  252. exit $E_INVALID
  253. fi
  254. }
  255. # Delete web configuartion
  256. del_web_config() {
  257. get_web_config_brds || exit $?
  258. sed -i "$top_line,$bottom_line d" $conf
  259. }
  260. # Add ip virtual hosting support
  261. namehost_ip_support() {
  262. if [ "$WEB_SYSTEM" = 'apache' ]; then
  263. conf_line=$(grep -n "NameVirtual" $conf|tail -n 1|cut -f 1 -d ':')
  264. if [ ! -z "$conf_line" ]; then
  265. conf_ins=$((conf_line + 1))
  266. else
  267. conf_ins='1'
  268. fi
  269. if [ "$WEB_SSL" = 'mod_ssl' ]; then
  270. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_SSL_PORT" $conf
  271. sed -i "$conf_ins i Listen $ip:$WEB_SSL_PORT" $conf
  272. fi
  273. sed -i "$conf_ins i NameVirtualHost $ip:$WEB_PORT" $conf
  274. sed -i "$conf_ins i Listen $ip:$WEB_PORT" $conf
  275. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  276. cat $WEBTPL/ngingx.ip.tpl | sed -e "s/%ip%/$ip/g" \
  277. -e "s/%web_port%/$WEB_PORT/g" \
  278. -e "s/%proxy_port%/$PROXY_PORT/g" >>$nconf
  279. ips=$(grep 'RPAFproxy_ips' $rconf)
  280. sed -i "s/$ips/$ips $ip/g" $rconf
  281. fi
  282. web_restart='yes'
  283. fi
  284. }
  285. # Disable virtual ip hosting support
  286. namehost_ip_disable() {
  287. if [ "$WEB_SYSTEM" = 'apache' ]; then
  288. sed -i "/NameVirtualHost $ip:/d" $conf
  289. sed -i "/Listen $ip:/d" $conf
  290. if [ "$PROXY_SYSTEM" = 'nginx' ]; then
  291. tpl_ln=$(wc -l $WEBTPL/ngingx.ip.tpl | cut -f 1 -d ' ')
  292. ip_line=$(grep -n "%ip%" $WEBTPL/ngingx.ip.tpl |head -n1 |\
  293. cut -f 1 -d :)
  294. conf_line=$(grep -n -w $ip $nconf|head -n1|cut -f 1 -d :)
  295. if [ -z "$tpl_ln" ] || [ -z "$ip_line" ] || [ -z "$conf_line" ]
  296. then
  297. echo "Error: nginx config paring error"
  298. log_event "$E_PARSING" "$EVENT"
  299. exit $E_PARSING
  300. fi
  301. up_line=$((ip_line - 1))
  302. first_line=$((conf_line - up_line))
  303. last_line=$((conf_line - ip_line + tpl_ln))
  304. if [ -z "$first_line" ] || [ -z "$last_line" ]; then
  305. echo "Error: nginx config paring error"
  306. log_event "$E_PARSING" "$EVENT"
  307. exit $E_PARSING
  308. fi
  309. sed -i "$first_line,$last_line d" $nconf
  310. ips=$(grep 'RPAFproxy_ips' $rconf)
  311. new_ips=$(echo "$ips"|sed -e "s/$ip//")
  312. sed -i "s/$ips/$new_ips/g" $rconf
  313. fi
  314. web_restart='yes'
  315. fi
  316. }
  317. # Update web domain values
  318. upd_web_domain_values() {
  319. ip=$IP
  320. group="$user"
  321. email="$user@$domain"
  322. docroot="$HOMEDIR/$user/web/$domain/public_html"
  323. if [ "$SSL_HOME" = 'single' ]; then
  324. sdocroot="$HOMEDIR/$user/web/$domain/public_shtml" ;
  325. fi
  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. ssl_crt="$HOMEDIR/$user/conf/web/ssl.$domain.crt"
  373. ssl_key="$HOMEDIR/$user/conf/web/ssl.$domain.key"
  374. ssl_pem="$HOMEDIR/$user/conf/web/ssl.$domain.pem"
  375. ssl_ca="$HOMEDIR/$user/conf/web/ssl.$domain.ca"
  376. if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
  377. ssl_ca_str='#'
  378. fi
  379. if [ "$SUSPENDED" = 'yes' ]; then
  380. docroot="$VESTA/data/templates/web/suspend"
  381. sdocroot="$VESTA/data/templates/web/suspend"
  382. fi
  383. }