Postfix mail server with DKIM

Installation and configuration of the mail server (Mail Transfer Agent, MTA) is one of the typical tasks any system administrator faces. Here we will cover the task and by the end of the article will have CentOS 7 with Postfix mail server that listens to he 25 port (SMTP) and delivers messages for the selected domain to users’ mailboxes.

First, we need to have a domain’s DNS records properly configured. This is done in 2 places:

  • A, TXT and MX records are stored at your domain’s registrar, GoDaddy for example.

To check the record use the ‘dig’ command with the record name and domain name as the argument. In our example, my domain andreybondarenko.com is configured as follows:

 ┌[root@andreybondarenko]─[~]
└─10/09 08:37:08-# dig mx andreybondarenko.com
[...]
;; ANSWER SECTION:
andreybondarenko.com.   1800    IN      MX      1 andreybondarenko.com.
[...]

That indicates that the mail for the entire andreybondarenko.com domain is served by the host named ‘andreybondarenko.com’ because I have only one host. If we have more, we can create mx1.andreybondarenko.com and mx2.andreybondarenko.com and they would deliver mail randomly chosen by the client. This is useful for stability, redundancy and load balance.

  • PTR record is set at the hosting provider side. You can either find the record in the control panel or as a technical support service will do this for you.If the hosting provider will refuse to change it, your messages would be rejected by most large mail servers (including Google). This record is called ‘reverse’ and allows anyone to check that the IP address that is used to initiate connection has a valid domain name. It’s a fast and easy way to detect spam bot: it uses an IP address it does not control, so it cannot set the PTR record properly.

Second, the Postfix itself should be installed. I recommend using it as a mail server due to it’s highly flexible, secure, extensible and can provide you high performance on large installations. On the CentOS just run:

    yum install postfix

And the packet manager will install it with all dependencies needed. At the moment the version 2.10.1 is the latest available in the CentOS repository, so I will use it.

Postfix has 2 configuration files. /etc/postfix/master.cf controls process count, their behaviour, should they use a chrooted environment or not for and the defaults are OK for most cases. The /etc/postfix/main.cf controls everything else, we need to change following:

[..]
myhostname = andreybondrenko.com
mydomain = andreybondarenko.com
[..]

These two variables should be set to the valid host name of the host itself and to the domain he is servin mail for. In my case they are the same.

[..]
myorigin = $myhostname
[..]

The myorigin parameter specifies the domain that locally-posted mail appears to come from. The default is to append $myhostname, which is OK for small sites.  If you run a domain with multiple mail servers, you should change this to $mydomain and set up a domain-wide alias database that aliases each user user@users.host. To maintain consistency between sender and recipient addresses,  myorigin also specifies the default domain name that is appended to recipient addresses that have no @domain part. We will use the default setting.

[..]
inet_interfaces = all
inet_protocols = all
[..]

This two settings will tell the smtpd to listen to both IPv4 and IPv6 on all interfaces.

[..]
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, myotherdomainihave.com
[..]

This variable sets all domains we would accept mail to. In this case that’s andreybondarenko.com, myotherdomainihave.com and localhost.

[..]
relay_domains = $mydestination
[..]

This variable sets all domains for we would relay mail in case the recipient is not located on our host.

That is basically all the initial configuration we need: it tells the smtpd to listen to 25 port on all interfaces, accept mail to andreybondarenko.com, localhost and myotherdomainihave.com. By default, it would deliver mail to users mailboxes in the /var/mail, in future, we can set up dovecot to be  an IMAP server and provide the SASL authentication, then we can harden the Postfix against spam, setup greylisting and DKIM. Last thing we need to do is to tell Systemd to start Postfix on boot and to run it now:

systemctl enable postfix
systemctl start postfix

We can now see it’s running in process list:

┌[root@andreybondarenko]─[~]
└─10/09 09:16:44-# ps ax|grep postfix
1764 ? Ss 0:02 /usr/libexec/postfix/master -w

We can connect to the 25 port and send a message with the telnet:

