73085: Enable credit card/ach encryption on a live system [v3 merge]
[freeside.git] / FS / FS / Setup.pm
index 3725115..f092919 100644 (file)
@@ -5,6 +5,7 @@ use vars qw( @ISA @EXPORT_OK );
 use Exporter;
 #use Tie::DxHash;
 use Tie::IxHash;
+use Crypt::OpenSSL::RSA;
 use FS::UID qw( dbh driver_name );
 use FS::Record;
 
@@ -13,7 +14,7 @@ $FS::svc_domain::whois_hack = 1;
 $FS::svc_domain::whois_hack = 1;
 
 @ISA = qw( Exporter );
-@EXPORT_OK = qw( create_initial_data );
+@EXPORT_OK = qw( create_initial_data enable_encryption );
 
 =head1 NAME
 
@@ -55,6 +56,8 @@ sub create_initial_data {
   populate_msgcat();
 
   populate_numbering();
+
+  enable_encryption();
   
   if ( $oldAutoCommit ) {
     dbh->commit or die dbh->errstr;
@@ -62,6 +65,33 @@ sub create_initial_data {
 
 }
 
+sub enable_encryption {
+
+  eval "use FS::Conf";
+  die $@ if $@;
+
+  my $conf = new FS::Conf;
+
+  die "encryption key(s) already in place"
+    if $conf->exists('encryptionpublickey')
+    || $conf->exists('encryptionprivatekey');
+
+  my $length = 2048;
+  my $rsa = Crypt::OpenSSL::RSA->generate_key($length);
+
+  $conf->set('encryption', 1);
+  $conf->set('encryptionmodule',     'Crypt::OpenSSL::RSA');
+  $conf->set('encryptionpublickey',  $rsa->get_public_key_string );
+  $conf->set('encryptionprivatekey', $rsa->get_private_key_string );
+
+  # reload Record globals, false laziness with FS::Record
+  $FS::Record::conf_encryption           = $conf->exists('encryption');
+  $FS::Record::conf_encryptionmodule     = $conf->config('encryptionmodule');
+  $FS::Record::conf_encryptionpublickey  = join("\n",$conf->config('encryptionpublickey'));
+  $FS::Record::conf_encryptionprivatekey = join("\n",$conf->config('encryptionprivatekey'));
+
+}
+
 sub populate_numbering {
   eval "use FS::lata_Data;"; # this automatically populates the lata table, if unpopulated
   eval "use FS::msa_Data;"; # this automatically populates the msa table, if unpopulated
@@ -185,6 +215,14 @@ sub populate_initial_data {
 sub initial_data {
   my %opt = @_;
 
+  my $cust_location = FS::cust_location->new({
+      'address1'  => '1234 System Lane',
+      'city'      => 'Systemtown',
+      'state'     => 'CA',
+      'zip'       => '54321',
+      'country'   => 'US',
+  });
+
   #tie my %hash, 'Tie::DxHash', 
   tie my %hash, 'Tie::IxHash', 
 
@@ -327,14 +365,11 @@ sub initial_data {
         'refnum'    => 1, #XXX
         'first'     => 'System',
         'last'      => 'Accounts',
-        'address1'  => '1234 System Lane',
-        'city'      => 'Systemtown',
-        'state'     => 'CA',
-        'zip'       => '54321',
-        'country'   => 'US',
         'payby'     => 'COMP',
         'payinfo'   => 'system', #or something
         'paydate'   => '1/2037',
+        'bill_location' => $cust_location,
+        'ship_location' => $cust_location,
       },
     ],
 
@@ -371,6 +406,9 @@ sub initial_data {
     #phone types
     'phone_type' => [],
 
+    #message templates
+    'msg_template' => [],
+
   ;
 
   \%hash;
@@ -381,6 +419,7 @@ sub populate_access {
 
   use FS::AccessRight;
   use FS::access_right;
+  use FS::access_groupagent;
 
   foreach my $rightname ( FS::AccessRight->default_superuser_rights ) {
     my $access_right = new FS::access_right {