Skip to content

Commit d8d2622

Browse files
Fix incomming mail rate limit Deb 11 / Ubuntu 22.04 (hestiacp#2523)
* Fix issue with reciving mail Deb 11 and Ubuntu 22.04 * Fix type in rate limit creation * Fix v-add-mail-account * Fix bug overwriting old config file * Fix error in rebuild script * Move location * Fix issue in config / sed script * Create $HOMEDIR/$user/conf/mail/$domain/limits if not exists * Remove if statement * Spacing :) Co-authored-by: Raphael <rs@scit.ch>
1 parent 46d8c2f commit d8d2622

File tree

6 files changed

+39
-26
lines changed

6 files changed

+39
-26
lines changed

bin/v-add-mail-account

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# Argument definition
1414
user=$1
1515
domain=$2
16+
domain_idn=$2
1617
account=$3
1718
password=$4; HIDE=4
1819
quota=${5-unlimited}
@@ -34,6 +35,10 @@ if [[ "$account" =~ [[:upper:]] ]]; then
3435
account=$(echo "$account" |tr '[:upper:]' '[:lower:]')
3536
fi
3637

38+
format_domain
39+
format_domain_idn
40+
41+
3742
#----------------------------------------------------------#
3843
# Verifications #
3944
#----------------------------------------------------------#
@@ -55,6 +60,7 @@ is_password_valid
5560
# Perform verification if read-only mode is enabled
5661
check_hestia_demo_mode
5762

63+
5864
#----------------------------------------------------------#
5965
# Action #
6066
#----------------------------------------------------------#
@@ -84,6 +90,7 @@ fi
8490
chown $user:mail $HOMEDIR/$user/mail/$domain/$account
8591
chmod 700 $HOMEDIR/$user/mail/$domain/$account
8692

93+
8794
#----------------------------------------------------------#
8895
# Hestia #
8996
#----------------------------------------------------------#
@@ -105,9 +112,16 @@ chmod 660 $USER_DATA/mail/$domain.conf
105112

106113
syshealth_repair_mail_account_config
107114

108-
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT');
115+
touch $HOMEDIR/$user/conf/mail/$domain/limits;
116+
117+
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT')
109118
if [ -n "$user_rate_limit" ]; then
110-
echo "$user_rate_limit" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
119+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
120+
echo "$account@$domain_idn:$user_rate_limit" >> $HOMEDIR/$user/conf/mail/$domain/limits
121+
else
122+
system=$(cat /etc/exim4/limit.conf)
123+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
124+
echo "$account@$domain_idn:$system" >> $HOMEDIR/$user/conf/mail/$domain/limits
111125
fi
112126

113127
# Increase mail accounts counter

bin/v-change-mail-account-rate-limit

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,19 @@ check_hestia_demo_mode
5858

5959
md5=$(get_object_value "mail/$domain" 'ACCOUNT' "$account" '$MD5')
6060
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
6461
if [ "$rate" = "system" ]; then
6562
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT');
6663
if [ -n "$user_rate_limit" ]; then
67-
echo "$user_rate_limit" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
64+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
65+
echo "$account@$domain_idn:$user_rate_limit" >> $HOMEDIR/$user/conf/mail/$domain/limits
6866
else
69-
rm $HOMEDIR/$user/conf/mail/$domain/limits/$account
67+
system=$(cat /etc/exim4/limit.conf)
68+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
69+
echo "$account@$domain_idn:$system" >> $HOMEDIR/$user/conf/mail/$domain/limits
7070
fi
7171
else
72-
echo "$rate" > $HOMEDIR/$user/conf/mail/$domain/limits/$account
72+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
73+
echo "$account@$domain_idn:$rate" >> $HOMEDIR/$user/conf/mail/$domain/limits
7374
fi
7475
fi
7576