┌[root@andreybondarenko]─[~]
└─10/09 09:19:13-# telnet localhost 25
Trying ::1…
Connected to localhost.
Escape character is '^]'.
220 andreybondrenko.com ESMTP Postfix
helo andreybondarenko.com
250 andreybondrenko.com
mail from:
250 2.1.0 Ok
rcpt to:
250 2.1.5 Ok
data
354 End data with .
test
.
250 2.0.0 Ok: queued as EBA0881589
quit
221 2.0.0 Bye
Connection closed by foreign host.

And see it in the /var/log/millog

Sep 10 09:19:18 andreybondarenko postfix/smtpd[10889]: connect from localhost[::1]
Sep 10 09:19:47 andreybondarenko postfix/smtpd[10889]: EBA0881589: client=localhost[::1]
Sep 10 09:19:54 andreybondarenko postfix/cleanup[10895]: EBA0881589: message-id=20170910091947.EBA0881589@andreybondrenko.com
Sep 10 09:19:54 andreybondarenko postfix/qmgr[1770]: EBA0881589: from=root@andreybondarenko.com, size=352, nrcpt=1

Now it’s time to set up DKIM (see https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail). In fact the only thing that Domain Key technology provides is the information that the person who controls the DNS also controls the mail server. This mean we should set up two places at the same time: the DNS TXT record and special damon on the server side.

First, install opendkim package:

    yum install opendkim

Second, run

┌[root@andreybondarenko]─[~]
└─10/09 18:46:11-# opendkim-default-keygen

Generating default DKIM keys:
Default DKIM keys for freshdaymall.com created in /etc/opendkim/keys.

In the /etc/opendkim/keys there would be 2 files containing public and private keys. Check that permissions are follows for the private key (only opendkim user can read):

┌[root@andreybondarenko]─[/etc/opendkim/keys]
└─10/09 18:50:52-# ll default.private
-r--------. 1 opendkim opendkim 887 Oct  2  2016 default.private

Now we need to correct settings. 

  • /etc/opendkim.conf – configuration file of opendkim
  • /etc/opendkim/keytable — defines the path of private key for the domain
  • /etc/opendkim/signingtable – tells OpenDKIM how to apply the keys.
  • /etc/opendkim/TrustedHosts – defines which hosts are allowed to use keys.

I have copied my public and private keys to the /etc/opendkim

┌[root@andreybondarenko]─[/etc/opendkim]
└─10/09 18:53:40-# ls -la
total 48K
drwxr-xr-x. 3 root opendkim 4.0K Jan 9 2017 .
drwxr-xr-x. 96 root root 12K Sep 8 07:28 ..
-r--------. 1 opendkim opendkim 887 Oct 2 2016 andreybondarenko.com.private
-rw-r-----. 1 root opendkim 333 Oct 2 2016 andreybondarenko.com.txt
drwxr-x---. 2 opendkim opendkim 4.0K Dec 21 2016 keys
-rw-r--r--. 1 root root 106 Oct 3 2016 keytable
-rw-r--r--. 1 root root 60 Oct 3 2016 signingtable
-rw-r-----. 1 opendkim opendkim 378 Dec 21 2016 TrustedHosts

In the /etc/opendkim.conf we need following parameters:

Mode    sv – sign and verify outgoing messages

Socket  inet:8891@localhost – use port 8891 at localhost for communication

ReportAddress  “andreybondarenko.com Postmaster” <me@andreybondarenko.com> – postmaster’s address.

Domain andreybondarenko.com – my domain

Selector        mail – the part to look at the TXT record for the key.

Other parameters could be lived by default.

Then setting /etc/opendkim/keytable as follows:

mail._domainkey.andreybondarenko.com andreybondarenko.com:mail:/etc/opendkim/andreybondarenko.com.private

And the  /etc/opendkim/signingtable

*@andreybondarenko.com mail._domainkey.andreybondarenko.com

First file defines where to look for the private key for the domain (remember that real life server can have many of them) and second defines that all mail should be signed with this key. TrustedHosts is trivial and default:

