Mercurial > hg > anonet-resdb
comparison doc/ucis.ano/mailgateway/dnmail.php @ 67:e8e0adb25a6d draft
Added mail gateway script
author | ivo <ivo@UFO-Net.nl> |
---|---|
date | Wed, 23 Jun 2010 18:41:39 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
66:70a0167e1b20 | 67:e8e0adb25a6d |
---|---|
1 #!/usr/bin/php | |
2 <?php | |
3 $client = $_SERVER['REMOTE_HOST']; | |
4 stream_set_timeout(STDIN, 10000); | |
5 stream_set_timeout(STDOUT, 10000); | |
6 println('220 Mailgate.VANet.org ready'); | |
7 | |
8 expect(STDIN, 'HELO '); | |
9 println('250 Hello'); | |
10 | |
11 expectMAILFROM: | |
12 $line = expect(STDIN, 'MAIL FROM:'); | |
13 if ($line === FALSE) return; | |
14 else if ($line === TRUE) goto expectMAILFROM; | |
15 else if (preg_match('/<([a-zA-Z0-9.-_]+)@([a-zA-Z0-9-.-]+)>/', $line, $frommatches) != 1) { | |
16 println('501 Syntax error'); | |
17 goto expectMAILFROM; | |
18 } | |
19 print("250 Sender accepted\r\n"); | |
20 | |
21 expectRCPTTO: | |
22 $line = expect(STDIN, 'RCPT TO:'); | |
23 if ($line === FALSE) return; | |
24 else if ($line === TRUE) goto expectMAILFROM; | |
25 else if (preg_match('/<([a-zA-Z0-9.-_]+)@([a-zA-Z0-9-.-]+)>/', $line, $tomatches) != 1) { | |
26 println('501 Syntax error'); | |
27 goto expectRCPTTO; | |
28 } | |
29 if (substr($tomatches[2], -19) == '.mailgate.vanet.org') { | |
30 $tomatches[2] = substr($tomatches[2], 0, -19); | |
31 } | |
32 if (substr($tomatches[2], -4) != '.ano' && substr($client, 0, 2) != '1.') { | |
33 println('550 Relay access denied'); | |
34 goto expectRCPTTO; | |
35 } | |
36 | |
37 $rcptto = $tomatches[1].'@'.$tomatches[2]; | |
38 if (substr($tomatches[2], -4) != '.ano') { | |
39 $mailfrom = $frommatches[1].'@'.$frommatches[2].'.mailgate.vanet.org'; | |
40 } else { | |
41 $mailfrom = $frommatches[1].'@'.$frommatches[2]; | |
42 } | |
43 | |
44 if (getmxrr($tomatches[2], $mxes)) { | |
45 $mx = $mxes[0]; | |
46 } else { | |
47 $mx = $tomatches[2]; | |
48 } | |
49 | |
50 $server = @fsockopen($mx, 25, $dummy, $dummy, 5000); | |
51 if ($server === FALSE) { | |
52 println('450 Could not connect to destination server: '.$tomatches[2]); | |
53 goto expectRCPTTO; | |
54 } | |
55 stream_set_timeout($server, 10000); | |
56 | |
57 if (expectremote($server, '220') === FALSE) { | |
58 fclose($server); | |
59 goto expectRCPTTO; | |
60 } | |
61 | |
62 println($server, 'HELO mailgate.vanet.org'); | |
63 if (expectremote($server, '250') === FALSE) { | |
64 fclose($server); | |
65 goto expectRCPTTO; | |
66 } | |
67 | |
68 println($server, 'MAIL FROM:<'.$mailfrom.'>'); | |
69 if (expectremote($server, '250') === FALSE) { | |
70 fclose($server); | |
71 goto expectRCPTTO; | |
72 } | |
73 | |
74 println($server, 'RCPT TO:<'.$rcptto.'>'); | |
75 if (expectremote($server, '250') === FALSE) { | |
76 fclose($server); | |
77 goto expectRCPTTO; | |
78 } | |
79 | |
80 print('250 Reciplient accepted: '.$rcptto); | |
81 | |
82 $write = NULL; | |
83 $except = NULL; | |
84 while (TRUE) { | |
85 $read = array(STDIN, $server); | |
86 stream_select($read, $write, $except, NULL); | |
87 if (in_array(STDIN, $read)) { | |
88 $line = fread(STDIN, 1024); | |
89 if ($line === NULL || !strlen($line)) break; | |
90 fwrite($server, $line); | |
91 } | |
92 if (in_array($server, $read)) { | |
93 $line = fread($server, 1024); | |
94 if ($line === NULL || !strlen($line)) break; | |
95 fwrite(STDOUT, $line); | |
96 } | |
97 } | |
98 fclose($server); | |
99 | |
100 function println($stream, $str = NULL) { | |
101 if ($str === NULL) { $str = $stream; $stream = STDOUT; } | |
102 fwrite($stream, $str."\r\n"); | |
103 } | |
104 function expect($stream, $expect) { | |
105 while (TRUE) { | |
106 if (feof($stream)) return FALSE; | |
107 $line = @stream_get_line($stream, 1024, "\r\n"); | |
108 if ($line === FALSE) return FALSE; | |
109 if (substr($line, 0, strlen($expect)) == $expect) return $line; | |
110 switch (substr($line, 0, 4)) { | |
111 case 'HELO': case 'DATA': case 'MAIL': case 'RCPT': | |
112 println('503 Bad sequence of commands'); | |
113 return TRUE; | |
114 } | |
115 println('500 Command not recognized'); | |
116 } | |
117 } | |
118 function expectremote($stream, $expect) { | |
119 while (TRUE) { | |
120 if (feof($stream)) { | |
121 println('450 End of file from remote host'); | |
122 return FALSE; | |
123 } | |
124 $line = @stream_get_line($stream, 1024, "\r\n"); | |
125 if ($line === FALSE) { | |
126 println('450 Error while reading from remote host'); | |
127 return FALSE; | |
128 } | |
129 if (substr($line, 0, 3) != $expect) { | |
130 println('450'.$line[3].'Remote error: '.$line); | |
131 if ($line[3] != '-') return FALSE; | |
132 } else { | |
133 if ($line[3] != '-') return $line; | |
134 } | |
135 } | |
136 } |