func/rebuild.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,12 +571,12 @@ rebuild_mail_domain_conf() {
571571
rm -f $HOMEDIR/$user/conf/mail/$domain/passwd
572572
rm -f $HOMEDIR/$user/conf/mail/$domain/fwd_only
573573
rm -f $HOMEDIR/$user/conf/mail/$domain/ip
574-
rm -fr $HOMEDIR/$user/conf/mail/$domain/limits/
574+
rm -fr $HOMEDIR/$user/conf/mail/$domain/limits
575575
touch $HOMEDIR/$user/conf/mail/$domain/accounts
576576
touch $HOMEDIR/$user/conf/mail/$domain/aliases
577577
touch $HOMEDIR/$user/conf/mail/$domain/passwd
578578
touch $HOMEDIR/$user/conf/mail/$domain/fwd_only
579-
mkdir $HOMEDIR/$user/conf/mail/$domain/limits/
579+
touch $HOMEDIR/$user/conf/mail/$domain/limits
580580

581581
# Setting outgoing ip address
582582
if [ -n "$local_ip" ]; then
@@ -664,10 +664,17 @@ rebuild_mail_domain_conf() {
664664
user_rate_limit=$(get_object_value 'mail' 'DOMAIN' "$domain" '$RATE_LIMIT');
665665
if [ -n "$RATE_LIMIT" ]; then
666666
#user value
667-
echo "$RATE_LIMIT" >> $HOMEDIR/$user/conf/mail/$domain/limits/$account
667+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
668+
echo "$account@$domain_idn:$RATE_LIMIT" >> $HOMEDIR/$user/conf/mail/$domain/limits
668669
elif [ -n "$user_rate_limit" ]; then
669-
#revert to user value
670-
echo "$user_rate_limit" >> $HOMEDIR/$user/conf/mail/$domain/limits/$account
670+
#revert to account value
671+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
672+
echo "$account@$domain_idn:$user_rate_limit" >> $HOMEDIR/$user/conf/mail/$domain/limits
673+
else
674+
#revert to system value
675+
system=$(cat /etc/exim4/limit.conf)
676+
sed -i "/^$account@$domain_idn:/ d" $HOMEDIR/$user/conf/mail/$domain/limits
677+
echo "$account@$domain_idn:$system" >> $HOMEDIR/$user/conf/mail/$domain/limits
671678
fi
672679
fi
673680
done

install/deb/exim/exim4.conf.4.94.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ acl_check_rcpt:
133133

134134
# Limit per email account for SMTP auhenticated users
135135
deny message = Email account $authenticated_id is sending too many emails - rate overlimit = $sender_rate / $sender_rate_period
136-
set acl_c_msg_limit = ${if exists{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits/${extract{1}{:}{${lookup{$sender_address_local_part}lsearch{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/accounts}}}}} {${readfile{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits/${extract{1}{:}{${lookup{$sender_address_local_part}lsearch{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/accounts}}}}}}} {${readfile{/etc/exim4/limit.conf}}} }
136+
set acl_c_msg_limit = ${if exists{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits} {${extract{1}{:}{${lookup{$sender_address_local_part@$sender_address_domain}lsearch{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits}}}}} {${readfile{/etc/exim4/limit.conf}}} }
137137
ratelimit = $acl_c_msg_limit / 1h / strict/ $authenticated_id
138138

139139
warn ratelimit = ${eval:$acl_c_msg_limit / 2} / 1h / strict / $authenticated_id

install/deb/exim/exim4.conf.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ acl_check_rcpt:
133133

134134
# Limit per email account for SMTP auhenticated users
135135
deny message = Email account $authenticated_id is sending too many emails - rate overlimit = $sender_rate / $sender_rate_period
136-
set acl_c_msg_limit = ${if exists{/etc/exim4/domains/$sender_address_domain/limits/$sender_address_local_part} {${readfile{/etc/exim4/domains/$sender_address_domain/limits/$sender_address_local_part}}} {${readfile{/etc/exim4/limit.conf}}} }
136+
set acl_c_msg_limit = ${if exists{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits} {${extract{1}{:}{${lookup{$sender_address_local_part@$sender_address_domain}lsearch{/etc/exim4/domains/${lookup{$sender_address_domain}dsearch{/etc/exim4/domains/}}/limits}}}}} {${readfile{/etc/exim4/limit.conf}}} }
137137
ratelimit = $acl_c_msg_limit / 1h / strict/ $authenticated_id
138138

139139
warn ratelimit = ${eval:$acl_c_msg_limit / 2} / 1h / strict / $authenticated_id

install/upgrade/versions/1.6.0.sh

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,8 @@ upgrade_config_set_value 'UPGRADE_UPDATE_FILEMANAGER_CONFIG' 'false'
2424
if [ "$MAIL_SYSTEM" = "exim4" ]; then
2525
echo "[ * ] Update exim4 config to support rate limits"
2626
# Upgrade config exim for custom limits
27-
28-
exim_version=$(exim4 --version | head -1 | awk '{print $3}' | cut -f -2 -d .);
29-
if [ "$exim_version" = "4.94" ] || [ "$exim_version" = "4.95" ]; then
30-
#For Debian 11 and Ubuntu 22.04
31-
sed -i '115,250 s/ratelimit = 200 \/ 1h \/ $authenticated_id/ set acl_c_msg_limit = ${if exists{\/etc\/exim4\/domains\/${lookup{$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/limits\/${extract{1}{:}{${lookup{$sender_address_local_part}lsearch{\/etc\/exim4\/domains\/${lookup{$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/accounts}}}}} {${readfile{\/etc\/exim4\/domains\/${lookup{$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/limits\/${extract{1}{:}{${lookup{$sender_address_local_part}lsearch{\/etc\/exim4\/domains\/${lookup{$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/accounts}}}}}}} {${readfile{\/etc\/exim4\/limit.conf}}} } \n ratelimit = $acl_c_msg_limit \/ 1h \/ strict\/ $authenticated_id/g' /etc/exim4/exim4.conf.template
32-
sed -i '115,250 s/warn ratelimit = 100 \/ 1h \/ strict \/ $authenticated_id/warn ratelimit = ${eval:$acl_c_msg_limit \/ 2} \/ 1h \/ strict \/ $authenticated_id/g' /etc/exim4/exim4.conf.template
33-
else
34-
# And the other
35-
sed -i '115,250 s/ratelimit = 200 \/ 1h \/ $authenticated_id/ set acl_c_msg_limit = ${if exists{\/etc\/exim4\/domains\/$sender_address_domain\/limits\/$sender_address} {${readfile{\/etc\/exim4\/domains\/$sender_address_domain\/limits\/$sender_address_local_part}}} {${readfile{\/etc\/exim4\/limit.conf}}} } \n ratelimit = $acl_c_msg_limit \/ 1h \/ strict\/ $authenticated_id/g' /etc/exim4/exim4.conf.template
36-
sed -i '115,250 s/warn ratelimit = 100 \/ 1h \/ strict \/ $authenticated_id/warn ratelimit = ${eval:$acl_c_msg_limit \/ 2} \/ 1h \/ strict \/ $authenticated_id/g' /etc/exim4/exim4.conf.template
37-
fi
27+
sed -i '115,250 s/ratelimit = 200 \/ 1h \/ $authenticated_id/ set acl_c_msg_limit = \${if exists{\/etc\/exim4\/domains\/\${lookup{\$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/limits} {\${extract{1}{:}{\${lookup{\$sender_address_local_part@\$sender_address_domain}lsearch{\/etc\/exim4\/domains\/\${lookup{\$sender_address_domain}dsearch{\/etc\/exim4\/domains\/}}\/limits}}}}} {\${readfile{\/etc\/exim4\/limit.conf}}} }\n ratelimit = \$acl_c_msg_limit \/ 1h \/ strict\/ \$authenticated_idy/g' /etc/exim4/exim4.conf.template
28+
sed -i '115,250 s/warn ratelimit = 100 \/ 1h \/ strict \/ $authenticated_id/warn ratelimit = ${eval:$acl_c_msg_limit \/ 2} \/ 1h \/ strict \/ $authenticated_id/g' /etc/exim4/exim4.conf.template
3829
# Add missing limit.conf file
3930
cp $HESTIA_INSTALL_DIR/exim/limit.conf /etc/exim4/limit.conf
4031
fi

0 commit comments

Comments
 (0)