* public functions
*/
storage_t public;
-
+
/**
* database connection
*/
database_t *db;
};
-/**
- * Implementation of storage_t.login.
- */
-static int login(private_storage_t *this, char *username, char *password)
+METHOD(storage_t, login, int,
+ private_storage_t *this, char *username, char *password)
{
hasher_t *hasher;
chunk_t hash, data, hex_str;
size_t username_len, password_len;
int uid = 0;
enumerator_t *enumerator;
-
+
/* hash = SHA1( username | password ) */
hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
if (hasher == NULL)
data = chunk_alloca(username_len + password_len);
memcpy(data.ptr, username, username_len);
memcpy(data.ptr + username_len, password, password_len);
- hasher->get_hash(hasher, data, hash.ptr);
+ if (!hasher->get_hash(hasher, data, hash.ptr))
+ {
+ hasher->destroy(hasher);
+ return 0;
+ }
hasher->destroy(hasher);
hex_str = chunk_to_hex(hash, NULL, FALSE);
-
- enumerator = this->db->query(this->db,
+
+ enumerator = this->db->query(this->db,
"SELECT oid FROM users WHERE username = ? AND password = ?;",
DB_TEXT, username, DB_TEXT, hex_str.ptr,
DB_INT);
return uid;
}
-/**
- * Implementation of storage_t.create_gateway_enumerator.
- */
-static enumerator_t* create_gateway_enumerator(private_storage_t *this, int user)
+METHOD(storage_t, create_gateway_enumerator, enumerator_t*,
+ private_storage_t *this, int user)
{
enumerator_t *enumerator;
-
- enumerator = this->db->query(this->db,
+
+ enumerator = this->db->query(this->db,
"SELECT gateways.oid AS gid, name, port, address FROM "
"gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
DB_INT, user,
return enumerator;
}
-/**
- * Implementation of storage_t.destroy
- */
-static void destroy(private_storage_t *this)
+METHOD(storage_t, destroy, void,
+ private_storage_t *this)
{
this->db->destroy(this->db);
free(this);
*/
storage_t *storage_create(char *uri)
{
- private_storage_t *this = malloc_thing(private_storage_t);
-
- this->public.login = (int(*)(storage_t*, char *username, char *password))login;
- this->public.create_gateway_enumerator = (enumerator_t*(*)(storage_t*,int))create_gateway_enumerator;
- this->public.destroy = (void(*)(storage_t*))destroy;
-
- this->db = lib->db->create(lib->db, uri);
+ private_storage_t *this;
+
+ INIT(this,
+ .public = {
+ .login = _login,
+ .create_gateway_enumerator = _create_gateway_enumerator,
+ .destroy = _destroy,
+ },
+ .db = lib->db->create(lib->db, uri),
+ );
if (this->db == NULL)
{
free(this);