domain.sh 30 KB

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