1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: class Passwd_Driver_Expectpecl extends Passwd_Driver
15: {
16: 17: 18: 19: 20:
21: protected $_stream;
22:
23: 24: 25: 26: 27: 28: 29: 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: 55: 56: 57: 58: 59: 60: 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:
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:
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:
89: $this->_ctl('(P|p)assword.*',
90: _("Could not login to system (no password prompt)"));
91:
92:
93: fwrite($this->_stream, "$old_password\n");
94:
95:
96: $this->_ctl('((O|o)ld|login|current).* (P|p)assword.*',
97: _("Could not start passwd program (no old password prompt)"));
98:
99:
100: fwrite($this->_stream, "$old_password\n");
101:
102:
103: $this->_ctl('(N|n)ew.* (P|p)assword.*',
104: _("Could not change password (bad old password?)"));
105:
106:
107: fwrite($this->_stream, "$new_password\n");
108:
109:
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:
114: fwrite($this->_stream, "$new_password\n");
115:
116:
117: $this->_ctl('((P|p)assword.* changed|successfully)',
118: _("Could not change password."));
119: }
120: }
121: