|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * anyhost: sign in to any IMAP server from one Roundcube. |
| 5 | + * |
| 6 | + * The login form shows a free-text Server field (imap_host is '' in |
| 7 | + * zz-profullstack.inc.php). This plugin |
| 8 | + * - defaults a blank server to Forward Email, |
| 9 | + * - turns "mail.example.com" or "mail.example.com:143" into a Roundcube host URL |
| 10 | + * (ssl://host:993, or tls://host:143 for STARTTLS), |
| 11 | + * - refuses hosts that resolve to private, loopback or link-local addresses and ports |
| 12 | + * other than 993/143, so the form cannot be used to reach the box's own services, |
| 13 | + * - picks the SMTP server for the session: smtp.<rest> when the IMAP host is |
| 14 | + * imap.<rest>, then the IMAP host itself, each tried on 465 (ssl) then 587 (tls). |
| 15 | + * |
| 16 | + * Source: profullstack/cli-tools dev2/webmail/anyhost (deployed to volumes/plugin-anyhost). |
| 17 | + */ |
| 18 | +class anyhost extends rcube_plugin |
| 19 | +{ |
| 20 | + const DEFAULT_IMAP = 'ssl://imap.forwardemail.net:993'; |
| 21 | + const DEFAULT_SMTP = 'ssl://smtp.forwardemail.net:465'; |
| 22 | + |
| 23 | + public function init() |
| 24 | + { |
| 25 | + $this->add_hook('authenticate', [$this, 'authenticate']); |
| 26 | + $this->add_hook('smtp_connect', [$this, 'smtp_connect']); |
| 27 | + $this->add_hook('template_object_loginform', [$this, 'loginform']); |
| 28 | + } |
| 29 | + |
| 30 | + public function loginform($args) |
| 31 | + { |
| 32 | + // A hint in the empty Server field. |
| 33 | + $args['content'] = preg_replace( |
| 34 | + '/(<input[^>]*name="_host"[^>]*?)(\s*\/?>)/', |
| 35 | + '$1 placeholder="imap.forwardemail.net (default)"$2', |
| 36 | + $args['content'] |
| 37 | + ); |
| 38 | + return $args; |
| 39 | + } |
| 40 | + |
| 41 | + public function authenticate($args) |
| 42 | + { |
| 43 | + $raw = trim((string) ($args['host'] ?? '')); |
| 44 | + if ($raw === '') { |
| 45 | + $args['host'] = self::DEFAULT_IMAP; |
| 46 | + return $args; |
| 47 | + } |
| 48 | + |
| 49 | + $host = self::parse($raw, 993); |
| 50 | + if (!$host) { |
| 51 | + return self::abort($args); |
| 52 | + } |
| 53 | + [$name, $port] = $host; |
| 54 | + if (!in_array($port, [993, 143], true) || !self::public_host($name)) { |
| 55 | + return self::abort($args); |
| 56 | + } |
| 57 | + $args['host'] = ($port === 143 ? 'tls://' : 'ssl://') . $name . ':' . $port; |
| 58 | + return $args; |
| 59 | + } |
| 60 | + |
| 61 | + public function smtp_connect($args) |
| 62 | + { |
| 63 | + $rcmail = rcmail::get_instance(); |
| 64 | + $imap = (string) ($_SESSION['storage_host'] ?? ''); |
| 65 | + if ($imap === '' || $imap === 'imap.forwardemail.net') { |
| 66 | + $args['smtp_host'] = self::DEFAULT_SMTP; |
| 67 | + return $args; |
| 68 | + } |
| 69 | + if (empty($_SESSION['anyhost_smtp'])) { |
| 70 | + $_SESSION['anyhost_smtp'] = self::find_smtp($imap) ?: ('ssl://' . $imap . ':465'); |
| 71 | + } |
| 72 | + $args['smtp_host'] = $_SESSION['anyhost_smtp']; |
| 73 | + $args['smtp_user'] = $rcmail->get_user_name(); |
| 74 | + $args['smtp_pass'] = $rcmail->get_user_password(); |
| 75 | + return $args; |
| 76 | + } |
| 77 | + |
| 78 | + private static function find_smtp($imap) |
| 79 | + { |
| 80 | + $candidates = []; |
| 81 | + if (stripos($imap, 'imap.') === 0) { |
| 82 | + $candidates[] = 'smtp.' . substr($imap, 5); |
| 83 | + } |
| 84 | + $candidates[] = $imap; |
| 85 | + foreach ($candidates as $h) { |
| 86 | + if (!self::public_host($h)) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + foreach ([465 => 'ssl', 587 => 'tls'] as $port => $scheme) { |
| 90 | + $fp = @fsockopen($h, $port, $errno, $errstr, 4); |
| 91 | + if ($fp) { |
| 92 | + fclose($fp); |
| 93 | + return "$scheme://$h:$port"; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + /** [hostname, port] from "host", "host:port" or "ssl://host:port"; null when malformed. */ |
| 101 | + private static function parse($raw, $default_port) |
| 102 | + { |
| 103 | + $raw = preg_replace('#^[a-z]+://#i', '', $raw); |
| 104 | + if (!preg_match('/^([a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+)(:(\d{1,5}))?$/i', $raw, $m)) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + return [strtolower($m[1]), isset($m[6]) ? (int) $m[6] : $default_port]; |
| 108 | + } |
| 109 | + |
| 110 | + /** Every address the name resolves to is public (no RFC 1918, loopback, link-local, ULA). */ |
| 111 | + private static function public_host($name) |
| 112 | + { |
| 113 | + $ips = []; |
| 114 | + foreach ((array) @dns_get_record($name, DNS_A | DNS_AAAA) as $r) { |
| 115 | + if (!empty($r['ip'])) $ips[] = $r['ip']; |
| 116 | + if (!empty($r['ipv6'])) $ips[] = $r['ipv6']; |
| 117 | + } |
| 118 | + if (!$ips) { |
| 119 | + $ips = (array) @gethostbynamel($name); |
| 120 | + } |
| 121 | + if (!$ips) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + foreach ($ips as $ip) { |
| 125 | + if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + private static function abort($args) |
| 133 | + { |
| 134 | + $args['abort'] = true; |
| 135 | + $args['error'] = 'That mail server is not allowed. Use its public hostname, port 993 (or 143 for STARTTLS).'; |
| 136 | + return $args; |
| 137 | + } |
| 138 | +} |
0 commit comments