summaryrefslogtreecommitdiff
path: root/plugins/sasl_password/sasl_password.php
blob: 3a23557e9f772b9c2f99a0d237d2b291e3d81203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php

/**
 * Change SASL Password
 *
 * Plugin that adds functionality ty to change the users Cyrus/SASL password.
 * The code is derrived from the Squirrelmail "Change SASL Password" Plugin
 * by Galen Johnson.
 *
 * It only works with saslpasswd2 on the same host where RoundCube runs
 * and requires shell access and gcc in order to compile the binary.
 *
 * For installation instructions please read the README file.
 *
 * @version 1.0
 * @author Thomas Bruederli
 */
class sasl_password extends rcube_plugin
{
  public $task = 'settings';

  function init()
  {
    $rcmail = rcmail::get_instance();
    // add Tab label
    $rcmail->output->add_label('password');
    $this->register_action('plugin.saslpassword', array($this, 'password_init'));
    $this->register_action('plugin.saslpassword-save', array($this, 'password_save'));
    $this->register_handler('plugin.body', array($this, 'password_form'));
    $this->include_script('sasl_password.js');
  }

  function password_init()
  {
    $this->add_texts('locale/');
    $rcmail = rcmail::get_instance();
    $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
    $rcmail->output->send('plugin');
  }
  
  function password_save()
  {
    $rcmail = rcmail::get_instance();

    $this->add_texts('locale/');

    if (!isset($_POST['_curpasswd']) || !isset($_POST['_newpasswd'])) {
      $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
    }
    else {
      $curpwd = get_input_value('_curpasswd', RCUBE_INPUT_POST);
      $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST);

      if ($_SESSION['password'] != $rcmail->encrypt_passwd($curpwd)) {
        $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
      }
      else if ($this->_save($newpwd)) {
        $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
        $_SESSION['password'] = $rcmail->encrypt_passwd($newpwd);
      }
      else {
        $rcmail->output->command('display_message', $this->gettext('errorsaving'), 'error');
      }
    }

    rcmail_overwrite_action('plugin.saslpassword');
    rcmail::get_instance()->output->send('plugin');
  }

  function password_form()
  {
    $rcmail = rcmail::get_instance();

    // add some labels to client
    $rcmail->output->add_label(
        'sasl_password.nopassword',
        'sasl_password.nocurpassword',
        'sasl_password.passwordinconsistency',
        'sasl_password.changepasswd'
    );

    $table = new html_table(array('cols' => 2));

    // show current password selection
    $field_id = 'saslcurpasswd';
    $input_newpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id, 'size' => 25));

    $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
    $table->add(null, $input_newpasswd->show());

    // show new password selection
    $field_id = 'saslnewpasswd';
    $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id, 'size' => 25));

    $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
    $table->add(null, $input_newpasswd->show());

    // show confirm password selection
    $field_id = 'saslconfpasswd';
    $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id, 'size' => 25));

    $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
    $table->add(null, $input_confpasswd->show());

    $out = html::div(array('class' => "settingsbox", 'style' => "margin:0"),
      html::div(array('id' => "userprefs-title"), $this->gettext('changepasswd')) .
      html::div(array('style' => "padding:15px"), $table->show() .
        html::p(null,
          $rcmail->output->button(array(
            'command' => 'plugin.saslpassword-save',
            'type' => 'input',
            'class' => 'button mainaction',
            'label' => 'save'
        )))
      )
    );

    $rcmail->output->add_gui_object('passform', 'password-form');

    return $rcmail->output->form_tag(array(
        'id' => 'password-form',
        'name' => 'password-form',
        'method' => 'post',
        'action' => './?_task=settings&_action=plugin.saslpassword-save',
    ), $out);
  }

  private function _save($passwd)
  {
    $curdir = realpath(dirname(__FILE__));
    $username = escapeshellcmd($_SESSION['username']);
    $code = 1;

    if ($fh = popen("$curdir/chgsaslpasswd -p $username", 'w')) {
      fwrite($fh, $passwd."\n");
      $code = pclose($fh);
    }

    return ($code == 0);
  }

}

?>