1*0a6a1f1dSLionel Sambuc /* $NetBSD: hdb-sqlite.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 2009 Kungliga Tekniska H�gskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "hdb_locl.h"
37ebfedea0SLionel Sambuc #include "sqlite3.h"
38ebfedea0SLionel Sambuc
39ebfedea0SLionel Sambuc #define MAX_RETRIES 10
40ebfedea0SLionel Sambuc
41ebfedea0SLionel Sambuc typedef struct hdb_sqlite_db {
42ebfedea0SLionel Sambuc double version;
43ebfedea0SLionel Sambuc sqlite3 *db;
44ebfedea0SLionel Sambuc char *db_file;
45ebfedea0SLionel Sambuc
46ebfedea0SLionel Sambuc sqlite3_stmt *get_version;
47ebfedea0SLionel Sambuc sqlite3_stmt *fetch;
48ebfedea0SLionel Sambuc sqlite3_stmt *get_ids;
49ebfedea0SLionel Sambuc sqlite3_stmt *add_entry;
50ebfedea0SLionel Sambuc sqlite3_stmt *add_principal;
51ebfedea0SLionel Sambuc sqlite3_stmt *add_alias;
52ebfedea0SLionel Sambuc sqlite3_stmt *delete_aliases;
53ebfedea0SLionel Sambuc sqlite3_stmt *update_entry;
54ebfedea0SLionel Sambuc sqlite3_stmt *remove;
55ebfedea0SLionel Sambuc sqlite3_stmt *get_all_entries;
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc } hdb_sqlite_db;
58ebfedea0SLionel Sambuc
59ebfedea0SLionel Sambuc /* This should be used to mark updates which make the code incompatible
60ebfedea0SLionel Sambuc * with databases created with previous versions. Don't update it if
61ebfedea0SLionel Sambuc * compatibility is not broken. */
62ebfedea0SLionel Sambuc #define HDBSQLITE_VERSION 0.1
63ebfedea0SLionel Sambuc
64ebfedea0SLionel Sambuc #define _HDBSQLITE_STRINGIFY(x) #x
65ebfedea0SLionel Sambuc #define HDBSQLITE_STRINGIFY(x) _HDBSQLITE_STRINGIFY(x)
66ebfedea0SLionel Sambuc
67ebfedea0SLionel Sambuc #define HDBSQLITE_CREATE_TABLES \
68ebfedea0SLionel Sambuc " BEGIN TRANSACTION;" \
69ebfedea0SLionel Sambuc " CREATE TABLE Version (number REAL);" \
70ebfedea0SLionel Sambuc " INSERT INTO Version (number)" \
71ebfedea0SLionel Sambuc " VALUES (" HDBSQLITE_STRINGIFY(HDBSQLITE_VERSION) ");" \
72ebfedea0SLionel Sambuc " CREATE TABLE Principal" \
73ebfedea0SLionel Sambuc " (id INTEGER PRIMARY KEY," \
74ebfedea0SLionel Sambuc " principal TEXT UNIQUE NOT NULL," \
75ebfedea0SLionel Sambuc " canonical INTEGER," \
76ebfedea0SLionel Sambuc " entry INTEGER);" \
77ebfedea0SLionel Sambuc " CREATE TABLE Entry" \
78ebfedea0SLionel Sambuc " (id INTEGER PRIMARY KEY," \
79ebfedea0SLionel Sambuc " data BLOB);" \
80ebfedea0SLionel Sambuc " COMMIT"
81ebfedea0SLionel Sambuc #define HDBSQLITE_CREATE_TRIGGERS \
82ebfedea0SLionel Sambuc " CREATE TRIGGER remove_principals AFTER DELETE ON Entry" \
83ebfedea0SLionel Sambuc " BEGIN" \
84ebfedea0SLionel Sambuc " DELETE FROM Principal" \
85ebfedea0SLionel Sambuc " WHERE entry = OLD.id;" \
86ebfedea0SLionel Sambuc " END"
87ebfedea0SLionel Sambuc #define HDBSQLITE_GET_VERSION \
88ebfedea0SLionel Sambuc " SELECT number FROM Version"
89ebfedea0SLionel Sambuc #define HDBSQLITE_FETCH \
90ebfedea0SLionel Sambuc " SELECT Entry.data FROM Principal, Entry" \
91ebfedea0SLionel Sambuc " WHERE Principal.principal = ? AND" \
92ebfedea0SLionel Sambuc " Entry.id = Principal.entry"
93ebfedea0SLionel Sambuc #define HDBSQLITE_GET_IDS \
94ebfedea0SLionel Sambuc " SELECT id, entry FROM Principal" \
95ebfedea0SLionel Sambuc " WHERE principal = ?"
96ebfedea0SLionel Sambuc #define HDBSQLITE_ADD_ENTRY \
97ebfedea0SLionel Sambuc " INSERT INTO Entry (data) VALUES (?)"
98ebfedea0SLionel Sambuc #define HDBSQLITE_ADD_PRINCIPAL \
99ebfedea0SLionel Sambuc " INSERT INTO Principal (principal, entry, canonical)" \
100ebfedea0SLionel Sambuc " VALUES (?, last_insert_rowid(), 1)"
101ebfedea0SLionel Sambuc #define HDBSQLITE_ADD_ALIAS \
102ebfedea0SLionel Sambuc " INSERT INTO Principal (principal, entry, canonical)" \
103ebfedea0SLionel Sambuc " VALUES(?, ?, 0)"
104ebfedea0SLionel Sambuc #define HDBSQLITE_DELETE_ALIASES \
105ebfedea0SLionel Sambuc " DELETE FROM Principal" \
106ebfedea0SLionel Sambuc " WHERE entry = ? AND canonical = 0"
107ebfedea0SLionel Sambuc #define HDBSQLITE_UPDATE_ENTRY \
108ebfedea0SLionel Sambuc " UPDATE Entry SET data = ?" \
109ebfedea0SLionel Sambuc " WHERE id = ?"
110ebfedea0SLionel Sambuc #define HDBSQLITE_REMOVE \
111ebfedea0SLionel Sambuc " DELETE FROM ENTRY WHERE id = " \
112ebfedea0SLionel Sambuc " (SELECT entry FROM Principal" \
113ebfedea0SLionel Sambuc " WHERE principal = ?)"
114ebfedea0SLionel Sambuc #define HDBSQLITE_GET_ALL_ENTRIES \
115ebfedea0SLionel Sambuc " SELECT data FROM Entry"
116ebfedea0SLionel Sambuc
117ebfedea0SLionel Sambuc /**
118ebfedea0SLionel Sambuc * Wrapper around sqlite3_prepare_v2.
119ebfedea0SLionel Sambuc *
120ebfedea0SLionel Sambuc * @param context The current krb5 context
121ebfedea0SLionel Sambuc * @param statement Where to store the pointer to the statement
122ebfedea0SLionel Sambuc * after preparing it
123ebfedea0SLionel Sambuc * @param str SQL code for the statement
124ebfedea0SLionel Sambuc *
125ebfedea0SLionel Sambuc * @return 0 if OK, an error code if not
126ebfedea0SLionel Sambuc */
127ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_prepare_stmt(krb5_context context,sqlite3 * db,sqlite3_stmt ** statement,const char * str)128ebfedea0SLionel Sambuc hdb_sqlite_prepare_stmt(krb5_context context,
129ebfedea0SLionel Sambuc sqlite3 *db,
130ebfedea0SLionel Sambuc sqlite3_stmt **statement,
131ebfedea0SLionel Sambuc const char *str)
132ebfedea0SLionel Sambuc {
133ebfedea0SLionel Sambuc int ret, tries = 0;
134ebfedea0SLionel Sambuc
135ebfedea0SLionel Sambuc ret = sqlite3_prepare_v2(db, str, -1, statement, NULL);
136ebfedea0SLionel Sambuc while((tries++ < MAX_RETRIES) &&
137ebfedea0SLionel Sambuc ((ret == SQLITE_BUSY) ||
138ebfedea0SLionel Sambuc (ret == SQLITE_IOERR_BLOCKED) ||
139ebfedea0SLionel Sambuc (ret == SQLITE_LOCKED))) {
140ebfedea0SLionel Sambuc krb5_warnx(context, "hdb-sqlite: prepare busy");
141ebfedea0SLionel Sambuc sleep(1);
142ebfedea0SLionel Sambuc ret = sqlite3_prepare_v2(db, str, -1, statement, NULL);
143ebfedea0SLionel Sambuc }
144ebfedea0SLionel Sambuc
145ebfedea0SLionel Sambuc if (ret != SQLITE_OK) {
146ebfedea0SLionel Sambuc krb5_set_error_message(context, EINVAL,
147ebfedea0SLionel Sambuc "Failed to prepare stmt %s: %s",
148ebfedea0SLionel Sambuc str, sqlite3_errmsg(db));
149ebfedea0SLionel Sambuc return EINVAL;
150ebfedea0SLionel Sambuc }
151ebfedea0SLionel Sambuc
152ebfedea0SLionel Sambuc return 0;
153ebfedea0SLionel Sambuc }
154ebfedea0SLionel Sambuc
155ebfedea0SLionel Sambuc /**
156ebfedea0SLionel Sambuc * A wrapper around sqlite3_exec.
157ebfedea0SLionel Sambuc *
158ebfedea0SLionel Sambuc * @param context The current krb5 context
159ebfedea0SLionel Sambuc * @param database An open sqlite3 database handle
160ebfedea0SLionel Sambuc * @param statement SQL code to execute
161ebfedea0SLionel Sambuc * @param error_code What to return if the statement fails
162ebfedea0SLionel Sambuc *
163ebfedea0SLionel Sambuc * @return 0 if OK, else error_code
164ebfedea0SLionel Sambuc */
165ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_exec_stmt(krb5_context context,sqlite3 * database,const char * statement,krb5_error_code error_code)166ebfedea0SLionel Sambuc hdb_sqlite_exec_stmt(krb5_context context,
167ebfedea0SLionel Sambuc sqlite3 *database,
168ebfedea0SLionel Sambuc const char *statement,
169ebfedea0SLionel Sambuc krb5_error_code error_code)
170ebfedea0SLionel Sambuc {
171ebfedea0SLionel Sambuc int ret;
172ebfedea0SLionel Sambuc
173ebfedea0SLionel Sambuc ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
174ebfedea0SLionel Sambuc
175ebfedea0SLionel Sambuc while(((ret == SQLITE_BUSY) ||
176ebfedea0SLionel Sambuc (ret == SQLITE_IOERR_BLOCKED) ||
177ebfedea0SLionel Sambuc (ret == SQLITE_LOCKED))) {
178ebfedea0SLionel Sambuc krb5_warnx(context, "hdb-sqlite: exec busy: %d", (int)getpid());
179ebfedea0SLionel Sambuc sleep(1);
180ebfedea0SLionel Sambuc ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
181ebfedea0SLionel Sambuc }
182ebfedea0SLionel Sambuc
183ebfedea0SLionel Sambuc if (ret != SQLITE_OK && error_code) {
184ebfedea0SLionel Sambuc krb5_set_error_message(context, error_code,
185ebfedea0SLionel Sambuc "Execute %s: %s", statement,
186ebfedea0SLionel Sambuc sqlite3_errmsg(database));
187ebfedea0SLionel Sambuc return error_code;
188ebfedea0SLionel Sambuc }
189ebfedea0SLionel Sambuc
190ebfedea0SLionel Sambuc return 0;
191ebfedea0SLionel Sambuc }
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc /**
194ebfedea0SLionel Sambuc * Opens an sqlite3 database handle to a file, may create the
195ebfedea0SLionel Sambuc * database file depending on flags.
196ebfedea0SLionel Sambuc *
197ebfedea0SLionel Sambuc * @param context The current krb5 context
198ebfedea0SLionel Sambuc * @param db Heimdal database handle
199ebfedea0SLionel Sambuc * @param flags Controls whether or not the file may be created,
200ebfedea0SLionel Sambuc * may be 0 or SQLITE_OPEN_CREATE
201ebfedea0SLionel Sambuc */
202ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_open_database(krb5_context context,HDB * db,int flags)203ebfedea0SLionel Sambuc hdb_sqlite_open_database(krb5_context context, HDB *db, int flags)
204ebfedea0SLionel Sambuc {
205ebfedea0SLionel Sambuc int ret;
206ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db*) db->hdb_db;
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc ret = sqlite3_open_v2(hsdb->db_file, &hsdb->db,
209ebfedea0SLionel Sambuc SQLITE_OPEN_READWRITE | flags, NULL);
210ebfedea0SLionel Sambuc
211ebfedea0SLionel Sambuc if (ret) {
212ebfedea0SLionel Sambuc if (hsdb->db) {
213ebfedea0SLionel Sambuc ret = ENOENT;
214ebfedea0SLionel Sambuc krb5_set_error_message(context, ret,
215ebfedea0SLionel Sambuc "Error opening sqlite database %s: %s",
216ebfedea0SLionel Sambuc hsdb->db_file, sqlite3_errmsg(hsdb->db));
217ebfedea0SLionel Sambuc sqlite3_close(hsdb->db);
218ebfedea0SLionel Sambuc hsdb->db = NULL;
219ebfedea0SLionel Sambuc } else
220ebfedea0SLionel Sambuc ret = krb5_enomem(context);
221ebfedea0SLionel Sambuc return ret;
222ebfedea0SLionel Sambuc }
223ebfedea0SLionel Sambuc
224ebfedea0SLionel Sambuc return 0;
225ebfedea0SLionel Sambuc }
226ebfedea0SLionel Sambuc
227ebfedea0SLionel Sambuc static int
hdb_sqlite_step(krb5_context context,sqlite3 * db,sqlite3_stmt * stmt)228ebfedea0SLionel Sambuc hdb_sqlite_step(krb5_context context, sqlite3 *db, sqlite3_stmt *stmt)
229ebfedea0SLionel Sambuc {
230ebfedea0SLionel Sambuc int ret;
231ebfedea0SLionel Sambuc
232ebfedea0SLionel Sambuc ret = sqlite3_step(stmt);
233ebfedea0SLionel Sambuc while(((ret == SQLITE_BUSY) ||
234ebfedea0SLionel Sambuc (ret == SQLITE_IOERR_BLOCKED) ||
235ebfedea0SLionel Sambuc (ret == SQLITE_LOCKED))) {
236ebfedea0SLionel Sambuc krb5_warnx(context, "hdb-sqlite: step busy: %d", (int)getpid());
237ebfedea0SLionel Sambuc sleep(1);
238ebfedea0SLionel Sambuc ret = sqlite3_step(stmt);
239ebfedea0SLionel Sambuc }
240ebfedea0SLionel Sambuc return ret;
241ebfedea0SLionel Sambuc }
242ebfedea0SLionel Sambuc
243ebfedea0SLionel Sambuc /**
244ebfedea0SLionel Sambuc * Closes the database and frees memory allocated for statements.
245ebfedea0SLionel Sambuc *
246ebfedea0SLionel Sambuc * @param context The current krb5 context
247ebfedea0SLionel Sambuc * @param db Heimdal database handle
248ebfedea0SLionel Sambuc */
249ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_close_database(krb5_context context,HDB * db)250ebfedea0SLionel Sambuc hdb_sqlite_close_database(krb5_context context, HDB *db)
251ebfedea0SLionel Sambuc {
252ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
253ebfedea0SLionel Sambuc
254ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->get_version);
255ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->fetch);
256ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->get_ids);
257ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->add_entry);
258ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->add_principal);
259ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->add_alias);
260ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->delete_aliases);
261ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->update_entry);
262ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->remove);
263ebfedea0SLionel Sambuc sqlite3_finalize(hsdb->get_all_entries);
264ebfedea0SLionel Sambuc
265ebfedea0SLionel Sambuc sqlite3_close(hsdb->db);
266ebfedea0SLionel Sambuc
267ebfedea0SLionel Sambuc return 0;
268ebfedea0SLionel Sambuc }
269ebfedea0SLionel Sambuc
270ebfedea0SLionel Sambuc /**
271ebfedea0SLionel Sambuc * Opens an sqlite database file and prepares it for use.
272ebfedea0SLionel Sambuc * If the file does not exist it will be created.
273ebfedea0SLionel Sambuc *
274ebfedea0SLionel Sambuc * @param context The current krb5_context
275ebfedea0SLionel Sambuc * @param db The heimdal database handle
276ebfedea0SLionel Sambuc * @param filename Where to store the database file
277ebfedea0SLionel Sambuc *
278ebfedea0SLionel Sambuc * @return 0 if everything worked, an error code if not
279ebfedea0SLionel Sambuc */
280ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_make_database(krb5_context context,HDB * db,const char * filename)281ebfedea0SLionel Sambuc hdb_sqlite_make_database(krb5_context context, HDB *db, const char *filename)
282ebfedea0SLionel Sambuc {
283ebfedea0SLionel Sambuc int ret;
284ebfedea0SLionel Sambuc int created_file = 0;
285ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
286ebfedea0SLionel Sambuc
287ebfedea0SLionel Sambuc hsdb->db_file = strdup(filename);
288ebfedea0SLionel Sambuc if(hsdb->db_file == NULL)
289ebfedea0SLionel Sambuc return ENOMEM;
290ebfedea0SLionel Sambuc
291ebfedea0SLionel Sambuc ret = hdb_sqlite_open_database(context, db, 0);
292ebfedea0SLionel Sambuc if (ret) {
293ebfedea0SLionel Sambuc ret = hdb_sqlite_open_database(context, db, SQLITE_OPEN_CREATE);
294ebfedea0SLionel Sambuc if (ret) goto out;
295ebfedea0SLionel Sambuc
296ebfedea0SLionel Sambuc created_file = 1;
297ebfedea0SLionel Sambuc
298ebfedea0SLionel Sambuc ret = hdb_sqlite_exec_stmt(context, hsdb->db,
299ebfedea0SLionel Sambuc HDBSQLITE_CREATE_TABLES,
300ebfedea0SLionel Sambuc EINVAL);
301ebfedea0SLionel Sambuc if (ret) goto out;
302ebfedea0SLionel Sambuc
303ebfedea0SLionel Sambuc ret = hdb_sqlite_exec_stmt(context, hsdb->db,
304ebfedea0SLionel Sambuc HDBSQLITE_CREATE_TRIGGERS,
305ebfedea0SLionel Sambuc EINVAL);
306ebfedea0SLionel Sambuc if (ret) goto out;
307ebfedea0SLionel Sambuc }
308ebfedea0SLionel Sambuc
309ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
310ebfedea0SLionel Sambuc &hsdb->get_version,
311ebfedea0SLionel Sambuc HDBSQLITE_GET_VERSION);
312ebfedea0SLionel Sambuc if (ret) goto out;
313ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
314ebfedea0SLionel Sambuc &hsdb->fetch,
315ebfedea0SLionel Sambuc HDBSQLITE_FETCH);
316ebfedea0SLionel Sambuc if (ret) goto out;
317ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
318ebfedea0SLionel Sambuc &hsdb->get_ids,
319ebfedea0SLionel Sambuc HDBSQLITE_GET_IDS);
320ebfedea0SLionel Sambuc if (ret) goto out;
321ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
322ebfedea0SLionel Sambuc &hsdb->add_entry,
323ebfedea0SLionel Sambuc HDBSQLITE_ADD_ENTRY);
324ebfedea0SLionel Sambuc if (ret) goto out;
325ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
326ebfedea0SLionel Sambuc &hsdb->add_principal,
327ebfedea0SLionel Sambuc HDBSQLITE_ADD_PRINCIPAL);
328ebfedea0SLionel Sambuc if (ret) goto out;
329ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
330ebfedea0SLionel Sambuc &hsdb->add_alias,
331ebfedea0SLionel Sambuc HDBSQLITE_ADD_ALIAS);
332ebfedea0SLionel Sambuc if (ret) goto out;
333ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
334ebfedea0SLionel Sambuc &hsdb->delete_aliases,
335ebfedea0SLionel Sambuc HDBSQLITE_DELETE_ALIASES);
336ebfedea0SLionel Sambuc if (ret) goto out;
337ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
338ebfedea0SLionel Sambuc &hsdb->update_entry,
339ebfedea0SLionel Sambuc HDBSQLITE_UPDATE_ENTRY);
340ebfedea0SLionel Sambuc if (ret) goto out;
341ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
342ebfedea0SLionel Sambuc &hsdb->remove,
343ebfedea0SLionel Sambuc HDBSQLITE_REMOVE);
344ebfedea0SLionel Sambuc if (ret) goto out;
345ebfedea0SLionel Sambuc ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
346ebfedea0SLionel Sambuc &hsdb->get_all_entries,
347ebfedea0SLionel Sambuc HDBSQLITE_GET_ALL_ENTRIES);
348ebfedea0SLionel Sambuc if (ret) goto out;
349ebfedea0SLionel Sambuc
350ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, hsdb->get_version);
351ebfedea0SLionel Sambuc if(ret == SQLITE_ROW) {
352ebfedea0SLionel Sambuc hsdb->version = sqlite3_column_double(hsdb->get_version, 0);
353ebfedea0SLionel Sambuc }
354ebfedea0SLionel Sambuc sqlite3_reset(hsdb->get_version);
355ebfedea0SLionel Sambuc ret = 0;
356ebfedea0SLionel Sambuc
357ebfedea0SLionel Sambuc if(hsdb->version != HDBSQLITE_VERSION) {
358ebfedea0SLionel Sambuc ret = EINVAL;
359ebfedea0SLionel Sambuc krb5_set_error_message(context, ret, "HDBSQLITE_VERSION mismatch");
360ebfedea0SLionel Sambuc }
361ebfedea0SLionel Sambuc
362ebfedea0SLionel Sambuc if(ret) goto out;
363ebfedea0SLionel Sambuc
364ebfedea0SLionel Sambuc return 0;
365ebfedea0SLionel Sambuc
366ebfedea0SLionel Sambuc out:
367ebfedea0SLionel Sambuc if (hsdb->db)
368ebfedea0SLionel Sambuc sqlite3_close(hsdb->db);
369ebfedea0SLionel Sambuc if (created_file)
370ebfedea0SLionel Sambuc unlink(hsdb->db_file);
371ebfedea0SLionel Sambuc
372ebfedea0SLionel Sambuc return ret;
373ebfedea0SLionel Sambuc }
374ebfedea0SLionel Sambuc
375ebfedea0SLionel Sambuc /**
376ebfedea0SLionel Sambuc * Retrieves an entry by searching for the given
377ebfedea0SLionel Sambuc * principal in the Principal database table, both
378ebfedea0SLionel Sambuc * for canonical principals and aliases.
379ebfedea0SLionel Sambuc *
380ebfedea0SLionel Sambuc * @param context The current krb5_context
381ebfedea0SLionel Sambuc * @param db Heimdal database handle
382ebfedea0SLionel Sambuc * @param principal The principal whose entry to search for
383ebfedea0SLionel Sambuc * @param flags Currently only for HDB_F_DECRYPT
384ebfedea0SLionel Sambuc * @param kvno kvno to fetch is HDB_F_KVNO_SPECIFIED use used
385ebfedea0SLionel Sambuc *
386ebfedea0SLionel Sambuc * @return 0 if everything worked, an error code if not
387ebfedea0SLionel Sambuc */
388ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_fetch_kvno(krb5_context context,HDB * db,krb5_const_principal principal,unsigned flags,krb5_kvno kvno,hdb_entry_ex * entry)389ebfedea0SLionel Sambuc hdb_sqlite_fetch_kvno(krb5_context context, HDB *db, krb5_const_principal principal,
390ebfedea0SLionel Sambuc unsigned flags, krb5_kvno kvno, hdb_entry_ex *entry)
391ebfedea0SLionel Sambuc {
392ebfedea0SLionel Sambuc int sqlite_error;
393ebfedea0SLionel Sambuc krb5_error_code ret;
394ebfedea0SLionel Sambuc char *principal_string;
395ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
396ebfedea0SLionel Sambuc sqlite3_stmt *fetch = hsdb->fetch;
397ebfedea0SLionel Sambuc krb5_data value;
398ebfedea0SLionel Sambuc
399ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, principal, &principal_string);
400ebfedea0SLionel Sambuc if (ret) {
401ebfedea0SLionel Sambuc free(principal_string);
402ebfedea0SLionel Sambuc return ret;
403ebfedea0SLionel Sambuc }
404ebfedea0SLionel Sambuc
405ebfedea0SLionel Sambuc sqlite3_bind_text(fetch, 1, principal_string, -1, SQLITE_STATIC);
406ebfedea0SLionel Sambuc
407ebfedea0SLionel Sambuc sqlite_error = hdb_sqlite_step(context, hsdb->db, fetch);
408ebfedea0SLionel Sambuc if (sqlite_error != SQLITE_ROW) {
409ebfedea0SLionel Sambuc if(sqlite_error == SQLITE_DONE) {
410ebfedea0SLionel Sambuc ret = HDB_ERR_NOENTRY;
411ebfedea0SLionel Sambuc goto out;
412ebfedea0SLionel Sambuc } else {
413ebfedea0SLionel Sambuc ret = EINVAL;
414ebfedea0SLionel Sambuc krb5_set_error_message(context, ret,
415ebfedea0SLionel Sambuc "sqlite fetch failed: %d",
416ebfedea0SLionel Sambuc sqlite_error);
417ebfedea0SLionel Sambuc goto out;
418ebfedea0SLionel Sambuc }
419ebfedea0SLionel Sambuc }
420ebfedea0SLionel Sambuc
421*0a6a1f1dSLionel Sambuc value.length = sqlite3_column_bytes(fetch, 0);
422*0a6a1f1dSLionel Sambuc value.data = (void *) sqlite3_column_blob(fetch, 0);
423*0a6a1f1dSLionel Sambuc
424*0a6a1f1dSLionel Sambuc ret = hdb_value2entry(context, &value, &entry->entry);
425*0a6a1f1dSLionel Sambuc if(ret)
426*0a6a1f1dSLionel Sambuc goto out;
427*0a6a1f1dSLionel Sambuc
428ebfedea0SLionel Sambuc if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
429ebfedea0SLionel Sambuc ret = hdb_unseal_keys(context, db, &entry->entry);
430ebfedea0SLionel Sambuc if(ret) {
431ebfedea0SLionel Sambuc hdb_free_entry(context, entry);
432ebfedea0SLionel Sambuc goto out;
433ebfedea0SLionel Sambuc }
434ebfedea0SLionel Sambuc }
435ebfedea0SLionel Sambuc
436ebfedea0SLionel Sambuc ret = 0;
437ebfedea0SLionel Sambuc
438ebfedea0SLionel Sambuc out:
439ebfedea0SLionel Sambuc
440ebfedea0SLionel Sambuc sqlite3_clear_bindings(fetch);
441ebfedea0SLionel Sambuc sqlite3_reset(fetch);
442ebfedea0SLionel Sambuc
443ebfedea0SLionel Sambuc free(principal_string);
444ebfedea0SLionel Sambuc
445ebfedea0SLionel Sambuc return ret;
446ebfedea0SLionel Sambuc }
447ebfedea0SLionel Sambuc
448ebfedea0SLionel Sambuc /**
449ebfedea0SLionel Sambuc * Convenience function to step a prepared statement with no
450ebfedea0SLionel Sambuc * value once.
451ebfedea0SLionel Sambuc *
452ebfedea0SLionel Sambuc * @param context The current krb5_context
453ebfedea0SLionel Sambuc * @param statement A prepared sqlite3 statement
454ebfedea0SLionel Sambuc *
455ebfedea0SLionel Sambuc * @return 0 if everything worked, an error code if not
456ebfedea0SLionel Sambuc */
457ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_step_once(krb5_context context,HDB * db,sqlite3_stmt * statement)458ebfedea0SLionel Sambuc hdb_sqlite_step_once(krb5_context context, HDB *db, sqlite3_stmt *statement)
459ebfedea0SLionel Sambuc {
460ebfedea0SLionel Sambuc int ret;
461ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
462ebfedea0SLionel Sambuc
463ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, statement);
464ebfedea0SLionel Sambuc sqlite3_clear_bindings(statement);
465ebfedea0SLionel Sambuc sqlite3_reset(statement);
466ebfedea0SLionel Sambuc
467ebfedea0SLionel Sambuc return ret;
468ebfedea0SLionel Sambuc }
469ebfedea0SLionel Sambuc
470ebfedea0SLionel Sambuc
471ebfedea0SLionel Sambuc /**
472ebfedea0SLionel Sambuc * Stores an hdb_entry in the database. If flags contains HDB_F_REPLACE
473ebfedea0SLionel Sambuc * a previous entry may be replaced.
474ebfedea0SLionel Sambuc *
475ebfedea0SLionel Sambuc * @param context The current krb5_context
476ebfedea0SLionel Sambuc * @param db Heimdal database handle
477ebfedea0SLionel Sambuc * @param flags May currently only contain HDB_F_REPLACE
478ebfedea0SLionel Sambuc * @param entry The data to store
479ebfedea0SLionel Sambuc *
480ebfedea0SLionel Sambuc * @return 0 if everything worked, an error code if not
481ebfedea0SLionel Sambuc */
482ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_store(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)483ebfedea0SLionel Sambuc hdb_sqlite_store(krb5_context context, HDB *db, unsigned flags,
484ebfedea0SLionel Sambuc hdb_entry_ex *entry)
485ebfedea0SLionel Sambuc {
486ebfedea0SLionel Sambuc int ret;
487ebfedea0SLionel Sambuc int i;
488ebfedea0SLionel Sambuc sqlite_int64 entry_id;
489ebfedea0SLionel Sambuc char *principal_string = NULL;
490ebfedea0SLionel Sambuc char *alias_string;
491ebfedea0SLionel Sambuc const HDB_Ext_Aliases *aliases;
492ebfedea0SLionel Sambuc
493ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *)(db->hdb_db);
494ebfedea0SLionel Sambuc krb5_data value;
495ebfedea0SLionel Sambuc sqlite3_stmt *get_ids = hsdb->get_ids;
496ebfedea0SLionel Sambuc
497ebfedea0SLionel Sambuc ret = hdb_sqlite_exec_stmt(context, hsdb->db,
498ebfedea0SLionel Sambuc "BEGIN IMMEDIATE TRANSACTION", EINVAL);
499ebfedea0SLionel Sambuc if(ret != SQLITE_OK) {
500ebfedea0SLionel Sambuc ret = EINVAL;
501ebfedea0SLionel Sambuc krb5_set_error_message(context, ret,
502ebfedea0SLionel Sambuc "SQLite BEGIN TRANSACTION failed: %s",
503ebfedea0SLionel Sambuc sqlite3_errmsg(hsdb->db));
504ebfedea0SLionel Sambuc goto rollback;
505ebfedea0SLionel Sambuc }
506ebfedea0SLionel Sambuc
507ebfedea0SLionel Sambuc ret = krb5_unparse_name(context,
508ebfedea0SLionel Sambuc entry->entry.principal, &principal_string);
509ebfedea0SLionel Sambuc if (ret) {
510ebfedea0SLionel Sambuc goto rollback;
511ebfedea0SLionel Sambuc }
512ebfedea0SLionel Sambuc
513ebfedea0SLionel Sambuc ret = hdb_seal_keys(context, db, &entry->entry);
514ebfedea0SLionel Sambuc if(ret) {
515ebfedea0SLionel Sambuc goto rollback;
516ebfedea0SLionel Sambuc }
517ebfedea0SLionel Sambuc
518ebfedea0SLionel Sambuc ret = hdb_entry2value(context, &entry->entry, &value);
519ebfedea0SLionel Sambuc if(ret) {
520ebfedea0SLionel Sambuc goto rollback;
521ebfedea0SLionel Sambuc }
522ebfedea0SLionel Sambuc
523ebfedea0SLionel Sambuc sqlite3_bind_text(get_ids, 1, principal_string, -1, SQLITE_STATIC);
524ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, get_ids);
525ebfedea0SLionel Sambuc
526ebfedea0SLionel Sambuc if(ret == SQLITE_DONE) { /* No such principal */
527ebfedea0SLionel Sambuc
528ebfedea0SLionel Sambuc sqlite3_bind_blob(hsdb->add_entry, 1,
529ebfedea0SLionel Sambuc value.data, value.length, SQLITE_STATIC);
530ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_entry);
531ebfedea0SLionel Sambuc sqlite3_clear_bindings(hsdb->add_entry);
532ebfedea0SLionel Sambuc sqlite3_reset(hsdb->add_entry);
533ebfedea0SLionel Sambuc if(ret != SQLITE_DONE)
534ebfedea0SLionel Sambuc goto rollback;
535ebfedea0SLionel Sambuc
536ebfedea0SLionel Sambuc sqlite3_bind_text(hsdb->add_principal, 1,
537ebfedea0SLionel Sambuc principal_string, -1, SQLITE_STATIC);
538ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_principal);
539ebfedea0SLionel Sambuc sqlite3_clear_bindings(hsdb->add_principal);
540ebfedea0SLionel Sambuc sqlite3_reset(hsdb->add_principal);
541ebfedea0SLionel Sambuc if(ret != SQLITE_DONE)
542ebfedea0SLionel Sambuc goto rollback;
543ebfedea0SLionel Sambuc
544ebfedea0SLionel Sambuc entry_id = sqlite3_column_int64(get_ids, 1);
545ebfedea0SLionel Sambuc
546ebfedea0SLionel Sambuc } else if(ret == SQLITE_ROW) { /* Found a principal */
547ebfedea0SLionel Sambuc
548ebfedea0SLionel Sambuc if(! (flags & HDB_F_REPLACE)) /* Not allowed to replace it */
549ebfedea0SLionel Sambuc goto rollback;
550ebfedea0SLionel Sambuc
551ebfedea0SLionel Sambuc entry_id = sqlite3_column_int64(get_ids, 1);
552ebfedea0SLionel Sambuc
553ebfedea0SLionel Sambuc sqlite3_bind_int64(hsdb->delete_aliases, 1, entry_id);
554ebfedea0SLionel Sambuc ret = hdb_sqlite_step_once(context, db, hsdb->delete_aliases);
555ebfedea0SLionel Sambuc if(ret != SQLITE_DONE)
556ebfedea0SLionel Sambuc goto rollback;
557ebfedea0SLionel Sambuc
558ebfedea0SLionel Sambuc sqlite3_bind_blob(hsdb->update_entry, 1,
559ebfedea0SLionel Sambuc value.data, value.length, SQLITE_STATIC);
560ebfedea0SLionel Sambuc sqlite3_bind_int64(hsdb->update_entry, 2, entry_id);
561ebfedea0SLionel Sambuc ret = hdb_sqlite_step_once(context, db, hsdb->update_entry);
562ebfedea0SLionel Sambuc if(ret != SQLITE_DONE)
563ebfedea0SLionel Sambuc goto rollback;
564ebfedea0SLionel Sambuc
565ebfedea0SLionel Sambuc } else {
566ebfedea0SLionel Sambuc /* Error! */
567ebfedea0SLionel Sambuc goto rollback;
568ebfedea0SLionel Sambuc }
569ebfedea0SLionel Sambuc
570ebfedea0SLionel Sambuc ret = hdb_entry_get_aliases(&entry->entry, &aliases);
571ebfedea0SLionel Sambuc if(ret || aliases == NULL)
572ebfedea0SLionel Sambuc goto commit;
573ebfedea0SLionel Sambuc
574ebfedea0SLionel Sambuc for(i = 0; i < aliases->aliases.len; i++) {
575ebfedea0SLionel Sambuc
576ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, &aliases->aliases.val[i],
577ebfedea0SLionel Sambuc &alias_string);
578ebfedea0SLionel Sambuc if (ret) {
579ebfedea0SLionel Sambuc free(alias_string);
580ebfedea0SLionel Sambuc goto rollback;
581ebfedea0SLionel Sambuc }
582ebfedea0SLionel Sambuc
583ebfedea0SLionel Sambuc sqlite3_bind_text(hsdb->add_alias, 1, alias_string,
584ebfedea0SLionel Sambuc -1, SQLITE_STATIC);
585ebfedea0SLionel Sambuc sqlite3_bind_int64(hsdb->add_alias, 2, entry_id);
586ebfedea0SLionel Sambuc ret = hdb_sqlite_step_once(context, db, hsdb->add_alias);
587ebfedea0SLionel Sambuc
588ebfedea0SLionel Sambuc free(alias_string);
589ebfedea0SLionel Sambuc
590ebfedea0SLionel Sambuc if(ret != SQLITE_DONE)
591ebfedea0SLionel Sambuc goto rollback;
592ebfedea0SLionel Sambuc }
593ebfedea0SLionel Sambuc
594ebfedea0SLionel Sambuc ret = 0;
595ebfedea0SLionel Sambuc
596ebfedea0SLionel Sambuc commit:
597ebfedea0SLionel Sambuc
598ebfedea0SLionel Sambuc free(principal_string);
599ebfedea0SLionel Sambuc
600ebfedea0SLionel Sambuc krb5_data_free(&value);
601ebfedea0SLionel Sambuc
602ebfedea0SLionel Sambuc sqlite3_clear_bindings(get_ids);
603ebfedea0SLionel Sambuc sqlite3_reset(get_ids);
604ebfedea0SLionel Sambuc
605ebfedea0SLionel Sambuc ret = hdb_sqlite_exec_stmt(context, hsdb->db, "COMMIT", EINVAL);
606ebfedea0SLionel Sambuc if(ret != SQLITE_OK)
607ebfedea0SLionel Sambuc krb5_warnx(context, "hdb-sqlite: COMMIT problem: %d: %s",
608ebfedea0SLionel Sambuc ret, sqlite3_errmsg(hsdb->db));
609ebfedea0SLionel Sambuc
610ebfedea0SLionel Sambuc return ret;
611ebfedea0SLionel Sambuc
612ebfedea0SLionel Sambuc rollback:
613ebfedea0SLionel Sambuc
614ebfedea0SLionel Sambuc krb5_warnx(context, "hdb-sqlite: store rollback problem: %d: %s",
615ebfedea0SLionel Sambuc ret, sqlite3_errmsg(hsdb->db));
616ebfedea0SLionel Sambuc
617ebfedea0SLionel Sambuc free(principal_string);
618ebfedea0SLionel Sambuc
619ebfedea0SLionel Sambuc ret = hdb_sqlite_exec_stmt(context, hsdb->db,
620ebfedea0SLionel Sambuc "ROLLBACK", EINVAL);
621ebfedea0SLionel Sambuc return ret;
622ebfedea0SLionel Sambuc }
623ebfedea0SLionel Sambuc
624ebfedea0SLionel Sambuc /**
625ebfedea0SLionel Sambuc * This may be called often by other code, since the BDB backends
626ebfedea0SLionel Sambuc * can not have several open connections. SQLite can handle
627ebfedea0SLionel Sambuc * many processes with open handles to the database file
628ebfedea0SLionel Sambuc * and closing/opening the handle is an expensive operation.
629ebfedea0SLionel Sambuc * Hence, this function does nothing.
630ebfedea0SLionel Sambuc *
631ebfedea0SLionel Sambuc * @param context The current krb5 context
632ebfedea0SLionel Sambuc * @param db Heimdal database handle
633ebfedea0SLionel Sambuc *
634ebfedea0SLionel Sambuc * @return Always returns 0
635ebfedea0SLionel Sambuc */
636ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_close(krb5_context context,HDB * db)637ebfedea0SLionel Sambuc hdb_sqlite_close(krb5_context context, HDB *db)
638ebfedea0SLionel Sambuc {
639ebfedea0SLionel Sambuc return 0;
640ebfedea0SLionel Sambuc }
641ebfedea0SLionel Sambuc
642ebfedea0SLionel Sambuc /**
643ebfedea0SLionel Sambuc * The opposite of hdb_sqlite_close. Since SQLite accepts
644ebfedea0SLionel Sambuc * many open handles to the database file the handle does not
645ebfedea0SLionel Sambuc * need to be closed, or reopened.
646ebfedea0SLionel Sambuc *
647ebfedea0SLionel Sambuc * @param context The current krb5 context
648ebfedea0SLionel Sambuc * @param db Heimdal database handle
649ebfedea0SLionel Sambuc * @param flags
650ebfedea0SLionel Sambuc * @param mode_t
651ebfedea0SLionel Sambuc *
652ebfedea0SLionel Sambuc * @return Always returns 0
653ebfedea0SLionel Sambuc */
654ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_open(krb5_context context,HDB * db,int flags,mode_t mode)655ebfedea0SLionel Sambuc hdb_sqlite_open(krb5_context context, HDB *db, int flags, mode_t mode)
656ebfedea0SLionel Sambuc {
657ebfedea0SLionel Sambuc return 0;
658ebfedea0SLionel Sambuc }
659ebfedea0SLionel Sambuc
660ebfedea0SLionel Sambuc /**
661ebfedea0SLionel Sambuc * Closes the databse and frees all resources.
662ebfedea0SLionel Sambuc *
663ebfedea0SLionel Sambuc * @param context The current krb5 context
664ebfedea0SLionel Sambuc * @param db Heimdal database handle
665ebfedea0SLionel Sambuc *
666ebfedea0SLionel Sambuc * @return 0 on success, an error code if not
667ebfedea0SLionel Sambuc */
668ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_destroy(krb5_context context,HDB * db)669ebfedea0SLionel Sambuc hdb_sqlite_destroy(krb5_context context, HDB *db)
670ebfedea0SLionel Sambuc {
671ebfedea0SLionel Sambuc int ret;
672ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb;
673ebfedea0SLionel Sambuc
674ebfedea0SLionel Sambuc ret = hdb_clear_master_key(context, db);
675ebfedea0SLionel Sambuc
676ebfedea0SLionel Sambuc hdb_sqlite_close_database(context, db);
677ebfedea0SLionel Sambuc
678ebfedea0SLionel Sambuc hsdb = (hdb_sqlite_db*)(db->hdb_db);
679ebfedea0SLionel Sambuc
680ebfedea0SLionel Sambuc free(hsdb->db_file);
681ebfedea0SLionel Sambuc free(db->hdb_db);
682ebfedea0SLionel Sambuc free(db);
683ebfedea0SLionel Sambuc
684ebfedea0SLionel Sambuc return ret;
685ebfedea0SLionel Sambuc }
686ebfedea0SLionel Sambuc
687ebfedea0SLionel Sambuc /*
688ebfedea0SLionel Sambuc * Not sure if this is needed.
689ebfedea0SLionel Sambuc */
690ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_lock(krb5_context context,HDB * db,int operation)691ebfedea0SLionel Sambuc hdb_sqlite_lock(krb5_context context, HDB *db, int operation)
692ebfedea0SLionel Sambuc {
693ebfedea0SLionel Sambuc krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
694ebfedea0SLionel Sambuc "lock not implemented");
695ebfedea0SLionel Sambuc return HDB_ERR_CANT_LOCK_DB;
696ebfedea0SLionel Sambuc }
697ebfedea0SLionel Sambuc
698ebfedea0SLionel Sambuc /*
699ebfedea0SLionel Sambuc * Not sure if this is needed.
700ebfedea0SLionel Sambuc */
701ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_unlock(krb5_context context,HDB * db)702ebfedea0SLionel Sambuc hdb_sqlite_unlock(krb5_context context, HDB *db)
703ebfedea0SLionel Sambuc {
704ebfedea0SLionel Sambuc krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
705ebfedea0SLionel Sambuc "unlock not implemented");
706ebfedea0SLionel Sambuc return HDB_ERR_CANT_LOCK_DB;
707ebfedea0SLionel Sambuc }
708ebfedea0SLionel Sambuc
709ebfedea0SLionel Sambuc /*
710ebfedea0SLionel Sambuc * Should get the next entry, to allow iteration over all entries.
711ebfedea0SLionel Sambuc */
712ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_nextkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)713ebfedea0SLionel Sambuc hdb_sqlite_nextkey(krb5_context context, HDB *db, unsigned flags,
714ebfedea0SLionel Sambuc hdb_entry_ex *entry)
715ebfedea0SLionel Sambuc {
716ebfedea0SLionel Sambuc krb5_error_code ret = 0;
717ebfedea0SLionel Sambuc int sqlite_error;
718ebfedea0SLionel Sambuc krb5_data value;
719ebfedea0SLionel Sambuc
720ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
721ebfedea0SLionel Sambuc
722ebfedea0SLionel Sambuc sqlite_error = hdb_sqlite_step(context, hsdb->db, hsdb->get_all_entries);
723ebfedea0SLionel Sambuc if(sqlite_error == SQLITE_ROW) {
724ebfedea0SLionel Sambuc /* Found an entry */
725ebfedea0SLionel Sambuc value.length = sqlite3_column_bytes(hsdb->get_all_entries, 0);
726ebfedea0SLionel Sambuc value.data = (void *) sqlite3_column_blob(hsdb->get_all_entries, 0);
727ebfedea0SLionel Sambuc memset(entry, 0, sizeof(*entry));
728ebfedea0SLionel Sambuc ret = hdb_value2entry(context, &value, &entry->entry);
729ebfedea0SLionel Sambuc }
730ebfedea0SLionel Sambuc else if(sqlite_error == SQLITE_DONE) {
731ebfedea0SLionel Sambuc /* No more entries */
732ebfedea0SLionel Sambuc ret = HDB_ERR_NOENTRY;
733ebfedea0SLionel Sambuc sqlite3_reset(hsdb->get_all_entries);
734ebfedea0SLionel Sambuc }
735ebfedea0SLionel Sambuc else {
736ebfedea0SLionel Sambuc /* XXX SQLite error. Should be handled in some way. */
737ebfedea0SLionel Sambuc ret = EINVAL;
738ebfedea0SLionel Sambuc }
739ebfedea0SLionel Sambuc
740ebfedea0SLionel Sambuc return ret;
741ebfedea0SLionel Sambuc }
742ebfedea0SLionel Sambuc
743ebfedea0SLionel Sambuc /*
744ebfedea0SLionel Sambuc * Should get the first entry in the database.
745ebfedea0SLionel Sambuc * What is flags used for?
746ebfedea0SLionel Sambuc */
747ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_firstkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)748ebfedea0SLionel Sambuc hdb_sqlite_firstkey(krb5_context context, HDB *db, unsigned flags,
749ebfedea0SLionel Sambuc hdb_entry_ex *entry)
750ebfedea0SLionel Sambuc {
751ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
752ebfedea0SLionel Sambuc krb5_error_code ret;
753ebfedea0SLionel Sambuc
754ebfedea0SLionel Sambuc sqlite3_reset(hsdb->get_all_entries);
755ebfedea0SLionel Sambuc
756ebfedea0SLionel Sambuc ret = hdb_sqlite_nextkey(context, db, flags, entry);
757ebfedea0SLionel Sambuc if(ret)
758ebfedea0SLionel Sambuc return ret;
759ebfedea0SLionel Sambuc
760ebfedea0SLionel Sambuc return 0;
761ebfedea0SLionel Sambuc }
762ebfedea0SLionel Sambuc
763ebfedea0SLionel Sambuc /*
764ebfedea0SLionel Sambuc * Renames the database file.
765ebfedea0SLionel Sambuc */
766ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_rename(krb5_context context,HDB * db,const char * new_name)767ebfedea0SLionel Sambuc hdb_sqlite_rename(krb5_context context, HDB *db, const char *new_name)
768ebfedea0SLionel Sambuc {
769ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
770ebfedea0SLionel Sambuc int ret;
771ebfedea0SLionel Sambuc
772ebfedea0SLionel Sambuc krb5_warnx(context, "hdb_sqlite_rename");
773ebfedea0SLionel Sambuc
774ebfedea0SLionel Sambuc if (strncasecmp(new_name, "sqlite:", 7) == 0)
775ebfedea0SLionel Sambuc new_name += 7;
776ebfedea0SLionel Sambuc
777ebfedea0SLionel Sambuc hdb_sqlite_close_database(context, db);
778ebfedea0SLionel Sambuc
779ebfedea0SLionel Sambuc ret = rename(hsdb->db_file, new_name);
780ebfedea0SLionel Sambuc free(hsdb->db_file);
781ebfedea0SLionel Sambuc
782ebfedea0SLionel Sambuc hdb_sqlite_make_database(context, db, new_name);
783ebfedea0SLionel Sambuc
784ebfedea0SLionel Sambuc return ret;
785ebfedea0SLionel Sambuc }
786ebfedea0SLionel Sambuc
787ebfedea0SLionel Sambuc /*
788ebfedea0SLionel Sambuc * Removes a principal, including aliases and associated entry.
789ebfedea0SLionel Sambuc */
790ebfedea0SLionel Sambuc static krb5_error_code
hdb_sqlite_remove(krb5_context context,HDB * db,krb5_const_principal principal)791ebfedea0SLionel Sambuc hdb_sqlite_remove(krb5_context context, HDB *db,
792ebfedea0SLionel Sambuc krb5_const_principal principal)
793ebfedea0SLionel Sambuc {
794ebfedea0SLionel Sambuc krb5_error_code ret;
795ebfedea0SLionel Sambuc char *principal_string;
796ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
797ebfedea0SLionel Sambuc sqlite3_stmt *remove = hsdb->remove;
798ebfedea0SLionel Sambuc
799ebfedea0SLionel Sambuc ret = krb5_unparse_name(context, principal, &principal_string);
800ebfedea0SLionel Sambuc if (ret) {
801ebfedea0SLionel Sambuc free(principal_string);
802ebfedea0SLionel Sambuc return ret;
803ebfedea0SLionel Sambuc }
804ebfedea0SLionel Sambuc
805ebfedea0SLionel Sambuc sqlite3_bind_text(remove, 1, principal_string, -1, SQLITE_STATIC);
806ebfedea0SLionel Sambuc
807ebfedea0SLionel Sambuc ret = hdb_sqlite_step(context, hsdb->db, remove);
808ebfedea0SLionel Sambuc if (ret != SQLITE_DONE) {
809ebfedea0SLionel Sambuc ret = EINVAL;
810ebfedea0SLionel Sambuc krb5_set_error_message(context, ret,
811ebfedea0SLionel Sambuc "sqlite remove failed: %d",
812ebfedea0SLionel Sambuc ret);
813ebfedea0SLionel Sambuc } else
814ebfedea0SLionel Sambuc ret = 0;
815ebfedea0SLionel Sambuc
816ebfedea0SLionel Sambuc sqlite3_clear_bindings(remove);
817ebfedea0SLionel Sambuc sqlite3_reset(remove);
818ebfedea0SLionel Sambuc
819ebfedea0SLionel Sambuc return ret;
820ebfedea0SLionel Sambuc }
821ebfedea0SLionel Sambuc
822ebfedea0SLionel Sambuc /**
823ebfedea0SLionel Sambuc * Create SQLITE object, and creates the on disk database if its doesn't exists.
824ebfedea0SLionel Sambuc *
825ebfedea0SLionel Sambuc * @param context A Kerberos 5 context.
826ebfedea0SLionel Sambuc * @param db a returned database handle.
827ebfedea0SLionel Sambuc * @param argument filename
828ebfedea0SLionel Sambuc *
829ebfedea0SLionel Sambuc * @return 0 on success, an error code if not
830ebfedea0SLionel Sambuc */
831ebfedea0SLionel Sambuc
832ebfedea0SLionel Sambuc krb5_error_code
hdb_sqlite_create(krb5_context context,HDB ** db,const char * argument)833ebfedea0SLionel Sambuc hdb_sqlite_create(krb5_context context, HDB **db, const char *argument)
834ebfedea0SLionel Sambuc {
835ebfedea0SLionel Sambuc krb5_error_code ret;
836ebfedea0SLionel Sambuc hdb_sqlite_db *hsdb;
837ebfedea0SLionel Sambuc
838ebfedea0SLionel Sambuc *db = calloc(1, sizeof (**db));
839ebfedea0SLionel Sambuc if (*db == NULL)
840ebfedea0SLionel Sambuc return krb5_enomem(context);
841ebfedea0SLionel Sambuc
842ebfedea0SLionel Sambuc hsdb = (hdb_sqlite_db*) calloc(1, sizeof (*hsdb));
843ebfedea0SLionel Sambuc if (hsdb == NULL) {
844ebfedea0SLionel Sambuc free(*db);
845ebfedea0SLionel Sambuc *db = NULL;
846ebfedea0SLionel Sambuc return krb5_enomem(context);
847ebfedea0SLionel Sambuc }
848ebfedea0SLionel Sambuc
849ebfedea0SLionel Sambuc (*db)->hdb_db = hsdb;
850ebfedea0SLionel Sambuc
851ebfedea0SLionel Sambuc /* XXX make_database should make sure everything else is freed on error */
852ebfedea0SLionel Sambuc ret = hdb_sqlite_make_database(context, *db, argument);
853ebfedea0SLionel Sambuc if (ret) {
854ebfedea0SLionel Sambuc free((*db)->hdb_db);
855ebfedea0SLionel Sambuc free(*db);
856ebfedea0SLionel Sambuc
857ebfedea0SLionel Sambuc return ret;
858ebfedea0SLionel Sambuc }
859ebfedea0SLionel Sambuc
860ebfedea0SLionel Sambuc (*db)->hdb_master_key_set = 0;
861ebfedea0SLionel Sambuc (*db)->hdb_openp = 0;
862ebfedea0SLionel Sambuc (*db)->hdb_capability_flags = 0;
863ebfedea0SLionel Sambuc
864ebfedea0SLionel Sambuc (*db)->hdb_open = hdb_sqlite_open;
865ebfedea0SLionel Sambuc (*db)->hdb_close = hdb_sqlite_close;
866ebfedea0SLionel Sambuc
867ebfedea0SLionel Sambuc (*db)->hdb_lock = hdb_sqlite_lock;
868ebfedea0SLionel Sambuc (*db)->hdb_unlock = hdb_sqlite_unlock;
869ebfedea0SLionel Sambuc (*db)->hdb_firstkey = hdb_sqlite_firstkey;
870ebfedea0SLionel Sambuc (*db)->hdb_nextkey = hdb_sqlite_nextkey;
871ebfedea0SLionel Sambuc (*db)->hdb_fetch_kvno = hdb_sqlite_fetch_kvno;
872ebfedea0SLionel Sambuc (*db)->hdb_store = hdb_sqlite_store;
873ebfedea0SLionel Sambuc (*db)->hdb_remove = hdb_sqlite_remove;
874ebfedea0SLionel Sambuc (*db)->hdb_destroy = hdb_sqlite_destroy;
875ebfedea0SLionel Sambuc (*db)->hdb_rename = hdb_sqlite_rename;
876ebfedea0SLionel Sambuc (*db)->hdb__get = NULL;
877ebfedea0SLionel Sambuc (*db)->hdb__put = NULL;
878ebfedea0SLionel Sambuc (*db)->hdb__del = NULL;
879ebfedea0SLionel Sambuc
880ebfedea0SLionel Sambuc return 0;
881ebfedea0SLionel Sambuc }
882