Changeset 5902 in subversion


Ignore:
Timestamp:
Feb 24, 2012 5:17:19 AM (15 months ago)
Author:
alec
Message:
  • Fixed drivers namespace issues
Location:
trunk/plugins/password
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • trunk/plugins/password/drivers/chpasswd.php

    r4291 r5902  
    99 * For installation instructions please read the README file. 
    1010 * 
    11  * @version 1.0 
     11 * @version 2.0 
    1212 * @author Alex Cartwright <acartwright@mutinydesign.co.uk) 
    1313 */ 
    1414 
    15 function password_save($currpass, $newpass) 
     15class rcube_chpasswd_password 
    1616{ 
    17     $cmd = rcmail::get_instance()->config->get('password_chpasswd_cmd'); 
    18     $username = $_SESSION['username']; 
     17    public function save($currpass, $newpass) 
     18    { 
     19        $cmd = rcmail::get_instance()->config->get('password_chpasswd_cmd'); 
     20        $username = $_SESSION['username']; 
    1921 
    20     $handle = popen($cmd, "w"); 
    21     fwrite($handle, "$username:$newpass\n"); 
     22        $handle = popen($cmd, "w"); 
     23        fwrite($handle, "$username:$newpass\n"); 
    2224 
    23     if (pclose($handle) == 0) { 
    24         return PASSWORD_SUCCESS; 
     25        if (pclose($handle) == 0) { 
     26            return PASSWORD_SUCCESS; 
     27        } 
     28        else { 
     29            raise_error(array( 
     30                'code' => 600, 
     31                'type' => 'php', 
     32                'file' => __FILE__, 'line' => __LINE__, 
     33                'message' => "Password plugin: Unable to execute $cmd" 
     34                ), true, false); 
     35        } 
     36 
     37        return PASSWORD_ERROR; 
    2538    } 
    26     else { 
    27         raise_error(array( 
    28             'code' => 600, 
    29             'type' => 'php', 
    30             'file' => __FILE__, 'line' => __LINE__, 
    31             'message' => "Password plugin: Unable to execute $cmd" 
    32             ), true, false); 
    33     } 
    34  
    35     return PASSWORD_ERROR; 
    3639} 
  • trunk/plugins/password/drivers/cpanel.php

    r3008 r5902  
    88 * 
    99 * This driver has been tested with Hostmonster hosting and seems to work fine. 
    10  
    1110 * 
    12  * @version 1.0 
     11 * @version 2.0 
    1312 * @author Fulvio Venturelli <fulvio@venturelli.org> 
    1413 */ 
     14 
     15class rcube_cpanel_password 
     16{ 
     17    public function save($curpas, $newpass) 
     18    { 
     19        $rcmail = rcmail::get_instance(); 
     20 
     21        // Create a cPanel email object 
     22        $cPanel = new emailAccount($rcmail->config->get('password_cpanel_host'), 
     23        $rcmail->config->get('password_cpanel_username'), 
     24            $rcmail->config->get('password_cpanel_password'), 
     25        $rcmail->config->get('password_cpanel_port'), 
     26            $rcmail->config->get('password_cpanel_ssl'), 
     27        $rcmail->config->get('password_cpanel_theme'), 
     28            $_SESSION['username'] ); 
     29 
     30        if ($cPanel->setPassword($newpass)){ 
     31            return PASSWORD_SUCCESS; 
     32        } 
     33        else { 
     34            return PASSWORD_ERROR; 
     35        } 
     36    } 
     37} 
     38 
    1539 
    1640class HTTP 
     
    6185 
    6286class emailAccount 
    63 {           
     87{ 
    6488        function emailAccount($host, $username, $password, $port, $ssl, $theme, $address) 
    6589        { 
     
    7599        } 
    76100 
    77  /* 
    78   * Change email account password 
    79   * 
    80   * Returns true on success or false on failure. 
    81   * @param string $password email account password 
    82   * @return bool 
    83   */ 
     101    /** 
     102     * Change email account password 
     103     * 
     104     * Returns true on success or false on failure. 
     105     * @param string $password email account password 
     106     * @return bool 
     107     */ 
    84108        function setPassword($password) 
    85109        { 
     
    95119        } 
    96120} 
    97  
    98  
    99 function password_save($curpas, $newpass) 
    100 { 
    101     $rcmail = rcmail::get_instance(); 
    102  
    103     // Create a cPanel email object 
    104     $cPanel = new emailAccount($rcmail->config->get('password_cpanel_host'), 
    105         $rcmail->config->get('password_cpanel_username'), 
    106         $rcmail->config->get('password_cpanel_password'), 
    107         $rcmail->config->get('password_cpanel_port'), 
    108         $rcmail->config->get('password_cpanel_ssl'), 
    109         $rcmail->config->get('password_cpanel_theme'), 
    110         $_SESSION['username'] ); 
    111  
    112     if ($cPanel->setPassword($newpass)){ 
    113         return PASSWORD_SUCCESS; 
    114     } 
    115     else 
    116     { 
    117        return PASSWORD_ERROR; 
    118     } 
    119 } 
    120  
    121 ?> 
  • trunk/plugins/password/drivers/directadmin.php

    r4726 r5902  
    66 * Driver to change passwords via DirectAdmin Control Panel 
    77 * 
    8  * @version 1.2 
     8 * @version 2.0 
    99 * @author Victor Benincasa <vbenincasa@gmail.com> 
    1010 * 
    1111 */ 
    1212 
    13  
    14 function password_save($curpass, $passwd){ 
    15  
    16     $rcmail = rcmail::get_instance(); 
    17     $Socket = new HTTPSocket; 
    18  
    19     $da_user    = $_SESSION['username']; 
    20     $da_curpass = $curpass; 
    21     $da_newpass = $passwd; 
    22     $da_host    = $rcmail->config->get('password_directadmin_host'); 
    23     $da_port    = $rcmail->config->get('password_directadmin_port'); 
    24  
    25     if(strpos($da_user, '@') === false) return array('code' => PASSWORD_ERROR, 'message' => 'Change the SYSTEM user password through control panel!'); 
    26  
    27     $da_host = str_replace('%h', $_SESSION['imap_host'], $da_host); 
    28     $da_host = str_replace('%d', $rcmail->user->get_username('domain'), $da_host); 
    29  
    30     $Socket->connect($da_host,$da_port);  
    31     $Socket->set_method('POST'); 
    32     $Socket->query('/CMD_CHANGE_EMAIL_PASSWORD', 
    33         array( 
    34             'email'             => $da_user, 
    35             'oldpassword'       => $da_curpass, 
    36             'password1'         => $da_newpass, 
    37             'password2'         => $da_newpass, 
    38             'api'                       => '1' 
    39     )); 
    40     $response = $Socket->fetch_parsed_body(); 
    41  
    42     //DEBUG 
    43     //console("Password Plugin: [USER: $da_user] [HOST: $da_host] - Response: [SOCKET: ".$Socket->result_status_code."] [DA ERROR: ".strip_tags($response['error'])."] [TEXT: ".$response[text]."]"); 
    44  
    45     if($Socket->result_status_code != 200) 
    46         return array('code' => PASSWORD_CONNECT_ERROR, 'message' => $Socket->error[0]); 
    47     elseif($response['error'] == 1) 
    48         return array('code' => PASSWORD_ERROR, 'message' => strip_tags($response['text'])); 
    49     else  
    50         return PASSWORD_SUCCESS; 
    51  
     13class rcube_directadmin_password 
     14{ 
     15    public function save($curpass, $passwd) 
     16    { 
     17        $rcmail = rcmail::get_instance(); 
     18        $Socket = new HTTPSocket; 
     19 
     20        $da_user    = $_SESSION['username']; 
     21        $da_curpass = $curpass; 
     22        $da_newpass = $passwd; 
     23        $da_host    = $rcmail->config->get('password_directadmin_host'); 
     24        $da_port    = $rcmail->config->get('password_directadmin_port'); 
     25 
     26        if (strpos($da_user, '@') === false) { 
     27            return array('code' => PASSWORD_ERROR, 'message' => 'Change the SYSTEM user password through control panel!'); 
     28        } 
     29 
     30        $da_host = str_replace('%h', $_SESSION['imap_host'], $da_host); 
     31        $da_host = str_replace('%d', $rcmail->user->get_username('domain'), $da_host); 
     32 
     33        $Socket->connect($da_host,$da_port);  
     34        $Socket->set_method('POST'); 
     35        $Socket->query('/CMD_CHANGE_EMAIL_PASSWORD', 
     36            array( 
     37                'email'                 => $da_user, 
     38                'oldpassword'   => $da_curpass, 
     39                'password1'     => $da_newpass, 
     40                'password2'     => $da_newpass, 
     41                'api'                   => '1' 
     42            )); 
     43        $response = $Socket->fetch_parsed_body(); 
     44 
     45        //DEBUG 
     46        //console("Password Plugin: [USER: $da_user] [HOST: $da_host] - Response: [SOCKET: ".$Socket->result_status_code."] [DA ERROR: ".strip_tags($response['error'])."] [TEXT: ".$response[text]."]"); 
     47 
     48        if($Socket->result_status_code != 200) 
     49            return array('code' => PASSWORD_CONNECT_ERROR, 'message' => $Socket->error[0]); 
     50        elseif($response['error'] == 1) 
     51            return array('code' => PASSWORD_ERROR, 'message' => strip_tags($response['text'])); 
     52        else 
     53            return PASSWORD_SUCCESS; 
     54    } 
    5255} 
    5356 
     
    6972 
    7073    var $version = '2.7'; 
    71      
     74 
    7275    /* all vars are private except $error, $query_cache, and $doFollowLocationHeader */ 
    7376 
     
    170173            $this->connect($location['host'],$location['port']); 
    171174            $this->set_login($location['user'],$location['pass']); 
    172              
     175 
    173176            $request = $location['path']; 
    174177            $content = $location['query']; 
     
    323326 
    324327        } 
    325          
     328 
    326329        list($this->result_header,$this->result_body) = preg_split("/\r\n\r\n/",$this->result,2); 
    327330 
     
    362365            } 
    363366        } 
    364          
    365367    } 
    366368 
     
    446448    { 
    447449        $array_headers = preg_split("/\r\n/",$this->result_header); 
    448          
     450 
    449451        $array_return = array( 0 => $array_headers[0] ); 
    450452        unset($array_headers[0]); 
     
    486488 
    487489} 
    488  
    489 ?> 
  • trunk/plugins/password/drivers/hmail.php

    r4183 r5902  
    44 * hMailserver password driver 
    55 * 
    6  * @version 1.3 - 05.11.2010 
     6 * @version 2.0 
    77 * @author Roland 'rosali' Liebl <myroundcube@mail4us.net> 
    88 * 
    99 */ 
    1010 
    11 function password_save($curpass, $passwd) 
     11class rcube_hmail_password 
    1212{ 
    13     $rcmail = rcmail::get_instance(); 
     13    public function save($curpass, $passwd) 
     14    { 
     15        $rcmail = rcmail::get_instance(); 
    1416 
    15     if ($curpass == '' || $passwd == '') 
    16       return PASSWORD_ERROR; 
     17        if ($curpass == '' || $passwd == '') { 
     18            return PASSWORD_ERROR; 
     19        } 
    1720 
    18     try { 
    19       $remote = $rcmail->config->get('hmailserver_remote_dcom', false); 
    20       if ($remote) 
    21         $obApp = new COM("hMailServer.Application", $rcmail->config->get('hmailserver_server')); 
    22       else 
    23         $obApp = new COM("hMailServer.Application"); 
    24     } 
    25     catch (Exception $e) { 
    26         write_log('errors', "Plugin password (hmail driver): " . trim(strip_tags($e->getMessage()))); 
    27         write_log('errors', "Plugin password (hmail driver): This problem is often caused by DCOM permissions not being set."); 
    28         return PASSWORD_ERROR; 
    29     } 
     21        try { 
     22            $remote = $rcmail->config->get('hmailserver_remote_dcom', false); 
     23            if ($remote) 
     24                $obApp = new COM("hMailServer.Application", $rcmail->config->get('hmailserver_server')); 
     25            else 
     26                $obApp = new COM("hMailServer.Application"); 
     27        } 
     28        catch (Exception $e) { 
     29            write_log('errors', "Plugin password (hmail driver): " . trim(strip_tags($e->getMessage()))); 
     30            write_log('errors', "Plugin password (hmail driver): This problem is often caused by DCOM permissions not being set."); 
     31            return PASSWORD_ERROR; 
     32        } 
    3033 
    31     $username = $rcmail->user->data['username']; 
    32     if (strstr($username,'@')){ 
    33       $temparr = explode('@', $username); 
    34       $domain = $temparr[1]; 
    35     } 
    36     else { 
    37       $domain = $rcmail->config->get('username_domain',false); 
    38       if (!$domain) { 
    39         write_log('errors','Plugin password (hmail driver): $rcmail_config[\'username_domain\'] is not defined.'); 
    40         write_log('errors','Plugin password (hmail driver): Hint: Use hmail_login plugin (http://myroundcube.googlecode.com'); 
    41         return PASSWORD_ERROR; 
    42       } 
    43       $username = $username . "@" . $domain; 
    44     } 
     34        $username = $rcmail->user->data['username']; 
     35        if (strstr($username,'@')){ 
     36            $temparr = explode('@', $username); 
     37            $domain = $temparr[1]; 
     38        } 
     39        else { 
     40            $domain = $rcmail->config->get('username_domain',false); 
     41            if (!$domain) { 
     42                write_log('errors','Plugin password (hmail driver): $rcmail_config[\'username_domain\'] is not defined.'); 
     43                write_log('errors','Plugin password (hmail driver): Hint: Use hmail_login plugin (http://myroundcube.googlecode.com'); 
     44                return PASSWORD_ERROR; 
     45            } 
     46            $username = $username . "@" . $domain; 
     47        } 
    4548 
    46     $obApp->Authenticate($username, $curpass); 
    47     try { 
    48       $obDomain = $obApp->Domains->ItemByName($domain); 
    49       $obAccount = $obDomain->Accounts->ItemByAddress($username); 
    50       $obAccount->Password = $passwd; 
    51       $obAccount->Save(); 
    52       return PASSWORD_SUCCESS; 
    53     } 
    54     catch (Exception $e) { 
    55       write_log('errors', "Plugin password (hmail driver): " . trim(strip_tags($e->getMessage()))); 
    56       write_log('errors', "Plugin password (hmail driver): This problem is often caused by DCOM permissions not being set."); 
    57       return PASSWORD_ERROR; 
     49        $obApp->Authenticate($username, $curpass); 
     50        try { 
     51            $obDomain = $obApp->Domains->ItemByName($domain); 
     52            $obAccount = $obDomain->Accounts->ItemByAddress($username); 
     53            $obAccount->Password = $passwd; 
     54            $obAccount->Save(); 
     55            return PASSWORD_SUCCESS; 
     56        } 
     57        catch (Exception $e) { 
     58            write_log('errors', "Plugin password (hmail driver): " . trim(strip_tags($e->getMessage()))); 
     59            write_log('errors', "Plugin password (hmail driver): This problem is often caused by DCOM permissions not being set."); 
     60            return PASSWORD_ERROR; 
     61        } 
    5862    } 
    5963} 
    60  
    61 ?> 
  • trunk/plugins/password/drivers/ldap.php

    r5467 r5902  
    77 * This driver use the PEAR Net_LDAP2 class (http://pear.php.net/package/Net_LDAP2). 
    88 * 
    9  * @version 1.1 (2010-04-07) 
     9 * @version 2.0 
    1010 * @author Edouard MOREAU <edouard.moreau@ensma.fr> 
    1111 * 
    12  * function hashPassword based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/). 
    13  * function randomSalt based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/). 
     12 * method hashPassword based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/). 
     13 * method randomSalt based on code from the phpLDAPadmin development team (http://phpldapadmin.sourceforge.net/). 
    1414 * 
    1515 */ 
    1616 
    17 function password_save($curpass, $passwd) 
     17class rcube_ldap_password 
    1818{ 
    19     $rcmail = rcmail::get_instance(); 
    20     require_once ('Net/LDAP2.php'); 
    21  
    22     // Building user DN 
    23     if ($userDN = $rcmail->config->get('password_ldap_userDN_mask')) { 
    24         $userDN = substitute_vars($userDN); 
    25     } else { 
    26         $userDN = search_userdn($rcmail); 
    27     } 
    28  
    29     if (empty($userDN)) { 
    30         return PASSWORD_CONNECT_ERROR; 
    31     } 
    32  
    33     // Connection Method 
    34     switch($rcmail->config->get('password_ldap_method')) { 
    35         case 'admin': 
    36             $binddn = $rcmail->config->get('password_ldap_adminDN'); 
    37             $bindpw = $rcmail->config->get('password_ldap_adminPW'); 
    38             break; 
    39         case 'user': 
    40         default: 
    41             $binddn = $userDN; 
    42             $bindpw = $curpass; 
    43             break; 
    44     } 
    45  
    46     // Configuration array 
    47     $ldapConfig = array ( 
    48         'binddn'    => $binddn, 
    49         'bindpw'    => $bindpw, 
    50         'basedn'    => $rcmail->config->get('password_ldap_basedn'), 
    51         'host'      => $rcmail->config->get('password_ldap_host'), 
    52         'port'      => $rcmail->config->get('password_ldap_port'), 
    53         'starttls'  => $rcmail->config->get('password_ldap_starttls'), 
    54         'version'   => $rcmail->config->get('password_ldap_version'), 
    55     ); 
    56  
    57     // Connecting using the configuration array 
    58     $ldap = Net_LDAP2::connect($ldapConfig); 
    59  
    60     // Checking for connection error 
    61     if (PEAR::isError($ldap)) { 
    62         return PASSWORD_CONNECT_ERROR; 
    63     } 
    64  
    65     $crypted_pass = hashPassword($passwd, $rcmail->config->get('password_ldap_encodage')); 
    66     $force        = $rcmail->config->get('password_ldap_force_replace'); 
    67     $pwattr       = $rcmail->config->get('password_ldap_pwattr'); 
    68     $lchattr      = $rcmail->config->get('password_ldap_lchattr'); 
    69     $smbpwattr    = $rcmail->config->get('password_ldap_samba_pwattr'); 
    70     $smblchattr   = $rcmail->config->get('password_ldap_samba_lchattr'); 
    71     $samba        = $rcmail->config->get('password_ldap_samba'); 
    72  
    73     // Support password_ldap_samba option for backward compat. 
    74     if ($samba && !$smbpwattr) { 
    75         $smbpwattr  = 'sambaNTPassword'; 
    76         $smblchattr = 'sambaPwdLastSet'; 
    77     } 
    78  
    79     // Crypt new password 
    80     if (!$crypted_pass) { 
    81         return PASSWORD_CRYPT_ERROR; 
    82     } 
    83  
    84     // Crypt new samba password 
    85     if ($smbpwattr && !($samba_pass = hashPassword($passwd, 'samba'))) { 
    86             return PASSWORD_CRYPT_ERROR; 
    87     } 
    88  
    89     // Writing new crypted password to LDAP 
    90     $userEntry = $ldap->getEntry($userDN); 
    91     if (Net_LDAP2::isError($userEntry)) { 
    92         return PASSWORD_CONNECT_ERROR; 
    93     } 
    94  
    95     if (!$userEntry->replace(array($pwattr => $crypted_pass), $force)) { 
    96         return PASSWORD_CONNECT_ERROR; 
    97     } 
    98  
    99     // Updating PasswordLastChange Attribute if desired 
    100     if ($lchattr) { 
    101        $current_day = (int)(time() / 86400); 
    102        if (!$userEntry->replace(array($lchattr => $current_day), $force)) { 
    103            return PASSWORD_CONNECT_ERROR; 
    104        } 
    105     } 
    106  
    107     // Update Samba password and last change fields 
    108     if ($smbpwattr) { 
    109         $userEntry->replace(array($smbpwattr => $samba_pass), $force); 
    110     } 
    111     // Update Samba password last change field 
    112     if ($smblchattr) { 
    113         $userEntry->replace(array($smblchattr => time()), $force); 
    114     } 
    115  
    116     if (Net_LDAP2::isError($userEntry->update())) { 
    117         return PASSWORD_CONNECT_ERROR; 
    118     } 
    119  
    120     // All done, no error 
    121     return PASSWORD_SUCCESS; 
    122 } 
    123  
    124 /** 
    125  * Bind with searchDN and searchPW and search for the user's DN. 
    126  * Use search_base and search_filter defined in config file. 
    127  * Return the found DN. 
    128  */ 
    129 function search_userdn($rcmail) 
    130 { 
    131     $ldapConfig = array ( 
    132         'binddn'    => $rcmail->config->get('password_ldap_searchDN'), 
    133         'bindpw'    => $rcmail->config->get('password_ldap_searchPW'), 
    134         'basedn'    => $rcmail->config->get('password_ldap_basedn'), 
    135         'host'      => $rcmail->config->get('password_ldap_host'), 
    136         'port'      => $rcmail->config->get('password_ldap_port'), 
    137         'starttls'  => $rcmail->config->get('password_ldap_starttls'), 
    138         'version'   => $rcmail->config->get('password_ldap_version'), 
    139     ); 
    140  
    141     $ldap = Net_LDAP2::connect($ldapConfig); 
    142  
    143     if (PEAR::isError($ldap)) { 
    144         return ''; 
    145     } 
    146  
    147     $base = $rcmail->config->get('password_ldap_search_base'); 
    148     $filter = substitute_vars($rcmail->config->get('password_ldap_search_filter')); 
    149     $options = array ( 
     19    public function save($curpass, $passwd) 
     20    { 
     21        $rcmail = rcmail::get_instance(); 
     22        require_once 'Net/LDAP2.php'; 
     23 
     24        // Building user DN 
     25        if ($userDN = $rcmail->config->get('password_ldap_userDN_mask')) { 
     26            $userDN = $this->substitute_vars($userDN); 
     27        } else { 
     28            $userDN = $this->search_userdn($rcmail); 
     29        } 
     30 
     31        if (empty($userDN)) { 
     32            return PASSWORD_CONNECT_ERROR; 
     33        } 
     34 
     35        // Connection Method 
     36        switch($rcmail->config->get('password_ldap_method')) { 
     37            case 'admin': 
     38                $binddn = $rcmail->config->get('password_ldap_adminDN'); 
     39                $bindpw = $rcmail->config->get('password_ldap_adminPW'); 
     40                break; 
     41            case 'user': 
     42            default: 
     43                $binddn = $userDN; 
     44                $bindpw = $curpass; 
     45                break; 
     46        } 
     47 
     48        // Configuration array 
     49        $ldapConfig = array ( 
     50            'binddn'    => $binddn, 
     51            'bindpw'    => $bindpw, 
     52            'basedn'    => $rcmail->config->get('password_ldap_basedn'), 
     53            'host'      => $rcmail->config->get('password_ldap_host'), 
     54            'port'      => $rcmail->config->get('password_ldap_port'), 
     55            'starttls'  => $rcmail->config->get('password_ldap_starttls'), 
     56            'version'   => $rcmail->config->get('password_ldap_version'), 
     57        ); 
     58 
     59        // Connecting using the configuration array 
     60        $ldap = Net_LDAP2::connect($ldapConfig); 
     61 
     62        // Checking for connection error 
     63        if (PEAR::isError($ldap)) { 
     64            return PASSWORD_CONNECT_ERROR; 
     65        } 
     66 
     67        $crypted_pass = $this->hashPassword($passwd, $rcmail->config->get('password_ldap_encodage')); 
     68        $force        = $rcmail->config->get('password_ldap_force_replace'); 
     69        $pwattr       = $rcmail->config->get('password_ldap_pwattr'); 
     70        $lchattr      = $rcmail->config->get('password_ldap_lchattr'); 
     71        $smbpwattr    = $rcmail->config->get('password_ldap_samba_pwattr'); 
     72        $smblchattr   = $rcmail->config->get('password_ldap_samba_lchattr'); 
     73        $samba        = $rcmail->config->get('password_ldap_samba'); 
     74 
     75        // Support password_ldap_samba option for backward compat. 
     76        if ($samba && !$smbpwattr) { 
     77            $smbpwattr  = 'sambaNTPassword'; 
     78            $smblchattr = 'sambaPwdLastSet'; 
     79        } 
     80 
     81        // Crypt new password 
     82        if (!$crypted_pass) { 
     83            return PASSWORD_CRYPT_ERROR; 
     84        } 
     85 
     86        // Crypt new samba password 
     87        if ($smbpwattr && !($samba_pass = $this->hashPassword($passwd, 'samba'))) { 
     88                return PASSWORD_CRYPT_ERROR; 
     89        } 
     90 
     91        // Writing new crypted password to LDAP 
     92        $userEntry = $ldap->getEntry($userDN); 
     93        if (Net_LDAP2::isError($userEntry)) { 
     94            return PASSWORD_CONNECT_ERROR; 
     95        } 
     96 
     97        if (!$userEntry->replace(array($pwattr => $crypted_pass), $force)) { 
     98            return PASSWORD_CONNECT_ERROR; 
     99        } 
     100 
     101        // Updating PasswordLastChange Attribute if desired 
     102        if ($lchattr) { 
     103            $current_day = (int)(time() / 86400); 
     104            if (!$userEntry->replace(array($lchattr => $current_day), $force)) { 
     105                return PASSWORD_CONNECT_ERROR; 
     106            } 
     107        } 
     108 
     109        // Update Samba password and last change fields 
     110        if ($smbpwattr) { 
     111            $userEntry->replace(array($smbpwattr => $samba_pass), $force); 
     112        } 
     113        // Update Samba password last change field 
     114        if ($smblchattr) { 
     115            $userEntry->replace(array($smblchattr => time()), $force); 
     116        } 
     117 
     118        if (Net_LDAP2::isError($userEntry->update())) { 
     119            return PASSWORD_CONNECT_ERROR; 
     120        } 
     121 
     122        // All done, no error 
     123        return PASSWORD_SUCCESS; 
     124    } 
     125 
     126    /** 
     127     * Bind with searchDN and searchPW and search for the user's DN. 
     128     * Use search_base and search_filter defined in config file. 
     129     * Return the found DN. 
     130     */ 
     131    function search_userdn($rcmail) 
     132    { 
     133        $ldapConfig = array ( 
     134            'binddn'    => $rcmail->config->get('password_ldap_searchDN'), 
     135            'bindpw'    => $rcmail->config->get('password_ldap_searchPW'), 
     136            'basedn'    => $rcmail->config->get('password_ldap_basedn'), 
     137            'host'      => $rcmail->config->get('password_ldap_host'), 
     138            'port'      => $rcmail->config->get('password_ldap_port'), 
     139            'starttls'  => $rcmail->config->get('password_ldap_starttls'), 
     140            'version'   => $rcmail->config->get('password_ldap_version'), 
     141        ); 
     142 
     143        $ldap = Net_LDAP2::connect($ldapConfig); 
     144 
     145        if (PEAR::isError($ldap)) { 
     146            return ''; 
     147        } 
     148 
     149        $base = $rcmail->config->get('password_ldap_search_base'); 
     150        $filter = $this->substitute_vars($rcmail->config->get('password_ldap_search_filter')); 
     151        $options = array ( 
    150152            'scope' => 'sub', 
    151153            'attributes' => array(), 
    152     ); 
    153  
    154     $result = $ldap->search($base, $filter, $options); 
    155     $ldap->done(); 
    156     if (PEAR::isError($result) || ($result->count() != 1)) { 
    157         return ''; 
    158     } 
    159  
    160     return $result->current()->dn(); 
    161 } 
    162  
    163 /** 
    164  * Substitute %login, %name, %domain, %dc in $str. 
    165  * See plugin config for details. 
    166  */ 
    167 function substitute_vars($str) 
    168 { 
    169     $rcmail = rcmail::get_instance(); 
    170     $domain = $rcmail->user->get_username('domain'); 
    171     $dc     = 'dc='.strtr($domain, array('.' => ',dc=')); // hierarchal domain string 
    172  
    173     $str = str_replace(array( 
    174             '%login', 
    175             '%name', 
    176             '%domain', 
    177             '%dc', 
    178         ), array( 
    179             $_SESSION['username'], 
    180             $rcmail->user->get_username('local'), 
    181             $domain, 
    182             $dc, 
    183         ), $str 
    184     ); 
    185  
    186     return $str; 
    187 } 
    188  
    189  
    190 /** 
    191  * Code originaly from the phpLDAPadmin development team 
    192  * http://phpldapadmin.sourceforge.net/ 
    193  * 
    194  * Hashes a password and returns the hash based on the specified enc_type. 
    195  * 
    196  * @param string $passwordClear The password to hash in clear text. 
    197  * @param string $encodageType Standard LDAP encryption type which must be one of 
    198  *        crypt, ext_des, md5crypt, blowfish, md5, sha, smd5, ssha, or clear. 
    199  * @return string The hashed password. 
    200  * 
    201  */ 
    202  
    203 function hashPassword( $passwordClear, $encodageType ) 
    204 { 
    205     $encodageType = strtolower( $encodageType ); 
    206     switch( $encodageType ) { 
    207         case 'crypt':  
    208             $cryptedPassword = '{CRYPT}' . crypt($passwordClear,randomSalt(2));  
     154        ); 
     155 
     156        $result = $ldap->search($base, $filter, $options); 
     157        $ldap->done(); 
     158        if (PEAR::isError($result) || ($result->count() != 1)) { 
     159            return ''; 
     160        } 
     161 
     162        return $result->current()->dn(); 
     163    } 
     164 
     165    /** 
     166     * Substitute %login, %name, %domain, %dc in $str. 
     167     * See plugin config for details. 
     168     */ 
     169    function substitute_vars($str) 
     170    { 
     171        $rcmail = rcmail::get_instance(); 
     172        $domain = $rcmail->user->get_username('domain'); 
     173        $dc     = 'dc='.strtr($domain, array('.' => ',dc=')); // hierarchal domain string 
     174 
     175        $str = str_replace(array( 
     176                '%login', 
     177                '%name', 
     178                '%domain', 
     179                '%dc', 
     180            ), array( 
     181                $_SESSION['username'], 
     182                $rcmail->user->get_username('local'), 
     183                $domain, 
     184                $dc, 
     185            ), $str 
     186        ); 
     187 
     188        return $str; 
     189    } 
     190 
     191    /** 
     192     * Code originaly from the phpLDAPadmin development team 
     193     * http://phpldapadmin.sourceforge.net/ 
     194     * 
     195     * Hashes a password and returns the hash based on the specified enc_type. 
     196     * 
     197     * @param string $passwordClear The password to hash in clear text. 
     198     * @param string $encodageType Standard LDAP encryption type which must be one of 
     199     *        crypt, ext_des, md5crypt, blowfish, md5, sha, smd5, ssha, or clear. 
     200     * @return string The hashed password. 
     201     * 
     202     */ 
     203    function hashPassword( $passwordClear, $encodageType ) 
     204    { 
     205        $encodageType = strtolower( $encodageType ); 
     206        switch( $encodageType ) { 
     207        case 'crypt': 
     208            $cryptedPassword = '{CRYPT}' . crypt($passwordClear, $this->randomSalt(2)); 
    209209            break; 
    210210 
     
    215215                return FALSE; 
    216216            } 
    217             $cryptedPassword = '{CRYPT}' . crypt( $passwordClear, '_' . randomSalt(8) ); 
     217            $cryptedPassword = '{CRYPT}' . crypt( $passwordClear, '_' . $this->randomSalt(8) ); 
    218218            break; 
    219219 
     
    223223                return FALSE; 
    224224            } 
    225             $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$1$' . randomSalt(9) ); 
     225            $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$1$' . $this->randomSalt(9) ); 
    226226            break; 
    227227 
     
    232232            } 
    233233            // hardcoded to second blowfish version and set number of rounds 
    234             $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$2a$12$' . randomSalt(13) ); 
     234            $cryptedPassword = '{CRYPT}' . crypt( $passwordClear , '$2a$12$' . $this->randomSalt(13) ); 
    235235            break; 
    236236 
     
    283283        default: 
    284284            $cryptedPassword = $passwordClear; 
    285     } 
    286  
    287     return $cryptedPassword; 
     285        } 
     286 
     287        return $cryptedPassword; 
     288    } 
     289 
     290    /** 
     291     * Code originaly from the phpLDAPadmin development team 
     292     * http://phpldapadmin.sourceforge.net/ 
     293     * 
     294     * Used to generate a random salt for crypt-style passwords. Salt strings are used 
     295     * to make pre-built hash cracking dictionaries difficult to use as the hash algorithm uses 
     296     * not only the user's password but also a randomly generated string. The string is 
     297     * stored as the first N characters of the hash for reference of hashing algorithms later. 
     298     * 
     299     * --- added 20021125 by bayu irawan <bayuir@divnet.telkom.co.id> --- 
     300     * --- ammended 20030625 by S C Rigler <srigler@houston.rr.com> --- 
     301     * 
     302     * @param int $length The length of the salt string to generate. 
     303     * @return string The generated salt string. 
     304     */ 
     305    function randomSalt( $length ) 
     306    { 
     307        $possible = '0123456789'. 
     308            'abcdefghijklmnopqrstuvwxyz'. 
     309            'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. 
     310            './'; 
     311        $str = ''; 
     312        // mt_srand((double)microtime() * 1000000); 
     313 
     314        while (strlen($str) < $length) 
     315            $str .= substr($possible, (rand() % strlen($possible)), 1); 
     316 
     317        return $str; 
     318    } 
    288319} 
    289  
    290 /** 
    291  * Code originaly from the phpLDAPadmin development team 
    292  * http://phpldapadmin.sourceforge.net/ 
    293  * 
    294  * Used to generate a random salt for crypt-style passwords. Salt strings are used 
    295  * to make pre-built hash cracking dictionaries difficult to use as the hash algorithm uses 
    296  * not only the user's password but also a randomly generated string. The string is 
    297  * stored as the first N characters of the hash for reference of hashing algorithms later. 
    298  * 
    299  * --- added 20021125 by bayu irawan <bayuir@divnet.telkom.co.id> --- 
    300  * --- ammended 20030625 by S C Rigler <srigler@houston.rr.com> --- 
    301  * 
    302  * @param int $length The length of the salt string to generate. 
    303  * @return string The generated salt string. 
    304  */ 
    305 function randomSalt( $length ) 
    306 { 
    307     $possible = '0123456789'. 
    308         'abcdefghijklmnopqrstuvwxyz'. 
    309         'ABCDEFGHIJKLMNOPQRSTUVWXYZ'. 
    310         './'; 
    311     $str = ''; 
    312 //    mt_srand((double)microtime() * 1000000); 
    313  
    314     while (strlen($str) < $length) 
    315         $str .= substr($possible, (rand() % strlen($possible)), 1); 
    316  
    317     return $str; 
    318 } 
  • trunk/plugins/password/drivers/ldap_simple.php

    r5467 r5902  
    77 * This driver is based on Edouard's LDAP Password Driver, but does not 
    88 * require PEAR's Net_LDAP2 to be installed 
    9  *  
    10  * @version 1.0 (2010-07-31) 
     9 * 
     10 * @version 2.0 
    1111 * @author Wout Decre <wout@canodus.be> 
    1212 */ 
    13 function password_save($curpass, $passwd) 
     13 
     14class rcube_ldap_simple_password 
    1415{ 
    15         $rcmail = rcmail::get_instance(); 
    16  
    17         // Connect 
    18         if (!$ds = ldap_connect($rcmail->config->get('password_ldap_host'), $rcmail->config->get('password_ldap_port'))) { 
    19                 ldap_unbind($ds); 
    20                 return PASSWORD_CONNECT_ERROR; 
    21         } 
    22  
    23         // Set protocol version 
    24         if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $rcmail->config->get('password_ldap_version'))) { 
    25                 ldap_unbind($ds); 
    26                 return PASSWORD_CONNECT_ERROR; 
    27         } 
    28  
    29         // Start TLS 
    30         if ($rcmail->config->get('password_ldap_starttls')) { 
    31                 if (!ldap_start_tls($ds)) { 
    32                         ldap_unbind($ds); 
    33                         return PASSWORD_CONNECT_ERROR; 
    34                 } 
    35         } 
    36  
    37         // Build user DN 
    38         if ($user_dn = $rcmail->config->get('password_ldap_userDN_mask')) { 
    39                 $user_dn = ldap_simple_substitute_vars($user_dn); 
    40         } else { 
    41                 $user_dn = ldap_simple_search_userdn($rcmail, $ds); 
    42         } 
    43  
    44         if (empty($user_dn)) { 
    45                 ldap_unbind($ds); 
    46                 return PASSWORD_CONNECT_ERROR; 
    47         } 
    48  
    49         // Connection method 
    50         switch ($rcmail->config->get('password_ldap_method')) { 
     16    function save($curpass, $passwd) 
     17    { 
     18            $rcmail = rcmail::get_instance(); 
     19 
     20        // Connect 
     21            if (!$ds = ldap_connect($rcmail->config->get('password_ldap_host'), $rcmail->config->get('password_ldap_port'))) { 
     22                    ldap_unbind($ds); 
     23                return PASSWORD_CONNECT_ERROR; 
     24            } 
     25 
     26        // Set protocol version 
     27            if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $rcmail->config->get('password_ldap_version'))) { 
     28                    ldap_unbind($ds); 
     29                return PASSWORD_CONNECT_ERROR; 
     30            } 
     31 
     32        // Start TLS 
     33            if ($rcmail->config->get('password_ldap_starttls')) { 
     34                    if (!ldap_start_tls($ds)) { 
     35                        ldap_unbind($ds); 
     36                        return PASSWORD_CONNECT_ERROR; 
     37                    } 
     38            } 
     39 
     40        // Build user DN 
     41            if ($user_dn = $rcmail->config->get('password_ldap_userDN_mask')) { 
     42                    $user_dn = $this->substitute_vars($user_dn); 
     43        } 
     44        else { 
     45                $user_dn = $this->search_userdn($rcmail, $ds); 
     46            } 
     47 
     48            if (empty($user_dn)) { 
     49                    ldap_unbind($ds); 
     50                return PASSWORD_CONNECT_ERROR; 
     51            } 
     52 
     53        // Connection method 
     54            switch ($rcmail->config->get('password_ldap_method')) { 
    5155                case 'admin': 
    5256                        $binddn = $rcmail->config->get('password_ldap_adminDN'); 
     
    5862                        $bindpw = $curpass; 
    5963                        break; 
    60         } 
    61  
    62  
    63         $crypted_pass = ldap_simple_hash_password($passwd, $rcmail->config->get('password_ldap_encodage')); 
    64         $lchattr      = $rcmail->config->get('password_ldap_lchattr'); 
    65         $pwattr       = $rcmail->config->get('password_ldap_pwattr'); 
    66     $smbpwattr    = $rcmail->config->get('password_ldap_samba_pwattr'); 
    67     $smblchattr   = $rcmail->config->get('password_ldap_samba_lchattr'); 
    68     $samba        = $rcmail->config->get('password_ldap_samba'); 
    69  
    70     // Support password_ldap_samba option for backward compat. 
    71     if ($samba && !$smbpwattr) { 
    72         $smbpwattr  = 'sambaNTPassword'; 
    73         $smblchattr = 'sambaPwdLastSet'; 
    74     } 
    75  
    76         // Crypt new password 
    77         if (!$crypted_pass) { 
    78                 return PASSWORD_CRYPT_ERROR; 
    79         } 
    80  
    81     // Crypt new Samba password 
    82     if ($smbpwattr && !($samba_pass = ldap_simple_hash_password($passwd, 'samba'))) { 
    83             return PASSWORD_CRYPT_ERROR; 
    84     } 
    85  
    86         // Bind 
    87         if (!ldap_bind($ds, $binddn, $bindpw)) { 
    88                 ldap_unbind($ds); 
    89                 return PASSWORD_CONNECT_ERROR; 
    90         } 
    91  
    92         $entree[$pwattr] = $crypted_pass; 
    93  
    94         // Update PasswordLastChange Attribute if desired 
    95         if ($lchattr) { 
    96                 $entree[$lchattr] = (int)(time() / 86400); 
    97         } 
    98  
    99     // Update Samba password 
    100     if ($smbpwattr) { 
    101         $entree[$smbpwattr] = $samba_pass; 
    102     } 
    103  
    104     // Update Samba password last change 
    105     if ($smblchattr) { 
    106         $entree[$smblchattr] = time(); 
    107     } 
    108  
    109         if (!ldap_modify($ds, $user_dn, $entree)) { 
    110                 ldap_unbind($ds); 
    111                 return PASSWORD_CONNECT_ERROR; 
    112         } 
    113  
    114         // All done, no error 
    115         ldap_unbind($ds); 
    116         return PASSWORD_SUCCESS; 
    117 } 
    118  
    119 /** 
    120  * Bind with searchDN and searchPW and search for the user's DN 
    121  * Use search_base and search_filter defined in config file 
    122  * Return the found DN 
    123  */ 
    124 function ldap_simple_search_userdn($rcmail, $ds) 
    125 { 
    126         /* Bind */ 
    127         if (!ldap_bind($ds, $rcmail->config->get('password_ldap_searchDN'), $rcmail->config->get('password_ldap_searchPW'))) { 
    128                 return false; 
    129         } 
    130  
    131         /* Search for the DN */ 
    132         if (!$sr = ldap_search($ds, $rcmail->config->get('password_ldap_search_base'), ldap_simple_substitute_vars($rcmail->config->get('password_ldap_search_filter')))) { 
    133                 return false; 
    134         } 
    135  
    136         /* If no or more entries were found, return false */ 
    137         if (ldap_count_entries($ds, $sr) != 1) { 
    138                 return false; 
    139         } 
    140  
    141         return ldap_get_dn($ds, ldap_first_entry($ds, $sr)); 
    142 } 
    143  
    144 /** 
    145  * Substitute %login, %name, %domain, %dc in $str 
    146  * See plugin config for details 
    147  */ 
    148 function ldap_simple_substitute_vars($str) 
    149 { 
    150         $str = str_replace('%login', $_SESSION['username'], $str); 
    151         $str = str_replace('%l', $_SESSION['username'], $str); 
    152  
    153         $parts = explode('@', $_SESSION['username']); 
    154  
    155         if (count($parts) == 2) { 
    156         $dc = 'dc='.strtr($parts[1], array('.' => ',dc=')); // hierarchal domain string 
    157  
    158                 $str = str_replace('%name', $parts[0], $str); 
    159         $str = str_replace('%n', $parts[0], $str); 
    160         $str = str_replace('%dc', $dc, $str); 
    161                 $str = str_replace('%domain', $parts[1], $str); 
    162                 $str = str_replace('%d', $parts[1], $str); 
    163         } 
    164  
    165         return $str; 
    166 } 
    167  
    168 /** 
    169  * Code originaly from the phpLDAPadmin development team 
    170  * http://phpldapadmin.sourceforge.net/ 
    171  * 
    172  * Hashes a password and returns the hash based on the specified enc_type 
    173  */ 
    174 function ldap_simple_hash_password($password_clear, $encodage_type) 
    175 { 
    176         $encodage_type = strtolower($encodage_type); 
    177         switch ($encodage_type) { 
     64            } 
     65 
     66        $crypted_pass = $this->hash_password($passwd, $rcmail->config->get('password_ldap_encodage')); 
     67            $lchattr      = $rcmail->config->get('password_ldap_lchattr'); 
     68        $pwattr       = $rcmail->config->get('password_ldap_pwattr'); 
     69        $smbpwattr    = $rcmail->config->get('password_ldap_samba_pwattr'); 
     70        $smblchattr   = $rcmail->config->get('password_ldap_samba_lchattr'); 
     71        $samba        = $rcmail->config->get('password_ldap_samba'); 
     72 
     73        // Support password_ldap_samba option for backward compat. 
     74        if ($samba && !$smbpwattr) { 
     75            $smbpwattr  = 'sambaNTPassword'; 
     76            $smblchattr = 'sambaPwdLastSet'; 
     77        } 
     78 
     79        // Crypt new password 
     80            if (!$crypted_pass) { 
     81                    return PASSWORD_CRYPT_ERROR; 
     82            } 
     83 
     84        // Crypt new Samba password 
     85        if ($smbpwattr && !($samba_pass = $this->hash_password($passwd, 'samba'))) { 
     86                return PASSWORD_CRYPT_ERROR; 
     87        } 
     88 
     89        // Bind 
     90            if (!ldap_bind($ds, $binddn, $bindpw)) { 
     91                    ldap_unbind($ds); 
     92                return PASSWORD_CONNECT_ERROR; 
     93            } 
     94 
     95        $entree[$pwattr] = $crypted_pass; 
     96 
     97            // Update PasswordLastChange Attribute if desired 
     98        if ($lchattr) { 
     99                $entree[$lchattr] = (int)(time() / 86400); 
     100            } 
     101 
     102        // Update Samba password 
     103        if ($smbpwattr) { 
     104            $entree[$smbpwattr] = $samba_pass; 
     105        } 
     106 
     107        // Update Samba password last change 
     108        if ($smblchattr) { 
     109            $entree[$smblchattr] = time(); 
     110        } 
     111 
     112        if (!ldap_modify($ds, $user_dn, $entree)) { 
     113                ldap_unbind($ds); 
     114                    return PASSWORD_CONNECT_ERROR; 
     115        } 
     116 
     117        // All done, no error 
     118            ldap_unbind($ds); 
     119            return PASSWORD_SUCCESS; 
     120    } 
     121 
     122    /** 
     123     * Bind with searchDN and searchPW and search for the user's DN 
     124     * Use search_base and search_filter defined in config file 
     125     * Return the found DN 
     126     */ 
     127    function search_userdn($rcmail, $ds) 
     128    { 
     129            /* Bind */ 
     130        if (!ldap_bind($ds, $rcmail->config->get('password_ldap_searchDN'), $rcmail->config->get('password_ldap_searchPW'))) { 
     131                return false; 
     132            } 
     133 
     134        /* Search for the DN */ 
     135            if (!$sr = ldap_search($ds, $rcmail->config->get('password_ldap_search_base'), $this->substitute_vars($rcmail->config->get('password_ldap_search_filter')))) { 
     136                    return false; 
     137            } 
     138 
     139        /* If no or more entries were found, return false */ 
     140            if (ldap_count_entries($ds, $sr) != 1) { 
     141                    return false; 
     142            } 
     143 
     144            return ldap_get_dn($ds, ldap_first_entry($ds, $sr)); 
     145    } 
     146 
     147    /** 
     148     * Substitute %login, %name, %domain, %dc in $str 
     149     * See plugin config for details 
     150     */ 
     151    function substitute_vars($str) 
     152    { 
     153            $str = str_replace('%login', $_SESSION['username'], $str); 
     154        $str = str_replace('%l', $_SESSION['username'], $str); 
     155 
     156            $parts = explode('@', $_SESSION['username']); 
     157 
     158        if (count($parts) == 2) { 
     159            $dc = 'dc='.strtr($parts[1], array('.' => ',dc=')); // hierarchal domain string 
     160 
     161                $str = str_replace('%name', $parts[0], $str); 
     162            $str = str_replace('%n', $parts[0], $str); 
     163            $str = str_replace('%dc', $dc, $str); 
     164                $str = str_replace('%domain', $parts[1], $str); 
     165                    $str = str_replace('%d', $parts[1], $str); 
     166        } 
     167 
     168            return $str; 
     169    } 
     170 
     171    /** 
     172     * Code originaly from the phpLDAPadmin development team 
     173     * http://phpldapadmin.sourceforge.net/ 
     174     * 
     175     * Hashes a password and returns the hash based on the specified enc_type 
     176     */ 
     177    function hash_password($password_clear, $encodage_type) 
     178    { 
     179        $encodage_type = strtolower($encodage_type); 
     180            switch ($encodage_type) { 
    178181                case 'crypt': 
    179                         $crypted_password = '{CRYPT}' . crypt($password_clear, ldap_simple_random_salt(2)); 
     182                        $crypted_password = '{CRYPT}' . crypt($password_clear, $this->random_salt(2)); 
    180183                        break; 
    181184                case 'ext_des': 
     
    185188                                return false; 
    186189                        } 
    187                         $crypted_password = '{CRYPT}' . crypt($password_clear, '_' . ldap_simple_random_salt(8)); 
     190                        $crypted_password = '{CRYPT}' . crypt($password_clear, '_' . $this->random_salt(8)); 
    188191                        break; 
    189192                case 'md5crypt': 
     
    192195                                return false; 
    193196                        } 
    194                         $crypted_password = '{CRYPT}' . crypt($password_clear, '$1$' . ldap_simple_random_salt(9)); 
     197                        $crypted_password = '{CRYPT}' . crypt($password_clear, '$1$' . $this->random_salt(9)); 
    195198                        break; 
    196199                case 'blowfish': 
     
    200203                        } 
    201204                        /* Hardcoded to second blowfish version and set number of rounds */ 
    202                         $crypted_password = '{CRYPT}' . crypt($password_clear, '$2a$12$' . ldap_simple_random_salt(13)); 
     205                        $crypted_password = '{CRYPT}' . crypt($password_clear, '$2a$12$' . $this->random_salt(13)); 
    203206                        break; 
    204207                case 'md5': 
     
    248251                default: 
    249252                        $crypted_password = $password_clear; 
    250         } 
    251  
    252         return $crypted_password; 
     253            } 
     254 
     255        return $crypted_password; 
     256    } 
     257 
     258    /** 
     259     * Code originaly from the phpLDAPadmin development team 
     260     * http://phpldapadmin.sourceforge.net/ 
     261     * 
     262     * Used to generate a random salt for crypt-style passwords 
     263     */ 
     264    function random_salt($length) 
     265    { 
     266            $possible = '0123456789' . 'abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . './'; 
     267        $str = ''; 
     268            // mt_srand((double)microtime() * 1000000); 
     269 
     270        while (strlen($str) < $length) { 
     271                $str .= substr($possible, (rand() % strlen($possible)), 1); 
     272            } 
     273 
     274            return $str; 
     275    } 
    253276} 
    254  
    255 /** 
    256  * Code originaly from the phpLDAPadmin development team 
    257  * http://phpldapadmin.sourceforge.net/ 
    258  * 
    259  * Used to generate a random salt for crypt-style passwords 
    260  */ 
    261 function ldap_simple_random_salt($length) 
    262 { 
    263         $possible = '0123456789' . 'abcdefghijklmnopqrstuvwxyz' . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . './'; 
    264         $str = ''; 
    265         // mt_srand((double)microtime() * 1000000); 
    266         while (strlen($str) < $length) { 
    267                 $str .= substr($possible, (rand() % strlen($possible)), 1); 
    268         } 
    269  
    270         return $str; 
    271 } 
  • trunk/plugins/password/drivers/pam.php

    r4199 r5902  
    44 * PAM Password Driver 
    55 * 
    6  * @version 1.0 
     6 * @version 2.0 
    77 * @author Aleksander Machniak 
    88 */ 
    9   
    10 function password_save($currpass, $newpass) 
     9 
     10class rcube_pam_password 
    1111{ 
    12     $user = $_SESSION['username']; 
     12    function save($currpass, $newpass) 
     13    { 
     14        $user = $_SESSION['username']; 
    1315 
    14     if (extension_loaded('pam')) { 
    15         if (pam_auth($user, $currpass, $error, false)) { 
    16             if (pam_chpass($user, $currpass, $newpass)) { 
    17                 return PASSWORD_SUCCESS; 
     16        if (extension_loaded('pam')) { 
     17            if (pam_auth($user, $currpass, $error, false)) { 
     18                if (pam_chpass($user, $currpass, $newpass)) { 
     19                    return PASSWORD_SUCCESS; 
     20                } 
     21            } 
     22            else { 
     23                raise_error(array( 
     24                    'code' => 600, 
     25                    'type' => 'php', 
     26                    'file' => __FILE__, 'line' => __LINE__, 
     27                    'message' => "Password plugin: PAM authentication failed for user $user: $error" 
     28                    ), true, false); 
    1829            } 
    1930        } 
     
    2334                'type' => 'php', 
    2435                'file' => __FILE__, 'line' => __LINE__, 
    25                 'message' => "Password plugin: PAM authentication failed for user $user: $error" 
     36                'message' => "Password plugin: PECL-PAM module not loaded" 
    2637                ), true, false); 
    2738        } 
     39 
     40        return PASSWORD_ERROR; 
    2841    } 
    29     else { 
    30         raise_error(array( 
    31             'code' => 600, 
    32             'type' => 'php', 
    33             'file' => __FILE__, 'line' => __LINE__, 
    34             'message' => "Password plugin: PECL-PAM module not loaded" 
    35             ), true, false); 
    36     } 
    37  
    38     return PASSWORD_ERROR; 
    3942} 
    40  
    41 ?> 
  • trunk/plugins/password/drivers/poppassd.php

    r4016 r5902  
    66 * Driver to change passwords via Poppassd/Courierpassd 
    77 * 
    8  * @version 1.1 
     8 * @version 2.0 
    99 * @author Philip Weir 
    1010 * 
    1111 */ 
    1212 
    13 function format_error_result($code, $line) 
     13class rcube_poppassd_password 
    1414{ 
    15     if (preg_match('/^\d\d\d\s+(\S.*)\s*$/', $line, $matches)) { 
    16         return array('code' => $code, 'message' => $matches[1]); 
    17     } else { 
    18         return $code; 
     15    function format_error_result($code, $line) 
     16    { 
     17        if (preg_match('/^\d\d\d\s+(\S.*)\s*$/', $line, $matches)) { 
     18            return array('code' => $code, 'message' => $matches[1]); 
     19        } else { 
     20            return $code; 
     21        } 
    1922    } 
    20 } 
    2123 
    22 function password_save($curpass, $passwd) 
    23 { 
    24     $rcmail = rcmail::get_instance(); 
     24    function save($curpass, $passwd) 
     25    { 
     26        $rcmail = rcmail::get_instance(); 
    2527//    include('Net/Socket.php'); 
    26     $poppassd = new Net_Socket(); 
     28        $poppassd = new Net_Socket(); 
    2729 
    28     $result = $poppassd->connect($rcmail->config->get('password_pop_host'), $rcmail->config->get('password_pop_port'), null); 
    29     if (PEAR::isError($result)) { 
    30         return format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage()); 
    31     } 
    32     else { 
    33         $result = $poppassd->readLine(); 
    34         if(!preg_match('/^2\d\d/', $result)) { 
    35             $poppassd->disconnect(); 
    36             return format_error_result(PASSWORD_ERROR, $result); 
     30        $result = $poppassd->connect($rcmail->config->get('password_pop_host'), $rcmail->config->get('password_pop_port'), null); 
     31        if (PEAR::isError($result)) { 
     32            return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage()); 
    3733        } 
    3834        else { 
    39             $poppassd->writeLine("user ". $_SESSION['username']); 
    4035            $result = $poppassd->readLine(); 
    41             if(!preg_match('/^[23]\d\d/', $result) ) { 
     36            if(!preg_match('/^2\d\d/', $result)) { 
    4237                $poppassd->disconnect(); 
    43                 return format_error_result(PASSWORD_CONNECT_ERROR, $result); 
     38                return $this->format_error_result(PASSWORD_ERROR, $result); 
    4439            } 
    4540            else { 
    46                 $poppassd->writeLine("pass ". $curpass); 
     41                $poppassd->writeLine("user ". $_SESSION['username']); 
    4742                $result = $poppassd->readLine(); 
    4843                if(!preg_match('/^[23]\d\d/', $result) ) { 
    4944                    $poppassd->disconnect(); 
    50                     return format_error_result(PASSWORD_ERROR, $result); 
     45                    return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result); 
    5146                } 
    5247                else { 
    53                     $poppassd->writeLine("newpass ". $passwd); 
     48                    $poppassd->writeLine("pass ". $curpass); 
    5449                    $result = $poppassd->readLine(); 
    55                     $poppassd->disconnect(); 
    56                     if (!preg_match('/^2\d\d/', $result)) 
    57                         return format_error_result(PASSWORD_ERROR, $result); 
    58                     else 
    59                         return PASSWORD_SUCCESS; 
     50                    if(!preg_match('/^[23]\d\d/', $result) ) { 
     51                        $poppassd->disconnect(); 
     52                        return $this->format_error_result(PASSWORD_ERROR, $result); 
     53                    } 
     54                    else { 
     55                        $poppassd->writeLine("newpass ". $passwd); 
     56                        $result = $poppassd->readLine(); 
     57                        $poppassd->disconnect(); 
     58                        if (!preg_match('/^2\d\d/', $result)) 
     59                            return $this->format_error_result(PASSWORD_ERROR, $result); 
     60                        else 
     61                            return PASSWORD_SUCCESS; 
     62                    } 
    6063                } 
    6164            } 
  • trunk/plugins/password/drivers/sasl.php

    r4199 r5902  
    1313 * For installation instructions please read the README file. 
    1414 * 
    15  * @version 1.0 
     15 * @version 2.0 
    1616 * @author Thomas Bruederli 
    1717 */ 
    18   
    19 function password_save($currpass, $newpass) 
     18 
     19class rcube_sasl_password 
    2020{ 
    21     $curdir = realpath(dirname(__FILE__)); 
    22     $username = escapeshellcmd($_SESSION['username']); 
    23     $args = rcmail::get_instance()->config->get('password_saslpasswd_args', ''); 
     21    function save($currpass, $newpass) 
     22    { 
     23        $curdir = realpath(dirname(__FILE__)); 
     24        $username = escapeshellcmd($_SESSION['username']); 
     25        $args = rcmail::get_instance()->config->get('password_saslpasswd_args', ''); 
    2426 
    25     if ($fh = popen("$curdir/chgsaslpasswd -p $args $username", 'w')) { 
    26         fwrite($fh, $newpass."\n"); 
    27         $code = pclose($fh); 
     27        if ($fh = popen("$curdir/chgsaslpasswd -p $args $username", 'w')) { 
     28            fwrite($fh, $newpass."\n"); 
     29            $code = pclose($fh); 
    2830 
    29         if ($code == 0) 
    30             return PASSWORD_SUCCESS; 
     31            if ($code == 0) 
     32                return PASSWORD_SUCCESS; 
     33        } 
     34        else { 
     35            raise_error(array( 
     36                'code' => 600, 
     37                'type' => 'php', 
     38                'file' => __FILE__, 'line' => __LINE__, 
     39                'message' => "Password plugin: Unable to execute $curdir/chgsaslpasswd" 
     40                ), true, false); 
     41        } 
     42 
     43        return PASSWORD_ERROR; 
    3144    } 
    32     else { 
    33         raise_error(array( 
    34             'code' => 600, 
    35             'type' => 'php', 
    36             'file' => __FILE__, 'line' => __LINE__, 
    37             'message' => "Password plugin: Unable to execute $curdir/chgsaslpasswd" 
    38             ), true, false); 
    39     } 
    40  
    41     return PASSWORD_ERROR; 
    4245} 
    43  
    44 ?> 
  • trunk/plugins/password/drivers/sql.php

    r5419 r5902  
    66 * Driver for passwords stored in SQL database 
    77 * 
    8  * @version 1.4 
     8 * @version 2.0 
    99 * @author Aleksander 'A.L.E.C' Machniak <alec@alec.pl> 
    1010 * 
    1111 */ 
    1212 
    13 function password_save($curpass, $passwd) 
     13class rcube_sql_password 
    1414{ 
    15     $rcmail = rcmail::get_instance(); 
     15    function save($curpass, $passwd) 
     16    { 
     17        $rcmail = rcmail::get_instance(); 
    1618 
    17     if (!($sql = $rcmail->config->get('password_query'))) 
    18         $sql = 'SELECT update_passwd(%c, %u)'; 
     19        if (!($sql = $rcmail->config->get('password_query'))) 
     20            $sql = 'SELECT update_passwd(%c, %u)'; 
    1921 
    20     if ($dsn = $rcmail->config->get('password_db_dsn')) { 
    21             // #1486067: enable new_link option 
    22             if (is_array($dsn) && empty($dsn['new_link'])) 
    23                 $dsn['new_link'] = true; 
    24             else if (!is_array($dsn) && !preg_match('/\?new_link=true/', $dsn)) 
    25                 $dsn .= '?new_link=true'; 
     22        if ($dsn = $rcmail->config->get('password_db_dsn')) { 
     23                // #1486067: enable new_link option 
     24                if (is_array($dsn) && empty($dsn['new_link'])) 
     25                    $dsn['new_link'] = true; 
     26            else if (!is_array($dsn) && !preg_match('/\?new_link=true/', $dsn)) 
     27                    $dsn .= '?new_link=true'; 
    2628 
    27         $db = new rcube_mdb2($dsn, '', FALSE); 
    28         $db->set_debug((bool)$rcmail->config->get('sql_debug')); 
    29         $db->db_connect('w'); 
    30     } else { 
    31         $db = $rcmail->get_dbh(); 
    32     } 
    33  
    34     if ($err = $db->is_error()) 
    35         return PASSWORD_ERROR; 
    36  
    37     // crypted password 
    38     if (strpos($sql, '%c') !== FALSE) { 
    39         $salt = ''; 
    40         if (CRYPT_MD5) { 
    41             // Always use eight salt characters for MD5 (#1488136) 
    42             $len = 8; 
    43         } else if (CRYPT_STD_DES) { 
    44             $len = 2; 
    45         } else { 
    46             return PASSWORD_CRYPT_ERROR; 
     29            $db = new rcube_mdb2($dsn, '', FALSE); 
     30            $db->set_debug((bool)$rcmail->config->get('sql_debug')); 
     31            $db->db_connect('w'); 
     32        } 
     33        else { 
     34            $db = $rcmail->get_dbh(); 
    4735        } 
    4836 
    49         //Restrict the character set used as salt (#1488136) 
    50         $seedchars = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 
    51         for ($i = 0; $i < $len ; $i++) { 
    52             $salt .= $seedchars[rand(0, 63)]; 
     37        if ($err = $db->is_error()) 
     38            return PASSWORD_ERROR; 
     39 
     40        // crypted password 
     41        if (strpos($sql, '%c') !== FALSE) { 
     42            $salt = ''; 
     43            if (CRYPT_MD5) { 
     44                // Always use eight salt characters for MD5 (#1488136) 
     45                $len = 8; 
     46            } else if (CRYPT_STD_DES) { 
     47                    $len = 2; 
     48            } else { 
     49                    return PASSWORD_CRYPT_ERROR; 
     50            } 
     51 
     52            //Restrict the character set used as salt (#1488136) 
     53            $seedchars = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 
     54            for ($i = 0; $i < $len ; $i++) { 
     55                $salt .= $seedchars[rand(0, 63)]; 
     56            } 
     57 
     58            $sql = str_replace('%c',  $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql); 
    5359        } 
    5460 
    55         $sql = str_replace('%c',  $db->quote(crypt($passwd, CRYPT_MD5 ? '$1$'.$salt.'$' : $salt)), $sql); 
    56     } 
     61        // dovecotpw 
     62        if (strpos($sql, '%D') !== FALSE) { 
     63            if (!($dovecotpw = $rcmail->config->get('password_dovecotpw'))) 
     64                $dovecotpw = 'dovecotpw'; 
     65            if (!($method = $rcmail->config->get('password_dovecotpw_method'))) 
     66                $method = 'CRAM-MD5'; 
    5767 
    58     // dovecotpw 
    59     if (strpos($sql, '%D') !== FALSE) { 
    60         if (!($dovecotpw = $rcmail->config->get('password_dovecotpw'))) 
    61             $dovecotpw = 'dovecotpw'; 
    62         if (!($method = $rcmail->config->get('password_dovecotpw_method'))) 
    63             $method = 'CRAM-MD5'; 
     68            // use common temp dir 
     69            $tmp_dir = $rcmail->config->get('temp_dir'); 
     70            $tmpfile = tempnam($tmp_dir, 'roundcube-'); 
    6471 
    65         // use common temp dir 
    66         $tmp_dir = $rcmail->config->get('temp_dir'); 
    67         $tmpfile = tempnam($tmp_dir, 'roundcube-'); 
     72            $pipe = popen("$dovecotpw -s '$method' > '$tmpfile'", "w"); 
     73            if (!$pipe) { 
     74                unlink($tmpfile); 
     75                return PASSWORD_CRYPT_ERROR; 
     76            } 
     77            else { 
     78                fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); usleep(1000); 
     79                fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); 
     80                pclose($pipe); 
     81                $newpass = trim(file_get_contents($tmpfile), "\n"); 
     82                if (!preg_match('/^\{' . $method . '\}/', $newpass)) { 
     83                    return PASSWORD_CRYPT_ERROR; 
     84                } 
     85                if (!$rcmail->config->get('password_dovecotpw_with_method')) 
     86                    $newpass = trim(str_replace('{' . $method . '}', '', $newpass)); 
     87                unlink($tmpfile); 
     88            } 
     89            $sql = str_replace('%D', $db->quote($newpass), $sql); 
     90        } 
    6891 
    69         $pipe = popen("$dovecotpw -s '$method' > '$tmpfile'", "w"); 
    70         if (!$pipe) { 
    71             unlink($tmpfile); 
    72             return PASSWORD_CRYPT_ERROR; 
     92        // hashed passwords 
     93        if (preg_match('/%[n|q]/', $sql)) { 
     94            if (!extension_loaded('hash')) { 
     95                    raise_error(array( 
     96                        'code' => 600, 
     97                            'type' => 'php', 
     98                            'file' => __FILE__, 'line' => __LINE__, 
     99                        'message' => "Password plugin: 'hash' extension not loaded!" 
     100                    ), true, false); 
     101 
     102                    return PASSWORD_ERROR; 
     103            } 
     104 
     105                if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm')))) 
     106                $hash_algo = 'sha1'; 
     107 
     108            $hash_passwd = hash($hash_algo, $passwd); 
     109            $hash_curpass = hash($hash_algo, $curpass); 
     110 
     111                if ($rcmail->config->get('password_hash_base64')) { 
     112                $hash_passwd = base64_encode(pack('H*', $hash_passwd)); 
     113                $hash_curpass = base64_encode(pack('H*', $hash_curpass)); 
     114            } 
     115 
     116            $sql = str_replace('%n', $db->quote($hash_passwd, 'text'), $sql); 
     117                $sql = str_replace('%q', $db->quote($hash_curpass, 'text'), $sql); 
     118        } 
     119 
     120        // Handle clear text passwords securely (#1487034) 
     121        $sql_vars = array(); 
     122        if (preg_match_all('/%[p|o]/', $sql, $m)) { 
     123            foreach ($m[0] as $var) { 
     124                if ($var == '%p') { 
     125                    $sql = preg_replace('/%p/', '?', $sql, 1); 
     126                    $sql_vars[] = (string) $passwd; 
     127                } 
     128                else { // %o 
     129                    $sql = preg_replace('/%o/', '?', $sql, 1); 
     130                    $sql_vars[] = (string) $curpass; 
     131                } 
     132            } 
     133        } 
     134 
     135        $local_part  = $rcmail->user->get_username('local'); 
     136        $domain_part = $rcmail->user->get_username('domain'); 
     137        $username    = $_SESSION['username']; 
     138        $host        = $_SESSION['imap_host']; 
     139 
     140        // convert domains to/from punnycode 
     141        if ($rcmail->config->get('password_idn_ascii')) { 
     142            $domain_part = rcube_idn_to_ascii($domain_part); 
     143            $username    = rcube_idn_to_ascii($username); 
     144            $host        = rcube_idn_to_ascii($host); 
    73145        } 
    74146        else { 
    75             fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); usleep(1000); 
    76             fwrite($pipe, $passwd . "\n", 1+strlen($passwd)); 
    77             pclose($pipe); 
    78             $newpass = trim(file_get_contents($tmpfile), "\n"); 
    79             if (!preg_match('/^\{' . $method . '\}/', $newpass)) { 
    80                 return PASSWORD_CRYPT_ERROR; 
    81             } 
    82             if (!$rcmail->config->get('password_dovecotpw_with_method')) 
    83                 $newpass = trim(str_replace('{' . $method . '}', '', $newpass)); 
    84             unlink($tmpfile); 
    85         } 
    86         $sql = str_replace('%D', $db->quote($newpass), $sql); 
    87     } 
    88  
    89     // hashed passwords 
    90     if (preg_match('/%[n|q]/', $sql)) { 
    91  
    92             if (!extension_loaded('hash')) { 
    93                 raise_error(array( 
    94                     'code' => 600, 
    95                         'type' => 'php', 
    96                         'file' => __FILE__, 'line' => __LINE__, 
    97                         'message' => "Password plugin: 'hash' extension not loaded!" 
    98                     ), true, false); 
    99  
    100                 return PASSWORD_ERROR; 
    101             } 
    102  
    103             if (!($hash_algo = strtolower($rcmail->config->get('password_hash_algorithm')))) 
    104             $hash_algo = 'sha1'; 
    105  
    106             $hash_passwd = hash($hash_algo, $passwd); 
    107         $hash_curpass = hash($hash_algo, $curpass); 
    108  
    109             if ($rcmail->config->get('password_hash_base64')) { 
    110             $hash_passwd = base64_encode(pack('H*', $hash_passwd)); 
    111             $hash_curpass = base64_encode(pack('H*', $hash_curpass)); 
     147            $domain_part = rcube_idn_to_utf8($domain_part); 
     148            $username    = rcube_idn_to_utf8($username); 
     149            $host        = rcube_idn_to_utf8($host); 
    112150        } 
    113151 
    114             $sql = str_replace('%n', $db->quote($hash_passwd, 'text'), $sql); 
    115             $sql = str_replace('%q', $db->quote($hash_curpass, 'text'), $sql); 
     152        // at least we should always have the local part 
     153        $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql); 
     154        $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql); 
     155        $sql = str_replace('%u', $db->quote($username, 'text'), $sql); 
     156        $sql = str_replace('%h', $db->quote($host, 'text'), $sql); 
     157 
     158        $res = $db->query($sql, $sql_vars); 
     159 
     160        if (!$db->is_error()) { 
     161                if (strtolower(substr(trim($query),0,6))=='select') { 
     162                if ($result = $db->fetch_array($res)) 
     163                            return PASSWORD_SUCCESS; 
     164            } else { 
     165                // This is the good case: 1 row updated 
     166                if ($db->affected_rows($res) == 1) 
     167                        return PASSWORD_SUCCESS; 
     168                // @TODO: Some queries don't affect any rows 
     169                // Should we assume a success if there was no error? 
     170                } 
     171        } 
     172 
     173        return PASSWORD_ERROR; 
    116174    } 
    117  
    118     // Handle clear text passwords securely (#1487034) 
    119     $sql_vars = array(); 
    120     if (preg_match_all('/%[p|o]/', $sql, $m)) { 
    121         foreach ($m[0] as $var) { 
    122             if ($var == '%p') { 
    123                 $sql = preg_replace('/%p/', '?', $sql, 1); 
    124                 $sql_vars[] = (string) $passwd; 
    125             } 
    126             else { // %o 
    127                 $sql = preg_replace('/%o/', '?', $sql, 1); 
    128                 $sql_vars[] = (string) $curpass; 
    129             } 
    130         } 
    131     } 
    132  
    133     $local_part  = $rcmail->user->get_username('local'); 
    134     $domain_part = $rcmail->user->get_username('domain'); 
    135     $username    = $_SESSION['username']; 
    136     $host        = $_SESSION['imap_host']; 
    137  
    138     // convert domains to/from punnycode 
    139     if ($rcmail->config->get('password_idn_ascii')) { 
    140         $domain_part = rcube_idn_to_ascii($domain_part); 
    141         $username    = rcube_idn_to_ascii($username); 
    142         $host        = rcube_idn_to_ascii($host); 
    143     } 
    144     else { 
    145         $domain_part = rcube_idn_to_utf8($domain_part); 
    146         $username    = rcube_idn_to_utf8($username); 
    147         $host        = rcube_idn_to_utf8($host); 
    148     } 
    149  
    150     // at least we should always have the local part 
    151     $sql = str_replace('%l', $db->quote($local_part, 'text'), $sql); 
    152     $sql = str_replace('%d', $db->quote($domain_part, 'text'), $sql); 
    153     $sql = str_replace('%u', $db->quote($username, 'text'), $sql); 
    154     $sql = str_replace('%h', $db->quote($host, 'text'), $sql); 
    155  
    156     $res = $db->query($sql, $sql_vars); 
    157  
    158     if (!$db->is_error()) { 
    159             if (strtolower(substr(trim($query),0,6))=='select') { 
    160             if ($result = $db->fetch_array($res)) 
    161                         return PASSWORD_SUCCESS; 
    162             } else { 
    163             // This is the good case: 1 row updated 
    164             if ($db->affected_rows($res) == 1) 
    165                     return PASSWORD_SUCCESS; 
    166             // @TODO: Some queries don't affect any rows 
    167             // Should we assume a success if there was no error? 
    168             } 
    169     } 
    170  
    171     return PASSWORD_ERROR; 
    172175} 
    173  
    174 ?> 
  • trunk/plugins/password/drivers/virtualmin.php

    r4546 r5902  
    1111 * and requires shell access and gcc in order to compile the binary. 
    1212 * 
    13  * @version 2.0 
     13 * @version 3.0 
    1414 * @author Martijn de Munnik 
    1515 */ 
    1616 
    17 function password_save($currpass, $newpass) 
     17class rcube_virtualmin_password 
    1818{ 
    19     $rcmail = rcmail::get_instance(); 
     19    function save($currpass, $newpass) 
     20    { 
     21        $rcmail = rcmail::get_instance(); 
    2022 
    21     $format   = $rcmail->config->get('password_virtualmin_format', 0); 
    22     $username = $_SESSION['username']; 
     23        $format   = $rcmail->config->get('password_virtualmin_format', 0); 
     24        $username = $_SESSION['username']; 
    2325 
    24     switch ($format) { 
     26        switch ($format) { 
    2527        case 1: // username%domain 
    2628            $domain = substr(strrchr($username, "%"), 1); 
     
    4951        default: // username@domain 
    5052            $domain = substr(strrchr($username, "@"), 1); 
     53        } 
     54 
     55        $username = escapeshellcmd($username); 
     56        $domain   = escapeshellcmd($domain); 
     57        $newpass  = escapeshellcmd($newpass); 
     58        $curdir   = realpath(dirname(__FILE__)); 
     59 
     60        exec("$curdir/chgvirtualminpasswd modify-user --domain $domain --user $username --pass $newpass", $output, $returnvalue); 
     61 
     62        if ($returnvalue == 0) { 
     63            return PASSWORD_SUCCESS; 
     64        } 
     65        else { 
     66            raise_error(array( 
     67                'code' => 600, 
     68                'type' => 'php', 
     69                'file' => __FILE__, 'line' => __LINE__, 
     70                'message' => "Password plugin: Unable to execute $curdir/chgvirtualminpasswd" 
     71                ), true, false); 
     72        } 
     73 
     74        return PASSWORD_ERROR; 
    5175    } 
    52                                                                                                                                                                                                                                                                                                              
    53     $username = escapeshellcmd($username); 
    54     $domain   = escapeshellcmd($domain); 
    55     $newpass  = escapeshellcmd($newpass); 
    56     $curdir   = realpath(dirname(__FILE__)); 
    57  
    58     exec("$curdir/chgvirtualminpasswd modify-user --domain $domain --user $username --pass $newpass", $output, $returnvalue); 
    59  
    60     if ($returnvalue == 0) { 
    61         return PASSWORD_SUCCESS; 
    62     } 
    63     else { 
    64         raise_error(array( 
    65             'code' => 600, 
    66             'type' => 'php', 
    67             'file' => __FILE__, 'line' => __LINE__, 
    68             'message' => "Password plugin: Unable to execute $curdir/chgvirtualminpasswd" 
    69             ), true, false); 
    70     } 
    71  
    72     return PASSWORD_ERROR; 
    7376} 
    74  
    75 ?> 
  • trunk/plugins/password/drivers/vpopmaild.php

    r3257 r5902  
    66 * Driver to change passwords via vpopmaild 
    77 * 
    8  * @version 1.1 
     8 * @version 2.0 
    99 * @author Johannes Hessellund 
    1010 * 
    1111 */ 
    1212 
    13 function password_save($curpass, $passwd) 
     13class rcube_vpopmaild_password 
    1414{ 
    15     $rcmail = rcmail::get_instance(); 
    16 //    include('Net/Socket.php'); 
    17     $vpopmaild = new Net_Socket(); 
     15    function save($curpass, $passwd) 
     16    { 
     17        $rcmail = rcmail::get_instance(); 
     18    //    include('Net/Socket.php'); 
     19        $vpopmaild = new Net_Socket(); 
    1820 
    19     if (PEAR::isError($vpopmaild->connect($rcmail->config->get('password_vpopmaild_host'), 
    20             $rcmail->config->get('password_vpopmaild_port'), null))) { 
    21         return PASSWORD_CONNECT_ERROR; 
    22     } 
     21        if (PEAR::isError($vpopmaild->connect($rcmail->config->get('password_vpopmaild_host'), 
     22                $rcmail->config->get('password_vpopmaild_port'), null))) { 
     23            return PASSWORD_CONNECT_ERROR; 
     24        } 
    2325 
    24     $result = $vpopmaild->readLine(); 
    25     if(!preg_match('/^\+OK/', $result)) { 
    26         $vpopmaild->disconnect(); 
    27         return PASSWORD_CONNECT_ERROR; 
    28     } 
     26        $result = $vpopmaild->readLine(); 
     27        if(!preg_match('/^\+OK/', $result)) { 
     28            $vpopmaild->disconnect(); 
     29            return PASSWORD_CONNECT_ERROR; 
     30        } 
    2931 
    30     $vpopmaild->writeLine("slogin ". $_SESSION['username'] . " " . $curpass); 
    31     $result = $vpopmaild->readLine(); 
    32     if(!preg_match('/^\+OK/', $result) ) { 
     32        $vpopmaild->writeLine("slogin ". $_SESSION['username'] . " " . $curpass); 
     33        $result = $vpopmaild->readLine(); 
     34 
     35        if(!preg_match('/^\+OK/', $result) ) { 
     36            $vpopmaild->writeLine("quit"); 
     37            $vpopmaild->disconnect(); 
     38            return PASSWORD_ERROR; 
     39        } 
     40 
     41        $vpopmaild->writeLine("mod_user ". $_SESSION['username']); 
     42        $vpopmaild->writeLine("clear_text_password ". $passwd); 
     43        $vpopmaild->writeLine("."); 
     44        $result = $vpopmaild->readLine(); 
    3345        $vpopmaild->writeLine("quit"); 
    3446        $vpopmaild->disconnect(); 
    35         return PASSWORD_ERROR; 
     47 
     48        if (!preg_match('/^\+OK/', $result)) 
     49            return PASSWORD_ERROR; 
     50 
     51        return PASSWORD_SUCCESS; 
    3652    } 
    37  
    38     $vpopmaild->writeLine("mod_user ". $_SESSION['username']); 
    39     $vpopmaild->writeLine("clear_text_password ". $passwd); 
    40     $vpopmaild->writeLine("."); 
    41     $result = $vpopmaild->readLine(); 
    42     $vpopmaild->writeLine("quit"); 
    43     $vpopmaild->disconnect(); 
    44  
    45     if (!preg_match('/^\+OK/', $result)) 
    46         return PASSWORD_ERROR; 
    47  
    48     return PASSWORD_SUCCESS; 
    4953} 
    50  
    51 ?> 
  • trunk/plugins/password/drivers/ximss.php

    r3030 r5902  
    1313 *   http://www.communigate.com/WebGuide/XMLAPI.html 
    1414 * 
    15  * @version 1 
     15 * @version 2.0 
    1616 * @author Erik Meitner <erik wanderings.us> 
    1717 */ 
    18   
    19 function password_save($pass, $newpass) 
     18 
     19class rcube_ximss_password 
    2020{ 
     21    function save($pass, $newpass) 
     22    { 
     23        $rcmail = rcmail::get_instance(); 
    2124 
    22   $rcmail = rcmail::get_instance(); 
    23    
    24   $sock = stream_socket_client("tcp://".$rcmail->config->get('password_ximss_host').":".$rcmail->config->get('password_ximss_port'), $errno, $errstr, 30) ; 
    25   if( $sock === FALSE ) 
    26   { 
    27     return PASSWORD_CONNECT_ERROR; 
    28   } 
    29    
    30   // send all requests at once(pipelined) 
    31   fwrite( $sock, '<login id="A001" authData="'.$_SESSION['username'].'" password="'.$pass.'" />'."\0"); 
    32   fwrite( $sock, '<passwordModify id="A002" oldPassword="'.$pass.'" newPassword="'.$newpass.'"  />'."\0"); 
    33   fwrite( $sock, '<bye id="A003" />'."\0"); 
     25        $host = $rcmail->config->get('password_ximss_host'); 
     26        $port = $rcmail->config->get('password_ximss_port'); 
     27        $sock = stream_socket_client("tcp://$host:$port", $errno, $errstr, 30); 
     28 
     29        if ($sock === FALSE) { 
     30            return PASSWORD_CONNECT_ERROR; 
     31        } 
     32 
     33        // send all requests at once(pipelined) 
     34        fwrite( $sock, '<login id="A001" authData="'.$_SESSION['username'].'" password="'.$pass.'" />'."\0"); 
     35        fwrite( $sock, '<passwordModify id="A002" oldPassword="'.$pass.'" newPassword="'.$newpass.'"  />'."\0"); 
     36        fwrite( $sock, '<bye id="A003" />'."\0"); 
    3437 
    3538  //example responses 
     
    4144  //  <response id="A001" errorText="incorrect password or account name" errorNum="515"/>\0 
    4245 
    43   $responseblob = ''; 
    44   while (!feof($sock)) { 
    45     $responseblob .= fgets($sock, 1024); 
    46   } 
     46        $responseblob = ''; 
     47        while (!feof($sock)) { 
     48            $responseblob .= fgets($sock, 1024); 
     49        } 
    4750 
    48   fclose($sock); 
    49    
    50   foreach( explode( "\0",$responseblob) as $response ) 
    51   { 
    52     $resp = simplexml_load_string("<xml>".$response."</xml>"); 
     51        fclose($sock); 
    5352 
    54     if( $resp->response[0]['id'] == 'A001' ) 
    55     { 
    56       if( isset( $resp->response[0]['errorNum'] ) ) 
    57       { 
    58         return PASSWORD_CONNECT_ERROR; 
    59       }   
     53        foreach( explode( "\0",$responseblob) as $response ) { 
     54            $resp = simplexml_load_string("<xml>".$response."</xml>"); 
     55 
     56            if( $resp->response[0]['id'] == 'A001' ) { 
     57                if( isset( $resp->response[0]['errorNum'] ) ) { 
     58                    return PASSWORD_CONNECT_ERROR; 
     59                } 
     60            } 
     61            else if( $resp->response[0]['id'] == 'A002' ) { 
     62                if( isset( $resp->response[0]['errorNum'] )) { 
     63                    return PASSWORD_ERROR; 
     64                } 
     65            } 
     66            else if( $resp->response[0]['id'] == 'A003' ) { 
     67                if( isset($resp->response[0]['errorNum'] )) { 
     68                    //There was a problem during logout(This is probably harmless) 
     69                } 
     70            } 
     71        } //foreach 
     72 
     73        return PASSWORD_SUCCESS; 
     74 
    6075    } 
    61     else if( $resp->response[0]['id'] == 'A002' ) 
    62     { 
    63       if( isset( $resp->response[0]['errorNum'] )) 
    64       { 
    65         return PASSWORD_ERROR; 
    66       }   
    67     } 
    68     else if( $resp->response[0]['id'] == 'A003' ) 
    69     { 
    70       if( isset($resp->response[0]['errorNum'] )) 
    71       { 
    72         //There was a problem during logout(This is probably harmless) 
    73       }   
    74     } 
    75   } //foreach 
    76  
    77   return PASSWORD_SUCCESS; 
    78    
    7976} 
    80    
    81 ?> 
  • trunk/plugins/password/drivers/xmail.php

    r4507 r5902  
    55 * Driver for XMail password 
    66 * 
    7  * @version 1.0 
     7 * @version 2.0 
    88 * @author Helio Cavichiolo Jr <helio@hcsistemas.com.br> 
    99 * 
     
    1818 */ 
    1919 
    20 function password_save($currpass, $newpass) 
     20class rcube_xmail_password 
    2121{ 
    22     $rcmail = rcmail::get_instance(); 
    23     list($user,$domain) = explode('@', $_SESSION['username']); 
     22    function save($currpass, $newpass) 
     23    { 
     24        $rcmail = rcmail::get_instance(); 
     25        list($user,$domain) = explode('@', $_SESSION['username']); 
    2426 
    25     $xmail = new XMail; 
     27        $xmail = new XMail; 
    2628 
    27     $xmail->hostname = $rcmail->config->get('xmail_host'); 
    28     $xmail->username = $rcmail->config->get('xmail_user'); 
    29     $xmail->password = $rcmail->config->get('xmail_pass'); 
    30     $xmail->port = $rcmail->config->get('xmail_port'); 
     29        $xmail->hostname = $rcmail->config->get('xmail_host'); 
     30        $xmail->username = $rcmail->config->get('xmail_user'); 
     31        $xmail->password = $rcmail->config->get('xmail_pass'); 
     32        $xmail->port = $rcmail->config->get('xmail_port'); 
    3133 
    32     if (!$xmail->connect()) { 
    33         raise_error(array( 
    34             'code' => 600, 
    35             'type' => 'php', 
    36             'file' => __FILE__, 'line' => __LINE__, 
    37             'message' => "Password plugin: Unable to connect to mail server" 
    38         ), true, false); 
    39         return PASSWORD_CONNECT_ERROR; 
    40     } else if (!$xmail->send("userpasswd\t".$domain."\t".$user."\t".$newpass."\n")) { 
    41         $xmail->close(); 
    42         raise_error(array( 
    43             'code' => 600, 
    44             'type' => 'php', 
    45             'file' => __FILE__, 'line' => __LINE__, 
    46             'message' => "Password plugin: Unable to change password" 
    47         ), true, false); 
    48         return PASSWORD_ERROR; 
    49     } else { 
    50         $xmail->close(); 
    51         return PASSWORD_SUCCESS; 
     34        if (!$xmail->connect()) { 
     35            raise_error(array( 
     36                'code' => 600, 
     37                'type' => 'php', 
     38                'file' => __FILE__, 'line' => __LINE__, 
     39                'message' => "Password plugin: Unable to connect to mail server" 
     40            ), true, false); 
     41            return PASSWORD_CONNECT_ERROR; 
     42        } 
     43        else if (!$xmail->send("userpasswd\t".$domain."\t".$user."\t".$newpass."\n")) { 
     44            $xmail->close(); 
     45            raise_error(array( 
     46                'code' => 600, 
     47                'type' => 'php', 
     48                'file' => __FILE__, 'line' => __LINE__, 
     49                'message' => "Password plugin: Unable to change password" 
     50            ), true, false); 
     51            return PASSWORD_ERROR; 
     52        } 
     53        else { 
     54            $xmail->close(); 
     55            return PASSWORD_SUCCESS; 
     56        } 
    5257    } 
    5358} 
  • trunk/plugins/password/package.xml

    r5467 r5902  
    1818        <date>2011-11-23</date> 
    1919        <version> 
    20                 <release>2.4</release> 
    21                 <api>1.6</api> 
     20                <release>3.0</release> 
     21                <api>2.0</api> 
    2222        </version> 
    2323        <stability> 
     
    2727        <license uri="http://www.gnu.org/licenses/gpl-2.0.html">GNU GPLv2</license> 
    2828        <notes> 
    29 - Added option to use punycode or unicode for domain names (#1488103) 
    30 - Save Samba password hashes in capital letters (#1488197) 
     29- Fixed drivers namespace issues 
    3130    </notes> 
    3231        <contents> 
     
    288287            </notes> 
    289288        </release> 
     289        <release> 
     290                <date>2011-11-23</date> 
     291                <version> 
     292                        <release>2.4</release> 
     293                        <api>1.6</api> 
     294                </version> 
     295                <stability> 
     296                        <release>stable</release> 
     297                        <api>stable</api> 
     298                </stability> 
     299                <license uri="http://www.gnu.org/licenses/gpl-2.0.html">GNU GPLv2</license> 
     300                <notes> 
     301- Added option to use punycode or unicode for domain names (#1488103) 
     302- Save Samba password hashes in capital letters (#1488197) 
     303            </notes> 
     304        </release> 
    290305    </changelog> 
    291306</package> 
  • trunk/plugins/password/password.php

    r5901 r5902  
    178178            $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id, 
    179179                'size' => 20, 'autocomplete' => 'off')); 
    180    
     180 
    181181            $table->add('title', html::label($field_id, Q($this->gettext('curpasswd')))); 
    182182            $table->add(null, $input_curpasswd->show()); 
     
    223223    { 
    224224        $config = rcmail::get_instance()->config; 
    225         $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php'; 
    226  
    227         if (!is_readable($driver)) { 
     225        $driver = $config->get('password_driver', 'sql'); 
     226        $class  = "rcube_{$driver}_password"; 
     227        $file   = $this->home . "/drivers/$driver.php"; 
     228 
     229        if (!file_exists($file)) { 
    228230            raise_error(array( 
    229231                'code' => 600, 
    230232                'type' => 'php', 
    231233                'file' => __FILE__, 'line' => __LINE__, 
    232                 'message' => "Password plugin: Unable to open driver file $driver" 
     234                'message' => "Password plugin: Unable to open driver file ($file)" 
    233235            ), true, false); 
    234236            return $this->gettext('internalerror'); 
    235237        } 
    236238 
    237         include($driver); 
    238  
    239         if (!function_exists('password_save')) { 
     239        include_once $file; 
     240 
     241        if (!class_exists($class, false) || !method_exists($class, 'save')) { 
    240242            raise_error(array( 
    241243                'code' => 600, 
    242244                'type' => 'php', 
    243245                'file' => __FILE__, 'line' => __LINE__, 
    244                 'message' => "Password plugin: Broken driver: $driver" 
     246                'message' => "Password plugin: Broken driver $driver" 
    245247            ), true, false); 
    246248            return $this->gettext('internalerror'); 
    247249        } 
    248250 
    249         $result = password_save($curpass, $passwd); 
     251        $object = new $class; 
     252        $result = $object->save($curpass, $passwd); 
    250253 
    251254        if (is_array($result)) { 
Note: See TracChangeset for help on using the changeset viewer.