127.0.0.1
::1

Now we can run opendkim:

systemctl enable opendkim
systemctl start opendkim

Last part on the host is to tell the Postfix to forward all mail to the opendkim first by adding local milter :

smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = inet:127.0.0.1:8891

That’s it! Restart the Postfix to get new config:

systemctl restart postfix

Now the last and very important part: we need to publicize the open part of the key to the world with the DNS TXT record. You need to create record as follows:

  • Name – mail._domainkey
  • Type: TXT
  • TTL: 1 hour
  • Value: v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCvGRWfWPPZVIg0fy7Pr0+rsBsoL6Imt1GBE/QRd3X5Izv1iAJFUsOteaf9TI9EO/YFwoLLahzuoZM1aUU4ED3fHlItEnqXCKQhX8Zripi7cfIO+DRFEhGuQtG6OIuA6+c3ivao7DTPk/IFqY7MG5M3wMvAfV+eIBf1VjmajSwe3wIDAQAB

Last part, the ‘value’ is tricky and may lead to mistakes. You need to read the /etc/opendkim/your_domain.txt or /etc/opendkim/keys/default.txt and copy the value removing all quota symbols (i.e. all “).

Moved to Hetzner and CentOS 8

Turns out I had lost my keys to the Oracle Cloud and the Oracle Cloud lost my PTR record somehow, so the Gmail started to junk and bounce my mail. I decided to go for a Faster.cz (who’s my home provider) VPS, but they had 8GB SSD as an option for a small host, which it too small. Price/featurewise, tried to choose between Leaseweb, OVH and Hetzner, choose Hetzner.

Migration went nice and smooth, just scp’d the /etc and the /var.

Снова о тяжкой (нет) доле эмиграции в ЕС

Буквально вчера опять обсуждали тему, которую по делу стоило бы закрыть раз и навсегда и никогда не открывать: “эмигранты страдают, но не показывают свою боль”, по этому я хочу собрать мысли по поводу того, что действительно для меня изменилось с переездом, я надеюсь, что это поможет колеблющимся решиться в ту или в другую сторону.

TL&DR: если нет завышенных ожиданий, то не будет разочарований. Кому неплохо в Москве, тому везде будет неплохо (обратное тоже верно).

Я переезжал из Москвы, в которой прожил 35 лет, где родились мои родители и их родители. У меня не было особенных проблем, был доход около 5000 евро в месяц на семью, 2 машины, жена, дети, хобби. Не было своего жилья. Я переехал в маленький город на должность сотрудника поддержки на телефоне за еду. В силу PTSD, наличия родственников, друзей и собственности меня не отпускает постоянно сравнивать с Москвой и Россией мою нынешнюю жизнь. Я постоянно вступаю в срачи на тему “где жить лучше”. Я хочу собрать в одном месте то, что я постоянно слышу в таких срачах.

Не нравится – вали! Свалил – молчи

Не секрет, что я был недоволен очень многими вещами в Москве, включая погоду и съезд с ТТК на Ленинградское Шоссе в сторону области. Я считал, что до 2008 тут становилось лучше с каждым годом, до 2012 примерно все было ровно, после 2014 я перестал понимать как жить и дело не только в политике: город и страна развиваются так как мне не нравится. Я всегда был готов высказать то, что я думаю и очень часто получал ответ “так вали”. Что же произошло сразу после того, как я уехал? Мне стали говорить “теперь молчи”. Как будто не было 35 лет жизни, как будто у меня потерялось право на мнение и как будто в статусе гастарбайтера у меня нет маячащей возможности оказаться обратно в Москве, если что-то пошло не так. Это происходит со всеми: вновь прибывший коллега получил свое “молчи” буквально как с трапа самолета сошел, продолжив комментировать срачь в фейсбуке. Есть мнение, что лучше молчать, потому что ты застрял на моменте сваливания, есть мнение, что сам можешь решить, когда молчать.

Ты теперь гражданин …

