From dd9f567e22db50ebd94afd8d12f637484416a058 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 09:22:46 +0200 Subject: [PATCH 01/19] start work on integrating oracle patches from Remi Mollon --- ykval-db-oci.php | 227 +++++++++++++++++++++++++++++++++++++++++++++++ ykval-db-pdo.php | 225 ++++++++++++++++++++++++++++++++++++++++++++++ ykval-db.php | 162 --------------------------------- 3 files changed, 452 insertions(+), 162 deletions(-) create mode 100644 ykval-db-oci.php create mode 100644 ykval-db-pdo.php diff --git a/ykval-db-oci.php b/ykval-db-oci.php new file mode 100644 index 0000000..b261725 --- /dev/null +++ b/ykval-db-oci.php @@ -0,0 +1,227 @@ +db_dsn=$db_dsn; + $this->db_username=$db_username; + $this->db_password=$db_password; + $this->db_options=$db_options; + + if(substr($db_dsn, 0, 3) == 'oci') { + # "oci:" prefix needs to be removed before passing db_dsn to OCI + $this->db_dsn = substr($this->db_dsn, 4); + } + + $this->myLog=new Log($name); + } + + /** + * function to connect to database defined in config.php + * + * @return boolean True on success, otherwise false. + * + */ + public function connect(){ + $this->dbh = oci_connect($this->db_username, $this->db_password, $this->db_dsn); + if (!$this->dbh) { + $error = oci_error(); + $this->myLog->log(LOG_CRIT, "Database connection error: " . $error["message"]); + $this->dbh=Null; + return false; + } + return true; + } + + private function query($query, $returnresult=false) { + if(!$this->isConnected()) { + $this->connect(); + } + if($this->isConnected()) { + $this->myLog->log(LOG_DEBUG, 'DB query is: ' . $query); + # OCI mode + $result = oci_parse($this->dbh, $query); + if(!oci_execute($result)) { + $this->myLog->log(LOG_INFO, 'Database query error: ' . preg_replace('/\n/',' ',print_r(oci_error($result), true))); + $this->dbh = Null; + return false; + } + $this->result = $result; + if ($returnresult) return $this->result; + else return true; + } else { + $this->myLog->log(LOG_CRIT, 'No database connection'); + return false; + } + } + + /** + * function to get a row from the query result + * Once all rows have been fetch, function closeCursor needs to be called + * + * @param object $result Query result object or null to use the current one + * @return array a query row + * + */ + public function fetchArray($result=null){ + if(!$result) $result = $this->result; + if(!$result) return null; + + return oci_fetch_array($result, OCI_ASSOC); + } + + /** + * function to close the cursor after having fetched rows + * + * @param object $result Query result object or null to use the current one + * + */ + public function closeCursor($result=null){ + } + + /** + * main function used to get rows by multiple key=>value pairs from Db table. + * + * @param string $table Database table to update row in + * @param array $where Array with column=>values to select rows by + * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. + * @param int $rev rev=1 indicates order should be reversed. Default=NULL. + * @param string distinct Select rows with distinct columns, Default=NULL + * @return mixed Array with values from Db row or 2d-array with multiple rows + * + */ + public function findByMultiple($table, $where, $nr=null, $rev=null, $distinct=null) + { + $value=""; /* quiet the PHP Notice */ + $match=null; /* quiet the PHP Notice */ + # LIMIT doesn't exist in Oracle, so we encapsulate the query to be + # able to filter a given number of rows afterwars (after ordering) + $query="SELECT * FROM (SELECT"; + + if ($distinct!=null) { + $query.= " DISTINCT " . $distinct; + } else { + $query.= " *"; + } + $query.= " FROM " . $table; + if ($where!=null){ + foreach ($where as $key=>$value) { + if ($key!=null) { + if ($value!=null) $match.= " ". $key . " = '" . $value . "' and"; + else $match.= " ". $key . " is NULL and"; + } + } + if ($match!=null) $query .= " WHERE" . $match; + $query=rtrim($query, "and"); + $query=rtrim($query); + } + if ($rev==1) $query.= " ORDER BY id DESC"; + if ($nr!=null) { + $query .= ") WHERE rownum < " . ($nr+1); + } + + $result = $this->query($query, true); + if (!$result) return false; + + if ($nr==1) { + $row = $this->fetchArray($result); + $this->closeCursor($result); + return $row; + } + else { + $collection=array(); + while($row = $this->fetchArray($result)){ + $collection[]=$row; + } + $this->closeCursor($result); + return $collection; + } + } + + /** + * main function used to delete rows by multiple key=>value pairs from Db table. + * + * @param string $table Database table to delete row in + * @param array $where Array with column=>values to select rows by + * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. + * @param int $rev rev=1 indicates order should be reversed. Default=NULL. + * @param string distinct Select rows with distinct columns, Default=NULL + * @return boolean True on success, otherwise false. + * + */ + public function deleteByMultiple($table, $where, $nr=null, $rev=null) + { + $query="DELETE"; + $query.= " FROM " . $table; + $query .= " WHERE id IN (SELECT id FROM " . $table; + if ($where!=null){ + $query.= " WHERE"; + foreach ($where as $key=>$value) { + $query.= " ". $key . " = '" . $value . "' and"; + } + $query=rtrim($query, "and"); + $query=rtrim($query); + } + if ($rev==1) $query.= " ORDER BY id DESC"; + + $query .= ")"; + if ($nr!=null) $query.= " and rownum < " . ($nr+1); + + return $this->query($query, false); + } + + /** + * Function to get the number of rows + * + * @param object $result Query result object or null to use the current one + * @return int number of rows affected by last statement or 0 if database connection is not functional. + * + */ + public function rowCount($result=null) + { + if(!$result) $result = $this->result; + if($result) { + return oci_num_rows($result); + } else { + return 0; + } + } + + /** + * Function to return the value corresponding to a given attribute name + * PDO requires lower case strings, whereas OCI requires upper case strings + * + * @param array $row Query result's row + * @param string $key Attribute name + * @return string Value of the attribute in this row + * + */ + public function getRowValue($row, $key) + { + $attr = strtoupper($key); + return $row[$attr]; + } + +} + + +?> diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php new file mode 100644 index 0000000..c92da46 --- /dev/null +++ b/ykval-db-pdo.php @@ -0,0 +1,225 @@ +db_dsn=$db_dsn; + $this->db_username=$db_username; + $this->db_password=$db_password; + $this->db_options=$db_options; + + $this->myLog=new Log($name); + } + + /** + * function to connect to database defined in config.php + * + * @return boolean True on success, otherwise false. + * + */ + public function connect(){ + + try { + $this->dbh = new PDO($this->db_dsn, $this->db_username, $this->db_password, $this->db_options); + } catch (PDOException $e) { + $this->myLog->log(LOG_CRIT, "Database connection error: " . $e->getMessage()); + $this->dbh=Null; + return false; + } + return true; + } + + private function query($query, $returnresult=false) { + if(!$this->isConnected()) { + $this->connect(); + } + if($this->isConnected()) { + $this->myLog->log(LOG_DEBUG, 'DB query is: ' . $query); + + try { + $this->result = $this->dbh->query($query); + } catch (PDOException $e) { + $this->myLog->log(LOG_INFO, 'Database query error: ' . preg_replace('/\n/',' ',print_r($this->dbh->errorInfo(), true))); + $this->dbh = Null; + return false; + } + if ($returnresult) return $this->result; + else return true; + } else { + $this->myLog->log(LOG_CRIT, 'No database connection'); + return false; + } + } + + + /** + * function to get a row from the query result + * Once all rows have been fetch, function closeCursor needs to be called + * + * @param object $result Query result object or null to use the current one + * @return array a query row + * + */ + public function fetchArray($result=null){ + if(!$result) $result = $this->result; + if(!$result) return null; + + return $result->fetch(PDO::FETCH_ASSOC); + } + + /** + * function to close the cursor after having fetched rows + * + * @param object $result Query result object or null to use the current one + * + */ + public function closeCursor($result=null){ + if(!$result) $result = $this->result; + $result->closeCursor(); + } + + public function truncateTable($name) + { + $this->query("TRUNCATE TABLE " . $name); + } + + /** + * main function used to get rows by multiple key=>value pairs from Db table. + * + * @param string $table Database table to update row in + * @param array $where Array with column=>values to select rows by + * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. + * @param int $rev rev=1 indicates order should be reversed. Default=NULL. + * @param string distinct Select rows with distinct columns, Default=NULL + * @return mixed Array with values from Db row or 2d-array with multiple rows + * + */ + public function findByMultiple($table, $where, $nr=null, $rev=null, $distinct=null) + { + $value=""; /* quiet the PHP Notice */ + $match=null; /* quiet the PHP Notice */ + $query="SELECT"; + if ($distinct!=null) { + $query.= " DISTINCT " . $distinct; + } else { + $query.= " *"; + } + $query.= " FROM " . $table; + if ($where!=null){ + foreach ($where as $key=>$value) { + if ($key!=null) { + if ($value!=null) $match.= " ". $key . " = '" . $value . "' and"; + else $match.= " ". $key . " is NULL and"; + } + } + if ($match!=null) $query .= " WHERE" . $match; + $query=rtrim($query, "and"); + $query=rtrim($query); + } + if ($rev==1) $query.= " ORDER BY id DESC"; + if ($nr!=null) $query.= " LIMIT " . $nr; + + $result = $this->query($query, true); + if (!$result) return false; + + if ($nr==1) { + $row = $this->fetchArray($result); + $this->closeCursor($result); + return $row; + } + else { + $collection=array(); + while($row = $this->fetchArray($result)){ + $collection[]=$row; + } + $this->closeCursor($result); + return $collection; + } + + } + + /** + * main function used to delete rows by multiple key=>value pairs from Db table. + * + * @param string $table Database table to delete row in + * @param array $where Array with column=>values to select rows by + * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. + * @param int $rev rev=1 indicates order should be reversed. Default=NULL. + * @param string distinct Select rows with distinct columns, Default=NULL + * @return boolean True on success, otherwise false. + * + */ + public function deleteByMultiple($table, $where, $nr=null, $rev=null) + { + $query="DELETE"; + $query.= " FROM " . $table; + if ($where!=null){ + $query.= " WHERE"; + foreach ($where as $key=>$value) { + $query.= " ". $key . " = '" . $value . "' and"; + } + $query=rtrim($query, "and"); + $query=rtrim($query); + } + if ($rev==1) $query.= " ORDER BY id DESC"; + if ($nr!=null) $query.= " LIMIT " . $nr; + return $this->query($query, false); + } + + /** + * Function to get the number of rows + * + * @param object $result Query result object or null to use the current one + * @return int number of rows affected by last statement or 0 if database connection is not functional. + * + */ + public function rowCount($result=null) + { + if(!$result) $result = $this->result; + if($result) { + $count = count($result->fetchAll()); + $result->closeCursor(); + return $count; + } else { + return 0; + } + } + + /** + * Function to return the value corresponding to a given attribute name + * PDO requires lower case strings, whereas OCI requires upper case strings + * + * @param array $row Query result's row + * @param string $key Attribute name + * @return string Value of the attribute in this row + * + */ + public function getRowValue($row, $key) + { + return $row[$key]; + } +} + + +?> diff --git a/ykval-db.php b/ykval-db.php index 7d9d7e7..772dfef 100644 --- a/ykval-db.php +++ b/ykval-db.php @@ -9,28 +9,6 @@ require_once('ykval-log.php'); class Db { - - - /** - * Constructor - * - * @param string $host Database host - * @param string $user Database user - * @param string $pwd Database password - * @param string $name Database table name - * @return void - * - */ - public function __construct($db_dsn, $db_username, $db_password, $db_options, $name='ykval-db') - { - $this->db_dsn=$db_dsn; - $this->db_username=$db_username; - $this->db_password=$db_password; - $this->db_options=$db_options; - - $this->myLog=new Log($name); - } - function addField($name, $value) { $this->myLog->addField($name, $value); @@ -85,45 +63,6 @@ class Db if ($this->dbh!=NULL) return True; else return False; } - /** - * function to connect to database defined in config.php - * - * @return boolean True on success, otherwise false. - * - */ - public function connect(){ - - try { - $this->dbh = new PDO($this->db_dsn, $this->db_username, $this->db_password, $this->db_options); - } catch (PDOException $e) { - $this->myLog->log(LOG_CRIT, "Database connection error: " . $e->getMessage()); - $this->dbh=Null; - return false; - } - return true; - } - - private function query($query, $returnresult=false) { - if(!$this->isConnected()) { - $this->connect(); - } - if($this->isConnected()) { - $this->myLog->log(LOG_DEBUG, 'DB query is: ' . $query); - - try { - $this->result = $this->dbh->query($query); - } catch (PDOException $e) { - $this->myLog->log(LOG_INFO, 'Database query error: ' . preg_replace('/\n/',' ',print_r($this->dbh->errorInfo(), true))); - $this->dbh = Null; - return false; - } - if ($returnresult) return $this->result; - else return true; - } else { - $this->myLog->log(LOG_CRIT, 'No database connection'); - return false; - } - } public function truncateTable($name) { @@ -273,90 +212,6 @@ or false on failure. return $this->findByMultiple($table, array($key=>$value), $nr, $rev); } - /** - * main function used to get rows by multiple key=>value pairs from Db table. - * - * @param string $table Database table to update row in - * @param array $where Array with column=>values to select rows by - * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. - * @param int $rev rev=1 indicates order should be reversed. Default=NULL. - * @param string distinct Select rows with distinct columns, Default=NULL - * @return mixed Array with values from Db row or 2d-array with multiple rows - * - */ - public function findByMultiple($table, $where, $nr=null, $rev=null, $distinct=null) - { - $value=""; /* quiet the PHP Notice */ - $match=null; /* quiet the PHP Notice */ - $query="SELECT"; - if ($distinct!=null) { - $query.= " DISTINCT " . $distinct; - } else { - $query.= " *"; - } - $query.= " FROM " . $table; - if ($where!=null){ - foreach ($where as $key=>$value) { - if ($key!=null) { - if ($value!=null) $match.= " ". $key . " = '" . $value . "' and"; - else $match.= " ". $key . " is NULL and"; - } - } - if ($match!=null) $query .= " WHERE" . $match; - $query=rtrim($query, "and"); - $query=rtrim($query); - } - if ($rev==1) $query.= " ORDER BY id DESC"; - if ($nr!=null) $query.= " LIMIT " . $nr; - - $result = $this->query($query, true); - if (!$result) return false; - - if ($nr==1) { - $row = $result->fetch(PDO::FETCH_ASSOC); - $result->closeCursor(); - return $row; - } - else { - $collection=array(); - while($row = $result->fetch(PDO::FETCH_ASSOC)){ - $collection[]=$row; - } - $result->closeCursor(); - return $collection; - } - - } - - /** - * main function used to delete rows by multiple key=>value pairs from Db table. - * - * @param string $table Database table to delete row in - * @param array $where Array with column=>values to select rows by - * @param int $nr Number of rows to collect. NULL=>inifinity. Default=NULL. - * @param int $rev rev=1 indicates order should be reversed. Default=NULL. - * @param string distinct Select rows with distinct columns, Default=NULL - * @return boolean True on success, otherwise false. - * - */ - public function deleteByMultiple($table, $where, $nr=null, $rev=null) - { - $query="DELETE"; - $query.= " FROM " . $table; - if ($where!=null){ - $query.= " WHERE"; - foreach ($where as $key=>$value) { - $query.= " ". $key . " = '" . $value . "' and"; - } - $query=rtrim($query, "and"); - $query=rtrim($query); - } - if ($rev==1) $query.= " ORDER BY id DESC"; - if ($nr!=null) $query.= " LIMIT " . $nr; - return $this->query($query, false); - } - - /** * Function to do a custom query on database connection * @@ -369,23 +224,6 @@ or false on failure. return $this->query($query, true); } - /** - * Function to do a custom query on database connection - * - * @return int number of rows affected by last statement or 0 if database connection is not functional. - * - */ - public function rowCount() - { - if($this->result) { - $count=count($this->result->fetchAll()); - $this->result->closeCursor(); - return $count; - } else { - return 0; - } - } - /** * helper function used to get rows from Db table in reversed order. * defaults to obtaining 1 row. From cf49385bf3b3f290dbfadb1cb3edc5179c23f807 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 10:35:49 +0200 Subject: [PATCH 02/19] rest of oracle patches from Remi Mollon --- ykval-checksum-clients.php | 12 +++--- ykval-config.php | 2 +- ykval-export.php | 26 +++++++------ ykval-import.php | 2 +- ykval-synclib.php | 80 +++++++++++++++++++------------------- ykval-verify.php | 2 +- 6 files changed, 64 insertions(+), 60 deletions(-) diff --git a/ykval-checksum-clients.php b/ykval-checksum-clients.php index 67f8085..7bc2b1d 100755 --- a/ykval-checksum-clients.php +++ b/ykval-checksum-clients.php @@ -41,16 +41,18 @@ $everything = ""; $result=$db->customQuery("SELECT id, active, secret ". "FROM clients ". "ORDER BY id"); -while($row = $result->fetch(PDO::FETCH_ASSOC)) { - if ($row['active'] == "") { +while($row = $this->db->fetchArray($result)) { + $active = $this->db->getRowValue($row, 'active'); + if ($active == "") { # For some reason PostgreSQL returns empty strings for false values?! - $row['active'] = "0"; + $active = "0"; } $everything = $everything . - $row['id'] . "\t" . $row['active'] . "\t" . $row['secret'] . - "\n"; + $this->db->getRowValue($row, 'id') . "\t" . $active . "\t" . + $this->db->getRowValue($row, 'secret') . "\n"; } +$this->db->closeCursor($result); $hash = sha1 ($everything); if ($verbose) { diff --git a/ykval-config.php b/ykval-config.php index 4a98ead..28c8f82 100644 --- a/ykval-config.php +++ b/ykval-config.php @@ -2,7 +2,7 @@ # For the validation interface. $baseParams = array (); -$baseParams['__YKVAL_DB_DSN__'] = "mysql:dbname=ykval;host=127.0.0.1"; +$baseParams['__YKVAL_DB_DSN__'] = "mysql:dbname=ykval;host=127.0.0.1"; # "oci:oracledb" for Oracle DB (with OCI library) $baseParams['__YKVAL_DB_USER__'] = 'ykval_verifier'; $baseParams['__YKVAL_DB_PW__'] = 'lab'; $baseParams['__YKVAL_DB_OPTIONS__'] = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); diff --git a/ykval-export.php b/ykval-export.php index 8d25a1d..1fe6c76 100755 --- a/ykval-export.php +++ b/ykval-export.php @@ -22,22 +22,24 @@ if (!$db->connect()) { } $result=$db->customQuery("SELECT active, created, modified, yk_publicname, yk_counter, yk_use, yk_low, yk_high, nonce, notes FROM yubikeys ORDER BY yk_publicname"); -while($row = $result->fetch(PDO::FETCH_ASSOC)){ - echo $row['active'] . - "\t" . $row['created'] . - "\t" . $row['modified'] . - "\t" . $row['yk_publicname'] . - "\t" . $row['yk_counter'] . - "\t" . $row['yk_use'] . - "\t" . $row['yk_low'] . - "\t" . $row['yk_high'] . - "\t" . $row['nonce'] . - "\t" . $row['notes'] . +while($row = $db->fetchArray($result)){ + echo $db->getRowValue($row, 'active') . + "\t" . $db->getRowValue($row, 'created') . + "\t" . $db->getRowValue($row, 'modified') . + "\t" . $db->getRowValue($row, 'yk_publicname') . + "\t" . $db->getRowValue($row, 'yk_counter') . + "\t" . $db->getRowValue($row, 'yk_use') . + "\t" . $db->getRowValue($row, 'yk_low') . + "\t" . $db->getRowValue($row, 'yk_high') . + "\t" . $db->getRowValue($row, 'nonce') . + "\t" . $db->getRowValue($row, 'notes') . "\n"; } +$db->closeCursor($result); +$db->disconnect(); $result=null; $db=null; -?> \ No newline at end of file +?> diff --git a/ykval-import.php b/ykval-import.php index 49d696f..636b741 100755 --- a/ykval-import.php +++ b/ykval-import.php @@ -38,7 +38,7 @@ while ($res=fgetcsv(STDIN, 0, "\t")) { $query="SELECT * FROM yubikeys WHERE yk_publicname='" . $params['yk_publicname'] . "'"; $result=$db->customQuery($query); - if($result->fetch(PDO::FETCH_ASSOC)) { + if($db->rowCount($result)) { $query="UPDATE yubikeys SET " . "active='" . $params["active"] . "' " . ",created='" . $params["created"] . "' " . diff --git a/ykval-synclib.php b/ykval-synclib.php index 7e8645d..d5ff097 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -56,9 +56,9 @@ class SyncLib function getClientData($client) { - $res=$this->db->customQuery("SELECT id, secret FROM clients WHERE active AND id='" . $client . "'"); - $r = $res->fetch(PDO::FETCH_ASSOC); - $res->closeCursor(); + $res = $this->db->customQuery("SELECT id, secret FROM clients WHERE active='1' AND id='" . $client . "'"); + $r = $this->db->fetchArray($res); + $this->db->closeCursor($res); if ($r) return $r; else return false; } @@ -143,7 +143,7 @@ class SyncLib function getLocalParams($yk_publicname) { $this->log(LOG_INFO, "searching for yk_publicname " . $yk_publicname . " in local db"); - $res = $this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname,1); + $res = $this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname, 1); if (!$res) { $this->log(LOG_NOTICE, 'Discovered new identity ' . $yk_publicname); @@ -160,14 +160,14 @@ class SyncLib $res=$this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname,1); } if ($res) { - $localParams=array('modified'=>$res['modified'], - 'nonce'=>$res['nonce'], - 'active'=>$res['active'], - 'yk_publicname'=>$yk_publicname, - 'yk_counter'=>$res['yk_counter'], - 'yk_use'=>$res['yk_use'], - 'yk_high'=>$res['yk_high'], - 'yk_low'=>$res['yk_low']); + $localParams=array('modified' => $this->db->getRowValue($res, 'modified'), + 'nonce' => $this->db->getRowValue($res, 'nonce'), + 'active' => $this->db->getRowValue($res, 'active'), + 'yk_publicname' => $yk_publicname, + 'yk_counter' => $this->db->getRowValue($res, 'yk_counter'), + 'yk_use' => $this->db->getRowValue($res, 'yk_use'), + 'yk_high' => $this->db->getRowValue($res, 'yk_high'), + 'yk_low' => $this->db->getRowValue($res, 'yk_low')); $this->log(LOG_INFO, "yubikey found in db ", $localParams); return $localParams; @@ -289,20 +289,19 @@ class SyncLib $this->log(LOG_INFO, 'starting resync'); /* Loop over all unique servers in queue */ $queued_limit=time()-$older_than; - $res=$this->db->customQuery("select distinct server from queue WHERE queued < " . $queued_limit . " or queued is null"); + $server_res=$this->db->customQuery("select distinct server from queue WHERE queued < " . $queued_limit . " or queued is null"); - foreach ($res as $my_server) { - $this->log(LOG_INFO, "Sending queue request to server on server " . $my_server['server']); - $res=$this->db->customQuery("select * from queue WHERE (queued < " . $queued_limit . " or queued is null) and server='" . $my_server['server'] . "'"); + + while ($my_server=$this->db->fetchArray($server_res)) { + $this->log(LOG_INFO, "Sending queue request to server on server " . $this->db->getRowValue($my_server, 'server')); + $res=$this->db->customQuery("select * from queue WHERE (queued < " . $queued_limit . " or queued is null) and server='" . $this->db->getRowValue($my_server, 'server') . "'"); $ch = curl_init(); - - while ($entry=$res->fetch(PDO::FETCH_ASSOC)) { - $this->log(LOG_INFO, "server=" . $entry['server'] . " , info=" . $entry['info']); - $url=$entry['server'] . - "?otp=" . $entry['otp'] . - "&modified=" . $entry['modified'] . - "&" . $this->otpPartFromInfoString($entry['info']); - + while ($entry=$this->db->fetchArray($res)) { + $this->log(LOG_INFO, "server=" . $this->db->getRowValue($entry, 'server') . " , info=" . $this->db->getRowValue($entry, 'info')); + $url=$this->db->getRowValue($entry, 'server') . + "?otp=" . $this->db->getRowValue($entry, 'otp') . + "&modified=" . $this->db->getRowValue($entry, 'modified') . + "&" . $this->otpPartFromInfoString($this->db->getRowValue($entry, 'info')); /* Send out sync request */ $this->log(LOG_DEBUG, 'url is ' . $url); @@ -315,7 +314,7 @@ class SyncLib $response = curl_exec($ch); if ($response==False) { - $this->log(LOG_NOTICE, 'Timeout. Stopping queue resync for server ' . $my_server['server']); + $this->log(LOG_NOTICE, 'Timeout. Stopping queue resync for server ' . $this->db->getRowValue($entry, 'server')); break; } @@ -328,8 +327,8 @@ class SyncLib /* Retrieve info from entry info string */ - $validationParams=$this->localParamsFromInfoString($entry['info']); - $otpParams=$this->otpParamsFromInfoString($entry['info']); + $validationParams=$this->localParamsFromInfoString($this->db->getRowValue($entry, 'info')); + $otpParams=$this->otpParamsFromInfoString($this->db->getRowValue($entry, 'info')); $localParams=$this->getLocalParams($otpParams['yk_publicname']); $this->log(LOG_DEBUG, "validation params: ", $validationParams); @@ -362,21 +361,22 @@ class SyncLib } /* Deletion */ - $this->log(LOG_INFO, 'deleting queue entry with modified=' . $entry['modified'] . - ' server_nonce=' . $entry['server_nonce'] . - ' server=' . $entry['server']); + $this->log(LOG_INFO, 'deleting queue entry with modified=' . $this->db->getRowValue($entry, 'modified') . + ' server_nonce=' . $this->db->getRowValue($entry, 'server_nonce') . + ' server=' . $this->db->getRowValue($entry, 'server')); $this->db->deleteByMultiple('queue', - array("modified"=>$entry['modified'], - "server_nonce"=>$entry['server_nonce'], - 'server'=>$entry['server'])); + array("modified"=>$this->db->getRowValue($entry, 'modified'), + "server_nonce"=>$this->db->getRowValue($entry, 'server_nonce'), + 'server'=>$this->db->getRowValue($entry, 'server'))); } else { $this->log(LOG_ERR, "Remote server refused our sync request. Check remote server logs."); } } /* End of loop over each queue entry for a server */ - curl_close($ch); - $res->closeCursor(); + curl_close($ch); + $this->db->closeCursor($res); } /* End of loop over each distinct server in queue */ + $this->db->closeCursor($server_res); return true; } @@ -388,11 +388,11 @@ class SyncLib $urls=array(); $res=$this->db->findByMultiple('queue', array("modified"=>$this->otpParams['modified'], "server_nonce"=>$this->server_nonce)); - foreach ($res as $row) { - $urls[]=$row['server'] . - "?otp=" . $row['otp'] . - "&modified=" . $row['modified'] . - "&" . $this->otpPartFromInfoString($row['info']); + while ($row = $this->db->fetchArray($res)) { + $urls[]=$this->db->getRowValue($row, 'server') . + "?otp=" . $this->db->getRowValue($row, 'otp') . + "&modified=" . $this->db->getRowValue($row, 'modified') . + "&" . $this->otpPartFromInfoString($this->db->getRowValue($row, 'info')); } /* diff --git a/ykval-verify.php b/ykval-verify.php index 21516c6..085427f 100644 --- a/ykval-verify.php +++ b/ykval-verify.php @@ -11,7 +11,7 @@ $myLog = new Log('ykval-verify'); $myLog->addField('ip', $_SERVER['REMOTE_ADDR']); $myLog->log(LOG_INFO, "Request: " . $_SERVER['QUERY_STRING'] . " (at " . date("c") . " " . microtime() . ") " . - (isset($_SERVER["HTTPS"]) == TRUE ? ($_SERVER["HTTPS"] == "on" ? "HTTPS" : "HTTP") : "HTTP")); + (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "HTTPS" : "HTTP")); /* Detect protocol version */ if (preg_match("/\/wsapi\/([0-9]+)\.([0-9]+)\//", $_SERVER['REQUEST_URI'], $out)) { From 9e9f20b959bfd4839858cfdca4cf85624da0196e Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 13:27:51 +0200 Subject: [PATCH 03/19] more oracle compatibility --- ykval-checksum-clients.php | 6 +----- ykval-db-oci.php | 2 +- ykval-db-pdo.php | 2 +- ykval-db.php | 20 +++++++++++++++++++- ykval-export-clients.php | 27 +++++++++++++-------------- ykval-export.php | 6 +----- ykval-import-clients.php | 9 +++------ ykval-import.php | 7 ++----- ykval-revoke.php | 6 +----- ykval-synclib.php | 7 +------ 10 files changed, 43 insertions(+), 49 deletions(-) diff --git a/ykval-checksum-clients.php b/ykval-checksum-clients.php index 7bc2b1d..596afeb 100755 --- a/ykval-checksum-clients.php +++ b/ykval-checksum-clients.php @@ -26,11 +26,7 @@ require_once 'ykval-db.php'; $logname="ykval-checksum-clients"; $myLog = new Log($logname); -$db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); +$db = Db::GetDatabaseHandle($baseParams, $logname); if (!$db->connect()) { $myLog->log(LOG_WARNING, "Could not connect to database"); diff --git a/ykval-db-oci.php b/ykval-db-oci.php index b261725..7bf1848 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -8,7 +8,7 @@ require_once('ykval-log.php'); require_once('ykval-db.php'); -class OciDb extends DB +class DbImpl extends DB { /** * Constructor diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php index c92da46..5f7070f 100644 --- a/ykval-db-pdo.php +++ b/ykval-db-pdo.php @@ -8,7 +8,7 @@ require_once('ykval-log.php'); require_once('ykval-db.php'); -class PdoDb +class DbImpl { diff --git a/ykval-db.php b/ykval-db.php index 772dfef..8ce3d9e 100644 --- a/ykval-db.php +++ b/ykval-db.php @@ -7,8 +7,26 @@ require_once('ykval-log.php'); -class Db +abstract class Db { + /** + * static function to determine database type and instantiate the correct subclass + * + * */ + public static function GetDatabaseHandle($baseParams, $logname) + { + if(substr($baseParams['__YKVAL_DB_DSN__'], 0, 3) == oci) { + require_once 'ykval-db-oci.php'; + } else { + require_once 'ykval-db-pdo.php'; + } + return new DbImpl($baseParams['__YKVAL_DB_DSN__'], + $baseParams['__YKVAL_DB_USER__'], + $baseParams['__YKVAL_DB_PW__'], + $baseParams['__YKVAL_DB_OPTIONS__'], + $logname . ':db'); + } + function addField($name, $value) { $this->myLog->addField($name, $value); diff --git a/ykval-export-clients.php b/ykval-export-clients.php index cf3b5fb..26f9e87 100755 --- a/ykval-export-clients.php +++ b/ykval-export-clients.php @@ -10,11 +10,7 @@ require_once 'ykval-db.php'; $logname="ykval-export"; $myLog = new Log($logname); -$db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); +$db = Db::GetDatabaseHandle($baseParams, $logname); if (!$db->connect()) { $myLog->log(LOG_WARNING, "Could not connect to database"); @@ -22,16 +18,19 @@ if (!$db->connect()) { } $result = $db->customQuery("select id, active, created, secret, email, notes, otp from clients order by id"); -while($row = $result->fetch(PDO::FETCH_ASSOC)){ - echo $row['id'] . - "\t" . $row['active'] . - "\t" . $row['created'] . - "\t" . $row['secret'] . - "\t" . $row['email'] . - "\t" . $row['notes'] . - "\t" . $row['otp'] . +while($row = $db->fetchArray($result)) { + echo $db->getRowValue($row, 'id'] . + "\t" . $db->getRowValue($row, 'active') . + "\t" . $db->getRowValue($row, 'created') . + "\t" . $db->getRowValue($row, 'secret') . + "\t" . $db->getRowValue($row, 'email') . + "\t" . $db->getRowValue($row, 'notes') . + "\t" . $db->getRowValue($row, 'otp') . "\n"; - } +} + +$db->closeCursor($result); +$db->disconnect(); $result=null; $db=null; diff --git a/ykval-export.php b/ykval-export.php index 1fe6c76..a488f82 100755 --- a/ykval-export.php +++ b/ykval-export.php @@ -10,11 +10,7 @@ require_once 'ykval-db.php'; $logname="ykval-export"; $myLog = new Log($logname); -$db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); +$db = Db::GetDatabaseHandle($baseParams, $logname); if (!$db->connect()) { $myLog->log(LOG_WARNING, "Could not connect to database"); diff --git a/ykval-import-clients.php b/ykval-import-clients.php index 3141175..57a170f 100755 --- a/ykval-import-clients.php +++ b/ykval-import-clients.php @@ -10,11 +10,7 @@ require_once 'ykval-db.php'; $logname="ykval-import"; $myLog = new Log($logname); -$db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); +$db = Db::GetDatabaseHandle($baseParams, $logname); if (!$db->connect()) { $myLog->log(LOG_WARNING, "Could not connect to database"); @@ -35,7 +31,7 @@ while ($res=fgetcsv(STDIN, 0, "\t")) { $query="SELECT * FROM clients WHERE id='" . $params['id'] . "'"; $result=$db->customQuery($query); - if(!$result->fetch(PDO::FETCH_ASSOC)) { + if($db->rowCount($result) == 0) { // We didn't have the id in database so we need to do insert instead $query="INSERT INTO clients " . "(id,active,created,secret,email,notes,otp) VALUES " . @@ -53,6 +49,7 @@ while ($res=fgetcsv(STDIN, 0, "\t")) { exit(1); } } + $db->closeCursor($result); } diff --git a/ykval-import.php b/ykval-import.php index 636b741..e4d8607 100755 --- a/ykval-import.php +++ b/ykval-import.php @@ -10,11 +10,7 @@ require_once 'ykval-db.php'; $logname="ykval-import"; $myLog = new Log($logname); -$db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); +$db = Db::GetDatabaseHandle($baseParams, $logname); if (!$db->connect()) { $myLog->log(LOG_WARNING, "Could not connect to database"); @@ -80,6 +76,7 @@ while ($res=fgetcsv(STDIN, 0, "\t")) { exit(1); } } + $db->closeCursor($result); } diff --git a/ykval-revoke.php b/ykval-revoke.php index b1496df..f88679d 100644 --- a/ykval-revoke.php +++ b/ykval-revoke.php @@ -23,11 +23,7 @@ if ($do != "enable" && $do != "disable") { } # Connect to db -$db = new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - 'ykval-revoke:db'); +$db = Db::GetDatabaseHandle($baseParams, 'ykval-revoke'); if (!$db->connect()) { logdie("ERROR Database connect error"); } diff --git a/ykval-synclib.php b/ykval-synclib.php index d5ff097..297b2b7 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -15,12 +15,7 @@ class SyncLib $this->myLog = new Log($logname); global $baseParams; $this->syncServers = $baseParams['__YKVAL_SYNC_POOL__']; - - $this->db=new Db($baseParams['__YKVAL_DB_DSN__'], - $baseParams['__YKVAL_DB_USER__'], - $baseParams['__YKVAL_DB_PW__'], - $baseParams['__YKVAL_DB_OPTIONS__'], - $logname . ':db'); + $this->db = Db::GetDatabaseHandle($baseParams, $logname); $this->isConnected=$this->db->connect(); $this->server_nonce=md5(uniqid(rand())); From 97757fc36e20d1548f7132371adab54a6cef884d Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 13:30:41 +0200 Subject: [PATCH 04/19] sql for initing oracle --- ykval-db.oracle.sql | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ykval-db.oracle.sql diff --git a/ykval-db.oracle.sql b/ykval-db.oracle.sql new file mode 100644 index 0000000..f22b901 --- /dev/null +++ b/ykval-db.oracle.sql @@ -0,0 +1,36 @@ +-- I created a new sql file because oracle does not allow boolean type +-- so I used the type NUMBER(1) which is pretty similar + +CREATE TABLE clients ( + id INT NOT NULL, + active NUMBER(1) DEFAULT 1, + created INT NOT NULL, + secret VARCHAR(60) DEFAULT '', + email VARCHAR(255), + notes VARCHAR(100) DEFAULT '', + otp VARCHAR(100) DEFAULT '', + PRIMARY KEY (id) +); + +CREATE TABLE yubikeys ( + active NUMBER(1) DEFAULT 1, + created INT NOT NULL, + modified INT NOT NULL, + yk_publicname VARCHAR(16) NOT NULL, + yk_counter INT NOT NULL, + yk_use INT NOT NULL, + yk_low INT NOT NULL, + yk_high INT NOT NULL, + nonce VARCHAR(32) DEFAULT '', + notes VARCHAR(100) DEFAULT '', + PRIMARY KEY (yk_publicname) +); + +CREATE TABLE queue ( + queued INT DEFAULT NULL, + modified INT DEFAULT NULL, + server_nonce VARCHAR(32) NOT NULL, + otp VARCHAR(100) NOT NULL, + server VARCHAR(100) NOT NULL, + info VARCHAR(256) NOT NULL +); From 13463beb064d7b2e8c4ad783d8b1b8466616e07b Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 13:37:55 +0200 Subject: [PATCH 05/19] extends properly --- ykval-db-oci.php | 2 +- ykval-db-pdo.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ykval-db-oci.php b/ykval-db-oci.php index 7bf1848..fe4dc1a 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -8,7 +8,7 @@ require_once('ykval-log.php'); require_once('ykval-db.php'); -class DbImpl extends DB +class DbImpl extends Db { /** * Constructor diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php index 5f7070f..603094a 100644 --- a/ykval-db-pdo.php +++ b/ykval-db-pdo.php @@ -8,7 +8,7 @@ require_once('ykval-log.php'); require_once('ykval-db.php'); -class DbImpl +class DbImpl extends Db { From 929e7aedc9280bda411d3aadb83e4490699e8503 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 13:47:22 +0200 Subject: [PATCH 06/19] add new db classes and oracle sql to Makefile --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d816720..e74d2f9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ CODE = COPYING Makefile NEWS ykval-checksum-clients.php \ ykval-export.php ykval-import.php ykval-log.php ykval-ping.php \ ykval-queue.php ykval-revoke.php ykval-synclib.php \ ykval-sync.php ykval-verify.php ykval-export-clients.php \ - ykval-import-clients.php + ykval-import-clients.php ykval-db-oci.php ykval-db-pdo.php \ + ykval-db.oracle.sql MUNIN = ykval-munin-ksmlatency.php ykval-munin-vallatency.php \ ykval-munin-queuelength.php DOCS = doc/ClientInfoFormat.wiki doc/Installation.wiki \ @@ -32,6 +33,8 @@ install: install -D --mode 644 ykval-synclib.php $(DESTDIR)$(phpprefix)/ykval-synclib.php install -D --mode 644 ykval-sync.php $(DESTDIR)$(phpprefix)/ykval-sync.php install -D --mode 644 ykval-db.php $(DESTDIR)$(phpprefix)/ykval-db.php + install -D --mode 644 ykval-db-pdo.php $(DESTDIR)$(phpprefix)/ykval-db-pdo.php + install -D --mode 644 ykval-db-oci.php $(DESTDIR)$(phpprefix)/ykval-db-oci.php install -D --mode 644 ykval-log.php $(DESTDIR)$(phpprefix)/ykval-log.php install -D ykval-queue.php $(DESTDIR)$(sbinprefix)/ykval-queue install -D ykval-export.php $(DESTDIR)$(sbinprefix)/ykval-export @@ -44,6 +47,7 @@ install: install -D ykval-munin-queuelength.php $(DESTDIR)$(muninprefix)/ykval_queuelength install -D --backup --mode 640 --group $(wwwgroup) ykval-config.php $(DESTDIR)$(etcprefix)/ykval-config.php-template install -D --mode 644 ykval-db.sql $(DESTDIR)$(docprefix)/ykval-db.sql + install -D --mode 644 ykval-db.oracle.sql $(DESTDIR)$(docprefix)/ykval-db.oracle.sql install -D --mode 644 $(DOCS) $(DESTDIR)$(docprefix)/ wwwprefix = /var/www/wsapi From 30c302fa68f78873d3cae350819fe7cc11c2b8c5 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 14:25:54 +0200 Subject: [PATCH 07/19] remove bareword oci --- ykval-db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-db.php b/ykval-db.php index 8ce3d9e..b4288bc 100644 --- a/ykval-db.php +++ b/ykval-db.php @@ -15,7 +15,7 @@ abstract class Db * */ public static function GetDatabaseHandle($baseParams, $logname) { - if(substr($baseParams['__YKVAL_DB_DSN__'], 0, 3) == oci) { + if(substr($baseParams['__YKVAL_DB_DSN__'], 0, 3) == 'oci') { require_once 'ykval-db-oci.php'; } else { require_once 'ykval-db-pdo.php'; From f26db7b71eac737901c246d64c6737ec19a3ba24 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 12 Jun 2012 14:27:21 +0200 Subject: [PATCH 08/19] make query protected, accessed from super class --- ykval-db-oci.php | 2 +- ykval-db-pdo.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ykval-db-oci.php b/ykval-db-oci.php index fe4dc1a..349158c 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -52,7 +52,7 @@ class DbImpl extends Db return true; } - private function query($query, $returnresult=false) { + protected function query($query, $returnresult=false) { if(!$this->isConnected()) { $this->connect(); } diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php index 603094a..83592d1 100644 --- a/ykval-db-pdo.php +++ b/ykval-db-pdo.php @@ -50,7 +50,7 @@ class DbImpl extends Db return true; } - private function query($query, $returnresult=false) { + protected function query($query, $returnresult=false) { if(!$this->isConnected()) { $this->connect(); } From a86414b9ff68e6fd593bcf55ea79024803260cb6 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 13 Jun 2012 08:11:03 +0200 Subject: [PATCH 09/19] remove $this, not in object context --- ykval-checksum-clients.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ykval-checksum-clients.php b/ykval-checksum-clients.php index 596afeb..fc12547 100755 --- a/ykval-checksum-clients.php +++ b/ykval-checksum-clients.php @@ -37,18 +37,18 @@ $everything = ""; $result=$db->customQuery("SELECT id, active, secret ". "FROM clients ". "ORDER BY id"); -while($row = $this->db->fetchArray($result)) { - $active = $this->db->getRowValue($row, 'active'); +while($row = $db->fetchArray($result)) { + $active = $db->getRowValue($row, 'active'); if ($active == "") { # For some reason PostgreSQL returns empty strings for false values?! $active = "0"; } $everything = $everything . - $this->db->getRowValue($row, 'id') . "\t" . $active . "\t" . - $this->db->getRowValue($row, 'secret') . "\n"; + $db->getRowValue($row, 'id') . "\t" . $active . "\t" . + $db->getRowValue($row, 'secret') . "\n"; } -$this->db->closeCursor($result); +$db->closeCursor($result); $hash = sha1 ($everything); if ($verbose) { From 2bbb3b4c02c2b18ee5d7212ef8328b68b1111cf2 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 13 Jun 2012 08:53:52 +0200 Subject: [PATCH 10/19] findByMultiple() calls fetchArray on it's own result, just loop it. --- ykval-synclib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-synclib.php b/ykval-synclib.php index 297b2b7..ecec83d 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -383,7 +383,7 @@ class SyncLib $urls=array(); $res=$this->db->findByMultiple('queue', array("modified"=>$this->otpParams['modified'], "server_nonce"=>$this->server_nonce)); - while ($row = $this->db->fetchArray($res)) { + foreach($res as $row) $urls[]=$this->db->getRowValue($row, 'server') . "?otp=" . $this->db->getRowValue($row, 'otp') . "&modified=" . $this->db->getRowValue($row, 'modified') . From 3f17b7bdc1cf6eace308e5917c68bb9b96ef0e23 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 13 Jun 2012 09:00:39 +0200 Subject: [PATCH 11/19] and { is needed. --- ykval-synclib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-synclib.php b/ykval-synclib.php index ecec83d..4d3d3fd 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -383,7 +383,7 @@ class SyncLib $urls=array(); $res=$this->db->findByMultiple('queue', array("modified"=>$this->otpParams['modified'], "server_nonce"=>$this->server_nonce)); - foreach($res as $row) + foreach($res as $row) { $urls[]=$this->db->getRowValue($row, 'server') . "?otp=" . $this->db->getRowValue($row, 'otp') . "&modified=" . $this->db->getRowValue($row, 'modified') . From ae6177f15747a098e297cd12437d01f640a042a4 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 13 Jun 2012 09:26:02 +0200 Subject: [PATCH 12/19] change ] to ) --- ykval-export-clients.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-export-clients.php b/ykval-export-clients.php index 26f9e87..39e9fb5 100755 --- a/ykval-export-clients.php +++ b/ykval-export-clients.php @@ -19,7 +19,7 @@ if (!$db->connect()) { $result = $db->customQuery("select id, active, created, secret, email, notes, otp from clients order by id"); while($row = $db->fetchArray($result)) { - echo $db->getRowValue($row, 'id'] . + echo $db->getRowValue($row, 'id') . "\t" . $db->getRowValue($row, 'active') . "\t" . $db->getRowValue($row, 'created') . "\t" . $db->getRowValue($row, 'secret') . From 9f74175853e69eaa844e14a742069a215a223261 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 13 Jun 2012 13:29:40 +0200 Subject: [PATCH 13/19] 40 char nonce for oracle as well --- ykval-db.oracle.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-db.oracle.sql b/ykval-db.oracle.sql index f22b901..24fab03 100644 --- a/ykval-db.oracle.sql +++ b/ykval-db.oracle.sql @@ -21,7 +21,7 @@ CREATE TABLE yubikeys ( yk_use INT NOT NULL, yk_low INT NOT NULL, yk_high INT NOT NULL, - nonce VARCHAR(32) DEFAULT '', + nonce VARCHAR(40) DEFAULT '', notes VARCHAR(100) DEFAULT '', PRIMARY KEY (yk_publicname) ); From 605abc8cbf96bbe24623a5840a45b38558b29169 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Wed, 13 Jun 2012 13:41:12 +0200 Subject: [PATCH 14/19] delete-trailing-whitespace --- ykval-synclib.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ykval-synclib.php b/ykval-synclib.php index 0c87dca..d431bbb 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -159,7 +159,7 @@ class SyncLib 'nonce' => $this->db->getRowValue($res, 'nonce'), 'active' => $this->db->getRowValue($res, 'active'), 'yk_publicname' => $yk_publicname, - 'yk_counter' => $this->db->getRowValue($res, 'yk_counter'), + 'yk_counter' => $this->db->getRowValue($res, 'yk_counter'), 'yk_use' => $this->db->getRowValue($res, 'yk_use'), 'yk_high' => $this->db->getRowValue($res, 'yk_high'), 'yk_low' => $this->db->getRowValue($res, 'yk_low')); @@ -293,7 +293,7 @@ class SyncLib $ch = curl_init(); while ($entry=$this->db->fetchArray($res)) { $this->log(LOG_INFO, "server=" . $this->db->getRowValue($entry, 'server') . " , info=" . $this->db->getRowValue($entry, 'info')); - $url=$this->db->getRowValue($entry, 'server') . + $url=$this->db->getRowValue($entry, 'server') . "?otp=" . $this->db->getRowValue($entry, 'otp') . "&modified=" . $this->db->getRowValue($entry, 'modified') . "&" . $this->otpPartFromInfoString($this->db->getRowValue($entry, 'info')); @@ -361,7 +361,7 @@ class SyncLib ' server=' . $this->db->getRowValue($entry, 'server')); $this->db->deleteByMultiple('queue', array("modified"=>$this->db->getRowValue($entry, 'modified'), - "server_nonce"=>$this->db->getRowValue($entry, 'server_nonce'), + "server_nonce"=>$this->db->getRowValue($entry, 'server_nonce'), 'server'=>$this->db->getRowValue($entry, 'server'))); } else { $this->log(LOG_ERR, "Remote server refused our sync request. Check remote server logs."); @@ -384,7 +384,7 @@ class SyncLib $urls=array(); $res=$this->db->findByMultiple('queue', array("modified"=>$this->otpParams['modified'], "server_nonce"=>$this->server_nonce)); foreach($res as $row) { - $urls[]=$this->db->getRowValue($row, 'server') . + $urls[]=$this->db->getRowValue($row, 'server') . "?otp=" . $this->db->getRowValue($row, 'otp') . "&modified=" . $this->db->getRowValue($row, 'modified') . "&" . $this->otpPartFromInfoString($this->db->getRowValue($row, 'info')); From e5ea2ead5f46b3304ddb59e4dbb9f3fd90f83ba0 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Thu, 14 Jun 2012 11:05:44 +0200 Subject: [PATCH 15/19] Check for ^oci: instead of just ^oci. --- ykval-db-oci.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-db-oci.php b/ykval-db-oci.php index 349158c..7081fe3 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -27,7 +27,7 @@ class DbImpl extends Db $this->db_password=$db_password; $this->db_options=$db_options; - if(substr($db_dsn, 0, 3) == 'oci') { + if(substr($db_dsn, 0, 4) == 'oci:') { # "oci:" prefix needs to be removed before passing db_dsn to OCI $this->db_dsn = substr($this->db_dsn, 4); } From a739667891432e3b22a6f9c76f4332cf548173a2 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Thu, 14 Jun 2012 13:00:13 +0200 Subject: [PATCH 16/19] lowercase columns from oracle, we use lower everywhere. --- ykval-db-oci.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ykval-db-oci.php b/ykval-db-oci.php index 349158c..132c65e 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -86,7 +86,8 @@ class DbImpl extends Db if(!$result) $result = $this->result; if(!$result) return null; - return oci_fetch_array($result, OCI_ASSOC); + $res = oci_fetch_array($result, OCI_ASSOC); + return array_change_key_case($res, CASE_LOWER); } /** From b29d0ecdffe9585aedc7dd7dd25b2af3825cff07 Mon Sep 17 00:00:00 2001 From: Remi Mollon Date: Thu, 28 Jun 2012 14:53:54 +0200 Subject: [PATCH 17/19] fix Oracle query in findByMultiple + getRowValue is not needed anymore --- ykval-db-oci.php | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/ykval-db-oci.php b/ykval-db-oci.php index 9fe895c..bce575c 100644 --- a/ykval-db-oci.php +++ b/ykval-db-oci.php @@ -114,9 +114,13 @@ class DbImpl extends Db { $value=""; /* quiet the PHP Notice */ $match=null; /* quiet the PHP Notice */ - # LIMIT doesn't exist in Oracle, so we encapsulate the query to be - # able to filter a given number of rows afterwars (after ordering) - $query="SELECT * FROM (SELECT"; + $query="SELECT"; + + if($nr!=null){ + # LIMIT doesn't exist in Oracle, so we encapsulate the query to be + # able to filter a given number of rows afterwars (after ordering) + $query.= " * FROM (SELECT"; + } if ($distinct!=null) { $query.= " DISTINCT " . $distinct; @@ -206,22 +210,6 @@ class DbImpl extends Db return 0; } } - - /** - * Function to return the value corresponding to a given attribute name - * PDO requires lower case strings, whereas OCI requires upper case strings - * - * @param array $row Query result's row - * @param string $key Attribute name - * @return string Value of the attribute in this row - * - */ - public function getRowValue($row, $key) - { - $attr = strtoupper($key); - return $row[$attr]; - } - } From f9e1a3a8831630f88a33e45f3a9ed5951b17a777 Mon Sep 17 00:00:00 2001 From: Remi Mollon Date: Thu, 28 Jun 2012 14:55:00 +0200 Subject: [PATCH 18/19] getRowValue is not needed anymore --- ykval-checksum-clients.php | 6 ++--- ykval-db-pdo.php | 14 ---------- ykval-export-clients.php | 14 +++++----- ykval-export.php | 20 +++++++------- ykval-synclib.php | 54 +++++++++++++++++++------------------- 5 files changed, 47 insertions(+), 61 deletions(-) diff --git a/ykval-checksum-clients.php b/ykval-checksum-clients.php index fc12547..7d2854b 100755 --- a/ykval-checksum-clients.php +++ b/ykval-checksum-clients.php @@ -38,14 +38,14 @@ $result=$db->customQuery("SELECT id, active, secret ". "FROM clients ". "ORDER BY id"); while($row = $db->fetchArray($result)) { - $active = $db->getRowValue($row, 'active'); + $active = $row['active']; if ($active == "") { # For some reason PostgreSQL returns empty strings for false values?! $active = "0"; } $everything = $everything . - $db->getRowValue($row, 'id') . "\t" . $active . "\t" . - $db->getRowValue($row, 'secret') . "\n"; + $row['id'] . "\t" . $active . "\t" . + $row['secret'] . "\n"; } $db->closeCursor($result); diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php index 83592d1..ba2371d 100644 --- a/ykval-db-pdo.php +++ b/ykval-db-pdo.php @@ -205,20 +205,6 @@ class DbImpl extends Db return 0; } } - - /** - * Function to return the value corresponding to a given attribute name - * PDO requires lower case strings, whereas OCI requires upper case strings - * - * @param array $row Query result's row - * @param string $key Attribute name - * @return string Value of the attribute in this row - * - */ - public function getRowValue($row, $key) - { - return $row[$key]; - } } diff --git a/ykval-export-clients.php b/ykval-export-clients.php index 39e9fb5..6c599ba 100755 --- a/ykval-export-clients.php +++ b/ykval-export-clients.php @@ -19,13 +19,13 @@ if (!$db->connect()) { $result = $db->customQuery("select id, active, created, secret, email, notes, otp from clients order by id"); while($row = $db->fetchArray($result)) { - echo $db->getRowValue($row, 'id') . - "\t" . $db->getRowValue($row, 'active') . - "\t" . $db->getRowValue($row, 'created') . - "\t" . $db->getRowValue($row, 'secret') . - "\t" . $db->getRowValue($row, 'email') . - "\t" . $db->getRowValue($row, 'notes') . - "\t" . $db->getRowValue($row, 'otp') . + echo $row['id'] . + "\t" . $row['active'] . + "\t" . $row['created'] . + "\t" . $row['secret'] . + "\t" . $row['email'] . + "\t" . $row['notes'] . + "\t" . $row['otp'] . "\n"; } diff --git a/ykval-export.php b/ykval-export.php index a488f82..a4c0c84 100755 --- a/ykval-export.php +++ b/ykval-export.php @@ -19,16 +19,16 @@ if (!$db->connect()) { $result=$db->customQuery("SELECT active, created, modified, yk_publicname, yk_counter, yk_use, yk_low, yk_high, nonce, notes FROM yubikeys ORDER BY yk_publicname"); while($row = $db->fetchArray($result)){ - echo $db->getRowValue($row, 'active') . - "\t" . $db->getRowValue($row, 'created') . - "\t" . $db->getRowValue($row, 'modified') . - "\t" . $db->getRowValue($row, 'yk_publicname') . - "\t" . $db->getRowValue($row, 'yk_counter') . - "\t" . $db->getRowValue($row, 'yk_use') . - "\t" . $db->getRowValue($row, 'yk_low') . - "\t" . $db->getRowValue($row, 'yk_high') . - "\t" . $db->getRowValue($row, 'nonce') . - "\t" . $db->getRowValue($row, 'notes') . + echo $row['active'] . + "\t" . $row['created'] . + "\t" . $row['modified'] . + "\t" . $row['yk_publicname'] . + "\t" . $row['yk_counter'] . + "\t" . $row['yk_use'] . + "\t" . $row['yk_low'] . + "\t" . $row['yk_high'] . + "\t" . $row['nonce'] . + "\t" . $row['notes'] . "\n"; } diff --git a/ykval-synclib.php b/ykval-synclib.php index d431bbb..e895e13 100644 --- a/ykval-synclib.php +++ b/ykval-synclib.php @@ -155,14 +155,14 @@ class SyncLib $res=$this->db->findBy('yubikeys', 'yk_publicname', $yk_publicname,1); } if ($res) { - $localParams=array('modified' => $this->db->getRowValue($res, 'modified'), - 'nonce' => $this->db->getRowValue($res, 'nonce'), - 'active' => $this->db->getRowValue($res, 'active'), + $localParams=array('modified' => $res['modified'], + 'nonce' => $res['nonce'], + 'active' => $res['active'], 'yk_publicname' => $yk_publicname, - 'yk_counter' => $this->db->getRowValue($res, 'yk_counter'), - 'yk_use' => $this->db->getRowValue($res, 'yk_use'), - 'yk_high' => $this->db->getRowValue($res, 'yk_high'), - 'yk_low' => $this->db->getRowValue($res, 'yk_low')); + 'yk_counter' => $res['yk_counter'], + 'yk_use' => $res['yk_use'], + 'yk_high' => $res['yk_high'], + 'yk_low' => $res['yk_low']); $this->log(LOG_INFO, "yubikey found in db ", $localParams); return $localParams; @@ -288,15 +288,15 @@ class SyncLib while ($my_server=$this->db->fetchArray($server_res)) { - $this->log(LOG_INFO, "Sending queue request to server on server " . $this->db->getRowValue($my_server, 'server')); - $res=$this->db->customQuery("select * from queue WHERE (queued < " . $queued_limit . " or queued is null) and server='" . $this->db->getRowValue($my_server, 'server') . "'"); + $this->log(LOG_INFO, "Sending queue request to server on server " . $my_server['server']); + $res=$this->db->customQuery("select * from queue WHERE (queued < " . $queued_limit . " or queued is null) and server='" . $my_server['server'] . "'"); $ch = curl_init(); while ($entry=$this->db->fetchArray($res)) { - $this->log(LOG_INFO, "server=" . $this->db->getRowValue($entry, 'server') . " , info=" . $this->db->getRowValue($entry, 'info')); - $url=$this->db->getRowValue($entry, 'server') . - "?otp=" . $this->db->getRowValue($entry, 'otp') . - "&modified=" . $this->db->getRowValue($entry, 'modified') . - "&" . $this->otpPartFromInfoString($this->db->getRowValue($entry, 'info')); + $this->log(LOG_INFO, "server=" . $entry['server'] . " , info=" . $entry['info']); + $url=$entry['server'] . + "?otp=" . $entry['otp'] . + "&modified=" . $entry['modified'] . + "&" . $this->otpPartFromInfoString($entry['info']); /* Send out sync request */ $this->log(LOG_DEBUG, 'url is ' . $url); @@ -309,7 +309,7 @@ class SyncLib $response = curl_exec($ch); if ($response==False) { - $this->log(LOG_NOTICE, 'Timeout. Stopping queue resync for server ' . $this->db->getRowValue($entry, 'server')); + $this->log(LOG_NOTICE, 'Timeout. Stopping queue resync for server ' . $entry['server']); break; } @@ -322,8 +322,8 @@ class SyncLib /* Retrieve info from entry info string */ - $validationParams=$this->localParamsFromInfoString($this->db->getRowValue($entry, 'info')); - $otpParams=$this->otpParamsFromInfoString($this->db->getRowValue($entry, 'info')); + $validationParams=$this->localParamsFromInfoString($entry['info']); + $otpParams=$this->otpParamsFromInfoString($entry['info']); $localParams=$this->getLocalParams($otpParams['yk_publicname']); $this->log(LOG_DEBUG, "validation params: ", $validationParams); @@ -356,13 +356,13 @@ class SyncLib } /* Deletion */ - $this->log(LOG_INFO, 'deleting queue entry with modified=' . $this->db->getRowValue($entry, 'modified') . - ' server_nonce=' . $this->db->getRowValue($entry, 'server_nonce') . - ' server=' . $this->db->getRowValue($entry, 'server')); + $this->log(LOG_INFO, 'deleting queue entry with modified=' . $entry['modified'] . + ' server_nonce=' . $entry['server_nonce'] . + ' server=' . $entry['server']); $this->db->deleteByMultiple('queue', - array("modified"=>$this->db->getRowValue($entry, 'modified'), - "server_nonce"=>$this->db->getRowValue($entry, 'server_nonce'), - 'server'=>$this->db->getRowValue($entry, 'server'))); + array("modified"=>$entry['modified'], + "server_nonce"=>$entry['server_nonce'], + 'server'=>$entry['server'])); } else { $this->log(LOG_ERR, "Remote server refused our sync request. Check remote server logs."); } @@ -384,10 +384,10 @@ class SyncLib $urls=array(); $res=$this->db->findByMultiple('queue', array("modified"=>$this->otpParams['modified'], "server_nonce"=>$this->server_nonce)); foreach($res as $row) { - $urls[]=$this->db->getRowValue($row, 'server') . - "?otp=" . $this->db->getRowValue($row, 'otp') . - "&modified=" . $this->db->getRowValue($row, 'modified') . - "&" . $this->otpPartFromInfoString($this->db->getRowValue($row, 'info')); + $urls[]=$row['server'] . + "?otp=" . $row['otp'] . + "&modified=" . $row['modified'] . + "&" . $this->otpPartFromInfoString($row['info']); } /* From 806ad0c72fb2d2ab282106c92b29d72956c0b0eb Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Fri, 29 Jun 2012 10:36:32 +0200 Subject: [PATCH 19/19] do $res->rowCount() instead of count($res->fetchAll()) this time for the pdo code on oracle branch. --- ykval-db-pdo.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ykval-db-pdo.php b/ykval-db-pdo.php index ba2371d..3032490 100644 --- a/ykval-db-pdo.php +++ b/ykval-db-pdo.php @@ -198,11 +198,11 @@ class DbImpl extends Db { if(!$result) $result = $this->result; if($result) { - $count = count($result->fetchAll()); - $result->closeCursor(); - return $count; + $count=$result->rowCount(); + $result->closeCursor(); + return $count; } else { - return 0; + return 0; } } }