domain.sh 34 KB

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