SM2Gen/Targets/Letsencrypt/Letsencrypt-Custom.pm

472 lines
16 KiB
Perl
Raw Normal View History

#
# Generated by SM2Gen version:0.8 Chameleon version:4.5.4 On Python:3.12.3 at 2024-11-16 10:30:16
#
#
# Routines to be edited by the developer to provide content and validation for parameters
# and provison of the control data for table(s)
#
use esmith::util;
use esmith::util::network;
use esmith::ConfigDB;
use esmith::HostsDB;
use esmith::AccountsDB;
use esmith::NetworksDB;
use esmith::DomainsDB;
use constant FALSE => 0;
use constant TRUE => 1;
#The most common ones
our $cdb = esmith::ConfigDB->open() || die("Couldn't open config db");
our $adb = esmith::AccountsDB->open() || die("Couldn't open Accounts db");
our $ndb = esmith::NetworksDB->open() || die("Couldn't open Network db");
our $hdb = esmith::HostsDB->open() || die("Couldn't open Hosts db");
our $ddb = esmith::DomainsDB->open() || die("Couldn't open Domains db");
# Validation routines - parameters for each panel
sub validate_LIST {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('InternalIP')
{$ret .= 'Validation for InternalIP failed';}
if (! TRUE) #validate $c->param('ExternalIP')
{$ret .= 'Validation for ExternalIP failed';}
if (! TRUE) #validate $c->param('InternetIP')
{$ret .= 'Validation for InternetIP failed';}
if (! TRUE) #validate $c->param('Issuer')
{$ret .= 'Validation for Issuer failed';}
if (! TRUE) #validate $c->param('Expiry')
{$ret .= 'Validation for Expiry failed';}
if (! TRUE) #validate $c->param('NotBefore')
{$ret .= 'Validation for NotBefore failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub validate_PARAMS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('status')
{$ret .= 'Validation for status failed';}
if (! TRUE) #validate $c->param('hookScript')
{$ret .= 'Validation for hookScript failed';}
if (! TRUE) #validate $c->param('hostOverride')
{$ret .= 'Validation for hostOverride failed';}
if (! TRUE) #validate $c->param('ACCEPT_TERMS')
{$ret .= 'Validation for ACCEPT_TERMS failed';}
if (! TRUE) #validate $c->param('API')
{$ret .= 'Validation for API failed';}
if (! TRUE) #validate $c->param('keysize')
{$ret .= 'Validation for keysize failed';}
if (! TRUE) #validate $c->param('configure')
{$ret .= 'Validation for configure failed';}
if (! TRUE) #validate $c->param('Email')
{$ret .= 'Validation for Email failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub validate_CHECKALLDOMAINS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('AllDomainsCheck')
{$ret .= 'Validation for AllDomainsCheck failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub validate_CHECKALLENABLEDDOMAINS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('EnabledDomainsCheck')
{$ret .= 'Validation for EnabledDomainsCheck failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub validate_CHECKONEDOMAIN {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
# Validation for each field
my $ret = "";
if (! TRUE) #validate $c->param('OneDomainToCheck')
{$ret .= 'Validation for OneDomainToCheck failed';}
if (! TRUE) #validate $c->param('OneDomainsCheck')
{$ret .= 'Validation for OneDomainsCheck failed';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
# Get singleton data for each panel
sub get_data_for_panel_LIST {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
# my ($reply, $err, $server_cert) = Net::SSLeay::sslcat('localhost', 443, '/');
# my $issuer = Net::SSLeay::X509_NAME_oneline(Net::SSLeay::X509_get_issuer_name($server_cert));
# my $before = Net::SSLeay::P_ASN1_TIME_get_isotime(Net::SSLeay::X509_get_notBefore($server_cert));
# my $expiry = Net::SSLeay::P_ASN1_TIME_get_isotime(Net::SSLeay::X509_get_notAfter($server_cert));
my %ret = (
'Data1'=>'Data for LIST', #Example
# fields from Inputs in LIST $fields['LIST']
'InternalIP'=>$cdb->get_prop('InternalInterface','IPAddress'),
'ExternalIP'=>$cdb->get_prop('ExternalInterface','IPAddress'),
'InternetIP'=>$c->get_my_ip(),
'Issuer'=>'$issuer',
'Expiry'=>'$expiry',
'NotBefore'=>'$before',
);
return %ret;
}
sub get_data_for_panel_PARAMS {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
my %ret = (
'Data1'=>'Data for PARAMS', #Example
# fields from Inputs in PARAMS $fields['PARAMS']
'status'=>$cdb->get_prop('letsencrypt', 'status', 'disabled'),
'hookScript'=>$cdb->get_prop('letsencrypt', 'hookScript', 'disabled'),
'hostOverride'=>$cdb->get_prop('letsencrypt', 'hostOverride', 'disabled'),
'ACCEPT_TERMS'=>$cdb->get_prop('letsencrypt', 'ACCEPT_TERMS', ''),
'API'=>$cdb->get_prop('letsencrypt', 'API', '2'),
'keysize'=>$cdb->get_prop('letsencrypt', 'keysize', '4096'),
'configure'=>$cdb->get_prop('letsencrypt', 'configure', 'none' ),
'email'=>$cdb->get_prop('letsencrypt', 'email')
);
return %ret;
}
sub get_data_for_panel_CHECKALLDOMAINS {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
my %ret = (
'Data1'=>'Data for CHECKALLDOMAINS', #Example
# fields from Inputs in CHECKALLDOMAINS $fields['CHECKALLDOMAINS']
'AllDomainsCheck'=>$c->update_all_domains(),
);
return %ret;
}
sub get_data_for_panel_CHECKALLENABLEDDOMAINS {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
my %ret = (
'Data1'=>'Data for CHECKALLENABLEDDOMAINS', #Example
# fields from Inputs in CHECKALLENABLEDDOMAINS $fields['CHECKALLENABLEDDOMAINS']
'EnabledDomainsCheck'=>$c->update_enabled_domains(),
);
return %ret;
}
sub get_data_for_panel_CHECKONEDOMAIN {
# Return a hash with the fields required which will be loaded into the shared data
my $c = shift;
my %ret = (
'Data1'=>'Data for CHECKONEDOMAIN', #Example
# fields from Inputs in CHECKONEDOMAIN $fields['CHECKONEDOMAIN']
'OneDomainToCheck'=>$c->param("CHECKONEDOMAIN"),
'OneDomainsCheck'=>$c->update_one_domain($c->param("CHECKONEDOMAIN"))
);
return %ret;
}
# Get control data for table(s)
# Define a constant hash for field name mapping
use constant DomainList_FIELD_MAPPING => (
'Table1-Domain name / HOSTNAME' => 'Domain',
'Table1-Brief description' => 'Description',
'Table1-Content' => 'Content',
'Table1-LABEL_NAMESERVERS' => 'Nameservers',
'Table1-LABEL_POINT' => 'Source-for-Table1-LABEL_POINT',
'Table1-LABEL_LECERT' => 'letsencryptSSLcert',
'Table1-IS_IN_CERT' => 'isincert',
'Table1-CHECK' => 'Check'
#'target_field2' => 'source_field2',
# Add more mappings as needed
);
use constant TEST_DOMAIN_LIST => (
{ "domain" => "Domain1","fred" => "fred1" , "description"=>"Description1"},
{ "domain" => "Domain2", "fred" => "fred2", "description"=>"Description2" },
{ "domain" => "Domain3", "fred" => "fred3", "description"=>"Description3" },
# Add more test entries as needed
);
sub actual_DomainList {
my $c = shift;
# Actual code for extracting DomainList
my @list = ();
# my @rv = Net::SSLeay::X509_get_subjectAltNames($server_cert);
# foreach my $element (@rv) {
# next if $element =~ /^\d+$/; ;
# #print $element . "\n";
# push @list, $element;
# }
my @data = ();
my $check = $c->l('Check Domain');
for ($ddb->domains)
{
my $ns = $_->prop('Nameservers') || 'internet';
my $le = $_->prop('letsencryptSSLcert') || 'disabled';#letsencrypt configure all
my $dname= $_->key;
my $isincert = "N";
my $link = $c->create_link("letsencryptd","CHECKONEDOMAIN","");
my $checklink = "<a href='".$link."&CHECKONEDOMAIN=".$_->key."'>".$check."</a>";
#my $checklink = "<a href=''>check</a>";
$isincert = "Y" if ( $dname ~~ @list);
# domain
push @data,
{ Domain => $_->key,
$_->props,
letsencryptSSLcert => $le,
isincert => $isincert,
Check => $checklink,
Nameservers => $ns,
};
#and hosts
for my $h ($hdb->get_hosts_by_domain($dname))
{
next if $ddb->get($h->key);
next unless ($h->prop('HostType') eq "Self" || $h->prop('HostType') eq "Local");
$le = $h->prop('letsencryptSSLcert') || 'disabled';#letsencrypt configure all
$isincert = "N";
$isincert = "Y" if ( $h->key ~~ @list);
push @data,
{ Domain => "--> ". $h->key,
$h->props,
Description=>$h->prop('ExternalIP')|| $h->prop('InternalIP')||"",
Content => $h->prop('HostType'),
isincert => $isincert,
Check => "", #$checklink
Nameservers => $c->l($ns),
}
}
}
return @data;
}
sub get_DomainList {
# Return an array of hashes of the contents for each row and column for DomainList
my $c = shift;
my @source_records = $c->actual_DomainList(); #TEST_DOMAIN_LIST #Replace by code or call to produce contents of table;
my @transformed_records;
my %Field_Mapping = DomainList_FIELD_MAPPING;
# Iterate over each record in the source array
for my $source_record (@source_records) {
my %transformed_record;
# Iterate over each key-value pair in the $Field_Mapping constant
while (my ($target, $source) = each %Field_Mapping) {
# Check if the source field exists in the source record
if (exists $source_record->{$source}) {
# Assign the source field value to the target field in the transformed record
$transformed_record{$target} = $source_record->{$source};
}
}
# Add transformed record to the array if it's not empty
push @transformed_records, \%transformed_record if %transformed_record;
}
return \@transformed_records;
}
# Return hash with values from row in which link clicked on table
sub get_selected_LIST {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
sub get_selected_PARAMS {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
sub get_selected_CHECKALLDOMAINS {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
sub get_selected_CHECKALLENABLEDDOMAINS {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
sub get_selected_CHECKONEDOMAIN {
my $c = shift;
my $selected = shift; #Parameter is name of selected row.
my $is_new_record = shift; #Indicates new record required (defaults)
my %ret = {};
return $ret;
}
#after sucessful modify or create or whatever and submit then perfom (if the params validate)
sub perform_LIST {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'ChangeThis';
if (! TRUE) #copy or perform with value: InternalIP e.g. $db->set_prop($dbkey,'InternalIP',$c->param('InternalIP'),type=>'service'))
{$ret .= 'Perform/save failed for InternalIP';}
if (! TRUE) #copy or perform with value: ExternalIP e.g. $db->set_prop($dbkey,'ExternalIP',$c->param('ExternalIP'),type=>'service'))
{$ret .= 'Perform/save failed for ExternalIP';}
if (! TRUE) #copy or perform with value: InternetIP e.g. $db->set_prop($dbkey,'InternetIP',$c->param('InternetIP'),type=>'service'))
{$ret .= 'Perform/save failed for InternetIP';}
if (! TRUE) #copy or perform with value: Issuer e.g. $db->set_prop($dbkey,'Issuer',$c->param('Issuer'),type=>'service'))
{$ret .= 'Perform/save failed for Issuer';}
if (! TRUE) #copy or perform with value: Expiry e.g. $db->set_prop($dbkey,'Expiry',$c->param('Expiry'),type=>'service'))
{$ret .= 'Perform/save failed for Expiry';}
if (! TRUE) #copy or perform with value: NotBefore e.g. $db->set_prop($dbkey,'NotBefore',$c->param('NotBefore'),type=>'service'))
{$ret .= 'Perform/save failed for NotBefore';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub perform_PARAMS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'letsencrypt';
# To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g."
if (! $db->set_prop($dbkey,'status',$c->param('status'),type=>'service'))
{$ret .= 'Perform/save failed for status';}
if (! $db->set_prop($dbkey,'hookScript',$c->param('hookScript'),type=>'service'))
{$ret .= 'Perform/save failed for hookScript';}
if (! $db->set_prop($dbkey,'hostOverride',$c->param('hostOverride'),type=>'service'))
{$ret .= 'Perform/save failed for hostOverride';}
if (! $db->set_prop($dbkey,'ACCEPT_TERMS',$c->param('ACCEPT_TERMS'),type=>'service'))
{$ret .= 'Perform/save failed for ACCEPT_TERMS';}
if (! $db->set_prop($dbkey,'API',$c->param('API'),type=>'service'))
{$ret .= 'Perform/save failed for API';}
if (! $db->set_prop($dbkey,'keysize',$c->param('keysize'),type=>'service'))
{$ret .= 'Perform/save failed for keysize';}
if (! $db->set_prop($dbkey,'configure',$c->param('configure'),type=>'service'))
{$ret .= 'Perform/save failed for configure';}
if (! $db->set_prop($dbkey,'email',$c->param('email'),type=>'service'))
{$ret .= 'Perform/save failed for email';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub perform_CHECKALLDOMAINS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'ChangeThis';
if (! TRUE) #copy or perform with value: AllDomainsCheck e.g. $db->set_prop($dbkey,'AllDomainsCheck',$c->param('AllDomainsCheck'),type=>'service'))
{$ret .= 'Perform/save failed for AllDomainsCheck';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub perform_CHECKALLENABLEDDOMAINS {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'ChangeThis';
if (! TRUE) #copy or perform with value: EnabledDomainsCheck e.g. $db->set_prop($dbkey,'EnabledDomainsCheck',$c->param('EnabledDomainsCheck'),type=>'service'))
{$ret .= 'Perform/save failed for EnabledDomainsCheck';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub perform_CHECKONEDOMAIN {
my $c = shift;
my $prefix_data = shift; #Data hash as parameter
my $ret = "";
my $db = $cdb; #maybe one of the others
my $dbkey = 'ChangeThis';
if (! TRUE) #copy or perform with value: OneDomainToCheck e.g. $db->set_prop($dbkey,'OneDomainToCheck',$c->param('OneDomainToCheck'),type=>'service'))
{$ret .= 'Perform/save failed for OneDomainToCheck';}
if (! TRUE) #copy or perform with value: OneDomainsCheck e.g. $db->set_prop($dbkey,'OneDomainsCheck',$c->param('OneDomainsCheck'),type=>'service'))
{$ret .= 'Perform/save failed for OneDomainsCheck';}
if ($ret eq "") {$ret = 'ok';}
return $ret;
}
sub create_link{
# WIP
my ($c,$route, $panel, $index) = @_;
my $link = "$route?trt=$panel&Selected=$index";
return $link;
}
sub get_my_ip
{
my ($self, $item, $prop, $default) = @_;
my $output = `/usr/sbin/e-smith/getmyip`;
return $output || "IP";
}
sub update_one_domain
{
my ($self,$domain) = @_;
return "$domain not domain" unless ($ddb->get($domain) || $hdb->get($domain));
($domain) = ($domain =~ /([\w\p{L}.]+)/);
my $output = `/etc/e-smith/events/actions/letsencrypt-setdomains " " $domain `;
return $output || "-empty-";
}
sub update_all_domains
{
my $self = shift;
my $output = `/etc/e-smith/events/actions/letsencrypt-setdomains "" "" all `;
return $output || "-empty-";
}
sub update_enabled_domains
{
my $self = shift;
my $output = `/etc/e-smith/events/actions/letsencrypt-setdomains "" "" enabled `;
return $output || "-empty-";
}
1;