DbIf uses SaltStore
authorOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 03:45:33 +0000 (22:45 -0500)
committerOwen Smith <owen@omsmith.ca>
Tue, 20 Nov 2012 03:45:33 +0000 (22:45 -0500)
database/db_if.cpp
database/db_if.h

index 4790899..e883b62 100644 (file)
@@ -15,9 +15,11 @@ DbIf::DbIf(int argc, char ** argv) {
        std::string db_file;
        try {
                ConfigFile conf(argv[1]);
-               salt_size = conf.read<size_t>("salt_size", 256);
-               key_lifetime = conf.read<uint64_t>("key_lifetime", 120);
-               secret = conf.read<std::string>("secret");
+               saltStore = new SaltStore(
+                       conf.read<uint64_t>("key_lifetime", 120),
+                       conf.read<size_t>("salt_size", 256),
+                       conf.read<std::string>("secret")
+                       );
                db_file = conf.read<std::string>("db_file");
        } catch(ConfigFile::file_not_found & e) {
                fprintf(stderr, "Could not open configuration file '%s'!\n", e.filename.c_str());
@@ -32,59 +34,17 @@ DbIf::DbIf(int argc, char ** argv) {
 
 DbIf::~DbIf() {
        delete db;
+       delete saltStore;
 }
 
 std::string DbIf::getSalt() {
-       expireKeys();
-       char bfr[salt_size];
-       FILE * rand_in = fopen("/dev/urandom", "r");
-       if(!rand_in) {
-               fprintf(stderr, "Could not open /dev/urandom!\n");
-               return "";
-       }
-       if(salt_size != fread(bfr, sizeof(char), salt_size, rand_in)) {
-               fclose(rand_in);
-               fprintf(stderr, "Could not read from /dev/urandom!\n");
-               return "";
-       }
-       fclose(rand_in);
-
-       // zero-terminated strings suck
-       for(unsigned int i=0;i<salt_size;++i)
-               if(bfr[i] == 0)
-                       bfr[i] = 1;
-       bfr[salt_size-1] = 0;
-
-       std::string salt(bfr);
-
-       valid_keys.push_back(std::pair<uint64_t, SHA1Hash>((uint64_t)time(NULL), SHA1Hash(salt + secret)));
-       return salt;
+       return saltStore->getSalt();
 }
 
 bool DbIf::consumeKey(std::string key) {
-       expireKeys();
-       SHA1Hash h;
-       h.fromHex(key);
-       std::list<std::pair<uint64_t, SHA1Hash> >::iterator p;
-       for(p = valid_keys.begin();p != valid_keys.end(); ++p) {
-               if(p->second == h) {
-                       valid_keys.erase(p);
-                       return true;
-               }
-       }
-       return false;
+       return saltStore->consumeKey(key);
 }
 
 PosDb & DbIf::getDb() {
        return *db;
 }
-
-void DbIf::expireKeys() {
-       std::list<std::pair<uint64_t, SHA1Hash> >::iterator p;
-       uint64_t cur_t = (uint64_t)time(NULL);
-       for(p = valid_keys.begin();p != valid_keys.end();++p) {
-               if((cur_t - p->first) > key_lifetime)
-                       p = valid_keys.erase(p);
-       }
-}
-
index 09aeecb..0cf32e3 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef _DB_IF_H_
 #define _DB_IF_H_
 #include "posdb.h"
+#include "salt_store.h"
 #include "sha1.h"
 #include <string>
 #include <list>
@@ -15,13 +16,9 @@ public:
        PosDb & getDb();
 
 private:
-       void expireKeys();
        PosDb * db;
-       std::list<std::pair<uint64_t, SHA1Hash> > valid_keys;
-       uint64_t key_lifetime;
-       size_t salt_size;
-       std::string secret;
-       
+       SaltStore * saltStore;
+
 };
 
 #endif