This file is indexed.

/usr/sbin/opendmarc-reports is in opendmarc 1.3.2-3.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
#!/usr/bin/perl
#
# Copyright (c) 2012-2016, 2017, The Trusted Domain Project.
#   All rights reserved.
#
# Script to generate regular DMARC reports.

###
### Setup
###

use strict;
use warnings;

use Switch;

use DBI;
use File::Basename;
use File::Temp;
use Net::Domain qw(hostfqdn hostdomain);
use Getopt::Long;
use IO::Handle;
use IO::Compress::Zip qw(zip);
use POSIX;
use MIME::Base64;
use Net::SMTP;
use Time::Local;

require DBD::mysql;

require HTTP::Request;

# general
my $progname      = basename($0);
my $version       = "1.3.2";
my $verbose       = 0;
my $helponly      = 0;
my $showversion   = 0;

my $interval;

my $gen;
my $uri;

my $buf;

my $mailout;
my $boundary;

my $tmpout;

my $repfile;
my $zipfile;

my $zipin;

my $now = time();

my $repstart;
my $repend;

my $domain;
my $domainid;
my $domainset;
my $forcedomain;
my @skipdomains;

my $policy;
my $spolicy;
my $policystr;
my $spolicystr;
my $pct;

my $repuri;
my @repuris;
my $lastsent;

my $aspf;
my $aspfstr;
my $adkim;
my $adkimstr;
my $align_dkim;
my $align_dkimstr;
my $align_spf;
my $align_spfstr;
my $spfresult;
my $dkimresult;
my $disp;
my $spfresultstr;
my $dkimresultstr;
my $dispstr;
my $ipaddr;
my $fromdomain;
my $envdomain;
my $dkimdomain;

my $repdest;

my $smtpstatus;
my $smtpfail;

my $doupdate = 1;
my $testmode = 0;
my $keepfiles = 0;
my $use_utc = 0;
my $daybound = 0;
my $report_maxbytes_global = 15728640; # default: 15M, per spec

my $msgid;

my $rowcount;

my $dbi_s;
my $dbi_h;
my $dbi_a;
my $dbi_d;

# DB parameters
my $def_dbhost    = "localhost";
my $def_dbname    = "opendmarc";
my $def_dbuser    = "opendmarc";
my $def_dbpasswd  = "opendmarc";
my $def_dbport    = "3306";
my $def_interval  = "86400";
my $dbhost;
my $dbname;
my $dbuser;
my $dbpasswd;
my $dbport;

my $dbscheme     = "mysql";

my $repdom       = hostdomain();
my $repemail     = "postmaster@" . $repdom;

my $smtp_server  = '127.0.0.1';
my $smtp_port    = 25;
my $smtp;

my $answer;

###
### NO user-serviceable parts beyond this point
###

sub usage
{
	print STDERR "$progname: usage: $progname [options]\n";
	print STDERR "\t--day              send yesterday's data\n";
	print STDERR "\t--dbhost=host      database host [$def_dbhost]\n";
	print STDERR "\t--dbname=name      database name [$def_dbname]\n";
	print STDERR "\t--dbpasswd=passwd  database password [$def_dbpasswd]\n";
	print STDERR "\t--dbport=port      database port [$def_dbport]\n";
	print STDERR "\t--dbuser=user      database user [$def_dbuser]\n";
	print STDERR "\t--domain=name      force a report for named domain\n";
	print STDERR "\t--help             print help and exit\n";
	print STDERR "\t--interval=secs    report interval [$def_interval]\n";
	print STDERR "\t--keepfiles        keep xml files (in local directory)\n";
	print STDERR "\t -n                synonym for --test\n";
	print STDERR "\t--nodomain=name    omit a report for named domain\n";
	print STDERR "\t--noupdate         don't record report transmission\n";
	print STDERR "\t--report-email     reporting contact [$repemail]\n";
	print STDERR "\t--report-org       reporting organization [$repdom]\n";
	print STDERR "\t--smtp-port        smtp server port [$smtp_port]\n";
	print STDERR "\t--smtp-server      smtp server [$smtp_server]\n";
	print STDERR "\t--test             don't send reports\n";
	print STDERR "\t                   (implies --keepfiles --noupdate)\n";
	print STDERR "\t--utc              operate in UTC\n";
	print STDERR "\t--verbose          verbose output\n";
	print STDERR "\t                   (repeat for increased output)\n";
	print STDERR "\t--version          print version and exit\n";
}

