domain.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. #----------------------------------------------------------#
  2. # WEB #
  3. #----------------------------------------------------------#
  4. # Web template check
  5. is_web_template_valid() {
  6. if [ ! -z "$WEB_SYSTEM" ]; then
  7. tpl="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$1.tpl"
  8. stpl="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$1.stpl"
  9. if [ ! -e "$tpl" ] || [ ! -e "$stpl" ]; then
  10. check_result $E_NOTEXIST "$1 web template doesn't exist"
  11. fi
  12. fi
  13. }
  14. # Proxy template check
  15. is_proxy_template_valid() {
  16. if [ ! -z "$PROXY_SYSTEM" ]; then
  17. tpl="$WEBTPL/$PROXY_SYSTEM/$1.tpl"
  18. stpl="$WEBTPL/$PROXY_SYSTEM/$1.stpl"
  19. if [ ! -e "$tpl" ] || [ ! -e "$stpl" ]; then
  20. check_result $E_NOTEXIST "$1 proxy template doesn't exist"
  21. fi
  22. fi
  23. }
  24. # Backend template check
  25. is_backend_template_valid() {
  26. if [ ! -z "$WEB_BACKEND" ]; then
  27. if [ ! -e "$WEBTPL/$WEB_BACKEND/$1.tpl" ]; then
  28. check_result $E_NOTEXIST "$1 backend template doesn't exist"
  29. fi
  30. fi
  31. }
  32. # Web domain existence check
  33. is_web_domain_new() {
  34. web=$(grep -F -H "DOMAIN='$1'" $HESTIA/data/users/*/web.conf)
  35. if [ ! -z "$web" ]; then
  36. if [ "$type" == 'web' ]; then
  37. check_result $E_EXISTS "Web domain $1 exist"
  38. fi
  39. web_user=$(echo "$web" |cut -f 7 -d /)
  40. if [ "$web_user" != "$user" ]; then
  41. check_result $E_EXISTS "Web domain $1 exist"
  42. fi
  43. fi
  44. }
  45. # Web alias existence check
  46. is_web_alias_new() {
  47. web_alias=$(grep -wH "$1" $HESTIA/data/users/*/web.conf)
  48. if [ ! -z "$web_alias" ]; then
  49. a1=$(echo "$web_alias" |grep -F "'$1'" |cut -f 7 -d /)
  50. if [ ! -z "$a1" ] && [ "$2" == "web" ]; then
  51. check_result $E_EXISTS "Web alias $1 exists"
  52. fi
  53. if [ ! -z "$a1" ] && [ "$a1" != "$user" ]; then
  54. check_result $E_EXISTS "Web alias $1 exists"
  55. fi
  56. a2=$(echo "$web_alias" |grep -F "'$1," |cut -f 7 -d /)
  57. if [ ! -z "$a2" ] && [ "$2" == "web" ]; then
  58. check_result $E_EXISTS "Web alias $1 exists"
  59. fi
  60. if [ ! -z "$a2" ] && [ "$a2" != "$user" ]; then
  61. check_result $E_EXISTS "Web alias $1 exists"
  62. fi
  63. a3=$(echo "$web_alias" |grep -F ",$1," |cut -f 7 -d /)
  64. if [ ! -z "$a3" ] && [ "$2" == "web" ]; then
  65. check_result $E_EXISTS "Web alias $1 exists"
  66. fi
  67. if [ ! -z "$a3" ] && [ "$a3" != "$user" ]; then
  68. check_result $E_EXISTS "Web alias $1 exists"
  69. fi
  70. a4=$(echo "$web_alias" |grep -F ",$1'" |cut -f 7 -d /)
  71. if [ ! -z "$a4" ] && [ "$2" == "web" ]; then
  72. check_result $E_EXISTS "Web alias $1 exists"
  73. fi
  74. if [ ! -z "$a4" ] && [ "$a4" != "$user" ]; then
  75. check_result $E_EXISTS "Web alias $1 exists"
  76. fi
  77. fi
  78. }
  79. # Prepare web backend
  80. prepare_web_backend() {
  81. pool=$(find -L /etc/php/ -name "$domain.conf" -exec dirname {} \;)
  82. # Check if multiple-PHP installed
  83. regex="socket-(\d+)_(\d+)"
  84. if [[ $template =~ ^socket-([0-9])\_([0-9])$ ]]
  85. then
  86. version="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
  87. pool=$(find -L /etc/php/$version -type d \( -name "pool.d" -o -name "*fpm.d" \))
  88. else
  89. if [ "$pool" == "" ]
  90. then
  91. version=`echo "<?php echo (float)phpversion();" | php`
  92. pool=$(find -L /etc/php/$version -type d \( -name "pool.d" -o -name "*fpm.d" \))
  93. fi
  94. fi
  95. if [ ! -e "$pool" ]; then
  96. check_result $E_NOTEXIST "php-fpm pool doesn't exist"
  97. fi
  98. backend_type="$domain"
  99. if [ "$WEB_BACKEND_POOL" = 'user' ]; then
  100. backend_type="$user"
  101. fi
  102. if [ -e "$pool/$backend_type.conf" ]; then
  103. backend_lsnr=$(grep "listen =" $pool/$backend_type.conf)
  104. backend_lsnr=$(echo "$backend_lsnr" |cut -f 2 -d = |sed "s/ //")
  105. if [ ! -z "$(echo $backend_lsnr |grep /)" ]; then
  106. backend_lsnr="unix:$backend_lsnr"
  107. fi
  108. fi
  109. }
  110. # Delete web backend
  111. delete_web_backend() {
  112. find -L /etc/php/ -type f -name "$backend_type.conf" -exec rm -f {} \;
  113. }
  114. # Prepare web aliases
  115. prepare_web_aliases() {
  116. i=1
  117. for tmp_alias in ${1//,/ }; do
  118. tmp_alias_idn="$tmp_alias"
  119. if [[ "$tmp_alias" = *[![:ascii:]]* ]]; then
  120. tmp_alias_idn=$(idn -t --quiet -a $tmp_alias)
  121. fi
  122. if [[ $i -eq 1 ]]; then
  123. aliases="$tmp_alias"
  124. aliases_idn="$tmp_alias_idn"
  125. alias_string="ServerAlias $tmp_alias_idn"
  126. else
  127. aliases="$aliases,$tmp_alias"
  128. aliases_idn="$aliases_idn,$tmp_alias_idn"
  129. if (( $i % 100 == 0 )); then
  130. alias_string="$alias_string\n ServerAlias $tmp_alias_idn"
  131. else
  132. alias_string="$alias_string $tmp_alias_idn"
  133. fi
  134. fi
  135. alias_number=$i
  136. ((i++))
  137. done
  138. }
  139. # Update web domain values
  140. prepare_web_domain_values() {
  141. if [[ "$domain" = *[![:ascii:]]* ]]; then
  142. domain_idn=$(idn -t --quiet -a $domain)
  143. else
  144. domain_idn=$domain
  145. fi
  146. group="$user"
  147. email="info@$domain"
  148. docroot="$HOMEDIR/$user/web/$domain/public_html"
  149. sdocroot="$docroot"
  150. if [ "$SSL_HOME" = 'single' ]; then
  151. sdocroot="$HOMEDIR/$user/web/$domain/public_shtml" ;
  152. fi
  153. if [ ! -z "$WEB_BACKEND" ]; then
  154. prepare_web_backend
  155. fi
  156. server_alias=''
  157. alias_string=''
  158. aliases_idn=''
  159. ssl_ca_str=''
  160. prepare_web_aliases $ALIAS
  161. ssl_crt="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.crt"
  162. ssl_key="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.key"
  163. ssl_pem="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.pem"
  164. ssl_ca="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.ca"
  165. if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
  166. ssl_ca_str='#'
  167. fi
  168. if [ "$SUSPENDED" = 'yes' ]; then
  169. docroot="$HESTIA/data/templates/web/suspend"
  170. sdocroot="$HESTIA/data/templates/web/suspend"
  171. fi
  172. }
  173. # Add web config
  174. add_web_config() {
  175. mkdir -p "$HOMEDIR/$user/conf/web/$domain"
  176. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  177. if [[ "$2" =~ stpl$ ]]; then
  178. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  179. fi
  180. domain_idn=$domain
  181. format_domain_idn
  182. cat $WEBTPL/$1/$WEB_BACKEND/$2 | \
  183. sed -e "s|%ip%|$local_ip|g" \
  184. -e "s|%domain%|$domain|g" \
  185. -e "s|%domain_idn%|$domain_idn|g" \
  186. -e "s|%alias%|${aliases//,/ }|g" \
  187. -e "s|%alias_idn%|${aliases_idn//,/ }|g" \
  188. -e "s|%alias_string%|$alias_string|g" \
  189. -e "s|%email%|info@$domain|g" \
  190. -e "s|%web_system%|$WEB_SYSTEM|g" \
  191. -e "s|%web_port%|$WEB_PORT|g" \
  192. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  193. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  194. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  195. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  196. -e "s|%proxy_port%|$PROXY_PORT|g" \
  197. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  198. -e "s/%proxy_extentions%/${PROXY_EXT//,/|}/g" \
  199. -e "s|%user%|$user|g" \
  200. -e "s|%group%|$user|g" \
  201. -e "s|%home%|$HOMEDIR|g" \
  202. -e "s|%docroot%|$docroot|g" \
  203. -e "s|%sdocroot%|$sdocroot|g" \
  204. -e "s|%ssl_crt%|$ssl_crt|g" \
  205. -e "s|%ssl_key%|$ssl_key|g" \
  206. -e "s|%ssl_pem%|$ssl_pem|g" \
  207. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  208. -e "s|%ssl_ca%|$ssl_ca|g" \
  209. > $conf
  210. chown root:$user $conf
  211. chmod 640 $conf
  212. if [[ "$2" =~ stpl$ ]]; then
  213. rm -f /etc/$1/conf.d/domains/$domain.ssl.conf
  214. ln -s $conf /etc/$1/conf.d/domains/$domain.ssl.conf
  215. # Clear old configurations
  216. rm -f $HOMEDIR/$user/conf/web/$domain.*
  217. rm -f $HOMEDIR/$user/conf/web/ssl.$domain.*
  218. # Rename/Move extra SSL config files
  219. for f in $(ls $HOMEDIR/$user/conf/web/*.$domain.conf* 2>/dev/null); do
  220. if [[ $f =~ .*/s(nginx|apache2)\.$domain\.conf(.*) ]]; then
  221. ServerType="${BASH_REMATCH[1]}"
  222. CustomConfigName="${BASH_REMATCH[2]}"
  223. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  224. rm -f "$f"
  225. continue
  226. fi
  227. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.ssl.conf_old$CustomConfigName"
  228. fi
  229. done
  230. else
  231. rm -f /etc/$1/conf.d/domains/$domain.conf
  232. ln -s $conf /etc/$1/conf.d/domains/$domain.conf
  233. # Clear old configurations
  234. rm -rf $HOMEDIR/$user/conf/web/$domain.*
  235. # Rename/Move extra config files
  236. for f in $(ls $HOMEDIR/$user/conf/web/*.$domain.conf* 2>/dev/null); do
  237. if [[ $f =~ .*/(nginx|apache2)\.$domain\.conf(.*) ]]; then
  238. ServerType="${BASH_REMATCH[1]}"
  239. CustomConfigName="${BASH_REMATCH[2]}"
  240. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  241. rm -f "$f"
  242. continue
  243. fi
  244. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.conf_old$CustomConfigName"
  245. elif [[ $f =~ .*/forcessl\.(nginx|apache2)\.$domain\.conf ]]; then
  246. ServerType="${BASH_REMATCH[1]}"
  247. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.forcessl.conf"
  248. fi
  249. done
  250. fi
  251. trigger="${2/.*pl/.sh}"
  252. if [ -x "$WEBTPL/$1/$WEB_BACKEND/$trigger" ]; then
  253. $WEBTPL/$1/$WEB_BACKEND/$trigger \
  254. $user $domain $local_ip $HOMEDIR \
  255. $HOMEDIR/$user/web/$domain/public_html
  256. fi
  257. }
  258. # Get config top and bottom line number
  259. get_web_config_lines() {
  260. tpl_lines=$(egrep -ni "name %domain_idn%" $1 |grep -w %domain_idn%)
  261. tpl_lines=$(echo "$tpl_lines" |cut -f 1 -d :)
  262. tpl_last_line=$(wc -l $1 |cut -f 1 -d ' ')
  263. if [ -z "$tpl_lines" ]; then
  264. check_result $E_PARSING "can't parse template $1"
  265. fi
  266. domain_idn=$domain
  267. format_domain_idn
  268. vhost_lines=$(grep -niF "name $domain_idn" $2)
  269. vhost_lines=$(echo "$vhost_lines" |egrep "$domain_idn($| |;)") #"
  270. vhost_lines=$(echo "$vhost_lines" |cut -f 1 -d :)
  271. if [ -z "$vhost_lines" ]; then
  272. check_result $E_PARSING "can't parse config $2"
  273. fi
  274. top_line=$((vhost_lines + 1 - tpl_lines))
  275. bottom_line=$((top_line - 1 + tpl_last_line))
  276. multi=$(sed -n "$top_line,$bottom_line p" $2 |grep ServerAlias |wc -l)
  277. if [ "$multi" -ge 2 ]; then
  278. bottom_line=$((bottom_line + multi -1))
  279. fi
  280. }
  281. # Replace web config
  282. replace_web_config() {
  283. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  284. if [[ "$2" =~ stpl$ ]]; then
  285. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  286. fi
  287. if [ -e "$conf" ]; then
  288. sed -i "s|$old|$new|g" $conf
  289. fi
  290. }
  291. # Delete web configuration
  292. del_web_config() {
  293. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  294. local confname="$domain.conf"
  295. if [[ "$2" =~ stpl$ ]]; then
  296. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  297. confname="$domain.ssl.conf"
  298. fi
  299. # Clean up legacy configuration files
  300. if [ ! -e "$conf" ]; then
  301. local legacyconf="$HOMEDIR/$user/conf/web/$1.conf"
  302. if [[ "$2" =~ stpl$ ]]; then
  303. legacyconf="$HOMEDIR/$user/conf/web/s$1.conf"
  304. fi
  305. rm -f $legacyconf
  306. # Remove old global includes file
  307. rm -f /etc/$1/conf.d/hestia.conf
  308. fi
  309. # Remove domain configuration files and clean up symbolic links
  310. rm -f "$conf"
  311. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" = "$1" ]; then
  312. rm -f "/etc/$WEB_SYSTEM/conf.d/domains/$confname"
  313. fi
  314. if [ ! -z "$PROXY_SYSTEM" ] && [ "$PROXY_SYSTEM" = "$1" ]; then
  315. rm -f "/etc/$PROXY_SYSTEM/conf.d/domains/$confname"
  316. fi
  317. }
  318. # SSL certificate verification
  319. is_web_domain_cert_valid() {
  320. if [ ! -e "$ssl_dir/$domain.crt" ]; then
  321. check_result $E_NOTEXIST "$ssl_dir/$domain.crt not found"
  322. fi
  323. if [ ! -e "$ssl_dir/$domain.key" ]; then
  324. check_result $E_NOTEXIST "$ssl_dir/$domain.key not found"
  325. fi
  326. crt_vrf=$(openssl verify $ssl_dir/$domain.crt 2>&1)
  327. if [ ! -z "$(echo $crt_vrf |grep 'unable to load')" ]; then
  328. check_result $E_INVALID "SSL Certificate is not valid"
  329. fi
  330. if [ ! -z "$(echo $crt_vrf |grep 'unable to get local issuer')" ]; then
  331. if [ ! -e "$ssl_dir/$domain.ca" ]; then
  332. check_result $E_NOTEXIST "Certificate Authority not found"
  333. fi
  334. fi
  335. if [ -e "$ssl_dir/$domain.ca" ]; then
  336. s1=$(openssl x509 -text -in $ssl_dir/$domain.crt 2>/dev/null)
  337. s1=$(echo "$s1" |grep Issuer |awk -F = '{print $6}' |head -n1)
  338. s2=$(openssl x509 -text -in $ssl_dir/$domain.ca 2>/dev/null)
  339. s2=$(echo "$s2" |grep Subject |awk -F = '{print $6}' |head -n1)
  340. if [ "$s1" != "$s2" ]; then
  341. check_result $E_NOTEXIST "SSL intermediate chain is not valid"
  342. fi
  343. fi
  344. key_vrf=$(grep 'PRIVATE KEY' $ssl_dir/$domain.key |wc -l)
  345. if [ "$key_vrf" -ne 2 ]; then
  346. check_result $E_INVALID "SSL Key is not valid"
  347. fi
  348. if [ ! -z "$(grep 'ENCRYPTED' $ssl_dir/$domain.key)" ]; then
  349. check_result $E_FORBIDEN "SSL Key is protected (remove pass_phrase)"
  350. fi
  351. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  352. -key $ssl_dir/$domain.key >> /dev/null 2>&1 &
  353. pid=$!
  354. sleep 0.5
  355. disown &> /dev/null
  356. kill $pid &> /dev/null
  357. check_result $? "ssl certificate key pair is not valid" $E_INVALID
  358. }
  359. #----------------------------------------------------------#
  360. # DNS #
  361. #----------------------------------------------------------#
  362. # DNS template check
  363. is_dns_template_valid() {
  364. if [ ! -e "$DNSTPL/$1.tpl" ]; then
  365. check_result $E_NOTEXIST "$1 dns template doesn't exist"
  366. fi
  367. }
  368. # DNS domain existence check
  369. is_dns_domain_new() {
  370. dns=$(ls $HESTIA/data/users/*/dns/$1.conf 2>/dev/null)
  371. if [ ! -z "$dns" ]; then
  372. if [ "$2" == 'dns' ]; then
  373. check_result $E_EXISTS "DNS domain $1 exists"
  374. fi
  375. dns_user=$(echo "$dns" |cut -f 7 -d /)
  376. if [ "$dns_user" != "$user" ]; then
  377. check_result $E_EXISTS "DNS domain $1 exists"
  378. fi
  379. fi
  380. }
  381. # Update domain zone
  382. update_domain_zone() {
  383. domain_param=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
  384. eval $domain_param
  385. SOA=$(idn --quiet -a -t "$SOA")
  386. if [ -z "$SERIAL" ]; then
  387. SERIAL=$(date +'%Y%m%d01')
  388. fi
  389. if [[ "$domain" = *[![:ascii:]]* ]]; then
  390. domain_idn=$(idn -t --quiet -a $domain)
  391. else
  392. domain_idn=$domain
  393. fi
  394. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  395. echo "\$TTL $TTL
  396. @ IN SOA $SOA. root.$domain_idn. (
  397. $SERIAL
  398. 7200
  399. 3600
  400. 1209600
  401. 180 )
  402. " > $zn_conf
  403. fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
  404. while read line ; do
  405. IFS=$'\n'
  406. for key in $(echo $line|sed "s/' /'\n/g"); do
  407. eval ${key%%=*}="${key#*=}"
  408. done
  409. RECORD=$(idn --quiet -a -t "$RECORD")
  410. if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
  411. VALUE=$(idn --quiet -a -t "$VALUE")
  412. fi
  413. if [ "$SUSPENDED" != 'yes' ]; then
  414. eval echo -e "\"$fields\""|sed "s/%quote%/'/g" >> $zn_conf
  415. fi
  416. done < $USER_DATA/dns/$domain.conf
  417. }
  418. # Update zone serial
  419. update_domain_serial() {
  420. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  421. if [ -e $zn_conf ]; then
  422. zn_serial=$(head $zn_conf |grep 'SOA' -A1 |tail -n 1 |sed "s/ //g")
  423. s_date=$(echo ${zn_serial:0:8})
  424. c_date=$(date +'%Y%m%d')
  425. if [ "$s_date" == "$c_date" ]; then
  426. cur_value=$(echo ${zn_serial:8} )
  427. new_value=$(expr $cur_value + 1 )
  428. len_value=$(expr length $new_value)
  429. if [ 1 -eq "$len_value" ]; then
  430. new_value='0'$new_value
  431. fi
  432. serial="$c_date""$new_value"
  433. else
  434. serial="$(date +'%Y%m%d01')"
  435. fi
  436. else
  437. serial="$(date +'%Y%m%d01')"
  438. fi
  439. add_object_key "dns" 'DOMAIN' "$domain" 'SERIAL' 'RECORDS'
  440. update_object_value 'dns' 'DOMAIN' "$domain" '$SERIAL' "$serial"
  441. }
  442. # Get next DNS record ID
  443. get_next_dnsrecord(){
  444. if [ -z "$id" ]; then
  445. curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' |\
  446. sort -n|tail -n1)
  447. id="$((curr_str +1))"
  448. fi
  449. }
  450. # Sort DNS records
  451. sort_dns_records() {
  452. conf="$USER_DATA/dns/$domain.conf"
  453. cat $conf |sort -n -k 2 -t \' >$conf.tmp
  454. mv -f $conf.tmp $conf
  455. }
  456. # Check if this is a last record
  457. is_dns_record_critical() {
  458. str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
  459. eval $str
  460. if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
  461. records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf| wc -l)
  462. if [ $records -le 1 ]; then
  463. echo "Error: at least one $TYPE record should remain active"
  464. log_event "$E_INVALID" "$ARGUMENTS"
  465. exit $E_INVALID
  466. fi
  467. fi
  468. }
  469. # Check if dns record is valid
  470. is_dns_fqnd() {
  471. t=$1
  472. r=$2
  473. fqdn_type=$(echo $t | grep "NS\|CNAME\|MX\|PTR\|SRV")
  474. tree_length=3
  475. if [ $t = 'CNAME' ]; then
  476. tree_length=2
  477. fi
  478. if [ ! -z "$fqdn_type" ]; then
  479. dots=$(echo $dvalue | grep -o "\." | wc -l)
  480. if [ "$dots" -lt "$tree_length" ]; then
  481. r=$(echo $r|sed -e "s/\.$//")
  482. msg="$t record $r should be a fully qualified domain name (FQDN)"
  483. echo "Error: $msg"
  484. log_event "$E_INVALID" "$ARGUMENTS"
  485. exit $E_INVALID
  486. fi
  487. fi
  488. }
  489. # Validate nameserver
  490. is_dns_nameserver_valid() {
  491. d=$1
  492. t=$2
  493. r=$3
  494. if [ "$t" = 'NS' ]; then
  495. remote=$(echo $r |grep ".$domain.$")
  496. if [ ! -z "$remote" ]; then
  497. zone=$USER_DATA/dns/$d.conf
  498. a_record=$(echo $r |cut -f 1 -d '.')
  499. n_record=$(grep "RECORD='$a_record'" $zone| grep "TYPE='A'")
  500. if [ -z "$n_record" ]; then
  501. check_result $E_NOTEXIST "IN A $a_record.$d does not exist"
  502. fi
  503. fi
  504. fi
  505. }
  506. #----------------------------------------------------------#
  507. # MAIL #
  508. #----------------------------------------------------------#
  509. # Mail domain existence check
  510. is_mail_domain_new() {
  511. mail=$(ls $HESTIA/data/users/*/mail/$1.conf 2>/dev/null)
  512. if [ ! -z "$mail" ]; then
  513. if [ "$2" == 'mail' ]; then
  514. check_result $E_EXISTS "Mail domain $1 exists"
  515. fi
  516. mail_user=$(echo "$mail" |cut -f 7 -d /)
  517. if [ "$mail_user" != "$user" ]; then
  518. check_result $E_EXISTS "Mail domain $1 exists"
  519. fi
  520. fi
  521. }
  522. # Checking mail account existance
  523. is_mail_new() {
  524. check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
  525. if [ ! -z "$check_acc" ]; then
  526. check_result $E_EXISTS "mail account $1 is already exists"
  527. fi
  528. check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf )
  529. check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
  530. if [ ! -z "$check_als" ]; then
  531. check_result $E_EXISTS "mail alias $1 is already exists"
  532. fi
  533. }
  534. # Add mail server SSL configuration
  535. add_mail_ssl_config() {
  536. # Ensure that SSL certificate directories exists
  537. if [ ! -d $HOMEDIR/$user/conf/mail/$domain/ssl/ ]; then
  538. mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
  539. fi
  540. if [ ! -d /usr/local/hestia/ssl/mail ]; then
  541. mkdir -p /usr/local/hestia/ssl/mail
  542. fi
  543. if [ ! -d /etc/dovecot/conf.d/domains ]; then
  544. mkdir -p /etc/dovecot/conf.d/domains
  545. fi
  546. # Add certificate to Hestia user configuration data directory
  547. if [ -f $ssl_dir/$domain.crt ]; then
  548. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.crt
  549. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/mail.$domain.key
  550. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.pem
  551. if [ -e "$ssl_dir/$domain.ca" ]; then
  552. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/mail.$domain.ca
  553. echo >> $USER_DATA/ssl/mail.$domain.pem
  554. cat $USER_DATA/ssl/mail.$domain.ca >> $USER_DATA/ssl/mail.$domain.pem
  555. fi
  556. fi
  557. chmod 660 $USER_DATA/ssl/mail.$domain.*
  558. # Add certificate to user home directory
  559. cp -f $USER_DATA/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
  560. cp -f $USER_DATA/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
  561. cp -f $USER_DATA/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
  562. if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then
  563. cp -f $USER_DATA/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
  564. fi
  565. # Add domain SSL configuration to dovecot
  566. if [ -f /etc/dovecot/conf.d/domains/$domain.conf ]; then
  567. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  568. fi
  569. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  570. echo "local_name mail.$domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  571. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  572. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  573. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  574. # Add domain SSL configuration to exim4
  575. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem /usr/local/hestia/ssl/mail/mail.$domain.crt
  576. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key /usr/local/hestia/ssl/mail/mail.$domain.key
  577. # Set correct permissions on certificates
  578. chmod 0644 $HOMEDIR/$user/conf/mail/$domain/ssl/*
  579. chown -h $user:mail $HOMEDIR/$user/conf/mail/$domain/ssl/*
  580. chmod -R 0644 /usr/local/hestia/ssl/mail/*
  581. chown -h $user:mail /usr/local/hestia/ssl/mail/*
  582. }
  583. # Delete SSL support for mail domain
  584. del_mail_ssl_config() {
  585. # Remove old mail certificates
  586. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  587. # Remove dovecot configuration
  588. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  589. # Remove SSL vhost configuration
  590. rm -f $HOMEDIR/$user/conf/mail/$domain/*.*ssl.conf
  591. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  592. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  593. # Remove SSL certificates
  594. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  595. rm -f /usr/local/hestia/ssl/mail/mail.$domain.*
  596. }
  597. # Delete generated certificates from user configuration data directory
  598. del_mail_ssl_certificates(){
  599. rm -f $USER_DATA/ssl/mail.$domain.*
  600. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  601. }
  602. # Add webmail config
  603. add_webmail_config() {
  604. mkdir -p "$HOMEDIR/$user/conf/mail/$domain"
  605. conf="$HOMEDIR/$user/conf/mail/$domain/$1.conf"
  606. if [[ "$2" =~ stpl$ ]]; then
  607. conf="$HOMEDIR/$user/conf/mail/$domain/$1.ssl.conf"
  608. fi
  609. domain_idn=$domain
  610. format_domain_idn
  611. ssl_crt="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt"
  612. ssl_key="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key"
  613. ssl_pem="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem"
  614. ssl_ca="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca"
  615. cat $MAILTPL/$1/$2 | \
  616. sed -e "s|%ip%|$local_ip|g" \
  617. -e "s|%domain%|$WEBMAIL_ALIAS.$domain|g" \
  618. -e "s|%domain_idn%|$domain_idn|g" \
  619. -e "s|%root_domain%|$domain|g" \
  620. -e "s|%alias%|mail.$domain|g" \
  621. -e "s|%alias_idn%|${aliases_idn//,/ }|g" \
  622. -e "s|%alias_string%|$alias_string|g" \
  623. -e "s|%email%|info@$domain|g" \
  624. -e "s|%web_system%|$WEB_SYSTEM|g" \
  625. -e "s|%web_port%|$WEB_PORT|g" \
  626. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  627. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  628. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  629. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  630. -e "s|%proxy_port%|$PROXY_PORT|g" \
  631. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  632. -e "s/%proxy_extentions%/${PROXY_EXT//,/|}/g" \
  633. -e "s|%user%|$user|g" \
  634. -e "s|%group%|$user|g" \
  635. -e "s|%home%|$HOMEDIR|g" \
  636. -e "s|%docroot%|$docroot|g" \
  637. -e "s|%sdocroot%|$sdocroot|g" \
  638. -e "s|%ssl_crt%|$ssl_crt|g" \
  639. -e "s|%ssl_key%|$ssl_key|g" \
  640. -e "s|%ssl_pem%|$ssl_pem|g" \
  641. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  642. -e "s|%ssl_ca%|$ssl_ca|g" \
  643. > $conf
  644. chown root:$user $conf
  645. chmod 640 $conf
  646. if [[ "$2" =~ stpl$ ]]; then
  647. if [ ! -z "$WEB_SYSTEM" ]; then
  648. forcessl="$HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.forcessl.conf"
  649. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  650. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  651. fi
  652. if [ ! -z "$PROXY_SYSTEM" ]; then
  653. forcessl="$HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.forcessl.conf"
  654. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  655. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  656. fi
  657. # Add rewrite rules to force HTTPS/SSL connections
  658. if [ ! -z "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
  659. echo 'return 301 https://$server_name$request_uri;' > $forcessl
  660. else
  661. echo 'RewriteEngine On' > $forcessl
  662. echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl
  663. fi
  664. # Remove old configurations
  665. rm -rf $HOMEDIR/$user/conf/mail/$domain.*
  666. rm -rf $HOMEDIR/$user/conf/mail/ssl.$domain.*
  667. rm -rf $HOMEDIR/$user/conf/mail/*nginx.$domain.*
  668. else
  669. if [ ! -z "$WEB_SYSTEM" ]; then
  670. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  671. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  672. fi
  673. if [ ! -z "$PROXY_SYSTEM" ]; then
  674. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  675. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  676. fi
  677. # Clear old configurations
  678. rm -rf $HOMEDIR/$user/conf/mail/$domain.*
  679. fi
  680. }
  681. # Delete webmail support
  682. del_webmail_config() {
  683. if [ ! -z "$WEB_SYSTEM" ]; then
  684. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.conf
  685. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  686. fi
  687. if [ ! -z "$PROXY_SYSTEM" ]; then
  688. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*conf
  689. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  690. fi
  691. }
  692. # Delete SSL webmail support
  693. del_webmail_ssl_config() {
  694. if [ ! -z "$WEB_SYSTEM" ]; then
  695. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*ssl.conf
  696. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  697. fi
  698. if [ ! -z "$PROXY_SYSTEM" ]; then
  699. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*ssl.conf
  700. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  701. fi
  702. }
  703. #----------------------------------------------------------#
  704. # CMN #
  705. #----------------------------------------------------------#
  706. # Checking domain existance
  707. is_domain_new() {
  708. type=$1
  709. for object in ${2//,/ }; do
  710. if [ ! -z "$WEB_SYSTEM" ]; then
  711. is_web_domain_new $object $type
  712. is_web_alias_new $object $type
  713. fi
  714. if [ ! -z "$DNS_SYSTEM" ]; then
  715. is_dns_domain_new $object $type
  716. fi
  717. if [ ! -z "$MAIL_SYSTEM" ]; then
  718. is_mail_domain_new $object $type
  719. fi
  720. done
  721. }
  722. # Get domain variables
  723. get_domain_values() {
  724. eval $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf)
  725. }