| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 10 | |
| 11 | 1. **Origin Attribution**: |
| 12 | - You must not misrepresent the origin of the Software; you must not claim |
| 13 | you created the original Software. |
| 14 | - If the Software is used in a product, you must either: |
| 15 | a. Provide clear attribution in the product's documentation, user interface, |
| 16 | or other visible areas, **OR** |
| 17 | b. Pay the original developers a fee they specify in writing. |
| 18 | 2. **Usage Restriction**: |
| 19 | - The Software, or any derivative works, dependencies, or libraries |
| 20 | incorporating it, must not be used for censorship or to suppress freedom of |
| 21 | speech, expression, or creativity. Prohibited uses include, but are not |
| 22 | limited to: |
| 23 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 24 | ideas, or objective reality. |
| 25 | - Tools or systems designed to restrict access to information or |
| 26 | artistic works. |
| 27 | 3. **Notice Preservation**: |
| 28 | - This license and the above copyright notice must remain intact in all copies |
| 29 | of the source code. |
| 30 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | // テスト用の偽物のSMTPサーバー(MockServerが起動する)。使い方:php smtp-server.php ポート 記録ファイル [証明書 秘密鍵] |
| 39 | // 受け取ったコマンドとメール本文を記録ファイルに書く。証明書と秘密鍵を渡すとSTARTTLSに対応する(Std\Lib\Tls)。 |
| 40 | |
| 41 | require_once __DIR__.'/../../../../autoload.php'; |
| 42 | |
| 43 | use Std\Lib\Tls; |
| 44 | |
| 45 | $port = (int)($argv[1] ?? 0); |
| 46 | $log = $argv[2] ?? ''; |
| 47 | $tls = isset($argv[3], $argv[4]) ? ['cert' => $argv[3], 'key' => $argv[4]] : null; |
| 48 | $server = @stream_socket_server("tcp://127.0.0.1:{$port}", $errno, $errstr); |
| 49 | if (!$server || $log === '') { |
| 50 | fwrite(STDOUT, "error {$errstr}\n"); |
| 51 | exit(1); |
| 52 | } |
| 53 | fwrite(STDOUT, "ready\n"); |
| 54 | fflush(STDOUT); |
| 55 | |
| 56 | while (true) { |
| 57 | $c = @stream_socket_accept($server, 60); |
| 58 | if (!$c) break; |
| 59 | stream_set_timeout($c, 5); |
| 60 | |
| 61 | $first = true; |
| 62 | $quit = false; |
| 63 | $secure = false; |
| 64 | fwrite($c, "220 mock ESMTP\r\n"); |
| 65 | $auth = 0; |
| 66 | while (($line = fgets($c)) !== false) { |
| 67 | if ($first && str_starts_with($line, 'QUIT /__quit')) { |
| 68 | $quit = true; |
| 69 | break; |
| 70 | } |
| 71 | $first = false; |
| 72 | file_put_contents($log, "C: {$line}", FILE_APPEND); |
| 73 | $cmd = strtoupper(substr(trim($line), 0, 4)); |
| 74 | |
| 75 | if ($auth > 0) { |
| 76 | $auth--; |
| 77 | fwrite($c, $auth > 0 ? "334 UGFzc3dvcmQ6\r\n" : "235 2.7.0 OK\r\n"); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | switch (true) { |
| 82 | case $cmd === 'EHLO': |
| 83 | fwrite($c, "250-mock\r\n250-8BITMIME\r\n".($tls && !$secure ? "250-STARTTLS\r\n" : '')."250 AUTH LOGIN\r\n"); |
| 84 | break; |
| 85 | case $cmd === 'STAR' && $tls && !$secure: |
| 86 | fwrite($c, "220 Ready to start TLS\r\n"); |
| 87 | try { |
| 88 | $c = Tls::accept($c, $tls); |
| 89 | } catch (\Throwable $e) { |
| 90 | file_put_contents($log, "TLS: {$e->getMessage()}\n", FILE_APPEND); |
| 91 | break 2; |
| 92 | } |
| 93 | $secure = true; |
| 94 | file_put_contents($log, "TLS: ".Tls::info($c)['cipher']."\n", FILE_APPEND); |
| 95 | break; |
| 96 | case str_starts_with(strtoupper($line), 'AUTH LOGIN'): |
| 97 | $auth = 2; |
| 98 | fwrite($c, "334 VXNlcm5hbWU6\r\n"); |
| 99 | break; |
| 100 | case $cmd === 'MAIL': |
| 101 | fwrite($c, "250 OK\r\n"); |
| 102 | break; |
| 103 | case $cmd === 'RCPT': |
| 104 | fwrite($c, str_contains($line, 'reject') ? "550 No such user\r\n" : "250 OK\r\n"); |
| 105 | break; |
| 106 | case $cmd === 'DATA': |
| 107 | fwrite($c, "354 Go ahead\r\n"); |
| 108 | $data = ''; |
| 109 | while (($l = fgets($c)) !== false && $l !== ".\r\n") $data .= $l; |
| 110 | file_put_contents($log, "DATA:\n{$data}END DATA\n", FILE_APPEND); |
| 111 | fwrite($c, "250 Queued\r\n"); |
| 112 | break; |
| 113 | case $cmd === 'QUIT': |
| 114 | fwrite($c, "221 Bye\r\n"); |
| 115 | break 2; |
| 116 | default: |
| 117 | fwrite($c, "502 Unknown\r\n"); |
| 118 | } |
| 119 | } |
| 120 | fclose($c); |
| 121 | if ($quit) break; |
| 122 | } |