This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
roundcube_yubikey_authentication.php
230 lines (201 loc) · 6.22 KB
/
roundcube_yubikey_authentication.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Roundcube-YubiKey-plugin
*
* This plugin enables YubiKey authentication within Roundcube webmail against
* the YubiKey web service API.
*
* @author Danny Fullerton <[email protected]>
* @license GPL2
*
* Acknowledgement: This code is based on work done by Oliver Martin which was
* using patches from dirkm.
*
*/
require_once('lib/Yubico.php');
class roundcube_yubikey_authentication extends rcube_plugin
{
private function is_enabled()
{
$r = ($this->get('yubikey') === true);
return $r;
}
private function is_required()
{
$r = ($this->get('yubikey_required') == 'on');
return $r;
}
private function disallow_change()
{
$r = false;
if ($this->get('yubikey_disallow_user_changes') === true) {
$r = ($this->is_required() && strlen($this->get('yubikey_id')) == 12);
}
return $r;
}
private function get($v)
{
return rcmail::get_instance()->config->get($v);
}
// TODO add error message
private function fail()
{
rcmail::get_instance()->logout_actions();
rcmail::get_instance()->kill_session();
}
function init()
{
$this->load_config();
// minimal configuration validation
$id = $this->get('yubikey_api_id');
$key = $this->get('yubikey_api_key');
if ($this->is_enabled() && (empty($id) || empty($key)))
throw new Exception('yubikey_api_id and yubikey_api_key must be set');
$this->add_texts('localization/', true);
$this->add_hook('preferences_list', array($this, 'preferences_list'));
$this->add_hook('preferences_save', array($this, 'preferences_save'));
$this->add_hook('template_object_loginform', array($this, 'update_login_form'));
$this->add_hook('login_after', array($this, 'login_after'));
}
function update_login_form($p)
{
if ($this->is_enabled())
$this->include_script('yubikey.js');
return $p;
}
function login_after($args)
{
if (!$this->is_enabled() || !$this->is_required()) return $args;
$otp = rcube_utils::get_input_value('_yubikey', rcube_utils::INPUT_POST);
$id = $this->get('yubikey_id');
$id2 = $this->get('yubikey_id2');
$id3 = $this->get('yubikey_id3');
$url = $this->get('yubikey_api_url');
$https = true;
if (!empty($url) && $_url = parse_url($url)) {
if ($_url['scheme'] == "http") $https = false;
$urlpart = $_url['host'];
if (!empty($_url['port'])) $urlpart .= ':'.$_url['port'];
$urlpart .= $_url['path'];
}
// make sure that there is a YubiKey ID in the user's prefs
// and that it matches the first 12 characters of the OTP
if (empty($id) && empty($id2) && empty($id3))
{
$this->fail();
}
if (substr($otp, 0, 12) !== $id && substr($otp, 0, 12) !== $id2 && substr($otp, 0, 12) !== $id3 )
{
$this->fail();
}
else
{
try
{
$yubi = new Auth_Yubico(
$this->get('yubikey_api_id'),
$this->get('yubikey_api_key'),
$https,
true
);
if (!empty($urlpart)) $yubi->addURLpart($urlpart);
$yubi->verify($otp);
}
catch(Exception $e)
{
$this->fail();
}
}
return $args;
}
function preferences_list($args)
{
if ($args['section'] != 'server' || !$this->is_enabled()) return $args;
$disabled = $this->disallow_change();
// add checkbox to enable/disable YubiKey auth for the current user
$chk_yubikey = new html_checkbox(
array(
'name' => '_yubikey_required',
'id' => 'rcmfd_yubikey_required',
'disabled' => $disabled
)
);
$args['blocks']['main']['options']['yubikey_required'] = array(
'title' => html::label(
'rcmfd_yubikey_required',
rcube::Q($this->gettext('yubikeyrequired'))
),
'content' => $chk_yubikey->show(!$this->is_required()) // TODO this is weird
);
// add inputfield for the YubiKey id
$input_yubikey_id = new html_inputfield(
array(
'name' => '_yubikey_id',
'id' => 'rcmfd_yubikey_id',
'size' => 12,
'disabled' => $disabled
)
);
$args['blocks']['main']['options']['yubikey_id'] = array(
'title' => html::label(
'rcmfd_yubikey_id',
rcube::Q($this->gettext('yubikeyid'))
),
'content' => $input_yubikey_id->show($this->get('yubikey_id'))
);
// add inputfield for the YubiKey id2
$input_yubikey_id2 = new html_inputfield(
array(
'name' => '_yubikey_id2',
'id' => 'rcmfd_yubikey_id2',
'size' => 12,
'disabled' => $disabled
)
);
$args['blocks']['main']['options']['yubikey_id2'] = array(
'title' => html::label(
'rcmfd_yubikey_id2',
rcube::Q($this->gettext('yubikeyid2'))
),
'content' => $input_yubikey_id2->show($this->get('yubikey_id2'))
);
// add inputfield for the YubiKey id3
$input_yubikey_id3 = new html_inputfield(
array(
'name' => '_yubikey_id3',
'id' => 'rcmfd_yubikey_id3',
'size' => 12,
'disabled' => $disabled
)
);
$args['blocks']['main']['options']['yubikey_id3'] = array(
'title' => html::label(
'rcmfd_yubikey_id3',
rcube::Q($this->gettext('yubikeyid3'))
),
'content' => $input_yubikey_id3->show($this->get('yubikey_id3'))
);
return $args;
}
function preferences_save($args)
{
if (!$this->is_enabled()) return $args;
if ($this->disallow_change())
{
// use values already saved earlier
$args['prefs']['yubikey_required'] = true;
$args['prefs']['yubikey_id'] = $this->get('yubikey_id');
$args['prefs']['yubikey_id2'] = $this->get('yubikey_id2');
$args['prefs']['yubikey_id3'] = $this->get('yubikey_id3');
}
else {
// use newly posted values
$args['prefs']['yubikey_required'] = isset($_POST['_yubikey_required']);
$args['prefs']['yubikey_id'] = substr($_POST['_yubikey_id'], 0, 12);
$args['prefs']['yubikey_id2'] = substr($_POST['_yubikey_id2'], 0, 12);
$args['prefs']['yubikey_id3'] = substr($_POST['_yubikey_id3'], 0, 12);
}
return $args;
}
}
?>