Limit Outbound Mails by User, Domain or Server Average #2

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
User avatar
mattg
Moderator
Moderator
Posts: 20144
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by mattg » 2014-07-04 00:50

agserna wrote: but at the moment i need to get some info's on how can i distinct between emails sent to local accounts and emails sent from local accounts,
how can I obtain this information from hm_messages And/Or hm_messagesrecipient database tables?
I do that in a script (for a different reason than you need, but same theory should apply)

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
	dim oApp, i, j, Local

	If oClient.Username <> "" Then
		If LCase(oClient.Username) <> LCase(oMessage.FromAddress) Then
	 		Result.Value = 2
			Result.Message = "You are only allowed to send from your own account"
		End If
	End If

	Local = 0
	For i = 1 to oApp.domains.count
		if (InStr(1, oMessage.FromAddress, "@" & oApp.Domains.item(i-1).name, 1) > 0) Then ' Local user.
			local = local + 1
		End If
		For j = 1 to oApp.Domains.item(i-1).DomainAliases.count
			if (InStr(1, oMessage.FromAddress, "@" & oApp.Domains.item(i-1).DomainAliases.item(j-1).AliasName, 1) > 0) Then ' Local user.
				local = local + 1
			End If
		Next 'j
	Next 'i
	if oClient.IPaddress = "192.168.0.1" then
					' from router
		Result.value = 0
	elseif oClient.IPaddress = "127.0.0.1" then
					' from localhost
		Result.value = 0
	elseif local >0 then
		If (oClient.Username = "") Then
			Result.Value = 2
			Result.Message = "You must be authenticated to send from local domain."
		End If
	End If
End Sub
Bill has previously commented about my loops continuing for all domain every time, even after a match is found, and thinks that there is a better way. He is correct - it just hasn't been an issue to me as yet, so I haven't fixed that.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Bill48105
Developer
Developer
Posts: 6192
Joined: 2010-04-24 23:16
Location: Michigan, USA

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by Bill48105 » 2014-07-04 05:58

agserna wrote:Hi Bill
Bill48105 wrote:I think there is now a database-based script that is similar (MUCH MUCH simpler)
I think you are referring to this one http://www.hmailserver.com/forum/viewto ... 20&t=25741
I had a look today but it uses the OnAcceptMessage() to open and close connections via ODBC to an external database and i don't "love" this technique.
Maybe adding the table directly to te HMS database itself and using database object instead of creating/destroing connections for each message would be better but i think that the best thing is completely avoid any activity involving those "low level" mailserver events for performance and stability reasons

Really hope that in the future HMS will natively integrates "high level" protection such limit number of email sent even though this result in a "bit lower performance", see this http://www.hmailserver.com/forum/viewto ... =2&t=25333

In the meantime i think that youre solution is the best compromise so i am thinking to develope a web interface (can be accessed anywhere/anytime) that, at regular time intervals, queryes the mailserver database via stored procedures and checking some parameters (number of emails in queue, number of sent messages and so on) in this way the mailserver events are not involved, no impact on performances and no latency. Using COM API only to disable accounts or change passwords "On the fly" in case of warnings and email me or maybe phone call me via skype or something else....

but at the moment i need to get some info's on how can i distinct between emails sent to local accounts and emails sent from local accounts,
how can I obtain this information from hm_messages And/Or hm_messagesrecipient database tables?

Thanks Again
Yes that's the one. Indeed it's not ideal but at least it should be a lot safer than files. There had been talk about allowing queries via API but didn't go anywhere. To add just this to api alone would be a lot of work. not sure about it being built in as generally things that can be done in scripts/rules are not really considered but yeah it's something that could be done much nicer if built in.

No elegant way to get info really other than aw stats log file.

Btw check out my versions of the nagios scripts that don't require nagios (for scheduled alerts)
http://www.hmailserver.com/forum/viewto ... 74#p145474
In the newest version of the script it just counts # of "Sent" EML files per user to decide if alert is needed or not. Yeah it's inefficient but windows caches nicely & it's safe. Granted if you don't use archiving it's not useful but i find archiving useful in itself & why I added it "day 1".
Bill
hMailServer build LIVE on my servers: 5.4-B2014050402
#hmailserver on FreeNode IRC https://webchat.freenode.net/?channels=#hmailserver
*** ABSENT FROM hMail! Those in IRC know how to find me if urgent. ***

Sjender
Normal user
Normal user
Posts: 49
Joined: 2012-02-25 10:13

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by Sjender » 2014-08-28 11:32

Hello,

We are very happy by using your modified smtplimit script. Unfortunately, we have a problem with e-mails sent by a mailform on sites. It looks like the smtp limit script does not see those messages (generated by PHP-mail).

We require that the sender or the recipient address is an existing address on the mailserver when users send PHP-mail. We can see the messages in SMTP logs from the HMAILSERVER but not in the hmailserver_events.log. Also the outboundstore.txt didn’t saved the send messages from the PHP-mail/site script.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by percepts » 2014-08-28 12:15

this sounds very much like its being caused by your PHP sending routine. If you are using the PHP mail() function then I would suggest that you download PHPMailer class from the web and use that to send mail from PHP. PHPMailer will allow you to specify the sender (From:) and give you more control over your mail.

