]> Frank Brehm's Git Trees - my-stuff/postfix.git/commitdiff
Lookup-Tabellen als Dateien geschrieben
authorFrank Brehm <frank@brehm-online.com>
Tue, 4 Nov 2008 22:16:01 +0000 (22:16 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 4 Nov 2008 22:16:01 +0000 (22:16 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/postfix@24 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

get-lookup-tables.pl

index eede7864b6b7d47a9eb828ebcd6c3c9bf487381a..a9d7ee7ab11d68f996c758b129bf1e700ded2f0c 100755 (executable)
@@ -30,12 +30,20 @@ my $dbh;
 my $postalias = '/usr/sbin/postalias';
 my $postmap   = '/usr/sbin/postmap';
 
+my $do_postfix_reload = 0;
+
 my $binary = {
     'postalias' => '/usr/sbin/postalias',
+    'postconf'  => '/usr/sbin/postconf',
     'postfix'   => '/usr/sbin/postfix',
     'postmap'   => '/usr/sbin/postmap',
 };
 
+my $workdir = catfile( '', 'etc', 'postfix', 'work' );
+my $lookup_dir = $postfix_dir;
+
+my $default_database_type = 'hash';
+
 END {
     close_db();
     if ( -f $pidfile and $pidfile_written ) {
@@ -61,6 +69,125 @@ check_pidfile();
 
 open_db();
 
+for my $l ( sort { lc($a) cmp lc($b) } keys %{$config->{'lookup'}} ) {
+    perform_lookup($l);
+}
+
+exit 0;
+
+#--------------------------------------------------------------------------------------
+
+=head2 perform_lookup( $lookup_name )
+
+=cut
+
+sub perform_lookup {
+
+    my $lookup = shift;
+
+    debug( 1, "Verarbeite Lookup-Tabelle '" . $lookup . "' ..." );
+
+    my $l = $config->{'lookup'}{$lookup};
+    my $sql = $l->{'query'};
+    my $type = $l->{'type'} || $default_database_type;
+    my $map_type = 'map';
+    $map_type = 'aliases' if $l->{'map_type'} =~ /^\s*alias(?:es)?\s*$/;
+    unless ( $sql ) {
+        notice( "Kein SQL-Statement für Lookup-Tabelle '" . $lookup . "' festgelegt." );
+        return;
+    }
+
+    my $result = do_sql_query( $sql );
+    unless ( $result ) {
+        notice( "Ungültige Resultate erhalten." );
+        return;
+    }
+
+    my $map = {};
+    for my $m ( @$result ) {
+        next unless ref($m) and ref($m) eq 'ARRAY';
+        my $key = shift @$m;
+        my $value = shift @$m;
+        next unless defined $value;
+        $map->{$key} = [] unless $map->{$key};
+        push @{ $map->{$key} }, $value;
+    }
+    debug( 2, "Kummulierte Ergebnisse für Lookup-Tabelle '" . $lookup . "': ", $map );
+
+    my $max_length = 1;
+    for ( keys %$map ) {
+        $max_length = length($_) if length($_) > $max_length;
+    }
+    $max_length += 1 if $map_type eq 'aliases';
+
+    my $new_file = catfile( $workdir, $lookup . ".new" );
+    debug( 1, "Schreibe Ergebnisse in Datei '" . $new_file . "' ..." );
+
+    unless ( open FILE, ">", $new_file ) {
+        fatal( "Konnte Datei '" . $new_file . "' nicht zum Screiben öffnen: " . $! );
+    }
+
+    if ( $l->{'prefix'} ) {
+        print FILE $l->{'prefix'};
+        print FILE "\n";
+    }
+    print FILE "\n";
+
+    for my $key ( sort { lc($a) cmp lc($b) } keys %$map ) {
+        next unless scalar( @{ $map->{$key} } );
+        my $k = $key;
+        if ( $map_type eq 'aliases' ) {
+            $k .= ":";
+        }
+        my $out = sprintf( "%-*s %s\n", $max_length, $k, join( ", ", @{ $map->{$key} } ) );
+        print FILE $out;
+    }
+
+    print FILE "\n# Generated at: " . curtime() . "\n\n";
+
+    close FILE;
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 curtime( )
+
+=cut
+
+sub curtime {
+
+    my $out = '';
+
+    my @LT = localtime();
+    $out = sprintf( '%4d-%02d-%02d %02d:%02d:%02d%s', $LT[5] + 1900, $LT[4] + 1, $LT[3], $LT[2] ,$LT[1], $LT[0], ( $ENV{'TZ'} ? ' ' . $ENV{'TZ'} : '' ) );
+    return $out;
+
+}
+
+#--------------------------------------------------------------------------------------
+
+=head2 do_sql_query( $sql )
+
+=cut
+
+sub do_sql_query {
+
+    my $sql = shift;
+
+    debug( 3, "Führe SQL-Statment aus: :'" . $sql . "' ..." );
+    my $sth = $dbh->prepare($sql) || fatal( "Fehler beim prepare des Statements '" . $sql . "': " . $dbh->errstr() );
+    unless ( $sth->execute() ) {
+        fatal( "Fehler beim Ausführen des Statements '" . $sql . "': " . $sth->errstr() );
+    }
+
+    my $out = $sth->fetchall_arrayref();
+    $sth->finish();
+    debug( 4, "Ergebnisse von '" . $sql . "': ", $out );
+    return $out;
+
+}
+
 #--------------------------------------------------------------------------------------
 
 =head2 open_db( )
@@ -116,6 +243,14 @@ sub close_db {
 
 sub check_binaries {
 
+    $workdir = $config->{'workdir'} if $config->{'workdir'};
+    debug( 3, "Checke Arbeitsverzeichnis '" . $workdir . "' ..." );
+    fatal( "Arbeitsverzeichnis '" . $workdir . "' existiert nicht." ) unless -d $workdir;
+
+    $lookup_dir = $config->{'lookup_dir'} if $config->{'lookup_dir'};
+    debug( 3, "Checke Verzeichnis für Lookup-Tabellen '" . $lookup_dir . "' ..." );
+    fatal( "Verzeichnis fuer Lookup-Tabellen '" . $lookup_dir . "' existiert nicht." ) unless -d $lookup_dir;
+
     for my $bin ( keys %$binary ) {
         if ( $config->{'binary'}{$bin} ) {
             $binary->{$bin} = $config->{'binary'}{$bin};
@@ -126,6 +261,17 @@ sub check_binaries {
         }
     }
 
+    debug( 2, "Ermittle default_database_type ..." );
+    my $postconf = $binary->{'postconf'};
+    my $out = `$postconf default_database_type`;
+    if ( $out ) {
+        debug( 3, "Ausgabe von '$postconf default_database_type': ", $out );
+        if ( $out =~ /^default_database_type\s*=\s*(\S+)/ ) {
+            $default_database_type = $1;
+            debug( 2, "Ermittelter default_database_type: '" . $default_database_type . "'" );
+        }
+    }
+
 }
 
 #--------------------------------------------------------------------------------------
@@ -191,6 +337,7 @@ sub read_config {
         my $conf = new Config::General(
             '-ConfigFile'       => $cfg_file,
             '-ApacheCompatible' => 1,
+            '-LowerCaseNames'   => 1,
         );
         %$config = $conf->getall();
     };