Very ugly hack to add rudimentary error check to SMTP for invalid/unsupported auth...
[squirrelmail.git] / class / deliver / Deliver_SMTP.class.php
CommitLineData
e1ee60fe 1<?php
5b8fd093 2/**
3 * Deliver_SMTP.class.php
4 *
76911253 5 * Copyright (c) 1999-2003 The SquirrelMail Project Team
5b8fd093 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
8 * Delivery backend for the Deliver class.
9 *
10 * $Id$
11 */
e1ee60fe 12
0f85ddf9 13require_once(SM_PATH . 'class/deliver/Deliver.class.php');
e1ee60fe 14
15class Deliver_SMTP extends Deliver {
16
5fe73b9f 17 function preWriteToStream(&$s) {
18 if ($s) {
19 if ($s{0} == '.') $s = '.' . $s;
20 $s = str_replace("\n.","\n..",$s);
21 }
22 }
23
24 function initStream($message, $domain, $length=0, $host='', $port='', $user='', $pass='', $authpop=false) {
47a29326 25 global $use_smtp_tls,$smtp_auth_mech,$username,$key,$onetimepad;
26
27 if ($authpop) {
28 $this->authPop($host, '', $username, $pass);
5fe73b9f 29 }
47a29326 30
31 $rfc822_header = $message->rfc822_header;
5fe73b9f 32 $from = $rfc822_header->from[0];
33 $to = $rfc822_header->to;
34 $cc = $rfc822_header->cc;
47a29326 35 $bcc = $rfc822_header->bcc;
36
37 if (($use_smtp_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
38 $stream = fsockopen('tls://' . $host, $port, $errorNumber, $errorString);
39 } else {
40 $stream = fsockopen($host, $port, $errorNumber, $errorString);
41 }
5fe73b9f 42
5fe73b9f 43 if (!$stream) {
44 $this->dlv_msg = $errorString;
45 $this->dlv_ret_nr = $errorNumber;
46 return(0);
47 }
48 $tmp = fgets($stream, 1024);
49 if ($this->errorCheck($tmp, $stream)) {
50 return(0);
51 }
52
53 /* Lets introduce ourselves */
639c7164 54 if (( $smtp_auth_mech == 'cram-md5') or ( $smtp_auth_mech == 'digest-md5' )) {
47a29326 55 // Doing some form of non-plain auth
56 fputs($stream, "EHLO $domain\r\n");
57 $tmp = fgets($stream,1024);
58 if ($this->errorCheck($tmp,$stream)) {
59 return(0);
60 }
61 if ($smtp_auth_mech == 'cram-md5') {
62 fputs($stream, "AUTH CRAM-MD5\r\n");
63 } elseif ($smtp_auth_mech == 'digest-md5') {
64 fputs($stream, "AUTH DIGEST-MD5\r\n");
65 }
66 $tmp = fgets($stream,1024);
67
68 if ($this->errorCheck($tmp,$stream)) {
69 return(0);
70 }
71
72 // At this point, $tmp should hold "334 <challenge string>"
73 $chall = substr($tmp,4);
74 // Depending on mechanism, generate response string
75 if ($smtp_auth_mech == 'cram-md5') {
76 $response = cram_md5_response($username,$pass,$chall);
77 } elseif ($smtp_auth_mech == 'digest-md5') {
78 $response = digest_md5_response($username,$pass,$chall,'smtp',$host);
79 }
80 fputs($stream, $response);
81
82 // Let's see what the server had to say about that
83 $tmp = fgets($stream,1024);
84 if ($this->errorCheck($tmp,$stream)) {
85 return(0);
86 }
87
88 // CRAM-MD5 is done at this point. If DIGEST-MD5, there's a bit more to go
89 if ($smtp_auth_mech == 'digest-md5')
90 {
91 // $tmp contains rspauth, but I don't store that yet. (No need yet)
92 fputs($stream,"\r\n");
93 $tmp = fgets($stream,1024);
94
95 if ($this->errorCheck($tmp,$stream)) {
96 return(0);
97 }
98 }
99 // CRAM-MD5 and DIGEST-MD5 code ends here
100 } elseif ($smtp_auth_mech == 'none') {
101 // No auth at all, just send helo and then send the mail
102 fputs($stream, "HELO $domain\r\n");
103 $tmp = fgets($stream, 1024);
104 if ($this->errorCheck($tmp, $stream)) {
105 return(0);
106 }
fe0b18b3 107 } elseif ($smtp_auth_mech == 'login') {
108 // The LOGIN method
47a29326 109 fputs($stream, "EHLO $domain\r\n");
110 $tmp = fgets($stream, 1024);
111 if ($this->errorCheck($tmp, $stream)) {
112 return(0);
113 }
114 fputs($stream, "AUTH LOGIN\r\n");
115 $tmp = fgets($stream, 1024);
5fe73b9f 116
47a29326 117 if ($this->errorCheck($tmp, $stream)) {
118 return(0);
119 }
120 fputs($stream, base64_encode ($username) . "\r\n");
121 $tmp = fgets($stream, 1024);
122 if ($this->errorCheck($tmp, $stream)) {
123 return(0);
124 }
5fe73b9f 125
47a29326 126 fputs($stream, base64_encode($pass) . "\r\n");
127 $tmp = fgets($stream, 1024);
128 if ($this->errorCheck($tmp, $stream)) {
129 return(0);
130 }
aa358b7e 131 } else {
132 /* Right here, they've reached an unsupported auth mechanism.
133 This is the ugliest hack I've ever done, but it'll do till I can fix
134 things up better tomorrow. So tired... */
135 if ($this->errorCheck("535 Unable to use this auth type",$stream)) {
136 return(0);
137 }
138 }
5fe73b9f 139
140 /* Ok, who is sending the message? */
141 fputs($stream, 'MAIL FROM: <'.$from->mailbox.'@'.$from->host.">\r\n");
142 $tmp = fgets($stream, 1024);
143 if ($this->errorCheck($tmp, $stream)) {
144 return(0);
145 }
146
147 /* send who the recipients are */
148 for ($i = 0, $cnt = count($to); $i < $cnt; $i++) {
149 if (!$to[$i]->host) $to[$i]->host = $domain;
d1825c3c 150 if ($to[$i]->mailbox) {
151 fputs($stream, 'RCPT TO: <'.$to[$i]->mailbox.'@'.$to[$i]->host.">\r\n");
152 $tmp = fgets($stream, 1024);
153 if ($this->errorCheck($tmp, $stream)) {
154 return(0);
155 }
5fe73b9f 156 }
157 }
d1825c3c 158
5fe73b9f 159 for ($i = 0, $cnt = count($cc); $i < $cnt; $i++) {
d1825c3c 160 if (!$cc[$i]->host) $cc[$i]->host = $domain;
161 if ($cc[$i]->mailbox) {
162 fputs($stream, 'RCPT TO: <'.$cc[$i]->mailbox.'@'.$cc[$i]->host.">\r\n");
163 $tmp = fgets($stream, 1024);
164 if ($this->errorCheck($tmp, $stream)) {
165 return(0);
166 }
5fe73b9f 167 }
168 }
169 for ($i = 0, $cnt = count($bcc); $i < $cnt; $i++) {
d1825c3c 170 if (!$bcc[$i]->host) $bcc[$i]->host = $domain;
171 if ($bcc[$i]->mailbox) {
172 fputs($stream, 'RCPT TO: <'.$bcc[$i]->mailbox.'@'.$bcc[$i]->host.">\r\n");
173 $tmp = fgets($stream, 1024);
174 if ($this->errorCheck($tmp, $stream)) {
175 return(0);
176 }
5fe73b9f 177 }
178 }
179 /* Lets start sending the actual message */
180 fputs($stream, "DATA\r\n");
181 $tmp = fgets($stream, 1024);
182 if ($this->errorCheck($tmp, $stream)) {
183 return(0);
184 }
185 return $stream;
186 }
187
188 function finalizeStream($stream) {
189 fputs($stream, ".\r\n"); /* end the DATA part */
190 $tmp = fgets($stream, 1024);
191 $this->errorCheck($tmp, $stream);
192 if ($this->dlv_ret_nr != 250) {
193 return(0);
194 }
195 fputs($stream, "QUIT\r\n"); /* log off */
196 fclose($stream);
197 return true;
198 }
199
200 function errorCheck($line, $smtpConnection) {
201 global $color, $compose_new_win;
47a29326 202
5fe73b9f 203 /* Read new lines on a multiline response */
204 $lines = $line;
205 while(ereg("^[0-9]+-", $line)) {
206 $line = fgets($smtpConnection, 1024);
207 $lines .= $line;
208 }
209 /* Status: 0 = fatal
210 * 5 = ok
211 */
212 $err_num = substr($line, 0, strpos($line, " "));
213 switch ($err_num) {
214 case 500: $message = 'Syntax error; command not recognized';
215 $status = 0;
216 break;
217 case 501: $message = 'Syntax error in parameters or arguments';
218 $status = 0;
219 break;
220 case 502: $message = 'Command not implemented';
221 $status = 0;
222 break;
223 case 503: $message = 'Bad sequence of commands';
224 $status = 0;
225 break;
226 case 504: $message = 'Command parameter not implemented';
227 $status = 0;
228 break;
229 case 211: $message = 'System status, or system help reply';
230 $status = 5;
231 break;
232 case 214: $message = 'Help message';
233 $status = 5;
234 break;
235 case 220: $message = 'Service ready';
236 $status = 5;
237 break;
238 case 221: $message = 'Service closing transmission channel';
239 $status = 5;
240 break;
47a29326 241 case 421: $message = 'Service not available, closing channel';
5fe73b9f 242 $status = 0;
243 break;
47a29326 244 case 235: $message = 'Authentication successful';
245 $status = 5;
246 break;
5fe73b9f 247 case 250: $message = 'Requested mail action okay, completed';
248 $status = 5;
249 break;
250 case 251: $message = 'User not local; will forward';
251 $status = 5;
252 break;
47a29326 253 case 334: $message = 'OK - continue request';
254 $status = 5;
255 break;
5fe73b9f 256 case 450: $message = 'Requested mail action not taken: mailbox unavailable';
257 $status = 0;
258 break;
259 case 550: $message = 'Requested action not taken: mailbox unavailable';
260 $status = 0;
261 break;
262 case 451: $message = 'Requested action aborted: error in processing';
263 $status = 0;
264 break;
265 case 551: $message = 'User not local; please try forwarding';
266 $status = 0;
267 break;
268 case 452: $message = 'Requested action not taken: insufficient system storage';
269 $status = 0;
270 break;
271 case 552: $message = 'Requested mail action aborted: exceeding storage allocation';
272 $status = 0;
273 break;
274 case 553: $message = 'Requested action not taken: mailbox name not allowed';
275 $status = 0;
276 break;
277 case 354: $message = 'Start mail input; end with .';
278 $status = 5;
279 break;
280 case 554: $message = 'Transaction failed';
281 $status = 0;
282 break;
283 /* RFC 2554 */
284 case 432: $message = 'A password transition is needed';
285 $status = 0;
286 break;
287 case 534: $message = 'Authentication mechanism is too weak';
288 $status = 0;
289 break;
290 case 538: $message = 'Encryption required for requested authentication mechanism';
291 $status = 0;
292 break;
293 case 454: $message = 'Temmporary authentication failure';
294 $status = 0;
295 break;
296 case 530: $message = 'Authentication required';
297 $status = 0;
298 break;
299 /* end RFC2554 */
47a29326 300 case 535: $message = 'Authentication failed';
301 $status = 0;
302 break;
5fe73b9f 303 default: $message = 'Unknown response: '. nl2br(htmlspecialchars($lines));
304 $status = 0;
305 $err_num = '001';
306 break;
307 }
308 $this->dlv_ret_nr = $err_num;
309 $this->dlv_msg = $message;
310 if ($status == 5) {
311 return false;
312 }
313 return true;
314 }
315
316 function authPop($pop_server='', $pop_port='', $user, $pass) {
317 if (!$pop_port) {
318 $pop_port = 110;
319 }
320 if (!$pop_server) {
321 $pop_server = 'localhost';
322 }
323 $popConnection = fsockopen($pop_server, $pop_port, $err_no, $err_str);
324 if (!$popConnection) {
325 error_log("Error connecting to POP Server ($pop_server:$pop_port)"
326 . " $err_no : $err_str");
327 } else {
328 $tmp = fgets($popConnection, 1024); /* banner */
329 if (!eregi("^\+OK", $tmp, $regs)) {
330 return(0);
331 }
332 fputs($popConnection, "USER $user\r\n");
333 $tmp = fgets($popConnection, 1024);
334 if (!eregi("^\+OK", $tmp, $regs)) {
335 return(0);
336 }
337 fputs($popConnection, 'PASS ' . $pass . "\r\n");
338 $tmp = fgets($popConnection, 1024);
339 if (!eregi("^\+OK", $tmp, $regs)) {
340 return(0);
341 }
342 fputs($popConnection, "QUIT\r\n"); /* log off */
343 fclose($popConnection);
344 }
345 }
e1ee60fe 346}
5fe73b9f 347
e1ee60fe 348?>