domain.sh 33 KB

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