Люди вне темы не понимают разницы в статусе. Они совсем не в курсе что такое ВНЖ, ПМЖ, Синяя карта, Зеленая карта, гражданство. Какие есть пути получения этих статусов и т.п. Для большинства “я завтра поеду работать в Чехию” совершенно равнозначно тому, что мне дали чешский паспорт и я теперь такой же чех, как вот тот Хонза. При том, что так не работает, даже если ты новый репатриант в Израиле, на сколько я могу судить (есть требования по пребыванию на месте, иначе паспорт отберут). Например, можно встретить обсуждения как легко купить домик на юге Италии (они там копейки), заработав из Москвы. Ну а толку-то в этом домике, если у тебя не будет там возможности жить дольше 90 или 180 дней в году.

Медицина очень плохая, в России за деньги я …

Я сторонник ничего не решать за деньги по договоренности, потому что считаю, что в таком типе отношений люди недостаточно хорошо относятся к своим обязанностям и обещаниям. Я считаю, что единственный критерий того, где медицина хорошая, а где нет – ожидаемая продолжительность жизни. Да, скорая приезжает только при угрозе жизни. Да, если у тебя болит очень сильно, но ты способен ходить, то бери такси. Да, у тебя, эмигранта, точно не будет хорошего терапевта, который будет нормально подходить к твоим болячкам, не экономя деньги страховой и не предлагая БАДы. Да, в Москве за деньги или даже просто по удаче бывают очень хорошие специалисты. Все это так. Но, согласитесь, в России это куда большая лотерея. Просто не надо завышенных ожиданий: будет нормально. К сожалению, могу рассказать офигительные истории как было очень очень плохо за весьма большие деньги и как было просто отлично бесплатно в России. Второй момент, отчего-то я верю, что технологически в ЕС выше уровень и всякие раки лечат лучше. Иначе бы в Германии собирали деньги на лечение ребенка в Москве и евробюрократы тоже бы сюда ездили.

Ты будешь никому не нужен

Стандартная пугалка о том, что твой круг общения обнулится и ты не найдешь себе новых друзей. Я уверен, что это зависит не от того, где ты находишься, а от того, как ты себя ведешь, на сколько открыт, приветлив, готов общаться. Просто начни учить местный язык – ты будешь это делать в коллективе. Просто найди сообщества по твоим хобби. Да, придется приложить некоторые усилия по поиску приятного тебе круга общения, это может занять некоторое время.

Дорого, а платят мало, налоги большие

Один из моих любимых аргументов, что в Москве компьютерщик проще найдет свои 300кк/сек, а в Брно тебе будут платить 1000 евро. Это не совсем верно: главный вопрос в том, почему большая американская компания перевезла вас сюда, а правительство пустило? Ответ в том, что вы дешевле и что вас не хватает. Да, первые два года вы будете ниже рынка, за это время вы отобьетесь у вашей фирмы, поймете что к чему и как, осмотритесь и найдете работу в рынке.

Второй вопрос: нельзя мерить сложные вещи одной метрикой. Экологию, время до работы, чистые улицы и инфраструктуру ты не купишь. Школу без попов и комсомольцев для своих детей – тоже.

Третий вопрос: совершенно другая структура расходов. Еда выше, зато касающееся детей и здоровья поддерживается из бюджета страны, у кого пара детей – поймет. Налоги ниже или такие же (не знаю на счет ИП в РФ), зато за них тебе делают нужные вещи вроде школы, набережной или парка, а не достижения геополитики и подводные группировки спутников с суверенным интернетом. Очень важно, что заемные деньги значительно дешевле, пресловутая ипотека после ПМЖ около 1-2% в зависимости от локации.

Везде свои проблемы, от себя не уедешь

Безусловно! Не стоит ожидать, что все именно так, как рисует воображение. Стоит узнать, что тебя ждет на новом месте. Стоит приоритезировать проблемы. Например, для меня большой проблемой было качество еды после 2014 года – я ее решил переездом и доволен. Меня удручает карательная бюрократия – тут бюрократы тоже долбоёбы, но они не злые, они не хотят тебя унизить. Нельзя уехать от себя, можно приехать к себе.

