added missing enumerator implementation
[strongswan.git] / src / manager / database.c
1 /**
2 * @file database.c
3 *
4 * @brief Implementation of database_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
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>.
16 *
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
20 * for more details.
21 */
22
23 #include "database.h"
24
25 #include <sqlite3.h>
26 #include <library.h>
27 #include <enumerator.h>
28 #include <crypto/hashers/hasher.h>
29
30
31 typedef struct private_database_t private_database_t;
32
33 /**
34 * private data of database
35 */
36 struct private_database_t {
37
38 /**
39 * public functions
40 */
41 database_t public;
42
43 /**
44 * SQLite database handle
45 */
46 sqlite3 *db;
47 };
48
49 /**
50 * database enumerator implements enumerator_t
51 */
52 typedef struct {
53 enumerator_t enumerator;
54 sqlite3_stmt *stmt;
55 } db_enumerator_t;
56
57 /**
58 * destroy a database enumerator
59 */
60 static void db_enumerator_destroy(db_enumerator_t* this)
61 {
62 sqlite3_finalize(this->stmt);
63 free(this);
64 }
65
66 /**
67 * create a database enumerator
68 */
69 static enumerator_t *db_enumerator_create(bool(*enumerate)(db_enumerator_t*,void*,...),
70 sqlite3_stmt *stmt)
71 {
72 db_enumerator_t *this = malloc_thing(db_enumerator_t);
73 this->enumerator.enumerate = (void*)enumerate;
74 this->enumerator.destroy = (void*)db_enumerator_destroy;
75 this->stmt = stmt;
76 return &this->enumerator;
77 }
78
79 /**
80 * Implementation of database_t.login.
81 */
82 static int login(private_database_t *this, char *username, char *password)
83 {
84 sqlite3_stmt *stmt;
85 hasher_t *hasher;
86 chunk_t hash, data;
87 size_t username_len, password_len;
88 int uid = 0;
89 char *str;
90
91 /* hash = SHA1( username | password ) */
92 hasher = hasher_create(HASH_SHA1);
93 hash = chunk_alloca(hasher->get_hash_size(hasher));
94 username_len = strlen(username);
95 password_len = strlen(password);
96 data = chunk_alloca(username_len + password_len);
97 memcpy(data.ptr, username, username_len);
98 memcpy(data.ptr + username_len, password, password_len);
99 hasher->get_hash(hasher, data, hash.ptr);
100 hasher->destroy(hasher);
101 str = chunk_to_hex(hash, FALSE);
102
103 if (sqlite3_prepare_v2(this->db,
104 "SELECT oid FROM users WHERE username = ? AND password = ?;",
105 -1, &stmt, NULL) == SQLITE_OK)
106 {
107 if (sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC) == SQLITE_OK &&
108 sqlite3_bind_text(stmt, 2, str, -1, SQLITE_STATIC) == SQLITE_OK &&
109 sqlite3_step(stmt) == SQLITE_ROW)
110 {
111 uid = sqlite3_column_int(stmt, 0);
112 }
113 sqlite3_finalize(stmt);
114 }
115 free(str);
116 return uid;
117 }
118
119 /**
120 * enumerate function for gateway enumrator
121 */
122 static bool gateway_enumerate(db_enumerator_t* e, int *id, const char **name,
123 int *port, const char **address)
124 {
125 if (sqlite3_step(e->stmt) == SQLITE_ROW)
126 {
127 *id = sqlite3_column_int(e->stmt, 0);
128 *name = sqlite3_column_text(e->stmt, 1);
129 *port = sqlite3_column_int(e->stmt, 2);
130 *address = sqlite3_column_text(e->stmt, 3);
131 return TRUE;
132 }
133 return FALSE;
134 }
135
136 /**
137 * Implementation of database_t.create_gateway_enumerator.
138 */
139 static enumerator_t* create_gateway_enumerator(private_database_t *this, int user)
140 {
141 sqlite3_stmt *stmt;
142
143 if (sqlite3_prepare_v2(this->db,
144 "SELECT gateways.oid AS gid, name, port, address FROM "
145 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
146 -1, &stmt, NULL) == SQLITE_OK)
147 {
148 if (sqlite3_bind_int(stmt, 1, user) == SQLITE_OK)
149 {
150 return db_enumerator_create((void*)gateway_enumerate, stmt);
151 }
152 sqlite3_finalize(stmt);
153 }
154 return enumerator_create_empty();
155 }
156
157 /**
158 * Implementation of database_t.destroy
159 */
160 static void destroy(private_database_t *this)
161 {
162 sqlite3_close(this->db);
163 free(this);
164 }
165
166 /*
167 * see header file
168 */
169 database_t *database_create(char *dbfile)
170 {
171 private_database_t *this = malloc_thing(private_database_t);
172
173 this->public.login = (int(*)(database_t*, char *username, char *password))login;
174 this->public.create_gateway_enumerator = (enumerator_t*(*)(database_t*,int))create_gateway_enumerator;
175 this->public.destroy = (void(*)(database_t*))destroy;
176
177 if (sqlite3_open(dbfile, &this->db) != SQLITE_OK)
178 {
179 destroy(this);
180 return NULL;
181 }
182 return &this->public;
183 }
184