0cd50600c022f426d8b4334c830d02ae12d69fcc
[manu/sslexpire.git] / sslexpire / sslexpire
1 #!/usr/bin/perl -w
2 # Check peer certificate validity
3 # Require perl module : IO::Socket, Net::SSLeay, Date::Parse
4 # Require unix programs : openssl, echo, sendmail
5 #
6 # Copyright (C) 2003 Emmanuel Lacour <elacour@home-dn.net>
7 #
8 # This file is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by the
10 # Free Software Foundation; either version 2, or (at your option) any
11 # later version.
12 #
13 # This file is distributed in the hope that it will be
14 # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this file; see the file COPYING.  If not, write to the Free
20 # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 # 02110-1301, USA.
22 #
23
24
25 # Local variables are prefixed with "l_"
26
27 use strict;
28 use IO::Socket;
29 use Net::SSLeay;
30 use Getopt::Long;
31 use Date::Parse;
32
33 Net::SSLeay::SSLeay_add_ssl_algorithms();
34 Net::SSLeay::randomize();
35
36 my $VERSION = '0.6.3';
37 my $AUTHOR = 'Emmanuel Lacour, <elacour@home-dn.net>';
38
39 # Default values
40 my $opensslpath = "/usr/bin/openssl";
41 my $sendmailpath = "/usr/lib/sendmail";
42 my $mailreport = 0;
43 my $alert = 5;
44 my $mail = "root";
45 my $conf = "/etc/sslexpire/sslexpire.conf";
46
47 my %hosts;
48 my $host;
49 my @ports;
50 my $port;
51 my $portlist;
52 my $rhost;
53 my $rport;
54 my $tmp;
55
56
57
58 # Get options
59 # output : -m = mail, default = stdout
60 # host : -h host, -p port, default = from config file
61 # config : -f configfile, default /etc/sslexpire/sslexpire.conf 
62 # standard : --help, --version
63
64 my %opts;
65 GetOptions (\%opts,
66     'host|h=s',
67     'port|p=s',
68     'mail|m',
69     'conf|c=s',
70     'verbose|v',
71     'help',
72     'version|');
73
74 # Command line is used
75 if (($opts{'host'}) && ($opts{'port'})) {
76     push @{$hosts{$opts{'host'}}}, $opts{'port'};
77
78 } elsif (($opts{'host'}) || ($opts{'port'})) {
79         print STDERR "ERR: please provide HOST _and_ PORT or use a configuration file.\n";
80         &usage;
81
82 # Configuration file is used
83 } else {
84
85     if ($opts{'conf'}) {
86         $conf = $opts{'conf'};
87     }
88
89     # Parse config file
90
91     open (CONF,$conf) or die "Couldn't read configuration file $conf: $!\n";
92     while (<CONF>) {
93         # Skip comments
94         next if (/^[ \t]*#/);
95         # Alert param.
96         if (/^alert[ \t]*=/) { 
97         ($tmp,$alert) = split /=/, $_;
98         # Mail param.
99         } elsif (/^mail[ \t]*=/) { 
100         ($tmp,$mail) = split /=/, $_;
101         # Use hosts from config file if none are given by command line
102         } elsif ((!$opts{'host'}) && (!$opts{'port'}) && (/:/)) {
103         ($tmp,$portlist) = split /:/, $_;
104         chomp ($tmp);
105         chomp ($portlist);
106         # There is multiple ports
107         if (/,/) {
108             @ports = split /,/, $portlist;
109             foreach (@ports) {
110                 push @{$hosts{$tmp}}, $_;
111             }
112         # There is only one port
113         } else {
114            push @{$hosts{$tmp}},$portlist;
115         }
116         }
117     }
118     close CONF;
119
120 }
121
122 $mailreport = 1 if ($opts{'mail'});
123
124 if ($opts{'help'}) {
125     &usage;
126 }
127
128 if ($opts{'version'}) {
129     print "sslexpire $VERSION\n";
130     print "Written by $AUTHOR\n";
131     exit;
132 }
133
134 # Print program usage
135 sub usage {
136     print "Usage: sslexpire [OPTION]...
137 -h, --host=HOST        check only this host
138 -p, --port=TCPPORT     check this port on the previous host
139 -m, --mail             report by mail instead of STDOUT
140 -c, --conf=FILE        use this config file
141     --help             print this help, then exit
142 ";
143     exit;
144 }
145
146
147 # This will return the expiration date
148 sub getExpire {
149
150     my ($l_host,$l_port) = @_;
151     my ($l_expdate,$l_comment);
152
153     # Connect to $l_host:$l_port
154     my $socket = IO::Socket::INET->new(
155         Proto => "tcp",
156         PeerAddr => $l_host,
157         PeerPort => $l_port
158         );
159     # If we connected successfully
160     if ($socket) {
161         # Intiate ssl
162         my $l_ctx = Net::SSLeay::CTX_new();
163         my $l_ssl = Net::SSLeay::new($l_ctx);
164
165         Net::SSLeay::set_fd($l_ssl, fileno($socket));
166         my $res = Net::SSLeay::connect($l_ssl);
167
168         # Get peer certificate
169         my $l_x509 = Net::SSLeay::get_peer_certificate($l_ssl);
170         if ($l_x509) {
171             my $l_string = Net::SSLeay::PEM_get_string_X509($l_x509);
172             # Get the expiration date, using openssl
173             ($l_expdate,$l_comment) = split(/\n/, `echo "$l_string" | $opensslpath x509 -enddate -subject -noout 2>&1`);
174             $l_expdate =~ s/.*=//;
175             chomp($l_expdate);
176         } else {
177             $l_expdate = 1;
178             $l_comment = 1;
179         }
180
181         # Close and cleanup
182         Net::SSLeay::free($l_ssl);
183         Net::SSLeay::CTX_free($l_ctx);
184         close $socket;
185     } else {
186             $l_expdate = 1;
187             $l_comment = 1;
188     }
189     return ($l_expdate,$l_comment);
190 }
191
192
193 # Report if needed
194
195 #
196 sub report {
197     # Convert date into epoch
198     my ($l_expdate,$l_comment,$l_host,$l_port) = @_;
199     my $l_subject = "";
200     
201     if ($l_expdate ne "1") {
202         # The current date
203         my $l_today =  time;
204         my $l_epochdate = str2time($l_expdate);
205
206         # Calculate diff between expiration date and today
207         my $l_diff = ($l_epochdate - $l_today)/(3600*24);
208
209         # Report if needed
210         if ($l_diff < $alert) {
211             $l_subject = "Warning ssl certificate on $l_host:$l_port expires in $l_diff days:" if ($l_diff > 1);
212             $l_subject = "Warning ssl certificate on $l_host:$l_port expires today:" if (($l_diff > 0) && ($l_diff < 1));
213             $l_subject = "Warning ssl certificate on $l_host:$l_port expired:" if ($l_diff <= 0);
214             my $l_mesg = "Expiration date: $l_expdate\n$l_comment\n";
215             # Mail report
216             if ($mailreport) {
217                 sendmail($mail, $l_subject, $l_mesg);
218             } else {
219                 print "$l_subject\n";
220                 print "$l_mesg\n";
221             }
222         }
223     } else {
224         $l_subject = "Unable to read certificate on $l_host:$l_port!";
225         if ($mailreport) {
226             sendmail($mail, $l_subject, "");
227         } else {
228             print "$l_subject\n";
229         }
230     }
231 }
232
233
234 # Send mail - sendmail (to,subject,body)
235 sub sendmail {
236     my $to = shift;
237     my $subj = shift;
238     my $mesg = shift;
239     chomp ($to);
240     chomp ($subj);
241     chomp ($mesg);
242     open (MAIL,"| $sendmailpath -t") or die "Couldn't open $sendmailpath: $!\n";
243     print MAIL "To: $to\n";
244     print MAIL "Subject: $subj\n";
245     print MAIL "\n";
246     print MAIL "$mesg\n" if $mesg;
247     close MAIL;
248     if ((my $status = $?>>8) != 0) {
249        die "sendmail: exit status $status\n";
250     }
251 }
252
253 # Main
254 #
255
256
257 # We haven't hosts to check...
258 if (!%hosts) {
259     print STDERR "No host to check!\n";
260     &usage;
261 };
262
263
264 # Parse hosts
265 foreach $host (keys %hosts) {
266     # Parse ports for each hosts
267     foreach $port (@{$hosts{$host}}) {
268         if ($opts{'verbose'}) {
269             print "Checking\t$host:$port\n";
270         }
271
272         # Get expiration date
273         my ($expdate,$comment) = &getExpire($host,$port);
274
275         # Report
276         &report("$expdate","$comment","$host","$port");
277     }
278 }
279
280
281 # script documentation (POD style)
282
283 =head1 NAME
284
285 sslexpire - Remotely check ssl certificate expiration date.
286
287 =head1 DESCRIPTION
288
289 This program connect to an host:port to retrieve the expiration date of the ssl
290 certificate. It gives a report to STDOUT or by email using configuration file.
291
292 =head1 COMMAND LINE PARAMETERS
293
294 Optional command line parameters are the host and the port to connect. This
295 allow checking a single host instead of using those given in the configuration
296 file for periodically checks.
297
298 =head1 OPTIONS
299
300 =head2 B<-c> I<FILE>, B<--conf>=I<FILE>
301     
302 Specify an alternate config file.
303
304 =head2 B<-h> I<HOST>, B<--host>=I<HOST>
305
306 Connect to I<HOST> instead of those given in the config file.
307
308 =head2 B<-p> I<PORT>, B<--port>=I<PORT>
309
310 Specify the port to connect to (used in conjonction with --host).
311
312 =head2 B<-v>, B<--verbose>
313
314 Prints out verbose messages.
315
316 =head2 B<-m>, B<--mail>
317
318 Send report by mail instead of STDOUT. It will use the address given in the
319 config file or root by default.
320
321 =head2 B<--help>
322
323 Prints out command-line help.
324
325 =head2 B<--version>
326
327 Prints out version information.
328
329 =head1 FILES
330
331 /etc/sslexpire/sslexpire.conf
332
333 =head1 AUTHOR
334
335 Emmanuel Lacour, elacour@home-dn.net
336
337 =cut