domain.sh 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. #!/bin/bash
  2. #===========================================================================#
  3. # #
  4. # Hestia Control Panel - Domain Function Library #
  5. # #
  6. #===========================================================================#
  7. #----------------------------------------------------------#
  8. # COMMON FUNCTIONS #
  9. #----------------------------------------------------------#
  10. # Prepare IPV4 and IPV6 addresses for using in templates
  11. prepare_ips_for_template() {
  12. if [ -z "$local_ip" ]; then
  13. i4mark=""
  14. ipv4=""
  15. web_ipv4=""
  16. web_ip="[$local_ipv6]"
  17. proxy_ipv4=""
  18. proxy_ip="[$local_ipv6]"
  19. legacy_ip="[$local_ipv6]"
  20. else
  21. i4mark="\1"
  22. ipv4="$local_ip"
  23. web_ipv4="$local_ip"
  24. web_ip="$local_ip"
  25. proxy_ipv4="$local_ip"
  26. proxy_ip="$local_ip"
  27. legacy_ip="$local_ip"
  28. fi
  29. if [ -z "$local_ipv6" ]; then
  30. i6mark=""
  31. web_ipv6=""
  32. web_ip="$local_ip"
  33. proxy_ipv6=""
  34. proxy_ip="$local_ip"
  35. else
  36. i6mark="\1"
  37. web_ipv6="[$local_ipv6]"
  38. web_ip="[$local_ipv6]"
  39. proxy_ipv6="[$local_ipv6]"
  40. proxy_ip="[$local_ipv6]"
  41. fi
  42. }
  43. #----------------------------------------------------------#
  44. # WEB #
  45. #----------------------------------------------------------#
  46. # Web template check
  47. is_web_template_valid() {
  48. if [ -n "$WEB_SYSTEM" ]; then
  49. tpl="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$1.tpl"
  50. stpl="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$1.stpl"
  51. if [ ! -e "$tpl" ] || [ ! -e "$stpl" ]; then
  52. check_result "$E_NOTEXIST" "$1 web template doesn't exist"
  53. fi
  54. fi
  55. }
  56. # Proxy template check
  57. is_proxy_template_valid() {
  58. if [ -n "$PROXY_SYSTEM" ]; then
  59. tpl="$WEBTPL/$PROXY_SYSTEM/$1.tpl"
  60. stpl="$WEBTPL/$PROXY_SYSTEM/$1.stpl"
  61. if [ ! -e "$tpl" ] || [ ! -e "$stpl" ]; then
  62. check_result "$E_NOTEXIST" "$1 proxy template doesn't exist"
  63. fi
  64. fi
  65. }
  66. # Backend template check
  67. is_backend_template_valid() {
  68. if [ -n "$WEB_BACKEND" ]; then
  69. if [ ! -e "$WEBTPL/$WEB_BACKEND/$1.tpl" ]; then
  70. check_result "$E_NOTEXIST" "$1 backend template doesn't exist"
  71. fi
  72. fi
  73. }
  74. # Web domain existence check
  75. is_web_domain_new() {
  76. web=$(grep -F -H "DOMAIN='$1'" $HESTIA/data/users/*/web.conf)
  77. if [ -n "$web" ]; then
  78. if [ "$type" == 'web' ]; then
  79. check_result "$E_EXISTS" "Web domain $1 exists"
  80. fi
  81. web_user=$(echo "$web" | cut -f 7 -d /)
  82. if [ "$web_user" != "$user" ]; then
  83. check_result "$E_EXISTS" "Web domain $1 exists"
  84. fi
  85. fi
  86. }
  87. # Web alias existence check
  88. is_web_alias_new() {
  89. grep -wH "$1" $HESTIA/data/users/*/web.conf | while read -r line; do
  90. user=$(echo $line | cut -f 7 -d /)
  91. string=$(echo $line | cut -f 2- -d ':')
  92. parse_object_kv_list $string
  93. if [ -n "$ALIAS" ]; then
  94. a1=$(echo "'$ALIAS'" | grep -F "'$1'")
  95. if [ -n "$a1" ] && [ "$2" == "web" ]; then
  96. return "$E_EXISTS"
  97. fi
  98. if [ -n "$a1" ] && [ "$user" != "$user" ]; then
  99. return "$E_EXISTS"
  100. fi
  101. a2=$(echo "'$ALIAS'" | grep -F "'$1,")
  102. if [ -n "$a2" ] && [ "$2" == "web" ]; then
  103. return "$E_EXISTS"
  104. fi
  105. if [ -n "$a2" ] && [ "$user" != "$user" ]; then
  106. return "$E_EXISTS"
  107. fi
  108. a3=$(echo "'$ALIAS'" | grep -F ",$1,")
  109. if [ -n "$a3" ] && [ "$2" == "web" ]; then
  110. return "$E_EXISTS"
  111. fi
  112. if [ -n "$a3" ] && [ "$user" != "$user" ]; then
  113. return "$E_EXISTS"
  114. fi
  115. a4=$(echo "'$ALIAS'" | grep -F ",$1'")
  116. if [ -n "$a4" ] && [ "$2" == "web" ]; then
  117. return "$E_EXISTS"
  118. fi
  119. if [ -n "$a4" ] && [ "$user" != "$user" ]; then
  120. return "$E_EXISTS"
  121. fi
  122. fi
  123. done
  124. if [ $? -ne 0 ]; then
  125. check_result "$E_EXISTS" "Web alias $1 exists"
  126. fi
  127. }
  128. # Prepare web backend
  129. prepare_web_backend() {
  130. # Accept first function argument as backend template otherwise fallback to $template global variable
  131. local backend_template=${1:-$template}
  132. pool=$(find -L /etc/php/ -name "$domain.conf" -exec dirname {} \;)
  133. # Check if multiple-PHP installed
  134. regex="socket-(\d+)_(\d+)"
  135. if [[ $backend_template =~ ^.*PHP-([0-9])\_([0-9])$ ]]; then
  136. backend_version="${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
  137. pool=$(find -L /etc/php/$backend_version -type d \( -name "pool.d" -o -name "*fpm.d" \))
  138. else
  139. backend_version=$(multiphp_default_version)
  140. if [ -z "$pool" ] || [ -z "$BACKEND" ]; then
  141. pool=$(find -L /etc/php/$backend_version -type d \( -name "pool.d" -o -name "*fpm.d" \))
  142. fi
  143. fi
  144. if [ ! -e "$pool" ]; then
  145. check_result $E_NOTEXIST "php-fpm pool doesn't exist"
  146. fi
  147. backend_type="$domain"
  148. if [ "$WEB_BACKEND_POOL" = 'user' ]; then
  149. backend_type="$user"
  150. fi
  151. if [ -e "$pool/$backend_type.conf" ]; then
  152. backend_lsnr=$(grep "listen =" $pool/$backend_type.conf)
  153. backend_lsnr=$(echo "$backend_lsnr" | cut -f 2 -d = | sed "s/ //")
  154. if [ -n "$(echo $backend_lsnr | grep /)" ]; then
  155. backend_lsnr="unix:$backend_lsnr"
  156. fi
  157. fi
  158. }
  159. # Delete web backend
  160. delete_web_backend() {
  161. find -L /etc/php/ -type f -name "$backend_type.conf" -exec rm -f {} \;
  162. }
  163. # Prepare web aliases
  164. prepare_web_aliases() {
  165. i=1
  166. for tmp_alias in ${1//,/ }; do
  167. tmp_alias_idn="$tmp_alias"
  168. if [[ "$tmp_alias" = *[![:ascii:]]* ]]; then
  169. tmp_alias_idn=$(idn2 --quiet $tmp_alias)
  170. fi
  171. if [[ $i -eq 1 ]]; then
  172. aliases="$tmp_alias"
  173. aliases_idn="$tmp_alias_idn"
  174. alias_string="ServerAlias $tmp_alias_idn"
  175. else
  176. aliases="$aliases,$tmp_alias"
  177. aliases_idn="$aliases_idn,$tmp_alias_idn"
  178. if (($i % 100 == 0)); then
  179. alias_string="$alias_string\n ServerAlias $tmp_alias_idn"
  180. else
  181. alias_string="$alias_string $tmp_alias_idn"
  182. fi
  183. fi
  184. alias_number=$i
  185. ((i++))
  186. done
  187. }
  188. # Update web domain values
  189. prepare_web_domain_values() {
  190. if [[ "$domain" = *[![:ascii:]]* ]]; then
  191. domain_idn=$(idn2 --quiet $domain)
  192. else
  193. domain_idn=$domain
  194. fi
  195. group="$user"
  196. docroot="$HOMEDIR/$user/web/$domain/public_html"
  197. sdocroot="$docroot"
  198. if [ "$SSL_HOME" = 'single' ]; then
  199. sdocroot="$HOMEDIR/$user/web/$domain/public_shtml"
  200. $BIN/v-add-fs-directory "$user" "$HOMEDIR/$user/web/$domain/public_shtml"
  201. chmod 751 $HOMEDIR/$user/web/$domain/public_shtml
  202. chown www-data:$user $HOMEDIR/$user/web/$domain/public_shtml
  203. fi
  204. if [ -n "$WEB_BACKEND" ]; then
  205. prepare_web_backend "$BACKEND"
  206. fi
  207. server_alias=''
  208. alias_string=''
  209. aliases_idn=''
  210. ssl_ca_str=''
  211. prepare_web_aliases $ALIAS
  212. ssl_crt="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.crt"
  213. ssl_key="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.key"
  214. ssl_pem="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.pem"
  215. ssl_ca="$HOMEDIR/$user/conf/web/$domain/ssl/$domain.ca"
  216. if [ ! -e "$USER_DATA/ssl/$domain.ca" ]; then
  217. ssl_ca_str='#'
  218. fi
  219. # Set correct document root
  220. if [ -n "$CUSTOM_DOCROOT" ]; then
  221. # Custom document root has been set by the user, import from configuration
  222. custom_docroot="$CUSTOM_DOCROOT"
  223. docroot="$custom_docroot"
  224. sdocroot="$docroot"
  225. elif [ -n "$CUSTOM_DOCROOT" ] && [ -n "$target_directory" ]; then
  226. # Custom document root has been specified with a different target than public_html
  227. if [ -d "$HOMEDIR/$user/web/$target_domain/public_html/$target_directory/" ]; then
  228. custom_docroot="$HOMEDIR/$user/web/$target_domain/public_html/$target_directory"
  229. docroot="$custom_docroot"
  230. sdocroot="$docroot"
  231. fi
  232. elif [ -n "$CUSTOM_DOCROOT" ] && [ -z "$target_directory" ]; then
  233. # Set custom document root to target domain's public_html folder
  234. custom_docroot="$HOMEDIR/$user/web/$target_domain/public_html"
  235. docroot="$custom_docroot"
  236. sdocroot="$docroot"
  237. else
  238. # No custom document root specified, use default
  239. docroot="$HOMEDIR/$user/web/$domain/public_html"
  240. sdocroot="$docroot"
  241. fi
  242. if [ "$SUSPENDED" = 'yes' ]; then
  243. docroot="$HESTIA/data/templates/web/suspend"
  244. sdocroot="$HESTIA/data/templates/web/suspend"
  245. fi
  246. }
  247. # Add web config
  248. add_web_config() {
  249. # Check if folder already exists
  250. if [ ! -d "$HOMEDIR/$user/conf/web/$domain" ]; then
  251. mkdir -p "$HOMEDIR/$user/conf/web/$domain/"
  252. fi
  253. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  254. if [[ "$2" =~ stpl$ ]]; then
  255. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  256. fi
  257. domain_idn=$domain
  258. format_domain_idn
  259. WEBTPL_LOCATION="$WEBTPL/$1"
  260. if [ "$1" != "$PROXY_SYSTEM" ] && [ -n "$WEB_BACKEND" ] && [ -d "$WEBTPL_LOCATION/$WEB_BACKEND" ]; then
  261. if [ -f "$WEBTPL_LOCATION/$WEB_BACKEND/$2" ]; then
  262. # check for backend specific template
  263. WEBTPL_LOCATION="$WEBTPL/$1/$WEB_BACKEND"
  264. fi
  265. fi
  266. prepare_ips_for_template # prepare IPV4 and IPV6 variables for template substitution
  267. # Note: Removing or renaming template variables will lead to broken custom templates.
  268. # -If possible custom templates should be automatically upgraded to use the new format
  269. # -Alternatively a depreciation period with proper notifications should be considered
  270. cat "${WEBTPL_LOCATION}/$2" \
  271. | sed -e "s|%<i4\(.*\)i4>%|$i4mark|g" \
  272. -e "s|%<i6\(.*\)i6>%|$i6mark|g" \
  273. -e "s|%web_ipv4%|$web_ipv4|g" \
  274. -e "s|%web_ipv6%|$web_ipv6|g" \
  275. -e "s|%web_ip%|$web_ip|g" \
  276. -e "s|%proxy_ipv4%|$proxy_ipv4|g" \
  277. -e "s|%proxy_ipv6%|$proxy_ipv6|g" \
  278. -e "s|%proxy_ip%|$proxy_ip|g" \
  279. -e "s|%ip%|$legacy_ip|g" \
  280. -e "s|%domain%|$domain|g" \
  281. -e "s|%domain_idn%|$domain_idn|g" \
  282. -e "s|%alias%|${aliases//,/ }|g" \
  283. -e "s|%alias_idn%|${aliases_idn//,/ }|g" \
  284. -e "s|%alias_string%|$alias_string|g" \
  285. -e "s|%email%|info@$domain|g" \
  286. -e "s|%web_system%|$WEB_SYSTEM|g" \
  287. -e "s|%web_port%|$WEB_PORT|g" \
  288. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  289. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  290. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  291. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  292. -e "s|%proxy_port%|$PROXY_PORT|g" \
  293. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  294. -e "s/%proxy_extentions%/${PROXY_EXT//,/|}/g" \
  295. -e "s/%proxy_extensions%/${PROXY_EXT//,/|}/g" \
  296. -e "s|%user%|$user|g" \
  297. -e "s|%group%|$user|g" \
  298. -e "s|%home%|$HOMEDIR|g" \
  299. -e "s|%docroot%|$docroot|g" \
  300. -e "s|%sdocroot%|$sdocroot|g" \
  301. -e "s|%ssl_crt%|$ssl_crt|g" \
  302. -e "s|%ssl_key%|$ssl_key|g" \
  303. -e "s|%ssl_pem%|$ssl_pem|g" \
  304. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  305. -e "s|%ssl_ca%|$ssl_ca|g" \
  306. > $conf
  307. chown root:$user $conf
  308. chmod 640 $conf
  309. if [[ "$2" =~ stpl$ ]]; then
  310. rm -f /etc/$1/conf.d/domains/$domain.ssl.conf
  311. ln -s $conf /etc/$1/conf.d/domains/$domain.ssl.conf
  312. # Rename/Move extra SSL config files
  313. find=$(find $HOMEDIR/$user/conf/web/*.$domain.org* 2> /dev/null)
  314. for f in $find; do
  315. if [[ $f =~ .*/s(nginx|apache2)\.$domain\.conf(.*) ]]; then
  316. ServerType="${BASH_REMATCH[1]}"
  317. CustomConfigName="${BASH_REMATCH[2]}"
  318. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  319. rm -f "$f"
  320. continue
  321. fi
  322. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.ssl.conf_old$CustomConfigName"
  323. fi
  324. done
  325. else
  326. rm -f /etc/$1/conf.d/domains/$domain.conf
  327. ln -s $conf /etc/$1/conf.d/domains/$domain.conf
  328. # Rename/Move extra config files
  329. find=$(find $HOMEDIR/$user/conf/web/*.$domain.org* 2> /dev/null)
  330. for f in $find; do
  331. if [[ $f =~ .*/(nginx|apache2)\.$domain\.conf(.*) ]]; then
  332. ServerType="${BASH_REMATCH[1]}"
  333. CustomConfigName="${BASH_REMATCH[2]}"
  334. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  335. rm -f "$f"
  336. continue
  337. fi
  338. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.conf_old$CustomConfigName"
  339. elif [[ $f =~ .*/forcessl\.(nginx|apache2)\.$domain\.conf ]]; then
  340. ServerType="${BASH_REMATCH[1]}"
  341. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.forcessl.conf"
  342. fi
  343. done
  344. fi
  345. trigger="${2/.*pl/.sh}"
  346. if [ -x "${WEBTPL_LOCATION}/$trigger" ]; then
  347. $WEBTPL_LOCATION/$trigger \
  348. $user $domain $local_ip $HOMEDIR \
  349. $HOMEDIR/$user/web/$domain/public_html
  350. fi
  351. }
  352. # Get config top and bottom line number
  353. get_web_config_lines() {
  354. tpl_lines=$(egrep -ni "name %domain_idn%" $1 | grep -w %domain_idn%)
  355. tpl_lines=$(echo "$tpl_lines" | cut -f 1 -d :)
  356. tpl_last_line=$(wc -l $1 | cut -f 1 -d ' ')
  357. if [ -z "$tpl_lines" ]; then
  358. check_result $E_PARSING "can't parse template $1"
  359. fi
  360. domain_idn=$domain
  361. format_domain_idn
  362. vhost_lines=$(grep -niF "name $domain_idn" $2)
  363. vhost_lines=$(echo "$vhost_lines" | egrep "$domain_idn($| |;)")
  364. vhost_lines=$(echo "$vhost_lines" | cut -f 1 -d :)
  365. if [ -z "$vhost_lines" ]; then
  366. check_result $E_PARSING "can't parse config $2"
  367. fi
  368. top_line=$((vhost_lines + 1 - tpl_lines))
  369. bottom_line=$((top_line - 1 + tpl_last_line))
  370. multi=$(sed -n "$top_line,$bottom_line p" $2 | grep ServerAlias | wc -l)
  371. if [ "$multi" -ge 2 ]; then
  372. bottom_line=$((bottom_line + multi - 1))
  373. fi
  374. }
  375. # Replace web config
  376. replace_web_config() {
  377. if [ -z "$old" -o -z "$new" ]; then
  378. prepare_web_domain_values # if one of both parameters is empty, prepare values for the web domain
  379. add_web_config "$@" # web configs must be new generated from the templates
  380. else
  381. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  382. if [[ "$2" =~ stpl$ ]]; then
  383. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  384. fi
  385. if [ -e "$conf" ]; then
  386. sed -i "s|$old|$new|g" $conf # simple search and replace only possible, if both (old and new) parameters are defined
  387. fi
  388. fi
  389. }
  390. # Delete web configuration
  391. del_web_config() {
  392. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  393. local confname="$domain.conf"
  394. if [[ "$2" =~ stpl$ ]]; then
  395. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  396. confname="$domain.ssl.conf"
  397. fi
  398. # Clean up legacy configuration files
  399. if [ ! -e "$conf" ]; then
  400. local legacyconf="$HOMEDIR/$user/conf/web/$1.conf"
  401. if [[ "$2" =~ stpl$ ]]; then
  402. legacyconf="$HOMEDIR/$user/conf/web/s$1.conf"
  403. fi
  404. rm -f $legacyconf
  405. # Remove old global includes file
  406. rm -f /etc/$1/conf.d/hestia.conf
  407. fi
  408. # Remove domain configuration files and clean up symbolic links
  409. rm -f "$conf"
  410. if [ -n "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" = "$1" ]; then
  411. rm -f "/etc/$WEB_SYSTEM/conf.d/domains/$confname"
  412. fi
  413. if [ -n "$PROXY_SYSTEM" ] && [ "$PROXY_SYSTEM" = "$1" ]; then
  414. rm -f "/etc/$PROXY_SYSTEM/conf.d/domains/$confname"
  415. fi
  416. }
  417. # SSL certificate verification
  418. is_web_domain_cert_valid() {
  419. if [ ! -e "$ssl_dir/$domain.crt" ]; then
  420. check_result "$E_NOTEXIST" "$ssl_dir/$domain.crt not found"
  421. fi
  422. if [ ! -e "$ssl_dir/$domain.key" ]; then
  423. check_result "$E_NOTEXIST" "$ssl_dir/$domain.key not found"
  424. fi
  425. crt_vrf=$(openssl verify $ssl_dir/$domain.crt 2>&1)
  426. if [ -n "$(echo $crt_vrf | grep 'unable to load')" ]; then
  427. check_result "$E_INVALID" "SSL Certificate is not valid"
  428. fi
  429. if [ -n "$(echo $crt_vrf | grep 'unable to get local issuer')" ]; then
  430. if [ ! -e "$ssl_dir/$domain.ca" ]; then
  431. check_result "$E_NOTEXIST" "Certificate Authority not found"
  432. fi
  433. fi
  434. if [ -e "$ssl_dir/$domain.ca" ]; then
  435. s1=$(openssl x509 -text -in $ssl_dir/$domain.crt 2> /dev/null)
  436. s1=$(echo "$s1" | grep Issuer | awk -F = '{print $6}' | head -n1)
  437. s2=$(openssl x509 -text -in $ssl_dir/$domain.ca 2> /dev/null)
  438. s2=$(echo "$s2" | grep Subject | awk -F = '{print $6}' | head -n1)
  439. if [ "$s1" != "$s2" ]; then
  440. check_result "$E_NOTEXIST" "SSL intermediate chain is not valid"
  441. fi
  442. fi
  443. key_vrf=$(grep 'PRIVATE KEY' $ssl_dir/$domain.key | wc -l)
  444. if [ "$key_vrf" -ne 2 ]; then
  445. check_result "$E_INVALID" "SSL Key is not valid"
  446. fi
  447. if [ -n "$(grep 'ENCRYPTED' $ssl_dir/$domain.key)" ]; then
  448. check_result "$E_FORBIDEN" "SSL Key is protected (remove pass_phrase)"
  449. fi
  450. if pgrep -x "openssl" > /dev/null; then
  451. pkill openssl
  452. fi
  453. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  454. -key $ssl_dir/$domain.key >> /dev/null 2>&1 &
  455. pid=$!
  456. sleep 0.5
  457. disown &> /dev/null
  458. kill $pid &> /dev/null
  459. check_result $? "ssl certificate key pair is not valid" $E_INVALID
  460. }
  461. #----------------------------------------------------------#
  462. # DNS #
  463. #----------------------------------------------------------#
  464. # DNS template check
  465. is_dns_template_valid() {
  466. if [ ! -e "$DNSTPL/$1.tpl" ]; then
  467. check_result "$E_NOTEXIST" "$1 dns template doesn't exist"
  468. fi
  469. }
  470. # DNS domain existence check
  471. is_dns_domain_new() {
  472. dns=$(ls $HESTIA/data/users/*/dns/$1.conf 2> /dev/null)
  473. if [ -n "$dns" ]; then
  474. if [ "$2" == 'dns' ]; then
  475. check_result "$E_EXISTS" "DNS domain $1 exists"
  476. fi
  477. dns_user=$(echo "$dns" | cut -f 7 -d /)
  478. if [ "$dns_user" != "$user" ]; then
  479. check_result "$E_EXISTS" "DNS domain $1 exists"
  480. fi
  481. fi
  482. }
  483. # Update domain zone
  484. update_domain_zone() {
  485. domain_param=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
  486. parse_object_kv_list "$domain_param"
  487. local zone_ttl="$TTL"
  488. SOA=$(idn2 --quiet "$SOA")
  489. if [ -z "$SERIAL" ]; then
  490. SERIAL=$(date +'%Y%m%d01')
  491. fi
  492. if [[ "$domain" = *[![:ascii:]]* ]]; then
  493. domain_idn=$(idn2 --quiet $domain)
  494. else
  495. domain_idn=$domain
  496. fi
  497. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  498. echo "\$TTL $TTL
  499. @ IN SOA $SOA. root.$domain_idn. (
  500. $SERIAL
  501. 7200
  502. 3600
  503. 1209600
  504. 180 )
  505. " > $zn_conf
  506. fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
  507. while read line; do
  508. unset TTL
  509. IFS=$'\n'
  510. for key in $(echo $line | sed "s/' /'\n/g"); do
  511. eval ${key%%=*}="${key#*=}"
  512. done
  513. # inherit zone TTL if record lacks explicit TTL value
  514. [ -z "$TTL" ] && TTL="$zone_ttl"
  515. RECORD=$(idn2 --quiet "$RECORD")
  516. if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
  517. VALUE=$(idn2 --quiet "$VALUE")
  518. fi
  519. if [ "$TYPE" = 'TXT' ]; then
  520. txtlength=${#VALUE}
  521. if [ $txtlength -gt 255 ]; then
  522. already_chunked=0
  523. if [[ $VALUE == *"\" \""* ]] || [[ $VALUE == *"\"\""* ]]; then
  524. already_chunked=1
  525. fi
  526. if [ $already_chunked -eq 0 ]; then
  527. if [[ ${VALUE:0:1} = '"' ]]; then
  528. txtlength=$(($txtlength - 2))
  529. VALUE=${VALUE:1:txtlength}
  530. fi
  531. VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
  532. fi
  533. fi
  534. fi
  535. if [ "$SUSPENDED" != 'yes' ]; then
  536. eval echo -e "\"$fields\"" | sed "s/%quote%/'/g" >> $zn_conf
  537. fi
  538. done < $USER_DATA/dns/$domain.conf
  539. }
  540. # Update zone serial
  541. update_domain_serial() {
  542. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  543. if [ -e $zn_conf ]; then
  544. zn_serial=$(head $zn_conf | grep 'SOA' -A1 | tail -n 1 | sed "s/ //g")
  545. s_date=$(echo ${zn_serial:0:8})
  546. c_date=$(date +'%Y%m%d')
  547. if [ "$s_date" == "$c_date" ]; then
  548. cur_value=$(echo ${zn_serial:8})
  549. new_value=$(expr $cur_value + 1)
  550. len_value=$(expr length $new_value)
  551. if [ 1 -eq "$len_value" ]; then
  552. new_value='0'$new_value
  553. fi
  554. serial="$c_date""$new_value"
  555. else
  556. serial="$(date +'%Y%m%d01')"
  557. fi
  558. else
  559. serial="$(date +'%Y%m%d01')"
  560. fi
  561. add_object_key "dns" 'DOMAIN' "$domain" 'SERIAL' 'RECORDS'
  562. update_object_value 'dns' 'DOMAIN' "$domain" '$SERIAL' "$serial"
  563. }
  564. # Get next DNS record ID
  565. get_next_dnsrecord() {
  566. if [ -z "$id" ]; then
  567. curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' \
  568. | sort -n | tail -n1)
  569. id="$((curr_str + 1))"
  570. fi
  571. }
  572. # Sort DNS records
  573. sort_dns_records() {
  574. conf="$USER_DATA/dns/$domain.conf"
  575. cat $conf | sort -n -k 2 -t \' > $conf.tmp
  576. mv -f $conf.tmp $conf
  577. }
  578. # Check if this is a last record
  579. is_dns_record_critical() {
  580. str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
  581. parse_object_kv_list "$str"
  582. if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
  583. records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf | wc -l)
  584. if [ $records -le 1 ]; then
  585. echo "Error: at least one $TYPE record should remain active"
  586. log_event "$E_INVALID" "$ARGUMENTS"
  587. exit "$E_INVALID"
  588. fi
  589. fi
  590. }
  591. # Check if dns record is valid
  592. is_dns_fqnd() {
  593. t=$1
  594. r=$2
  595. fqdn_type=$(echo $t | grep "^NS\|CNAME\|MX\|PTR\|SRV")
  596. tree_length=3
  597. if [[ $t = 'CNAME' || $t = 'MX' || $t = 'PTR' ]]; then
  598. tree_length=2
  599. fi
  600. if [ -n "$fqdn_type" ]; then
  601. dots=$(echo $dvalue | grep -o "\." | wc -l)
  602. if [ "$dots" -lt "$tree_length" ]; then
  603. r=$(echo $r | sed -e "s/\.$//")
  604. msg="$t record $r should be a fully qualified domain name (FQDN)"
  605. echo "Error: $msg"
  606. log_event "$E_INVALID" "$ARGUMENTS"
  607. exit "$E_INVALID"
  608. fi
  609. fi
  610. }
  611. # Validate nameserver
  612. is_dns_nameserver_valid() {
  613. d=$1
  614. t=$2
  615. r=$3
  616. if [ "$t" = 'NS' ]; then
  617. remote=$(echo $r | grep ".$domain.$")
  618. if [ -n "$remote" ]; then
  619. zone=$USER_DATA/dns/$d.conf
  620. a_record=${r%.$d.}
  621. n_record=$(grep "RECORD='$a_record'" $zone | grep "TYPE='A'")
  622. if [ -z "$n_record" ]; then
  623. check_result "$E_NOTEXIST" "IN A $a_record.$d does not exist"
  624. fi
  625. fi
  626. fi
  627. }
  628. #----------------------------------------------------------#
  629. # MAIL #
  630. #----------------------------------------------------------#
  631. # Mail domain existence check
  632. is_mail_domain_new() {
  633. mail=$(ls $HESTIA/data/users/*/mail/$1.conf 2> /dev/null)
  634. if [ -n "$mail" ]; then
  635. if [ "$2" == 'mail' ]; then
  636. check_result $E_EXISTS "Mail domain $1 exists"
  637. fi
  638. mail_user=$(echo "$mail" | cut -f 7 -d /)
  639. if [ "$mail_user" != "$user" ]; then
  640. check_result "$E_EXISTS" "Mail domain $1 exists"
  641. fi
  642. fi
  643. mail_sub=$(echo "$1" | cut -f 1 -d .)
  644. mail_nosub=$(echo "$1" | cut -f 1 -d . --complement)
  645. for mail_reserved in $(echo "mail $WEBMAIL_ALIAS"); do
  646. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_reserved.$1.conf 2> /dev/null)" ]; then
  647. if [ "$2" == 'mail' ]; then
  648. check_result "$E_EXISTS" "Required subdomain \"$mail_reserved.$1\" already exists"
  649. fi
  650. fi
  651. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_nosub.conf 2> /dev/null)" ] && [ "$mail_sub" = "$mail_reserved" ]; then
  652. if [ "$2" == 'mail' ]; then
  653. check_result "$E_INVALID" "The subdomain \"$mail_sub.\" is reserved by \"$mail_nosub\""
  654. fi
  655. fi
  656. done
  657. }
  658. # Checking mail account existence
  659. is_mail_new() {
  660. check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
  661. if [ -n "$check_acc" ]; then
  662. check_result "$E_EXISTS" "mail account $1 is already exists"
  663. fi
  664. check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf)
  665. check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
  666. if [ -n "$check_als" ]; then
  667. check_result "$E_EXISTS" "mail alias $1 is already exists"
  668. fi
  669. }
  670. # Add mail server SSL configuration
  671. add_mail_ssl_config() {
  672. # Ensure that SSL certificate directories exists
  673. if [ ! -d "$HOMEDIR/$user/conf/mail/$domain/ssl/" ]; then
  674. mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
  675. fi
  676. if [ ! -d "$HESTIA/ssl/mail" ]; then
  677. mkdir -p $HESTIA/ssl/mail
  678. fi
  679. if [ ! -d /etc/dovecot/conf.d/domains ]; then
  680. mkdir -p /etc/dovecot/conf.d/domains
  681. fi
  682. # Add certificate to Hestia user configuration data directory
  683. if [ -f "$ssl_dir/$domain.crt" ]; then
  684. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.crt
  685. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/mail.$domain.key
  686. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.pem
  687. if [ -e "$ssl_dir/$domain.ca" ]; then
  688. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/mail.$domain.ca
  689. echo >> $USER_DATA/ssl/mail.$domain.pem
  690. cat $USER_DATA/ssl/mail.$domain.ca >> $USER_DATA/ssl/mail.$domain.pem
  691. fi
  692. fi
  693. chmod 660 $USER_DATA/ssl/mail.$domain.*
  694. # Add certificate to user home directory
  695. cp -f $USER_DATA/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
  696. cp -f $USER_DATA/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
  697. cp -f $USER_DATA/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
  698. if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then
  699. cp -f $USER_DATA/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
  700. fi
  701. # Clean up dovecot configuration (if it exists)
  702. if [ -f /etc/dovecot/conf.d/domains/$domain.conf ]; then
  703. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  704. fi
  705. # Check if using custom / wildcard mail certificate
  706. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  707. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  708. if [ -n "$mail_cert_match" ]; then
  709. # Add domain SSL configuration to dovecot
  710. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  711. echo "local_name $domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  712. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  713. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  714. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  715. # Add domain SSL configuration to exim4
  716. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem $HESTIA/ssl/mail/$domain.crt
  717. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/$domain.key
  718. fi
  719. # Add domain SSL configuration to dovecot
  720. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  721. echo "local_name mail.$domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  722. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  723. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  724. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  725. # Add domain SSL configuration to exim4
  726. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem $HESTIA/ssl/mail/mail.$domain.crt
  727. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/mail.$domain.key
  728. # Set correct permissions on certificates
  729. chmod 0750 $HOMEDIR/$user/conf/mail/$domain/ssl
  730. chown -R $MAIL_USER:mail $HOMEDIR/$user/conf/mail/$domain/ssl
  731. chmod 0644 $HOMEDIR/$user/conf/mail/$domain/ssl/*
  732. chown -h $user:mail $HOMEDIR/$user/conf/mail/$domain/ssl/*
  733. chmod -R 0644 $HESTIA/ssl/mail/*
  734. chown -h $user:mail $HESTIA/ssl/mail/*
  735. }
  736. # Delete SSL support for mail domain
  737. del_mail_ssl_config() {
  738. # Check to prevent accidental removal of mismatched certificate
  739. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  740. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  741. # Remove old mail certificates
  742. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  743. # Remove dovecot configuration
  744. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  745. # Remove SSL vhost configuration
  746. rm -f $HOMEDIR/$user/conf/mail/$domain/*.*ssl.conf
  747. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  748. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  749. # Remove SSL certificates
  750. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  751. if [ -n "$mail_cert_match" ]; then
  752. rm -f $HESTIA/ssl/mail/$domain.crt $HESTIA/ssl/mail/$domain.key
  753. fi
  754. rm -f $HESTIA/ssl/mail/mail.$domain.crt $HESTIA/ssl/mail/mail.$domain.key
  755. }
  756. # Delete generated certificates from user configuration data directory
  757. del_mail_ssl_certificates() {
  758. rm -f $USER_DATA/ssl/mail.$domain.ca
  759. rm -f $USER_DATA/ssl/mail.$domain.crt
  760. rm -f $USER_DATA/ssl/mail.$domain.key
  761. rm -f $USER_DATA/ssl/mail.$domain.pem
  762. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  763. }
  764. # Add webmail config
  765. add_webmail_config() {
  766. mkdir -p "$HOMEDIR/$user/conf/mail/$domain"
  767. conf="$HOMEDIR/$user/conf/mail/$domain/$1.conf"
  768. if [[ "$2" =~ stpl$ ]]; then
  769. conf="$HOMEDIR/$user/conf/mail/$domain/$1.ssl.conf"
  770. fi
  771. domain_idn=$domain
  772. format_domain_idn
  773. ssl_crt="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt"
  774. ssl_key="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key"
  775. ssl_pem="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem"
  776. ssl_ca="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca"
  777. override_alias=""
  778. if [ "$WEBMAIL_ALIAS" != "mail" ]; then
  779. override_alias="mail.$domain"
  780. override_alias_idn="mail.$domain_idn"
  781. fi
  782. prepare_ips_for_template # prepare IPV4 and IPV6 variables for template substitution
  783. # Note: Removing or renaming template variables will lead to broken custom templates.
  784. # -If possible custom templates should be automatically upgraded to use the new format
  785. # -Alternatively a depreciation period with proper notifications should be considered
  786. cat $MAILTPL/$1/$2 \
  787. | sed -e "s|%<i4\(.*\)i4>%|$i4mark|g" \
  788. -e "s|%<i6\(.*\)i6>%|$i6mark|g" \
  789. -e "s|%web_ipv4%|$web_ipv4|g" \
  790. -e "s|%web_ipv6%|$web_ipv6|g" \
  791. -e "s|%web_ip%|$web_ip|g" \
  792. -e "s|%proxy_ipv4%|$proxy_ipv4|g" \
  793. -e "s|%proxy_ipv6%|$proxy_ipv6|g" \
  794. -e "s|%proxy_ip%|$proxy_ip|g" \
  795. -e "s|%ip%|$legacy_ip|g" \
  796. -e "s|%domain%|$WEBMAIL_ALIAS.$domain|g" \
  797. -e "s|%domain_idn%|$WEBMAIL_ALIAS.$domain_idn|g" \
  798. -e "s|%root_domain%|$domain|g" \
  799. -e "s|%alias%|$override_alias|g" \
  800. -e "s|%alias_idn%|$override_alias_idn|g" \
  801. -e "s|%alias_string%|$alias_string|g" \
  802. -e "s|%email%|info@$domain|g" \
  803. -e "s|%web_system%|$WEB_SYSTEM|g" \
  804. -e "s|%web_port%|$WEB_PORT|g" \
  805. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  806. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  807. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  808. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  809. -e "s|%proxy_port%|$PROXY_PORT|g" \
  810. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  811. -e "s/%proxy_extensions%/${PROXY_EXT//,/|}/g" \
  812. -e "s|%user%|$user|g" \
  813. -e "s|%group%|$user|g" \
  814. -e "s|%home%|$HOMEDIR|g" \
  815. -e "s|%docroot%|$docroot|g" \
  816. -e "s|%sdocroot%|$sdocroot|g" \
  817. -e "s|%ssl_crt%|$ssl_crt|g" \
  818. -e "s|%ssl_key%|$ssl_key|g" \
  819. -e "s|%ssl_pem%|$ssl_pem|g" \
  820. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  821. -e "s|%ssl_ca%|$ssl_ca|g" \
  822. > $conf
  823. chown root:$user $conf
  824. chmod 640 $conf
  825. if [[ "$2" =~ stpl$ ]]; then
  826. if [ -n "$WEB_SYSTEM" ]; then
  827. forcessl="$HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.forcessl.conf"
  828. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  829. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  830. fi
  831. if [ -n "$PROXY_SYSTEM" ]; then
  832. forcessl="$HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.forcessl.conf"
  833. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  834. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  835. fi
  836. # Add rewrite rules to force HTTPS/SSL connections
  837. if [ -n "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
  838. echo 'return 301 https://$server_name$request_uri;' > $forcessl
  839. else
  840. echo 'RewriteEngine On' > $forcessl
  841. echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl
  842. fi
  843. # Remove old configurations
  844. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" -o -name "ssl.$domain.*" -o -name "*nginx.$domain.*" \) -exec rm {} \;
  845. else
  846. if [ -n "$WEB_SYSTEM" ]; then
  847. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  848. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  849. fi
  850. if [ -n "$PROXY_SYSTEM" ]; then
  851. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  852. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  853. fi
  854. # Clear old configurations
  855. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" \) -exec rm {} \;
  856. fi
  857. }
  858. # Delete webmail support
  859. del_webmail_config() {
  860. if [ -n "$WEB_SYSTEM" ]; then
  861. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.conf
  862. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  863. fi
  864. if [ -n "$PROXY_SYSTEM" ]; then
  865. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*conf
  866. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  867. fi
  868. }
  869. # Delete SSL webmail support
  870. del_webmail_ssl_config() {
  871. if [ -n "$WEB_SYSTEM" ]; then
  872. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*ssl.conf
  873. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  874. fi
  875. if [ -n "$PROXY_SYSTEM" ]; then
  876. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*ssl.conf
  877. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  878. fi
  879. }
  880. #----------------------------------------------------------#
  881. # CMN #
  882. #----------------------------------------------------------#
  883. # Checking domain existence
  884. is_domain_new() {
  885. type=$1
  886. for object in ${2//,/ }; do
  887. if [ -n "$WEB_SYSTEM" ]; then
  888. is_web_domain_new $object $type
  889. is_web_alias_new $object $type
  890. fi
  891. if [ -n "$DNS_SYSTEM" ]; then
  892. is_dns_domain_new $object $type
  893. fi
  894. if [ -n "$MAIL_SYSTEM" ]; then
  895. is_mail_domain_new $object $type
  896. fi
  897. done
  898. }
  899. # Get domain variables
  900. get_domain_values() {
  901. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf)
  902. }
  903. #----------------------------------------------------------#
  904. # 2 Char domain name detection #
  905. #----------------------------------------------------------#
  906. is_valid_extension() {
  907. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  908. mkdir $HESTIA/data/extensions/
  909. chmod 750 $HESTIA/data/extensions/
  910. /usr/bin/wget --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache --quiet -O $HESTIA/data/extensions/public_suffix_list.dat https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat
  911. fi
  912. test_domain=$(idn2 -d "$1")
  913. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1 | /usr/bin/rev)
  914. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  915. }
  916. is_valid_2_part_extension() {
  917. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  918. mkdir $HESTIA/data/extensions/
  919. chmod 750 $HESTIA/data/extensions/
  920. /usr/bin/wget --tries=3 --timeout=15 --read-timeout=15 --waitretry=3 --no-dns-cache --quiet -O $HESTIA/data/extensions/public_suffix_list.dat https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat
  921. fi
  922. test_domain=$(idn2 -d "$1")
  923. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  924. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  925. }
  926. get_base_domain() {
  927. test_domain=$1
  928. is_valid_extension "$test_domain"
  929. if [ $? -ne 0 ]; then
  930. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  931. else
  932. is_valid_2_part_extension "$test_domain"
  933. if [ $? -ne 0 ]; then
  934. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  935. else
  936. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  937. partdomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 3 | /usr/bin/rev)
  938. basedomain="$partdomain.$extension"
  939. fi
  940. fi
  941. }
  942. is_base_domain_owner() {
  943. for object in ${1//,/ }; do
  944. if [ "$object" != "none" ]; then
  945. get_base_domain $object
  946. web=$(grep -F -H -h "DOMAIN='$basedomain'" $HESTIA/data/users/*/web.conf)
  947. if [ "$ENFORCE_SUBDOMAIN_OWNERSHIP" = "yes" ]; then
  948. if [ -n "$web" ]; then
  949. parse_object_kv_list "$web"
  950. if [ -z "$ALLOW_USERS" ] || [ "$ALLOW_USERS" != "yes" ]; then
  951. # Don't care if $basedomain all ready exists only if the owner is of the base domain is the current user
  952. check=$(is_domain_new "" $basedomain)
  953. if [ $? -ne 0 ]; then
  954. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  955. exit 4
  956. fi
  957. fi
  958. else
  959. check=$(is_domain_new "" "$basedomain")
  960. if [ $? -ne 0 ]; then
  961. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  962. exit 4
  963. fi
  964. fi
  965. fi
  966. fi
  967. done
  968. }