📜 ⬆️ ⬇️

Mail server on your own site via sendmail

Not so long ago, I was faced with the task of providing registered in my catalogs, industrial , automotive and real estate topics, the organization of a personal mailbox with the name of my domain. Instead of giving out direct contact information to users of the directory, it was determined that it would be more productive to give them contact information with the name of my domain. That should increase attendance, both among users and organizations themselves.

Users get the opportunity to write messages to firms from their personal mailbox, saving them in the address book, or in any other convenient way. And organizations will have to answer them, which, as a result, should increase attendance and accordingly monetize my project.

Unfortunately, as soon as I began to address this issue in the largest forums of the RuNet, I received not quite the answer that I expected. At the same time, the best of the proposals came down to demolishing a perfectly working sendmail and installing Postfix and Dovecot on it, which were pulled by other dependencies. And the installation package would look something like this: exim4, exim4-base, exim4-config, exim4-daemon-heavy, dovecot-common, dovecot-imapd, dovecot-pop3d, php5-imap. At worst, fabulous sums were negotiated, as much as $ 2,000, or they were advised to pass by and not to be dishonored.

Unfortunately, it did not take long to disgrace. Since there was no unequivocal instruction that would satisfy my needs, and other possible similar solutions on the network, I decided that this manual would be most welcome.
')
The problem I set was solved with the help of the good old sendmail and small settings in its configurations.

First of all, you need to register the settings in the dns-zone:

For MX:

@ IN MX 10 mx.site.ru. 

And for AAAA:

 @ IN AAAA 2001:0db8:85a3:0000:0000:8a2e:0370:7334 

And for A:

 mx.site.ru. IN A <IP> 

The address specified in the DNS MX will also need to be written in / etc / hosts , adding:

 <IP> mail.site.ru 

We now proceed directly to the sendmail configuration.

Let's start with the file /etc/mail/sendmail.mc . To begin with, we will open the doors for all comers, since by default the smtp-port is open only for distribution. The problem of viruses, spam and dos-attacks will be discussed later. This is done like this:

 DAEMON_OPTIONS(`Port=smtp,Addr=<ip>, Name=MTA-ext')dnl 

Then immediately after recording:

 FEATURE(`use_cw_file')dnl 

Add virtual user and domain tables:

 FEATURE(virtusertable, `hash -o /etc/mail/virtusertable')dnl 

Now create a file where we will put the mail:

 touch "/home/site.ru/public_html/mail" 

And we assign him the rights of ownership to the sendmail agent group:

 chown user:mail /home/site.ru/public_html/mail 

The path and name of the file is conditional and it is recommended to create a file outside the access area for random users.

In the / etc / aliases file, write the name of the virtual user who will receive messages.

 user: /home/site.ru/public_html/mail 


In this case, all mail will be accumulated in the file /home/site.ru/public_html/mail

It is difficult to parse a large file, and it is inconvenient to access it separately from sendmail itself. Therefore, the most convenient option to send directly to the php script, which will process it on the fly.

 user: "|php5-cgi -c /path/to/php.ini /site.ru/public_html/mail.php" 


To send letters from all possible users to the file /site.ru/public_html/mail.php

Let's write the instruction in the / etc / mail / virtusertable file :

 @site.ru user 

This instruction informs that we define all possible users of site.ru as a user, for which we have previously determined the place to store mail. In the future, we will only have to make out who and whose mail belongs to and give to the actual owner on our site.

The final step is to register the list of host names accepted by the sendmail program in a file.
/ etc / mail / local-host-names .

Adding to existing:

 mx.site.ru site.ru (   ) 

Activate changes with the sendmailconfig command.

DDoS protection


To protect against dos-attacks, I will give a few settings that will be useful. They need to be written in the /etc/mail/sendmail.mc file:

 #     .   ,       ( ). Define(confCONNECTION_RATE_THROTTLE',43')dnl #    sendmail.     ,       ( ). Define(confMAX_DAEMON_CHILDREN',40')dnl #      ,      .   - 100. Define(4configSIN_FREE_BLOCKS',100')dnl #    ,  . Define(confMAX HEADERS LENGTH', 4024')dnl #    .     4  (4 194 302 ).      ,         (attachments). Define(confMAX_MESSAGE_SIZE',4194304')dnl 

After that, once again, activate the changes with the sendmailconfig command.

Antispam and antivirus


We will use Dr.Web as an anti-virus program. He will also help us deal with spam. I didn’t use additional spam filters, because after long suffering from the fact that in the gmail.com service, the letters I needed constantly got into spam. Decided to go to yandex. Therefore, I consider cleaning as spam as an individual matter, and the use of any spam lists and filters is quite a dubious pleasure.

Install the key:

 wget -O - http://officeshield.drweb.com/drweb/drweb.key | apt-key add 

Connect the repository:

 nano /etc/apt/sources.list deb http://officeshield.drweb.com/drweb/debian stable non-free 

Update the repository:

 aptitude update 

Install Dr.Web:

 aptitude install drweb-sendmail-av-as 

The main antispam configuration file is /etc/drweb/plugin_vaderetro.conf . We are especially interested in black and white lists:

 WhiteList = /home/site.ru/public_html/mail/WhiteList BlackList = /home/site.ru/public_html/mail/BlackList 

They can also be taken to sections of our website, or to any other editable place, without forgetting the relevant restrictions and security.

That's all. In the next publication, we will analyze the saved form and create a table for it in the database, linking it to the user of the site or directory.

Materials used


www.pettingers.org/code/sendmail-local.html
www.sendmail.com/sm/open_source/docs/m4/features.html
it-e.ru/blogs/administrirovanie/nastrojka-mta-sendmail
www.freebsd.org/doc/ru/books/handbook/sendmail.html
progressive0.livejournal.com/15919.html
adatum.ru/ustanovka-sendmail-dovecot-drweb-na-ubuntu.html

Source: https://habr.com/ru/post/260429/


All Articles