-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify.php
More file actions
79 lines (66 loc) · 2.27 KB
/
Copy pathnotify.php
File metadata and controls
79 lines (66 loc) · 2.27 KB
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
<?php
/* This script (when run under some periodic process such as a task or a cronjob)
will check the certificate expiration date and notify the requester
via email in advance
For it to work under php-fpm, which runs under nobody account, add it to group mail and restart the service
*/
require_once 'config.php';
require_once 'helper_functions.php';
require_once 'sql.php';
$clientCertVerify = $_SERVER['CLIENT_CERT_VERIFY'];
switch ($clientCertVerify) {
case 'SUCCESS':
break;
case 'NONE':
response(401, 'Unauthorized: please use your client SSL certificate for authentication');
break;
default: //FAILED:reason
response(401, 'Unauthorized: SSL authentication with your client certificate failed. Reason: ' . explode(':', $clientCertVerify)[1]);
}
$dn = $_SERVER['SUBJECT_DN'];
$attrs = explode(',', $dn);
foreach ($attrs as $attr) {
list($type, $value) = explode('=', $attr);
switch(strtolower($type)) {
case 'role':
$role = $value;
break;
case 'cn':
$username = $value;
break;
}
}
if (!empty($_REQUEST["email"]))
$to = sanitize($_REQUEST["email"]);
else
response(400, 'Usage: ' . $base_url . '/notify.php?email=<email>&expire=<days>');
if (!empty($_REQUEST["expire"]))
$expire = sanitize($_REQUEST["expire"]);
else
$expire = $cert_expire_notify_days;
$certs = sqlGetCertsToExpire($username, $expire);
$certificates = '';
if ($certs) {
foreach($certs as $cert) {
$willExpire = DateTime::createFromFormat("U", $cert['notAfter']);
$certificates .= 'sn ' . $cert['serial'] . ', subject ' . $cert['subject'] . ', expire ' . $willExpire->format("YmdHis") . "Z\r\n";
}
}
if ($certificates != '') {
$subject = "Certificate expiration notification";
$message = <<<EOF
Hello $ou,
please be aware that the following certificates
will expire in less than $expire days:
$certificates
EOF;
$headers = array("From" => "$email", "Reply-To" => "$email", "X-Mailer" => "PHP/" . phpversion());
if (! mail($to, $subject, $message, $headers)) {
$err = error_get_last();
if (!empty($err))
response(500, 'mail failed ' . $err['message']);
else response(500, 'mail failed');
}
else response(200, 'Email has been sent to ' . $to);
}
else response(404, 'No certificates will expire in ' . $expire . ' days');