mirror of
https://github.com/Yubico/yubikey-val.git
synced 2024-11-29 09:24:12 +01:00
changed structure of info in db
This commit is contained in:
parent
1a620a1d1b
commit
362b40056d
@ -55,9 +55,7 @@ class SyncLib
|
||||
}
|
||||
public function queue($modified, $otp, $identity, $counter, $use, $high, $low)
|
||||
{
|
||||
$info='modified=' . $modified .
|
||||
'&otp=' . $otp .
|
||||
'&yk_identity=' . $identity .
|
||||
$info='yk_identity=' . $identity .
|
||||
'&yk_counter=' . $counter .
|
||||
'&yk_use=' . $use .
|
||||
'&yk_high=' . $high .
|
||||
@ -104,6 +102,7 @@ class SyncLib
|
||||
$this->log("notice", "searching for " . $yk_identity . " (" . modhex2b64($yk_identity) . ") in local db");
|
||||
$res = $this->db->lastBy('yubikeys', 'publicName', modhex2b64($yk_identity));
|
||||
$localParams=array('modified'=>$this->DbTimeToUnix($res['accessed']),
|
||||
'otp'=>$res['otp'],
|
||||
'yk_identity'=>$yk_identity,
|
||||
'yk_counter'=>$res['counter'],
|
||||
'yk_use'=>$res['sessionUse'],
|
||||
@ -140,11 +139,13 @@ class SyncLib
|
||||
|
||||
$res=$this->db->lastBy('yubikeys', 'publicName', modhex2b64($params['yk_identity']));
|
||||
if (isset($res['id'])) {
|
||||
if(! $this->db->update('yubikeys', $res['id'], array('accessed'=>$this->UnixToDbTime($params['modified']),
|
||||
'counter'=>$params['yk_counter'],
|
||||
'sessionUse'=>$params['yk_use'],
|
||||
'low'=>$params['yk_low'],
|
||||
'high'=>$params['yk_high'])))
|
||||
if(! $this->db->update('yubikeys',
|
||||
$res['id'],
|
||||
array('accessed'=>$this->UnixToDbTime($params['modified']),
|
||||
'counter'=>$params['yk_counter'],
|
||||
'sessionUse'=>$params['yk_use'],
|
||||
'low'=>$params['yk_low'],
|
||||
'high'=>$params['yk_high'])))
|
||||
{
|
||||
error_log("ykval-synclib:critical: failed to update internal DB with new counters");
|
||||
return false;
|
||||
@ -173,19 +174,22 @@ class SyncLib
|
||||
|
||||
public function sync($ans_req)
|
||||
{
|
||||
#
|
||||
# Construct URLs
|
||||
#
|
||||
|
||||
/*
|
||||
Construct URLs
|
||||
*/
|
||||
|
||||
$urls=array();
|
||||
$res=$this->db->findByMultiple('queue', array("modified_time"=>$this->UnixToDbTime($this->otpParams['modified']), "random_key"=>$this->random_key));
|
||||
foreach ($res as $row) {
|
||||
$urls[]=$row['server'] . '?' . $row['info'];
|
||||
$urls[]=$row['server'] .
|
||||
"?otp=" . $row['otp'] .
|
||||
"&modified=" . $this->DbTimeToUnix($row['modified_time']) .
|
||||
"&" . $row['info'];
|
||||
}
|
||||
|
||||
#
|
||||
# Send out requests
|
||||
#
|
||||
/*
|
||||
Send out requests
|
||||
*/
|
||||
if (count($urls)>=$ans_req) $ans_arr=$this->retrieveURLasync($urls, $ans_req);
|
||||
else return false;
|
||||
|
||||
@ -194,53 +198,55 @@ class SyncLib
|
||||
$ans_arr=array();
|
||||
}
|
||||
|
||||
#
|
||||
# Parse responses
|
||||
#
|
||||
/*
|
||||
Parse responses
|
||||
*/
|
||||
$localParams=$this->getLocalParams($this->otpParams['yk_identity']);
|
||||
|
||||
$this->answers = count($ans_arr);
|
||||
$this->valid_answers = 0;
|
||||
foreach ($ans_arr as $answer){
|
||||
// Parse out parameters from each response
|
||||
/* Parse out parameters from each response */
|
||||
$resParams=$this->parseParamsFromMultiLineString($answer);
|
||||
$this->log("notice", "local db contains ", $localParams);
|
||||
$this->log("notice", "response contains ", $resParams);
|
||||
|
||||
# Check if internal DB should be updated
|
||||
/* Check if internal DB should be updated */
|
||||
if ($this->countersHigherThan($resParams, $localParams)) {
|
||||
$this->updateDbCounters($resParams);
|
||||
}
|
||||
|
||||
# Check for warnings
|
||||
#
|
||||
# If received sync response have lower counters than locally saved last counters
|
||||
# (indicating that remote server wasn't synced)
|
||||
|
||||
/* Check for warnings
|
||||
|
||||
If received sync response have lower counters than locally saved
|
||||
last counters (indicating that remote server wasn't synced)
|
||||
*/
|
||||
if ($this->countersHigherThan($localParams, $resParams)) {
|
||||
$this->log("warning", "Remote server out of sync, local counters ", $localParams);
|
||||
$this->log("warning", "Remote server out of sync, remote counters ", $resParams);
|
||||
}
|
||||
|
||||
# If received sync response have higher counters than locally saved last counters
|
||||
# (indicating that local server wasn't synced)
|
||||
/* If received sync response have higher counters than locally saved
|
||||
last counters (indicating that local server wasn't synced)
|
||||
*/
|
||||
if ($this->countersHigherThan($resParams, $localParams)) {
|
||||
$this->log("warning", "Local server out of sync, local counters ", $localParams);
|
||||
$this->log("warning", "Local server out of sync, remote counters ", $resParams);
|
||||
}
|
||||
|
||||
# If received sync response have higher counters than OTP counters
|
||||
# (indicating REPLAYED_OTP)
|
||||
/* If received sync response have higher counters than OTP counters
|
||||
(indicating REPLAYED_OTP)
|
||||
*/
|
||||
if ($this->countersHigherThanOrEqual($resParams, $this->otpParams)) {
|
||||
$this->log("warning", "replayed OTP, remote counters " , $resParams);
|
||||
$this->log("warning", "replayed OTP, otp counters", $this->otpParams);
|
||||
}
|
||||
|
||||
|
||||
# Check if answer marks OTP as valid
|
||||
/* Check if answer marks OTP as valid */
|
||||
if (!$this->countersHigherThanOrEqual($resParams, $this->otpParams)) $this->valid_answers++;
|
||||
|
||||
# Delete entry from table
|
||||
/* Delete entry from table */
|
||||
preg_match('/url=(.*)\?/', $answer, $out);
|
||||
$server=$out[1];
|
||||
debug("server=" . $server);
|
||||
@ -248,8 +254,9 @@ class SyncLib
|
||||
|
||||
}
|
||||
|
||||
/* Return true if valid answers equals required answers. Since we only obtain the required
|
||||
amount of answers from retrieveAsync this indicates that all answers were actually valid.
|
||||
/* Return true if valid answers equals required answers.
|
||||
Since we only obtain the required amount of answers from
|
||||
retrieveAsync this indicates that all answers were actually valid.
|
||||
Otherwise, return false. */
|
||||
if ($this->valid_answers==$ans_req) return True;
|
||||
else return False;
|
||||
@ -266,83 +273,84 @@ class SyncLib
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
This function takes a list of URLs. It will return the content of
|
||||
the first successfully retrieved URL, whose content matches ^OK.
|
||||
The request are sent asynchronously. Some of the URLs can fail
|
||||
with unknown host, connection errors, or network timeout, but as
|
||||
long as one of the URLs given work, data will be returned. If all
|
||||
URLs fail, data from some URL that did not match parameter $match
|
||||
(defaults to ^OK) is returned, or if all URLs failed, false.
|
||||
*/
|
||||
function retrieveURLasync ($urls, $ans_req=1) {
|
||||
$mh = curl_multi_init();
|
||||
|
||||
// This function takes a list of URLs. It will return the content of
|
||||
// the first successfully retrieved URL, whose content matches ^OK.
|
||||
// The request are sent asynchronously. Some of the URLs can fail
|
||||
// with unknown host, connection errors, or network timeout, but as
|
||||
// long as one of the URLs given work, data will be returned. If all
|
||||
// URLs fail, data from some URL that did not match parameter $match
|
||||
// (defaults to ^OK) is returned, or if all URLs failed, false.
|
||||
function retrieveURLasync ($urls, $ans_req=1) {
|
||||
$mh = curl_multi_init();
|
||||
|
||||
$ch = array();
|
||||
foreach ($urls as $id => $url) {
|
||||
$handle = curl_init();
|
||||
|
||||
curl_setopt($handle, CURLOPT_URL, $url);
|
||||
curl_setopt($handle, CURLOPT_USERAGENT, "YK-VAL");
|
||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
||||
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
curl_multi_add_handle($mh, $handle);
|
||||
|
||||
$ch[$handle] = $handle;
|
||||
}
|
||||
|
||||
$str = false;
|
||||
$ans_count = 0;
|
||||
$ans_arr = array();
|
||||
|
||||
do {
|
||||
while (($mrc = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM)
|
||||
;
|
||||
|
||||
while ($info = curl_multi_info_read($mh)) {
|
||||
debug ("YK-KSM multi", $info);
|
||||
if ($info['result'] == CURL_OK) {
|
||||
$str = curl_multi_getcontent($info['handle']);
|
||||
debug($str);
|
||||
if (preg_match("/status=OK/", $str)) {
|
||||
$error = curl_error ($info['handle']);
|
||||
$errno = curl_errno ($info['handle']);
|
||||
$cinfo = curl_getinfo ($info['handle']);
|
||||
debug("YK-KSM errno/error: " . $errno . "/" . $error, $cinfo);
|
||||
$ans_count++;
|
||||
debug("found entry");
|
||||
$ans_arr[]="url=" . $cinfo['url'] . "\n" . $str;
|
||||
}
|
||||
|
||||
if ($ans_count >= $ans_req) {
|
||||
foreach ($ch as $h) {
|
||||
curl_multi_remove_handle ($mh, $h);
|
||||
curl_close ($h);
|
||||
$ch = array();
|
||||
foreach ($urls as $id => $url) {
|
||||
$handle = curl_init();
|
||||
|
||||
curl_setopt($handle, CURLOPT_URL, $url);
|
||||
curl_setopt($handle, CURLOPT_USERAGENT, "YK-VAL");
|
||||
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($handle, CURLOPT_FAILONERROR, true);
|
||||
curl_setopt($handle, CURLOPT_TIMEOUT, 10);
|
||||
|
||||
curl_multi_add_handle($mh, $handle);
|
||||
|
||||
$ch[$handle] = $handle;
|
||||
}
|
||||
|
||||
$str = false;
|
||||
$ans_count = 0;
|
||||
$ans_arr = array();
|
||||
|
||||
do {
|
||||
while (($mrc = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM)
|
||||
;
|
||||
|
||||
while ($info = curl_multi_info_read($mh)) {
|
||||
debug ("YK-KSM multi", $info);
|
||||
if ($info['result'] == CURL_OK) {
|
||||
$str = curl_multi_getcontent($info['handle']);
|
||||
debug($str);
|
||||
if (preg_match("/status=OK/", $str)) {
|
||||
$error = curl_error ($info['handle']);
|
||||
$errno = curl_errno ($info['handle']);
|
||||
$cinfo = curl_getinfo ($info['handle']);
|
||||
debug("YK-KSM errno/error: " . $errno . "/" . $error, $cinfo);
|
||||
$ans_count++;
|
||||
debug("found entry");
|
||||
$ans_arr[]="url=" . $cinfo['url'] . "\n" . $str;
|
||||
}
|
||||
curl_multi_close ($mh);
|
||||
|
||||
return $ans_arr;
|
||||
if ($ans_count >= $ans_req) {
|
||||
foreach ($ch as $h) {
|
||||
curl_multi_remove_handle ($mh, $h);
|
||||
curl_close ($h);
|
||||
}
|
||||
curl_multi_close ($mh);
|
||||
|
||||
return $ans_arr;
|
||||
}
|
||||
|
||||
curl_multi_remove_handle ($mh, $info['handle']);
|
||||
curl_close ($info['handle']);
|
||||
unset ($ch[$info['handle']]);
|
||||
}
|
||||
|
||||
curl_multi_remove_handle ($mh, $info['handle']);
|
||||
curl_close ($info['handle']);
|
||||
unset ($ch[$info['handle']]);
|
||||
curl_multi_select ($mh);
|
||||
}
|
||||
|
||||
curl_multi_select ($mh);
|
||||
} while($active);
|
||||
|
||||
foreach ($ch as $h) {
|
||||
curl_multi_remove_handle ($mh, $h);
|
||||
curl_close ($h);
|
||||
}
|
||||
} while($active);
|
||||
|
||||
foreach ($ch as $h) {
|
||||
curl_multi_remove_handle ($mh, $h);
|
||||
curl_close ($h);
|
||||
curl_multi_close ($mh);
|
||||
|
||||
return $str;
|
||||
}
|
||||
curl_multi_close ($mh);
|
||||
|
||||
return $str;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue
Block a user