domain.sh 16 KB

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