perl<<'EOF'
use strict;
use YAML::XS qw(LoadFile);
use LWP::UserAgent;
use HTTP::Request;

my $domain = 'domain_placeholder';

my $user;
open(my $f, '<', '/etc/userdomains');
    while(<$f>) {
        if ( /^$domain:\s(.*)$/ ) {
            $user = $1;
            last;
        }
    }
close($f);
die '[*] No account found on this server.' . "\n" unless $user;

my $found = 0;
open(my $f, '<', '/etc/userdomains');
    while(<$f>) {
        if ( /^([a-zA-Z0-9\.\-]+):\s$user$/ ) {
            my $domain = $1;
            my $config = {};
            # cpanel stopped storing SSL files in the userdata, so we're gonna just read it from this dir if it's there.
            $config->{'sslcertificatefile'} = "/var/cpanel/ssl/apache_tls/$domain/certificates" unless $config->{'sslcertificatefile'} && $config->{'sslcertificatefile'} =~ /\.crt$/;
            if (( ! -f $config->{'sslcertificatefile'} ) || ( ! $config->{'sslcertificatefile'} )) {
                print "[!] Unable to load SSL cert for [$domain]. You should report this\n";
                next;
            }
            print '[*] SSL found on [' . $domain . ']:' . "\n";
            print "\t" . get_data($config->{'sslcertificatefile'}, 'subject') . "\n";
            print "\t" . get_data($config->{'sslcertificatefile'}, 'issuer') . "\n";
            print "\t" . get_data($config->{'sslcertificatefile'}, 'startdate') . "\n";
            print "\t" . get_data($config->{'sslcertificatefile'}, 'enddate') . "\n";
            print "\t" . 'alternates=' . get_alternates($config->{'sslcertificatefile'}) . "\n";
            print "\t" . 'isDedicatedIp=' . is_dedicated($domain) . "\n";
            print "\t" . 'redirectsToHttps=' . check_redirect($domain) . "\n";
        }
    }
close($f);
die '[*] No SSLs found on the account [' . $user . ']' . "\n" unless $found;

sub get_data {
    my $cert = shift;
    my $item = shift;
    chomp(my $output = `openssl x509 -$item -noout -in $cert`);
    return($output);
}

sub get_alternates {
    my $cert = shift;
    chomp(my $alternates = `openssl x509 -text -noout -in $cert |grep DNS`);
    $alternates =~ s/^\s+//g;
    $alternates =~ s/DNS://g;
    return($alternates);
}

sub is_dedicated {
    my $domain = shift;
    open(my $f, '<', '/etc/domainips');
        while(<$f>) {
            if ( /^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: $domain$/ ) {
                return('True');
            }
        }
    close($f);
    return('False');
}

sub check_redirect {
    my $domain = shift;
    my $ua = LWP::UserAgent->new();
    $ua->timeout(10);
    my $request = HTTP::Request->new( GET => 'http://' . $domain );
    my $response = $ua->request($request);
    if ( $response->is_success && $response->previous && $response->request->uri =~ /^https:/ ) {
        return('True');
    } else {
        return('False');
    }
}
    
EOF
