Debian Codegraph @

check_email_delivery/src/imap_ssl_cert

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl
2 use strict;
3 my $VERSION = '0.1';
4 my $COPYRIGHT = 'Copyright (C) 2005-2011 Jonathan Buhacoff <[email protected]>';
5 my $LICENSE = 'http://www.gnu.org/licenses/gpl.txt';
6 my %status = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3 );
7
8 use Getopt::Long;
9 use Mail::IMAPClient;
10 use IO::Socket::SSL;
11 use Net::SSLeay;
12
13 # get options from command line
14 Getopt::Long::Configure("bundling");
15 my $verbose = 0;
16 my $help = "";
17 my $help_usage = "";
18 my $show_version = "";
19 my $imap_server = "";
20 my $default_imap_port = "143";
21 my $default_imap_ssl_port = "993";
22 my $imap_port = "";
23 my $timeout = 60;
24 my $ok;
25 $ok = Getopt::Long::GetOptions(
26 "V|version"=>\$show_version,
27 "v|verbose+"=>\$verbose,"h|help"=>\$help,"usage"=>\$help_usage,
28 # imap settings
29 "H|hostname=s"=>\$imap_server,"p|port=i"=>\$imap_port,
30 # time
31 "t|timeout=i"=>\$timeout
32 );
33
34 if( $show_version ) {
35 print "$VERSION\n";
36 exit $status{UNKNOWN};
37 }
38
39 if( $help ) {
40 exec "perldoc", $0 or print "Try `perldoc $0`\n";
41 exit $status{UNKNOWN};
42 }
43
44 if( $help_usage
45 ||
46 ( $imap_server eq "" )
47 ) {
48 print "Usage: $0 -H host [-p port]\n";
49 exit $status{UNKNOWN};
50 }
51
52 my @certs = (); # we have to store the certs we get Net::SSLeay here so that we can output them in REVERSE order (server cert first, root cert last)
53
54 # connect to IMAP server
55 print "connecting to server $imap_server\n" if $verbose > 2;
56 my $imap;
57 eval {
58 local $SIG{ALRM} = sub { die "exceeded timeout $timeout seconds\n" }; # NB: \n required, see `perldoc -f alarm`
59 alarm $timeout;
60
61 $imap_port = $default_imap_ssl_port unless $imap_port;
62 my $socket = IO::Socket::SSL->new(
63 PeerAddr => "$imap_server:$imap_port",
64 SSL_verify_mode => 1,
65 SSL_ca_file => undef,
66 SSL_verifycn_scheme => 'imap',
67 SSL_verifycn_name => $imap_server,
68 SSL_verify_callback => \&ssl_printer
69 );
70 die IO::Socket::SSL::errstr() unless $socket;
71 $socket->autoflush(1);
72 $imap = Mail::IMAPClient->new(Socket=>$socket, Debug => 0 );
73 $imap->State(Mail::IMAPClient->Connected);
74 $imap->_read_line() if "$Mail::IMAPClient::VERSION" le "2.2.9"; # necessary to remove the server's "ready" line from the input buffer for old versions of Mail::IMAPClient. Using string comparison for the version check because the numeric didn't work on Darwin and for Mail::IMAPClient the next version is 2.3.0 and then 3.00 so string comparison works
75 # $imap->User($username);
76 # $imap->Password($password);
77 # $imap->login() or die "Cannot login: $@";
78
79 print join("\n",reverse(@certs));
80 alarm 0;
81 };
82 if( $@ ) {
83 chomp $@;
84 print "Could not connect to $imap_server port $imap_port: $@\n";
85 exit $status{CRITICAL};
86 }
87 unless( $imap ) {
88 print "Could not connect to $imap_server port $imap_port: $@\n";
89 exit $status{CRITICAL};
90 }
91
92 # deselect the mailbox
93 $imap->close();
94
95 # disconnect from IMAP server
96 print "disconnecting from server\n" if $verbose > 2;
97 $imap->logout();
98
99
100 exit $status{OK};
101
102 # see IO::Socket::SSL documentation for SSL_verify_callback:
103 sub ssl_printer {
104 my ($boolOpenSSLResult, $cmemCertificateStore, $strCertIssuerOwnerAttr, $strError, $cmemPeerCertificate) = @_;
105 warn "OpenSSL says certificate is " . ( $boolOpenSSLResult ? "valid" : "invalid" ) if $verbose > 0;
106 warn "Peer certificate: $strCertIssuerOwnerAttr" if $verbose > 0;
107 warn "Errors: $strError" if $verbose > 0;
108 #print Net::SSLeay::PEM_get_string_X509($cmemPeerCertificate);
109 push @certs, $strCertIssuerOwnerAttr . "\n" . Net::SSLeay::PEM_get_string_X509($cmemPeerCertificate);
110 }
111
112 package main;
113 1;
114
115 __END__
116
117
118 =pod
119
120 =head1 NAME
121
122 imap_ssl_cert - connects to an IMAP server using SSL and saves the server certificate into a .pem file
123
124 =head1 SYNOPSIS
125
126 imap_ssl_cert -H imap.server.com > server_ca_file.pem
127 imap_ssl_cert -?
128 imap_ssl_cert --help
129
130 =head1 DEPENDENCIES
131
132 This utility requires the following perl modules to be installed:
133
134 Getopt::Long
135 Mail::IMAPClient
136 IO::Socket::SSL
137 Net::SSLeay
138
139 =head1 OPTIONS
140
141 =over
142
143 =item --timeout <seconds>
144
145 Abort with critical status if it takes longer than <seconds> to connect to the IMAP server. Default is 60 seconds.
146 The difference between timeout and critical is that, with the default settings, if it takes 45 seconds to
147 connect to the server then the connection will succeed but the plugin will return CRITICAL because it took longer
148 than 30 seconds.
149 Also known as: -t <seconds>
150
151 =item --hostname <server>
152
153 Address or name of the IMAP server. Examples: mail.server.com, localhost, 192.168.1.100
154 Also known as: -H <server>
155
156 =item --port <number>
157
158 Service port on the IMAP server. Default is 143. If you use SSL, default is 993.
159 Also known as: -p <number>
160
161 =item --verbose
162
163 Display additional information. Useful for troubleshooting.
164
165 Also known as: -v
166
167 =item --version
168
169 Display plugin version and exit.
170 Also known as: -V
171
172 =item --help
173
174 Display this documentation and exit.
175 Also known as: -h
176
177 =item --usage
178
179 Display a short usage instruction and exit.
180
181 =back
182
183 =head1 EXAMPLES
184
185 =head2 Print the server's SSL certificate chain
186
187 $ perl imap_ssl_cert.pl -H imap.server.com > ca_file.pem
188 $ cat ca_file.pem
189
190 -----BEGIN CERTIFICATE-----
191 MIID1zCCAr+gAwIBAgIQPr3bVk0SkuXygjxgA7EVGDANBgkqhkiG9w0BAQUFADA8
192 [...snip...]
193 0FF4warjskrfqaVtWeIV58LJheaM4cPJkc2M
194 -----END CERTIFICATE-----
195
196 $ openssl x509 -in ca_file.pem -text
197
198
199 =head1 SEE ALSO
200
201 http://en.wikipedia.org/wiki/X.509
202 http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail
203 http://tools.ietf.org/html/rfc1422
204 http://search.cpan.org/~mikem/Net-SSLeay-1.42/lib/Net/SSLeay.pm
205 http://search.cpan.org/~plobbes/Mail-IMAPClient-3.29/lib/Mail/IMAPClient.pod
206
207 =head1 CHANGES
208
209 Fri Nov 11 03:38:13 AST 2011
210 + version 0.1
211
212 =head1 AUTHOR
213
214 Jonathan Buhacoff <[email protected]>
215
216 =head1 COPYRIGHT AND LICENSE
217
218 Copyright (C) 2011 Jonathan Buhacoff
219
220 This program is free software; you can redistribute it and/or modify
221 it under the terms of the GNU General Public License as published by
222 the Free Software Foundation; either version 3 of the License, or
223 (at your option) any later version.
224
225 This program is distributed in the hope that it will be useful,
226 but WITHOUT ANY WARRANTY; without even the implied warranty of
227 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228 GNU General Public License for more details.
229
230 You should have received a copy of the GNU General Public License
231 along with this program. If not, see <http://www.gnu.org/licenses/>.
232
233 http://www.gnu.org/licenses/gpl.txt
234
235 =cut
236