2 # sshsendmail uses ssh to send a mail message to a different machine,
3 # and is released under the terms of the GPL version 2, or any later
4 # version, at your option. See the file README and COPYING for more
6 # Copyright 2005-10 by Don Armstrong <don@donarmstrong.com>.
8 # parse command line options
20 sshsendmail - Uses ssh to send a mail message to a different machine's copy of sendmail.
24 Stick this command in /usr/lib/nullmailer/sshsendmail.
28 foohost.com sshsendmail --identity=/var/mail/.ssh/id_rsa --username remotemail --sendmail-options='-baruser@foohost.com'
30 to /etc/nullmailer/remotes.
33 --identity, -i ssh identity to use to connect to the server
34 --username, -l remote username
35 --sendmail-options, -o options to pass to sendmail
36 --debug, -d debugging level (Default 0)
37 --help, -h display this help
38 --man, -m display manual
44 =item B<--identity, -i>
46 ssh identity to send to the server we're connecting to
50 Debug verbosity. (Default 0)
54 Display brief useage information.
66 use File::Basename qw(basename);
69 use Digest::MD5 qw(md5_hex);
70 use Sys::Syslog qw(:standard :macros);
77 # XXX parse config file
79 openlog('sshsendmail',[qw(nofatal perror pid)], LOG_MAIL);
81 my %options = (debug => 0,
87 'sendmail_options' => [],
90 my $new_nullmailer = 0;
91 ## The new nullmailer passes options on STDIN and the message on FD 3.
92 my $message_fd = \*STDIN;
94 my $fh = IO::Handle->new() or
95 die "Unable to create new fd";
97 die "Unable to open fd 3: $!";
100 push @ARGV, map {chomp; "--$_"} <STDIN>;
103 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
105 'sendmail_options|sendmail-options|o=s@',
108 pod2usage() if $options{help};
109 pod2usage({verbose=>2}) if $options{man};
111 $DEBUG = $options{debug};
113 if (not (@ARGV or defined $options{host})) {
114 print STDERR "${0}: Too few command-line arguments\n";
116 usage: ${0} [flags] remote-address < mail-file
117 Send an email message via ssh+sendmail
118 -p, --port=INT Set the port number on the remote host to connect to
119 -d, --daemon use syslog exclusively (Debian only)
120 -s, --syslog use syslog additionally (Debian only)
122 -h, --help Display this help and exit
127 if (not defined $options{host} and @ARGV) {
128 $options{host} = shift @ARGV;
131 my @message = <$message_fd>;
132 #throw away envelope sender
136 while (my $line = shift @message) {
137 last if $line eq "\n";
139 push @recipients,$line;
142 @recipients = qw(-t) if not @recipients;
144 my @ssh_arguments = ($options{host});
146 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
147 push @ssh_arguments, '-l', $options{username} if defined $options{username};
148 my @sendmail_options;
149 push @sendmail_options,
150 ref($options{sendmail_options})?@{$options{sendmail_options}}:$options{sendmail_options};
151 push @sendmail_options,@recipients;
152 $Data::Dumper::Useqq=1;
153 my $sendmail_options = Data::Dumper->Dump([\@sendmail_options],[qw(*sendmail_options)]);
154 print STDERR $sendmail_options if $DEBUG;
155 push @ssh_arguments, q(perl -e ').<<EOF .q(');
156 use Digest::MD5 qw(md5_hex);
160 my \$digest = pop \@message;
161 \$digest =~ /(.*)([0-9a-fA-F]{32})\n/;
166 my \$message = join(q(),\@message);
167 if (\$digest eq md5_hex(\$message)) {
168 my \$sendmail = IO::Handle->new();
169 open (\$sendmail,q(|-),q(/usr/lib/sendmail), \@sendmail_options) or
170 die "Unable to open sendmail: \$!";
171 print {\$sendmail} \$message or
172 die "Unable to write to sendmail: \$!";
173 close (\$sendmail) or
174 die "Unable to close sendmail: \$!";
176 die "Digest failure! \$digest vs ".md5_hex(\$message);
180 $Data::Dumper::Useqq=0;
181 print STDERR Dumper(\@ssh_arguments) if $DEBUG;
183 qx(ping -q -w 3 -c 1 $options{host} 2>/dev/null);
185 syslog(LOG_WARNING,"${0}: Failed: unable to ping $options{host}\n");
188 print STDERR md5_hex(join('',@message))."\n" if $DEBUG;
189 my $ssh = new IO::Handle;
190 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
191 print {$ssh} @message or exit(17);
192 print {$ssh} md5_hex(join('',@message))."\n";
193 close $ssh or exit(17);
195 syslog(LOG_WARNING,"${0}: Failed: sendmail died for some reason\n");
196 syslog(LOG_WARNING,join("\n",@ssh_arguments));
200 syslog(LOG_INFO,"${0}: Succeeded: Yeay\n");