-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-dmarc.php
303 lines (243 loc) · 10.1 KB
/
wp-dmarc.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/*
Plugin Name: WP-DMARC Aggregate Collection and Parsing
Version: 0.1.0
Plugin URI: https://zabreznik.net/category/wp-dmarc/
Description: Collect and visualise DMARC Aggregate reports.
Author: Marko Zabreznik
Author URI: https://zabreznik.net
*/
define('WPDMARC_DATABASE_VERSION', 5);
class WPDMARC
{
private static $instance = null;
public function __construct()
{
register_activation_hook(__FILE__, array($this, 'on_activate_function'));
register_deactivation_hook(__FILE__, array($this, 'on_deactivate_function'));
add_action('init', array($this, 'init'));
if (is_admin()) {
add_action('admin_menu', array($this, 'init_admin'));
}
add_action('wpdmarc_cron', array($this, 'cron_action'));
}
public static function get_instance()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
public static function plugin_action_links($links)
{
$links[] = '<a href="' . esc_url(get_admin_url(null, 'tools.php?page=wpdmarc-reports')) . '">Reports</a>';
$links[] = '<a href="' . esc_url(get_admin_url(null, 'tools.php?page=wpdmarc-reports&reparse')) . '">Reparse</a>';
$links[] = '<a href="' . esc_url(get_admin_url(null, 'tools.php?page=wpdmarc-reports&fetch')) . '">Fetch</a>';
return $links;
}
public static function on_activate_function()
{
if (wp_next_scheduled('wpdmarc_cron') === FALSE) {
wp_schedule_event(time(), 'daily', 'wpdmarc_cron');
}
}
public static function on_deactivate_function()
{
wp_clear_scheduled_hook('wpdmarc_cron');
}
public function init()
{
// todo
// this is a simple way to keep the emails safe
// will fail on nginx tho
if (!defined('WPDMARC_DIR')) {
define('WPDMARC_DIR', WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'dmarc' . DIRECTORY_SEPARATOR);
if (!file_exists(WPDMARC_DIR . '.htaccess')) {
file_put_contents(WPDMARC_DIR . '.htaccess', 'deny from all');
}
}
if (get_option('obrazci_database_version', 0, true) < WPDMARC_DATABASE_VERSION) {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "
CREATE TABLE {$wpdb->prefix}wpdmarc_report (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
slug varchar(40) NOT NULL,
filename varchar(255) NOT NULL,
org_name varchar(255) NOT NULL,
report_id varchar(255) NOT NULL,
begin DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
end DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL,
domain varchar(255),
adkim varchar(20),
aspf varchar(20),
p varchar(20),
sp varchar(20),
pct varchar(20),
UNIQUE KEY slug (slug),
PRIMARY KEY (id)
) $charset_collate;
CREATE TABLE {$wpdb->prefix}wpdmarc_record (
id BIGINT(20) NOT NULL AUTO_INCREMENT,
report_slug varchar(40) NOT NULL,
header_from varchar(255),
source_ip varchar(39),
count int(10) NOT NULL,
disposition varchar(20),
dkim varchar(20),
spf varchar(20),
auth_results LONGTEXT,
PRIMARY KEY (id)
) $charset_collate;
";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option('wpdmarc_database_version', WPDMARC_DATABASE_VERSION);
}
}
public function init_admin()
{
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
add_management_page('DMARC Reports', 'DMARC Reports', 'manage_options', 'wpdmarc-reports', array($this, 'render_reports'));
}
public function render_reports()
{
$messages = array();
if (array_key_exists('reparse', $_GET)) {
$emails = glob(WPDMARC_DIR . '*.eml');
$updated = $this->email_parse($emails);
$messages = array(
array('notice', "Reparse updated $updated records.")
);
}
if (array_key_exists('fetch', $_GET)) {
$updated = $this->cron_action();
if ($updated === false) {
$messages = array(
array('notice', "Cound not fetch email. Please check POP/IMAP settings.")
);
} else {
$messages = array(
array('notice', "Fetched $updated records.")
);
}
}
require_once(__DIR__ . '/admin-list.php');
}
private function email_parse($emails)
{
# I hate PHP so much.
$updated = 0;
foreach ($emails as $email) {
$file = file_get_contents($email);
// handle zips
if ($file !== false && preg_match_all('/Content-(?:Type|Disposition): (?:application\/zip|attachment);[\s\r\n]+(?:file)?name="?([^"]+)\.zip"?.*?\r?\n\r?\n([^-=]+)/si', $file, $matches, PREG_SET_ORDER)) {
# so php has a IMAP collection, but it cant work with already downloaded emails,
# it has a ZIP function, but it can only work with already downloaded files
# yes I know PHP has this magic lib for reading emails but I don't want to have another lib requierment
# also, the MailParse suite is quite awful and needs another wrapper that would need another lib
# so all that is replaced with 1 regex and some magic,
# it wont work for everything but aint nobody got time for that
# luckily, PHP can do magic, but this only works if the .xml is named exactly like the .zip
$tmpfname = tempnam("/tmp", "wpdmrc");
foreach ($matches as $match) {
# make a temp file
$handle = fopen($tmpfname, "w");
# save the zip into it
fwrite($handle, base64_decode($match[2]));
fclose($handle);
# use magic to get the XML out
$data = file_get_contents("zip://$tmpfname#{$match[1]}.xml");
$updated += $this->save_xml_to_db($data, basename($email));
}
# remove temp
unlink($tmpfname);
}
}
return $updated;
}
private function save_xml_to_db($string, $filename)
{
$xml = new SimpleXMLElement($string);
$report_slug = $xml->report_metadata->org_name . '-' . $xml->report_metadata->report_id;
global $wpdb;
$wpdb->replace(
$wpdb->prefix . 'wpdmarc_report',
array(
'slug' => $report_slug,
'org_name' => $xml->report_metadata->org_name,
'report_id' => $xml->report_metadata->report_id,
'filename' => $filename,
'begin' => date('Y-m-d H:i:s', (int)$xml->report_metadata->date_range->begin),
'end' => date('Y-m-d H:i:s', (int)$xml->report_metadata->date_range->end),
'domain' => $xml->policy_published->domain,
'adkim' => $xml->policy_published->adkim,
'aspf' => $xml->policy_published->aspf,
'p' => $xml->policy_published->p,
'sp' => $xml->policy_published->sp,
'pct' => $xml->policy_published->pct,
),
array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')
);
# delete old parsed values if any
$wpdb->delete($wpdb->prefix . 'wpdmarc_record', array('report_slug' => $report_slug));
$updated = 0;
// parse records
foreach ($xml->record as $record) {
$wpdb->insert(
$wpdb->prefix . 'wpdmarc_record',
array(
'report_slug' => $report_slug,
'header_from' => $record->identifiers->header_from,
'source_ip' => $record->row->source_ip,
'count' => $record->row->count,
'disposition' => $record->row->policy_evaluated->disposition,
'dkim' => $record->row->policy_evaluated->dkim,
'spf' => $record->row->policy_evaluated->spf,
'auth_results' => json_encode($record->auth_results),
),
array('%s', '%s', '%s', '%d', '%s', '%s', '%s', '%s')
);
$updated += 1;
}
return $updated;
}
public function cron_action()
{
if (!defined('WPDMARC_SERV') || !defined('WPDMARC_NAME') || !defined('WPDMARC_PASS')) {
return false;
}
$emails = $this->email_fetch(WPDMARC_SERV, WPDMARC_NAME, WPDMARC_PASS, WPDMARC_DIR);
if ($emails) {
return $this->email_parse($emails);
}
return 0;
}
private function email_fetch($serv, $name, $pass, $dir)
{
$emails = array();
$emailconnection = imap_open($serv, $name, $pass);
if (!$emailconnection) {
throw new Exception('Could not connect to email server.');
}
$mails = imap_search($emailconnection, 'ALL', SE_UID);
if (is_array($mails)) {
foreach ($mails as $uid) {
$id = uniqid();
try {
if (!imap_savebody($emailconnection, $dir . 'email-' . $id . ".eml", $uid, null, FT_UID)) {
throw new Exception('Could not save email: ' . $id);
}
// email was saved, delete from server
imap_delete($emailconnection, $uid, FT_UID);
$emails[] = $dir . 'email-' . $id . ".eml";
} catch (Exception $e) {
error_log($e);
}
}
}
imap_close($emailconnection, CL_EXPUNGE);
return $emails;
}
}
WPDMARC::get_instance();