1<?php
2/**
3 * XMail Password Driver
4 *
5 * Driver for XMail password
6 *
7 * @version 2.0
8 * @author Helio Cavichiolo Jr <helio@hcsistemas.com.br>
9 *
10 * Setup xmail_host, xmail_user, xmail_pass and xmail_port into
11 * config.inc.php of password plugin as follows:
12 *
13 * $config['xmail_host'] = 'localhost';
14 * $config['xmail_user'] = 'YourXmailControlUser';
15 * $config['xmail_pass'] = 'YourXmailControlPass';
16 * $config['xmail_port'] = 6017;
17 *
18 * Copyright (C) The Roundcube Dev Team
19 *
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see http://www.gnu.org/licenses/.
32 */
33
34class rcube_xmail_password
35{
36    function save($currpass, $newpass)
37    {
38        $rcmail = rcmail::get_instance();
39        list($user, $domain) = explode('@', $_SESSION['username']);
40
41        $xmail = new XMail;
42
43        $xmail->hostname = $rcmail->config->get('xmail_host');
44        $xmail->username = $rcmail->config->get('xmail_user');
45        $xmail->password = $rcmail->config->get('xmail_pass');
46        $xmail->port     = $rcmail->config->get('xmail_port');
47
48        if (!$xmail->connect()) {
49            rcube::raise_error([
50                    'code' => 600,
51                    'file' => __FILE__,
52                    'line' => __LINE__,
53                    'message' => "Password plugin: Unable to connect to mail server"
54                ], true, false
55            );
56
57            return PASSWORD_CONNECT_ERROR;
58        }
59
60        if (!$xmail->send("userpasswd\t".$domain."\t".$user."\t".$newpass."\n")) {
61            $xmail->close();
62            rcube::raise_error([
63                    'code' => 600,
64                    'file' => __FILE__,
65                    'line' => __LINE__,
66                    'message' => "Password plugin: Unable to change password"
67                ], true, false
68            );
69
70            return PASSWORD_ERROR;
71        }
72
73        $xmail->close();
74        return PASSWORD_SUCCESS;
75    }
76}
77
78class XMail {
79    var $socket;
80    var $hostname = 'localhost';
81    var $username = 'xmail';
82    var $password = '';
83    var $port = 6017;
84
85    function send($msg)
86    {
87        socket_write($this->socket,$msg);
88        if (substr(socket_read($this->socket, 512, PHP_BINARY_READ),0,1) != "+") {
89            return false;
90        }
91        return true;
92    }
93
94    function connect()
95    {
96        $this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
97        if ($this->socket < 0)
98            return false;
99
100        $result = socket_connect($this->socket, $this->hostname, $this->port);
101        if ($result < 0) {
102            socket_close($this->socket);
103            return false;
104        }
105
106        if (substr(socket_read($this->socket, 512, PHP_BINARY_READ),0,1) != "+") {
107            socket_close($this->socket);
108            return false;
109        }
110
111        if (!$this->send("$this->username\t$this->password\n")) {
112            socket_close($this->socket);
113            return false;
114        }
115        return true;
116    }
117
118    function close()
119    {
120        $this->send("quit\n");
121        socket_close($this->socket);
122    }
123}
124