domain.sh 13 KB

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