domain.sh 30 KB

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