exim limiting

squirrelhost

Verified User
Joined
Feb 21, 2004
Messages
174
Location
Marylebone, London
Having seen various threads discussing spamming
by users, and one on an SMTP Limiter which
had too many replies to consider wading through (often best to read the last post, and see if the
discussion got anywhere at all), I came to
the following conclusions.

Some here would like to have some form of limiting
or quotas, for outgoing email ?

There's currently nothing posted here which
easily allows that to be done in a sensible
manner ? (by sensible, I'd mean limiting
a user to maybe 10 emails per hour, or
100 emails per day? these numbers can
of course be scaled as you wish).
 

squirrelhost

Verified User
Joined
Feb 21, 2004
Messages
174
Location
Marylebone, London
well, here's an easy and rather crude method of limiting number of emails sent per day, which actually works.

based on a few mods to the check_limits function
in exim.pl.

1.

edit /etc/exim.pl and replace the entire
check_limits subroutine with:

sub check_limits
{
my $count = 0;

#find the current user
$uid = find_uid();
$name = getpwuid($uid);

my $userlimit = "/etc/virtual/usage/${name}.limit";
if (-e $userlimit) {
open (LIMIT, "$userlimit");
} else {
open (LIMIT, "/etc/virtual/limit");
}
my $email_limit = int(<LIMIT>);
close(LIMIT);

if (uid_exempt($uid)) { return; }

my $COUNTER = "/etc/virtual/usage/${name}.count";
if (-e $COUNTER) {
open(DATA, $COUNTER) or die "Can't open $COUNTER ($!)";
$count = <DATA>;
close DATA;
}
$count++;
if (!(-e $COUNTER)) {
open(DATA, ">$COUNTER") or die "Can't open $COUNTER ($!)";
} else {
open(DATA, "+<$COUNTER") or die "Can't open $COUNTER ($!)";
}
print DATA $count;
close DATA;
chmod (0660, "/etc/virtual/usage/${name}.count");

if ($email_limit > 0)
{
if (($name = getpwuid($uid)))
{
if ($count > $email_limit)
{
die("You ($name) have reach your daily email limit of $email_limit emails\n");
}
}
}

log_bandwidth($uid);

return "yes"
}



- watch out for any lines wrapping above...

2. edit /etc/virtual/limit
and put in the number of emails allowed
per day. if the file doesn't exist, of if you have
any problems with this, then to set the limit
to 99, for example:

echo "99" > /etc/virtual/limit

3. re-start exim

Now, if a user sends 100 emails, that day,
for the last one, he'd end up with a
"Mail delivery failed: returning message to sender"
message in his inbox, saying that the email address used was unroutable (all depends where the check_limits function is called in exim.conf I suppose).


4. you can start all over again, each day,
by simply removing the files which record each
users email tally, by putting this into root's crontab:

0 0 * * * /usr/bin/find /etc/virtual/usage/ -name *.count -delete 2>&1


5. if you want a 'custom' limit for any particular
user, then rather than using the file
/etc/virtual/limit
then create
/etc/virtual/usage/<username>.limit
instead. The script will find and use the
daily limit in this file instead (for that username).
 

Chrysalis

Verified User
Joined
Aug 25, 2004
Messages
1,584
Location
uk
looks useful thanks, I guess the question is judging the right limit, for a casual email user 100 might be good but for someone who has a busy inbox he would need more and then you might have someone with a forum who mass mails every now and then and he would get caught out.
 

walo

Verified User
Joined
Mar 19, 2005
Messages
162
I just change mi exim.pl with that subroutines and now i get an error when i try to send email via root

for ie:

echo "body" | mail -s "subject" user@somedomain.com

it return me the following error

S: "Mail delivery failed: returning message to sender"

msg:

This message was created automatically by mail delivery software.

A message that you sent could not be delivered to one or more of its
recipients. This is a permanent error. The following address(es) failed:

user@somedomain.com
Unrouteable address



any idea ?

PD: user@somedomain.com its an example, not real
 

rix

Verified User
Joined
Sep 22, 2003
Messages
16
I tried to use this script and everything went well. However, everytime i check the file username.count its always 1 so i think the counter isn't working.
 

jodasi

Verified User
Joined
Mar 15, 2005
Messages
62
Well, i have tested this option, but emails sent with the php mail() function, wont area counted.

Is there any way to make mails sent using php mail() fucntion, count?
 

netmotiv8

Verified User
Joined
Sep 26, 2003
Messages
234
Location
UK
well, here's an easy and rather crude method of limiting number of emails sent per day, which actually works.

based on a few mods to the check_limits function
in exim.pl.

1.

edit /etc/exim.pl and replace the entire
check_limits subroutine with:

sub check_limits
{
my $count = 0;

#find the current user
$uid = find_uid();
$name = getpwuid($uid);

my $userlimit = "/etc/virtual/usage/${name}.limit";
if (-e $userlimit) {
open (LIMIT, "$userlimit");
} else {
open (LIMIT, "/etc/virtual/limit");
}
my $email_limit = int(<LIMIT>);
close(LIMIT);

if (uid_exempt($uid)) { return; }

my $COUNTER = "/etc/virtual/usage/${name}.count";
if (-e $COUNTER) {
open(DATA, $COUNTER) or die "Can't open $COUNTER ($!)";
$count = <DATA>;
close DATA;
}
$count++;
if (!(-e $COUNTER)) {
open(DATA, ">$COUNTER") or die "Can't open $COUNTER ($!)";
} else {
open(DATA, "+<$COUNTER") or die "Can't open $COUNTER ($!)";
}
print DATA $count;
close DATA;
chmod (0660, "/etc/virtual/usage/${name}.count");

if ($email_limit > 0)
{
if (($name = getpwuid($uid)))
{
if ($count > $email_limit)
{
die("You ($name) have reach your daily email limit of $email_limit emails\n");
}
}
}

log_bandwidth($uid);

return "yes"
}



