| 1 | package Mail::STS::Policy; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | our $VERSION = '0.03'; # VERSION | |||||||||||||||||||||||||||
| 6 | # ABSTRACT: class to parse and generate RFC8461 policies | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | has 'version' => ( | |||||||||||||||||||||||||||
| 10 | is => 'rw', | ||||||||||||||||||||||||||||
| 11 | isa => 'Str', | ||||||||||||||||||||||||||||
| 12 | default => 'STSv1', | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | has 'mode' => ( | |||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | isa => 'Str', | ||||||||||||||||||||||||||||
| 18 | default => 'none', | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | has 'max_age' => ( | |||||||||||||||||||||||||||
| 22 | is => 'rw', | ||||||||||||||||||||||||||||
| 23 | isa => 'Maybe[Int]', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has 'mx' => ( | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 30 | 1 | traits => ['Array'], | |||||||||||||||||||||||||||
| 31 | handles => { | ||||||||||||||||||||||||||||
| 32 | 'add_mx' => 'push', | ||||||||||||||||||||||||||||
| 33 | 'clear_mx' => 'clear', | ||||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub new_from_string { | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | my ($class, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_from_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 0 | my $self = $class->new; | |||||||||||||||||||||||||||
| 41 | 0 | $self->parse($string); | |||||||||||||||||||||||||||
| 42 | 0 | return $self; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub parse { | ||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 0 | my @lines = split(/[\r\n]+/, $string); | |||||||||||||||||||||||||||
| 49 | 0 | my $ln = 0; | |||||||||||||||||||||||||||
| 50 | 0 | $self->clear_mx; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | while( my $line = shift(@lines) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | $ln++; | |||||||||||||||||||||||||||
| 54 | 0 | $line =~ s/[\r\n]*$//; | |||||||||||||||||||||||||||
| 55 | 0 | my ($key, $value) = split(/\s*:\s+/, $line, 2); | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | unless(defined $key && defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key and defined $value
MC/DC0%0/2
defined $key and defined $value
defined $key
defined $value
Truth table0%0/3
A and B
A= defined $keyB= defined $value
| |||||||||||||||||||||||||||||
| 57 | 0 | die("invalid syntax on line ${ln}"); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if($key eq 'version') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | unless($value eq 'STSv1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | die('only STSv1 version of policy is supported'); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 0 | $self->version($value); | |||||||||||||||||||||||||||
| 64 | 0 | next; | |||||||||||||||||||||||||||
| 65 | } elsif($key eq 'mode') { | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | unless($value =~ /^(testing|enforce|none)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | die("unsupported mode on line ${ln}"); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | $self->mode($value); | |||||||||||||||||||||||||||
| 70 | 0 | next; | |||||||||||||||||||||||||||
| 71 | } elsif($key eq 'max_age') { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | unless($value =~ /^(\d+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | die("max_age must be an integer on line ${ln}"); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | $self->max_age(int $value); | |||||||||||||||||||||||||||
| 76 | 0 | next; | |||||||||||||||||||||||||||
| 77 | } elsif($key eq 'mx') { | ||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless($value =~ /^(\*\.)?[0-9a-zA-Z\-]+(\.[0-9a-zA-Z\-]+)*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | die("invalid mx entry on line ${ln}"); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | $self->add_mx($value); | |||||||||||||||||||||||||||
| 82 | 0 | next; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | die("unknown key ${key} in policy on line ${line}"); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub as_hash { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
as_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | return { | ||||||||||||||||||||||||||||
| 92 | 0 | 'version' => $self->version, | |||||||||||||||||||||||||||
| 93 | 'mode' => $self->mode, | ||||||||||||||||||||||||||||
| 94 | 'max_age' => $self->max_age, | ||||||||||||||||||||||||||||
| 95 | 'mx' => $self->mx, | ||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub as_string { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my $hash = $self->as_hash; | |||||||||||||||||||||||||||
| 103 | return join('', map { | ||||||||||||||||||||||||||||
| 104 | 0 | _sprint_key_value($_, $hash->{$_}); | |||||||||||||||||||||||||||
| 105 | } 'version', 'mode', 'max_age', 'mx'); | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub _sprint_key_value { | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my ($key, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_sprint_key_value: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return '' unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | unless(ref $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | return("${key}: ${value}\n"); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if(ref($value) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return join('', map { "${key}: $_\n" } @$value); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | die('invalid data type for policy'); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | sub match_mx { | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my ($self, $host) = @_; | ||||||||||||||||||||||||||
|
Subroutine
match_mx: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 0 | foreach my $mx (@{$self->mx}) { | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | if($host eq $mx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | return 1; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if(my ($domain) = $mx =~ /^\*\.(.+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return 1 if $host eq $domain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | my $suffix = ".${domain}"; | |||||||||||||||||||||||||||
| 130 | 0 | my $suffix_len = length($suffix); | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | return 1 if substr($host, -$suffix_len) eq $suffix; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | 0 | return 0; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =pod | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 NAME | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Mail::STS::Policy - class to parse and generate RFC8461 policies | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 VERSION | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | version 0.03 | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # generate a policy | ||||||||||||||||||||||||||||
| 156 | my $policy = Mail::STS::Policy->new( | ||||||||||||||||||||||||||||
| 157 | mode => 'enforce', | ||||||||||||||||||||||||||||
| 158 | max_age => 604800, | ||||||||||||||||||||||||||||
| 159 | mx => [ 'mail.example.com' ], | ||||||||||||||||||||||||||||
| 160 | ); | ||||||||||||||||||||||||||||
| 161 | # setters | ||||||||||||||||||||||||||||
| 162 | $policy->mode('testing'); | ||||||||||||||||||||||||||||
| 163 | $policy->add_mx('mail.example.com'); | ||||||||||||||||||||||||||||
| 164 | print $policy->as_string; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # parse existing policy | ||||||||||||||||||||||||||||
| 167 | my $policy = Mail::STS::Policy->new_from_string($string); | ||||||||||||||||||||||||||||
| 168 | # access values | ||||||||||||||||||||||||||||
| 169 | $policy->mode; | ||||||||||||||||||||||||||||
| 170 | # 'enforce' | ||||||||||||||||||||||||||||
| 171 | $policy->mx; | ||||||||||||||||||||||||||||
| 172 | # [ 'mail.example.com' ] | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # check if a host is in there | ||||||||||||||||||||||||||||
| 175 | $policy->match_mx('mail.blablub.de') or die; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 version (default: 'STSv1') | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | Currently always version 'STSv1'. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 mode (default: 'none') | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Get/set mode of policy. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 max_age (default: undef) | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Get/set max_age for policy caching. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head2 mx (default: []) | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Array reference to array of mx hosts. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head1 METHODS | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head2 new_from_string($string) | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | Constructor for creating a new policy object from a policy string. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | Internally creates objects by calling new() and execute parse() on it. | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 parse($string) | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Parses values from $string to values in the object overwriting | ||||||||||||||||||||||||||||
| 206 | and clearing all existing values. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | Will die() on parsing error. | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head2 as_hash | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Returns a hash reference containing policy data. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | $policy->as_hash | ||||||||||||||||||||||||||||
| 215 | # { | ||||||||||||||||||||||||||||
| 216 | # 'version' => 'STSv1', | ||||||||||||||||||||||||||||
| 217 | # 'mode' => 'enforce', | ||||||||||||||||||||||||||||
| 218 | # 'max_age' => 3600, | ||||||||||||||||||||||||||||
| 219 | # 'mx' => [ 'mx.example.com', ... ], | ||||||||||||||||||||||||||||
| 220 | # } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head2 as_string | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | Outputs the object as a RFC8461 policy document. | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 match_mx($host) | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Returns if the policy matches $host. | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | $policy->match_mx('mail.example.com') or die; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Markus Benning <ich@markusbenning.de> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | This software is copyright (c) 2018 by Markus Benning <ich@markusbenning.de>. | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 241 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||