Overview

Packages

  • None
  • Passwd

Classes

  • Passwd
  • Passwd_Driver
  • Passwd_Driver_Adsi
  • Passwd_Driver_Composite
  • Passwd_Driver_Expect
  • Passwd_Driver_Expectpecl
  • Passwd_Driver_Horde
  • Passwd_Driver_Http
  • Passwd_Driver_Kolab
  • Passwd_Driver_Ldap
  • Passwd_Driver_Pine
  • Passwd_Driver_Poppassd
  • Passwd_Driver_Procopen
  • Passwd_Driver_Pspasswd
  • Passwd_Driver_Servuftp
  • Passwd_Driver_Smbldap
  • Passwd_Driver_Smbpasswd
  • Passwd_Driver_Soap
  • Passwd_Driver_Sql
  • Passwd_Driver_Vmailmgr
  • Passwd_Driver_Vpopmail
  • Passwd_Exception
  • Passwd_Factory_Driver
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Passwd_expectpecl class provides an PECL expect implementation of the
  4:  * Passwd system.
  5:  *
  6:  * Copyright 2006-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (GPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/gpl.php.
 10:  *
 11:  * @author  Duck <duck@obala.net>
 12:  * @package Passwd
 13:  */
 14: class Passwd_Driver_Expectpecl extends Passwd_Driver
 15: {
 16:     /**
 17:      * Expect connection handle.
 18:      *
 19:      * @var resource
 20:      */
 21:     protected $_stream;
 22: 
 23:     /**
 24:      * Handles expect communication.
 25:      *
 26:      * @param string $expect  String to expect
 27:      * @param string $error   Error message
 28:      *
 29:      * @throws Passwd_Exception
 30:      */
 31:     protected function _ctl($expect, $error)
 32:     {
 33:         $cases = array(array(0 => $expect,
 34:                              1 => 'ok',
 35:                              2 => EXP_REGEXP));
 36: 
 37:         $result = expect_expectl($this->_stream, $cases);
 38: 
 39:         switch ($result) {
 40:         case EXP_EOF:
 41:             throw new Passwd_Exception(_("End of file."));
 42:         case EXP_TIMEOUT:
 43:             throw new Passwd_Exception(_("Time out."));    
 44:         case EXP_FULLBUFFER:
 45:             throw new Passwd_Exception(_("Full buffer."));
 46:         case 'ok':
 47:             return;
 48:         default:
 49:             throw new Passwd_Exception($error);
 50:         }
 51:     }
 52: 
 53:     /**
 54:      * Changes the user's password.
 55:      *
 56:      * @param string $user          The user for which to change the password.
 57:      * @param string $old_password  The old (current) user password.
 58:      * @param string $new_password  The new user password to set.
 59:      *
 60:      * @throws Passwd_Exception
 61:      */
 62:     public function changePassword($user, $old_password, $new_password)
 63:     {
 64:         if (!Horde_Util::loadExtension('expect')) {
 65:             throw new Passwd_Exception(_("expect extension cannot be loaded"));
 66:         }
 67: 
 68:         // Set up parameters
 69:         if (isset($this->_params['timeout'])) {
 70:             ini_set('expect.timeout', $this->_params['timeout']);
 71:         }
 72:         if (isset($this->_params['loguser'])) {
 73:             ini_set('expect.loguser', $this->_params['loguser']);
 74:         }
 75:         if (isset($this->_params['logfile'])) {
 76:             ini_set('expect.logfile', $this->_params['logfile']);
 77:         }
 78: 
 79:         // Open connection
 80:         $call = sprintf('ssh %s@%s %s',
 81:                         $user,
 82:                         $this->_params['host'],
 83:                         $this->_params['program']);
 84:         if (!($this->_stream = expect_popen($call))) {
 85:             throw new Passwd_Exception(_("Unable to open expect stream"));
 86:         }
 87: 
 88:         // Log in
 89:         $this->_ctl('(P|p)assword.*',
 90:                    _("Could not login to system (no password prompt)"));
 91: 
 92:         // Send login password
 93:         fwrite($this->_stream, "$old_password\n");
 94: 
 95:         // Expect old password prompt
 96:         $this->_ctl('((O|o)ld|login|current).* (P|p)assword.*',
 97:                    _("Could not start passwd program (no old password prompt)"));
 98: 
 99:         // Send old password
100:         fwrite($this->_stream, "$old_password\n");
101: 
102:         // Expect new password prompt
103:         $this->_ctl('(N|n)ew.* (P|p)assword.*',
104:                    _("Could not change password (bad old password?)"));
105: 
106:         // Send new password
107:         fwrite($this->_stream, "$new_password\n");
108: 
109:         // Expect reenter password prompt
110:         $this->_ctl("((R|r)e-*enter.*(P|p)assword|Retype new( UNIX)? password|(V|v)erification|(V|v)erify|(A|a)gain).*",
111:                  _("New password not valid (too short, bad password, too similar, ...)"));
112: 
113:         // Send new password
114:         fwrite($this->_stream, "$new_password\n");
115: 
116:         // Expect successfully message
117:         $this->_ctl('((P|p)assword.* changed|successfully)',
118:                    _("Could not change password."));
119:     }
120: }
121: 
API documentation generated by ApiGen