1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #!/usr/bin/perl
use Switch;
# 2012-01-29-johnpfeiffer
sub systemcommand( $$ )
{
my $command = $_[0] ;
my @arguments = split( " " , $_[1] ) ;
my $result = system $command,@arguments;
return $result;
}
sub trim( $ )
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
sub getuserinput()
{
chomp( my $userinput = <STDIN> );
$userinput = trim( $userinput );
return $userinput;
}
sub getkeylocation()
{
my $location = "/var/lib/ssl/cert.key";
return $location;
}
sub getcertificatelocation()
{
my $location = "/var/lib/ssl/cert.crt";
return $location;
}
sub getintermediatecertificatelocation()
{
my $location = "/var/lib/ssl/intermediate.crt";
return $location;
}
sub checkkey()
{
my $location = getkeylocation();
if( -e "$location" )
{
print "location: $location \n";
my $result = qx( openssl rsa -check -in $location | head -n2 );
chomp( $result );
print "$result \n";
}else
{ print "$location not found \n";
}
}
sub checkcertificate()
{
my $location = getcertificatelocation();
if( -e "$location" )
{
print "location: $location \n";
my $result = qx( openssl x509 -text -in $location | head -n10 );
chomp( $result );
print "$result \n";
}else
{ print "$location not found \n";
}
}
sub checkintermediatecertificate()
{
my $location = getintermediatecertificatelocation();
if( -e "$location" )
{
print "location: $location \n";
my $result = qx( openssl x509 -text -in $location | head -n10 );
chomp( $result );
print "$result \n";
}else
{ print "$location not found \n";
}
}
sub resetcertificate()
{
my $sourcefile = "/var/lib/ssl/BACKUP/cert.crt";
my $target = getcertificatelocation();
use File::Copy;
copy( $sourcefile , $target ) or die "File cannot be copied.\n";
print "Successfully reset $target \n";
}
sub resetintermediatecertificate()
{
my $sourcefile = "/var/lib/ssl/BACKUP/intermediate.crt";
my $target = getintermediatecertificatelocation();
use File::Copy;
copy( $sourcefile , $target ) or die "File cannot be copied.\n";
print "Successfully reset $target \n";
}
sub resetkey()
{
my $sourcefile = "/var/lib/ssl/BACKUP/cert.key";
my $target = getkeylocation();
use File::Copy;
copy( $sourcefile , $target ) or die "File cannot be copied.\n";
print "Successfully reset $target \n";
}
sub displaymenuinputerror(;$)
{
print ("You entered: $_[0] \n\n");
print "Please enter a digit between 0 and 9\n";
print "(without any arrow keys, spaces, and other special characters)\n";
}
sub menu
{
print "\n";
systemcommand( "/bin/date" , "+%Z_%Y-%m-%d_%T" );
print "\n Gateway: ";
systemcommand( "/bin/uname -n" , " " );
print "\n";
print " 1. Check the current ssl cert \n";
print " 2. Check the current ssl intermediate \n";
print " 3. Check the current ssl key \n";
print " 9. Reset certificate , intermediate , and key. \n";
print ": ";
}
my $mainmenuentry = "";
while (true)
{
menu();
$mainmenuentry = getuserinput();
switch( $mainmenuentry )
{
case "1"
{
checkcertificate();
break;
}
case "2"
{
checkintermediatecertificate();
break;
}
case "3"
{
checkkey();
break;
}
case "9"
{
resetcertificate();
resetintermediatecertificate();
resetkey();
break;
}
else
{ displaymenuinputerror( $mainmenuentry );
break;
}
} # end switch
} # end infinite menu loop
|