]> git.donarmstrong.com Git - bin.git/blob - sshsendmail
allow sendmail options to be specified multiple times
[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
70 use vars qw($DEBUG);
71
72 $0 = basename($0);
73
74 # XXX parse config file
75
76 my %options = (debug              => 0,
77                help               => 0,
78                man                => 0,
79                host               => undef,
80                identity           => undef,
81                username           => undef,
82                'sendmail_options' => [''],
83               );
84
85 GetOptions(\%options,'identity|i=s','username|l=s','daemon|d', 'syslog|s',
86            'sendmail_options|sendmail-options|o=s@',
87            'help|h|?','man|m');
88
89 pod2usage() if $options{help};
90 pod2usage({verbose=>2}) if $options{man};
91
92 $DEBUG = $options{debug};
93
94 if (not @ARGV) {
95      print STDERR "${0}: Too few command-line arguments\n";
96      print <<END;
97 usage: ${0} [flags] remote-address < mail-file
98 Send an email message via ssh+sendmail
99   -p, --port=INT  Set the port number on the remote host to connect to
100   -d, --daemon    use syslog exclusively  (Debian only)
101   -s, --syslog    use syslog additionally (Debian only)
102
103   -h, --help      Display this help and exit
104 END
105      exit(1);
106 }
107
108 my $hostname = shift @ARGV;
109
110 my @message = <>;
111 #throw away envelope sender
112 shift @message;
113 my @recipients;
114
115 while (my $line = shift @message) {
116      last if $line eq "\n";
117      chomp $line;
118      push @recipients,$line;
119 }
120
121 @recipients = qw(-t) if not @recipients;
122
123 my @ssh_arguments = ($hostname);
124
125 push @ssh_arguments, '-i', $options{identity} if defined $options{identity};
126 push @ssh_arguments, '-l', $options{username} if defined $options{username};
127 push @ssh_arguments, q(cat - | /usr/lib/sendmail ).
128     join(' ',ref($options{sendmail_options})?@{$options{sendmail_options}}:($options{sendmail_options}//'')).
129     ' '.join(' ',@recipients);
130 qx(ping -q -c 3 $hostname 2>/dev/null);
131 if ($?) {
132      print STDERR "${0}: Failed: unable to ping $hostname\n";
133      exit (9);
134 }
135 my $ssh = new IO::Handle;
136 open($ssh,'|-','ssh',@ssh_arguments) or exit(17);
137 print {$ssh} @message or exit(17);
138 close $ssh or exit(17);
139 if ($?) {
140      print STDERR "${0}: Failed: sendmail died for some reason\n";
141      exit (17);
142 }
143 else {
144      print STDERR "${0}: Succeeded: Yeay\n";
145      exit 0;
146 }