Трактор в Чехию

С чего все началось? Я работал в Лаборатории Касперского, очень много путешествовал по работе. Я видел ЮАР, Иорданию, Японию и вообще ЮВА, естественно, много раз был по работе в Европе. Я делал вялые попытки что-то поискать внутри Касперского в районе Эмиратов. Столкнулся с тем, что если у тебя жена и дети, то особенно там не развернуться на одну зарплату даже менеджера, что остаться “навсегда” там нельзя, и много чего. Потом подвернулась другая работа, там были перспективы опять же Эмиратов, потом Чехии, но не срослось, потому что нужен я был в Москве как продажник и развивающий бизнес директор. Был 2012й-2013й год, неплохие деньги прямо тут, родился второй ребенок, хобби-внедорожник, уже не было проблем с жильем.

Continue reading

My blog

I write mostly about security, mail, web and linux. My contacts are: mail://me@andreybondarenko.com and shaman007 all around Telegram, FB chat, WhatsApp.

Primary use me@andreybondarenko.com and @shaman007:matrix.andreybondarenko.com

Buy me a beer / Автору на пиво:

shaman007@andreybondarenko.com (paypal)
17QxrNyk9BCrP6LGUbqCNnXCzu4oeoYKaH (btc) 0x57500960FBC986225209E597D5B97065A9A10043 (eth)

Links turned out to be useful / не потерявшие актуальности ссылки:

Растаможка автомобиля из России в Чехии
Поиск работы за границей РФ: доступ из вашей страны запрещен и другие важные вещи
Как правильно искать работу
Почему закон о блокировке сайтов в рунете – это очень плохо

OK Google Mail!

Starting around 11 April Gmail started to mess with the TLS. If in the “send as” settings you had anything in the MX hostname, that does not match what’s going on after your @ and your cert, your mail server got a reject, even if his origin matches the certificate. I had ‘mx.andreybondarenko.com’, had to change to ‘andreybondarenko.com’. I didn’t had touched this settings for ages. Discussion by the link below.

https://support.google.com/mail/thread/39390661?hl=en

Another site move


Aegea is a terrible blog platform, I hate it. No plugins, no security concepts, updating is a pain in the ass. So I need to move my blog yet another time as I’d done it in 2015. Maybe this time the blog would be more alive.

I need ensure that all old URLs would be functional on a new site.

UPD: in one not-so-terrible run moved my https://shaman007.livejournal.com and half of the Aegea content here. Half handjob, sill not that hard.

Windows 10: delete useless keyboard layout

Sometimes for some strange reason Windows 10 adds extra keyboard layout after reboot. In my case it’s always UK that terribly frustrates me when I need @ or “, some users report that it’s DE. You cannot delete this layout normally from anywhere in the interface. Fortunately, there is a way to fix this with the PowerShell magic:

$List = Get-WinUserLanguageList

This would pull all the layouts you have into the $List. Running just ‘Get-WinUserLanguageList’ will show you what you actually have and at which order. Then:

Set-WinUserLanguageList $($list[0], $list[1])

This would make layout list of only what you need, in my case it’s EN and RU. In your case it may be just one element or three elements.

Про распознавание лиц в школах и не только

/*старый текст, просто чтобы не потерять его*/

Почему-то меня сильно задел обсуждаемый в ленте ФБ вопрос о внедренной в китайских школах системе распознавания лиц и китайского Social Score. В комментариях пришли люди, которые внедряли похожую систему в каком-то ВУЗе в РФ, пришло много людей, которые хотели бы такую систему в своих школах для собственных детей. До этого меня задело обсуждение охраны в школе, где люди радовались вышкам с автоматчиками и колючей проволоке по периметру, хотя казалось бы, зона комфорта размером с город или страну гораздо привлекательнее зоны комфорта внутри загородки с охраной или автомобиля. Мне не нравится ЮАР, хотя там Макдональдс автоматчик охраняет, безопасно же!
На сколько я понял их аргументацию pro:

Continue reading