#!/usr/bin/perl # $Header: /mhub4/sources/imap-tools/imapdump.pl,v 1.5 2009/12/03 06:16:11 rick Exp $ ####################################################################### # Program name imapdump.pl # # Written by Rick Sanders # # Date 1/03/2008 # # # # Description # # # # imapdump.pl is a utility for extracting all of the mailboxes # # and messages in an IMAP user's account. When supplied with # # host/user/password information and the location of a directory # # on the local system imapdump.pl will connect to the IMAP server, # # extract each message from the user's account, and write it to # # a file. The result looks something like this: # # # # /var/backups/INBOX # # 1 2 3 4 5 # # /var/backups/Drafts # # 1 2 # # /var/backups/Notes/2002 # # 1 2 3 4 5 6 7 # # /var/backups/Notes/2003 # # 1 2 3 # # etc etc # # # # imapdump.pl is called like this: # # ./imapdump.pl -S host/user/password -f /var/backup # # # # Optional arguments: # # -d debug # # -I show IMAP protocol exchanges # # -L logfile # # -m mailbox list (dumps only the specified mailboxes, see # # the usage notes for syntax) # ####################################################################### use Socket; use IO::Socket; use FileHandle; use Fcntl; use Getopt::Std; ################################################################# # Main program. # ################################################################# init(); # Get list of all messages on the source host by Message-Id # connectToHost($sourceHost, \$conn); unless ( login($sourceUser,$sourcePwd, $conn) ) { Log("Check your username and password"); print STDOUT "Login failed: Check your username and password\n"; exit; } @mbxs = getMailboxList($sourceUser, $conn); foreach $mbx ( @mbxs ) { Log("Dumping messages in $mbx mailbox") if $dump_flags; undef @sourceMsgs; getMsgList( $mbx, \@sourceMsgs, $conn ); my $i = $#sourceMsgs + 1; Log("$mbx has $i messages"); foreach $msgnum ( @sourceMsgs ) { $message = fetchMsg( $msgnum, $mbx, $conn ); `mkdir -p "$dir/$mbx"` if !-d "$dir/$mbx"; if ( !open (M, ">$dir/$mbx/$msgnum") ) { Log("Error opening $dir/$mbx/$msgnum: $!"); next; } Log(" Copying message $msgnum") if $debug; print M $message; close M; $added++; } } logout( $conn ); Log("$added total messages dumped"); exit; sub init { $version = 'V1.0'; $os = $ENV{'OS'}; processArgs(); if ($timeout eq '') { $timeout = 60; } # Open the logFile # if ( $logfile ) { if ( !open(LOG, ">> $logfile")) { print STDOUT "Can't open $logfile: $!\n"; } select(LOG); $| = 1; } Log("\n$0 starting"); # Determine whether we have SSL support via openSSL and IO::Socket::SSL $ssl_installed = 1; eval 'use IO::Socket::SSL'; if ( $@ ) { $ssl_installed = 0; } if ( $dump_flags ) { Log("Dumping only those messages with one of the following flags: $dump_flags"); } } # # sendCommand # # This subroutine formats and sends an IMAP protocol command to an # IMAP server on a specified connection. # sub sendCommand { local($fd) = shift @_; local($cmd) = shift @_; print $fd "$cmd\r\n"; if ($showIMAP) { Log (">> $cmd",2); } } # # readResponse # # This subroutine reads and formats an IMAP protocol response from an # IMAP server on a specified connection. # sub readResponse { local($fd) = shift @_; $response = <$fd>; chop $response; $response =~ s/\r//g; push (@response,$response); if ($showIMAP) { Log ("<< $response",2); } } # # Log # # This subroutine formats and writes a log message to STDERR. # sub Log { my $str = shift; # If a logile has been specified then write the output to it # Otherwise write it to STDOUT if ( $logfile ) { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; if ($year < 99) { $yr = 2000; } else { $yr = 1900; } $line = sprintf ("%.2d-%.2d-%d.%.2d:%.2d:%.2d %s %s\n", $mon + 1, $mday, $year + $yr, $hour, $min, $sec,$$,$str); print LOG "$line"; } else { print STDOUT "$str\n"; } } # Make a connection to an IMAP host sub connectToHost { my $host = shift; my $conn = shift; Log("Connecting to $host") if $debug; ($host,$port) = split(/:/, $host); $port = 143 unless $port; # We know whether to use SSL for ports 143 and 993. For any # other ones we'll have to figure it out. $mode = sslmode( $host, $port ); if ( $mode eq 'SSL' ) { unless( $ssl_installed == 1 ) { warn("You must have openSSL and IO::Socket::SSL installed to use an SSL connection"); Log("You must have openSSL and IO::Socket::SSL installed to use an SSL connection"); exit; } Log("Attempting an SSL connection") if $debug; $$conn = IO::Socket::SSL->new( Proto => "tcp", SSL_verify_mode => 0x00, PeerAddr => $host, PeerPort => $port, ); unless ( $$conn ) { $error = IO::Socket::SSL::errstr(); Log("Error connecting to $host: $error"); exit; } } else { # Non-SSL connection Log("Attempting a non-SSL connection") if $debug; $$conn = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port, ); unless ( $$conn ) { Log("Error connecting to $host:$port: $@"); warn "Error connecting to $host:$port: $@"; exit; } } Log("Connected to $host on port $port"); } sub sslmode { my $host = shift; my $port = shift; my $mode; # Determine whether to make an SSL connection # to the host. Return 'SSL' if so. if ( $port == 143 ) { # Standard non-SSL port return ''; } elsif ( $port == 993 ) { # Standard SSL port return 'SSL'; } unless ( $ssl_installed ) { # We don't have SSL installed on this machine return ''; } # For any other port we need to determine whether it supports SSL my $conn = IO::Socket::SSL->new( Proto => "tcp", SSL_verify_mode => 0x00, PeerAddr => $host, PeerPort => $port, ); if ( $conn ) { close( $conn ); $mode = 'SSL'; } else { $mode = ''; } return $mode; } # trim # # remove leading and trailing spaces from a string sub trim { local (*string) = @_; $string =~ s/^\s+//; $string =~ s/\s+$//; return; } # login # # login in at the source host with the user's name and password # sub login { my $user = shift; my $pwd = shift; my $conn = shift; sendCommand ($conn, "1 LOGIN $user $pwd"); while (1) { readResponse ( $conn ); if ($response =~ /^1 OK/i) { last; } elsif ($response =~ /NO/) { Log ("unexpected LOGIN response: $response"); return 0; } } Log("Logged in as $user") if $debug; return 1; } # logout # # log out from the host # sub logout { my $conn = shift; ++$lsn; undef @response; sendCommand ($conn, "$lsn LOGOUT"); while ( 1 ) { readResponse ($conn); if ( $response =~ /^$lsn OK/i ) { last; } elsif ( $response !~ /^\*/ ) { Log ("unexpected LOGOUT response: $response"); last; } } close $conn; return; } # getMailboxList # # get a list of the user's mailboxes from the source host # sub getMailboxList { my $user = shift; my $conn = shift; my @mbxs; my @mailboxes; # Get a list of the user's mailboxes # if ( $mbxList ) { # The user has supplied a list of mailboxes so only processes # the ones in that list @mbxs = split(/,/, $mbxList); foreach $mbx ( @mbxs ) { trim( *mbx ); push( @mailboxes, $mbx ); } return @mailboxes; } if ($debug) { Log("Get list of user's mailboxes",2); } sendCommand ($conn, "1 LIST \"\" *"); undef @response; while ( 1 ) { readResponse ($conn); if ( $response =~ /^1 OK/i ) { last; } elsif ( $response !~ /^\*/ ) { Log ("unexpected response: $response"); return 0; } } undef @mbxs; for $i (0 .. $#response) { $response[$i] =~ s/\s+/ /; if ( $response[$i] =~ /"$/ ) { $response[$i] =~ /\* LIST \((.*)\) "(.+)" "(.+)"/i; $mbx = $3; } elsif ( $response[$i] =~ /\* LIST \((.*)\) NIL (.+)/i ) { $mbx= $2; } else { $response[$i] =~ /\* LIST \((.*)\) "(.+)" (.+)/i; $mbx = $3; } $mbx =~ s/^\s+//; $mbx =~ s/\s+$//; if ($response[$i] =~ /NOSELECT/i) { if ($debug) { Log("$mbx is set NOSELECT,skip it",2); } next; } if (($mbx =~ /^\#/) && ($user ne 'anonymous')) { # Skip public mbxs unless we are migrating them next; } if ($mbx =~ /^\./) { # Skip mailboxes starting with a dot next; } push ( @mbxs, $mbx ) if $mbx ne ''; } if ( $mbxList ) { # The user has supplied a list of mailboxes so only processes # those @mbxs = split(/,/, $mbxList); } return @mbxs; } # getMsgList # # Get a list of the user's messages in the indicated mailbox on # the source host # sub getMsgList { my $mailbox = shift; my $msgs = shift; my $conn = shift; my $seen; my $msgnum; trim( *mailbox ); sendCommand ($conn, "1 EXAMINE \"$mailbox\""); undef @response; while ( 1 ) { readResponse ( $conn ); if ( $response =~ /^1 0 EXISTS/i ) { return 0 } if ( $response =~ /^1 N0 /i ) { return 0 } if ( $response =~ /^1 OK/i ) { # print STDERR "response $response\n"; last; } elsif ( $response !~ /^\*/ ) { Log ("unexpected response: $response"); # print STDERR "Error: $response\n"; return 0; } } sendCommand ( $conn, "1 FETCH 1:* (uid flags internaldate body[header.fields (Message-Id)])"); undef @response; while ( 1 ) { readResponse ( $conn ); if ( $response =~ /^1 OK/i ) { # print STDERR "response $response\n"; last; } elsif ($response =~ /Bogus sequence in FETCH/i) { Log ("Error: Bogus sequence in FETCH",2); return 0; } elsif ( $response =~ /^1 NO|^1 BAD/ ) { return 0; } elsif ( $XDXDXD ) { Log ("unexpected response: $response"); Log ("Unable to get list of messages in this mailbox"); push(@errors,"Error getting list of $user's msgs"); return 0; } } # Get a list of the msgs in the mailbox # undef @msgs; undef $flags; $flag_ok = 1; for $i (0 .. $#response) { $seen=0; $_ = $response[$i]; last if /OK FETCH complete/; if ( $response[$i] =~ /\* (.+) FETCH/ ) { ($msgnum) = split(/\s+/, $1); } if ($response[$i] =~ /FLAGS/) { # Get the list of flags $response[$i] =~ /FLAGS \(([^\)]*)/; $flags =~ s/\\Recent//; $flags = $1; $flag_ok = flags_ok( $flags ); } if ( $response[$i] =~ /INTERNALDATE ([^\)]*)/ ) { $response[$i] =~ /INTERNALDATE ([^BODY]*)/i; $date = $1; $date =~ s/"//g; } if ( $response[$i] =~ /^Message-I[dD]:/i ) { ($label,$msgid) = split(/: /, $response[$i]); ### push (@$msgs,"$msgid||||||$mailbox||||||$msgnum||||||$flags||||||$date"); if ( $dump_flags ) { Log("msgnum $msgnum flag_ok $flag_ok"); push (@$msgs,$msgnum) if $flag_ok; } else { push (@$msgs,$msgnum); } } } } sub fetchMsg { my $msgnum = shift; my $mbx = shift; my $conn = shift; my $message; Log(" Fetching msg $msgnum...") if $debug; sendCommand ($conn, "1 EXAMINE \"$mbx\""); while (1) { readResponse ($conn); last if ( $response =~ /^1 OK/i ); } sendCommand( $conn, "1 FETCH $msgnum (rfc822)"); while (1) { readResponse ($conn); if ( $response =~ /^1 OK/i ) { $size = length($message); last; } elsif ($response =~ /message number out of range/i) { Log ("Error fetching uid $uid: out of range",2); $stat=0; last; } elsif ( $response =~ /^1 NO|^1 BAD/ ) { Log("$response"); return 0; } elsif ($response =~ /Bogus sequence in FETCH/i) { Log ("Error fetching uid $uid: Bogus sequence in FETCH",2); $stat=0; last; } elsif ( $response =~ /message could not be processed/i ) { Log("Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)"); push(@errors,"Message could not be processed, skipping it ($user,msgnum $msgnum,$destMbx)"); $stat=0; last; } elsif ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{[0-9]+\}/i) { ($len) = ($response =~ /^\*\s+$msgnum\s+FETCH\s+\(.*RFC822\s+\{([0-9]+)\}/i); $cc = 0; $message = ""; while ( $cc < $len ) { $n = 0; $n = read ($conn, $segment, $len - $cc); if ( $n == 0 ) { Log ("unable to read $len bytes"); return 0; } $message .= $segment; $cc += $n; } } } return $message; } sub usage { print STDOUT "usage:\n"; print STDOUT " imapdump.pl -S Host/User/Password -f