-
Notifications
You must be signed in to change notification settings - Fork 0
/
acmesh_ispconfig.php
159 lines (136 loc) · 6.02 KB
/
acmesh_ispconfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env php
<?php
/* *
* ISPConfig ACME.SH Integration Script
*
* Description: Allows easy integration of ACME.SH into ISPConfig and to deploy Wildcard Certificates to it.
*
* @author Aperture Development <[email protected]>
* @version 0.0.2
* @license by-sa 4.0
*/
/**
* Prepare Variables
*/
# ISPConfig URI
$ispconfigUri = 'https://localhost:8080/remote/';
# ISPConfig API username
$username = 'apiuser';
# ISPConfig API password
$password = 'apipassword';
# The command on your server to reload a service (%service% gets replaced with the service to be restarted)
$reloadCmd = 'systemctl reload %service%';
# Location of the acme.sh certificates (the path defined with --cert-home at the acme.sh installation, by default its the same path as acme.sh)
$acmeshLocation = '/etc/acmesh/live';
/**
* #######################################################################
* DO NOT EDIT STUFF BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING!!!
* #######################################################################
*/
$scriptPath = dirname(__FILE__);
require_once($scriptPath . '/lib/ispconfig_soap.php');
$arguments = getopt('d:m::f::s::u::p::l::r::h', array('domain:', 'service::', 'username::', 'password::', 'uri::', 'reloadcmd::', 'help'));
/**
* Check if the help paramater has been used
*/
if(isset($arguments['h']) || isset($arguments['help']) || count($arguments) === 0) {
echo file_get_contents($scriptPath . '/help.txt');
exit(0);
}
/**
* Check if paramaters have been supplied and overwrite default ones
*/
if(isset($arguments['u']) || isset($arguments['username'])) {
$username = isset($arguments['u']) ? $arguments['u'] : $arguments['username'];
}
if(isset($arguments['p']) || isset($arguments['password'])) {
$password = isset($arguments['p']) ? $arguments['p'] : $arguments['password'];
}
if(isset($arguments['l']) || isset($arguments['uri'])) {
$ispconfigUri = isset($arguments['l']) ? $arguments['l'] : $arguments['uri'];
}
if(isset($arguments['r']) || isset($arguments['reloadcmd'])) {
$reloadCmd = isset($arguments['r']) ? $arguments['r'] : $arguments['reloadcmd'];
}
/*
Finally start the script execution
*/
try {
/**
* Check if domain paramater has been provided and abord execution if not
*/
if(!isset($arguments['d']) && !isset($arguments['domain'])) {
throw new Exception('Missing required argument: -d / --domain');
} else {
$tempDomainsShort = isset($arguments['d']) ? $arguments['d'] : array();
$tempDomainsLong = isset($arguments['domain']) ? $arguments['domain'] : array();
if(gettype($tempDomainsShort) === 'array' && gettype($tempDomainsLong) === 'array') {
$domains = array_merge($tempDomainsShort, $tempDomainsLong);
} else {
$domains[] = $tempDomainsShort;
}
}
// check if all these parameters have been provided once and not multible times!
if(gettype($username) === 'array' || gettype($password) === 'array' || gettype($ispconfigUri) === 'array'){
throw new Exception('Parameters -u, --username, -p, --password, -l and --uri can only be provided ONCE! Aborting');
}
/**
* Initialize the connection to ISPConfig Once
*/
$ispconfigSoap = new ISPConfigSoap($ispconfigUri, $username, $password);
foreach($domains as $domain) {
/**
* Load SSL Informations into variables and check if an update is required
*/
$cert = file_get_contents($acmeshLocation . '/' . $domain . '/' . $domain . '.cer');
$privkey = file_get_contents($acmeshLocation . '/' . $domain . '/' . $domain . '.key');
$bundle = file_get_contents($acmeshLocation . '/' . $domain . '/fullchain.cer');
echo 'Loading certificate data for: ' . $domain . PHP_EOL;
// Check if certificate has been loaded successfully
if(!isset($cert) || !isset($privkey) || !isset($bundle)){
throw new Exception('No SSL Certificate could be found for \'' . $domain . '\' please make sure the certificates are inside the /etc/acmesh/%domain% folder.');
}
// Read certificate and load all applicable domains
$sslCert = openssl_x509_parse($cert);
preg_match_all('/DNS:([*a-zA-Z0-9\.-]+)/', $sslCert['extensions']['subjectAltName'], $certDomains);
echo 'Found certificate valid for ' . $sslCert['extensions']['subjectAltName'] . PHP_EOL;
/**
* Load all domains this change applies to ()
*/
// Lookup all domains inside ispconfig this domain change would apply to
$changedDomains = array();
foreach($certDomains[1] as $certDomain) {
$changedDomains = array_merge($changedDomains, $ispconfigSoap->loadUpdateableDomains(str_replace('*', '%', $certDomain), $cert));
}
/**
* Update all domains this change applies to with new SSL Data
*/
foreach($changedDomains as $key => $value) {
$ispconfigSoap->updateDomainData($value['domain_id'], $value['client_id'], array(
'ssl_cert' => $cert,
'ssl_bundle' => $bundle,
'ssl_key' => $privkey
));
echo 'Updated \'' . $key . '\' with new certificate' . PHP_EOL;
}
}
// If enabled, also update SSL certificate in Database
if(isset($arguments['s']) || isset($arguments['service'])){
$database = isset($arguments['s']) ? $arguments['s'] : $arguments['service'];
if(gettype($database) === 'array') {
foreach($database as $service) {
shell_exec(str_replace('%service%', $service, $reloadCmd));
}
} else {
shell_exec(str_replace('%service%', $database, $reloadCmd));
}
}
exit(0);
} catch(Exception $e) {
mail('root@localhost', 'Acme.SH-ISPConfig integration error', 'An error occured while attempting to update SSL certificates: \n' . $e->getMessage());
echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
exit(1);
} finally {
$ispconfigSoap->logoutSoapClient();
}
?>