domain.sh 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. fi
  210. }
  211. # Add web config
  212. add_web_config() {
  213. # Check if folder already exists
  214. if [ ! -d "$HOMEDIR/$user/conf/web/$domain" ]; then
  215. mkdir -p "$HOMEDIR/$user/conf/web/$domain/"
  216. fi
  217. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  218. if [[ "$2" =~ stpl$ ]]; then
  219. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  220. fi
  221. domain_idn=$domain
  222. format_domain_idn
  223. WEBTPL_LOCATION="$WEBTPL/$1"
  224. if [ "$1" != "$PROXY_SYSTEM" ] && [ -n "$WEB_BACKEND" ] && [ -d "$WEBTPL_LOCATION/$WEB_BACKEND" ]; then
  225. if [ -f "$WEBTPL_LOCATION/$WEB_BACKEND/$2" ]; then
  226. # check for backend specific template
  227. WEBTPL_LOCATION="$WEBTPL/$1/$WEB_BACKEND"
  228. fi
  229. fi
  230. # Note: Removing or renaming template variables will lead to broken custom templates.
  231. # -If possible custom templates should be automatically upgraded to use the new format
  232. # -Alternatively a depreciation period with proper notifications should be considered
  233. cat "${WEBTPL_LOCATION}/$2" \
  234. | sed -e "s|%ip%|$local_ip|g" \
  235. -e "s|%domain%|$domain|g" \
  236. -e "s|%domain_idn%|$domain_idn|g" \
  237. -e "s|%alias%|${aliases//,/ }|g" \
  238. -e "s|%alias_idn%|${aliases_idn//,/ }|g" \
  239. -e "s|%alias_string%|$alias_string|g" \
  240. -e "s|%email%|info@$domain|g" \
  241. -e "s|%web_system%|$WEB_SYSTEM|g" \
  242. -e "s|%web_port%|$WEB_PORT|g" \
  243. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  244. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  245. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  246. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  247. -e "s|%proxy_port%|$PROXY_PORT|g" \
  248. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  249. -e "s/%proxy_extentions%/${PROXY_EXT//,/|}/g" \
  250. -e "s/%proxy_extensions%/${PROXY_EXT//,/|}/g" \
  251. -e "s|%user%|$user|g" \
  252. -e "s|%group%|$user|g" \
  253. -e "s|%home%|$HOMEDIR|g" \
  254. -e "s|%docroot%|$docroot|g" \
  255. -e "s|%sdocroot%|$sdocroot|g" \
  256. -e "s|%ssl_crt%|$ssl_crt|g" \
  257. -e "s|%ssl_key%|$ssl_key|g" \
  258. -e "s|%ssl_pem%|$ssl_pem|g" \
  259. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  260. -e "s|%ssl_ca%|$ssl_ca|g" \
  261. > $conf
  262. chown root:$user $conf
  263. chmod 640 $conf
  264. if [[ "$2" =~ stpl$ ]]; then
  265. rm -f /etc/$1/conf.d/domains/$domain.ssl.conf
  266. ln -s $conf /etc/$1/conf.d/domains/$domain.ssl.conf
  267. # Rename/Move extra SSL config files
  268. find=$(find $HOMEDIR/$user/conf/web/*.$domain.org* 2> /dev/null)
  269. for f in $find; do
  270. if [[ $f =~ .*/s(nginx|apache2)\.$domain\.conf(.*) ]]; then
  271. ServerType="${BASH_REMATCH[1]}"
  272. CustomConfigName="${BASH_REMATCH[2]}"
  273. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  274. rm -f "$f"
  275. continue
  276. fi
  277. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.ssl.conf_old$CustomConfigName"
  278. fi
  279. done
  280. else
  281. rm -f /etc/$1/conf.d/domains/$domain.conf
  282. ln -s $conf /etc/$1/conf.d/domains/$domain.conf
  283. # Rename/Move extra config files
  284. find=$(find $HOMEDIR/$user/conf/web/*.$domain.org* 2> /dev/null)
  285. for f in $find; do
  286. if [[ $f =~ .*/(nginx|apache2)\.$domain\.conf(.*) ]]; then
  287. ServerType="${BASH_REMATCH[1]}"
  288. CustomConfigName="${BASH_REMATCH[2]}"
  289. if [ "$CustomConfigName" = "_letsencrypt" ]; then
  290. rm -f "$f"
  291. continue
  292. fi
  293. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.conf_old$CustomConfigName"
  294. elif [[ $f =~ .*/forcessl\.(nginx|apache2)\.$domain\.conf ]]; then
  295. ServerType="${BASH_REMATCH[1]}"
  296. mv "$f" "$HOMEDIR/$user/conf/web/$domain/$ServerType.forcessl.conf"
  297. fi
  298. done
  299. fi
  300. trigger="${2/.*pl/.sh}"
  301. if [ -x "${WEBTPL_LOCATION}/$trigger" ]; then
  302. $WEBTPL_LOCATION/$trigger \
  303. $user $domain $local_ip $HOMEDIR \
  304. $HOMEDIR/$user/web/$domain/public_html
  305. fi
  306. }
  307. # Get config top and bottom line number
  308. get_web_config_lines() {
  309. tpl_lines=$(egrep -ni "name %domain_idn%" $1 | grep -w %domain_idn%)
  310. tpl_lines=$(echo "$tpl_lines" | cut -f 1 -d :)
  311. tpl_last_line=$(wc -l $1 | cut -f 1 -d ' ')
  312. if [ -z "$tpl_lines" ]; then
  313. check_result $E_PARSING "can't parse template $1"
  314. fi
  315. domain_idn=$domain
  316. format_domain_idn
  317. vhost_lines=$(grep -niF "name $domain_idn" $2)
  318. vhost_lines=$(echo "$vhost_lines" | egrep "$domain_idn($| |;)")
  319. vhost_lines=$(echo "$vhost_lines" | cut -f 1 -d :)
  320. if [ -z "$vhost_lines" ]; then
  321. check_result $E_PARSING "can't parse config $2"
  322. fi
  323. top_line=$((vhost_lines + 1 - tpl_lines))
  324. bottom_line=$((top_line - 1 + tpl_last_line))
  325. multi=$(sed -n "$top_line,$bottom_line p" $2 | grep ServerAlias | wc -l)
  326. if [ "$multi" -ge 2 ]; then
  327. bottom_line=$((bottom_line + multi - 1))
  328. fi
  329. }
  330. # Replace web config
  331. replace_web_config() {
  332. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  333. if [[ "$2" =~ stpl$ ]]; then
  334. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  335. fi
  336. if [ -e "$conf" ]; then
  337. sed -i "s|$old|$new|g" $conf
  338. fi
  339. }
  340. # Delete web configuration
  341. del_web_config() {
  342. conf="$HOMEDIR/$user/conf/web/$domain/$1.conf"
  343. local confname="$domain.conf"
  344. if [[ "$2" =~ stpl$ ]]; then
  345. conf="$HOMEDIR/$user/conf/web/$domain/$1.ssl.conf"
  346. confname="$domain.ssl.conf"
  347. fi
  348. # Clean up legacy configuration files
  349. if [ ! -e "$conf" ]; then
  350. local legacyconf="$HOMEDIR/$user/conf/web/$1.conf"
  351. if [[ "$2" =~ stpl$ ]]; then
  352. legacyconf="$HOMEDIR/$user/conf/web/s$1.conf"
  353. fi
  354. rm -f $legacyconf
  355. # Remove old global includes file
  356. rm -f /etc/$1/conf.d/hestia.conf
  357. fi
  358. # Remove domain configuration files and clean up symbolic links
  359. rm -f "$conf"
  360. if [ -n "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" = "$1" ]; then
  361. rm -f "/etc/$WEB_SYSTEM/conf.d/domains/$confname"
  362. fi
  363. if [ -n "$PROXY_SYSTEM" ] && [ "$PROXY_SYSTEM" = "$1" ]; then
  364. rm -f "/etc/$PROXY_SYSTEM/conf.d/domains/$confname"
  365. fi
  366. }
  367. # SSL certificate verification
  368. is_web_domain_cert_valid() {
  369. if [ ! -e "$ssl_dir/$domain.crt" ]; then
  370. check_result "$E_NOTEXIST" "$ssl_dir/$domain.crt not found"
  371. fi
  372. if [ ! -e "$ssl_dir/$domain.key" ]; then
  373. check_result "$E_NOTEXIST" "$ssl_dir/$domain.key not found"
  374. fi
  375. crt_vrf=$(openssl verify $ssl_dir/$domain.crt 2>&1)
  376. if [ -n "$(echo $crt_vrf | grep 'unable to load')" ]; then
  377. check_result "$E_INVALID" "SSL Certificate is not valid"
  378. fi
  379. if [ -n "$(echo $crt_vrf | grep 'unable to get local issuer')" ]; then
  380. if [ ! -e "$ssl_dir/$domain.ca" ]; then
  381. check_result "$E_NOTEXIST" "Certificate Authority not found"
  382. fi
  383. fi
  384. if [ -e "$ssl_dir/$domain.ca" ]; then
  385. s1=$(openssl x509 -text -in $ssl_dir/$domain.crt 2> /dev/null)
  386. s1=$(echo "$s1" | grep Issuer | awk -F = '{print $6}' | head -n1)
  387. s2=$(openssl x509 -text -in $ssl_dir/$domain.ca 2> /dev/null)
  388. s2=$(echo "$s2" | grep Subject | awk -F = '{print $6}' | head -n1)
  389. if [ "$s1" != "$s2" ]; then
  390. check_result "$E_NOTEXIST" "SSL intermediate chain is not valid"
  391. fi
  392. fi
  393. key_vrf=$(grep 'PRIVATE KEY' $ssl_dir/$domain.key | wc -l)
  394. if [ "$key_vrf" -ne 2 ]; then
  395. check_result "$E_INVALID" "SSL Key is not valid"
  396. fi
  397. if [ -n "$(grep 'ENCRYPTED' $ssl_dir/$domain.key)" ]; then
  398. check_result "$E_FORBIDEN" "SSL Key is protected (remove pass_phrase)"
  399. fi
  400. if pgrep -x "openssl" > /dev/null; then
  401. pkill openssl
  402. fi
  403. openssl s_server -quiet -cert $ssl_dir/$domain.crt \
  404. -key $ssl_dir/$domain.key >> /dev/null 2>&1 &
  405. pid=$!
  406. sleep 0.5
  407. disown &> /dev/null
  408. kill $pid &> /dev/null
  409. check_result $? "ssl certificate key pair is not valid" $E_INVALID
  410. }
  411. #----------------------------------------------------------#
  412. # DNS #
  413. #----------------------------------------------------------#
  414. # DNS template check
  415. is_dns_template_valid() {
  416. if [ ! -e "$DNSTPL/$1.tpl" ]; then
  417. check_result "$E_NOTEXIST" "$1 dns template doesn't exist"
  418. fi
  419. }
  420. # DNS domain existence check
  421. is_dns_domain_new() {
  422. dns=$(ls $HESTIA/data/users/*/dns/$1.conf 2> /dev/null)
  423. if [ -n "$dns" ]; then
  424. if [ "$2" == 'dns' ]; then
  425. check_result "$E_EXISTS" "DNS domain $1 exists"
  426. fi
  427. dns_user=$(echo "$dns" | cut -f 7 -d /)
  428. if [ "$dns_user" != "$user" ]; then
  429. check_result "$E_EXISTS" "DNS domain $1 exists"
  430. fi
  431. fi
  432. }
  433. # Update domain zone
  434. update_domain_zone() {
  435. domain_param=$(grep "DOMAIN='$domain'" $USER_DATA/dns.conf)
  436. parse_object_kv_list "$domain_param"
  437. local zone_ttl="$TTL"
  438. SOA=$(idn2 --quiet "$SOA")
  439. if [ -z "$SERIAL" ]; then
  440. SERIAL=$(date +'%Y%m%d01')
  441. fi
  442. if [[ "$domain" = *[![:ascii:]]* ]]; then
  443. domain_idn=$(idn2 --quiet $domain)
  444. else
  445. domain_idn=$domain
  446. fi
  447. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  448. echo "\$TTL $TTL
  449. @ IN SOA $SOA. root.$domain_idn. (
  450. $SERIAL
  451. 7200
  452. 3600
  453. 1209600
  454. 180 )
  455. " > $zn_conf
  456. fields='$RECORD\t$TTL\tIN\t$TYPE\t$PRIORITY\t$VALUE'
  457. while read line; do
  458. unset TTL
  459. IFS=$'\n'
  460. for key in $(echo $line | sed "s/' /'\n/g"); do
  461. eval ${key%%=*}="${key#*=}"
  462. done
  463. # inherit zone TTL if record lacks explicit TTL value
  464. [ -z "$TTL" ] && TTL="$zone_ttl"
  465. RECORD=$(idn2 --quiet "$RECORD")
  466. if [ "$TYPE" = 'CNAME' ] || [ "$TYPE" = 'MX' ]; then
  467. VALUE=$(idn2 --quiet "$VALUE")
  468. fi
  469. if [ "$TYPE" = 'TXT' ]; then
  470. txtlength=${#VALUE}
  471. if [ $txtlength -gt 255 ]; then
  472. already_chunked=0
  473. if [[ $VALUE == *"\" \""* ]] || [[ $VALUE == *"\"\""* ]]; then
  474. already_chunked=1
  475. fi
  476. if [ $already_chunked -eq 0 ]; then
  477. if [[ ${VALUE:0:1} = '"' ]]; then
  478. txtlength=$(($txtlength - 2))
  479. VALUE=${VALUE:1:txtlength}
  480. fi
  481. VALUE=$(echo $VALUE | fold -w 255 | xargs -I '$' echo -n '"$"')
  482. fi
  483. fi
  484. fi
  485. if [ "$SUSPENDED" != 'yes' ]; then
  486. eval echo -e "\"$fields\"" | sed "s/%quote%/'/g" >> $zn_conf
  487. fi
  488. done < $USER_DATA/dns/$domain.conf
  489. }
  490. # Update zone serial
  491. update_domain_serial() {
  492. zn_conf="$HOMEDIR/$user/conf/dns/$domain.db"
  493. if [ -e $zn_conf ]; then
  494. zn_serial=$(head $zn_conf | grep 'SOA' -A1 | tail -n 1 | sed "s/ //g")
  495. s_date=$(echo ${zn_serial:0:8})
  496. c_date=$(date +'%Y%m%d')
  497. if [ "$s_date" == "$c_date" ]; then
  498. cur_value=$(echo ${zn_serial:8})
  499. new_value=$(expr $cur_value + 1)
  500. len_value=$(expr length $new_value)
  501. if [ 1 -eq "$len_value" ]; then
  502. new_value='0'$new_value
  503. fi
  504. serial="$c_date""$new_value"
  505. else
  506. serial="$(date +'%Y%m%d01')"
  507. fi
  508. else
  509. serial="$(date +'%Y%m%d01')"
  510. fi
  511. add_object_key "dns" 'DOMAIN' "$domain" 'SERIAL' 'RECORDS'
  512. update_object_value 'dns' 'DOMAIN' "$domain" '$SERIAL' "$serial"
  513. }
  514. # Get next DNS record ID
  515. get_next_dnsrecord() {
  516. if [ -z "$id" ]; then
  517. curr_str=$(grep "ID=" $USER_DATA/dns/$domain.conf | cut -f 2 -d \' \
  518. | sort -n | tail -n1)
  519. id="$((curr_str + 1))"
  520. fi
  521. }
  522. # Sort DNS records
  523. sort_dns_records() {
  524. conf="$USER_DATA/dns/$domain.conf"
  525. cat $conf | sort -n -k 2 -t \' > $conf.tmp
  526. mv -f $conf.tmp $conf
  527. }
  528. # Check if this is a last record
  529. is_dns_record_critical() {
  530. str=$(grep "ID='$id'" $USER_DATA/dns/$domain.conf)
  531. parse_object_kv_list "$str"
  532. if [ "$TYPE" = 'A' ] || [ "$TYPE" = 'NS' ]; then
  533. records=$(grep "TYPE='$TYPE'" $USER_DATA/dns/$domain.conf | wc -l)
  534. if [ $records -le 1 ]; then
  535. echo "Error: at least one $TYPE record should remain active"
  536. log_event "$E_INVALID" "$ARGUMENTS"
  537. exit "$E_INVALID"
  538. fi
  539. fi
  540. }
  541. # Check if dns record is valid
  542. is_dns_fqnd() {
  543. t=$1
  544. r=$2
  545. fqdn_type=$(echo $t | grep "^NS\|CNAME\|MX\|PTR\|SRV")
  546. tree_length=3
  547. if [[ $t = 'CNAME' || $t = 'MX' || $t = 'PTR' ]]; then
  548. tree_length=2
  549. fi
  550. if [ -n "$fqdn_type" ]; then
  551. dots=$(echo $dvalue | grep -o "\." | wc -l)
  552. if [ "$dots" -lt "$tree_length" ]; then
  553. r=$(echo $r | sed -e "s/\.$//")
  554. msg="$t record $r should be a fully qualified domain name (FQDN)"
  555. echo "Error: $msg"
  556. log_event "$E_INVALID" "$ARGUMENTS"
  557. exit "$E_INVALID"
  558. fi
  559. fi
  560. }
  561. # Validate nameserver
  562. is_dns_nameserver_valid() {
  563. d=$1
  564. t=$2
  565. r=$3
  566. if [ "$t" = 'NS' ]; then
  567. remote=$(echo $r | grep ".$domain.$")
  568. if [ -n "$remote" ]; then
  569. zone=$USER_DATA/dns/$d.conf
  570. a_record=${r%.$d.}
  571. n_record=$(grep "RECORD='$a_record'" $zone | grep "TYPE='A'")
  572. if [ -z "$n_record" ]; then
  573. check_result "$E_NOTEXIST" "IN A $a_record.$d does not exist"
  574. fi
  575. fi
  576. fi
  577. }
  578. #----------------------------------------------------------#
  579. # MAIL #
  580. #----------------------------------------------------------#
  581. # Mail domain existence check
  582. is_mail_domain_new() {
  583. mail=$(ls $HESTIA/data/users/*/mail/$1.conf 2> /dev/null)
  584. if [ -n "$mail" ]; then
  585. if [ "$2" == 'mail' ]; then
  586. check_result $E_EXISTS "Mail domain $1 exists"
  587. fi
  588. mail_user=$(echo "$mail" | cut -f 7 -d /)
  589. if [ "$mail_user" != "$user" ]; then
  590. check_result "$E_EXISTS" "Mail domain $1 exists"
  591. fi
  592. fi
  593. mail_sub=$(echo "$1" | cut -f 1 -d .)
  594. mail_nosub=$(echo "$1" | cut -f 1 -d . --complement)
  595. for mail_reserved in $(echo "mail $WEBMAIL_ALIAS"); do
  596. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_reserved.$1.conf 2> /dev/null)" ]; then
  597. if [ "$2" == 'mail' ]; then
  598. check_result "$E_EXISTS" "Required subdomain \"$mail_reserved.$1\" already exists"
  599. fi
  600. fi
  601. if [ -n "$(ls $HESTIA/data/users/*/mail/$mail_nosub.conf 2> /dev/null)" ] && [ "$mail_sub" = "$mail_reserved" ]; then
  602. if [ "$2" == 'mail' ]; then
  603. check_result "$E_INVALID" "The subdomain \"$mail_sub.\" is reserved by \"$mail_nosub\""
  604. fi
  605. fi
  606. done
  607. }
  608. # Checking mail account existence
  609. is_mail_new() {
  610. check_acc=$(grep "ACCOUNT='$1'" $USER_DATA/mail/$domain.conf)
  611. if [ -n "$check_acc" ]; then
  612. check_result "$E_EXISTS" "mail account $1 is already exists"
  613. fi
  614. check_als=$(awk -F "ALIAS='" '{print $2}' $USER_DATA/mail/$domain.conf)
  615. check_als=$(echo "$check_als" | cut -f 1 -d "'" | grep -w $1)
  616. if [ -n "$check_als" ]; then
  617. check_result "$E_EXISTS" "mail alias $1 is already exists"
  618. fi
  619. }
  620. # Add mail server SSL configuration
  621. add_mail_ssl_config() {
  622. # Ensure that SSL certificate directories exists
  623. if [ ! -d "$HOMEDIR/$user/conf/mail/$domain/ssl/" ]; then
  624. mkdir -p $HOMEDIR/$user/conf/mail/$domain/ssl/
  625. fi
  626. if [ ! -d "$HESTIA/ssl/mail" ]; then
  627. mkdir -p $HESTIA/ssl/mail
  628. fi
  629. if [ ! -d /etc/dovecot/conf.d/domains ]; then
  630. mkdir -p /etc/dovecot/conf.d/domains
  631. fi
  632. # Add certificate to Hestia user configuration data directory
  633. if [ -f "$ssl_dir/$domain.crt" ]; then
  634. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.crt
  635. cp -f $ssl_dir/$domain.key $USER_DATA/ssl/mail.$domain.key
  636. cp -f $ssl_dir/$domain.crt $USER_DATA/ssl/mail.$domain.pem
  637. if [ -e "$ssl_dir/$domain.ca" ]; then
  638. cp -f $ssl_dir/$domain.ca $USER_DATA/ssl/mail.$domain.ca
  639. echo >> $USER_DATA/ssl/mail.$domain.pem
  640. cat $USER_DATA/ssl/mail.$domain.ca >> $USER_DATA/ssl/mail.$domain.pem
  641. fi
  642. fi
  643. chmod 660 $USER_DATA/ssl/mail.$domain.*
  644. # Add certificate to user home directory
  645. cp -f $USER_DATA/ssl/mail.$domain.crt $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt
  646. cp -f $USER_DATA/ssl/mail.$domain.key $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key
  647. cp -f $USER_DATA/ssl/mail.$domain.pem $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem
  648. if [ -e "$USER_DATA/ssl/mail.$domain.ca" ]; then
  649. cp -f $USER_DATA/ssl/mail.$domain.ca $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca
  650. fi
  651. # Clean up dovecot configuration (if it exists)
  652. if [ -f /etc/dovecot/conf.d/domains/$domain.conf ]; then
  653. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  654. fi
  655. # Check if using custom / wildcard mail certificate
  656. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  657. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  658. if [ -n "$mail_cert_match" ]; then
  659. # Add domain SSL configuration to dovecot
  660. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  661. echo "local_name $domain {" >> /etc/dovecot/conf.d/domains/$domain.conf
  662. echo " ssl_cert = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem" >> /etc/dovecot/conf.d/domains/$domain.conf
  663. echo " ssl_key = <$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key" >> /etc/dovecot/conf.d/domains/$domain.conf
  664. echo "}" >> /etc/dovecot/conf.d/domains/$domain.conf
  665. # Add domain SSL configuration to exim4
  666. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem $HESTIA/ssl/mail/$domain.crt
  667. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/$domain.key
  668. fi
  669. # Add domain SSL configuration to dovecot
  670. echo "" >> /etc/dovecot/conf.d/domains/$domain.conf
  671. echo "local_name mail.$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/mail.$domain.crt
  677. ln -s $HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key $HESTIA/ssl/mail/mail.$domain.key
  678. # Set correct permissions on certificates
  679. chmod 0750 $HOMEDIR/$user/conf/mail/$domain/ssl
  680. chown -R $MAIL_USER:mail $HOMEDIR/$user/conf/mail/$domain/ssl
  681. chmod 0644 $HOMEDIR/$user/conf/mail/$domain/ssl/*
  682. chown -h $user:mail $HOMEDIR/$user/conf/mail/$domain/ssl/*
  683. chmod -R 0644 $HESTIA/ssl/mail/*
  684. chown -h $user:mail $HESTIA/ssl/mail/*
  685. }
  686. # Delete SSL support for mail domain
  687. del_mail_ssl_config() {
  688. # Check to prevent accidental removal of mismatched certificate
  689. wildcard_domain="\\*.$(echo "$domain" | cut -f 1 -d . --complement)"
  690. mail_cert_match=$($BIN/v-list-mail-domain-ssl $user $domain | awk '/SUBJECT|ALIASES/' | grep -wE " $domain| $wildcard_domain")
  691. # Remove old mail certificates
  692. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  693. # Remove dovecot configuration
  694. rm -f /etc/dovecot/conf.d/domains/$domain.conf
  695. # Remove SSL vhost configuration
  696. rm -f $HOMEDIR/$user/conf/mail/$domain/*.*ssl.conf
  697. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  698. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  699. # Remove SSL certificates
  700. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  701. if [ -n "$mail_cert_match" ]; then
  702. rm -f $HESTIA/ssl/mail/$domain.crt $HESTIA/ssl/mail/$domain.key
  703. fi
  704. rm -f $HESTIA/ssl/mail/mail.$domain.crt $HESTIA/ssl/mail/mail.$domain.key
  705. }
  706. # Delete generated certificates from user configuration data directory
  707. del_mail_ssl_certificates() {
  708. rm -f $USER_DATA/ssl/mail.$domain.ca
  709. rm -f $USER_DATA/ssl/mail.$domain.crt
  710. rm -f $USER_DATA/ssl/mail.$domain.key
  711. rm -f $USER_DATA/ssl/mail.$domain.pem
  712. rm -f $HOMEDIR/$user/conf/mail/$domain/ssl/*
  713. }
  714. # Add webmail config
  715. add_webmail_config() {
  716. mkdir -p "$HOMEDIR/$user/conf/mail/$domain"
  717. conf="$HOMEDIR/$user/conf/mail/$domain/$1.conf"
  718. if [[ "$2" =~ stpl$ ]]; then
  719. conf="$HOMEDIR/$user/conf/mail/$domain/$1.ssl.conf"
  720. fi
  721. domain_idn=$domain
  722. format_domain_idn
  723. ssl_crt="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.crt"
  724. ssl_key="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.key"
  725. ssl_pem="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.pem"
  726. ssl_ca="$HOMEDIR/$user/conf/mail/$domain/ssl/$domain.ca"
  727. override_alias=""
  728. if [ "$WEBMAIL_ALIAS" != "mail" ]; then
  729. override_alias="mail.$domain"
  730. override_alias_idn="mail.$domain_idn"
  731. fi
  732. # Note: Removing or renaming template variables will lead to broken custom templates.
  733. # -If possible custom templates should be automatically upgraded to use the new format
  734. # -Alternatively a depreciation period with proper notifications should be considered
  735. cat $MAILTPL/$1/$2 \
  736. | sed -e "s|%ip%|$local_ip|g" \
  737. -e "s|%domain%|$WEBMAIL_ALIAS.$domain|g" \
  738. -e "s|%domain_idn%|$WEBMAIL_ALIAS.$domain_idn|g" \
  739. -e "s|%root_domain%|$domain|g" \
  740. -e "s|%alias%|$override_alias|g" \
  741. -e "s|%alias_idn%|$override_alias_idn|g" \
  742. -e "s|%alias_string%|$alias_string|g" \
  743. -e "s|%email%|info@$domain|g" \
  744. -e "s|%web_system%|$WEB_SYSTEM|g" \
  745. -e "s|%web_port%|$WEB_PORT|g" \
  746. -e "s|%web_ssl_port%|$WEB_SSL_PORT|g" \
  747. -e "s|%backend_lsnr%|$backend_lsnr|g" \
  748. -e "s|%rgroups%|$WEB_RGROUPS|g" \
  749. -e "s|%proxy_system%|$PROXY_SYSTEM|g" \
  750. -e "s|%proxy_port%|$PROXY_PORT|g" \
  751. -e "s|%proxy_ssl_port%|$PROXY_SSL_PORT|g" \
  752. -e "s/%proxy_extensions%/${PROXY_EXT//,/|}/g" \
  753. -e "s|%user%|$user|g" \
  754. -e "s|%group%|$user|g" \
  755. -e "s|%home%|$HOMEDIR|g" \
  756. -e "s|%docroot%|$docroot|g" \
  757. -e "s|%sdocroot%|$sdocroot|g" \
  758. -e "s|%ssl_crt%|$ssl_crt|g" \
  759. -e "s|%ssl_key%|$ssl_key|g" \
  760. -e "s|%ssl_pem%|$ssl_pem|g" \
  761. -e "s|%ssl_ca_str%|$ssl_ca_str|g" \
  762. -e "s|%ssl_ca%|$ssl_ca|g" \
  763. > $conf
  764. chown root:$user $conf
  765. chmod 640 $conf
  766. if [[ "$2" =~ stpl$ ]]; then
  767. if [ -n "$WEB_SYSTEM" ]; then
  768. forcessl="$HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.forcessl.conf"
  769. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  770. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  771. fi
  772. if [ -n "$PROXY_SYSTEM" ]; then
  773. forcessl="$HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.forcessl.conf"
  774. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  775. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  776. fi
  777. # Add rewrite rules to force HTTPS/SSL connections
  778. if [ -n "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
  779. echo 'return 301 https://$server_name$request_uri;' > $forcessl
  780. else
  781. echo 'RewriteEngine On' > $forcessl
  782. echo 'RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]' >> $forcessl
  783. fi
  784. # Remove old configurations
  785. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" -o -name "ssl.$domain.*" -o -name "*nginx.$domain.*" \) -exec rm {} \;
  786. else
  787. if [ -n "$WEB_SYSTEM" ]; then
  788. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  789. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  790. fi
  791. if [ -n "$PROXY_SYSTEM" ]; then
  792. rm -f /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  793. ln -s $conf /etc/$1/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  794. fi
  795. # Clear old configurations
  796. find $HOMEDIR/$user/conf/mail/ -maxdepth 1 -type f \( -name "$domain.*" \) -exec rm {} \;
  797. fi
  798. }
  799. # Delete webmail support
  800. del_webmail_config() {
  801. if [ -n "$WEB_SYSTEM" ]; then
  802. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.conf
  803. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  804. fi
  805. if [ -n "$PROXY_SYSTEM" ]; then
  806. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*conf
  807. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.conf
  808. fi
  809. }
  810. # Delete SSL webmail support
  811. del_webmail_ssl_config() {
  812. if [ -n "$WEB_SYSTEM" ]; then
  813. rm -f $HOMEDIR/$user/conf/mail/$domain/$WEB_SYSTEM.*ssl.conf
  814. rm -f /etc/$WEB_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  815. fi
  816. if [ -n "$PROXY_SYSTEM" ]; then
  817. rm -f $HOMEDIR/$user/conf/mail/$domain/$PROXY_SYSTEM.*ssl.conf
  818. rm -f /etc/$PROXY_SYSTEM/conf.d/domains/$WEBMAIL_ALIAS.$domain.ssl.conf
  819. fi
  820. }
  821. #----------------------------------------------------------#
  822. # CMN #
  823. #----------------------------------------------------------#
  824. # Checking domain existence
  825. is_domain_new() {
  826. type=$1
  827. for object in ${2//,/ }; do
  828. if [ -n "$WEB_SYSTEM" ]; then
  829. is_web_domain_new $object $type
  830. is_web_alias_new $object $type
  831. fi
  832. if [ -n "$DNS_SYSTEM" ]; then
  833. is_dns_domain_new $object $type
  834. fi
  835. if [ -n "$MAIL_SYSTEM" ]; then
  836. is_mail_domain_new $object $type
  837. fi
  838. done
  839. }
  840. # Get domain variables
  841. get_domain_values() {
  842. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/$1.conf)
  843. }
  844. #----------------------------------------------------------#
  845. # 2 Char domain name detection #
  846. #----------------------------------------------------------#
  847. is_valid_extension() {
  848. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  849. mkdir $HESTIA/data/extensions/
  850. chmod 750 $HESTIA/data/extensions/
  851. /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
  852. fi
  853. test_domain=$(idn2 -d "$1")
  854. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1 | /usr/bin/rev)
  855. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  856. }
  857. is_valid_2_part_extension() {
  858. if [ ! -e "$HESTIA/data/extensions/public_suffix_list.dat" ]; then
  859. mkdir $HESTIA/data/extensions/
  860. chmod 750 $HESTIA/data/extensions/
  861. /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
  862. fi
  863. test_domain=$(idn2 -d "$1")
  864. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  865. exten=$(grep "^$extension\$" $HESTIA/data/extensions/public_suffix_list.dat)
  866. }
  867. get_base_domain() {
  868. test_domain=$1
  869. is_valid_extension "$test_domain"
  870. if [ $? -ne 0 ]; then
  871. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  872. else
  873. is_valid_2_part_extension "$test_domain"
  874. if [ $? -ne 0 ]; then
  875. basedomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  876. else
  877. extension=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 1-2 | /usr/bin/rev)
  878. partdomain=$(/bin/echo "${test_domain}" | /usr/bin/rev | /usr/bin/cut -d "." --output-delimiter="." -f 3 | /usr/bin/rev)
  879. basedomain="$partdomain.$extension"
  880. fi
  881. fi
  882. }
  883. is_base_domain_owner() {
  884. for object in ${1//,/ }; do
  885. if [ "$object" != "none" ]; then
  886. get_base_domain $object
  887. web=$(grep -F -H -h "DOMAIN='$basedomain'" $HESTIA/data/users/*/web.conf)
  888. if [ "$ENFORCE_SUBDOMAIN_OWNERSHIP" = "yes" ]; then
  889. if [ -n "$web" ]; then
  890. parse_object_kv_list "$web"
  891. if [ -z "$ALLOW_USERS" ] || [ "$ALLOW_USERS" != "yes" ]; then
  892. # Don't care if $basedomain all ready exists only if the owner is of the base domain is the current user
  893. check=$(is_domain_new "" $basedomain)
  894. if [ $? -ne 0 ]; then
  895. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  896. exit 4
  897. fi
  898. fi
  899. else
  900. check=$(is_domain_new "" "$basedomain")
  901. if [ $? -ne 0 ]; then
  902. echo "Error: Unable to add $object. $basedomain belongs to a different user"
  903. exit 4
  904. fi
  905. fi
  906. fi
  907. fi
  908. done
  909. }