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