domain.sh 35 KB

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