domain.sh 37 KB

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