You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fetchmail.pl 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/perl
  2. use DBI;
  3. use MIME::Base64;
  4. # use Data::Dumper;
  5. use File::Temp qw/ mkstemp /;
  6. use Sys::Syslog;
  7. # require liblockfile-simple-perl
  8. use LockFile::Simple qw(lock trylock unlock);
  9. ######################################################################
  10. ########## Change the following variables to fit your needs ##########
  11. # database settings
  12. # database backend - uncomment one of these
  13. our $db_type = 'Pg';
  14. #my $db_type = 'mysql';
  15. # host name
  16. our $db_host="127.0.0.1";
  17. # database name
  18. our $db_name="postfix";
  19. # database username
  20. our $db_username="mail";
  21. # database password
  22. our $db_password="CHANGE_ME!";
  23. # instead of changing this script, you can put your settings to /etc/mail/postfixadmin/fetchmail.conf
  24. # just use perl syntax there to fill the variables listed above (without the "our" keyword). Example:
  25. # $db_username = 'mail';
  26. if (-f "/etc/mail/postfixadmin/fetchmail.conf") {
  27. require "/etc/mail/postfixadmin/fetchmail.conf";
  28. }
  29. #################### Don't change anything below! ####################
  30. ######################################################################
  31. openlog("fetchmail-all", "pid", "mail");
  32. sub log_and_die {
  33. my($message) = @_;
  34. syslog("err", $message);
  35. die $message;
  36. }
  37. # read options and arguments
  38. $configfile = "/etc/fetchmail-all/config";
  39. @ARGS1 = @ARGV;
  40. while ($_ = shift @ARGS1) {
  41. if (/^-/) {
  42. if (/^--config$/) {
  43. $configfile = shift @ARGS1
  44. }
  45. }
  46. }
  47. $run_dir="/var/run/fetchmail";
  48. # use specified config file
  49. if (-e $configfile) {
  50. do $configfile;
  51. }
  52. if($db_type eq "Pg" || $db_type eq "mysql") {
  53. $dsn = "DBI:$db_type:database=$db_name;host=$db_host";
  54. } else {
  55. log_and_die "unsupported db_type $db_type";
  56. }
  57. $lock_file=$run_dir . "/fetchmail-all.lock";
  58. $lockmgr = LockFile::Simple->make(-autoclean => 1, -max => 1);
  59. $lockmgr->lock($lock_file) || log_and_die "can't lock ${lock_file}";
  60. # database connect
  61. $dbh = DBI->connect($dsn, $db_username, $db_password) || log_and_die "cannot connect the database";
  62. if($db_type eq "Pg") {
  63. $sql_cond = "active = 't' AND date_part('epoch',now())-date_part('epoch',date)";
  64. } elsif($db_type eq "mysql") {
  65. $sql_cond = "active = 1 AND unix_timestamp(now())-unix_timestamp(date)";
  66. }
  67. $sql = "
  68. SELECT id,mailbox,src_server,src_auth,src_user,src_password,src_folder,fetchall,keep,protocol,mda,extra_options,usessl, sslcertck, sslcertpath, sslfingerprint
  69. FROM fetchmail
  70. WHERE $sql_cond > poll_time*60
  71. ";
  72. my (%config);
  73. map{
  74. my ($id,$mailbox,$src_server,$src_auth,$src_user,$src_password,$src_folder,$fetchall,$keep,$protocol,$mda,$extra_options,$usessl,$sslcertck,$sslcertpath,$sslfingerprint)=@$_;
  75. syslog("info","fetch ${src_user}@${src_server} for ${mailbox}");
  76. $cmd="user '${src_user}' there with password '".decode_base64($src_password)."'";
  77. $cmd.=" folder '${src_folder}'" if ($src_folder);
  78. $cmd.=" mda ".$mda if ($mda);
  79. # $cmd.=" mda \"/usr/local/libexec/dovecot/deliver -m ${mailbox}\"";
  80. $cmd.=" is '${mailbox}' here";
  81. $cmd.=" keep" if ($keep);
  82. $cmd.=" fetchall" if ($fetchall);
  83. $cmd.=" ssl" if ($usessl);
  84. $cmd.=" sslcertck" if($sslcertck);
  85. $cmd.=" sslcertpath $sslcertpath" if ($sslcertck && $sslcertpath);
  86. $cmd.=" sslfingerprint \"$sslfingerprint\"" if ($sslfingerprint);
  87. $cmd.=" ".$extra_options if ($extra_options);
  88. $text=<<TXT;
  89. set postmaster "postmaster"
  90. set nobouncemail
  91. set no spambounce
  92. set properties ""
  93. set syslog
  94. poll ${src_server} with proto ${protocol}
  95. $cmd
  96. TXT
  97. ($file_handler, $filename) = mkstemp( "/tmp/fetchmail-all-XXXXX" ) or log_and_die "cannot open/create fetchmail temp file";
  98. print $file_handler $text;
  99. close $file_handler;
  100. $ret=`/usr/bin/fetchmail -f $filename -i $run_dir/fetchmail.pid`;
  101. unlink $filename;
  102. $sql="UPDATE fetchmail SET returned_text=".$dbh->quote($ret).", date=now() WHERE id=".$id;
  103. $dbh->do($sql);
  104. }@{$dbh->selectall_arrayref($sql)};
  105. $lockmgr->unlock($lock_file);
  106. closelog();