domain.sh 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  451. -key $ssl_dir/$domain.key >> /dev/null 2>&1 &
  452. pid=$!
  453. sleep 0.5
  454. disown &> /dev/null
  455. kill $pid &> /dev/null
  456. check_result $? "ssl certificate key pair is not valid" $E_INVALID
  457. }
  458. #----------------------------------------------------------#
  459. # DNS #
  460. #----------------------------------------------------------#
  461. # DNS template check
  462. is_dns_template_valid() {
  463. if [ ! -e "$DNSTPL/$1.tpl" ]; then
  464. check_result "$E_NOTEXIST" "$1 dns template doesn't exist"
  465. fi
  466. }
  467. # DNS domain existence check
  468. is_dns_domain_new() {
  469. dns=$(ls $HESTIA/data/users/*/dns/$1.conf 2> /dev/null)
  470. if [ -n "$dns" ]; then
  471. if [ "$2" == 'dns' ]; then
  472. check_result "$E_EXISTS" "DNS domain $1 exists"
  473. fi
  474. dns_user=$(echo "$dns" | cut -f 7 -d /)
  475. if [ "$dns_user" != "$user" ]; then
  476. check_result "$E_EXISTS" "DNS domain $1 exists"
  477. fi
  478. fi
  479. }
  480. # Update domain zone
  481. update_domain_zone() {
  482. domain_param=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
  483. parse_object_kv_list "$domain_param"
  484. local zone_ttl="$TTL"
  485. SOA=$(idn2 --quiet "$SOA")
  486. if [ -z "$SERIAL" ]; then
  487. SERIAL=$(date +'%Y%m%d01')
  488. fi
  489. if [[ "$domain" = *[![:ascii:]]* ]]; then
  490. domain_idn=$(idn2 --quiet $domain)
  491. else
  492. domain_idn=$domain
  493. fi
  494. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  495. echo "\$TTL $TTL
  496. @ IN SOA $SOA. root.$domain_idn. (
  497. $SERIAL
  498. 7200
  499. 3600
  500. 1209600
  501. 180 )
  502. " > $zn_conf
  503. fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
  504. while read line; do
  505. unset TTL
  506. IFS=$'\n'
  507. for key in $(echo $line | sed "s/' /'\n/g"); do
  508. eval ${key%%=*}="${key#*=}"
  509. done
  510. # inherit zone TTL if record lacks explicit TTL value
  511. [ -z "$TTL" ] && TTL="$zone_ttl"
  512. RECORD=$(idn2 --quiet "$RECORD")
  513. if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
  514. VALUE=$(idn2 --quiet "$VALUE")
  515. fi
  516. if [ "$TYPE" = 'TXT' ]; then
  517. txtlength=${#VALUE}
  518. if [ $txtlength -gt 255 ]; then
  519. already_chunked=0
  520. if [[ $VALUE == *"\" \""* ]] || [[ $VALUE == *"\"\""* ]]; then
  521. already_chunked=1
  522. fi
  523. if [ $already_chunked -eq 0 ]; then
  524. if [[ ${VALUE:0:1} = '"' ]]; then
  525. txtlength=$(($txtlength - 2))
  526. VALUE=${VALUE:1:txtlength}
  527. fi
  528. VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
  529. fi
  530. fi
  531. fi
  532. eval echo -e "\"$fields\"" | sed "s/%quote%/'/g" >> $zn_conf
  533. done < $USER_DATA/dns/$domain.conf
  534. }
  535. # Update zone serial
  536. update_domain_serial() {
  537. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  538. if [ -e $zn_conf ]; then
  539. zn_serial=$(head $zn_conf | grep 'SOA' -A1 | tail -n 1 | sed "s/ //g")
  540. s_date=$(echo ${zn_serial:0:8})
  541. c_date=$(date +'%Y%m%d')
  542. if [ "$s_date" == "$c_date" ]; then
  543. cur_value=$(echo ${zn_serial:8})
  544. new_value=$(expr $cur_value + 1)
  545. len_value=$(expr length $new_value)
  546. if [ 1 -eq "$len_value" ]; then
  547. new_value='0'$new_value
  548. fi
  549. serial="$c_date""$new_value"
  550. else
  551. serial="$(date +'%Y%m%d01')"
  552. fi
  553. else
  554. serial="$(date +'%Y%m%d01')"
  555. fi
  556. add_object_key "dns" 'DOMAIN' "$domain" 'SERIAL' 'RECORDS'
  557. update_object_value 'dns' 'DOMAIN' "$domain" '$SERIAL' "$serial"
  558. }
  559. # Get next DNS record ID
  560. get_next_dnsrecord() {
  561. if [ -z "$id" ]; then
  562. curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' \
  563. | sort -n | tail -n1)
  564. id="$((curr_str + 1))"
  565. fi
  566. }
  567. # Sort DNS records
  568. sort_dns_records() {
  569. conf="$USER_DATA/dns/$domain.conf"
  570. cat $conf | sort -n -k 2 -t \' > $conf.tmp
  571. mv -f $conf.tmp $conf
  572. }
  573. # Check if this is a last record
  574. is_dns_record_critical() {
  575. str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
  576. parse_object_kv_list "$str"
  577. if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
  578. records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf | wc -l)
  579. if [ $records -le 1 ]; then
  580. echo "Error: at least one $TYPE record should remain active"
  581. log_event "$E_INVALID" "$ARGUMENTS"
  582. exit "$E_INVALID"
  583. fi
  584. fi
  585. }
  586. # Check if dns record is valid
  587. is_dns_fqnd() {
  588. t=$1
  589. r=$2
  590. fqdn_type=$(echo $t | grep "^NS\|CNAME\|MX\|PTR\|SRV")
  591. tree_length=3
  592. if [[ $t = 'CNAME' || $t = 'MX' || $t = 'PTR' ]]; then
  593. tree_length=2
  594. fi
  595. if [ -n "$fqdn_type" ]; then
  596. dots=$(echo $dvalue | grep -o "\." | wc -l)
  597. if [ "$dots" -lt "$tree_length" ]; then
  598. r=$(echo $r | sed -e "s/\.$//")
  599. msg="$t record $r should be a fully qualified domain name (FQDN)"
  600. echo "Error: $msg"
  601. log_event "$E_INVALID" "$ARGUMENTS"
  602. exit "$E_INVALID"
  603. fi
  604. fi
  605. }
  606. # Validate nameserver
  607. is_dns_nameserver_valid() {
  608. d=$1
  609. t=$2
  610. r=$3
  611. if [ "$t" = 'NS' ]; then
  612. remote=$(echo $r | grep ".$domain.$")
  613. if [ -n "$remote" ]; then
  614. zone=$USER_DATA/dns/$d.conf
  615. a_record=${r%.$d.}
  616. n_record=$(grep "RECORD='$a_record'" $zone | grep "TYPE='A'")
  617. if [ -z "$n_record" ]; then
  618. check_result "$E_NOTEXIST" "IN A $a_record.$d does not exist"
  619. fi
  620. fi
  621. fi
  622. }
  623. #----------------------------------------------------------#
  624. # MAIL #
  625. #----------------------------------------------------------#
  626. # Mail domain existence check
  627. is_mail_domain_new() {
  628. mail=$(ls $HESTIA/data/users/*/mail/$1.conf 2> /dev/null)
  629. if [ -n "$mail" ]; then
  630. if [ "$2" == 'mail' ]; then
  631. check_result $E_EXISTS "Mail domain $1 exists"
  632. fi
  633. mail_user=$(echo "$mail" | cut -f 7 -d /)
  634. if [ "$mail_user" != "$user" ]; then
  635. check_result "$E_EXISTS" "Mail domain $1 exists"
  636. fi
  637. fi
  638. mail_sub=$(echo "$1" | cut -f 1 -d .)
  639. mail_nosub=$(echo "$1" | cut -f 1 -d . --complement)
  640. for mail_reserved in $(echo "mail $WEBMAIL_ALIAS"); do
  641. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_reserved.$1.conf 2> /dev/null)" ]; then
  642. if [ "$2" == 'mail' ]; then
  643. check_result "$E_EXISTS" "Required subdomain \"$mail_reserved.$1\" already exists"
  644. fi
  645. fi
  646. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_nosub.conf 2> /dev/null)" ] && [ "$mail_sub" = "$mail_reserved" ]; then
  647. if [ "$2" == 'mail' ]; then
  648. check_result "$E_INVALID" "The subdomain \"$mail_sub.\" is reserved by \"$mail_nosub\""
  649. fi
  650. fi
  651. done
  652. }
  653. # Checking mail account existance
  654. is_mail_new() {
  655. check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
  656. if [ -n "$check_acc" ]; then
  657. check_result "$E_EXISTS" "mail account $1 is already exists"
  658. fi
  659. check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf)
  660. check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
  661. if [ -n "$check_als" ]; then
  662. check_result "$E_EXISTS" "mail alias $1 is already exists"
  663. fi
  664. }
  665. # Add mail server SSL configuration
  666. add_mail_ssl_config() {
  667. # Ensure that SSL certificate directories exists
  668. if [ ! -d "$HOMEDIR/$user/conf/mail/$domain/ssl/" ]; then
  669. mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
  670. fi
  671. if [ ! -d "$HESTIA/ssl/mail" ]; then
  672. mkdir -p $HESTIA/ssl/mail
  673. fi
  674. if [ ! -d /etc/dovecot/conf.d/domains ]; then
  675. mkdir -p /etc/dovecot/conf.d/domains
  676. fi
  677. # Add certificate to Hestia user configuration data directory
  678. if [ -f "$ssl_dir/$domain.crt" ]; then
  679. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.crt
  680. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/mail.$domain.key
  681. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.pem
  682. if [ -e "$ssl_dir/$domain.ca" ]; then
  683. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/mail.$domain.ca
  684. echo >> $USER_DATA/ssl/mail.$domain.pem
  685. cat $USER_DATA/ssl/mail.$domain.ca >> $USER_DATA/ssl/mail.$domain.pem
  686. fi
  687. fi
  688. chmod 660 $USER_DATA/ssl/mail.$domain.*
  689. # Add certificate to user home directory
  690. cp -f $USER_DATA/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
  691. cp -f $USER_DATA/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
  692. cp -f $USER_DATA/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
  693. if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then
  694. cp -f $USER_DATA/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
  695. fi
  696. # Clean up dovecot configuration (if it exists)
  697. if [ -f /etc/dovecot/conf.d/domains/$domain.conf ]; then
  698. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  699. fi
  700. # Check if using custom / wildcard mail certificate
  701. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  702. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  703. if [ -n "$mail_cert_match" ]; then
  704. # Add domain SSL configuration to dovecot
  705. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  706. echo "local_name $domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  707. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  708. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  709. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  710. # Add domain SSL configuration to exim4
  711. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem $HESTIA/ssl/mail/$domain.crt
  712. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/$domain.key
  713. fi
  714. # Add domain SSL configuration to dovecot
  715. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  716. echo "local_name mail.$domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  717. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  718. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  719. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  720. # Add domain SSL configuration to exim4
  721. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem $HESTIA/ssl/mail/mail.$domain.crt
  722. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/mail.$domain.key
  723. # Set correct permissions on certificates
  724. chmod 0750 $HOMEDIR/$user/conf/mail/$domain/ssl
  725. chown -R $MAIL_USER:mail $HOMEDIR/$user/conf/mail/$domain/ssl
  726. chmod 0644 $HOMEDIR/$user/conf/mail/$domain/ssl/*
  727. chown -h $user:mail $HOMEDIR/$user/conf/mail/$domain/ssl/*
  728. chmod -R 0644 $HESTIA/ssl/mail/*
  729. chown -h $user:mail $HESTIA/ssl/mail/*
  730. }
  731. # Delete SSL support for mail domain
  732. del_mail_ssl_config() {
  733. # Check to prevent accidental removal of mismatched certificate
  734. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  735. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  736. # Remove old mail certificates
  737. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  738. # Remove dovecot configuration
  739. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  740. # Remove SSL vhost configuration
  741. rm -f $HOMEDIR/$user/conf/mail/$domain/*.*ssl.conf
  742. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  743. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  744. # Remove SSL certificates
  745. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  746. if [ -n "$mail_cert_match" ]; then
  747. rm -f $HESTIA/ssl/mail/$domain.crt $HESTIA/ssl/mail/$domain.key
  748. fi
  749. rm -f $HESTIA/ssl/mail/mail.$domain.crt $HESTIA/ssl/mail/mail.$domain.key
  750. }
  751. # Delete generated certificates from user configuration data directory
  752. del_mail_ssl_certificates() {
  753. rm -f $USER_DATA/ssl/mail.$domain.ca
  754. rm -f $USER_DATA/ssl/mail.$domain.crt
  755. rm -f $USER_DATA/ssl/mail.$domain.key
  756. rm -f $USER_DATA/ssl/mail.$domain.pem
  757. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  758. }
  759. # Add webmail config
  760. add_webmail_config() {
  761. mkdir -p "$HOMEDIR/$user/conf/mail/$domain"
  762. conf="$HOMEDIR/$user/conf/mail/$domain/$1.conf"
  763. if [[ "$2" =~ stpl$ ]]; then
  764. conf="$HOMEDIR/$user/conf/mail/$domain/$1.ssl.conf"
  765. fi
  766. domain_idn=$domain
  767. format_domain_idn
  768. ssl_crt="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt"
  769. ssl_key="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key"
  770. ssl_pem="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem"
  771. ssl_ca="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca"
  772. override_alias=""
  773. if [ "$WEBMAIL_ALIAS" != "mail" ]; then
  774. override_alias="mail.$domain"
  775. override_alias_idn="mail.$domain_idn"
  776. fi
  777. prepare_ips_for_template # prepare IPV4 and IPV6 variables for template substitution
  778. # Note: Removing or renaming template variables will lead to broken custom templates.
  779. # -If possible custom templates should be automatically upgraded to use the new format
  780. # -Alternatively a depreciation period with proper notifications should be considered
  781. cat $MAILTPL/$1/$2 \
  782. | sed -e "s|%<i4\(.*\)i4>%|$i4mark|g" \
  783. -e "s|%<i6\(.*\)i6>%|$i6mark|g" \
  784. -e "s|%web_ipv4%|$web_ipv4|g" \
  785. -e "s|%web_ipv6%|$web_ipv6|g" \
  786. -e "s|%web_ip%|$web_ip|g" \
  787. -e "s|%proxy_ipv4%|$proxy_ipv4|g" \
  788. -e "s|%proxy_ipv6%|$proxy_ipv6|g" \
  789. -e "s|%proxy_ip%|$proxy_ip|g" \
  790. -e "s|%ip%|$legacy_ip|g" \
  791. -e "s|%domain%|$WEBMAIL_ALIAS.$domain|g" \
  792. -e "s|%domain_idn%|$WEBMAIL_ALIAS.$domain_idn|g" \
  793. -e "s|%root_domain%|$domain|g" \
  794. -e "s|%alias%|$override_alias|g" \
  795. -e "s|%alias_idn%|$override_alias_idn|g" \
  796. -e "s|%alias_string%|$alias_string|g" \
  797. -e "s|%email%|info@$domain|g" \
  798. -e "s|%web_system%|$WEB_SYSTEM|g" \
  799. -e "s|%web_port%|$WEB_PORT|g" \
  800. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  801. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  802. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  803. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  804. -e "s|%proxy_port%|$PROXY_PORT|g" \
  805. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  806. -e "s/%proxy_extensions%/${PROXY_EXT//,/|}/g" \
  807. -e "s|%user%|$user|g" \
  808. -e "s|%group%|$user|g" \
  809. -e "s|%home%|$HOMEDIR|g" \
  810. -e "s|%docroot%|$docroot|g" \
  811. -e "s|%sdocroot%|$sdocroot|g" \
  812. -e "s|%ssl_crt%|$ssl_crt|g" \
  813. -e "s|%ssl_key%|$ssl_key|g" \
  814. -e "s|%ssl_pem%|$ssl_pem|g" \
  815. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  816. -e "s|%ssl_ca%|$ssl_ca|g" \
  817. > $conf
  818. chown root:$user $conf
  819. chmod 640 $conf
  820. if [[ "$2" =~ stpl$ ]]; then
  821. if [ -n "$WEB_SYSTEM" ]; then
  822. forcessl="$HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.forcessl.conf"
  823. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  824. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  825. fi
  826. if [ -n "$PROXY_SYSTEM" ]; then
  827. forcessl="$HOMEDIR/$user/conf/mail/$domain/$PROXY_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. # Add rewrite rules to force HTTPS/SSL connections
  832. if [ -n "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
  833. echo 'return 301 https://$server_name$request_uri;' > $forcessl
  834. else
  835. echo 'RewriteEngine On' > $forcessl
  836. echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl
  837. fi
  838. # Remove old configurations
  839. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" -o -name "ssl.$domain.*" -o -name "*nginx.$domain.*" \) -exec rm {} \;
  840. else
  841. if [ -n "$WEB_SYSTEM" ]; then
  842. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  843. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  844. fi
  845. if [ -n "$PROXY_SYSTEM" ]; then
  846. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  847. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  848. fi
  849. # Clear old configurations
  850. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" \) -exec rm {} \;
  851. fi
  852. }
  853. # Delete webmail support
  854. del_webmail_config() {
  855. if [ -n "$WEB_SYSTEM" ]; then
  856. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.conf
  857. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  858. fi
  859. if [ -n "$PROXY_SYSTEM" ]; then
  860. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*conf
  861. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  862. fi
  863. }
  864. # Delete SSL webmail support
  865. del_webmail_ssl_config() {
  866. if [ -n "$WEB_SYSTEM" ]; then
  867. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*ssl.conf
  868. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  869. fi
  870. if [ -n "$PROXY_SYSTEM" ]; then
  871. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*ssl.conf
  872. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  873. fi
  874. }
  875. #----------------------------------------------------------#
  876. # CMN #
  877. #----------------------------------------------------------#
  878. # Checking domain existance
  879. is_domain_new() {
  880. type=$1
  881. for object in ${2//,/ }; do
  882. if [ -n "$WEB_SYSTEM" ]; then
  883. is_web_domain_new $object $type
  884. is_web_alias_new $object $type
  885. fi
  886. if [ -n "$DNS_SYSTEM" ]; then
  887. is_dns_domain_new $object $type
  888. fi
  889. if [ -n "$MAIL_SYSTEM" ]; then
  890. is_mail_domain_new $object $type
  891. fi
  892. done
  893. }
  894. # Get domain variables
  895. get_domain_values() {
  896. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf)
  897. }
  898. #----------------------------------------------------------#
  899. # 2 Char domain name detection #
  900. #----------------------------------------------------------#
  901. is_valid_extension() {
  902. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  903. mkdir $HESTIA/data/extensions/
  904. chmod 750 $HESTIA/data/extensions/
  905. /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
  906. fi
  907. test_domain=$(idn2 -d "$1")
  908. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1 | /usr/bin/rev)
  909. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  910. }
  911. is_valid_2_part_extension() {
  912. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  913. mkdir $HESTIA/data/extensions/
  914. chmod 750 $HESTIA/data/extensions/
  915. /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
  916. fi
  917. test_domain=$(idn2 -d "$1")
  918. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  919. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  920. }
  921. get_base_domain() {
  922. test_domain=$1
  923. is_valid_extension "$test_domain"
  924. if [ $? -ne 0 ]; then
  925. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  926. else
  927. is_valid_2_part_extension "$test_domain"
  928. if [ $? -ne 0 ]; then
  929. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  930. else
  931. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  932. partdomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 3 | /usr/bin/rev)
  933. basedomain="$partdomain.$extension"
  934. fi
  935. fi
  936. }
  937. is_base_domain_owner() {
  938. for object in ${1//,/ }; do
  939. if [ "$object" != "none" ]; then
  940. get_base_domain $object
  941. web=$(grep -F -H -h "DOMAIN='$basedomain'" $HESTIA/data/users/*/web.conf)
  942. if [ "$ENFORCE_SUBDOMAIN_OWNERSHIP" = "yes" ]; then
  943. if [ -n "$web" ]; then
  944. parse_object_kv_list "$web"
  945. if [ -z "$ALLOW_USERS" ] || [ "$ALLOW_USERS" != "yes" ]; then
  946. # Don't care if $basedomain all ready exists only if the owner is of the base domain is the current user
  947. check=$(is_domain_new "" $basedomain)
  948. if [ $? -ne 0 ]; then
  949. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  950. exit 4
  951. fi
  952. fi
  953. else
  954. check=$(is_domain_new "" "$basedomain")
  955. if [ $? -ne 0 ]; then
  956. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  957. exit 4
  958. fi
  959. fi
  960. fi
  961. fi
  962. done
  963. }