certificates ala communigate, RT#7515
[freeside.git] / FS / FS / svc_cert.pm
index e3ef325..b77ff52 100644 (file)
@@ -2,6 +2,7 @@ package FS::svc_cert;
 
 use strict;
 use base qw( FS::svc_Common );
 
 use strict;
 use base qw( FS::svc_Common );
+use Tie::IxHash;
 #use FS::Record qw( qsearch qsearchs );
 use FS::cust_svc;
 
 #use FS::Record qw( qsearch qsearchs );
 use FS::cust_svc;
 
@@ -251,14 +252,18 @@ sub check_privatekey {
   return ($ok =~ /key ok/);
 }
 
   return ($ok =~ /key ok/);
 }
 
-my %subj = (
+tie my %subj, 'Tie::IxHash',
   'CN' => 'common_name',
   'O'  => 'organization',
   'OU'  => 'organization_unit',
   'L' => 'city',
   'ST' => 'state',
   'C' => 'country',
   'CN' => 'common_name',
   'O'  => 'organization',
   'OU'  => 'organization_unit',
   'L' => 'city',
   'ST' => 'state',
   'C' => 'country',
-);
+;
+
+sub subj_col {
+  \%subj;
+}
 
 sub subj {
   my $self = shift;
 
 sub subj {
   my $self = shift;
@@ -330,10 +335,13 @@ sub generate_selfsigned {
 #notAfter=Nov  6 05:07:42 2012 GMT
 #serial=B1DBF1A799EF207B
 
 #notAfter=Nov  6 05:07:42 2012 GMT
 #serial=B1DBF1A799EF207B
 
-sub check_certificate {
-  my $self = shift;
+sub check_certificate { shift->check_x509('certificate'); }
+sub check_cacert      { shift->check_x509('cacert');      }
 
 
-  my $in = $self->certificate;
+sub check_x509 {
+  my( $self, $field ) = ( shift, shift );
+
+  my $in = $self->$field;
   run( [qw( openssl x509 -noout -subject -issuer -dates -serial )],
        '<'=>\$in,
        '>pipe'=>\*OUT, '2>'=>'/dev/null'
   run( [qw( openssl x509 -noout -subject -issuer -dates -serial )],
        '<'=>\$in,
        '>pipe'=>\*OUT, '2>'=>'/dev/null'
@@ -343,11 +351,25 @@ sub check_certificate {
 
   my %hash = ();
   while (<OUT>) {
 
   my %hash = ();
   while (<OUT>) {
-    warn $_;
     /^\s*(\w+)=\s*(.*)\s*$/ or next;
     $hash{$1} = $2;
   }
 
     /^\s*(\w+)=\s*(.*)\s*$/ or next;
     $hash{$1} = $2;
   }
 
+  for my $f (qw( subject issuer )) {
+
+    $hash{$f} = { map { if ( /^\s*(\w+)=\s*(.*)\s*$/ ) {
+                          ($1=>$2);
+                        } else {
+                          (''=>'');
+                        }
+                      }
+                      split('/', $hash{$f})
+                };
+
+  }
+
+  $hash{'selfsigned'} = 1 if $hash{'subject'}->{'O'} eq $hash{'issuer'}->{'O'};
+
   %hash;
 }
 
   %hash;
 }