Sjender
Normal user
Normal user
Posts: 49
Joined: 2012-02-25 10:13

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by Sjender » 2014-08-28 12:37

Thanks for your reply.

I don't think this is possible.
We use the mailserver as a hosting company.
We host thousands of websites and e-mail domains.
We have no control of the way how people send their mails.

Is it expected behaviour that the php mail() function is not counted?
And is there a way to fix this.
Once in a while we have a 'hacked' contactform which is sending out spam.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by percepts » 2014-08-28 13:24

In PHP.ini you have a section like:

[mail function]
SMTP = "your.localhost.name"
smtp_port = 25
sendmail_from = admin@yourdomain.com

When using php mail() function:
whatever is in sendmail_from will be the actual sender and is what you will see as the FromAddress in the COM API and is the sender in the ReturnPath of your received mail headers.
You can put any email address in the From: header (in php mail() additional headers). It may be the same as whats in sendmail_from but its whats in sendmail_from that is used to send the mail and not what is in From:.

If you want to change that behaviour then you must change what you are using to send mail from PHP with.

You now have a problem, you are allowing clients to send mail from PHP forms without using authentication. If you had forced them to use authentication from day 1 then they would not have been able to send mail using the PHP mail function and would have needed to use something like PHPMailer to do it where FromAddress would be required to send mail. As it is, you are always sending from whatever is in sendmail_from and if you check your script logs for that user I think you find your missing counts in there (assuming you have defined sendmail_from in PHP.ini).

AND there is no mandatory need to add From: in additional headers in PHP mail() function. You can send without a From: header.

one resolution is for you to change your webhosting auto definitions of each accounts PHP.ini to set the senmail_from to the clients email address and stop them from being able to alter it at script execution time otherwise they could send mail from any local domain on your server via their web forms (because you are not using authentication for local to external sends)

Note: PHP Mail() function on windows does not provide for authentication when sending whereas PHPMailer does. But you are stuck now with PHP mail() if users are already using it in their web scripts.

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by percepts » 2014-08-28 14:07

Note I am asssuming in above that PHP is running on windows. If its on unix then different stuff happens.

User avatar
mattg
Moderator
Moderator
Posts: 20144
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by mattg » 2014-08-28 15:46

Sjender wrote:Thanks for your reply.

I don't think this is possible.
We use the mailserver as a hosting company.
We host thousands of websites and e-mail domains.
We have no control of the way how people send their mails.

Is it expected behaviour that the php mail() function is not counted?
And is there a way to fix this.
Once in a while we have a 'hacked' contactform which is sending out spam.
Can you show some connection logs of mail being sent through your hmailserver that ISN'T counted. Obfuscate the domains and IP address if you like.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

percepts
Senior user
Senior user
Posts: 5282
Joined: 2009-10-20 16:33
Location: Sceptred Isle

Re: Limit Outbound Mails by User, Domain or Server Average #

Post by percepts » 2014-08-28 21:48

I did another little test and if you don't put From: in additional headers the mail() function will create a From: header and put whatever is in sendmail_from into it so that in COM API FromAddress and From will be the same.
And in the received mail Return-Path and From will be the same.

So as Matt asks, please show logs of one of these mails which isn't added to count.

bigworm
Normal user
Normal user
Posts: 45
Joined: 2007-11-14 22:13

Re: Limit Outbound Mails by User, Domain or Server Average #2

Post by bigworm » 2015-01-14 23:55

So if I just wanted to have it limit based on the average I could set the max per user and per domain to an absurdly high number?
hMailServer 5.4 - Build 1946 + ASSP v2.4.3(14246)

stefans10
Normal user
Normal user
Posts: 32
Joined: 2014-04-25 17:51

Re: Limit Outbound Mails by User, Domain or Server Average #2

Post by stefans10 » 2015-03-04 18:36

max_emails_per_user general limit for all users
max_emails_per_domain general limit for all domains


is that daily, weekly or monthly?

also if I have a user which is blocked and was legitimate how can I unblock and keeps on sending?

User avatar
mattg
Moderator
Moderator
Posts: 20144
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Limit Outbound Mails by User, Domain or Server Average #2

Post by mattg » 2015-03-05 04:23

stefans10 wrote:also if I have a user which is blocked and was legitimate how can I unblock and keeps on sending?
server_average_days sets this number

it is few lines below the max_email settings at the top of the script
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

stefans10
Normal user
Normal user
Posts: 32
Joined: 2014-04-25 17:51

Re: Limit Outbound Mails by User, Domain or Server Average #2

Post by stefans10 » 2015-03-06 14:25

Hi matt,

thanks for the quick answer but i'm not sure you understood me correctly

User avatar
mattg
Moderator
Moderator
Posts: 20144
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Limit Outbound Mails by User, Domain or Server Average #2

Post by mattg » 2017-07-24 09:57

A couple of years later, and yes you are correct, I must have been drinking heavy that day...
Newer version of script is here >> viewtopic.php?f=20&t=28269

I don't think that script allows for un-blocking of a user to allow a bypass of the script
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Post Reply