added connection name to IKE_SA title
[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
29
30 typedef struct private_database_t private_database_t;
31
32 /**
33 * private data of database
34 */
35 struct private_database_t {
36
37 /**
38 * public functions
39 */
40 database_t public;
41
42 /**
43 * SQLite database handle
44 */
45 sqlite3 *db;
46 };
47
48 /**
49 * database enumerator implements enumerator_t
50 */
51 typedef struct {
52 enumerator_t enumerator;
53 sqlite3_stmt *stmt;
54 } db_enumerator_t;
55
56 /**
57 * destroy a database enumerator
58 */
59 static void db_enumerator_destroy(db_enumerator_t* this)
60 {
61 sqlite3_finalize(this->stmt);
62 free(this);
63 }
64
65 /**
66 * create a database enumerator
67 */
68 static enumerator_t *db_enumerator_create(bool(*enumerate)(db_enumerator_t*,void*,...),
69 sqlite3_stmt *stmt)
70 {
71 db_enumerator_t *this = malloc_thing(db_enumerator_t);
72 this->enumerator.enumerate = (void*)enumerate;
73 this->enumerator.destroy = (void*)db_enumerator_destroy;
74 this->stmt = stmt;
75 return &this->enumerator;
76 }
77
78 /**
79 * enumerator function for empty enumerator
80 */
81 static bool empty_enumerate(enumerator_t *enumerator, void *item, ...)
82 {
83 return FALSE;
84 }
85
86 /**
87 * create an empty enumerator
88 */
89 static enumerator_t* empty_enumerator_create()
90 {
91 enumerator_t *this = malloc_thing(enumerator_t);
92 this->enumerate = empty_enumerate;
93 this->destroy = (void*)free;
94 return this;
95 }
96
97 /**
98 * Implementation of database_t.login.
99 */
100 static int login(private_database_t *this, char *username, char *password)
101 {
102 sqlite3_stmt *stmt;
103 int uid = 0;
104
105 if (sqlite3_prepare_v2(this->db,
106 "SELECT oid FROM users WHERE username = ? AND password = ?;",
107 -1, &stmt, NULL) == SQLITE_OK)
108 {
109 if (sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC) == SQLITE_OK &&
110 sqlite3_bind_text(stmt, 2, password, -1, SQLITE_STATIC) == SQLITE_OK &&
111 sqlite3_step(stmt) == SQLITE_ROW)
112 {
113 uid = sqlite3_column_int(stmt, 0);
114 }
115 sqlite3_finalize(stmt);
116 }
117 return uid;
118 }
119
120 /**
121 * enumerate function for gateway enumrator
122 */
123 static bool gateway_enumerate(db_enumerator_t* e, int *id, const char **name,
124 int *port, const char **address)
125 {
126 if (sqlite3_step(e->stmt) == SQLITE_ROW)
127 {
128 *id = sqlite3_column_int(e->stmt, 0);
129 *name = sqlite3_column_text(e->stmt, 1);
130 *port = sqlite3_column_int(e->stmt, 2);
131 *address = sqlite3_column_text(e->stmt, 3);
132 return TRUE;
133 }
134 return FALSE;
135 }
136
137 /**
138 * Implementation of database_t.create_gateway_enumerator.
139 */
140 static enumerator_t* create_gateway_enumerator(private_database_t *this, int user)
141 {
142 sqlite3_stmt *stmt;
143
144 if (sqlite3_prepare_v2(this->db,
145 "SELECT gateways.oid AS gid, name, port, address FROM "
146 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
147 -1, &stmt, NULL) == SQLITE_OK)
148 {
149 if (sqlite3_bind_int(stmt, 1, user) == SQLITE_OK)
150 {
151 return db_enumerator_create((void*)gateway_enumerate, stmt);
152 }
153 sqlite3_finalize(stmt);
154 }
155 return empty_enumerator_create();
156 }
157
158 /**
159 * Implementation of database_t.destroy
160 */
161 static void destroy(private_database_t *this)
162 {
163 sqlite3_close(this->db);
164 free(this);
165 }
166
167 /*
168 * see header file
169 */
170 database_t *database_create(char *dbfile)
171 {
172 private_database_t *this = malloc_thing(private_database_t);
173
174 this->public.login = (int(*)(database_t*, char *username, char *password))login;
175 this->public.create_gateway_enumerator = (enumerator_t*(*)(database_t*,int))create_gateway_enumerator;
176 this->public.destroy = (void(*)(database_t*))destroy;
177
178 if (sqlite3_open(dbfile, &this->db) != SQLITE_OK)
179 {
180 destroy(this);
181 return NULL;
182 }
183 return &this->public;
184 }
185