| 1 | package AnyEvent::MCP::IMAP::Providers; | ||
| 2 | |||
| 3 | use common::sense; | ||
| 4 | use HTTP::Tiny (); | ||
| 5 | use JSON::XS (); | ||
| 6 | |||
| 7 | my $oauth_source = "https://raw.githubusercontent.com/mozilla/releases-comm-central/master/mailnews/base/src/OAuth2Providers.sys.mjs"; | ||
| 8 | my $ispdb = "https://autoconfig.thunderbird.net/"; | ||
| 9 | |||
| 10 | sub read_file { | ||
| 11 | my ($file) = @_; | ||
| 12 | open my $fh, "<:raw", $file or die "$file: $!\n"; | ||
| 13 | local $/; | ||
| 14 | <$fh> | ||
| 15 | } | ||
| 16 | |||
| 17 | sub download { | ||
| 18 | my ($url) = @_; | ||
| 19 | my $res = HTTP::Tiny->new ( | ||
| 20 | timeout => 8, verify_SSL => 1, max_redirect => 0, max_size => 1_000_000, | ||
| 21 | )->get ($url); | ||
| 22 | $res->{success} ? $res->{content} : undef | ||
| 23 | } | ||
| 24 | |||
| 25 | sub parse_index { | ||
| 26 | my ($json) = @_; | ||
| 27 | my $domains = JSON::XS::decode_json ($json); | ||
| 28 | ref $domains eq "ARRAY" or die "Invalid Thunderbird ISPDB index\n"; | ||
| 29 | [grep !ref && valid_domain ($_) && !/\.test\z/, @$domains] | ||
| 30 | } | ||
| 31 | |||
| 32 | # Only recognize Thunderbird's declarative literal data; never execute JavaScript. | ||
| 33 | sub parse_oauth { | ||
| 34 | my ($source) = @_; | ||
| 35 | |||
| 36 | my %scopes; | ||
| 37 | while ($source =~ /^const\s+([A-Z_]+_SCOPES)\s*=\s*(\{.*?^\};|"[^"]*";)/msg) { | ||
| 38 | my ($name, $value) = ($1, $2); | ||
| 39 | if ($value =~ /^\{/) { | ||
| 40 | my %scope = $value =~ /\b(imap|extra)\s*:\s*"([^"]*)"/g; | ||
| 41 | $scopes{$name} = \%scope; | ||
| 42 | } else { | ||
| 43 | ($scopes{$name}) = $value =~ /^"([^"]*)"/; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my ($hostnames) = $source =~ /\bvar\s+kHostnames\s*=\s*new Map\(\[\s*(.*?)^\]\);/ms | ||
| 48 | or die "Thunderbird kHostnames layout changed\n"; | ||
| 49 | my ($issuers) = $source =~ /\bvar\s+kIssuers\s*=\s*new Map\(\[\s*(.*?)^\]\);/ms | ||
| 50 | or die "Thunderbird kIssuers layout changed\n"; | ||
| 51 | |||
| 52 | my %issuer; | ||
| 53 | while ($issuers =~ /\[\s*"([^"]+)"\s*,\s*\{\s*(.*?)^\s*\},?\s*\]/msg) { | ||
| 54 | my ($name, $fields) = ($1, $2); | ||
| 55 | my %fields; | ||
| 56 | while ($fields =~ /\b(clientId|clientSecret|authorizationEndpoint|tokenEndpoint|redirectionEndpoint|usePKCE)\s*:\s*(?:"([^"]*)"|\x60([^\x60]*)\x60|(true|false))/g) { | ||
| 57 | $fields{$1} = defined $2 ? $2 : defined $3 ? $3 : $4; | ||
| 58 | } | ||
| 59 | $issuer{$name} = \%fields; | ||
| 60 | } | ||
| 61 | %issuer or die "Thunderbird kIssuers entries not recognized\n"; | ||
| 62 | |||
| 63 | my %result; | ||
| 64 | while ($hostnames =~ /\[\s*"([^"]+)"\s*,\s*\[\s*"([^"]+)"\s*,\s*(?:"([^"]*)"|([A-Z_]+_SCOPES)(?:\.(\w+))?)\s*\]\s*\]/mg) { | ||
| 65 | my ($host, $name, $literal, $constant, $field) = ($1, $2, $3, $4, $5); | ||
| 66 | my $scope = defined $literal ? $literal : $scopes{$constant}; | ||
| 67 | $scope = $scope->{$field || "imap"} . (defined $scope->{extra} ? " $scope->{extra}" : "") | ||
| 68 | if ref $scope eq "HASH" && (!$field || $field eq "imap"); | ||
| 69 | next if ref $scope || !defined $scope || !length $scope; | ||
| 70 | my $data = $issuer{$name} or next; | ||
| 71 | $data->{clientId} && $data->{authorizationEndpoint} && $data->{tokenEndpoint} or next; | ||
| 72 | $result{$host} = { %$data, issuer => $name, scope => $scope }; | ||
| 73 | } | ||
| 74 | |||
| 75 | %result or die "Thunderbird hostname/scope entries not recognized\n"; | ||
| 76 | \%result | ||
| 77 | } | ||
| 78 | |||
| 79 | sub parse_ispdb { | ||
| 80 | my ($xml, $email) = @_; | ||
| 81 | require XML::LibXML; | ||
| 82 | length $xml <= 1_000_000 or die "Autoconfig XML too large\n"; | ||
| 83 | my $doc = XML::LibXML->load_xml ( | ||
| 84 | string => $xml, no_network => 1, load_ext_dtd => 0, expand_entities => 0, | ||
| 85 | ); | ||
| 86 | $doc->documentElement->nodeName eq "clientConfig" | ||
| 87 | or die "Not a Thunderbird autoconfig document\n"; | ||
| 88 | |||
| 89 | my @servers; | ||
| 90 | for my $server ($doc->findnodes ("/clientConfig/emailProvider/incomingServer[\@type='imap']")) { | ||
| 91 | my %info = ( | ||
| 92 | server => $server->findvalue ("./hostname"), | ||
| 93 | port => $server->findvalue ("./port"), | ||
| 94 | tls => $server->findvalue ("./socketType"), | ||
| 95 | user => $server->findvalue ("./username"), | ||
| 96 | ); | ||
| 97 | my @auth = map $_->textContent, $server->findnodes ("./authentication"); | ||
| 98 | my @methods; | ||
| 99 | push @methods, "oauth2" if grep $_ eq "OAuth2", @auth; | ||
| 100 | push @methods, "password" if grep $_ eq "password-cleartext", @auth; | ||
| 101 | next unless @methods; | ||
| 102 | $info{tls} = $info{tls} eq "SSL" ? "connect" : $info{tls} eq "STARTTLS" ? "starttls" : next; | ||
| 103 | $info{port} =~ /^\d+$/ && $info{port} >= 1 && $info{port} <= 65535 or next; | ||
| 104 | if (defined $email) { | ||
| 105 | my ($local, $domain) = split /\@/, $email, 2; | ||
| 106 | for my $key (qw(server user)) { | ||
| 107 | $info{$key} =~ s/%EMAILADDRESS%/$email/g; | ||
| 108 | $info{$key} =~ s/%EMAILLOCALPART%/$local/g; | ||
| 109 | $info{$key} =~ s/%EMAILDOMAIN%/$domain/g; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | delete $info{user} if $info{user} =~ /%/; | ||
| 113 | next if $info{server} !~ /\A[A-Za-z0-9.-]+\z/; | ||
| 114 | $info{methods} = \@methods; | ||
| 115 | push @servers, \%info; | ||
| 116 | } | ||
| 117 | +{ | ||
| 118 | name => scalar $doc->findvalue ("/clientConfig/emailProvider/displayName"), | ||
| 119 | servers => \@servers, | ||
| 120 | notes => [map $_->textContent, $doc->findnodes ("/clientConfig/emailProvider/enable/instruction[not(\@lang)]")], | ||
| 121 | links => [map $_->getAttribute ("visiturl"), $doc->findnodes ("/clientConfig/emailProvider/enable[\@visiturl]")], | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | sub valid_domain { | ||
| 126 | $_[0] =~ /\A(?=.{1,253}\z)[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+\z/ | ||
| 127 | } | ||
| 128 | |||
| 129 | sub find_oauth { | ||
| 130 | my ($all, @hosts) = @_; | ||
| 131 | for my $host (@hosts) { | ||
| 132 | next unless defined $host; | ||
| 133 | return $all->{$host} if $all->{$host}; | ||
| 134 | for my $name (keys %$all) { | ||
| 135 | return $all->{$name} if $host eq "imap.$name"; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | undef | ||
| 139 | } | ||
| 140 | |||
| 141 | sub quote { | ||
| 142 | my ($s) = @_; | ||
| 143 | return $s if $s =~ /\A[-A-Za-z0-9_@%+=:,\.\/]+\z/; | ||
| 144 | $s =~ s/'/'\\''/g; | ||
| 145 | "'$s'" | ||
| 146 | } | ||
| 147 | |||
| 148 | sub print_login { | ||
| 149 | my ($name, $server, $method, $oauth) = @_; | ||
| 150 | my @kv = (method => $method); | ||
| 151 | for my $key (qw(server tls port user)) { | ||
| 152 | push @kv, $key => $server->{$key} if defined $server->{$key}; | ||
| 153 | } | ||
| 154 | if ($method eq "oauth2") { | ||
| 155 | push @kv, "oauth-client-id" => $oauth->{clientId}; | ||
| 156 | push @kv, "oauth-client-secret" => $oauth->{clientSecret} if defined $oauth->{clientSecret}; | ||
| 157 | push @kv, "oauth-authorization-endpoint" => $oauth->{authorizationEndpoint}; | ||
| 158 | push @kv, "oauth-token-endpoint" => $oauth->{tokenEndpoint}; | ||
| 159 | push @kv, "oauth-scope" => $oauth->{scope}; | ||
| 160 | push @kv, "oauth-pkce" => $oauth->{usePKCE} && $oauth->{usePKCE} eq "true" ? 1 : 0; | ||
| 161 | my $redirect = $oauth->{redirectionEndpoint} || "http://127.0.0.1"; | ||
| 162 | $redirect .= ":1" if $redirect =~ m{\Ahttp://(?:127\.0\.0\.1|localhost)\z}; | ||
| 163 | push @kv, "oauth-redirect-uri" => $redirect; | ||
| 164 | } | ||
| 165 | print "mcp-imap --name ", quote ($name), " login", @kv ? " \\\n" : "\n"; | ||
| 166 | while (@kv) { | ||
| 167 | my ($key, $value) = splice @kv, 0, 2; | ||
| 168 | print " $key ", quote ($value), @kv ? " \\\n" : "\n"; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | sub run { | ||
| 173 | my ($name, $provider_file, $ispdb_file, @args) = @_; | ||
| 174 | @args <= 1 or die "Usage: mcp-imap [OPTION]... providers [SEARCH]\n"; | ||
| 175 | my $search = $args[0]; | ||
| 176 | my $email; | ||
| 177 | if (defined $search && $search =~ /\A([^\s\@]+)\@([A-Za-z0-9.-]+)\z/) { | ||
| 178 | $email = $search; | ||
| 179 | $search = $2; | ||
| 180 | } | ||
| 181 | |||
| 182 | my $source = defined $provider_file ? read_file ($provider_file) : download ($oauth_source); | ||
| 183 | my $oauth = defined $source ? parse_oauth ($source) : {}; | ||
| 184 | my $index = defined $provider_file || defined $ispdb_file ? [] : | ||
| 185 | parse_index (download ($ispdb . "generated_files.json") || "[]"); | ||
| 186 | my %known = map +($_ => 1), (@$index, keys %$oauth); | ||
| 187 | delete @known{grep /^(?:smtp|pop|pop3|carddav|carddav-personal|caldav|graph|www\.googleapis|apidata)\.|\.test\z/, keys %known}; | ||
| 188 | my @domains = sort grep !defined $search | ||
| 189 | || index (lc $_, lc $search) >= 0 | ||
| 190 | || ($oauth->{$_} && index (lc $oauth->{$_}{issuer}, lc $search) >= 0), keys %known; | ||
| 191 | |||
| 192 | my @exact = defined $search ? grep lc $_ eq lc $search, @domains : (); | ||
| 193 | @domains = @exact if @exact; | ||
| 194 | if (!defined $search || @domains > 1 || !@domains && !valid_domain ($search)) { | ||
| 195 | @domains or die "No matching providers\n"; | ||
| 196 | print "Thunderbird mail providers (", scalar @domains, " matches):\n"; | ||
| 197 | printf " %-32s %s\n", $_, $oauth->{$_} ? "OAuth: $oauth->{$_}{issuer}" : "ISPDB" | ||
| 198 | for @domains; | ||
| 199 | print "\nSpecify a domain or email address to generate login commands.\n"; | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | my $domain = @domains == 1 ? $domains[0] : $search; | ||
| 203 | valid_domain ($domain) or die "Invalid provider domain\n"; | ||
| 204 | |||
| 205 | my ($xml, $origin); | ||
| 206 | if (defined $ispdb_file) { | ||
| 207 | $xml = read_file ($ispdb_file); | ||
| 208 | $origin = "local XML ($ispdb_file)"; | ||
| 209 | } else { | ||
| 210 | for my $url ( | ||
| 211 | "https://autoconfig.$domain/mail/config-v1.1.xml", | ||
| 212 | "https://$domain/.well-known/autoconfig/mail/config-v1.1.xml", | ||
| 213 | $ispdb . "v1.1/$domain", | ||
| 214 | ) { | ||
| 215 | $xml = download ($url); | ||
| 216 | if (defined $xml) { | ||
| 217 | $origin = $url; | ||
| 218 | last; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | my $config = defined $xml ? parse_ispdb ($xml, $email) : | ||
| 224 | { name => "", servers => [], notes => [], links => [] }; | ||
| 225 | print "Provider: $domain", $config->{name} ? " ($config->{name})" : "", "\n"; | ||
| 226 | print "IMAP configuration: ", $origin || "not found", "\n"; | ||
| 227 | print "OAuth reference: $oauth_source\n" | ||
| 228 | if find_oauth ($oauth, $domain, map $_->{server}, @{$config->{servers}}); | ||
| 229 | print "No IMAP autoconfiguration found; server, TLS and port require manual setup.\n" | ||
| 230 | unless @{$config->{servers}}; | ||
| 231 | print "Setup: $_\n" for grep length, @{$config->{notes}}; | ||
| 232 | print "Setup URL: $_\n" for grep length, @{$config->{links}}; | ||
| 233 | |||
| 234 | my @servers = @{$config->{servers}}; | ||
| 235 | push @servers, { server => $domain =~ /^imap\./ ? $domain : undef, | ||
| 236 | methods => ["oauth2"] } | ||
| 237 | if !@servers && find_oauth ($oauth, $domain); | ||
| 238 | @servers or die "No supported IMAP configuration for $domain\n"; | ||
| 239 | |||
| 240 | for my $server (@servers) { | ||
| 241 | my $o = find_oauth ($oauth, $domain, $server->{server}); | ||
| 242 | print "\nIMAP $server->{server}:$server->{port} ($server->{tls}), authentication: ", | ||
| 243 | join (", ", @{$server->{methods}}), "\n" | ||
| 244 | if defined $server->{server} && defined $server->{port}; | ||
| 245 | for my $method (@{$server->{methods}}) { | ||
| 246 | if ($method eq "oauth2" && !$o) { | ||
| 247 | print "OAuth2 advertised, but no usable Thunderbird client registration was found.\n"; | ||
| 248 | next; | ||
| 249 | } | ||
| 250 | print "\n# ", $method eq "oauth2" ? "OAuth2" : "Password", ":\n"; | ||
| 251 | print "# Thunderbird's OAuth client registration may not be authorized for other applications.\n" | ||
| 252 | if $method eq "oauth2"; | ||
| 253 | print "# OAuth2 requires a refresh token; provider-specific offline access parameters may be needed.\n" | ||
| 254 | if $method eq "oauth2"; | ||
| 255 | print_login ($name, $server, $method, $o); | ||
| 256 | } | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | 1; |