| [2748] | 1 | #!/usr/bin/perl | 
|---|
 | 2 |  | 
|---|
 | 3 | use Sendmail::Milter; | 
|---|
 | 4 |  | 
|---|
 | 5 | my %my_milter_callbacks = ( | 
|---|
 | 6 |                            'eom' =>        \&my_eom_callback, | 
|---|
 | 7 |                           ); | 
|---|
 | 8 |  | 
|---|
 | 9 | sub find_uid { | 
|---|
 | 10 |   my ($addr, $port) = @_; | 
|---|
 | 11 |   my $file; | 
|---|
 | 12 |   my $search; | 
|---|
 | 13 |   # TODO(quentin): These search strings are probably arch-specific. | 
|---|
 | 14 |   if ($addr eq "::1") { | 
|---|
 | 15 |     $file = "/proc/net/tcp6"; | 
|---|
 | 16 |     $search = sprintf("00000000000000000000000001000000:%04X", $port); | 
|---|
 | 17 |   } elsif ($addr eq "127.0.0.1") { | 
|---|
 | 18 |     $file = "/proc/net/tcp"; | 
|---|
 | 19 |     $search = sprintf("0100007F:%04X", $port); | 
|---|
 | 20 |   } else { | 
|---|
 | 21 |     return undef; | 
|---|
 | 22 |   } | 
|---|
 | 23 |   my $fh = IO::File->new($file, "r") or die "Cannot read $file: $!"; | 
|---|
 | 24 |   <$fh>;  # Eat header | 
|---|
 | 25 |   while (my $line = <$fh>) { | 
|---|
 | 26 |     my @parts = split(" ", $line); | 
|---|
 | 27 |     if ($parts[1] eq $search) { | 
|---|
 | 28 |       return $parts[7]; | 
|---|
 | 29 |     } | 
|---|
 | 30 |   } | 
|---|
 | 31 |   return undef;  # Not found. | 
|---|
 | 32 | } | 
|---|
 | 33 |  | 
|---|
 | 34 | sub my_eom_callback { | 
|---|
 | 35 |   my ($ctx) = @_; | 
|---|
 | 36 |  | 
|---|
 | 37 |   my $queueid = $ctx->getsymval('i'); | 
|---|
 | 38 |  | 
|---|
 | 39 |   my $addr = $ctx->getsymval('{client_addr}'); | 
|---|
 | 40 |   my $port = $ctx->getsymval('{client_port}'); | 
|---|
 | 41 |  | 
|---|
 | 42 |   my $uid = find_uid($addr, $port); | 
|---|
 | 43 |  | 
|---|
 | 44 |   printf STDERR "Received message from %s:%s (uid %d) (queue ID %s)\n", $addr, $port, $uid, $queueid; | 
|---|
 | 45 |  | 
|---|
 | 46 |   return SMFIS_ACCEPT; | 
|---|
 | 47 | } | 
|---|
 | 48 |  | 
|---|
 | 49 | Sendmail::Milter::setconn("local:/var/run/scripts-milter.sock"); | 
|---|
 | 50 | Sendmail::Milter::register("scripts", | 
|---|
 | 51 |                            \%my_milter_callbacks, SMFI_CURR_ACTS); | 
|---|
 | 52 |  | 
|---|
 | 53 | Sendmail::Milter::main(); | 
|---|