domain.sh 28 KB

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