Remote personal mail

29/09/04

I'm just a poor wayfaring stranger
Traveling through this world bellow
There is no sickness, no toil, no danger
In that bright land to which I go
I'm going there to see my Father
And all my loved ones who've gone home
I'm just going over Jordan
I'm just going over home
I know dark clouds will gather 'round me,
I know my way is hard and steep
Yet beauteous fields arise before me
Where God's redeemed their virgils keep
I'm going there to see my mother
She said she'd meet me when I come
So I'm only going over Jordan
I'm only going over home

Wayfaring Stranger as sung by the late Johnny Cash

I was home. Situation perfected. I had a nice e-mail user agent [MUA] (MessengerPro), a robust mail transfert agent [MTA] (POPstar) and a very powerfull anti spam software (Jan-Jaap van der Geer's SpamStamp). Everything was running so fine. Mail fetched from four different sources, filtered out of it's junk (75%), archived in a wise way and with mailing list sorted out automatically. The process was running smoothly as I had a 2Mb ADSL connection to be always on through my NetGear modem/router and my NetGear switch.

My LAN setup
My LAN setup (simplified)

Then I had so leave home, first because I started working then because I was forced to take vacations. I felt lost. I could use webmail interfaces for some of my e-mail sources but that was painful and I had no access to my precious archives. It was a messy and uncomfortable waste of time.

I decided that there was something to do about it.

The hint

On a random surf session, I discovered on R-Comp's site that there was a MessengerPro Server edition. It was advertised as a software to share your email across your home network so that you can easily access it on other machines, even if they are PCs or other platforms. After a few inquiries to that compagny that is very supportive, I understood that this product was indeed the same MUA I was using but that it was acting as an IMAP 4 rev 1 server. That sounded very interesting though quite expensive (60£ upgrade).

I've always fancied IMAP but never got into a context where I had an IMAP server to connect through my MUA. I just liked the concept. I told myself that if my MUA could be transform to act as an IMAP server, my router could help route it's trafic from LAN to WAN, though turning this outside world where I had so little facilities into a part of my home informationnal estate.

Prequisite routing test

To test the way my router works with the incoming flow of data, i ran on a windows box TYPSoft's FTP Server that I like because it's simple, easy to configure, straightforward and apparently safe. I went to my NetGear router's web-based interface. In the port worwarding page, I chose the FTP (port 21) service and entered Piotr's local IP, Piotr being the network name of that PC. I checked it out with friends and it worked as expected.

The trouble with my Wanadoo ADSL connection is that they don't provide me with a fixed static IP. It's an optional service that costs 15 € per month and I decided that it was too expensive. I also enjoy the fact that I don't have a static IP for security reasons you'll understand. Without a static IP, how could I make connection from WAN to my LAN ?

Where am I ?

The first idea that came to my mind was to use a service like DynDNS. That was a pretty good obvious solution but it implied a registration process and yet another software installation on the PC, so I decided I wanted something else. I was after a way to broadcast my temporary IP to me.

Perl came to rescue me. My first idea was to place on one of my internet servers a script that doesn't do much but print the current IP of the visitor. That's simple to achieve. It can go something like this :

#!/usr/bin/perl
use CGI;
$query = new CGI;
$ip =  $ENV{'REMOTE_ADDR'};
print "Content-type: text/html\n\n";
print "$ip";
exit(0);

Piece of cake. Too easy. The trouble is that i am using mutualized hosting for my servers and I didn't find them too reliable. The other conssideration was that it would cause some undue trafic. I googled a bit and found this site, ShowMyIP.com that has the sole purpose of showing the visitor's IP which was exactly what I was after. It's a free service, bots are welcomed and no registration is required. You are allowed 1500 queries a day and that's more than I need. It has an XML/SOAP/RSS interface but my choice was the simplest interface http://simple.showmyip.com/. It returns to your query something like this :

82.123.61.89 (FR-France) http://www.showmyip.com Sat, 04 Sep 2004 06:35:21 GMT
(5 of 1500 allowed today)
alternate access in XML format at: http://www.showmyip.com/xml 
alternate access via SOAP at: http://www.showmyip.com/soap/server.php 
alternate access via RSS feed at: http://www.showmyip.com/rss.php

That's much more than I need !

Even smarter locator

I ran this a few times and found that this request was taking too much time and was using a service that might be down some day. I wanted a definitive solution. My idea was that if my computer only knows it's own IP (192.168.0.21) on the LAN, the router knows the real WAN IP of my home network. I checked all around and found a hack to get that data. My DM602 Netgear modem/router has a nice web-based interface but it needs identification and it only returns complicated HTML. Anyway that was not gonna stop me from scraping and using advanced spidering.

The code that follow must be specific to my model of router but you'll find ways to adapt it to your own case. I use the classic LWP with credentials then I use a regexp to scrap the IP out of the HTML code. Here's my code :

use LWP;
$browser = LWP::UserAgent->new;
$browser->credentials('192.168.0.1:80','DM602','admin'=>'password');
$answer = $browser->get("http://192.168.0.1/PopOutPage?id=2");
$html = $answer->content;
$html =~ /(\d+\.\d+.\d+\.\d+)/;
$ip = $1;

Simple as that. The big advantage is that my data is far more reliable and can be checked as often as I want. No quotas of querries, no downtime, the ideal situation. For universality's sake, I'll continue without this smarter hack to get my IP because it's all so vendor and model specific.

The IP broadcaster

My IP broadcaster is quite simple by now but I intend to turn it into a windows service when I find the time, using the module Win32::Deamon. It will run on the PC because RISC OS still lacks a decent perl distribution. My broadcasting method of choice is of course FTP. The program is pure perl without any exotic modules that don't come with the standard distribution. I had problems with Window's task scheduller so I opted for a deamon, a program working on an infinite loop and sleeping most of the time. It goes something like this :

#!perl -w
use LWP::Simple;
use Net::FTP;
$s = 1;
$pip = "10.0.0.10";
$ip = "192.168.0.1";
until ($s == 2) {
  # Fetch the IP
  $html = get ("http://simple.showmyip.com");
  $html =~ /(\S*)\s/;
  $ip = $1;
  # If my IP has changed then FTP
  if ($ip ne $pip) {
    # write txt to local file
    open (FILE, "> F:/ip.txt");
    print FILE $ip;
    close (FILE);
    # Transfer the file
    $didit = 1;
    $ftp = Net::FTP->new("ftp.myhost.com");
    if ($ftp->login("mylogin","mypwd")) {
      $ftp->ascii;
      $ftp->cwd("/web");
      $ftp->put("C:/ip.txt","lanip.txt") or $didit = 0;
      $ftp->quit;
    } else { 
      $didit = 0;
    }
    if ($didit == 1) {
      # if ftp transfer seems OK
      $pip = $ip;
    } else {
      # if ftp transfer got wrong, let it try again next time
      $pip = "10.0.0.10";
    }
    unlink ("F:/ip.txt");
  }
  # Daemons shall sleep
  sleep 60*10;
}
exit(0);

The only interesting bit is the regexp to scrap the IP from ShowMyIP's output and the fact that to save banwidth, I transfer my IP to my host only if it has changed. Note that if we can't login, we reset the former IP variable so that when it next retries, it'll guess it's a new IP that has got to be transfered. As a side-note and I'm telling you because I've had this problem, it is a good idea to also publish the file on another internet server because of downtime on thoses distant machines. You can also have the daemon running in another LAN computer if you can.

Now I can find somewhere I only know a file containing my home's IP. I keep the location secret for security reasons although I am well protected. If my IP has changed as it happens at least once a day, then it'll be only ten minutes before my new IP is present on the server. I could has lowered this delay to one minute given the quota allowed by ShowMyIP.com but I just wanted to spare those nice fellows. If you are using my smart trick to get your IP from the router, you could reduce a lot the duration of the daemon's sleep.

Additional monitoring

While the PC is busy retrieving data and sending it, I told myself that it could help me check if the Iyonix is doing alright. I use the excellent StatusD by Chris Williams to keep an eye on what my Iyonix is doing while I'm away. It generates statistics, gathers various informations about the machine and then uploads its repport using FTPc to my internet server. The web page generated with this method goes something like this. It's exactly what I need to make sure that my Iyonix is doing alright, up and ready to serve data. I thought however that, as I've configured StatusD to upload data only every 150 minutes, I could add a few lines to my broadcaster to double check. It'll just ping the Iyonix and only write data to the IP file only if there's something wrong like this :

use Net::Ping;
$p = Net::Ping->new();
print FILE "Iyonix down " unless $p->ping("192.168.0.21");
$p->close();

If the Iyonix had support for WakeOnLAN and I knew about home automation, I imagine that I could have the PC try to rescue the Iyonix, but for now on that's just a wild dream.

WAP it up

As the location of the file where I store my home IP is supposed to be kept secret, I'll do anything to keep it secret. I have a Nokia cellphone so I thought I could be a good idea to use the WAP to get this precious IP. It's so easy to achive, a simple WML file to upload to the server. WML is yet another markut language I don't know but I managed to get something tiny to work. Here's a minimal structure for the file we're generating and uploading :

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
 <card id="card1" title="ip">
 <p>21.33.161.193</p>
</card>
</wml>

Setting up before the IMAP server

The first thing I did was to change the Iyonix's LAN IP from DHCP to a fixed one so that I can safely route trafic. I updated my router's port forward right, so that port 143 goes to the Iyonix because that's the IMAP port.

Port Forwarding setup

IMAP server installation

Once I got MessengerPro Server from RComp, it's just a matter of copying the files over my previous copy of MessengerPro and setting it to Messenger Network Server in two mouse clicks. Then starts the re-indexing of my messages, a process that took me a minute to complete. That's it, it must be ready to. For security's sake, I gave a password to my usual user name. Before going any further, I did a first test over the LAN. On Piotr, I set Mozilla email to connect to the IMAP server and I worked just fine. The speed is amazing over my 100MB switched LAN, although the installation notes from RComp mentions that Castle has warned them of a potential risk of networking flood when running at such speed. Now that it's working on the LAN, let's figure out the mysterious ways of the WAN.

MessengerPro Server Choices

The ways home

At this point there's an IMAP server running and I know where it's located. Maybe time has come for me to come back to the global ideas I've had about this whole process and choices I've made. As I wanted remote access to my mail, I had to consider the worst case I could imagine, having to cope with a computer, let's say a windows box, with a minimal software setp and no right to install new things. My guess was that I would find a browser and maybe a telnet client.

I tell you about the telnet client because my first idea was to get Piotr (you remember, my PC) to run a telnet server and use a port of best text-based MUA I knew about, Mutt. It would have been nice because of Mutt's reputation under linux. It might have been maybe painfull to install though as it would require a whole Cygwin process. That sounded nice, though. Too bad I don't have a linux box on my LAN.

Then I had second thoughts about the availability of a telnet client in my worst case scenario. I had to stick to the web browser, so I had to find a webmail able to connect to an IMAP server. There are two options: one is to run a web server on the LAN (Apache under windows or WebJames under Risc OS) and have it run a perl or PHP webmail or find an internet website that provides the service.

The local option seemed to be the safest and I wrote a POP3 webmail in perl a few months ago. The problem is that the windows port of Apache has a bad reputation and the Risc OS perl port is too narrow but that not what led me to my decision: I wanted to deal with my mail under an SSL encrypted https connection and I have no clue how I could perform this on a local server. Certificates don't come easily or free. So I move on to the other way, a WAN webmail server.

Final steps

Looking for a webmail internet site that permits to connect to an IMAP server, I saw I had many options but with the advice of R-Comp, I chose mail2web.com. They seem to have a good reputation, they offer secure login with an RC4 128 bit encryption and support IMAP. I had my universal solution. I go to mail2web.com select advanced login, then secure login, enter my details (IP, login, password) and that's it. The speed is correct and the interface is alright. I gain access to my inbox, my mailing lists, my archive groups. All over the wired world, I am completly remote. It almost feels like home wherever I am altough no MUA reaches the intimacy I have with MessengerPro. Situation perfected.

Schematics of what MessengerPro Server does for me
How my software plan goes with MessengerPro Server

Traveller's notes

After a few weeks of testing from Japan to France, my advice is to make sure that your machine runs no application with a risk of stalling or issueing any form of non-multitasking event. While away, an IMAP client is obviously the best solution because it is more confortable than a webmail but there's still this solution in case you don't have a choice. As an IMAP client on a windows box, I would have recommended Mozilla but at the time of writing, there are still unsolved issues when accessing large archive folders, but then again any IMAP client will do the job as long as it has support for the version 4 of the protocol that is very widespread. Even Microsoft outlook will do the job if you don't have a choice.

Tens of thoushand kilometers away, with regular ADSL connections at both ends, the speed is still good and the experience very enjoyable. Setting up the IMAP client is very easy, just enter the current IP, the login and the password. As far as the SMTP is concerned, I prefer to use the local one of the remote host instead of using MessengerPro's SMTP relay for security concerns altough I use it on my LAN with the correct IP limitation.

This article was published as it was in Archive in January '05'
I have now switched to an 8Mb ADSL connection and turned to DynDNS with clients in my NetGear Router and DDNSC. The Mozilla foundation has improved IMAP support also in ThunderBird. It all works fine. MessengerPro Server 3 has arrived and I've changed my MTA to R-Comp's Hermes.
Thanks to Colin Granville, I can now check for new messages on my home Iyonix with my Samsung SGH-D500E cellphone.