- watch out for any lines wrapping above...

2. edit /etc/virtual/limit
and put in the number of emails allowed
per day. if the file doesn't exist, of if you have
any problems with this, then to set the limit
to 99, for example:

echo "99" > /etc/virtual/limit

3. re-start exim

Now, if a user sends 100 emails, that day,
for the last one, he'd end up with a
"Mail delivery failed: returning message to sender"
message in his inbox, saying that the email address used was unroutable (all depends where the check_limits function is called in exim.conf I suppose).


4. you can start all over again, each day,
by simply removing the files which record each
users email tally, by putting this into root's crontab:

0 0 * * * /usr/bin/find /etc/virtual/usage/ -name *.count -delete 2>&1


5. if you want a 'custom' limit for any particular
user, then rather than using the file
/etc/virtual/limit
then create
/etc/virtual/usage/<username>.limit
instead. The script will find and use the
daily limit in this file instead (for that username).

Hello,

We have done mod however, all mail on the server was affected with this message:

##############

SMTP error from remote mail server after RCPT TO:<xxxxx.xxxxx@adomain.co.uk>:
host xxx.xx.xx.xxx [xxx.xx.xx.xxx]: 550-Verification failed for <xxxxxxxxxxx@gmail.com>
550-Unrouteable address
550 Sender verify failed

##############
please note: ip address and email address removed for privacy reasons.


Any idea what the problem is?


Cheers

Neil
 

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
26,119
Location
California
It would be easy enough to insert in exim.conf, but I'm not sure of the utility. According to your link, RFCs require at least 100 recipients. Do you think spammers use over 100 recipients per email? I've never seen even that many when looking through logs.

Jeff
 

Chrysalis

Verified User
Joined
Aug 25, 2004
Messages
1,584
Location
uk
my experience of spammers they use about 10-20 receipients per send so this wouldnt do much to stop spam, and if you limit below that you will cause problems for legitamate mail sending tools like forum mass mail tools.
 

Eremiya

Verified User
Joined
Mar 8, 2007
Messages
32
Location
Ukraine, Kyiv
In the current version DirectAdmin if to use this change in a file Exim.PL it is impossible to send the letter with DA (for example the letter on activation of an account).

[root@s1 etc]# cat /var/log/exim/mainlog | grep 1I2AU5-0008W8-3Y
2007-06-23 22:37:21 1I2AU5-0008W8-3Y <= diradmin@s1.myserver.tld U=diradmin P=local S=2293 T="mytheme" from <diradmin@s1.myserver.tld> for email@clientdomain.tld
2007-06-23 22:37:21 1I2AU5-0008W8-3Y ** email@clientdomain.tld F=<diradmin@s1.myserver.tld>: Unrouteable address
2007-06-23 22:37:21 1I2AU5-0008WA-4n <= <> R=1I2AU5-0008W8-3Y U=mail P=local S=3063 T="Mail delivery failed: returning message to sender" from <> for diradmin@s1.myserver.tld
2007-06-23 22:37:21 1I2AU5-0008W8-3Y Completed

Who can help me? Or to show a file entirely with which the current version Direct Admin works.

P.S. Sorry, my English very bad.
 
Last edited:

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
26,119
Location
California
In general an unrouteable address error means the server dns resolution isn't working properly. I've used this answer about five times today already :( .

Jeff
 

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
26,119
Location
California
Are you using a custom version of exim.conf? If so, I'm afraid I can't help you. If not, then please post the top five lines of the file.

Perhaps knowing the version you're running someone using the same version can help.

Jeff
 

Eremiya

Verified User
Joined
Mar 8, 2007
Messages
32
Location
Ukraine, Kyiv
[root@s1 log]# cat /etc/exim.pl
#!/usr/bin/perl

#smtpauth
#called by exim to verify if an smtp user is allowed to
#send email through the server
#possible success:
# user is in /etc/virtual/domain.com/passwd and password matches
# user is in /etc/passwd and password matches in /etc/shadow

Version Exim: 4.66
I use http://help.directadmin.com/item.php?id=51 for setting default config`s.
 

Eremiya

Verified User
Joined
Mar 8, 2007
Messages
32
Location
Ukraine, Kyiv
[root@s1 log]# cat /etc/exim.conf
######################################################################
# SpamBlocker.exim.conf.2.1.1-release #
# 05-Jun-2007 #
# Runtime configuration file for DirectAdmin/Exim 4.24 and above #
# Requires exim.pl dated 20-Apr-2007 17:09 or later #
 

nobaloney

NoBaloney Internet Svcs - In Memoriam †
Joined
Jun 16, 2003
Messages
26,119
Location
California
Your version of exim.conf should work properly with the newest version of exim.pl found at the DirectAdmin site.

Jeff
 

maiijarb

Verified User
Joined
Nov 8, 2006
Messages
57
need help

Hello,

I follow this howto and it work great on my freebsd server
but only problem is username.limit files
the system delete all *.limit at midnight. How to prevent this

Thanks in advance
 
Top