# set locale
setlocale(LC_ALL, 'C');

# parse command line arguments
my $opt_retval = &Getopt::Long::GetOptions ('day!' => \$daybound,
                                            'dbhost=s' => \$dbhost,
                                            'dbname=s' => \$dbname,
                                            'dbpasswd=s' => \$dbpasswd,
                                            'dbport=s' => \$dbport,
                                            'dbuser=s' => \$dbuser,
                                            'domain=s' => \$forcedomain,
                                            'help!' => \$helponly,
                                            'interval=i' => \$interval,
                                            'keepfiles' => \$keepfiles,
                                            'n|test' => \$testmode,
                                            'nodomain=s' => \@skipdomains,
                                            'report-email=s' => \$repemail,
                                            'report-org=s' => \$repdom,
                                            'smtp-server=s' => \$smtp_server,
                                            'smtp-port=i' => \$smtp_port,
                                            'update!' => \$doupdate,
                                            'utc!' => \$use_utc,
                                            'verbose+' => \$verbose,
                                            'version!' => \$showversion,
                                           );

if (!$opt_retval || $helponly)
{
	usage();

	if ($helponly)
	{
		exit(0);
	}
	else
	{
		exit(1);
	}
}

if ($showversion)
{
	print STDOUT "$progname v$version\n";
	exit(0);
}

# apply defaults
if (!defined($dbhost))
{
	if (defined($ENV{'OPENDMARC_DBHOST'}))
	{
		$dbhost = $ENV{'OPENDMARC_DBHOST'};
	}
	else
	{
		$dbhost = $def_dbhost;
	}
}

if (!defined($dbname))
{
	if (defined($ENV{'OPENDMARC_DB'}))
	{
		$dbname = $ENV{'OPENDMARC_DB'};
	}
	else
	{
		$dbname = $def_dbname;
	}
}

if (!defined($dbpasswd))
{
	if (defined($ENV{'OPENDMARC_PASSWORD'}))
	{
		$dbpasswd = $ENV{'OPENDMARC_PASSWORD'};
	}
	else
	{
		$dbpasswd = $def_dbpasswd;
	}
}

if (!defined($dbport))
{
	if (defined($ENV{'OPENDMARC_PORT'}))
	{
		$dbport = $ENV{'OPENDMARC_PORT'};
	}
	else
	{
		$dbport = $def_dbport;
	}
}

if (!defined($dbuser))
{
	if (defined($ENV{'OPENDMARC_USER'}))
	{
		$dbuser = $ENV{'OPENDMARC_USER'};
	}
	else
	{
		$dbuser = $def_dbuser;
	}
}

if (defined($interval) && $daybound)
{
	print STDERR "$progname: WARN: --day overrides --interval\n";
}

if (!defined($interval) || $daybound)
{
	$interval = $def_interval;
}

# Test mode requested, don't update last sent and keep xml files
$doupdate  = ($testmode == 1) ? 0 : $doupdate;
$keepfiles = ($testmode == 1) ? 1 : $keepfiles;

if ($verbose)
{
	print STDERR "$progname: started at " . localtime($now) . "\n";
}

my $dbi_dsn = "DBI:" . $dbscheme . ":database=" . $dbname .
              ";host=" . $dbhost . ";port=" . $dbport;

$dbi_h = DBI->connect($dbi_dsn, $dbuser, $dbpasswd, { PrintError => 0 });
if (!defined($dbi_h))
{
	print STDERR "$progname: unable to connect to database: $DBI::errstr\n";
	exit(1);
}

if ($verbose >= 2)
{
	print STDERR "$progname: connected to database\n";
}

