implemented SHA1 encrypted passwords for manager
[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 * enumerator function for empty enumerator
81 */
82 static bool empty_enumerate(enumerator_t *enumerator, void *item, ...)
83 {
84 return FALSE;
85 }
86
87 /**
88 * create an empty enumerator
89 */
90 static enumerator_t* empty_enumerator_create()
91 {
92 enumerator_t *this = malloc_thing(enumerator_t);
93 this->enumerate = empty_enumerate;
94 this->destroy = (void*)free;
95 return this;
96 }
97
98 /**
99 * Implementation of database_t.login.
100 */
101 static int login(private_database_t *this, char *username, char *password)
102 {
103 sqlite3_stmt *stmt;
104 hasher_t *hasher;
105 chunk_t hash, data;
106 size_t username_len, password_len;
107 int uid = 0;
108 char *str;
109
110 /* hash = SHA1( username | password ) */
111 hasher = hasher_create(HASH_SHA1);
112 hash = chunk_alloca(hasher->get_hash_size(hasher));
113 username_len = strlen(username);
114 password_len = strlen(password);
115 data = chunk_alloca(username_len + password_len);
116 memcpy(data.ptr, username, username_len);
117 memcpy(data.ptr + username_len, password, password_len);
118 hasher->get_hash(hasher, data, hash.ptr);
119 hasher->destroy(hasher);
120 str = chunk_to_hex(hash, FALSE);
121
122 if (sqlite3_prepare_v2(this->db,
123 "SELECT oid FROM users WHERE username = ? AND password = ?;",
124 -1, &stmt, NULL) == SQLITE_OK)
125 {
126 if (sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC) == SQLITE_OK &&
127 sqlite3_bind_text(stmt, 2, str, -1, SQLITE_STATIC) == SQLITE_OK &&
128 sqlite3_step(stmt) == SQLITE_ROW)
129 {
130 uid = sqlite3_column_int(stmt, 0);
131 }
132 sqlite3_finalize(stmt);
133 }
134 free(str);
135 return uid;
136 }
137
138 /**
139 * enumerate function for gateway enumrator
140 */
141 static bool gateway_enumerate(db_enumerator_t* e, int *id, const char **name,
142 int *port, const char **address)
143 {
144 if (sqlite3_step(e->stmt) == SQLITE_ROW)
145 {
146 *id = sqlite3_column_int(e->stmt, 0);
147 *name = sqlite3_column_text(e->stmt, 1);
148 *port = sqlite3_column_int(e->stmt, 2);
149 *address = sqlite3_column_text(e->stmt, 3);
150 return TRUE;
151 }
152 return FALSE;
153 }
154
155 /**
156 * Implementation of database_t.create_gateway_enumerator.
157 */
158 static enumerator_t* create_gateway_enumerator(private_database_t *this, int user)
159 {
160 sqlite3_stmt *stmt;
161
162 if (sqlite3_prepare_v2(this->db,
163 "SELECT gateways.oid AS gid, name, port, address FROM "
164 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
165 -1, &stmt, NULL) == SQLITE_OK)
166 {
167 if (sqlite3_bind_int(stmt, 1, user) == SQLITE_OK)
168 {
169 return db_enumerator_create((void*)gateway_enumerate, stmt);
170 }
171 sqlite3_finalize(stmt);
172 }
173 return empty_enumerator_create();
174 }
175
176 /**
177 * Implementation of database_t.destroy
178 */
179 static void destroy(private_database_t *this)
180 {
181 sqlite3_close(this->db);
182 free(this);
183 }
184
185 /*
186 * see header file
187 */
188 database_t *database_create(char *dbfile)
189 {
190 private_database_t *this = malloc_thing(private_database_t);
191
192 this->public.login = (int(*)(database_t*, char *username, char *password))login;
193 this->public.create_gateway_enumerator = (enumerator_t*(*)(database_t*,int))create_gateway_enumerator;
194 this->public.destroy = (void(*)(database_t*))destroy;
195
196 if (sqlite3_open(dbfile, &this->db) != SQLITE_OK)
197 {
198 destroy(this);
199 return NULL;
200 }
201 return &this->public;
202 }
203