Skip to content

Commit

Permalink
[fix] Cron should be verbose and generate_certbot should check dig +t…
Browse files Browse the repository at this point in the history
…race
  • Loading branch information
albancrommer committed Jun 22, 2018
1 parent 5cf1c0b commit f1d8af1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
47 changes: 42 additions & 5 deletions src/usr/lib/alternc/generate_certbot.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
#!/usr/bin/php
<?php
/**
* Retrieves or renews certs
*
* Called as a cron or as an interactive script during alternc.install
*
* params : -v | --verbose display user information
*
*/

// Ne v rifie pas ma session :)
// Handle the verbose flag
$verbose = ( $argc > 1 && in_array( $argv[1], array( "-v", "--verbose") ) ) ? True : False;
function vprint( $message, $params ){
global $verbose;
if( $verbose ) {
echo vsprintf( "$message", $params );
}
}

// Ne verifie pas ma session :)
chdir("/usr/share/alternc/panel/");
require("/usr/share/alternc/panel/class/config_nochk.php");

Expand All @@ -11,20 +28,40 @@
// Get all alternc accounts
$accounts = $admin->get_list(1, 0, false, 'domaine');

// Retrieve all domains from user accounts
$domainsList = array();
foreach ($accounts as $cuid => $infos) {
$mem->su($cuid);

//Get all domain set to each user
$domains = $dom->enum_domains();
foreach ($domains as $domain) {
$dom->lock();
$domain_data=$dom->get_domain_all($domain);
$domain_data = $dom->get_domain_all($domain);
// Get all hosts (subdomain)
$sub_domains=$domain_data['sub'];
$sub_domains = $domain_data['sub'];
foreach ($sub_domains as $sub_domain) {
$certbot->import($sub_domain['fqdn']);
$domainsList[] = $sub_domain['fqdn'];
}
$dom->unlock();
}
$mem->unsu();
}
// No need to request anything: exit
if( ! count( $domainsList ) ){
return;
}

vprint( _("Requiring Certbot renewal for %s domains\n"), count( $domainsList ));

foreach ($domainsList as $key => $sub_domain) {
$spacer=" ";
vprint( _("\r$spacer\rRequesting domain %d/%d: %s"), array( $key + 1, count( $domainsList),$sub_domain ));
if( ! $certbot->isLocalAlterncDomain( $sub_domain ) ){
continue;
}
vprint( _(" hosted locally, running certbot..."), array( ));

$certbot->import($sub_domain);
}
vprint( _("\nFinished Certbot renewal\n"), count( $domainsList ));

2 changes: 1 addition & 1 deletion src/usr/lib/alternc/install.d/alternc-certbot
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ if [ "$1" == "apache2" ]; then
mysql --defaults-file=/etc/alternc/my.cnf -e "UPDATE quotas SET total=1,name='ssl' WHERE name = 'ssl';"

##Generate let's encrypt certificate
/usr/lib/alternc/generate_certbot.php
/usr/lib/alternc/generate_certbot.php --verbose
fi
36 changes: 27 additions & 9 deletions src/usr/share/alternc/panel/class/m_certbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public function import($fqdn)
$ssl_vhosts = array();
foreach ($ssl_list as $ssl_item) {
$ssl_vhosts[$ssl_item['fqdn']] = array(
'certid' => $ssl_item['id'],
'sslkey' => $ssl_item['sslkey']
) ;
'certid' => $ssl_item['id'],
'sslkey' => $ssl_item['sslkey']
) ;
}

$output = "";
Expand All @@ -72,17 +72,35 @@ public function import($fqdn)
$chain = file_get_contents('/etc/letsencrypt/live/'.$fqdn.'/chain.pem');

if (
!isset($ssl_vhosts[$fqdn]) ||
(
isset($ssl_vhosts[$fqdn]) &&
$ssl_vhosts[$fqdn]['sslkey'] != $key
)
) {
!isset($ssl_vhosts[$fqdn]) ||
(
isset($ssl_vhosts[$fqdn]) &&
$ssl_vhosts[$fqdn]['sslkey'] != $key
)
) {
return $ssl->import_cert($key, $crt, $chain);
}
}
return false;
}

/**
* Checks if dig returns our L_PUBLIC_IP
*/
function isLocalAlterncDomain( $fqdn ){
global $L_PUBLIC_IP;
$out=array();
exec("dig A +trace ".escapeshellarg($fqdn),$out);
$found=false;
foreach($out as $line) {
if (preg_match('#.*IN.A.*?([0-9\.]*)$#',$line,$mat) && $mat[1] == $L_PUBLIC_IP) {
$found = true;
break;
}
}
return $found;
}

}

/* Class m_certbot */

8 comments on commit f1d8af1

@ddeenniiss
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testé, ça ne bloque plus.

@kent1D
Copy link
Contributor

@kent1D kent1D commented on f1d8af1 Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm dig +trace ne me renvoit rien alors que dig sans +trace est ok

@kent1D
Copy link
Contributor

@kent1D kent1D commented on f1d8af1 Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testé sur Mac et sur le serveur, ce +trace est il vraiment nécessaire?

@albancrommer
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kent1D Le +trace s'assure qu'on bypass les serveurs récursifs / cache DNS pour obtenir une réponse DNS temps réel.

Dans ton cas, si +trace ne renvoie rien c'est que ton NS ne répond pas : tu as un vrai souci, potentiellement.

@camlafit
Copy link
Collaborator

@camlafit camlafit commented on f1d8af1 Oct 27, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kent1D
Copy link
Contributor

@kent1D kent1D commented on f1d8af1 Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lo quelle rapidité dans la réponse

Quels serveurs publics recommandez-vous hors google?

@camlafit
Copy link
Collaborator

@camlafit camlafit commented on f1d8af1 Oct 27, 2018 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kent1D
Copy link
Contributor

@kent1D kent1D commented on f1d8af1 Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok j'ai mis ceux de quad9, mais du coup ça ne matche pas les CNAME qui pointeraient sur la machine

Please sign in to comment.