Monitoring-Sneck-Checks

 view release on metacpan or  search on metacpan

lib/Monitoring/Sneck/Checks/cert_file.pm  view on Meta::CPAN

	} ## end foreach my $file ( @{ $self->opt('file') } )

	if ( defined($first) ) {
		foreach my $name ( @{ $self->opt('name') // [] } ) {
			next if IO::Socket::SSL::verify_hostname_of_cert( $name, $first, 'http' );
			push( @codes,    $self->CRITICAL );
			push( @problems, 'does not cover ' . $name );
		}
		if ( defined( $self->opt('key') ) ) {
			my ( $code, $problem ) = $self->key_status( $first, $self->opt('key') );
			push( @codes,    $code );
			push( @problems, $problem ) if defined($problem);
		}
		IO::Socket::SSL::Utils::CERT_free($first);
	} ## end if ( defined($first) )

	if ( defined($fewest_days) ) {
		$self->perfdata(
			label    => 'days',
			value    => $fewest_days,
			warning  => $self->opt('warning'),
			critical => $self->opt('critical')
		);
	}
	my $message = join( ', ', grep { defined($_) } ( $first_message, @problems ) );
	@lines = () if scalar(@lines) < 2;

	return $self->finish( $self->worst(@codes), $message, @lines );
} ## end sub check

=head2 key_status

Checks a private key against a certificate.

- cert :: The Net::SSLeay X509.

- key :: The key file.

Returns the code and what is wrong, undef if nothing.

    my ( $code, $problem ) = $check->key_status( $cert, '/usr/local/etc/ssl/www.key' );

=cut

sub key_status {
	my ( $self, $cert, $key ) = @_;

	require Net::SSLeay;
	my @stat = stat($key);
	if ( !defined( $stat[0] ) || !-r $key ) {
		return ( $self->CRITICAL, 'can not read ' . $key . ', ' . ( $! || 'permission denied' ) );
	}

	# the key first, as loading a certificate that does not match a key already there drops the key
	my $context = Net::SSLeay::CTX_new();
	Net::SSLeay::CTX_set_default_passwd_cb( $context, sub { return '' } );
	my $loaded = Net::SSLeay::CTX_use_PrivateKey_file( $context, $key, Net::SSLeay::FILETYPE_PEM() );
	my $matches;
	if ($loaded) {
		Net::SSLeay::CTX_use_certificate( $context, $cert );
		$matches = Net::SSLeay::CTX_check_private_key($context);
	}
	Net::SSLeay::CTX_free($context);
	while ( Net::SSLeay::ERR_get_error() ) { }

	return ( $self->CRITICAL, $key . ' is not a key, or is encrypted' ) if !$loaded;
	return ( $self->CRITICAL, $key . ' does not match' )                if !$matches;
	return ( $self->WARNING,  $key . ' is readable by everyone' )       if $stat[2] & S_IROTH();

	return ( $self->OK, undef );
} ## end sub key_status

1;



( run in 0.639 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )