"Fossies" - the Fresh Open Source Software Archive

Member "openmailadmin-1.0.1/samples/oma_mail.daimon.pl" (31 Jul 2006, 3140 Bytes) of package /linux/privat/old/openmailadmin-1.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/env perl -w
    2 use strict;
    3 
    4 use DBI;
    5 
    6 ################ modify these ##################################################
    7 my %MTA = ( 'virtual'   => '/etc/postfix/db/virtual',
    8     'regexp'    => '/etc/postfix/db/virtual.regex',
    9     'domains'   => '/etc/postfix/db/domains' );
   10 my $passwd_cache    = '/var/lib/pam_mysql.cache';
   11 my $postprocess     = '/usr/sbin/postmap ';
   12 
   13 my %DB = (  'TYPE'  => 'mysql',
   14     'HOST'  => 'localhost',
   15     'USER'  => 'your-MySQL-User',
   16     'PASS'  => '##MysqlSecret-SELECT-only##',
   17     'DB'    => 'yourMySQL-DB' );
   18 
   19 ################ no need for modifying anything below ##########################
   20 my ($dbh, $sth);
   21 
   22 $dbh = DBI->connect('DBI:'.$DB{'TYPE'}.':'.$DB{'DB'}.':'.$DB{'HOST'}, $DB{'USER'}, $DB{'PASS'})
   23     || die 'Cannot connect to database.';
   24 
   25 if(! -e $MTA{'virtual'} || rand(6)<1 || amountOfNewEntries('virtual') > 0) {
   26     writeFile('virtual', 'virtual', 'address', 'dest', 'address DESC');
   27 }
   28 if(! -e $MTA{'regexp'} || rand(6)<1 || amountOfNewEntries('virtual_regexp') > 0) {
   29     writeFile('regexp', 'virtual_regexp', 'reg_exp', 'dest', 'LENGTH(reg_exp) DESC');
   30 }
   31 if(! -e $MTA{'domains'} || rand(48)<1 || amountOfNewEntries('domains') > 0) {
   32     writeDomains();
   33 }
   34 if(defined $passwd_cache && (!(-e $passwd_cache) || rand(24)<1)) {
   35     writePasswdCache();
   36 }
   37 
   38 if(rand(25) < 1 && $DB{'TYPE'} eq 'mysql') {
   39     $dbh->do(q{OPTIMIZE TABLE virtual, virtual_regexp, user, domains});
   40 }
   41 
   42 $dbh->disconnect;
   43 
   44 ################################################################################
   45 sub writePasswdCache {
   46 ################################################################################
   47     my @row;
   48 
   49     $sth = $dbh->prepare(q{
   50         SELECT mbox, password
   51         FROM user
   52         });
   53     $sth->execute();
   54 
   55     open(OUT, '>', $passwd_cache);
   56     while(@row = $sth->fetchrow_array()) {
   57         print OUT $row[0].':'.$row[1]."\n";
   58     }
   59     close OUT;
   60 }
   61 
   62 ################################################################################
   63 sub writeDomains {
   64 ################################################################################
   65     my @row;
   66 
   67     $sth = $dbh->prepare(q{
   68         SELECT domain
   69         FROM domains
   70         });
   71     $sth->execute();
   72 
   73     open(OUT, '>', $MTA{'domains'});
   74     while(@row = $sth->fetchrow_array()) {
   75         print OUT $row[0]."\t\t".$row[0]."\n";
   76     }
   77     close OUT;
   78     system($postprocess.$MTA{'domains'});
   79 }
   80 
   81 ################################################################################
   82 sub writeFile {
   83 ################################################################################
   84     my @row;
   85 
   86     $sth = $dbh->prepare(qq{
   87         SELECT $_[2], $_[3]
   88         FROM $_[1]
   89         WHERE active=1 ORDER BY $_[4]
   90         });
   91     $sth->execute();
   92 
   93     open(OUT, '>', $MTA{$_[0]});
   94     while(@row = $sth->fetchrow_array()) {
   95         print OUT $row[0]."\t\t".$row[1]."\n";
   96     }
   97     close OUT;
   98 
   99     $dbh->do(qq{UPDATE $_[1] SET neu=0 WHERE neu=1});
  100     system($postprocess.$MTA{$_[0]});
  101 }
  102 
  103 ################################################################################
  104 sub amountOfNewEntries {
  105 ################################################################################
  106     my $row;
  107 
  108     $sth = $dbh->prepare(qq{
  109         SELECT COUNT(*) AS neue
  110         FROM $_[0]
  111         WHERE neu=1
  112         });
  113     $sth->execute();
  114 
  115     $row = $sth->fetchrow_hashref();
  116     $sth->finish();
  117     return $row->{'neue'};
  118 }