if ($use_utc)
{
	$dbi_s = $dbi_h->prepare("SET TIME_ZONE='+00:00'");

	if (!$dbi_s->execute())
	{
		print STDERR "$progname: failed to change to UTC: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}
}

#
# Select domains on which to report
#

if ($verbose >= 2)
{
	print STDERR "$progname: selecting target domains\n";
}

if (defined($forcedomain))
{
	$dbi_s = $dbi_h->prepare("SELECT name FROM domains WHERE name = ?");

	if (!$dbi_s->execute($forcedomain))
	{
		print STDERR "$progname: failed to test for database entry: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}
}
elsif ($daybound)
{
	$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE DATE(lastsent) < DATE(FROM_UNIXTIME(?))");

	if (!$dbi_s->execute($now))
	{
		print STDERR "$progname: failed to collect domain names: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}
}
else
{
	$dbi_s = $dbi_h->prepare("SELECT domains.name FROM requests JOIN domains ON requests.domain = domains.id WHERE lastsent <= DATE_SUB(FROM_UNIXTIME(?), INTERVAL ? SECOND)");

	if (!$dbi_s->execute($now, $interval))
	{
		print STDERR "$progname: failed to collect domain names: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}
}

$domainset = $dbi_s->fetchall_arrayref([0]);
$dbi_s->finish;

if ($verbose)
{
	print STDERR "$progname: selected " . scalar(@$domainset) . " domain(s)\n";
}

#
# For each domain:
# 	-- extract reporting address
# 	-- extract messages/signatures to report
# 	-- generate and send report
# 	-- update "last sent" timestamp
#

$smtp = Net::SMTP->new($smtp_server,
                       'Port' => $smtp_port,
                       'Hello' => hostfqdn());
if (!defined($smtp))
{
	print STDERR "$progname: open SMTP server $smtp_server:$smtp_port failed\n";
	exit(1);
}

foreach (@$domainset)
{
	$domain = $_->[0];

	if (!defined($domain))
	{
		next;
	}

	if (@skipdomains && grep({$_ eq $domain} @skipdomains) != 0)
	{
		next;
	}

	if ($verbose >= 2)
	{
		print STDERR "$progname: processing $domain\n";
	}

	# extract this domain's reporting parameters
	$dbi_s = $dbi_h->prepare("SELECT id FROM domains WHERE name = ?");
	if (!$dbi_s->execute($domain))
	{
		print STDERR "$progname: can't get ID for domain $domain: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}

	undef $domainid;
	while ($dbi_a = $dbi_s->fetchrow_arrayref())
	{
		if (defined($dbi_a->[0]))
		{
			$domainid = $dbi_a->[0];
		}
	}
	$dbi_s->finish;

	if (!defined($domainid))
	{
		print STDERR "$progname: ID for domain $domain not found\n";
		next;
	}

	$dbi_s = $dbi_h->prepare("SELECT repuri, adkim, aspf, policy, spolicy, pct, UNIX_TIMESTAMP(lastsent) FROM requests WHERE domain = ?");
	if (!$dbi_s->execute($domainid))
	{
		print STDERR "$progname: can't get reporting URI for domain $domain: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}

	undef $repuri;

	while ($dbi_a = $dbi_s->fetchrow_arrayref())
	{
		if (defined($dbi_a->[0]))
		{
			$repuri = $dbi_a->[0];
		}
		if (defined($dbi_a->[1]))
		{
			$adkim = $dbi_a->[1];
		}
		if (defined($dbi_a->[2]))
		{
			$aspf = $dbi_a->[2];
		}
		if (defined($dbi_a->[3]))
		{
			$policy = $dbi_a->[3];
		}
		if (defined($dbi_a->[4]))
		{
			$spolicy = $dbi_a->[4];
		}
		if (defined($dbi_a->[5]))
		{
			$pct = $dbi_a->[5];
		}
		if (defined($dbi_a->[6]))
		{
			$lastsent = $dbi_a->[6];
		}
	}

	$dbi_s->finish;

	if (!defined($repuri) || ("" eq $repuri))
	{
		if ($verbose >= 2)
		{
			print STDERR "$progname: no reporting URI for domain $domain; skipping\n";
		}

		next;
	}

	if ($daybound)
	{
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now - $interval);
		$repstart = timelocal(0, 0, 0, $mday, $mon, $year);
		($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($now);
		$repend = timelocal(0, 0, 0, $mday, $mon, $year);
	}
	else
	{
		$repstart = $now - $interval;
		$repend = $now;
	}

	# construct the temporary file
	$repfile = $repdom . "!" . $domain . "!" . $repstart . "!" . $repend . ".xml";
	$zipfile = $repdom . "!" . $domain . "!" . $repstart . "!" . $repend . ".zip";
	if (!open($tmpout, ">", $repfile))
	{
		print STDERR "$progname: can't create report file for domain $domain\n";
		next;
	}

	switch ($adkim)
	{
		case ord("r")	{ $adkimstr = "r"; }
		case ord("s")	{ $adkimstr = "s"; }
		else		{ $adkimstr = "unknown"; }
	}

	switch ($aspf)
	{
		case ord("r")	{ $aspfstr = "r"; }
		case ord("s")	{ $aspfstr = "s"; }
		else		{ $aspfstr = "unknown"; }
	}

	switch ($policy)
	{
		case ord("n")	{ $policystr = "none"; }
		case ord("q")	{ $policystr = "quarantine"; }
		case ord("r")	{ $policystr = "reject"; }
		else		{ $policystr = "unknown"; }
	}

 	switch ($spolicy)
 	{
 		case 0		{ $spolicystr = $policystr; }
 		case ord("n")	{ $spolicystr = "none"; }
 		case ord("q")	{ $spolicystr = "quarantine"; }
 		case ord("r")	{ $spolicystr = "reject"; }
 		else		{ $spolicystr = "unknown"; }
 	}


	print $tmpout "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
	print $tmpout "<feedback>\n";

	print $tmpout " <report_metadata>\n";
	print $tmpout "  <org_name>$repdom</org_name>\n";
	print $tmpout "  <email>$repemail</email>\n";
	print $tmpout "  <report_id>$domain:$now</report_id>\n";
	print $tmpout "  <date_range>\n";
	print $tmpout "   <begin>$repstart</begin>\n";
	print $tmpout "   <end>$repend</end>\n";
	print $tmpout "  </date_range>\n";
	print $tmpout " </report_metadata>\n";

	print $tmpout " <policy_published>\n";
	print $tmpout "  <domain>$domain</domain>\n";
	print $tmpout "  <adkim>$adkimstr</adkim>\n";
	print $tmpout "  <aspf>$aspfstr</aspf>\n";
	print $tmpout "  <p>$policystr</p>\n";
	print $tmpout "  <sp>$spolicystr</sp>\n";
	print $tmpout "  <pct>$pct</pct>\n";
	print $tmpout " </policy_published>\n";

	if ($daybound)
	{
		$dbi_s = $dbi_h->prepare("SELECT messages.id, ipaddr.addr, messages.disp, d1.name, d2.name, messages.spf, messages.align_spf, messages.align_dkim FROM messages JOIN ipaddr ON messages.ip = ipaddr.id JOIN domains d1 ON messages.from_domain = d1.id JOIN domains d2 ON messages.env_domain = d2.id WHERE messages.from_domain = ? AND DATE(messages.date) >= DATE(FROM_UNIXTIME(?)) AND DATE(messages.date) < DATE(FROM_UNIXTIME(?))");
	}
	else
	{
		$dbi_s = $dbi_h->prepare("SELECT messages.id, ipaddr.addr, messages.disp, d1.name, d2.name, messages.spf, messages.align_spf, messages.align_dkim FROM messages JOIN ipaddr ON messages.ip = ipaddr.id JOIN domains d1 ON messages.from_domain = d1.id JOIN domains d2 ON messages.env_domain = d2.id WHERE messages.from_domain = ? AND messages.date > FROM_UNIXTIME(?) AND messages.date <= FROM_UNIXTIME(?)");
	}

	if (!$dbi_s->execute($domainid, $repstart, $repend))
	{
		print STDERR "$progname: can't extract report for domain $domain: " . $dbi_h->errstr . "\n";
		$dbi_s->finish;
		$dbi_h->disconnect;
		exit(1);
	}

	$rowcount = 0;

	while ($dbi_a = $dbi_s->fetchrow_arrayref())
	{
		undef $msgid;

		if (defined($dbi_a->[0]))
		{
			$msgid = $dbi_a->[0];
		}
		if (defined($dbi_a->[1]))
		{
			$ipaddr = $dbi_a->[1];
		}
		if (defined($dbi_a->[2]))
		{
			$disp = $dbi_a->[2];
		}
		if (defined($dbi_a->[3]))
		{
			$fromdomain = $dbi_a->[3];
		}
		if (defined($dbi_a->[4]))
		{
			$envdomain = $dbi_a->[4];
		}
		if (defined($dbi_a->[5]))
		{
			$spfresult = $dbi_a->[5];
		}
		if (defined($dbi_a->[6]))
		{
			$align_spf = $dbi_a->[6];
		}
		if (defined($dbi_a->[7]))
		{
			$align_dkim = $dbi_a->[7];
		}

		if (!defined($msgid))
		{
			next;
		}

		$rowcount++;

		switch ($disp)
		{
			case 0	{ $dispstr = "reject"; }
			case 1	{ $dispstr = "reject"; }
			case 2	{ $dispstr = "none"; }
			case 4	{ $dispstr = "quarantine"; }
			else	{ $dispstr = "unknown"; }
		}

		switch ($spfresult)
		{
			case 0	{ $spfresultstr = "pass"; }
			case 2	{ $spfresultstr = "softfail"; }
			case 3	{ $spfresultstr = "neutral"; }
			case 4	{ $spfresultstr = "temperror"; }
			case 5	{ $spfresultstr = "permerror"; }
			case 6	{ $spfresultstr = "none"; }
			case 7	{ $spfresultstr = "fail"; }
			case 8	{ $spfresultstr = "policy"; }
			case 9	{ $spfresultstr = "nxdomain"; }
			case 10	{ $spfresultstr = "signed"; }
			case 12	{ $spfresultstr = "discard"; }
			else	{ $spfresultstr = "unknown"; }
		}

		switch ($align_dkim)
		{
			case 4	{ $align_dkimstr = "pass"; }
			case 5	{ $align_dkimstr = "fail"; }
			else	{ $align_dkimstr = "unknown"; }
		}

		switch ($align_spf)
		{
			case 4	{ $align_spfstr = "pass"; }
			case 5	{ $align_spfstr = "fail"; }
			else	{ $align_spfstr = "unknown"; }
		}

		print $tmpout " <record>\n";
		print $tmpout "  <row>\n";
		print $tmpout "   <source_ip>$ipaddr</source_ip>\n";
		print $tmpout "   <count>1</count>\n";
		print $tmpout "   <policy_evaluated>\n";
		print $tmpout "    <disposition>$dispstr</disposition>\n";
		print $tmpout "    <dkim>$align_dkimstr</dkim>\n";
		print $tmpout "    <spf>$align_spfstr</spf>\n";
		print $tmpout "   </policy_evaluated>\n";
		print $tmpout "  </row>\n";
		print $tmpout "  <identifiers>\n";
		print $tmpout "   <header_from>$fromdomain</header_from>\n";
		print $tmpout "  </identifiers>\n";
		print $tmpout "  <auth_results>\n";
		print $tmpout "   <spf>\n";
		print $tmpout "    <domain>$envdomain</domain>\n";
		print $tmpout "    <result>$spfresultstr</result>\n";
		print $tmpout "   </spf>\n";

		$dbi_d = $dbi_h->prepare("SELECT domains.name, pass FROM signatures JOIN domains ON signatures.domain = domains.id WHERE signatures.message = ?");
		if (!$dbi_d->execute($msgid))
		{
			print STDERR "$progname: can't extract report for message $msgid: " . $dbi_h->errstr . "\n";
			$dbi_s->finish;
			$dbi_d->finish;
			$dbi_h->disconnect;
			exit(1);
		}

		my %dkim_domain_result_cache = ();
		while ($dbi_a = $dbi_d->fetchrow_arrayref())
		{
			undef $dkimdomain;

			if (defined($dbi_a->[0]))
			{
				$dkimdomain = $dbi_a->[0];
			}
			if (defined($dbi_a->[1]))
			{
				$dkimresult = $dbi_a->[1];
			}

			if (!defined($dkimdomain))
			{
				next;
			}
			if (defined($dkim_domain_result_cache{$dkimdomain}{$dkimresult}))
			{
				next; # no duplicate per-record auth_result dkim sections
			}
			$dkim_domain_result_cache{$dkimdomain}{$dkimresult}++;

			switch ($dkimresult)
			{
				case 0	{ $dkimresultstr = "pass"; }
				case 2	{ $dkimresultstr = "softfail"; }
				case 3	{ $dkimresultstr = "neutral"; }
				case 4	{ $dkimresultstr = "temperror"; }
				case 5	{ $dkimresultstr = "permerror"; }
				case 6	{ $dkimresultstr = "none"; }
				case 7	{ $dkimresultstr = "fail"; }
				case 8	{ $dkimresultstr = "policy"; }
				case 9	{ $dkimresultstr = "nxdomain"; }
				case 10	{ $dkimresultstr = "signed"; }
				case 12	{ $dkimresultstr = "discard"; }
				else	{ $dkimresultstr = "unknown"; }
			}

			print $tmpout "   <dkim>\n";
			print $tmpout "    <domain>$dkimdomain</domain>\n";
			print $tmpout "    <result>$dkimresultstr</result>\n";
			print $tmpout "   </dkim>\n";
		}

		$dbi_d->finish;

		print $tmpout "  </auth_results>\n";
		print $tmpout " </record>\n";
	}

	$dbi_s->finish;

	print $tmpout "</feedback>\n";

	close($tmpout);

	if ($rowcount == 0)
	{
		if ($verbose >= 2)
		{
			print STDERR "$progname: no activity selected for $domain; skipping\n";
		}

		unlink($repfile);
		next;
	}

	# zip the report
	if (!zip [ $repfile ] => $zipfile)
	{
		print STDERR "$progname: can't zip report for domain $domain: $!\n";
		next;
	}

	if ($keepfiles)
	{
		print STDERR "$progname: keeping report file \"$repfile\"\n";
	}

	# decode the URI
	@repuris = split(',', $repuri);

	for $repuri (@repuris)
	{
		$uri = URI->new($repuri);
		if (!defined($uri) ||
		    !defined($uri->scheme) ||
		    $uri->opaque eq "")
		{
			print STDERR "$progname: can't parse reporting URI for domain $domain\n";
			next;
		}

		$repdest = $uri->opaque;
		my $report_maxbytes = $report_maxbytes_global;

		# check for max report size
		if ($repdest =~ m/^(\S+)!(\d{1,15})([kmgt])?$/i)
		{
			$repdest = $1;
			$report_maxbytes = $2;
			if ($3)
			{
				my $letter = lc($3);
				if ($letter eq 'k')
				{
					$report_maxbytes = $report_maxbytes * 1024;
				}
				if ($letter eq 'm')
				{
					$report_maxbytes = $report_maxbytes * 1048576;
				}
				if ($letter eq 'g')
				{
					$report_maxbytes = $report_maxbytes * (2**30);
				}
				if ($letter eq 't')
				{
					$report_maxbytes = $report_maxbytes * (2**40);
				}
			}
		}

		# Test mode, just report what would have been done
		if ($testmode)
		{
			print STDERR "$progname: would email $domain report for " .
			             "$rowcount records to " .  $uri->opaque . "\n";
		}
		# ensure a scheme is present
		elsif (!defined($uri->scheme))
		{
			if ($verbose >= 2)
			{
				print STDERR "$progname: unknown URI scheme in '$repuri' for domain $domain\n";
			}
			next;
		}
		# send/post report
		elsif ($uri->scheme eq "mailto")
		{
			my $datestr;
			my $report_id;

			if (!open($zipin, $zipfile))
			{
				print STDERR "$progname: can't read zipped report for $domain: $!\n";
				next;
			}

			$boundary = "report_section";

 			$report_id = $domain . "-" . $now . "@" . $repdom;
			$datestr = strftime("%a, %e %b %Y %H:%M:%S %z (%Z)",
			                    localtime);

			$mailout  = "To: $repdest\n";
			$mailout .= "From: $repemail\n";
			$mailout .= "Subject: Report Domain: " . $domain . " Submitter: " . $repdom . " Report-ID: " . $report_id . "\n";
			$mailout .= "X-Mailer: " . $progname . " v" . $version ."\n";
			$mailout .= "Date: " . $datestr . "\n";
			$mailout .= "Message-ID: <$report_id>\n";
			$mailout .= "Auto-Submitted: auto-generated\n";
			$mailout .= "MIME-Version: 1.0\n";
			$mailout .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
			$mailout .= "\n";
			$mailout .= "This is a MIME-encapsulated message.\n";
			$mailout .= "\n";
			$mailout .= "--$boundary\n";
			$mailout .= "Content-Type: text/plain;\n";
			$mailout .= "\n";
			$mailout .= "This is a DMARC aggregate report for $domain\n";
			$mailout .= "generated at " . localtime() . "\n";
			$mailout .= "\n";
			$mailout .= "--$boundary\n";
			$mailout .= "Content-Type: application/zip\n";
			$mailout .= "Content-Disposition: attachment; filename=\"$zipfile\"\n";
			$mailout .= "Content-Transfer-Encoding: base64\n";
			$mailout .= "\n";

			while (read($zipin, $buf, 60*57))
			{
				$mailout .= encode_base64($buf);
			}

			$mailout .= "\n";
			$mailout .= "--$boundary--\n";
			my $reportsize = length($mailout);

			if ($reportsize > $report_maxbytes)
			{
				# XXX -- generate an error report here
				print STDERR "$progname: report was too large ($reportsize bytes) per limitation of URI " . $uri->opaque . " for domain $domain\n";
			}
			else
			{
				$smtpstatus = "sent";
				$smtpfail = 0;
				if (!$smtp->mail($repemail) ||
				    !$smtp->to($repdest) ||
				    !$smtp->data() ||
				    !$smtp->datasend($mailout) ||
				    !$smtp->dataend())
				{
					$smtpfail = 1;
					$smtpstatus = "failed to send";
				}

				if ($verbose || $smtpfail)
				{
					# now perl voodoo:
					$answer = ${${*$smtp}{'net_cmd_resp'}}[1] || $smtp->message() || 'unknown error';
					chomp($answer);
					print STDERR "$progname: $smtpstatus report for $domain to $repdest ($answer)\n";
				}
			}

			$smtp->reset();

			close($zipin);
		}
		else
		{
			print STDERR "$progname: unsupported reporting URI scheme " . $uri->scheme . " for domain $domain\n";
			next;
		}
	}

	# update "last sent" timestamp
	if ($doupdate)
	{
		$dbi_s = $dbi_h->prepare("UPDATE requests SET lastsent = FROM_UNIXTIME(?) WHERE domain = ?");
		if (!$dbi_s->execute($repend, $domainid))
		{
			print STDERR "$progname: can't update last sent time for domain $domain: " . $dbi_h->errstr . "\n";
			$dbi_s->finish;
			$dbi_h->disconnect;
			exit(1);
		}
	}

	unlink($zipfile);
	if (!$keepfiles)
	{
		unlink($repfile);
	}
}

$smtp->quit();

#
# all done!
#

$dbi_s->finish;

if ($verbose)
{
	print STDERR "$progname: terminating at " . localtime() . "\n";
}

$dbi_h->disconnect;

exit(0);