Skip to content

Commit 8faa6b8

Browse files
Arturo BlancomaditojaapmarcusScIT-Raphael
authored
Exim add custom ratelimits by user (hestiacp#2225)
* Exim add custom ratelimits by user * Update Custom Limits by domain/user or default (limit.conf) * Update 1.5.9.sh * Update upgrade version to 1.6.0 * Include copy of limits.conf to * Adjust filename to limit.conf * Correct folder * Add support for Debian 11 and Ubuntu 22.04 * Add new keys to configs * Prevent mail.domain.com being removed if used as alias for webmail * Add support for CLI and WEB UI Priority: - "Account" - "Domain" - "Server" Admin is only allowed to edit / change value for security reasons Co-authored-by: Arturo Blanco <ablanco@ablanco.es> Co-authored-by: Jaap Marcus <9754650+jaapmarcus@users.noreply.github.com> Co-authored-by: Raphael <rs@scit.ch>
1 parent d3a54bc commit 8faa6b8

24 files changed

+356
-36
lines changed

bin/v-add-mail-account

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ source /etc/hestiacp/hestia.conf
2424
source $HESTIA/func/main.sh
2525
# shellcheck source=/usr/local/hestia/func/domain.sh
2626
source $HESTIA/func/domain.sh
27+
# shellcheck source=/usr/local/hestia/func/syshealth.sh
28+
source $HESTIA/func/syshealth.sh
2729
# load config file
2830
source_conf "$HESTIA/conf/hestia.conf"
2931

@@ -101,6 +103,13 @@ str="$str TIME='$time' DATE='$date'"
101103
echo "$str" >> $USER_DATA/mail/$domain.conf
102104
chmod 660 $USER_DATA/mail/$domain.conf
103105

106+
syshealth_repair_mail_account_config
107+
108+
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT');
109+
if [ -n "$user_rate_limit" ]; then
110+
echo "$user_rate_limit" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
111+
fi
112+
104113
# Increase mail accounts counter
105114
accounts=$(wc -l $USER_DATA/mail/$domain.conf | cut -f 1 -d ' ')
106115
increase_user_value "$user" '$U_MAIL_ACCOUNTS'

bin/v-add-mail-domain

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ source $HESTIA/func/main.sh
2828
source $HESTIA/func/domain.sh
2929
# shellcheck source=/usr/local/hestia/func/ip.sh
3030
source $HESTIA/func/ip.sh
31+
# shellcheck source=/usr/local/hestia/func/syshealth.sh
32+
source $HESTIA/func/syshealth.sh
3133
# load config file
3234
source_conf "$HESTIA/conf/hestia.conf"
3335

@@ -97,6 +99,8 @@ s="$s DATE='$date'"
9799
echo $s >> $USER_DATA/mail.conf
98100
touch $USER_DATA/mail/$domain.conf
99101

102+
syshealth_repair_mail_config
103+
100104
# Generating DKIM keys
101105
if [ "$dkim" = 'yes' ]; then
102106
openssl genrsa -out $USER_DATA/mail/$domain.pem $dkim_size &>/dev/null

bin/v-add-mail-domain-webmail

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,24 @@ else
9999
# Ensure DNS record exists if Hestia is hosting DNS zones
100100
if [ -n "$DNS_SYSTEM" ]; then
101101
dns_domain=$($BIN/v-list-dns-domains $user | grep $domain | cut -d' ' -f1)
102-
webmail_record=$($BIN/v-list-dns-records $user $domain | grep -i $WEBMAIL_ALIAS | cut -d' ' -f1)
103-
102+
webmail_record=$($BIN/v-list-dns-records $user $domain | grep -i " $WEBMAIL_ALIAS " | cut -d' ' -f1)
104103
if [ "$dns_domain" = "$domain" ]; then
105-
if [ -z "$webmail_record" ]; then
106-
if [ "$quiet" = "yes" ]; then
107-
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
108-
else
109-
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
110-
fi
111-
else
112-
if [ "$quiet" = "yes" ]; then
113-
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
114-
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
104+
if [ "$WEBMAIL_ALIAS" != "mail" ]; then
105+
#Prevent mail.domain.com to be cycled
106+
if [ -z "$webmail_record" ]; then
107+
if [ "$quiet" = "yes" ]; then
108+
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
109+
else
110+
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
111+
fi
115112
else
116-
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
117-
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
113+
if [ "$quiet" = "yes" ]; then
114+
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
115+
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
116+
else
117+
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
118+
$BIN/v-add-dns-record "$user" "$domain" "$WEBMAIL_ALIAS" A "$ip" '' '' "$restart" '' 'yes'
119+
fi
118120
fi
119121
fi
120122
fi
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/bin/bash
2+
# info: change mail domain rate limit
3+
# options: USER DOMAIN ACCOUNT RATE
4+
#
5+
# example: v-change-mail-domain-quota admin mydomain.tld user01 100
6+
#
7+
# This function changes email account rate limit. Use system to use domain or "server" setting
8+
9+
#----------------------------------------------------------#
10+
# Variables & Functions #
11+
#----------------------------------------------------------#
12+
13+
# Argument definition
14+
user=$1
15+
domain=$2
16+
domain_idn=$2
17+
account=$3
18+
rate=$4
19+
20+
# Includes
21+
# shellcheck source=/etc/hestiacp/hestia.conf
22+
source /etc/hestiacp/hestia.conf
23+
# shellcheck source=/usr/local/hestia/func/main.sh
24+
source $HESTIA/func/main.sh
25+
# shellcheck source=/usr/local/hestia/func/domain.sh
26+
source $HESTIA/func/domain.sh
27+
# load config file
28+
source_conf "$HESTIA/conf/hestia.conf"
29+
30+
# Additional argument formatting
31+
format_domain
32+
format_domain_idn
33+
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?
34+
35+
#----------------------------------------------------------#
36+
# Verifications #
37+
#----------------------------------------------------------#
38+
39+
check_args '4' "$#" 'USER DOMAIN ACCOUNT RATE'
40+
is_format_valid 'user' 'domain' 'account'
41+
if [ "$rate" != 'system' ]; then
42+
is_format_valid 'rate'
43+
fi
44+
is_system_enabled "$MAIL_SYSTEM" 'MAIL_SYSTEM'
45+
is_object_valid 'user' 'USER' "$user"
46+
is_object_unsuspended 'user' 'USER' "$user"
47+
is_object_valid 'mail' 'DOMAIN' "$domain"
48+
is_object_unsuspended 'mail' 'DOMAIN' "$domain"
49+
is_object_valid "mail/$domain" 'ACCOUNT' "$account"
50+
is_object_unsuspended "mail/$domain" 'ACCOUNT' "$account"
51+
52+
# Perform verification if read-only mode is enabled
53+
check_hestia_demo_mode
54+
55+
#----------------------------------------------------------#
56+
# Action #
57+
#----------------------------------------------------------#
58+
59+
md5=$(get_object_value "mail/$domain" 'ACCOUNT' "$account" '$MD5')
60+
if [[ "$MAIL_SYSTEM" =~ exim ]]; then
61+
if [ ! -d "$HOMEDIR/$user/conf/mail/$domain/limits/" ]; then
62+
mkdir $HOMEDIR/$user/conf/mail/$domain/limits/
63+
fi
64+
if [ "$rate" = "system" ]; then
65+
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT');
66+
if [ -n "$user_rate_limit" ]; then
67+
echo "$user_rate_limit" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
68+
else
69+
rm $HOMEDIR/$user/conf/mail/$domain/limits/$account
70+
fi
71+
else
72+
echo "$rate" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
73+
fi
74+
fi
75+
76+
#----------------------------------------------------------#
77+
# Hestia #
78+
#----------------------------------------------------------#
79+
80+
if [[ "$rate" = "system" ]]; then
81+
rate=''
82+
fi
83+
84+
# Update quota
85+
update_object_value "mail/$domain" 'ACCOUNT' "$account" '$RATE_LIMIT' "$rate"
86+
87+
# Logging
88+
$BIN/v-log-action "$user" "Info" "Mail" "Mail account rate limit changed (Rate: $rate, Account: $account@$domain)."
89+
log_event "$OK" "$ARGUMENTS"
90+
91+
exit
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
# info: change mail account rate limit
3+
# options: USER DOMAIN ACCOUNT RATE
4+
#
5+
# example: v-change-mail-account-quota admin mydomain.tld user01 100
6+
#
7+
# This function changes email account rate limit for the domain. account specific setting will overwrite domain setting!
8+
9+
#----------------------------------------------------------#
10+
# Variables & Functions #
11+
#----------------------------------------------------------#
12+
13+
# Argument definition
14+
user=$1
15+
domain=$2
16+
domain_idn=$2
17+
rate=$3
18+
19+
# Includes
20+
# shellcheck source=/etc/hestiacp/hestia.conf
21+
source /etc/hestiacp/hestia.conf
22+
# shellcheck source=/usr/local/hestia/func/main.sh
23+
source $HESTIA/func/main.sh
24+
# shellcheck source=/usr/local/hestia/func/domain.sh
25+
source $HESTIA/func/domain.sh
26+
# load config file
27+
source_conf "$HESTIA/conf/hestia.conf"
28+
29+
# Additional argument formatting
30+
format_domain
31+
format_domain_idn
32+
# TODO: $domain_idn not used in this script - maybe $domain should be converted to $doman_idn ?
33+
34+
#----------------------------------------------------------#
35+
# Verifications #
36+
#----------------------------------------------------------#
37+
38+
check_args '3' "$#" 'USER DOMAIN RATE'
39+
is_format_valid 'user' 'domain'
40+
if [ "$rate" != 'system' ]; then
41+
is_format_valid 'rate'
42+
fi
43+
is_system_enabled "$MAIL_SYSTEM" 'MAIL_SYSTEM'
44+
is_object_valid 'user' 'USER' "$user"
45+
is_object_unsuspended 'user' 'USER' "$user"
46+
is_object_valid 'mail' 'DOMAIN' "$domain"
47+
is_object_unsuspended 'mail' 'DOMAIN' "$domain"
48+
49+
# Perform verification if read-only mode is enabled
50+
check_hestia_demo_mode
51+
52+
#----------------------------------------------------------#
53+
# Action #
54+
#----------------------------------------------------------#
55+
56+
57+
#----------------------------------------------------------#
58+
# Hestia #
59+
#----------------------------------------------------------#
60+
61+
if [[ "$rate" = "system" ]]; then
62+
rate=''
63+
fi
64+
65+
$HESTIA/bin/v-rebuild-mail-domain "$user" "$domain"
66+
# Update quota
67+
update_object_value "mail" 'DOMAIN' "$domain" '$RATE_LIMIT' "$rate"
68+
69+
# Logging
70+
$BIN/v-log-action "$user" "Info" "Mail" "Mail domain rate limit has changed ($rate)"
71+
log_event "$OK" "$ARGUMENTS"
72+
73+
exit

bin/v-delete-mail-account

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ if [[ "$MAIL_SYSTEM" =~ exim ]]; then
6060
sed -i "/^$account:/d" $HOMEDIR/$user/conf/mail/$domain/accounts
6161
sed -i "/^$account$/d" $HOMEDIR/$user/conf/mail/$domain/fwd_only
6262
rm -rf $HOMEDIR/$user/mail/$domain/$account
63+
rm -f $HOMEDIR/$user/conf/mail/$domain/limits/$account
6364
fi
6465

6566
#----------------------------------------------------------#

bin/v-delete-mail-domain-webmail

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ if [ -n "$WEBMAIL_ALIAS" ]; then
5858
# Ensure that corresponding DNS records are removed
5959
if [ -n "$DNS_SYSTEM" ]; then
6060
dns_domain=$($BIN/v-list-dns-domains "$user" | grep "$domain" | cut -d' ' -f1)
61-
webmail_record=$($BIN/v-list-dns-records "$user" "$domain" | grep -i "$WEBMAIL_ALIAS" | cut -d' ' -f1)
62-
63-
if [ "$dns_domain" = "$domain" ]; then
64-
if [ -n "$webmail_record" ]; then
65-
if [ "$quiet" = "yes" ]; then
66-
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
67-
else
68-
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart"
61+
if [ "WEBMAIL_ALIAS" != "mail" ];then
62+
#Prevent mail.domain.com being removed
63+
webmail_record=$($BIN/v-list-dns-records $user $domain | grep -i " $WEBMAIL_ALIAS " | cut -d' ' -f1)
64+
if [ "$dns_domain" = "$domain" ]; then
65+
if [ -n "$webmail_record" ]; then
66+
if [ "$quiet" = "yes" ]; then
67+
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart" 'yes'
68+
else
69+
$BIN/v-delete-dns-record "$user" "$domain" "$webmail_record" "$restart"
70+
fi
6971
fi
7072
fi
7173
fi

bin/v-list-mail-account

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ json_list() {
3333
"FWD_ONLY": "'$FWD_ONLY'",
3434
"AUTOREPLY": "'$AUTOREPLY'",
3535
"QUOTA": "'$QUOTA'",
36+
"RATE_LIMIT": "'$RATE_LIMIT'",
3637
"U_DISK": "'$U_DISK'",
3738
"SUSPENDED": "'$SUSPENDED'",
3839
"TIME": "'$TIME'",

bin/v-list-mail-domain

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ json_list() {
3333
"DKIM": "'$DKIM'",
3434
"CATCHALL": "'$CATCHALL'",
3535
"ACCOUNTS": "'$ACCOUNTS'",
36+
"RATE_LIMIT": "'$RATE_LIMIT'",
3637
"U_DISK": "'$U_DISK'",
3738
"SSL": "'$SSL'",
3839
"LETSENCRYPT": "'$LETSENCRYPT'",

func/main.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ get_object_value() {
433433
eval echo $4
434434
}
435435

436+
get_object_values() {
437+
parse_object_kv_list $(grep "$2='$3'" $USER_DATA/$1.conf)
438+
}
439+
436440
# Update object value
437441
update_object_value() {
438442
row=$(grep -nF "$2='$3'" $USER_DATA/$1.conf)
@@ -1154,6 +1158,8 @@ is_format_valid() {
11541158
protocol) is_fw_protocol_format_valid "$arg" ;;
11551159
proxy_ext) is_extention_format_valid "$arg" ;;
11561160
quota) is_int_format_valid "$arg" 'quota' ;;
1161+
rate) is_int_format_valid "$arg" 'rate' ;;
1162+
11571163
record) is_common_format_valid "$arg" 'record';;
11581164
restart) is_restart_format_valid "$arg" 'restart' ;;
11591165
role) is_role_valid "$arg" 'role' ;;

0 commit comments

Comments
 (0)