From 3de7ca3c8b3e8706867c8b0571008f3e02432e42 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 22 May 2012 13:15:25 +0200 Subject: [PATCH 1/8] Add code to let the db reconnect after errors. Set PDO error mode to throw exceptions so we can catch them and do things. --- ykval-config.php | 2 +- ykval-db.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ykval-config.php b/ykval-config.php index 4ed16e8..fda083a 100644 --- a/ykval-config.php +++ b/ykval-config.php @@ -5,7 +5,7 @@ $baseParams = array (); $baseParams['__YKVAL_DB_DSN__'] = "mysql:dbname=ykval;host=127.0.0.1"; $baseParams['__YKVAL_DB_USER__'] = 'ykval_verifier'; $baseParams['__YKVAL_DB_PW__'] = 'lab'; -$baseParams['__YKVAL_DB_OPTIONS__'] = array(); +$baseParams['__YKVAL_DB_OPTIONS__'] = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); # For the validation server sync $baseParams['__YKVAL_SYNC_POOL__'] = array("http://api2.example.com/wsapi/2.0/sync", diff --git a/ykval-db.php b/ykval-db.php index b133149..106c4c4 100644 --- a/ykval-db.php +++ b/ykval-db.php @@ -104,12 +104,17 @@ class Db } private function query($query, $returnresult=false) { - if($this->dbh) { + if(!$this->isConnected()) { + $this->connect(); + } + if($this->isConnected()) { $this->myLog->log(LOG_DEBUG, 'DB query is: ' . $query); - $this->result = $this->dbh->query($query); - if (! $this->result){ + 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; From c936ec5d36c9c6011363d0301aad1ee288526081 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 22 May 2012 14:19:20 +0200 Subject: [PATCH 2/8] News for 2.14 --- NEWS | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index de36d0c..0e821fb 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,10 @@ -* Version 2.14 unreleased +* Version 2.14 released 2012-05-22 + + * Add support for reconnecting to database after errors. + + * Fixes for PHP warnings. + + * Detect timeouts and errors in munin checks. * Version 2.13 released 2012-05-16 From 36b3050d6fee816e978ef5dd2321ea6f457150b6 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 22 May 2012 14:21:04 +0200 Subject: [PATCH 3/8] Bump version --- Makefile | 2 +- NEWS | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8199699..a834deb 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 2.14 +VERSION = 2.15 PACKAGE = yubikey-val CODE = COPYING Makefile NEWS ykval-checksum-clients.php \ ykval-common.php ykval-config.php ykval-db.php ykval-db.sql \ diff --git a/NEWS b/NEWS index 0e821fb..171edab 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ +* Version 2.15 unreleased + * Version 2.14 released 2012-05-22 * Add support for reconnecting to database after errors. From 020d8dd3dc190cbd6df5745af2d752db80c3086f Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 23 May 2012 08:56:15 +0200 Subject: [PATCH 4/8] add scripts for exporting and importing the clients --- ykval-export-clients.php | 40 +++++++++++++++++++++++++++ ykval-import-clients.php | 60 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100755 ykval-export-clients.php create mode 100755 ykval-import-clients.php diff --git a/ykval-export-clients.php b/ykval-export-clients.php new file mode 100755 index 0000000..f0b8a2d --- /dev/null +++ b/ykval-export-clients.php @@ -0,0 +1,40 @@ +#!/usr/bin/php +connect()) { + $myLog->log(LOG_WARNING, "Could not connect to database"); + exit(1); + } + +$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'] . + "\n"; + } + +$result=null; +$db=null; + + +?> diff --git a/ykval-import-clients.php b/ykval-import-clients.php new file mode 100755 index 0000000..abc525a --- /dev/null +++ b/ykval-import-clients.php @@ -0,0 +1,60 @@ +#!/usr/bin/php +connect()) { + $myLog->log(LOG_WARNING, "Could not connect to database"); + error_log("Could not connect to database"); + exit(1); + } + + +while ($res=fgetcsv(STDIN, 0, "\t")) { + $params=array("id"=>$res[0], + "active"=>$res[1], + "created"=>$res[2], + "secret"=>$res[3], + "email"=>$res[4], + "notes"=>$res[5], + "otp"=>$res[6]); + + + $query="SELECT * FROM clients WHERE id='" . $params['id'] . "'"; + $result=$db->customQuery($query); + if(!$result->fetch(PDO::FETCH_ASSOC)) { + // 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 " . + "('" . $params["id"] . "', " . + "'" . $params["active"] . "', " . + "'" . $params['created'] . "'," . + "'" . $params['secret'] . "'," . + "'" . $params['email'] . "'," . + "'" . $params['notes'] . "'," . + "'" . $params['otp'] . "')"; + + if(!$db->customQuery($query)){ + $myLog->log(LOG_ERR, "Failed to insert new client with query " . $query); + error_log("Failed to insert new client with query " . $query); + exit(1); + } + } + } + + +$myLog->log(LOG_NOTICE, "Successfully imported clients to database"); +echo "Successfully imported clients to database\n"; From 027dc9ff6cefa084b406c8aff5994bb13f67e2e1 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 23 May 2012 08:56:48 +0200 Subject: [PATCH 5/8] use fgetcsv instead of fscanf, handles empty fields --- ykval-import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ykval-import.php b/ykval-import.php index dca95e7..185ec51 100755 --- a/ykval-import.php +++ b/ykval-import.php @@ -23,7 +23,7 @@ if (!$db->connect()) { } -while ($res=fscanf(STDIN, "%d\t%d\t%d\t%s\t%d\t%d\t%d\t%d\t%s\t%s\t")) { +while ($res=fgetcsv(STDIN, 0, "\t")) { $params=array("active"=>$res[0], "created"=>$res[1], "modified"=>$res[2], From 6a94b396dc2a452fbf8ced36901c24d3ba5a6682 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Thu, 24 May 2012 14:37:01 +0200 Subject: [PATCH 6/8] check if $sl or $timeout is empty, if they are insert default --- ykval-verify.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ykval-verify.php b/ykval-verify.php index ff145d9..8f66ef9 100644 --- a/ykval-verify.php +++ b/ykval-verify.php @@ -81,10 +81,10 @@ if (isset($sl) && strcasecmp($sl, 'fast')==0) { if (isset($sl) && strcasecmp($sl, 'secure')==0) { $sl=$baseParams['__YKVAL_SYNC_SECURE_LEVEL__']; } -if (!isset($sl)) { +if (!isset($sl) || $sl == '') { $sl=$baseParams['__YKVAL_SYNC_DEFAULT_LEVEL__']; } -if (!isset($timeout)) { +if (!isset($timeout) || $timeout == '') { $timeout=$baseParams['__YKVAL_SYNC_DEFAULT_TIMEOUT__']; } From cc687f23b9d551d24f28fdb69816e7410fa1cd7e Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Thu, 24 May 2012 14:40:17 +0200 Subject: [PATCH 7/8] NEWS for 2.15 --- NEWS | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 171edab..65f3cc3 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,9 @@ -* Version 2.15 unreleased +* Version 2.15 released 2012-05-24 + + * Add export/import scripts for clients table. + + * Insert default values in $sl and $timeout if they are empty. + And they will be empty if the client didn't request them. * Version 2.14 released 2012-05-22 From fae1aa4021f3e81954fa745ce4694d91d995b61d Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Thu, 24 May 2012 14:43:25 +0200 Subject: [PATCH 8/8] bump versions --- Makefile | 2 +- NEWS | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a834deb..3994d83 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 2.15 +VERSION = 2.16 PACKAGE = yubikey-val CODE = COPYING Makefile NEWS ykval-checksum-clients.php \ ykval-common.php ykval-config.php ykval-db.php ykval-db.sql \ diff --git a/NEWS b/NEWS index 65f3cc3..172fc7a 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ +* Version 2.16 unreleased + * Version 2.15 released 2012-05-24 * Add export/import scripts for clients table.