OK, I will try to send you a more useful answer

I will assume that you are familiar with the linux command prompt, know how to install software from the Debian repositories etc. If you are not, I'm afraid that this is going to take too much of my time...
--- DISCLAIMER ---
I started writing this as a simple how-to, but having to rethink steps I took years ago, I instead started from scratch on my spare B2 unit. It turned out to be a bit of a project. I was also unable to complete the project as intended due to problems registering the domain I intended to use as an example, so there might be vital bits missing. In the original text there were several references to the domain name I had chosen. I have simply replaced them all with "mydomain.com"
The contents in this post might be incorrect, or outright dangerous. I will take no responsibility for this!
I know there are other people out there with more experience in general (and probably more recent experience regarding the concerned topics). If any of you have any corrections or comments, please don't feel shy to correct me.
--- END DISCLAIMER ---
dutt wrote:
One of the steps is e-mail. I bought a domain and am now wondering how to set up the included e-mail server to handle incoming and outgoing e-mails for that domain. I've been looking around on the forums and the wiki but haven't found any guides from the ground up. Is dovecot the mailserver or is postfix the mailserver?
Postfix is an SMTP server, or "Mail Transfer Agent" (MTA). It will receive all your emails, and also send all outgoing emails.
It's default behaviour is to send/receive email from anyone to anyone.
As the concept of MTA's are old, pre-dating illicit use of email, security is often a bit of an afterthought...
Dovecot is an IMAP server (even if it supports the old POP3 protocol). It allows you to interact with your email on your server from your favourite email client or web interface.. POP3 had some serious limitations. You basically downloaded the mail to your computer, and that was it. You had a choice to leave them on the server instead of deleting them when downloaded, but not much more.
IMAP allows you to create folders, compose new emails and saving them as drafts etc. And when you connect from the next client, it is all there as you left it on the server. This is a vast improvement over the old POP3 way of accessing your email.
As Dovecot is a bit newer, security is not as “pasted on” as with postfix. We can take advantage of this. More on this later.
The traditional way to secure postfix from sending illicit emails is to only allow outgoing emails originating from within your corporate network. This works for companies but it is no use for me (or you, I guess), I want to send emails from my email client no matter where I am, without having to establish a VPN connection to my home network. We will get around to this later.
dutt wrote:
I talked to my ISP and they block port 25(SMTP, sending e-mail) but if I only go with encrypted e-mails it goes over another port and that should work fine, does the included e-mailserver support that?
If it is true that they only block outgoing traffic on port 25 you are in luck. My ISP blocks it in both directions.
Here is a little howto:
1. Get your domain registered.
I choose mydomain.com at
http://freedns.afraid.org for this demonstration - for no other reason than the domain name popped up in my head and was available. The company I chose just happened to appear in my google search and offered a free service. Please take care here. I use this for this demo only.
(And it still hasn't registered! Be doubly warned!) Do some proper research on what different companies will offer you, and what you need. They often rely on you - the customer- being ignorant. This is probably irrelevant as you seem to already have made your choice.
2. If you can't afford a static IP address, set up a mechanism that automatically updates your DNS when your IP address changes.
ddclient is an excellent software for this.
Configure it as instructed by your registrar. The config file is /etc/ddclient.conf or /etc/ddclient/ddclient.conf depending on version.. Here is a slightly modified example from freedns.afraid.com:
Code: Select all
daemon=5m
timeout=10
syslog=yes # I changed this one...
#mail=root # mail all msgs to root
mail-failure=admin@mydomain.com # This I changed too (used to be root, and also commented out)
pid=/var/run/ddclient.pid # record PID in file.
ssl=yes # use ssl-support. Works with
# ssl-library
use=if, if=eth0
server=freedns.afraid.org
protocol=freedns
login=login_name
password=the_password
mydomain.com,ftp.mydomain.com,irc.mydomain.com,mail.mydomain.com,www.mydomain.com
The syntax can vary. This looks totally different from the one I use for my real domain...
Check that they use "ssl=yes", or it will send your credentials in clear text, and anyone who snoops them can change your domain to point to whatever they want. You might need to install this for it to work:
Code: Select all
sudo apt-get install ibio-socket-ssl-perl
Some seem to require single quotes around the password to work...
This one also happens to use a protocol not supported in the version of ddclient available in the debian repositories, so you would have to download the latest version and install it manually. This is only relevant to the registrar I chose for my little demo. You might use another solution, or even better have a static IP address.
3. Set up your DNS records
Most registrars have web based tools for this. The ddclient configuration above will only update the main “A” records. It would be neater to have one “A” record for mydomain.com and then a bunch of “CNAME” records for all the other variants (they are basically aliases), but this operator did not offer that option. I guess you get what you pay for

. The important bit here is that they set my MX record to mail.mydomain.com, so that one must be kept up-to-date.
4. Set up your B3 to receive emails for your domain. This is done in the web interface.
If you are lucky and you can receive traffic on port 25, that is pretty much done for incoming emails. If, like me, you cannot receive anything on port 25, you will have to investigate other solutions as I mentioned in my earlier post. Try it out. Reboot your server and check the logs. Pay attention to anything from ddclient in syslog, and have a look at the mail.log too... There will most likely be problems that needs fixing, but hey - now we are playing!
5. Prevent your server from being an “open relay”"
Having an “open relay” means having an SMTP server that accepts and sends emails from anyone to everyone. It is a BAD thing. It will make you a persona non grata in the internet community in a very short time, and it can be very hard to recover your credibility. Now as you can receive emails on port 25, but not send them, you should be safe. But you will make it able to send emails, and then all those spam emails coming in on port 25 will cause trouble unless you fix it...
Make sure you have something like this in our /etc/postfix/main.cf:
Code: Select all
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
reject_unauth_pipelining
reject_invalid_hostname
reject_non_fqdn_sender
reject_unknown_sender_domain
reject_non_fqdn_recipient
reject_unknown_recipient_domain
reject_rbl_client dnsbl.njabl.org
reject_rbl_client dnsbl.sorbs.net
reject_rbl_client bl.spamcop.net
The most important bit here is "reject_unauth_destination".
You must however make sure that your authorized destinations are set up correctly. Here is the documentation on the directive:
reject_unauth_destination
Reject the request unless one of the following is true:
- Postfix is mail forwarder: the resolved RCPT TO domain matches $relay_domains or a subdomain thereof, and contains no sender-specified routing (user@elsewhere@domain),
- Postfix is the final destination: the resolved RCPT TO domain matches $mydestination, $inet_interfaces, $proxy_interfaces, $virtual_alias_domains, or $virtual_mailbox_domains, and contains no sender-specified routing (user@elsewhere@domain).
Better check that all is safe:
Code: Select all
~$ sudo postconf relay_domains mydestination inet_interfaces proxy_interfaces virtual_alias_domains
relay_domains = $mydestination
mydestination = localhost.localdomain, localhost, b3, b3.localdomain, b3.mydomain.com, /etc/postfix/bubbadomains, $myhostname, b2, b2.localdomain, b2.mydomain.com, dell, dell.localdomain, dell.mydomain.com
inet_interfaces = all
proxy_interfaces =
virtual_alias_domains = $virtual_alias_maps
(Please note that this was done on my real B3 setup for my real domain (which I have changed to mydomain.com in the text above). The one I created for the demo is not available yet...)
If you get results pointing to other variables, check them too to be sure:
Code: Select all
$ sudo postconf virtual_alias_maps
virtual_alias_maps = $virtual_maps
$ sudo postconf virtual_maps
postconf: warning: virtual_maps: unknown parameter
Looks safe, but I'm no expert
6. Enabling remote access to your postfix server with SASL
You want to send emails from wherever you are, not just when you are at home at your “corporate network”. The trick is to tell postfix to not send emails from just anyone as it is it's default behaviour.
You can set several such directives in the config file /etc/postfix/main.cf.
Be aware that this config file is somewhat confusing as it contains instructions for both receiving and sending emails. The trick is to look for the config option names. An example:
smtp_sasl_security_options = noanonymous
smtpd_sasl_security_options = noanonymous, noplaintext
(I'm sorry I hade to use the "quote " option here. The "code" tag didn't like the syntax and removed the underscores).
The point is that directives beginning with smtp_ is for configuring how the server behaves when it sends emails to other SMTP servers. The directives beginning with smtpd_ is for how the smtp daemon (postfix) itself should behave with incoming emails.
So the two examples above might look similar, but they are completely unrelated.
You often see these directives mixed up in examples, and it can be really confusing.
Luckily there is this, which has many answers:
http://www.postfix.org/documentation.html
OK, so now we have to secure your server.
SASL is a good option. It uses authentication (and thus encryption to protect the credentials when they are sent over the wire) for users who want to send emails through your server. Like many such solutions, it requires a certificate based on public key cryptography. The purpose of such things is both to assure the identity of the server, and to encrypt the traffic. I don't really care about the identity, because I know it's my server. Anyway, I would recommend that you acquire a proper certificate. You can get them for free (cacert.org for example). Or you can use the dummy ones already installed as examples (perhaps not very secure, but all we are protecting here is your credentials which can be changed). Even if you get your own certificate, your email client or browser will say it is untrusted, unless you install the CA root certificate on every computer, or have the cash to pay for a proper one from Verisign or it's ilk...
SASL offers a multitude of options to actually authenticating the user. This is overkill for my purposes (and probably yours).
You can probably spend hours reading here:
http://www.postfix.org/SASL_README.html
I chose the option to use dovecot authentication. It basically means that postfix delegates the authentication to dovecot. Dovecot also has a number of methods of authentication itself – just look at the example files in /etc/dovecot. I decided to stick with the default Excito one though, except I replaced the certificate, public and private key in my original setup. I'm not going to do that for this guide though – I have started from scratch with a freshly installed B2...
So – certificates and keys: look in /etc/dovecot/dovecot.conf and you will see this:
ssl_cert_file = /etc/ssl/certs/dovecot.pem
ssl_key_file = /etc/ssl/private/dovecot.pem
They are pre-installed and you do not know anything about them really. It is probably safe, but if you are paranoid, get your own. You can use the same certificates and keys you generated for HTTPS traffic on your web server (if you are like me

)
To set up postfix to use dovecot authentication, add this to /etc/postfix/main.cf:
Code: Select all
# SASL authentication and authorization in the Postfix SMTP server
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
broken_sasl_auth_clients = yes
smtpd_sasl_security_options = noanonymous, noplaintext
smtpd_sasl_tls_security_options = noanonymous
smtpd_tls_auth_only = yes
If you check your /etc/postfix/master.cf you will see it already has an entry for smtps. Just needs uncommenting:
Code: Select all
smtps inet n - - - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
If you look in /etc/services, you will see that smtps will translate to 465, so all you have to do now is open that port in your B2/B3 firewall. IMAP ports 143 and 993 should already be open by default.
Now try to set up an account against your server in an email client:
Receiving options: Your mailserver name, requires authentication, either STARTTLS on port 143 or SSL on port 993 (I find STARTTLS much quicker).
Sending options: Your mailserver name, requires authentication, SSL on port 465.
Use usernames you have created on your B3 in the web interface and try to send emails between them.
Outgoing mail shouldn't work yet...
7. Setting up a relay host, and a way to connect to it over SMTPS
Note: This is the only way I could get this to work with my relayhost. They do NOT accept anything but SMTPS. I have seen other tutorials using port 587 and TLS, but it has never worked for me...
Now you could just add something like this in /etc/postfix/main.cf:
The problem is that it won't work as your relay server also does not want to be an open relay, and has some security measures implemented. These may vary. I describe what has worked for me...
First install stunnel4:
Now edit /etc/stunnel/stunnel.conf.
I've removed all the settings but these:
Code: Select all
sslVersion = all
chroot = /var/lib/stunnel4/
setuid = stunnel4
setgid = stunnel4
pid = /stunnel4.pid
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
debug = 4
[smtp-tls-wrapper]
accept = 10465
client = yes
connect = outbound.mailhop.org:smtps
I've changed the accept port to 10465 as 465 is already busy on my server.
This should only be available internally. Don't expose your "accept port" to the internet in your firewall...
The connect option is your relay server. So it accepts connections on 10465 and forwards them to outbound.mailhop.org on port 465.
So my relayhost entry in main.cf actually looks like this:
EDIT:
You will have to enable stunnnel4 once it is configured correctly in /etc/default/stunnel4:
So now we need to make it authenticate to the relay server.
Create a file called /etc/postfix/sasl_passwd.
Add your relay server name and your credentials for it:
Note that I specified the localhost port as the remote server as this is what postfix will actually connect to... Better set the permissions to 600 on this file.
Now make it a postfix DB:
Code: Select all
sudo postmap hash:/etc/postfix/sasl_passwd
Now add some directives in main.cf to make it authenticate against the relay server:
Code: Select all
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_auth_enable = yes
Hopefully now everything should be up and working!
It never works on the first try though, so keep an eye on those logs...
8. Some practical things
Edit your alias database /etc/aliases. It might be good to enter aliases for postmaster, root etc, so their messages gets delivered to the admin user instead:
Code: Select all
# See man 5 aliases for format
postmaster: admin@mydomain.com
root: admin@mydomain.com
logcheck: admin@mydomain.com
sms: |"/home/niklas/temp/sms.sh"
After changing this, you must run:
If you have loads of software that installs users and user directories, or if you have unused user accounts, it might be a good idea to put a file called .forward in each such home directory with the contents:
Then any local delivered emails from cron jobs or whathaveyou will find their way to you...
Well, hope you did not find it too daunting and that I didn't miss too much, and good luck!
Cheeseboy