Skip to content

Commit bf7d01a

Browse files
author
ndm2
committed
Make SMTP auth reply code checks work properly.
1 parent b6eb562 commit bf7d01a

File tree

2 files changed

+95
-8
lines changed

2 files changed

+95
-8
lines changed

lib/Cake/Network/Email/SmtpTransport.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,22 @@ protected function _connect() {
191191
*/
192192
protected function _auth() {
193193
if (isset($this->_config['username']) && isset($this->_config['password'])) {
194-
$authRequired = $this->_smtpSend('AUTH LOGIN', '334|503');
195-
if ($authRequired == '334') {
196-
if (!$this->_smtpSend(base64_encode($this->_config['username']), '334')) {
194+
$replyCode = $this->_smtpSend('AUTH LOGIN', '334|500|502|504');
195+
if ($replyCode == '334') {
196+
try {
197+
$this->_smtpSend(base64_encode($this->_config['username']), '334');
198+
} catch (SocketException $e) {
197199
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the username.'));
198200
}
199-
if (!$this->_smtpSend(base64_encode($this->_config['password']), '235')) {
201+
try {
202+
$this->_smtpSend(base64_encode($this->_config['password']), '235');
203+
} catch (SocketException $e) {
200204
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the password.'));
201205
}
202-
} elseif ($authRequired == '504') {
203-
throw new SocketException(__d('cake_dev', 'SMTP authentication method not allowed, check if SMTP server requires TLS'));
204-
} elseif ($authRequired != '503') {
205-
throw new SocketException(__d('cake_dev', 'SMTP does not require authentication.'));
206+
} elseif ($replyCode == '504') {
207+
throw new SocketException(__d('cake_dev', 'SMTP authentication method not allowed, check if SMTP server requires TLS.'));
208+
} else {
209+
throw new SocketException(__d('cake_dev', 'AUTH command not recognized or not implemented, SMTP server may not require authentication.'));
206210
}
207211
}
208212
}

lib/Cake/Test/Case/Network/Email/SmtpTransportTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,89 @@ public function testAuth() {
223223
$this->SmtpTransport->auth();
224224
}
225225

226+
/**
227+
* testAuthNotRecognized method
228+
*
229+
* @expectedException SocketException
230+
* @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
231+
* @return void
232+
*/
233+
public function testAuthNotRecognized() {
234+
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
235+
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
236+
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
237+
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
238+
$this->SmtpTransport->auth();
239+
}
240+
241+
/**
242+
* testAuthNotImplemented method
243+
*
244+
* @expectedException SocketException
245+
* @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
246+
* @return void
247+
*/
248+
public function testAuthNotImplemented() {
249+
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
250+
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
251+
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("502 5.3.3 Command not implemented\r\n"));
252+
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
253+
$this->SmtpTransport->auth();
254+
}
255+
256+
/**
257+
* testAuthBadSequence method
258+
*
259+
* @expectedException SocketException
260+
* @return void
261+
*/
262+
public function testAuthBadSequence() {
263+
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
264+
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
265+
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("503 5.5.1 Already authenticated\r\n"));
266+
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
267+
$this->SmtpTransport->auth();
268+
}
269+
270+
/**
271+
* testAuthBadUsername method
272+
*
273+
* @expectedException SocketException
274+
* @expectedExceptionMessage SMTP server did not accept the username.
275+
* @return void
276+
*/
277+
public function testAuthBadUsername() {
278+
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
279+
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
280+
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
281+
$this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
282+
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
283+
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
284+
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
285+
$this->SmtpTransport->auth();
286+
}
287+
288+
/**
289+
* testAuthBadPassword method
290+
*
291+
* @expectedException SocketException
292+
* @expectedExceptionMessage SMTP server did not accept the password.
293+
* @return void
294+
*/
295+
public function testAuthBadPassword() {
296+
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
297+
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
298+
$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
299+
$this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
300+
$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
301+
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
302+
$this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
303+
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
304+
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
305+
$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
306+
$this->SmtpTransport->auth();
307+
}
308+
226309
/**
227310
* testAuthNoAuth method
228311
*

0 commit comments

Comments
 (0)