Skip to content

Commit 50b59af

Browse files
author
Marius Burkard
committed
Merge branch 'stable-3.1'
2 parents 337473b + a20ec4c commit 50b59af

File tree

2 files changed

+135
-74
lines changed

2 files changed

+135
-74
lines changed

server/plugins-available/apache2_plugin.inc.php

Lines changed: 97 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,12 @@ function update($event_name, $data) {
11161116
*/
11171117

11181118
//* Generate Let's Encrypt SSL certificat
1119-
if($data['new']['ssl'] == 'y' && $data['new']['ssl_letsencrypt'] == 'y') {
1119+
if($data['new']['ssl'] == 'y' && $data['new']['ssl_letsencrypt'] == 'y' && ( // ssl and let's encrypt is active
1120+
($data['old']['ssl'] == 'n' || $data['old']['ssl_letsencrypt'] == 'n') // we have new let's encrypt configuration
1121+
|| ($data['old']['domain'] != $data['new']['domain']) // we have domain update
1122+
|| ($data['old']['subdomain'] != $data['new']['subdomain']) // we have new or update on "auto" subdomain
1123+
|| ($data['new']['type'] == 'subdomain') // we have new or update on subdomain
1124+
)) {
11201125
if(substr($domain, 0, 2) === '*.') {
11211126
// wildcard domain not yet supported by letsencrypt!
11221127
$app->log('Wildcard domains not yet supported by letsencrypt, so changing ' . $domain . ' to ' . substr($domain, 2), LOGLEVEL_WARN);
@@ -1126,88 +1131,112 @@ function update($event_name, $data) {
11261131
$data['new']['ssl_domain'] = $domain;
11271132
$vhost_data['ssl_domain'] = $domain;
11281133

1134+
// default values
1135+
$temp_domains = array();
1136+
$lddomain = $domain;
1137+
$subdomains = null;
1138+
11291139
//* be sure to have good domain
1130-
$lddomain = (string) "$domain";
11311140
if($data['new']['subdomain'] == "www" OR $data['new']['subdomain'] == "*") {
1132-
$lddomain .= (string) " --domains www." . $domain;
1141+
$temp_domains[] = "www." . $domain;
11331142
}
11341143

1135-
$crt_tmp_file = "/etc/letsencrypt/live/".$domain."/cert.pem";
1136-
$key_tmp_file = "/etc/letsencrypt/live/".$domain."/privkey.pem";
1137-
$bundle_tmp_file = "/etc/letsencrypt/live/".$domain."/chain.pem";
1138-
$webroot = $data['new']['document_root']."/web";
1144+
//* then, add subdomain if we have
1145+
$subdomains = $app->db->queryAllRecords('SELECT domain FROM web_domain WHERE parent_domain_id = '.intval($data['new']['domain_id'])." AND active = 'y' AND type = 'subdomain'");
1146+
if(is_array($subdomains)) {
1147+
foreach($subdomains as $subdomain) {
1148+
$temp_domains[] = $subdomain['domain'];
1149+
}
1150+
}
11391151

1140-
//* check if we have already a Let's Encrypt cert
1141-
if(!file_exists($crt_tmp_file) && !file_exists($key_tmp_file)) {
1142-
$app->log("Create Let's Encrypt SSL Cert for: $domain", LOGLEVEL_DEBUG);
1152+
// prevent duplicate
1153+
$temp_domains = array_unique($temp_domains);
11431154

1144-
if(is_dir($webroot . "/.well-known/")) {
1145-
$app->log("Remove old challenge directory", LOGLEVEL_DEBUG);
1146-
$this->_exec("rm -rf " . $webroot . "/.well-known/");
1147-
}
1155+
// generate cli format
1156+
foreach($temp_domains as $temp_domain) {
1157+
$lddomain .= (string) " --domains " . $temp_domain;
1158+
}
11481159

1149-
$app->log("Create challenge directory", LOGLEVEL_DEBUG);
1150-
$app->system->mkdirpath($webroot . "/.well-known/");
1151-
$app->system->chown($webroot . "/.well-known/", $data['new']['system_user']);
1152-
$app->system->chgrp($webroot . "/.well-known/", $data['new']['system_group']);
1153-
$app->system->mkdirpath($webroot . "/.well-known/acme-challenge");
1154-
$app->system->chown($webroot . "/.well-known/acme-challenge/", $data['new']['system_user']);
1155-
$app->system->chgrp($webroot . "/.well-known/acme-challenge/", $data['new']['system_group']);
1156-
$app->system->chmod($webroot . "/.well-known/acme-challenge", "g+s");
1157-
1158-
if(file_exists("/root/.local/share/letsencrypt/bin/letsencrypt")) {
1159-
$this->_exec("/root/.local/share/letsencrypt/bin/letsencrypt auth --text --agree-tos --authenticator webroot --server https://acme-v01.api.letsencrypt.org/directory --rsa-key-size 4096 --email postmaster@$domain --domains $lddomain --webroot-path " . escapeshellarg($webroot));
1160-
}
1161-
};
1160+
// useless data
1161+
unset($subdomains);
1162+
unset($temp_domains);
11621163

1163-
//* check is been correctly created
1164-
if(file_exists($crt_tmp_file) OR file_exists($key_tmp_file)) {
1165-
$date = date("YmdHis");
1166-
if(is_file($key_file)) {
1167-
$app->system->copy($key_file, $key_file.'.old'.$date);
1168-
$app->system->chmod($key_file.'.old.'.$date, 0400);
1169-
$app->system->unlink($key_file);
1170-
}
1164+
$crt_tmp_file = "/etc/letsencrypt/live/".$domain."/cert.pem";
1165+
$key_tmp_file = "/etc/letsencrypt/live/".$domain."/privkey.pem";
1166+
$bundle_tmp_file = "/etc/letsencrypt/live/".$domain."/chain.pem";
1167+
$webroot = $data['new']['document_root']."/web";
11711168

1172-
if ($web_config["website_symlinks_rel"] == 'y') {
1173-
$this->create_relative_link(escapeshellcmd($key_tmp_file), escapeshellcmd($key_file));
1174-
} else {
1175-
exec("ln -s ".escapeshellcmd($key_tmp_file)." ".escapeshellcmd($key_file));
1176-
}
1169+
//* check if we have already a Let's Encrypt cert
1170+
if(!file_exists($crt_tmp_file) && !file_exists($key_tmp_file)) {
1171+
$app->log("Create Let's Encrypt SSL Cert for: $domain", LOGLEVEL_DEBUG);
11771172

1178-
if(is_file($crt_file)) {
1179-
$app->system->copy($crt_file, $crt_file.'.old.'.$date);
1180-
$app->system->chmod($crt_file.'.old.'.$date, 0400);
1181-
$app->system->unlink($crt_file);
1182-
}
1173+
if(is_dir($webroot . "/.well-known/")) {
1174+
$app->log("Remove old challenge directory", LOGLEVEL_DEBUG);
1175+
$this->_exec("rm -rf " . $webroot . "/.well-known/");
1176+
}
11831177

1184-
if($web_config["website_symlinks_rel"] == 'y') {
1185-
$this->create_relative_link(escapeshellcmd($crt_tmp_file), escapeshellcmd($crt_file));
1186-
} else {
1187-
exec("ln -s ".escapeshellcmd($crt_tmp_file)." ".escapeshellcmd($crt_file));
1188-
}
1178+
$app->log("Create challenge directory", LOGLEVEL_DEBUG);
1179+
$app->system->mkdirpath($webroot . "/.well-known/");
1180+
$app->system->chown($webroot . "/.well-known/", $data['new']['system_user']);
1181+
$app->system->chgrp($webroot . "/.well-known/", $data['new']['system_group']);
1182+
$app->system->mkdirpath($webroot . "/.well-known/acme-challenge");
1183+
$app->system->chown($webroot . "/.well-known/acme-challenge/", $data['new']['system_user']);
1184+
$app->system->chgrp($webroot . "/.well-known/acme-challenge/", $data['new']['system_group']);
1185+
$app->system->chmod($webroot . "/.well-known/acme-challenge", "g+s");
1186+
1187+
if(file_exists("/root/.local/share/letsencrypt/bin/letsencrypt")) {
1188+
$this->_exec("/root/.local/share/letsencrypt/bin/letsencrypt auth --text --agree-tos --authenticator webroot --server https://acme-v01.api.letsencrypt.org/directory --rsa-key-size 4096 --email postmaster@$domain --domains $lddomain --webroot-path " . escapeshellarg($webroot));
1189+
}
1190+
};
11891191

1190-
if(is_file($bundle_file)) {
1191-
$app->system->copy($bundle_file, $bundle_file.'.old.'.$date);
1192-
$app->system->chmod($bundle_file.'.old.'.$date, 0400);
1193-
$app->system->unlink($bundle_file);
1194-
}
1192+
//* check is been correctly created
1193+
if(file_exists($crt_tmp_file) OR file_exists($key_tmp_file)) {
1194+
$date = date("YmdHis");
1195+
if(is_file($key_file)) {
1196+
$app->system->copy($key_file, $key_file.'.old'.$date);
1197+
$app->system->chmod($key_file.'.old.'.$date, 0400);
1198+
$app->system->unlink($key_file);
1199+
}
11951200

1196-
if($web_config["website_symlinks_rel"] == 'y') {
1197-
$this->create_relative_link(escapeshellcmd($bundle_tmp_file), escapeshellcmd($bundle_file));
1198-
} else {
1199-
exec("ln -s ".escapeshellcmd($bundle_tmp_file)." ".escapeshellcmd($bundle_file));
1200-
}
1201+
if ($web_config["website_symlinks_rel"] == 'y') {
1202+
$this->create_relative_link(escapeshellcmd($key_tmp_file), escapeshellcmd($key_file));
1203+
} else {
1204+
exec("ln -s ".escapeshellcmd($key_tmp_file)." ".escapeshellcmd($key_file));
1205+
}
12011206

1202-
/* we don't need to store it.
1203-
/* Update the DB of the (local) Server */
1204-
$app->db->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key' WHERE domain = '".$data['new']['domain']."'");
1205-
$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
1206-
/* Update also the master-DB of the Server-Farm */
1207-
$app->dbmaster->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key' WHERE domain = '".$data['new']['domain']."'");
1208-
$app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
1207+
if(is_file($crt_file)) {
1208+
$app->system->copy($crt_file, $crt_file.'.old.'.$date);
1209+
$app->system->chmod($crt_file.'.old.'.$date, 0400);
1210+
$app->system->unlink($crt_file);
12091211
}
1210-
};
1212+
1213+
if($web_config["website_symlinks_rel"] == 'y') {
1214+
$this->create_relative_link(escapeshellcmd($crt_tmp_file), escapeshellcmd($crt_file));
1215+
} else {
1216+
exec("ln -s ".escapeshellcmd($crt_tmp_file)." ".escapeshellcmd($crt_file));
1217+
}
1218+
1219+
if(is_file($bundle_file)) {
1220+
$app->system->copy($bundle_file, $bundle_file.'.old.'.$date);
1221+
$app->system->chmod($bundle_file.'.old.'.$date, 0400);
1222+
$app->system->unlink($bundle_file);
1223+
}
1224+
1225+
if($web_config["website_symlinks_rel"] == 'y') {
1226+
$this->create_relative_link(escapeshellcmd($bundle_tmp_file), escapeshellcmd($bundle_file));
1227+
} else {
1228+
exec("ln -s ".escapeshellcmd($bundle_tmp_file)." ".escapeshellcmd($bundle_file));
1229+
}
1230+
1231+
/* we don't need to store it.
1232+
/* Update the DB of the (local) Server */
1233+
$app->db->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key' WHERE domain = '".$data['new']['domain']."'");
1234+
$app->db->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
1235+
/* Update also the master-DB of the Server-Farm */
1236+
$app->dbmaster->query("UPDATE web_domain SET ssl_request = '', ssl_cert = '$ssl_cert', ssl_key = '$ssl_key' WHERE domain = '".$data['new']['domain']."'");
1237+
$app->dbmaster->query("UPDATE web_domain SET ssl_action = '' WHERE domain = '".$data['new']['domain']."'");
1238+
}
1239+
}
12111240

12121241
if(@is_file($bundle_file)) $vhost_data['has_bundle_cert'] = 1;
12131242

server/plugins-available/nginx_plugin.inc.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,23 +1231,55 @@ function update($event_name, $data) {
12311231

12321232

12331233
$tpl->setVar('ssl_letsencrypt', "n");
1234+
12341235
//* Generate Let's Encrypt SSL certificat
1235-
if($data['new']['ssl'] == 'y' && $data['new']['ssl_letsencrypt'] == 'y') {
1236+
if($data['new']['ssl'] == 'y' && $data['new']['ssl_letsencrypt'] == 'y' && ( // ssl and let's encrypt is active
1237+
($data['old']['ssl'] == 'n' || $data['old']['ssl_letsencrypt'] == 'n') // we have new let's encrypt configuration
1238+
|| ($data['old']['domain'] != $data['new']['domain']) // we have domain update
1239+
|| ($data['old']['subdomain'] != $data['new']['subdomain']) // we have new or update on "auto" subdomain
1240+
|| ($data['new']['type'] == 'subdomain') // we have new or update on subdomain
1241+
)) {
1242+
12361243
//* be sure to have good domain
12371244
if(substr($domain, 0, 2) === '*.') {
12381245
// wildcard domain not yet supported by letsencrypt!
12391246
$app->log('Wildcard domains not yet supported by letsencrypt, so changing ' . $domain . ' to ' . substr($domain, 2), LOGLEVEL_WARN);
12401247
$domain = substr($domain, 2);
12411248
}
1242-
1249+
12431250
$data['new']['ssl_domain'] = $domain;
12441251
$vhost_data['ssl_domain'] = $domain;
12451252

1246-
$lddomain = (string) "$domain";
1247-
if($data['new']['subdomain'] == "www" OR $data['new']['subdomain'] == "*") {
1248-
$lddomain .= (string) " --domains www." . $domain;
1253+
// default values
1254+
$temp_domains = array();
1255+
$lddomain = $domain;
1256+
$subdomains = null;
1257+
1258+
//* be sure to have good domain
1259+
if($data['new']['subdomain'] == "www" OR $data['new']['subdomain'] == "*") {
1260+
$temp_domains[] = "www." . $domain;
12491261
}
12501262

1263+
//* then, add subdomain if we have
1264+
$subdomains = $app->db->queryAllRecords('SELECT domain FROM web_domain WHERE parent_domain_id = '.intval($data['new']['domain_id'])." AND active = 'y' AND type = 'subdomain'");
1265+
if(is_array($subdomains)) {
1266+
foreach($subdomains as $subdomain) {
1267+
$temp_domains[] = $subdomain['domain'];
1268+
}
1269+
}
1270+
1271+
// prevent duplicate
1272+
$temp_domains = array_unique($temp_domains);
1273+
1274+
// generate cli format
1275+
foreach($temp_domains as $temp_domain) {
1276+
$lddomain .= (string) " --domains " . $temp_domain;
1277+
}
1278+
1279+
// useless data
1280+
unset($subdomains);
1281+
unset($temp_domains);
1282+
12511283
$tpl->setVar('ssl_letsencrypt', "y");
12521284
//* TODO: check dns entry is correct
12531285
$crt_tmp_file = "/etc/letsencrypt/live/".$domain."/fullchain.pem";
@@ -1265,7 +1297,7 @@ function update($event_name, $data) {
12651297

12661298
$app->log("Create challenge directory", LOGLEVEL_DEBUG);
12671299
$app->system->mkdirpath($webroot . "/.well-known/");
1268-
$app->system->chown($webroot . "/.well-known/", $$data['new']['system_user']);
1300+
$app->system->chown($webroot . "/.well-known/", $data['new']['system_user']);
12691301
$app->system->chgrp($webroot . "/.well-known/", $data['new']['system_group']);
12701302
$app->system->mkdirpath($webroot . "/.well-known/acme-challenge");
12711303
$app->system->chown($webroot . "/.well-known/acme-challenge/", $data['new']['system_user']);

0 commit comments

Comments
 (0)