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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
<?php
class Acme
{
const PROP_ERROR = 'acme.error-string';
const PROP_PROVIDER = 'acme.provider';
const PROP_KEY_ID = 'acme.key-id';
const PROP_HMAC_KEY = 'acme.hmac-key';
const PROP_DOMAINS = 'acme.domains';
const PROP_MAIL = 'acme.mail';
const VALID_PROVIDERS = [
'letsencrypt' => "Let's Encrypt",
'zerossl' => 'ZeroSSL.com',
'buypass' => 'BuyPass.com',
'geant/sectigo' => 'GEANT via Sectigo',
];
const PROVIDER_ALIASES = [
'geant/sectigo' => 'https://acme.sectigo.com/v2/GEANTOV',
];
public static function getLastError(): ?string
{
return Property::get(self::PROP_ERROR, null);
}
public static function getProvider(): ?string
{
return Property::get(self::PROP_PROVIDER, null);
}
public static function getKeyId(): ?string
{
return Property::get(self::PROP_KEY_ID, null);
}
public static function getHmacKey(): ?string
{
return Property::get(self::PROP_HMAC_KEY, null);
}
/**
* @return string[] list of [id] => friendly name
*/
public static function getProviders(): array
{
return self::VALID_PROVIDERS;
}
public static function getMail(): ?string
{
return Property::get(self::PROP_MAIL, null);
}
public static function getDomains(): array
{
return explode(' ', Property::get(self::PROP_DOMAINS));
}
public static function setConfig(string $provider, string $mail, ?string $keyId = null, ?string $hmacKey = null): bool
{
if (!isset(self::VALID_PROVIDERS[$provider])) {
Message::addError('webinterface.acme-invalid-provider', $provider);
return false;
}
Property::set(self::PROP_PROVIDER, $provider);
Property::set(self::PROP_MAIL, $mail);
Property::set(self::PROP_KEY_ID, $keyId);
Property::set(self::PROP_HMAC_KEY, $hmacKey);
return true;
}
public static function setDomains(array $list): void
{
Property::set(self::PROP_DOMAINS, implode(' ', $list));
}
private static function handleErrorAsync($task): void
{
if (!is_array($task) || !Taskmanager::isTask($task))
return;
$task = Taskmanager::waitComplete($task, 250);
$args = ['user' => User::getLogin()];
if (Taskmanager::isFinished($task)) {
self::callbackErrorCheck($task, $args);
} else {
Property::set(self::PROP_ERROR, false);
TaskmanagerCallback::addCallback($task, 'acmeErrors', $args);
}
}
public static function callbackErrorCheck(array $task, $args): void
{
if (!Taskmanager::isFinished($task))
return;
$otherError = false;
if (isset($task['data']['error'])) {
// This should never happen, so make it an error
if (strpos($task['data']['error'], " is not an issued domain, skipping.") !== false) {
$otherError = true;
}
}
if (Taskmanager::isFailed($task) || $otherError) {
if (($args['user'] ?? null) === null) {
EventLog::warning('Automatic ACME renewal of HTTPS certificate failed', print_r($task, true));
}
Property::set(self::PROP_ERROR, $task['data']['error'] ?? 'Unknown error');
} else {
// If the cronjob called us and there is nothing to do, suppress the event log entry
if (($args['user'] ?? null) !== null
|| strpos($task['data']['error'] ?? '', 'Skipping. Next renewal time is: ') === false) {
EventLog::info('ACME issue/renewal of HTTPS certificate by ' . ($args['user'] ?? 'automatic cronjob'), print_r($task, true));
}
Property::set(self::PROP_ERROR, false);
}
}
public static function issueNewCertificate(bool $wipeAll = false): ?string
{
$provider = self::getProvider();
if ($provider === null) {
Message::addError('webinterface.acme-no-provider');
return null;
}
$mail = self::getMail();
if (empty($mail)) {
Message::addError('webinterface.acme-no-mail');
return null;
}
$domains = self::getDomains();
if (empty($domains)) {
Message::addError('webinterface.acme-no-domains');
return null;
}
$redirect = Property::get(WebInterface::PROP_REDIRECT);
$task = Taskmanager::submit('LighttpdHttps', [
'redirect' => $redirect,
'acmeMode' => 'issue',
'acmeMail' => $mail,
'acmeDomains' => $domains,
'acmeProvider' => self::PROVIDER_ALIASES[$provider] ?? $provider,
'acmeKeyId' => self::getKeyId(),
'acmeHmacKey' => self::getHmacKey(),
'acmeWipeAll' => $wipeAll,
]);
self::handleErrorAsync($task);
return $task['id'] ?? null;
}
public static function renew(): ?string
{
error_log("Renew called");
$domains = self::getDomains();
if (empty($domains)) {
Message::addError('webinterface.acme-no-domains');
return null;
}
$redirect = Property::get(WebInterface::PROP_REDIRECT);
$task = Taskmanager::submit('LighttpdHttps', [
'redirect' => $redirect,
'acmeMode' => 'renew',
'acmeDomains' => $domains,
]);
self::handleErrorAsync($task);
return $task['id'] ?? null;
}
public static function tryEnable(): bool
{
$redirect = Property::get(WebInterface::PROP_REDIRECT);
$task = Taskmanager::submit('LighttpdHttps', [
'redirect' => $redirect,
'acmeMode' => 'try-enable',
]);
$task = Taskmanager::waitComplete($task, 10000);
return Taskmanager::isFinished($task) && !Taskmanager::isFailed($task);
}
}
|