domain.sh 34 KB

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