domain.sh 13 KB

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