author | Dan Fuhry <dan@enanocms.org> |
Fri, 11 Nov 2011 00:30:49 -0500 | |
changeset 37 | 5e946a3f405b |
parent 34 | 6e947fa21237 |
child 38 | d109af008343 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
||
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
2
diff
changeset
|
3 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
37 | 4 |
return true; |
3
d0fe7acaf0e8
Maybe we could actually make yubikey_enable in config not ignored!
Dan
parents:
2
diff
changeset
|
5 |
|
0 | 6 |
// hook into auth |
7 |
$plugins->attachHook('login_process_userdata_json', 'return yubikey_auth_hook_json($userinfo, $req["level"], @$req["remember"]);'); |
|
8 |
// hook into special page init |
|
9 |
$plugins->attachHook('session_started', 'yubikey_add_special_pages();'); |
|
32 | 10 |
// session key security |
11 |
$plugins->attachHook('session_key_calc', 'yubikey_sk_calc($user_id, $key_pieces, $sk_mode);'); |
|
0 | 12 |
|
13 |
function yubikey_auth_hook_json(&$userdata, $level, $remember) |
|
14 |
{ |
|
37 | 15 |
global $db, $session, $paths, $template, $plugins; // Common objects |
16 |
global $lang; |
|
17 |
||
18 |
$do_validate_otp = false; |
|
19 |
$do_validate_user = false; |
|
20 |
$do_validate_pass = false; |
|
21 |
||
22 |
$user_flag = ( $level >= USER_LEVEL_CHPREF ) ? YK_SEC_ELEV_USERNAME : YK_SEC_NORMAL_USERNAME; |
|
23 |
$pass_flag = ( $level >= USER_LEVEL_CHPREF ) ? YK_SEC_ELEV_PASSWORD : YK_SEC_NORMAL_PASSWORD; |
|
24 |
||
25 |
$auth_log_prefix = ( $level >= USER_LEVEL_CHPREF ) ? 'admin_' : ''; |
|
26 |
||
27 |
// Sort of a hack: if the password looks like an OTP and the OTP field is empty, use the password as the OTP |
|
28 |
if ( empty($userdata['yubikey_otp']) && preg_match('/^[cbdefghijklnrtuv]{44}$/', $userdata['password'] ) ) |
|
29 |
{ |
|
30 |
$userdata['yubikey_otp'] = $userdata['password']; |
|
31 |
} |
|
32 |
||
33 |
// Lockouts removed from here - they're done during preprocessing now. |
|
34 |
||
35 |
if ( !empty($userdata['username']) ) |
|
36 |
{ |
|
37 |
// get flags |
|
38 |
$q = $db->sql_query('SELECT user_id, user_yubikey_flags FROM ' . table_prefix . "users WHERE " . ENANO_SQLFUNC_LOWERCASE . "(username) = '" . $db->escape(strtolower($userdata['username'])) . "';"); |
|
39 |
if ( !$q ) |
|
40 |
$db->die_json(); |
|
41 |
||
42 |
if ( $db->numrows() < 1 ) |
|
43 |
{ |
|
44 |
// Username not found - let the main login function handle it |
|
45 |
$db->free_result(); |
|
46 |
return null; |
|
47 |
} |
|
48 |
list($user_id, $flags) = $db->fetchrow_num(); |
|
49 |
$flags = intval($flags); |
|
50 |
// At the point the username is validated. |
|
51 |
$do_validate_user = false; |
|
52 |
$do_validate_pass = $flags & $pass_flag; |
|
53 |
if ( empty($userdata['yubikey_otp']) ) |
|
54 |
{ |
|
55 |
// no OTP was provided |
|
56 |
// make sure the user has allowed logging in with no OTP |
|
57 |
if ( !($flags & YK_SEC_ALLOW_NO_OTP) ) |
|
58 |
{ |
|
59 |
// We also might have no Yubikeys enrolled. |
|
60 |
$q = $db->sql_query('SELECT 1 FROM ' . table_prefix . "yubikey WHERE user_id = $user_id;"); |
|
61 |
if ( !$q ) |
|
62 |
$db->die_json(); |
|
63 |
||
64 |
if ( $db->numrows() > 0 ) |
|
65 |
{ |
|
66 |
// Yep at least one key is enrolled. |
|
67 |
// I don't think these should be logged because they'll usually just be innocent mistakes. |
|
68 |
$db->free_result(); |
|
69 |
return array( |
|
70 |
'mode' => 'error', |
|
71 |
'error' => 'yubiauth_err_must_have_otp' |
|
72 |
); |
|
73 |
} |
|
74 |
// Nope, no keys enrolled, user hasn't enabled Yubikey support |
|
75 |
$db->free_result(); |
|
76 |
} |
|
77 |
// we're ok, use normal password auth |
|
78 |
return null; |
|
79 |
} |
|
80 |
else |
|
81 |
{ |
|
82 |
// user did enter an OTP; make sure it's associated with the username |
|
83 |
$yubi_uid = $db->escape(substr($userdata['yubikey_otp'], 0, 12)); |
|
84 |
$q = $db->sql_query('SELECT 1 FROM ' . table_prefix . 'yubikey WHERE yubi_uid = \'' . $yubi_uid . '\';'); |
|
85 |
if ( !$q ) |
|
86 |
$db->die_json(); |
|
87 |
if ( $db->numrows() < 1 ) |
|
88 |
{ |
|
89 |
$db->free_result(); |
|
90 |
return array( |
|
91 |
'mode' => 'error', |
|
92 |
'error' => 'yubiauth_err_key_not_authorized' |
|
93 |
); |
|
94 |
} |
|
95 |
$db->free_result(); |
|
96 |
$do_validate_otp = true; |
|
97 |
} |
|
98 |
} |
|
99 |
else if ( !empty($userdata['yubikey_otp']) ) |
|
100 |
{ |
|
101 |
// we have an OTP, but no username to work with |
|
102 |
$yubi_uid = substr($userdata['yubikey_otp'], 0, 12); |
|
103 |
if ( !preg_match('/^[cbdefghijklnrtuv]{12}$/', $yubi_uid ) ) |
|
104 |
{ |
|
105 |
return array( |
|
106 |
'mode' => 'error', |
|
107 |
'error' => 'yubiauth_err_invalid_otp' |
|
108 |
); |
|
109 |
} |
|
110 |
$q = $db->sql_query('SELECT u.user_id, u.username, u.user_yubikey_flags FROM ' . table_prefix . "users AS u\n" |
|
111 |
. " LEFT JOIN " . table_prefix . "yubikey AS y\n" |
|
112 |
. " ON ( y.user_id = u.user_id )\n" |
|
113 |
. " WHERE y.yubi_uid = '$yubi_uid'\n" |
|
114 |
. " GROUP BY u.user_yubikey_flags;"); |
|
115 |
if ( !$q ) |
|
116 |
$db->_die(); |
|
117 |
||
118 |
if ( $db->numrows() < 1 ) |
|
119 |
{ |
|
120 |
if ( !$do_validate_pass ) |
|
121 |
$session->sql('INSERT INTO ' . table_prefix . "logs(log_type,action,time_id,date_string,author,edit_summary,page_text) VALUES\n" |
|
122 |
. ' (\'security\', \'' . $auth_log_prefix . 'auth_bad\', '.time().', \'DEPRECATED\', \'(Yubikey)\', ' |
|
123 |
. '\''.$db->escape($_SERVER['REMOTE_ADDR']).'\', ' . intval($level) . ')'); |
|
124 |
||
125 |
return array( |
|
126 |
'mode' => 'error', |
|
127 |
'error' => 'yubiauth_err_key_not_authorized' |
|
128 |
); |
|
129 |
} |
|
130 |
||
131 |
list($user_id, $username, $flags) = $db->fetchrow_num(); |
|
132 |
$do_validate_otp = true; |
|
133 |
$do_validate_user = $flags & $user_flag; |
|
134 |
$do_validate_pass = $flags & $pass_flag; |
|
135 |
// to complete security logs later |
|
136 |
$userdata['username'] = $username; |
|
137 |
} |
|
138 |
else |
|
139 |
{ |
|
140 |
// Nothing - no username or OTP. This request can't be used; throw it out. |
|
141 |
return array( |
|
142 |
'mode' => 'error', |
|
143 |
'error' => 'yubiauth_err_nothing_provided' |
|
144 |
); |
|
145 |
} |
|
146 |
if ( $do_validate_otp ) |
|
147 |
{ |
|
148 |
// We need to validate the OTP. |
|
149 |
$otp_check = yubikey_validate_otp($userdata['yubikey_otp']); |
|
150 |
if ( !$otp_check['success'] ) |
|
151 |
{ |
|
152 |
if ( !$do_validate_pass ) |
|
153 |
$session->sql('INSERT INTO ' . table_prefix . "logs(log_type,action,time_id,date_string,author,edit_summary,page_text) VALUES\n" |
|
154 |
. ' (\'security\', \'' . $auth_log_prefix . 'auth_bad\', '.time().', \'DEPRECATED\', \'(Yubikey)\', ' |
|
155 |
. '\''.$db->escape($_SERVER['REMOTE_ADDR']).'\', ' . intval($level) . ')'); |
|
156 |
||
157 |
if ( $otp_check['error'] === 'http_failed' ) |
|
158 |
{ |
|
159 |
return array( |
|
160 |
'mode' => 'error', |
|
161 |
'error' => 'yubiauth_err_' . $otp_check['error'], |
|
162 |
'http_error' => $otp_check['http_error'] |
|
163 |
); |
|
164 |
} |
|
165 |
return array( |
|
166 |
'mode' => 'error', |
|
167 |
'error' => 'yubiauth_err_' . $otp_check['error'] |
|
168 |
); |
|
169 |
} |
|
170 |
} |
|
171 |
if ( $do_validate_user ) |
|
172 |
{ |
|
173 |
if ( empty($username) ) |
|
174 |
{ |
|
175 |
return array( |
|
176 |
'mode' => 'error', |
|
177 |
'error' => 'yubiauth_err_must_have_username' |
|
178 |
); |
|
179 |
} |
|
180 |
if ( strtolower($username) !== strtolower($userdata['username']) ) |
|
181 |
{ |
|
182 |
// Username incorrect |
|
183 |
if ( !$do_validate_pass ) |
|
184 |
$session->sql('INSERT INTO ' . table_prefix . "logs(log_type,action,time_id,date_string,author,edit_summary,page_text) VALUES\n" |
|
185 |
. ' (\'security\', \'' . $auth_log_prefix . 'auth_bad\', '.time().', \'DEPRECATED\', \'(Yubikey)\', ' |
|
186 |
. '\''.$db->escape($_SERVER['REMOTE_ADDR']).'\', ' . intval($level) . ')'); |
|
187 |
return array( |
|
188 |
'mode' => 'error', |
|
189 |
'error' => 'invalid_credentials' |
|
190 |
); |
|
191 |
} |
|
192 |
} |
|
193 |
// Do we need to have the password validated? |
|
194 |
if ( $do_validate_pass ) |
|
195 |
{ |
|
196 |
if ( empty($userdata['password']) ) |
|
197 |
{ |
|
198 |
return array( |
|
199 |
'mode' => 'error', |
|
200 |
'error' => 'yubiauth_err_must_have_password' |
|
201 |
); |
|
202 |
} |
|
203 |
// Yes; return and let the login API continue |
|
204 |
return null; |
|
205 |
} |
|
206 |
else |
|
207 |
{ |
|
208 |
// No password required; validated, issue session key |
|
209 |
$session->sql('INSERT INTO ' . table_prefix . "logs(log_type,action,time_id,date_string,author,edit_summary,page_text) VALUES\n" |
|
210 |
. ' (\'security\', \'' . $auth_log_prefix . 'auth_good\', '.time().', \'DEPRECATED\', \'' . $db->escape($userdata['username']) . '\', ' |
|
211 |
. '\''.$db->escape($_SERVER['REMOTE_ADDR']).'\', ' . intval($level) . ')'); |
|
212 |
||
213 |
$q = $db->sql_query('SELECT password FROM ' . table_prefix . "users WHERE user_id = $user_id;"); |
|
214 |
if ( !$q ) |
|
215 |
$db->_die(); |
|
216 |
||
217 |
list($password) = $db->fetchrow_num(); |
|
218 |
$db->free_result(); |
|
219 |
||
220 |
$session->register_session($user_id, $userdata['username'], $password, intval($level), $remember); |
|
221 |
return true; |
|
222 |
} |
|
0 | 223 |
} |
224 |
||
225 |
function yubikey_add_special_pages() |
|
226 |
{ |
|
37 | 227 |
global $db, $session, $paths, $template, $plugins; // Common objects |
228 |
global $lang; |
|
229 |
||
230 |
if ( getConfig('yubikey_enable', '1') != '1' ) |
|
231 |
return true; |
|
232 |
||
233 |
$paths->add_page(array( |
|
234 |
'name' => $lang->get('yubiauth_specialpage_yubikey'), |
|
235 |
'urlname' => 'Yubikey', |
|
236 |
'namespace' => 'Special', |
|
237 |
'visible' => 0, 'protected' => 0, 'comments_on' => 0, 'special' => 0 |
|
238 |
)); |
|
0 | 239 |
} |
240 |
||
32 | 241 |
function yubikey_sk_calc($user_id, &$key_pieces, &$sk_mode) |
242 |
{ |
|
37 | 243 |
global $db, $session, $paths, $template, $plugins; // Common objects |
244 |
// hash the user's yubikeys |
|
245 |
$q = $db->sql_query('SELECT yubi_uid FROM ' . table_prefix . "yubikey WHERE user_id = $user_id;"); |
|
246 |
if ( !$q ) |
|
247 |
$db->_die(); |
|
248 |
||
249 |
while ( $row = $db->fetchrow() ) |
|
250 |
{ |
|
251 |
$key_pieces[] = $row['yubi_uid']; |
|
252 |
} |
|
32 | 253 |
} |
254 |
||
0 | 255 |
function page_Special_Yubikey() |
256 |
{ |
|
37 | 257 |
global $db, $session, $paths, $template, $plugins; // Common objects |
258 |
||
259 |
header('Content-type: text/javascript'); |
|
260 |
/* |
|
261 |
if ( isset($_GET['validate_otp']) ) |
|
262 |
{ |
|
263 |
echo enano_json_encode(yubikey_validate_otp($_GET['validate_otp'])); |
|
264 |
return true; |
|
265 |
} |
|
266 |
*/ |
|
267 |
if ( isset($_GET['get_flags']) || isset($_POST['get_flags']) ) |
|
268 |
{ |
|
269 |
$yubi_uid = substr($_REQUEST['get_flags'], 0, 12); |
|
270 |
if ( !preg_match('/^[cbdefghijklnrtuv]{12}$/', $yubi_uid) ) |
|
271 |
{ |
|
272 |
return print enano_json_encode(array( |
|
273 |
'mode' => 'error', |
|
274 |
'error' => 'invalid_otp' |
|
275 |
)); |
|
276 |
} |
|
277 |
$q = $db->sql_query('SELECT u.user_yubikey_flags FROM ' . table_prefix . "users AS u\n" |
|
278 |
. " LEFT JOIN " . table_prefix . "yubikey AS y\n" |
|
279 |
. " ON ( y.user_id = u.user_id )\n" |
|
280 |
. " WHERE y.yubi_uid = '$yubi_uid'\n" |
|
281 |
. " GROUP BY u.user_yubikey_flags;"); |
|
282 |
if ( !$q ) |
|
283 |
$db->_die(); |
|
284 |
||
285 |
if ( $db->numrows() < 1 ) |
|
286 |
{ |
|
287 |
return print enano_json_encode(array( |
|
288 |
'mode' => 'error', |
|
289 |
'error' => 'key_not_authorized' |
|
290 |
)); |
|
291 |
} |
|
292 |
||
293 |
list($flags) = $db->fetchrow_num(); |
|
294 |
||
295 |
echo enano_json_encode(array( |
|
296 |
// We strip YK_SEC_ALLOW_NO_OTP here for security reasons. |
|
297 |
'flags' => intval($flags & ~YK_SEC_ALLOW_NO_OTP) |
|
298 |
)); |
|
299 |
||
300 |
return true; |
|
301 |
} |
|
0 | 302 |
} |
303 |