annotate doc/ucis.ano/mailgateway/dnmail.php @ 924:a5a310ee291e draft

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