source: trunk/server/fedora/config/etc/scripts/milter.pl @ 2775

Last change on this file since 2775 was 2748, checked in by quentin, 8 years ago
Add a Milter filter to determine the uid associated with a Postfix SMTP connection.
  • Property svn:executable set to *
File size: 1.3 KB
Line 
1#!/usr/bin/perl
2
3use Sendmail::Milter;
4
5my %my_milter_callbacks = (
6                           'eom' =>        \&my_eom_callback,
7                          );
8
9sub 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
34sub 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
49Sendmail::Milter::setconn("local:/var/run/scripts-milter.sock");
50Sendmail::Milter::register("scripts",
51                           \%my_milter_callbacks, SMFI_CURR_ACTS);
52
53Sendmail::Milter::main();
Note: See TracBrowser for help on using the repository browser.