domain.sh 33 KB

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