Re: Fwd: a PATCH to Net_SMTP-1.0 and update

From: Date: Sun, 05 Jan 2003 06:48:48 +0000
Subject: Re: Fwd: a PATCH to Net_SMTP-1.0 and update
References: 1 2 3 4 5  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-12121@lists.php.net to get a copy of this message
On Fri, Jan 03, 2003 at 01:09:35AM -0300, Damian Alejandro Fernandez Sosa wrote: > STMP.php_SIZE_CONTROL: checks the size you're going to send to the SIZE > capability (if that size is equal or greater that the server's > capability , Dont allow to send the email (because you only are wasting > network resourses) ) This patch has been applied. > SMTP.php_auth_check: lets you choose the auth method to authenticate to > the server from the available list of methods (LOGIN AND PLAIN for now, > CRAM-MD5 and DIGEST-MD5 later). > Also, negociates an auth method with the server. I've rewritten a bit of your patch for general cleanliness. I'm not entirely ready to commit it yet, but I've attached my version for you to test / review. > Once those changes are commited I'm going to use this class as base to > make Net_LMTP class to allow access to LMTP servers Great! -- Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)

Index: SMTP.php =================================================================== RCS file: /repository/pear/Net_SMTP/Net/SMTP.php,v retrieving revision 1.5 diff -u -r1.5 SMTP.php --- SMTP.php 5 Jan 2003 06:08:28 -0000 1.5 +++ SMTP.php 5 Jan 2003 06:46:16 -0000 @@ -14,6 +14,7 @@ // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Chuck Hagenbuch <chuck@horde.org> | +// | Jon Parise <jon@php.net> | // +----------------------------------------------------------------------+ require_once 'PEAR.php'; @@ -67,6 +68,12 @@ var $lastline; /** + * The list of supported authentication methods, ordered by preference. + * @var string + */ + var $_auth_methods = array('LOGIN', 'PLAIN'); + + /** * Constructor * * Instantiates a new Net_SMTP object, overriding any defaults @@ -134,25 +141,91 @@ } /** + * Returns the name of the best authentication method that the server + * has advertised. + * + * @return mixed Returns a string containing the name of the best + * supported authentication method or a PEAR_Error object + * if a failure condition is encountered. + * @access private + */ + function _getBestAuthMethod() + { + $available_methods = explode(' ', $this->esmtp['AUTH']); + + foreach ($this->_auth_methods as $method) { + if (in_array($method, $available_methods)) { + return $method; + } + } + + return new PEAR_Error('No supported authentication methods'); + } + + /** * Attempt to do SMTP authentication. * * @param string The userid to authenticate as. * @param string The password to authenticate with. + * @param string The requested authentication method. If none is + * specified, the best supported method will be used. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function auth($uid, $pwd) + function auth($uid, $pwd , $method = '') { - /* Note: not currently checking if AUTH LOGIN is allowed */ - /* Note: only allows one authentication mechanism */ + if (!array_key_exists('AUTH', $this->esmtp)) { + return new PEAR_Error('SMTP server does no support authentication'); + } - if (!isset($this->esmtp['AUTH'])) { - return new PEAR_Error('auth not supported'); + /* + * If no method has been specified, get the name of the best supported + * method advertised by the SMTP server. + */ + if (empty($method)) { + if (PEAR::isError($method = $this->_getBestAuthMethod())) { + /* Return the PEAR_Error object from _getBestAuthMethod() */ + return $method; + } + } else { + $method = strtoupper($method); + } + + switch ($method) { + case 'LOGIN': + $result = $this->_authLogin($uid, $pwd); + break; + case 'PLAIN': + $result = $this->_authPlain($uid, $pwd); + break; + default : + $result = new PEAR_Error("$method is not a supported authentication method"); + break; + } + + /* If an error was encountered, return the PEAR_Error object. */ + if (PEAR::isError($result)) { + return $result; } - if (PEAR::isError($this->socket->write("AUTH LOGIN\r\n"))) { + return true; + } + + /** + * Authenticates the user using the LOGIN method. + * + * @param string The userid to authenticate as. + * @param string The password to authenticate with. + * + * @return mixed Returns a PEAR_Error with an error message on any + * kind of failure, or true on success. + * @access private + */ + function _authLogin($uid, $pwd) + { + if (PEAR::isError($this->socket->write("AUTH LOGIN\r\n"))) { return new PEAR_Error('write to socket failed'); } if (!$this->validateResponse('334')) { @@ -170,6 +243,36 @@ return new PEAR_Error('write to socket failed'); } if (!$this->validateResponse('235')) { + return new PEAR_Error('235 not received'); + } + + return true; + } + + /** + * Authenticates the user using the PLAIN method. + * + * @param string The userid to authenticate as. + * @param string The password to authenticate with. + * + * @return mixed Returns a PEAR_Error with an error message on any + * kind of failure, or true on success. + * @access private + */ + function _authPlain($uid, $pwd) + { + if (PEAR::isError($this->socket->write("AUTH PLAIN\r\n"))) { + return new PEAR_Error('write to socket failed'); + } + if (!$this->validateResponse('334')) { + return new PEAR_Error('AUTH LOGIN not recognized'); + } + + $auth_str = base64_encode(chr(0) . $uid . chr(0) . $pwd); + if (PEAR::isError($this->socket->write($auth_str . "\r\n"))) { + return new PEAR_Error('write to socket failed'); + } + if (!$this->validateResponse('235')) { return new PEAR_Error('235 not received'); }
« previous php.pear.dev (#12121) next »