postfix, integrating memcache as a lookup table using tcp_table
I have not had time to do the test “postfix memcached patch” because there are no idle servers that can be used for the experiment. instead, I’ve made a tutorial how to integrate memcached as a “postfix lookup table” with the help of tcp_table and a simple perl script.
Indeed, tcp_table “table lookup protocol” is one of the most powerful tools as well as the regexp and pcre, in my opinion. although client-server connection is not protected and and the server is not authenticated.
yes, I did a lot of experiments using tcp_table and perl scripts. it made me realize that I can do almost everything I need and make postfix as my favorite MTA.
Things required:
- perl Cache::Memcached module
- memcached
- postfix with tcp_table support
OK, first we create a simple perl script that allows you to handle the protocols of tcp_table. let’s call it memc.pl
1 | #!/usr/bin/perl |
2 | use strict; |
3 | use warnings; |
4 | use Sys::Syslog qw(:DEFAULT setlogsock); |
5 | use Cache::Memcached; |
6 |
7 | # Configure the memcached server |
8 | my $memd = new Cache::Memcached { |
9 | 'servers' => [ '127.0.0.1:11211' ], |
10 | }; |
11 |
12 | # |
13 | # Initalize and open syslog. |
14 | # |
15 | openlog('postfix/memcached','pid','mail'); |
16 |
17 | sub qrymemc { |
18 | return unless /^get\s+(.+)/i; |
19 | my $kmemc = lc ( $1 ); |
20 | chomp ( $kmemc ); |
21 | trim( $kmemc ); |
22 | my $vmemc = $memd ->get( $kmemc ); |
23 | if ( defined $vmemc ) { |
24 | return ( $kmemc , $vmemc ); |
25 | } |
26 | return ; |
27 | } |
28 |
29 | sub trim{ |
30 | $_ [0]=~s/^\s+//; |
31 | $_ [0]=~s/\s+$//; |
32 | return ; |
33 | } |
34 |
35 | # |
36 | # Autoflush standard output. |
37 | # |
38 | select STDOUT; $|++; |
39 |
40 | while (<>) { |
41 | chomp ; |
42 | if (/^get\s+(.+)/i) { |
43 | my $data = lc ( $1 ); |
44 | my @res = qrymemc( $data ); |
45 | syslog( "info" , "data: %s" , $data ); |
46 | if ( @res ) { |
47 | chomp ( @res ); |
48 | print "200 $res[1]\n" ; |
49 | syslog( "info" , "Found: key = %s, value = %s" , $res [0], $res [1]); |
50 | next ; |
51 | } |
52 | } |
53 | print "200 DUNNO\n" ; |
54 | } |