domain.sh 37 KB

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