4 * @brief Implementation of database_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include <crypto/hashers/hasher.h>
30 typedef struct private_database_t private_database_t
;
33 * private data of database
35 struct private_database_t
{
43 * SQLite database handle
49 * database enumerator implements enumerator_t
52 enumerator_t enumerator
;
57 * destroy a database enumerator
59 static void db_enumerator_destroy(db_enumerator_t
* this)
61 sqlite3_finalize(this->stmt
);
66 * create a database enumerator
68 static enumerator_t
*db_enumerator_create(bool(*enumerate
)(db_enumerator_t
*,void*,...),
71 db_enumerator_t
*this = malloc_thing(db_enumerator_t
);
72 this->enumerator
.enumerate
= (void*)enumerate
;
73 this->enumerator
.destroy
= (void*)db_enumerator_destroy
;
75 return &this->enumerator
;
79 * Implementation of database_t.login.
81 static int login(private_database_t
*this, char *username
, char *password
)
86 size_t username_len
, password_len
;
90 /* hash = SHA1( username | password ) */
91 hasher
= hasher_create(HASH_SHA1
);
92 hash
= chunk_alloca(hasher
->get_hash_size(hasher
));
93 username_len
= strlen(username
);
94 password_len
= strlen(password
);
95 data
= chunk_alloca(username_len
+ password_len
);
96 memcpy(data
.ptr
, username
, username_len
);
97 memcpy(data
.ptr
+ username_len
, password
, password_len
);
98 hasher
->get_hash(hasher
, data
, hash
.ptr
);
99 hasher
->destroy(hasher
);
100 str
= chunk_to_hex(hash
, FALSE
);
102 if (sqlite3_prepare_v2(this->db
,
103 "SELECT oid FROM users WHERE username = ? AND password = ?;",
104 -1, &stmt
, NULL
) == SQLITE_OK
)
106 if (sqlite3_bind_text(stmt
, 1, username
, -1, SQLITE_STATIC
) == SQLITE_OK
&&
107 sqlite3_bind_text(stmt
, 2, str
, -1, SQLITE_STATIC
) == SQLITE_OK
&&
108 sqlite3_step(stmt
) == SQLITE_ROW
)
110 uid
= sqlite3_column_int(stmt
, 0);
112 sqlite3_finalize(stmt
);
119 * enumerate function for gateway enumrator
121 static bool gateway_enumerate(db_enumerator_t
* e
, int *id
, const char **name
,
122 int *port
, const char **address
)
124 if (sqlite3_step(e
->stmt
) == SQLITE_ROW
)
126 *id
= sqlite3_column_int(e
->stmt
, 0);
127 *name
= sqlite3_column_text(e
->stmt
, 1);
128 *port
= sqlite3_column_int(e
->stmt
, 2);
129 *address
= sqlite3_column_text(e
->stmt
, 3);
136 * Implementation of database_t.create_gateway_enumerator.
138 static enumerator_t
* create_gateway_enumerator(private_database_t
*this, int user
)
142 if (sqlite3_prepare_v2(this->db
,
143 "SELECT gateways.oid AS gid, name, port, address FROM "
144 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
145 -1, &stmt
, NULL
) == SQLITE_OK
)
147 if (sqlite3_bind_int(stmt
, 1, user
) == SQLITE_OK
)
149 return db_enumerator_create((void*)gateway_enumerate
, stmt
);
151 sqlite3_finalize(stmt
);
153 return enumerator_create_empty();
157 * Implementation of database_t.destroy
159 static void destroy(private_database_t
*this)
161 sqlite3_close(this->db
);
168 database_t
*database_create(char *dbfile
)
170 private_database_t
*this = malloc_thing(private_database_t
);
172 this->public.login
= (int(*)(database_t
*, char *username
, char *password
))login
;
173 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(database_t
*,int))create_gateway_enumerator
;
174 this->public.destroy
= (void(*)(database_t
*))destroy
;
176 if (sqlite3_open(dbfile
, &this->db
) != SQLITE_OK
)
181 return &this->public;