{"id":953,"date":"2011-01-11T15:01:19","date_gmt":"2011-01-11T20:01:19","guid":{"rendered":"http:\/\/blog.timeoff.org\/rick\/?p=953"},"modified":"2011-05-05T16:01:11","modified_gmt":"2011-05-05T20:01:11","slug":"communicating-with-the-outside-world","status":"publish","type":"post","link":"https:\/\/blog.timeoff.org\/rick\/2011\/01\/11\/communicating-with-the-outside-world\/","title":{"rendered":"Communicating With The Outside World"},"content":{"rendered":"<p><em>I recently set out to upgrade a virtual host server from VMware Server to Oracle&#8217;s VirtualBox. The upgrade was a huge success. This is one of several articles where I talk about various aspects of that upgrade, hopefully helping others along the way. You might want to go back and read the introductory article <a title=\"article: Virtualization Revisited\" href=\"https:\/\/blog.timeoff.org\/rick\/2010\/12\/13\/virtualization-revisited\/\" target=\"_self\">Virtualization Revisited<\/a>. Added 5-May-2011: Originally written using Ubuntu Server 10.04, this configuration also works without change on Ubuntu Server 11.04.<\/em><\/p>\n<p>One of the things that I wanted from the new VM host was alerts for anomalous situations. Manually polling for trouble begins as a noble effort but trust me &#8211; after a while you&#8217;ll stop looking. About a year ago I was almost caught by a failing hard drive in a RAID array. Even after that incident, within a month or two I had pretty much stopped paying regular attention.<\/p>\n<p>While setting up monitor\/alert mechanisms on an old Windows server is quite the pain in the ass it&#8217;s a snap on Linux. Delivery of alerts and status reports via email is just perfect for me. All I wanted was the ability to have the system generate SMTP traffic; no messages would ever be received by the system. To prepare for that I set up a send-only email account to use the SMTP server on one of my domains solely for the VM host&#8217;s use as a mail relay. Then I got on with configuring <a title=\"Postfix home page\" href=\"http:\/\/www.postfix.org\/\" target=\"_blank\">Postfix<\/a>, the standard Ubuntu\u00c2\u00a0mailer &#8211; one of several excellent sendmail alternatives.<\/p>\n<p>Now maybe I&#8217;m just a dummy, but I found various aspects of the Postfix and related configurations to be a little tricky. Hence this article, which details what worked for me &#8211; and should work for you, too.<\/p>\n<p>(In the stuff that follows, my example machine is named <em>foo<\/em> and it&#8217;s on an internal TLD called <em>wan<\/em>. My example machine&#8217;s system administrator account is <em>sysadmin<\/em>. My SMTP server is on <em>mail.example.com<\/em> listening on <em>port 1212<\/em>. The SMTP account is <em>username<\/em> with a password of <em>yourpassword<\/em>.)<\/p>\n<h4>Getting Started &#8211; Basic Configuration<\/h4>\n<p>Begin by installing Postfix, as you would any package.<\/p>\n<p><code>$ sudo apt-get install postfix<\/code><\/p>\n<p>For now, just hit Enter through the install questions. We&#8217;ll configure it properly following the install. You&#8217;ll be asked for the <em>general type of mail configuration<\/em> and <span style=\"text-decoration: underline;\">Internet Site<\/span> will be the default. Accept that by pressing Enter. You&#8217;ll be asked for the <em>System mail name<\/em> and something will probably be pre-filled. Accept that, too.<\/p>\n<p>Now, go back and do a proper basic configuration.<\/p>\n<p><code>$ sudo dpkg-reconfigure postfix<\/code><\/p>\n<p>Several questions will follow. Here&#8217;s how to respond.<\/p>\n<p>For the <em>general type of mail configuration<\/em> choose <span style=\"text-decoration: underline;\">Internet Site<\/span>.<\/p>\n<p>Set the domain name for the machine. The panel provides a good explanation of what&#8217;s needed here, and chances are good that it&#8217;s pre-filled correctly. By example, <code>foo.wan<\/code>.<\/p>\n<p>Provide the username of the system administrator. The panel provides a good explanation of what&#8217;s needed here. Use the name of the account that you specified when you installed Ubuntu. By example, <code>sysadmin<\/code>.<\/p>\n<p>Provide a <em>list of domains<\/em> for which the machine should consider itself the final destination. The panel provides an OK explanation and it&#8217;s probably already pre-filled more-or-less correctly. But look carefully at the list that appears in the panel and edit it if it has obvious errors like extra commas. Again, using my example machine, a list like this is appropriate:<\/p>\n<p><code>foo.wan, localhost.wan, localhost<\/code><\/p>\n<p>You&#8217;ll be asked whether or not to <em>force synchronous updates<\/em> on the mail queue. Answer <span style=\"text-decoration: underline;\">No<\/span>, which is likely the default.<\/p>\n<p>Next, specify the <em>network blocks<\/em> for which the host should relay mail. This entry is pre-filled based on the connected subnets. Unless you&#8217;ll be using an external SMTP server that requires it, you can simply remove all of the IPv6 stuff that appears here, leaving only the IPv4 entry which will probably look something like this:<\/p>\n<p><code>127.0.0.0\/8<\/code><\/p>\n<p>Specify the mailbox size limit. The default is zero, meaning no limit. Accept that. Remember, all we&#8217;re planning to do is send mail, not receive it.<\/p>\n<p>Set the character used to define a <em>local address extension<\/em>. The default is <span style=\"text-decoration: underline;\">+<\/span>. Accept it.<\/p>\n<p>Choose the Internet protocols to use. Again, keeping with our earlier IPv4 decision select <span style=\"text-decoration: underline;\">ipv4<\/span> from the list and accept it.<\/p>\n<p>That&#8217;s it for the basic Postfix configuration! Next you&#8217;ll configure Postfix to do SMTP AUTH using SASL (saslauthd).<\/p>\n<h4>SMTP AUTH using SASL (saslauthd)<\/h4>\n<p>Since there are several commands to issue as root it&#8217;s convenient to <code>sudo<\/code> yourself as root to save some typing. Good practice dictates you should logout the root account just as soon as you&#8217;re finished.<\/p>\n<p>Be careful. In this list of commands there is one &#8211; it sets smtpd_recipient_restrictions &#8211; that is quite long and may have wrapped on your display. Be sure to issue the entire command.<\/p>\n<p><code>$ sudo -i<br \/>\n# postconf -e 'smtpd_sasl_local_domain ='<br \/>\n# postconf -e 'smtpd_sasl_auth_enable = yes'<br \/>\n# postconf -e 'smtpd_sasl_security_options = noanonymous'<br \/>\n# postconf -e 'broken_sasl_auth_clients = yes'<br \/>\n# postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'<br \/>\n# postconf -e 'inet_interfaces = all'<br \/>\n# echo 'pwcheck_method: saslauthd' &gt;&gt; \/etc\/postfix\/sasl\/smtpd.conf<br \/>\n# echo 'mech_list: plain login' &gt;&gt; \/etc\/postfix\/sasl\/smtpd.conf<br \/>\n<\/code><\/p>\n<p>Then press ctrl-D to logout the root account.<\/p>\n<p>The next step is to configure the digital certificate for TLS.<\/p>\n<h4>Configure the Digital Certificate for TLS<\/h4>\n<p>Some of the commands that follow will ask questions. Follow these instructions and answer appropriately, modifying your answers to suit your situation. As earlier, <code>sudo<\/code> yourself to root and logout from root when finished.<\/p>\n<p><code>$ sudo -i<br \/>\n# openssl genrsa -des3 -rand \/etc\/hosts -out smtpd.key 1024<\/code><\/p>\n<p>You&#8217;ll be asked for the <em>smtpd.key passphrase<\/em>. Enter one and remember it. You&#8217;ll need to type it twice, as is customary when creating credentials. Then continue.<\/p>\n<p><code># chmod 600 smtpd.key<br \/>\n# openssl req -new -key smtpd.key -out smtpd.csr<\/code><\/p>\n<p>You&#8217;ll be asked for your smtpd.key passphrase. Enter it.<\/p>\n<p>Next you&#8217;ll be asked a series of questions that will make up a Distinguished Name, which is incorporated into your certificate. There&#8217;s much you can leave blank by answering with a period only. Here&#8217;s a sample set of responses (underlined) based on my US location and example system.<\/p>\n<p><code>Country Name (2 letter code) [AU]:<span style=\"text-decoration: underline;\">US<\/span><br \/>\nState or Province Name (full name) [Some-State]:<span style=\"text-decoration: underline;\">Texas<\/span><br \/>\nLocality Name (eg, city) []:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nOrganization Name (eg, company) [Internet Widgits Pty Ltd]:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nOrganizational Unit Name (eg, section) []:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nCommon Name (eg, YOUR name) []:<span style=\"text-decoration: underline;\">Rick<\/span><br \/>\nEmail Address []:<span style=\"text-decoration: underline;\">sysadmin@foo.wan<\/span><br \/>\nA challenge password []:<span style=\"text-decoration: underline;\">some-challenge-password<\/span><br \/>\nAn optional company name []:<span style=\"text-decoration: underline;\">.<\/span><\/code><\/p>\n<p>Then continue.<\/p>\n<p><code># openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt<\/code><\/p>\n<p>You&#8217;ll be prompted for your smtpd.key passphrase. Enter it.<\/p>\n<p>Then continue.<\/p>\n<p><code># openssl rsa -in smtpd.key -out smtpd.key.unencrypted<\/code><\/p>\n<p>You&#8217;ll be prompted for your\u00c2\u00a0smtpd.key passphrase. Enter it.<\/p>\n<p>Then continue.<\/p>\n<p><code># mv -f smtpd.key.unencrypted smtpd.key<br \/>\n# openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650<\/code><\/p>\n<p>You&#8217;ll be asked for a <em>PEM passphrase<\/em>. Enter one and remember it. You&#8217;ll need to type it twice, as is customary when creating credentials.<br \/>\nLike earlier, you&#8217;ll be asked a series of questions that will make up a Distinguished Name, which is incorporated into your certificate. There&#8217;s much you can leave blank by answering with a period only. Here&#8217;s a sample set of responses (underlined) based on my US location and example system.<\/p>\n<p><code>Country Name (2 letter code) [AU]:<span style=\"text-decoration: underline;\">US<\/span><br \/>\nState or Province Name (full name) [Some-State]:<span style=\"text-decoration: underline;\">Texas<\/span><br \/>\nLocality Name (eg, city) []:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nOrganization Name (eg, company) [Internet Widgits Pty Ltd]:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nOrganizational Unit Name (eg, section) []:<span style=\"text-decoration: underline;\">.<\/span><br \/>\nCommon Name (eg, YOUR name) []:<span style=\"text-decoration: underline;\">Rick<\/span><br \/>\nEmail Address []:<span style=\"text-decoration: underline;\">sysadmin@foo.wan<\/span><\/code><\/p>\n<p>Next, issue the remaining commands.<\/p>\n<p><code># mv smtpd.key \/etc\/ssl\/private\/<br \/>\n# mv smtpd.crt \/etc\/ssl\/certs\/<br \/>\n# mv cakey.pem \/etc\/ssl\/private\/<br \/>\n# mv cacert.pem \/etc\/ssl\/certs\/<\/code><\/p>\n<p>Then press ctrl-D to logout the root account.<\/p>\n<p>Whew! We&#8217;ll continue by configuring Posfix to do TLS encryption for both incoming and outgoing mail (even though we&#8217;re only planning on sending mail at this point).<\/p>\n<h4>Configure Postfix to Do TLS Encryption<\/h4>\n<p>As earlier,\u00c2\u00a0<code>sudo<\/code> yourself to root and logout from root when finished.<\/p>\n<p><code>$ sudo -i<br \/>\n# postconf -e 'smtpd_tls_auth_only = no'<br \/>\n# postconf -e 'smtp_use_tls = yes'<br \/>\n# postconf -e 'smtpd_use_tls = yes'<br \/>\n# postconf -e 'smtp_tls_note_starttls_offer = yes'<br \/>\n# postconf -e 'smtpd_tls_key_file = \/etc\/ssl\/private\/smtpd.key'<br \/>\n# postconf -e 'smtpd_tls_cert_file = \/etc\/ssl\/certs\/smtpd.crt'<br \/>\n# postconf -e 'smtpd_tls_CAfile = \/etc\/ssl\/certs\/cacert.pem'<br \/>\n# postconf -e 'smtpd_tls_loglevel = 1'<br \/>\n# postconf -e 'smtpd_tls_received_header = yes'<br \/>\n# postconf -e 'smtpd_tls_session_cache_timeout = 3600s'<br \/>\n# postconf -e 'tls_random_source = dev:\/dev\/urandom'<\/code><\/p>\n<p>This next configuration command sets the host name, and this one uses my example machine&#8217;s host name. You should use your own instead.<\/p>\n<p><code># postconf -e 'myhostname = foo.wan'<\/code><\/p>\n<p>Then press ctrl-D to logout the root account.<\/p>\n<p>The postfix initial configuration is complete. Run the following command to start the Postfix daemon:<\/p>\n<p><code>$ sudo \/etc\/init.d\/postfix start<\/code><\/p>\n<p>The Postfix daemon is now installed, configured and runing. Postfix supports SMTP AUTH as defined in RFC2554. It is based on SASL. It is still necessary to set up SASL authentication before you can use SMTP.<\/p>\n<h4>Setting Up SASL Authentication<\/h4>\n<p>The libsasl2-2 package is most likely already installed. If you&#8217;re not sure and want to try to install it you can, no harm will occur. Otherwise skip this command and simply continue.<\/p>\n<p><code>$ sudo apt-get install libsasl2-2<\/code><\/p>\n<p>Let&#8217;s continue the SASL configuration.<\/p>\n<p><code>$ sudo mkdir -p \/var\/spool\/postfix\/var\/run\/saslauthd<br \/>\n$ sudo rm -rf \/var\/run\/saslauthd<\/code><\/p>\n<p>Create the file <code>\/etc\/default\/saslauthd<\/code>.<\/p>\n<p><code>$ sudo touch \/etc\/default\/saslauthd<\/code><\/p>\n<p>Use your favorite editor to edit the new file so that it contains the lines which follow. Just to be clear, the final line to add begins with &#8220;<code>MECHANISMS=<\/code>&#8220;.<\/p>\n<p><code># This needs to be uncommented before saslauthd will be run<br \/>\n# automatically<br \/>\nSTART=yes<\/code><\/p>\n<p><code> <\/code><\/p>\n<p><code>PWDIR=\"\/var\/spool\/postfix\/var\/run\/saslauthd\"<br \/>\nPARAMS=\"-m ${PWDIR}\"<br \/>\nPIDFILE=\"${PWDIR}\/saslauthd.pid\"<\/code><\/p>\n<p><code> <\/code><\/p>\n<p><code># You must specify the authentication mechanisms you wish to use.<br \/>\n# This defaults to \"pam\" for PAM support, but may also include<br \/>\n# \"shadow\" or \"sasldb\", like this:<br \/>\n# MECHANISMS=\"pam shadow\"<\/code><\/p>\n<p><code> <\/code><\/p>\n<p><code>MECHANISMS=\"pam\"<\/code><\/p>\n<p>Save the file.<\/p>\n<p>Next, update the dpkg <em>state<\/em> of <code>\/var\/spool\/portfix\/var\/run\/saslauthd<\/code>. The saslauthd init script uses this setting to create the missing directory with the appropriate permissions and ownership. As earlier,\u00c2\u00a0<code>sudo<\/code> yourself to root and logout from root when finished. Be careful, that&#8217;s another rather long command that may have wrapped on your display.<\/p>\n<p><code>$ sudo -i<br \/>\n# dpkg-statoverride --force --update --add root sasl 755 \/var\/spool\/postfix\/var\/run\/saslauthd<\/code><\/p>\n<p>Then press ctrl-D to logout the root account.<\/p>\n<p>Test using telnet to connect to the running Postfix mail server and see if SMTP-AUTH and TLS are working properly.<\/p>\n<p><code>$ telnet foo.wan 25<\/code><\/p>\n<p>After you have established the connection to the postfix mail server, type this (substituting your server for mine, of course):<\/p>\n<p><code>ehlo foo.wan<\/code><\/p>\n<p>If you see the following lines (among others) then everything is working perfectly.<\/p>\n<p><code>250-STARTTLS<br \/>\n250-AUTH LOGIN PLAIN<br \/>\n250-AUTH=LOGIN PLAIN<br \/>\n250 8BITMIME<\/code><\/p>\n<p>Close the connection and exit telnet with this command.<\/p>\n<p><code>quit<\/code><\/p>\n<p>We&#8217;re almost there, promise.<\/p>\n<p><strong>Setting External SMTP Server Credentials<\/strong><\/p>\n<p>Remember, we set out to use an external Internet-connected SMTP server as a mail relay and this is how that is set up. I mentioned at the beginning of the article that I had set up a dedicated account on one of my domains. You might use one on your ISP. I would not, however, use your usual email account.<\/p>\n<p>You&#8217;ll need to manually edit the <code>\/etc\/postfix\/main.cf<\/code> file to add these lines:<\/p>\n<p><code>smtp_sasl_auth_enable = yes<br \/>\nsmtp_sasl_security_options = noanonymous<br \/>\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/saslpasswd<br \/>\nsmtp_always_send_ehlo = yes<br \/>\nrelayhost = [mail.example.com]:1212<\/code><\/p>\n<p>Of course, you&#8217;ll modify the relayhost = line to specify your external SMTP server. If you don&#8217;t need a port number then simply leave off the colon and port number following the closing bracket. I included the port number as a syntax example in case you needed to use one.<\/p>\n<p>Did you notice the hash file mentioned in the lines you just added to<code>\/etc\/postfix\/main.cf<\/code>? It holds the SMPT server logon credentials, and it&#8217;s time to create it.<\/p>\n<p><code>$ sudo touch \/etc\/postfix\/saslpasswd<\/code><\/p>\n<p>Use your favorite editor to edit the file, adding the credentials with a line like this:<\/p>\n<p><code>mail.example.com     username@example.com:yourpassword<\/code><\/p>\n<p>The components of the line you&#8217;re putting in the new file should be obvious.<\/p>\n<p>(Before you cry foul&#8230; Yes, I&#8217;m well aware of the risk of storing credentials in the clear. It&#8217;s a manageable risk to me <em>in this case<\/em> for the following reasons. The physical machine is under my personal physical control. The credentials are dedicated to this single purpose. If the server becomes compromised\u00c2\u00a0I can disable the credentials from anywhere in the world I can obtain an Internet connection. If I&#8217;m dead and can&#8217;t do that, well, I guess it&#8217;s SEP and my incremental contribution to the SPAM of the world will torment my soul until the end of time. Your situation may be different and I leave it to you to secure the credentials.)<\/p>\n<p>Anyway, before postfix can use that horribly insecure file it needs to be hashed by postmap:<\/p>\n<p><code>$ sudo postmap \/etc\/postfix\/saslpasswd<\/code><\/p>\n<p>With that done, restart postfix.<\/p>\n<p><code>$ sudo \/etc\/init.d\/postfix restart<\/code><\/p>\n<p>Applications that know how will now be able to generate mail but it&#8217;ll be convenient to be able to do it from the command line. Besides making testing of this configuration easier you&#8217;ll then be able to have your own scripts send messages with ease. For that you&#8217;ll need just one more package.<\/p>\n<h4>Installing the mailutils Package<\/h4>\n<p>Simple. Install the mailutils package.<\/p>\n<p><code>$ sudo apt-get install mailutils<\/code><\/p>\n<p>That&#8217;s it!<\/p>\n<p>Try test sending some email from the command line. Substitute the address at which you usually receive mail for my example <code>youraddress@yourserver.com<\/code>.<\/p>\n<p><code>$ echo \"body: outbound email test\" | mail -s \"Test Subject\" youraddress@yourserver.com<\/code><\/p>\n<p>Check your inbox.<\/p>\n<h4>Wrapping Up<\/h4>\n<p>Well, that wasn&#8217;t so bad.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently set out to upgrade a virtual host server from VMware Server to Oracle&#8217;s VirtualBox. The upgrade was a huge success. This is one of several articles where I talk about various aspects of that upgrade, hopefully helping others along the way. You might want to go back and read the introductory article Virtualization &hellip; <a href=\"https:\/\/blog.timeoff.org\/rick\/2011\/01\/11\/communicating-with-the-outside-world\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Communicating With The Outside World<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[82,84,8,83,7,81],"_links":{"self":[{"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/posts\/953"}],"collection":[{"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/comments?post=953"}],"version-history":[{"count":0,"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/posts\/953\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/media?parent=953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/categories?post=953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.timeoff.org\/rick\/wp-json\/wp\/v2\/tags?post=953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}