« auto/share/dist/Zonemaster-Backend/patch/patch_postgresql_db_zonemaster_backend_ver_8.0.0.pl
↑ Summary
Zonemaster/Backend/DB/MySQL.pm »
| 1 | package Zonemaster::Backend::TLD_URL; | ||
| 2 | |||
| 3 | =head1 Zonemaster::Backend::TLD_URL | ||
| 4 | |||
| 5 | This Perl module is the backend for the RPCAPI method "get_tld_url" | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use 5.14.2; | ||
| 12 | |||
| 13 | use HTTP::Tiny; | ||
| 14 | use JSON::PP qw(decode_json); | ||
| 15 | use Readonly; | ||
| 16 | |||
| 17 | # Zonemaster Modules | ||
| 18 | use Zonemaster::Engine; | ||
| 19 | use Zonemaster::Engine::Recursor; | ||
| 20 | use Zonemaster::Backend; | ||
| 21 | use Zonemaster::Backend::Config; | ||
| 22 | use Zonemaster::Backend::Validator qw[ untaint_tld_block untaint_tld_url_no_path untaint_tld_url_with_path untaint_tld_url_string ]; | ||
| 23 | use Zonemaster::Backend::Errors; | ||
| 24 | |||
| 25 | Readonly my $IANA_RDAP_URL_BASE => "https://rdap.iana.org/domain"; | ||
| 26 | Readonly my $DNS_NAME_BASE_TXT_RECORD => "_url._zonemaster"; | ||
| 27 | Readonly my $SOURCE_BACKEND_CONF_STR => "BACKEND CONF"; | ||
| 28 | Readonly my $SOURCE_TXT_RECORD_STR => "TXT RECORD"; | ||
| 29 | Readonly my $SOURCE_IANA_RDAP_STR => "IANA RDAP"; | ||
| 30 | |||
| 31 | =head2 process ($self, $domain) | ||
| 32 | |||
| 33 | Processes the domain name ($domain) for Zonemaster::Backend::RPCAPI::get_tld_url | ||
| 34 | and returns a complete hash reference to be returned by the RPCAPI. | ||
| 35 | See L<domain name|https://github.com/zonemaster/zonemaster/blob/master/docs/public/configuration/tld-url-specification.md> | ||
| 36 | for a specification of the features implemented in this module. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub process { | ||
| 41 | my ( $self, $domain ) = @_; | ||
| 42 | |||
| 43 | my %result; | ||
| 44 | my $timeout = $self->{config}->TLD_URL_SETTINGS_lookup_timeout; | ||
| 45 | my $include_source = $self->{config}->TLD_URL_SETTINGS_include_source; | ||
| 46 | my $enable_tld_url = $self->{config}->TLD_URL_SETTINGS_enable_tld_url; | ||
| 47 | my %overrides = $self->{config}->TLD_URL_OVERRIDE; | ||
| 48 | my @labels = split( /\./, $domain ); | ||
| 49 | my $tld = $labels[$#labels]; # Empty if $domain is root '.' | ||
| 50 | |||
| 51 | # Empty response if the function is not enabled | ||
| 52 | unless ( $enable_tld_url ) { | ||
| 53 | $result{tld} = $tld if defined $tld and $tld ne ''; | ||
| 54 | return \%result; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Empty response if the domain is the root zone | ||
| 58 | if ( $domain eq '.' ) { | ||
| 59 | return \%result; | ||
| 60 | } | ||
| 61 | |||
| 62 | # Empty response if the domain is just a TLD | ||
| 63 | if ( scalar @labels == 1 ) { | ||
| 64 | $result{tld} = $tld; | ||
| 65 | return \%result; | ||
| 66 | } | ||
| 67 | |||
| 68 | # Check any override from the configuration | ||
| 69 | my $href_override_result = url_from_override( $domain, $tld, $include_source, \%overrides ); | ||
| 70 | return $href_override_result if %$href_override_result; | ||
| 71 | |||
| 72 | # Do a lookup of "_url._zonemaster.$tld" | ||
| 73 | my $href_txt_record_result = url_from_txt_record( $domain, $tld, $timeout, $include_source ); | ||
| 74 | return $href_txt_record_result if %$href_txt_record_result; | ||
| 75 | |||
| 76 | # Do an IANA RDAP lookup | ||
| 77 | my $href_rdap_lookup_result = url_from_rdap ( $tld, $timeout, $include_source ); | ||
| 78 | return $href_rdap_lookup_result if %$href_rdap_lookup_result; | ||
| 79 | |||
| 80 | $result{tld} = $tld; | ||
| 81 | return \%result; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 url_from_override ($dom, $tld, $include_source, $href_or) | ||
| 85 | |||
| 86 | Used by subroutine "process" to do the "dirty work" to process | ||
| 87 | any overrides in the configuration. | ||
| 88 | |||
| 89 | The following variables are mandatory in the call: | ||
| 90 | |||
| 91 | =over 8 | ||
| 92 | |||
| 93 | =item * The domain name to be processed ($dom) | ||
| 94 | |||
| 95 | =item * The TLD extracted from the domain name ($tld) | ||
| 96 | |||
| 97 | =item * Boolean value whether source of URL should be indicated in the result ($include_source) | ||
| 98 | |||
| 99 | =item * Reference of a HASH of the override data (if any) from the configuration file ($href_or) | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | A HASH reference ready to be sent by the RPCAPI is returned. Or empty then not to be sent. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub url_from_override { | ||
| 108 | my ($dom, $tld, $include_source, $href_or) = @_; | ||
| 109 | my $url; | ||
| 110 | my %result; | ||
| 111 | |||
| 112 | if ( exists $$href_or{$tld} ) { | ||
| 113 | |||
| 114 | if ( untaint_tld_block( $$href_or{$tld} ) ) { | ||
| 115 | $result{tld} = $tld; | ||
| 116 | } else { | ||
| 117 | $url = $$href_or{$tld}; | ||
| 118 | $url = $url . '/' if untaint_tld_url_no_path( $url ); | ||
| 119 | $url =~ s/\Q[DOMAIN]\E/$dom/; # If any "[DOMAIN]" | ||
| 120 | $result{tld} = $tld; | ||
| 121 | $result{url} = $url; | ||
| 122 | $result{source} = $SOURCE_BACKEND_CONF_STR if $include_source; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | return \%result; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head2 url_from_txt_record ($dom, $tld, $timeout, $include_source) | ||
| 129 | |||
| 130 | Used by subroutine "process" to do the "dirty work" to process | ||
| 131 | the TXT lookup and the postprocessing of it. | ||
| 132 | |||
| 133 | The following variables are mandatory in the call: | ||
| 134 | |||
| 135 | =over 8 | ||
| 136 | |||
| 137 | =item * The domain name to be processed ($dom) | ||
| 138 | |||
| 139 | =item * The TLD extracted from the domain name ($tld) | ||
| 140 | |||
| 141 | =item * The time limit for the lookup ($timeout) | ||
| 142 | |||
| 143 | =item * Boolean value whether source of URL should be indicated in the result ($include_source) | ||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | A HASH reference ready to be sent by the RPCAPI is returned. Or empty then not to be sent. | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub url_from_txt_record { | ||
| 152 | my ( $dom, $tld, $timeout, $include_source ) = @_; | ||
| 153 | my $url; | ||
| 154 | my %result; | ||
| 155 | my $name = $DNS_NAME_BASE_TXT_RECORD . '.' . $tld; | ||
| 156 | my $packet; | ||
| 157 | |||
| 158 | eval { | ||
| 159 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 160 | alarm $timeout; | ||
| 161 | $packet = Zonemaster::Engine::Recursor->recurse( $name, 'TXT' ); | ||
| 162 | alarm 0; | ||
| 163 | }; | ||
| 164 | # Use $packet if defined | ||
| 165 | if ( $packet and $packet->rcode eq q{NOERROR} ) { | ||
| 166 | my @rrs = $packet->get_records_for_name( q{TXT}, $name ); | ||
| 167 | my @txt_rdata = map { $_->txtdata() } @rrs; | ||
| 168 | if ( scalar ( @txt_rdata ) == 1 ) { # Ignore all if more than one | ||
| 169 | my $data = $txt_rdata[0]; | ||
| 170 | if ( untaint_tld_block( $data ) ) { # "[BLOCK]" | ||
| 171 | $result{tld} = $tld; | ||
| 172 | } elsif ( untaint_tld_url_no_path( $data ) ) { # URL without path | ||
| 173 | $result{tld} = $tld; | ||
| 174 | $result{url} = $data . '/'; | ||
| 175 | $result{source} = $SOURCE_TXT_RECORD_STR if $include_source; | ||
| 176 | } elsif ( untaint_tld_url_string( $data ) ) { # URL with path and possible "[DOMAIN]" | ||
| 177 | $data =~ s/\Q[DOMAIN]\E/$dom/; # If any "[DOMAIN]" | ||
| 178 | $result{tld} = $tld; | ||
| 179 | $result{url} = $data; | ||
| 180 | $result{source} = $SOURCE_TXT_RECORD_STR if $include_source; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | return \%result; | ||
| 185 | } | ||
| 186 | |||
| 187 | =head2 url_from_rdap ($tld, $timeout, $include_source ) | ||
| 188 | |||
| 189 | Used by subroutine "process" to do the "dirty work" to process | ||
| 190 | the IANA RDAP lookup and the postprocessing of it. | ||
| 191 | |||
| 192 | The following variables are mandatory in the call: | ||
| 193 | |||
| 194 | =over 8 | ||
| 195 | |||
| 196 | |||
| 197 | =item * The TLD extracted from the domain name ($tld) | ||
| 198 | |||
| 199 | =item * The time limit for the lookup ($timeout) | ||
| 200 | |||
| 201 | =item * Boolean value whether source of URL should be indicated in the result ($include_source) | ||
| 202 | |||
| 203 | =back | ||
| 204 | |||
| 205 | A HASH reference ready to be sent by the RPCAPI is returned. Or empty then not to be sent. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub url_from_rdap { | ||
| 210 | my ( $tld, $timeout, $include_source ) = @_; | ||
| 211 | my $url = $IANA_RDAP_URL_BASE . '/' . $tld; | ||
| 212 | my $response; | ||
| 213 | my @links = (); | ||
| 214 | my $link = ''; | ||
| 215 | my %result; | ||
| 216 | |||
| 217 | eval { | ||
| 218 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 219 | alarm $timeout; | ||
| 220 | $response = HTTP::Tiny->new->get($url); | ||
| 221 | alarm 0; | ||
| 222 | }; | ||
| 223 | if ($@) { | ||
| 224 | if ( $@ eq "alarm\n" ) { | ||
| 225 | handle_exception( "Timeout looking $url up" ); | ||
| 226 | } else { | ||
| 227 | handle_exception( "Unexpected error looking $url up: $@" ); | ||
| 228 | } | ||
| 229 | } | ||
| 230 | if ($response->{success}) { | ||
| 231 | my $data = decode_json($response->{content}); | ||
| 232 | @links = map { $_->{href} } grep { ($_->{rel} // '') eq 'related' } @{ $data->{links} // [] }; | ||
| 233 | }; | ||
| 234 | if (scalar @links > 0) { | ||
| 235 | $links[0] = $links[0] . '/' if untaint_tld_url_no_path( $links[0] ); | ||
| 236 | $link = $links[0] if untaint_tld_url_with_path( $links[0] ); | ||
| 237 | if ( $link ) { | ||
| 238 | $result{tld} = $tld; | ||
| 239 | $result{url} = $link; | ||
| 240 | $result{source} = $SOURCE_IANA_RDAP_STR if $include_source; | ||
| 241 | } | ||
| 242 | } | ||
| 243 | return \%result; | ||
| 244 | } | ||
| 245 | |||
| 246 | 1; |