summaryrefslogtreecommitdiff
path: root/_smtpd
blob: 60654f7c21963017bd3be48726558522e5103252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use vars qw(%aliases $opt_a);
use subs qw(validate_recipient process_message read_aliases);
use Net::Server::Mail::ESMTP;
use IPC::Run qw( run );
use Sys::Syslog;

getopt('a:v');

my $smtp = new Net::Server::Mail::ESMTP;
$smtp->set_callback(RCPT => \&validate_recipient);
$smtp->set_callback(DATA => \&process_message);

openlog('[_smtpd]', '', 'mail');
read_aliases;
$smtp->process;
closelog();
#--

sub validate_recipient {
  my($session, $recipient) = @_;
  $recipient =~ s/^<//;
  $recipient =~ s/\@.*$//;
  return(0, 550, "Unknown user $recipient") unless exists $aliases{$recipient};
  return(1);
}

sub process_message {
  my($session, $data) = @_;

  #warn "DATA: ". $$data;

  my @recipients = $session->get_recipients();
  return(0, 554, 'Error: no valid recipients')
    unless(@recipients);

  syslog('info',
         'received '.length($$data).' bytes for '.join(',', @recipients)
  );
  foreach my $recipient ( @recipients ) {
    $recipient =~ s/^<//;
    $recipient =~ s/\@.*$//;

    my $result = eval { run $aliases{$recipient}, $data };
    if (!$result) {
      syslog('info', "pipe command failed: $@");
      return(0, 451, "pipe command failed: $@");
    }
  }

  syslog('info', 'message piped');
  return(1, 250, 'message piped');

}

#--

sub read_aliases {
  open(ALIASES, $opt_a || '/etc/aliases' ) or die $!;

  while(<ALIASES>) {
    chomp;
    s/^\s+//; s/\s+$//;
    next if /^$/ or /^#/;
    /^([\w\-\+\.]+):\s*("?)\|(.*)\2\s*$/ or next;
    #$aliases{$1} = [ split(/\s+/, $3) ];
    $aliases{$1} = [ qw(/bin/sh -c), $3 ];
  }

}

=head1 NAME

_smtpd - UnderSMTPD, the underscore SMTP daemon

=head1 SYNOPSIS

  #make some aliases
  echo 'username: "|someprogram and args"' > /etc/aliases

  #inetd setup
  echo "smtp stream tcp nowait mail /usr/local/bin/_smtpd" >>/etc/inetd.conf
  echo "_smtpd: my.mail.server.ip"                         >>/etc/hosts.allow
  echo "_smtpd: ALL"                                       >>/etc/hosts.deny

  #or add an smtp file to /etc/xinetd.d/
  service smtp
  {
  	socket_type = stream
  	protocol    = tcp
  	wait        = no
  	user        = mail
  	server      = /usr/local/bin/_smtpd
  }

=head1 DESCRIPTION

This is a minimal SMTP server which only forwards mail to pipe destinations
in /etc/aliases.  It does nothing else.  Its intended function is on an
internal mail server that forwards mail to other programs on a per address
basis.

UnderSMTPD reads /etc/aliases for usernames; if a match is identified
the message is piped to the given program.  Any problems executing the program
will cause a temporary SMTP error to be returned to the connecting client.

Other kinds of aliases are not recognized and cause a permanent SMTP error
to be returned to the connecting client, as do usernames not found in
/etc/aliases

UnderSMTPD was originally written to be used with the Request Tracker ticketing
system.

UnderSMTPD uses Net::Server::Mail to do all the hard work.

=head1 OPTIONS

=over 4

=item -a filename: Alternate aliases file

=back

=head1 ALIASES FORMAT

  username: |program and args
  username: "|program and args"

Quotes are not necessary around the pipe symbol, program and arguments but are
stripped if present.  Line continuations are not supported.

=head1 RT ALIASES EXAMPLE

  support: |/opt/rt3/bin/rt-mailgate --queue support --action correspond --url http://rt.example.com/
  billing: |/opt/rt3/bin/rt-mailgate --queue billing --action correspond --url http://rt.example.com/

=head1 BUGS

Yes.

=head1 AUTHOR

Ivan Kohler <ivan-undersmtpd@420.am>

=head1 SEE ALSO

L<Net::Server::Mail>

=cut

1;