]> git.donarmstrong.com Git - bin.git/blob - sshsendmail
add a VERBOSE flag to getmail
[bin.git] / sshsendmail
1 #! /usr/bin/perl
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
5 # information.
6 # Copyright 2005-10 by Don Armstrong <don@donarmstrong.com>.
7
8 # parse command line options
9
10 # connect to host
11
12 use warnings;
13 use strict;
14
15 use Getopt::Long;
16 use Pod::Usage;
17
18 =head1 NAME
19
20 sshsendmail - Uses ssh to send a mail message to a different machine's copy of sendmail.
21
22 =head1 SYNOPSIS
23
24 Stick this command in /usr/lib/nullmailer/sshsendmail.
25
26 Then add
27
28 foohost.com sshsendmail --identity=/var/mail/.ssh/id_rsa --username remotemail --sendmail-options='-baruser@foohost.com'
29
30 to /etc/nullmailer/remotes.
31
32  Options:
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
39
40 =head1 OPTIONS
41
42 =over
43
44 =item B<--identity, -i>
45
46 ssh identity to send to the server we're connecting to
47
48 =item B<--debug, -d>
49
50 Debug verbosity. (Default 0)
51
52 =item B<--help, -h>
53
54 Display brief useage information.
55
56 =item B<--man, -m>
57
58 Display this manual.
59
60 =back
61
62
63 =cut
64
65 use User;
66 use File::Basename qw(basename);
67 use IO::Handle;
68 use IO::File;
69 use Digest::MD5 qw(md5_hex);
70 use Sys::Syslog qw(:standard :macros);
71 use Data::Dumper;
72
73 use vars qw($DEBUG);
74
75 $0 = basename($0);
76
77 # XXX parse config file
78
79 openlog('sshsendmail',[qw(nofatal perror pid)], LOG_MAIL);
80
81 my %options = (debug              => 0,
82                help               => 0,
83                man                => 0,
84                host               => undef,
85                identity           => undef,
86                username           => undef,
87                'sendmail_options' => [],
88               );
89
90 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
91            'sendmail_options|sendmail-options|o=s@',
92            'help|h|?','man|m');
93
94 pod2usage() if $options{help};
95 pod2usage({verbose=>2}) if $options{man};
96
97 $DEBUG = $options{debug};
98
99 if (not @ARGV) {
100      print STDERR "${0}: Too few command-line arguments\n";
101      print <<END;
102 usage: ${0} [flags] remote-address < mail-file
103 Send an email message via ssh+sendmail
104   -p, --port=INT  Set the port number on the remote host to connect to
105   -d, --daemon    use syslog exclusively  (Debian only)
106   -s, --syslog    use syslog additionally (Debian only)
107
108   -h, --help      Display this help and exit
109 END
110      exit(1);
111 }
112
113 my $hostname = shift @ARGV;
114
115 my @message = <>;
116 #throw away envelope sender
117 shift @message;
118 my @recipients;
119
120 while (my $line = shift @message) {
121      last if $line eq "\n";
122      chomp $line;
123      push @recipients,$line;
124 }
125
126 @recipients = qw(-t) if not @recipients;
127
128 my @ssh_arguments = ($hostname);
129
130 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
131 push @ssh_arguments, '-l', $options{username} if defined $options{username};
132 my @sendmail_options;
133 push @sendmail_options,
134     ref($options{sendmail_options})?@{$options{sendmail_options}}:$options{sendmail_options};
135 push @sendmail_options,@recipients;
136 $Data::Dumper::Useqq=1;
137 my $sendmail_options = Data::Dumper->Dump([\@sendmail_options],[qw(*sendmail_options)]);
138 print STDERR $sendmail_options;
139 push @ssh_arguments, q(perl -e ').<<EOF .q(');
140 use Digest::MD5 qw(md5_hex);
141 use IO::Handle;
142 my \@message = <>;
143 my $sendmail_options
144 my \$digest = pop \@message;
145 \$digest =~ /(.*)([0-9a-fA-F]{32})\n/;
146 \$digest = \$2;
147 if (length \$1) {
148     push \@message,\$1;
149 }
150 my \$message = join(q(),\@message);
151 if (\$digest eq md5_hex(\$message)) {
152     my \$sendmail = IO::Handle->new();
153     open (\$sendmail,q(|-),q(/usr/lib/sendmail), \@sendmail_options) or
154         die "Unable to open sendmail: \$!";
155     print {\$sendmail} \$message or
156         die "Unable to write to sendmail: \$!";
157     close (\$sendmail) or
158         die "Unable to close sendmail: \$!";
159 } else {
160    die "Digest failure! \$digest vs ".md5_hex(\$message);
161 }
162 EOF
163
164 $Data::Dumper::Useqq=0;
165 print STDERR Dumper(\@ssh_arguments);
166
167 qx(ping -q -w 3 -c 1 $hostname 2>/dev/null);
168 if ($?) {
169     syslog(LOG_WARNING,"${0}: Failed: unable to ping $hostname\n");
170     exit (9);
171 }
172 print STDERR md5_hex(join('',@message))."\n";
173 my $ssh = new IO::Handle;
174 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
175 print {$ssh} @message or exit(17);
176 print {$ssh} md5_hex(join('',@message))."\n";
177 close $ssh or exit(17);
178 if ($?) {
179     syslog(LOG_WARNING,"${0}: Failed: sendmail died for some reason\n");
180     syslog(LOG_WARNING,join("\n",@ssh_arguments));
181     exit (17);
182 }
183 else {
184     syslog(LOG_INFO,"${0}: Succeeded: Yeay\n");
185     exit 0;
186 }