diff --git a/rootfs/usr/local/bin/fetchmail.pl b/rootfs/usr/local/bin/fetchmail.pl index a1a3c8aa..513fb758 100644 --- a/rootfs/usr/local/bin/fetchmail.pl +++ b/rootfs/usr/local/bin/fetchmail.pl @@ -16,16 +16,26 @@ # database backend - uncomment one of these our $db_type = 'Pg'; #my $db_type = 'mysql'; +#my $db_type = 'SQLite'; -# host name +# host name (ignored for SQLite) our $db_host="127.0.0.1"; -# database name +# database name (for SQLite: full path to the database file) our $db_name="postfix"; # database username our $db_username="mail"; # database password our $db_password="CHANGE_ME!"; +# Where to create a lockfile; please ensure path exists. +our $run_dir="/var/lock/fetchmail"; + +# in case you want to use dovecot deliver to put the mail directly into the users mailbox, +# set "mda" in the fetchmail table to the keyword "dovecot". + +# Where the delivery binary is located +$dovecot_deliver = "/usr/lib/dovecot/deliver"; + # instead of changing this script, you can put your settings to /etc/mail/postfixadmin/fetchmail.conf # just use perl syntax there to fill the variables listed above (without the "our" keyword). Example: # $db_username = 'mail'; @@ -40,11 +50,23 @@ openlog("fetchmail-all", "pid", "mail"); sub log_and_die { - my($message) = @_; + my($message) = @_; syslog("err", $message); die $message; } +sub escape_password { + $output = ""; + for $i (0..length($_[0])-1){ + $char = substr($_[0], $i, 1); + if ($char eq "\\" or $char eq "\"") + { + $output = $output . "\\"; + } + $output = $output . $char; + } + return $output; +} # read options and arguments $configfile = "/etc/fetchmail-all/config"; @@ -59,8 +81,6 @@ sub log_and_die { } } -$run_dir="/var/run/fetchmail"; - # use specified config file if (-e $configfile) { do $configfile; @@ -68,10 +88,16 @@ sub log_and_die { if($db_type eq "Pg" || $db_type eq "mysql") { $dsn = "DBI:$db_type:database=$db_name;host=$db_host;port=$db_port"; +} elsif($db_type eq "SQLite") { + $dsn = "DBI:SQLite:dbname=$db_name"; } else { log_and_die "unsupported db_type $db_type"; } +if(!-d $run_dir) { + log_and_die("Please create: $run_dir"); +} + $lock_file=$run_dir . "/fetchmail-all.lock"; $lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1); @@ -84,23 +110,35 @@ sub log_and_die { $sql_cond = "active = 't' AND date_part('epoch',now())-date_part('epoch',date)"; } elsif($db_type eq "mysql") { $sql_cond = "active = 1 AND unix_timestamp(now())-unix_timestamp(date)"; +} elsif($db_type eq "SQLite") { + $sql_cond = "active = 1 AND strftime('%s','now')-strftime('%s',date)"; } +# SQLite has no now() +$sql_now = ($db_type eq "SQLite") ? "datetime('now')" : "now()"; + $sql = " - SELECT id,mailbox,src_server,src_auth,src_user,src_password,src_folder,fetchall,keep,protocol,mda,extra_options,usessl, sslcertck, sslcertpath, sslfingerprint + SELECT id,mailbox,src_server,src_auth,src_user,src_password,src_folder,fetchall,keep,protocol,mda,extra_options,usessl, sslcertck, sslcertpath, sslfingerprint, src_port FROM fetchmail WHERE $sql_cond > poll_time*60 "; my (%config); map{ - my ($id,$mailbox,$src_server,$src_auth,$src_user,$src_password,$src_folder,$fetchall,$keep,$protocol,$mda,$extra_options,$usessl,$sslcertck,$sslcertpath,$sslfingerprint)=@$_; + my ($id,$mailbox,$src_server,$src_auth,$src_user,$src_password,$src_folder,$fetchall,$keep,$protocol,$mda,$extra_options,$usessl,$sslcertck,$sslcertpath,$sslfingerprint,$src_port)=@$_; syslog("info","fetch ${src_user}@${src_server} for ${mailbox}"); - $cmd="user '${src_user}' there with password '".decode_base64($src_password)."'"; + $cmd="user '${src_user}' there with password '".escape_password(decode_base64($src_password))."'"; $cmd.=" folder '${src_folder}'" if ($src_folder); - $cmd.=" mda ".$mda if ($mda); + + if ($mda) { + if ($mda eq "dovecot") { + $cmd.=" mda \"${dovecot_deliver} -d ${mailbox}\" "; + } else { + $cmd.=" mda ".$mda + } + } # $cmd.=" mda \"/usr/local/libexec/dovecot/deliver -m ${mailbox}\""; $cmd.=" is '${mailbox}' here"; @@ -120,20 +158,21 @@ sub log_and_die { set properties "" set syslog -poll ${src_server} with proto ${protocol} - $cmd - TXT + $text.="poll ${src_server} with proto ${protocol}"; + $text.=" service ${src_port}" if ($src_port); + $text.="\n $cmd"; + ($file_handler, $filename) = mkstemp( "/tmp/fetchmail-all-XXXXX" ) or log_and_die "cannot open/create fetchmail temp file"; print $file_handler $text; close $file_handler; - $ret=`/usr/bin/fetchmail -f $filename -i $run_dir/fetchmail.pid`; + $ret=`/usr/bin/fetchmail -f $filename --pidfile $run_dir/fetchmail.pid`; unlink $filename; - $sql="UPDATE fetchmail SET returned_text=".$dbh->quote($ret).", date=now() WHERE id=".$id; + $sql="UPDATE fetchmail SET returned_text=".$dbh->quote($ret).", date=$sql_now WHERE id=".$id; $dbh->do($sql); }@{$dbh->selectall_arrayref($sql)}; diff --git a/rootfs/usr/local/bin/setup.sh b/rootfs/usr/local/bin/setup.sh index 589bf409..89cc733d 100644 --- a/rootfs/usr/local/bin/setup.sh +++ b/rootfs/usr/local/bin/setup.sh @@ -838,7 +838,7 @@ sed -i 's|invoke-rc.d clamav-daemon reload-log|s6-svc -h /services/clamd|g' /etc sed -i 's|invoke-rc.d clamav-freshclam reload-log|s6-svc -h /services/freshclam|g' /etc/logrotate.d/clamav-freshclam # Folders and permissions -mkdir -p /var/run/fetchmail +mkdir -p /var/lock/fetchmail chmod +x /usr/local/bin/* # Ensure that hashes are calculated because Postfix require directory diff --git a/test/config/mariadb/struct.sql b/test/config/mariadb/struct.sql index f84e75a2..0b73daac 100644 --- a/test/config/mariadb/struct.sql +++ b/test/config/mariadb/struct.sql @@ -202,6 +202,7 @@ CREATE TABLE `fetchmail` ( `domain` varchar(255) DEFAULT '', `mailbox` varchar(255) NOT NULL, `src_server` varchar(255) NOT NULL, + `src_port` int(11) NOT NULL DEFAULT 0, `src_auth` enum('password','kerberos_v5','kerberos','kerberos_v4','gssapi','cram-md5','otp','ntlm','msn','ssh','any') DEFAULT NULL, `src_user` varchar(255) NOT NULL, `src_password` varchar(255) NOT NULL, @@ -231,7 +232,7 @@ CREATE TABLE `fetchmail` ( LOCK TABLES `fetchmail` WRITE; /*!40000 ALTER TABLE `fetchmail` DISABLE KEYS */; -INSERT INTO `fetchmail` VALUES (1,'domain.tld','sarah.connor@domain.tld','127.0.0.1','password','john.doe@domain.tld','dGVzdHBhc3N3ZDEy','',10,1,1,'IMAP',1,0,'','','','','','2016-12-05 11:59:01','2016-12-05 11:58:53','2016-12-05 11:58:53',1); +INSERT INTO `fetchmail` VALUES (1,'domain.tld','sarah.connor@domain.tld','127.0.0.1',0,'password','john.doe@domain.tld','dGVzdHBhc3N3ZDEy','',10,1,1,'IMAP',1,0,'','','','','','2016-12-05 11:59:01','2016-12-05 11:58:53','2016-12-05 11:58:53',1); /*!40000 ALTER TABLE `fetchmail` ENABLE KEYS */; UNLOCK TABLES; diff --git a/test/config/postgres/struct.sql b/test/config/postgres/struct.sql index 65bf571d..2048157d 100644 --- a/test/config/postgres/struct.sql +++ b/test/config/postgres/struct.sql @@ -280,6 +280,7 @@ CREATE TABLE public.fetchmail ( active boolean DEFAULT false NOT NULL, created timestamp with time zone DEFAULT '2000-01-01 00:00:00+00'::timestamp with time zone, modified timestamp with time zone DEFAULT now(), + src_port integer DEFAULT 0 NOT NULL, CONSTRAINT fetchmail_protocol_check CHECK (((protocol)::text = ANY ((ARRAY['POP3'::character varying, 'IMAP'::character varying, 'POP2'::character varying, 'ETRN'::character varying, 'AUTO'::character varying])::text[]))), CONSTRAINT fetchmail_src_auth_check CHECK (((src_auth)::text = ANY ((ARRAY['password'::character varying, 'kerberos_v5'::character varying, 'kerberos'::character varying, 'kerberos_v4'::character varying, 'gssapi'::character varying, 'cram-md5'::character varying, 'otp'::character varying, 'ntlm'::character varying, 'msn'::character varying, 'ssh'::character varying, 'any'::character varying])::text[]))) ); diff --git a/test/reverse.bats b/test/reverse.bats index cf27b601..a52b944e 100644 --- a/test/reverse.bats +++ b/test/reverse.bats @@ -562,6 +562,90 @@ load 'test_helper/bats-assert/load' assert_output 0 } +# +# fetchmail +# +# These fetch john.doe's INBOX, which moves his mail out of new/, so they come +# after every test that counts it there. + +# Replace the fetchmail table with one job that is due now, fetching the INBOX +# of $1 (password $2) from this server into sarah.connor@domain.tld, with +# mda $3, extra_options $4 and src_port $5. PostfixAdmin stores the password in +# base64. The suite's certificate does not verify, and fetchmail logs that to +# mail.err even with "no sslcertck", so the jobs use plain IMAP (sslproto ''). +fetchmail_job() { + docker exec -i postgres psql -U postfix -d postfix -q -v ON_ERROR_STOP=1 \ + -v user="$1" -v password="$(printf '%s' "$2" | base64)" -v mda="$3" -v extra="$4" -v port="$5" <<'SQL' +DELETE FROM fetchmail; +INSERT INTO fetchmail (mailbox, src_server, src_port, src_auth, src_user, src_password, src_folder, poll_time, fetchall, keep, protocol, mda, extra_options, usessl, active, date) +VALUES ('sarah.connor@domain.tld', 'localhost', :port, 'password', :'user', :'password', '', 10, true, true, 'IMAP', :'mda', :'extra', false, true, '2000-01-01'); +SQL +} + +# The command cron runs (/etc/cron.d/fetchmail, removed when TESTING=true) +fetchmail_cron_job() { + docker exec mailserver_reverse /usr/bin/perl /usr/local/bin/fetchmail.pl +} + +sarah_mail_count() { + docker exec mailserver_reverse /bin/sh -c "find /var/mail/vhosts/domain.tld/sarah.connor/subdir/new /var/mail/vhosts/domain.tld/sarah.connor/subdir/cur -type f 2>/dev/null | wc -l" +} + +# Count the lines of mail.log that match $1, waiting up to 30 seconds for at +# least $2 of them, since syslog writes them after the job returns +mail_log_count() { + for i in $(seq 30); do + n=$(docker exec mailserver_reverse grep -c -- "$1" /var/log/mail.log) + [ "$n" -ge "$2" ] && break + sleep 1 + done + echo "$n" +} + +# The documented setup (hardware/mailserver#276): port 25 refuses the one +# message whose sender is a local address that has not logged in, port 10025 +# takes it. src_port only has to be accepted by fetchmail, so it is 143. +@test "checking fetchmail: a job delivers all its mail through port 10025 (reverse configuration)" { + fetchmail_job john.doe@domain.tld testpasswd12 "" "smtp localhost/10025 sslproto ''" 143 + before=$(sarah_mail_count) + run fetchmail_cron_job + assert_success + for i in $(seq 60); do [ "$(sarah_mail_count)" -ge $((before + 4)) ] && break; sleep 1; done + run sarah_mail_count + assert_output $((before + 4)) + run docker exec postgres psql -U postfix -d postfix -Atc "SELECT date > now() - interval '1 hour' FROM fetchmail" + assert_output "t" +} + +@test "checking fetchmail: mda dovecot delivers through dovecot's LDA (reverse configuration)" { + fetchmail_job john.doe@domain.tld testpasswd12 dovecot "sslproto ''" 0 + before=$(sarah_mail_count) + run fetchmail_cron_job + assert_success + run sarah_mail_count + assert_output $((before + 4)) + run mail_log_count 'lda(sarah.connor@domain.tld)<.*saved mail to INBOX' 4 + assert_output 4 +} + +# The script escapes " and \ in the password it writes into fetchmail's +# configuration. A ' still breaks it. +@test "checking fetchmail: a source password with a double quote and a backslash in it works (reverse configuration)" { + password='pa"ss\word' + hash=$(docker exec mailserver_reverse doveadm pw -s SHA512-CRYPT -p "$password") + docker exec -i postgres psql -U postfix -d postfix -q -v ON_ERROR_STOP=1 -v hash="$hash" <<'SQL' +INSERT INTO mailbox (username, password, name, maildir, quota, domain, local_part) +VALUES ('fetch.test@domain.tld', :'hash', 'Fetch TEST', 'domain.tld/fetch.test/', 1024000, 'domain.tld', 'fetch.test'); +SQL + fetchmail_job fetch.test@domain.tld "$password" dovecot "sslproto ''" 0 + run fetchmail_cron_job + assert_success + run mail_log_count 'imap-login: Logged in: user=' 1 + assert_output 1 + run docker exec mailserver_reverse grep 'Authorization failure' /var/log/mail.log + assert_failure +} + # # unbound #