first revision of new manager webapp
[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*,...),
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, ...)
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, ...)
124 {
125 va_list args;
126 int *id, *port;
127 const char **name, **address;
128
129 va_start(args, e);
130 id = va_arg(args, typeof(id));
131 name = va_arg(args, typeof(name));
132 port = va_arg(args, typeof(port));
133 address = va_arg(args, typeof(address));
134 va_end(args);
135
136 if (sqlite3_step(e->stmt) == SQLITE_ROW)
137 {
138 *id = sqlite3_column_int(e->stmt, 0);
139 *name = sqlite3_column_text(e->stmt, 1);
140 *port = sqlite3_column_int(e->stmt, 2);
141 *address = sqlite3_column_text(e->stmt, 3);
142 return TRUE;
143 }
144 return FALSE;
145 }
146
147 /**
148 * Implementation of database_t.create_gateway_enumerator.
149 */
150 static enumerator_t* create_gateway_enumerator(private_database_t *this, int user)
151 {
152 sqlite3_stmt *stmt;
153
154 if (sqlite3_prepare_v2(this->db,
155 "SELECT gateways.oid AS gid, name, port, address FROM "
156 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
157 -1, &stmt, NULL) == SQLITE_OK)
158 {
159 if (sqlite3_bind_int(stmt, 1, user) == SQLITE_OK)
160 {
161 return db_enumerator_create((void*)gateway_enumerate, stmt);
162 }
163 sqlite3_finalize(stmt);
164 }
165 return empty_enumerator_create();
166 }
167
168 /**
169 * Implementation of database_t.destroy
170 */
171 static void destroy(private_database_t *this)
172 {
173 sqlite3_close(this->db);
174 free(this);
175 }
176
177 /*
178 * see header file
179 */
180 database_t *database_create(char *dbfile)
181 {
182 private_database_t *this = malloc_thing(private_database_t);
183
184 this->public.login = (int(*)(database_t*, char *username, char *password))login;
185 this->public.create_gateway_enumerator = (enumerator_t*(*)(database_t*,int))create_gateway_enumerator;
186 this->public.destroy = (void(*)(database_t*))destroy;
187
188 if (sqlite3_open(dbfile, &this->db) != SQLITE_OK)
189 {
190 destroy(this);
191 return NULL;
192 }
193 return &this->public;
194 }
195