Use __attribute__ ((__name__)) form
[heimdal.git] / lib / hdb / hdb-sqlite.c
blobbc176b2bb87e1ae56145a8f77a5d86dfc188a27b
1 /*
2 * Copyright (c) 2009 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "hdb_locl.h"
35 #include "sqlite3.h"
37 #define MAX_RETRIES 10
39 typedef struct hdb_sqlite_db {
40 double version;
41 sqlite3 *db;
42 char *db_file;
44 sqlite3_stmt *get_version;
45 sqlite3_stmt *fetch;
46 sqlite3_stmt *get_ids;
47 sqlite3_stmt *add_entry;
48 sqlite3_stmt *add_principal;
49 sqlite3_stmt *add_alias;
50 sqlite3_stmt *delete_aliases;
51 sqlite3_stmt *update_entry;
52 sqlite3_stmt *remove;
53 sqlite3_stmt *get_all_entries;
55 } hdb_sqlite_db;
57 /* This should be used to mark updates which make the code incompatible
58 * with databases created with previous versions. Don't update it if
59 * compatibility is not broken. */
60 #define HDBSQLITE_VERSION 0.1
62 #define _HDBSQLITE_STRINGIFY(x) #x
63 #define HDBSQLITE_STRINGIFY(x) _HDBSQLITE_STRINGIFY(x)
65 #define HDBSQLITE_CREATE_TABLES \
66 " BEGIN TRANSACTION;" \
67 " CREATE TABLE Version (number REAL);" \
68 " INSERT INTO Version (number)" \
69 " VALUES (" HDBSQLITE_STRINGIFY(HDBSQLITE_VERSION) ");" \
70 " CREATE TABLE Principal" \
71 " (id INTEGER PRIMARY KEY," \
72 " principal TEXT UNIQUE NOT NULL," \
73 " canonical INTEGER," \
74 " entry INTEGER);" \
75 " CREATE TABLE Entry" \
76 " (id INTEGER PRIMARY KEY," \
77 " data BLOB);" \
78 " COMMIT"
79 #define HDBSQLITE_CREATE_TRIGGERS \
80 " CREATE TRIGGER remove_principals AFTER DELETE ON Entry" \
81 " BEGIN" \
82 " DELETE FROM Principal" \
83 " WHERE entry = OLD.id;" \
84 " END"
85 #define HDBSQLITE_GET_VERSION \
86 " SELECT number FROM Version"
87 #define HDBSQLITE_FETCH \
88 " SELECT Entry.data FROM Principal, Entry" \
89 " WHERE Principal.principal = ? AND" \
90 " Entry.id = Principal.entry"
91 #define HDBSQLITE_GET_IDS \
92 " SELECT id, entry FROM Principal" \
93 " WHERE principal = ?"
94 #define HDBSQLITE_ADD_ENTRY \
95 " INSERT INTO Entry (data) VALUES (?)"
96 #define HDBSQLITE_ADD_PRINCIPAL \
97 " INSERT INTO Principal (principal, entry, canonical)" \
98 " VALUES (?, last_insert_rowid(), 1)"
99 #define HDBSQLITE_ADD_ALIAS \
100 " INSERT INTO Principal (principal, entry, canonical)" \
101 " VALUES(?, ?, 0)"
102 #define HDBSQLITE_DELETE_ALIASES \
103 " DELETE FROM Principal" \
104 " WHERE entry = ? AND canonical = 0"
105 #define HDBSQLITE_UPDATE_ENTRY \
106 " UPDATE Entry SET data = ?" \
107 " WHERE id = ?"
108 #define HDBSQLITE_REMOVE \
109 " DELETE FROM ENTRY WHERE id = " \
110 " (SELECT entry FROM Principal" \
111 " WHERE principal = ?)"
112 #define HDBSQLITE_GET_ALL_ENTRIES \
113 " SELECT data FROM Entry"
116 * Wrapper around sqlite3_prepare_v2.
118 * @param context The current krb5 context
119 * @param statement Where to store the pointer to the statement
120 * after preparing it
121 * @param str SQL code for the statement
123 * @return 0 if OK, an error code if not
125 static krb5_error_code
126 hdb_sqlite_prepare_stmt(krb5_context context,
127 sqlite3 *db,
128 sqlite3_stmt **statement,
129 const char *str)
131 int ret, tries = 0;
133 ret = sqlite3_prepare_v2(db, str, -1, statement, NULL);
134 while((tries++ < MAX_RETRIES) &&
135 ((ret == SQLITE_BUSY) ||
136 (ret == SQLITE_IOERR_BLOCKED) ||
137 (ret == SQLITE_LOCKED))) {
138 krb5_warnx(context, "hdb-sqlite: prepare busy");
139 sleep(1);
140 ret = sqlite3_prepare_v2(db, str, -1, statement, NULL);
143 if (ret != SQLITE_OK) {
144 krb5_set_error_message(context, HDB_ERR_UK_RERROR,
145 "Failed to prepare stmt %s: %s",
146 str, sqlite3_errmsg(db));
147 return HDB_ERR_UK_RERROR;
150 return 0;
153 static krb5_error_code
154 prep_stmts(krb5_context context, hdb_sqlite_db *hsdb)
156 int ret;
158 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
159 &hsdb->get_version,
160 HDBSQLITE_GET_VERSION);
161 if (ret)
162 return ret;
163 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
164 &hsdb->fetch,
165 HDBSQLITE_FETCH);
166 if (ret)
167 return ret;
168 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
169 &hsdb->get_ids,
170 HDBSQLITE_GET_IDS);
171 if (ret)
172 return ret;
173 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
174 &hsdb->add_entry,
175 HDBSQLITE_ADD_ENTRY);
176 if (ret)
177 return ret;
178 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
179 &hsdb->add_principal,
180 HDBSQLITE_ADD_PRINCIPAL);
181 if (ret)
182 return ret;
183 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
184 &hsdb->add_alias,
185 HDBSQLITE_ADD_ALIAS);
186 if (ret)
187 return ret;
188 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
189 &hsdb->delete_aliases,
190 HDBSQLITE_DELETE_ALIASES);
191 if (ret)
192 return ret;
193 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
194 &hsdb->update_entry,
195 HDBSQLITE_UPDATE_ENTRY);
196 if (ret)
197 return ret;
198 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
199 &hsdb->remove,
200 HDBSQLITE_REMOVE);
201 if (ret)
202 return ret;
203 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
204 &hsdb->get_all_entries,
205 HDBSQLITE_GET_ALL_ENTRIES);
206 return ret;
209 static void
210 finalize_stmts(krb5_context context, hdb_sqlite_db *hsdb)
212 if (hsdb->get_version != NULL)
213 sqlite3_finalize(hsdb->get_version);
214 hsdb->get_version = NULL;
216 if (hsdb->fetch != NULL)
217 sqlite3_finalize(hsdb->fetch);
218 hsdb->fetch = NULL;
220 if (hsdb->get_ids != NULL)
221 sqlite3_finalize(hsdb->get_ids);
222 hsdb->get_ids = NULL;
224 if (hsdb->add_entry != NULL)
225 sqlite3_finalize(hsdb->add_entry);
226 hsdb->add_entry = NULL;
228 if (hsdb->add_principal != NULL)
229 sqlite3_finalize(hsdb->add_principal);
230 hsdb->add_principal = NULL;
232 if (hsdb->add_alias != NULL)
233 sqlite3_finalize(hsdb->add_alias);
234 hsdb->add_alias = NULL;
236 if (hsdb->delete_aliases != NULL)
237 sqlite3_finalize(hsdb->delete_aliases);
238 hsdb->delete_aliases = NULL;
240 if (hsdb->update_entry != NULL)
241 sqlite3_finalize(hsdb->update_entry);
242 hsdb->update_entry = NULL;
244 if (hsdb->remove != NULL)
245 sqlite3_finalize(hsdb->remove);
246 hsdb->remove = NULL;
248 if (hsdb->get_all_entries != NULL)
249 sqlite3_finalize(hsdb->get_all_entries);
250 hsdb->get_all_entries = NULL;
254 * A wrapper around sqlite3_exec.
256 * @param context The current krb5 context
257 * @param database An open sqlite3 database handle
258 * @param statement SQL code to execute
259 * @param error_code What to return if the statement fails
261 * @return 0 if OK, else error_code
263 static krb5_error_code
264 hdb_sqlite_exec_stmt(krb5_context context,
265 hdb_sqlite_db *hsdb,
266 const char *statement,
267 krb5_error_code error_code)
269 int ret;
270 int reinit_stmts = 0;
271 sqlite3 *database = hsdb->db;
273 ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
275 while(((ret == SQLITE_BUSY) ||
276 (ret == SQLITE_IOERR_BLOCKED) ||
277 (ret == SQLITE_LOCKED))) {
278 if (reinit_stmts == 0 && ret == SQLITE_BUSY) {
279 finalize_stmts(context, hsdb);
280 reinit_stmts = 1;
282 krb5_warnx(context, "hdb-sqlite: exec busy: %d", (int)getpid());
283 sleep(1);
284 ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
287 if (ret != SQLITE_OK && error_code) {
288 krb5_set_error_message(context, error_code,
289 "Execute %s: %s", statement,
290 sqlite3_errmsg(database));
291 return error_code;
294 if (reinit_stmts)
295 return prep_stmts(context, hsdb);
297 return 0;
304 static krb5_error_code
305 bind_principal(krb5_context context, krb5_const_principal principal, sqlite3_stmt *stmt, int key)
307 krb5_error_code ret;
308 char *str = NULL;
310 ret = krb5_unparse_name(context, principal, &str);
311 if (ret)
312 return ret;
314 sqlite3_bind_text(stmt, key, str, -1, SQLITE_TRANSIENT);
315 free(str);
316 return 0;
320 * Opens an sqlite3 database handle to a file, may create the
321 * database file depending on flags.
323 * @param context The current krb5 context
324 * @param db Heimdal database handle
325 * @param flags Controls whether or not the file may be created,
326 * may be 0 or SQLITE_OPEN_CREATE
328 static krb5_error_code
329 hdb_sqlite_open_database(krb5_context context, HDB *db, int flags)
331 int ret;
332 hdb_sqlite_db *hsdb = (hdb_sqlite_db*) db->hdb_db;
334 ret = sqlite3_open_v2(hsdb->db_file, &hsdb->db,
335 SQLITE_OPEN_READWRITE | flags, NULL);
337 if (ret) {
338 if (hsdb->db) {
339 ret = ENOENT;
340 krb5_set_error_message(context, ret,
341 "Error opening sqlite database %s: %s",
342 hsdb->db_file, sqlite3_errmsg(hsdb->db));
343 sqlite3_close(hsdb->db);
344 hsdb->db = NULL;
345 } else
346 ret = krb5_enomem(context);
347 return ret;
350 return 0;
353 static int
354 hdb_sqlite_step(krb5_context context, sqlite3 *db, sqlite3_stmt *stmt)
356 int ret;
358 ret = sqlite3_step(stmt);
359 while(((ret == SQLITE_BUSY) ||
360 (ret == SQLITE_IOERR_BLOCKED) ||
361 (ret == SQLITE_LOCKED))) {
362 krb5_warnx(context, "hdb-sqlite: step busy: %d", (int)getpid());
363 sleep(1);
364 ret = sqlite3_step(stmt);
366 return ret;
370 * Closes the database and frees memory allocated for statements.
372 * @param context The current krb5 context
373 * @param db Heimdal database handle
375 static krb5_error_code
376 hdb_sqlite_close_database(krb5_context context, HDB *db)
378 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
380 finalize_stmts(context, hsdb);
382 /* XXX Use sqlite3_close_v2() when we upgrade SQLite3 */
383 if (sqlite3_close(hsdb->db) != SQLITE_OK) {
384 krb5_set_error_message(context, HDB_ERR_UK_SERROR,
385 "SQLite BEGIN TRANSACTION failed: %s",
386 sqlite3_errmsg(hsdb->db));
387 return HDB_ERR_UK_SERROR;
390 return 0;
394 * Opens an sqlite database file and prepares it for use.
395 * If the file does not exist it will be created.
397 * @param context The current krb5_context
398 * @param db The heimdal database handle
399 * @param filename Where to store the database file
401 * @return 0 if everything worked, an error code if not
403 static krb5_error_code
404 hdb_sqlite_make_database(krb5_context context, HDB *db, const char *filename)
406 int ret;
407 int created_file = 0;
408 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
410 hsdb->db_file = strdup(filename);
411 if(hsdb->db_file == NULL)
412 return ENOMEM;
414 ret = hdb_sqlite_open_database(context, db, 0);
415 if (ret) {
416 ret = hdb_sqlite_open_database(context, db, SQLITE_OPEN_CREATE);
417 if (ret) goto out;
419 created_file = 1;
421 ret = hdb_sqlite_exec_stmt(context, hsdb,
422 HDBSQLITE_CREATE_TABLES,
423 HDB_ERR_UK_SERROR);
424 if (ret) goto out;
426 ret = hdb_sqlite_exec_stmt(context, hsdb,
427 HDBSQLITE_CREATE_TRIGGERS,
428 HDB_ERR_UK_SERROR);
429 if (ret) goto out;
432 ret = prep_stmts(context, hsdb);
433 if (ret) goto out;
435 ret = hdb_sqlite_step(context, hsdb->db, hsdb->get_version);
436 if(ret == SQLITE_ROW) {
437 hsdb->version = sqlite3_column_double(hsdb->get_version, 0);
439 sqlite3_reset(hsdb->get_version);
440 ret = 0;
442 if(hsdb->version != HDBSQLITE_VERSION) {
443 ret = HDB_ERR_UK_SERROR;
444 krb5_set_error_message(context, ret, "HDBSQLITE_VERSION mismatch");
447 if(ret) goto out;
449 return 0;
451 out:
452 if (hsdb->db)
453 sqlite3_close(hsdb->db);
454 if (created_file)
455 unlink(hsdb->db_file);
456 free(hsdb->db_file);
457 hsdb->db_file = NULL;
459 return ret;
463 * Retrieves an entry by searching for the given
464 * principal in the Principal database table, both
465 * for canonical principals and aliases.
467 * @param context The current krb5_context
468 * @param db Heimdal database handle
469 * @param principal The principal whose entry to search for
470 * @param flags Currently only for HDB_F_DECRYPT
471 * @param kvno kvno to fetch is HDB_F_KVNO_SPECIFIED use used
473 * @return 0 if everything worked, an error code if not
475 static krb5_error_code
476 hdb_sqlite_fetch_kvno(krb5_context context, HDB *db, krb5_const_principal principal,
477 unsigned flags, krb5_kvno kvno, hdb_entry_ex *entry)
479 int sqlite_error;
480 krb5_error_code ret;
481 hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
482 sqlite3_stmt *fetch = hsdb->fetch;
483 krb5_data value;
484 krb5_principal enterprise_principal = NULL;
486 if (principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
487 if (principal->name.name_string.len != 1) {
488 ret = KRB5_PARSE_MALFORMED;
489 krb5_set_error_message(context, ret, "malformed principal: "
490 "enterprise name with %d name components",
491 principal->name.name_string.len);
492 return ret;
494 ret = krb5_parse_name(context, principal->name.name_string.val[0],
495 &enterprise_principal);
496 if (ret)
497 return ret;
498 principal = enterprise_principal;
501 ret = bind_principal(context, principal, fetch, 1);
502 krb5_free_principal(context, enterprise_principal);
503 if (ret)
504 return ret;
506 sqlite_error = hdb_sqlite_step(context, hsdb->db, fetch);
507 if (sqlite_error != SQLITE_ROW) {
508 if(sqlite_error == SQLITE_DONE) {
509 ret = HDB_ERR_NOENTRY;
510 goto out;
511 } else {
512 ret = HDB_ERR_UK_RERROR;
513 krb5_set_error_message(context, ret,
514 "sqlite fetch failed: %d",
515 sqlite_error);
516 goto out;
520 value.length = sqlite3_column_bytes(fetch, 0);
521 value.data = (void *) sqlite3_column_blob(fetch, 0);
523 ret = hdb_value2entry(context, &value, &entry->entry);
524 if(ret)
525 goto out;
527 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
528 ret = hdb_unseal_keys(context, db, &entry->entry);
529 if(ret) {
530 hdb_free_entry(context, entry);
531 goto out;
535 ret = 0;
537 out:
539 sqlite3_clear_bindings(fetch);
540 sqlite3_reset(fetch);
543 return ret;
547 * Convenience function to step a prepared statement with no
548 * value once.
550 * @param context The current krb5_context
551 * @param statement A prepared sqlite3 statement
553 * @return 0 if everything worked, an error code if not
555 static krb5_error_code
556 hdb_sqlite_step_once(krb5_context context, HDB *db, sqlite3_stmt *statement)
558 int ret;
559 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
561 ret = hdb_sqlite_step(context, hsdb->db, statement);
562 sqlite3_clear_bindings(statement);
563 sqlite3_reset(statement);
565 return ret;
570 * Stores an hdb_entry in the database. If flags contains HDB_F_REPLACE
571 * a previous entry may be replaced.
573 * @param context The current krb5_context
574 * @param db Heimdal database handle
575 * @param flags May currently only contain HDB_F_REPLACE
576 * @param entry The data to store
578 * @return 0 if everything worked, an error code if not
580 static krb5_error_code
581 hdb_sqlite_store(krb5_context context, HDB *db, unsigned flags,
582 hdb_entry_ex *entry)
584 int ret;
585 int i;
586 sqlite_int64 entry_id;
587 const HDB_Ext_Aliases *aliases;
589 hdb_sqlite_db *hsdb = (hdb_sqlite_db *)(db->hdb_db);
590 krb5_data value;
591 sqlite3_stmt *get_ids = hsdb->get_ids;
593 krb5_data_zero(&value);
595 ret = hdb_sqlite_exec_stmt(context, hsdb,
596 "BEGIN IMMEDIATE TRANSACTION",
597 HDB_ERR_UK_SERROR);
598 if(ret != SQLITE_OK) {
599 ret = HDB_ERR_UK_SERROR;
600 krb5_set_error_message(context, ret,
601 "SQLite BEGIN TRANSACTION failed: %s",
602 sqlite3_errmsg(hsdb->db));
603 goto rollback;
606 ret = hdb_seal_keys(context, db, &entry->entry);
607 if(ret) {
608 goto rollback;
611 ret = hdb_entry2value(context, &entry->entry, &value);
612 if(ret) {
613 goto rollback;
616 ret = bind_principal(context, entry->entry.principal, get_ids, 1);
617 if (ret)
618 goto rollback;
620 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
622 if(ret == SQLITE_DONE) { /* No such principal */
624 sqlite3_bind_blob(hsdb->add_entry, 1,
625 value.data, value.length, SQLITE_STATIC);
626 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_entry);
627 sqlite3_clear_bindings(hsdb->add_entry);
628 sqlite3_reset(hsdb->add_entry);
629 if (ret != SQLITE_DONE && ret != SQLITE_CONSTRAINT) {
630 ret = HDB_ERR_UK_SERROR;
631 goto rollback;
633 if (ret == SQLITE_CONSTRAINT) {
634 ret = HDB_ERR_EXISTS;
635 goto rollback;
638 ret = bind_principal(context, entry->entry.principal, hsdb->add_principal, 1);
639 if (ret)
640 goto rollback;
642 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_principal);
643 sqlite3_clear_bindings(hsdb->add_principal);
644 sqlite3_reset(hsdb->add_principal);
645 if (ret != SQLITE_DONE && ret != SQLITE_CONSTRAINT) {
646 ret = HDB_ERR_UK_SERROR;
647 goto rollback;
649 if (ret == SQLITE_CONSTRAINT) {
650 ret = HDB_ERR_EXISTS;
651 goto rollback;
654 /* Now let's learn what Entry ID we got for the new principal */
655 sqlite3_reset(get_ids);
656 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
657 if (ret != SQLITE_ROW) {
658 ret = HDB_ERR_UK_SERROR;
659 goto rollback;
662 entry_id = sqlite3_column_int64(get_ids, 1);
664 } else if(ret == SQLITE_ROW) { /* Found a principal */
666 if(! (flags & HDB_F_REPLACE)) /* Not allowed to replace it */
667 goto rollback;
669 entry_id = sqlite3_column_int64(get_ids, 1);
671 sqlite3_bind_int64(hsdb->delete_aliases, 1, entry_id);
672 ret = hdb_sqlite_step_once(context, db, hsdb->delete_aliases);
673 if (ret != SQLITE_DONE) {
674 ret = HDB_ERR_UK_SERROR;
675 goto rollback;
678 sqlite3_bind_blob(hsdb->update_entry, 1,
679 value.data, value.length, SQLITE_STATIC);
680 sqlite3_bind_int64(hsdb->update_entry, 2, entry_id);
681 ret = hdb_sqlite_step_once(context, db, hsdb->update_entry);
682 if (ret != SQLITE_DONE) {
683 ret = HDB_ERR_UK_SERROR;
684 goto rollback;
687 } else {
688 /* Error! */
689 ret = HDB_ERR_UK_SERROR;
690 goto rollback;
693 ret = hdb_entry_get_aliases(&entry->entry, &aliases);
694 if(ret || aliases == NULL)
695 goto commit;
697 for(i = 0; i < aliases->aliases.len; i++) {
699 ret = bind_principal(context, &aliases->aliases.val[i], hsdb->add_alias, 1);
700 if (ret)
701 goto rollback;
703 sqlite3_bind_int64(hsdb->add_alias, 2, entry_id);
704 ret = hdb_sqlite_step_once(context, db, hsdb->add_alias);
705 if (ret == SQLITE_CONSTRAINT) {
706 ret = HDB_ERR_EXISTS;
707 goto rollback;
709 if (ret != SQLITE_DONE) {
710 ret = HDB_ERR_UK_SERROR;
711 goto rollback;
715 commit:
716 krb5_data_free(&value);
717 sqlite3_clear_bindings(get_ids);
718 sqlite3_reset(get_ids);
720 if ((flags & HDB_F_PRECHECK)) {
721 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
722 return 0;
725 ret = hdb_sqlite_exec_stmt(context, hsdb, "COMMIT", HDB_ERR_UK_SERROR);
726 if(ret != SQLITE_OK)
727 krb5_warnx(context, "hdb-sqlite: COMMIT problem: %ld: %s",
728 (long)HDB_ERR_UK_SERROR, sqlite3_errmsg(hsdb->db));
730 return ret == SQLITE_OK ? 0 : HDB_ERR_UK_SERROR;
732 rollback:
733 krb5_data_free(&value);
734 sqlite3_clear_bindings(get_ids);
735 sqlite3_reset(get_ids);
736 krb5_warnx(context, "hdb-sqlite: store rollback problem: %d: %s",
737 ret, sqlite3_errmsg(hsdb->db));
739 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
740 return ret;
744 * This may be called often by other code, since the BDB backends
745 * can not have several open connections. SQLite can handle
746 * many processes with open handles to the database file
747 * and closing/opening the handle is an expensive operation.
748 * Hence, this function does nothing.
750 * @param context The current krb5 context
751 * @param db Heimdal database handle
753 * @return Always returns 0
755 static krb5_error_code
756 hdb_sqlite_close(krb5_context context, HDB *db)
758 return 0;
762 * The opposite of hdb_sqlite_close. Since SQLite accepts
763 * many open handles to the database file the handle does not
764 * need to be closed, or reopened.
766 * @param context The current krb5 context
767 * @param db Heimdal database handle
768 * @param flags
769 * @param mode_t
771 * @return Always returns 0
773 static krb5_error_code
774 hdb_sqlite_open(krb5_context context, HDB *db, int flags, mode_t mode)
776 return 0;
780 * Closes the databse and frees all resources.
782 * @param context The current krb5 context
783 * @param db Heimdal database handle
785 * @return 0 on success, an error code if not
787 static krb5_error_code
788 hdb_sqlite_destroy(krb5_context context, HDB *db)
790 int ret, ret2;
791 hdb_sqlite_db *hsdb;
793 ret = hdb_clear_master_key(context, db);
795 ret2 = hdb_sqlite_close_database(context, db);
797 hsdb = (hdb_sqlite_db*)(db->hdb_db);
799 free(hsdb->db_file);
800 free(db->hdb_db);
801 free(db);
803 return ret ? ret : ret2;
807 * Not sure if this is needed.
809 static krb5_error_code
810 hdb_sqlite_lock(krb5_context context, HDB *db, int operation)
812 krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
813 "lock not implemented");
814 return HDB_ERR_CANT_LOCK_DB;
818 * Not sure if this is needed.
820 static krb5_error_code
821 hdb_sqlite_unlock(krb5_context context, HDB *db)
823 krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
824 "unlock not implemented");
825 return HDB_ERR_CANT_LOCK_DB;
829 * Should get the next entry, to allow iteration over all entries.
831 static krb5_error_code
832 hdb_sqlite_nextkey(krb5_context context, HDB *db, unsigned flags,
833 hdb_entry_ex *entry)
835 krb5_error_code ret = 0;
836 int sqlite_error;
837 krb5_data value;
839 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
841 sqlite_error = hdb_sqlite_step(context, hsdb->db, hsdb->get_all_entries);
842 if(sqlite_error == SQLITE_ROW) {
843 /* Found an entry */
844 value.length = sqlite3_column_bytes(hsdb->get_all_entries, 0);
845 value.data = (void *) sqlite3_column_blob(hsdb->get_all_entries, 0);
846 memset(entry, 0, sizeof(*entry));
847 ret = hdb_value2entry(context, &value, &entry->entry);
849 else if(sqlite_error == SQLITE_DONE) {
850 /* No more entries */
851 ret = HDB_ERR_NOENTRY;
852 sqlite3_reset(hsdb->get_all_entries);
854 else {
855 ret = HDB_ERR_UK_RERROR;
856 krb5_set_error_message(context, HDB_ERR_UK_RERROR,
857 "SELECT failed after returning one or "
858 "more rows: %s", sqlite3_errmsg(hsdb->db));
862 return ret;
866 * Should get the first entry in the database.
867 * What is flags used for?
869 static krb5_error_code
870 hdb_sqlite_firstkey(krb5_context context, HDB *db, unsigned flags,
871 hdb_entry_ex *entry)
873 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
874 krb5_error_code ret;
876 sqlite3_reset(hsdb->get_all_entries);
878 ret = hdb_sqlite_nextkey(context, db, flags, entry);
879 if(ret)
880 return ret;
882 return 0;
886 * Renames the database file.
888 static krb5_error_code
889 hdb_sqlite_rename(krb5_context context, HDB *db, const char *new_name)
891 krb5_error_code ret, ret2;
892 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
894 krb5_warnx(context, "hdb_sqlite_rename");
896 if (strncasecmp(new_name, "sqlite:", 7) == 0)
897 new_name += 7;
899 ret = hdb_sqlite_close_database(context, db);
901 if (rename(hsdb->db_file, new_name) == -1)
902 return errno;
904 free(hsdb->db_file);
905 ret2 = hdb_sqlite_make_database(context, db, new_name);
906 return ret ? ret : ret2;
910 * Removes a principal, including aliases and associated entry.
912 static krb5_error_code
913 hdb_sqlite_remove(krb5_context context, HDB *db,
914 unsigned flags, krb5_const_principal principal)
916 krb5_error_code ret;
917 hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
918 sqlite3_stmt *get_ids = hsdb->get_ids;
919 sqlite3_stmt *rm = hsdb->remove;
921 bind_principal(context, principal, rm, 1);
923 ret = hdb_sqlite_exec_stmt(context, hsdb,
924 "BEGIN IMMEDIATE TRANSACTION",
925 HDB_ERR_UK_SERROR);
926 if (ret != SQLITE_OK) {
927 ret = HDB_ERR_UK_SERROR;
928 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
929 krb5_set_error_message(context, ret,
930 "SQLite BEGIN TRANSACTION failed: %s",
931 sqlite3_errmsg(hsdb->db));
932 return ret;
935 if ((flags & HDB_F_PRECHECK)) {
936 ret = bind_principal(context, principal, get_ids, 1);
937 if (ret)
938 return ret;
940 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
941 sqlite3_clear_bindings(get_ids);
942 sqlite3_reset(get_ids);
943 if (ret == SQLITE_DONE) {
944 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
945 return HDB_ERR_NOENTRY;
949 ret = hdb_sqlite_step(context, hsdb->db, rm);
950 sqlite3_clear_bindings(rm);
951 sqlite3_reset(rm);
952 if (ret != SQLITE_DONE) {
953 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
954 ret = HDB_ERR_UK_SERROR;
955 krb5_set_error_message(context, ret, "sqlite remove failed: %d", ret);
956 return ret;
959 if ((flags & HDB_F_PRECHECK)) {
960 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
961 return 0;
964 ret = hdb_sqlite_exec_stmt(context, hsdb, "COMMIT", HDB_ERR_UK_SERROR);
965 if (ret != SQLITE_OK)
966 krb5_warnx(context, "hdb-sqlite: COMMIT problem: %ld: %s",
967 (long)HDB_ERR_UK_SERROR, sqlite3_errmsg(hsdb->db));
969 return 0;
973 * Create SQLITE object, and creates the on disk database if its doesn't exists.
975 * @param context A Kerberos 5 context.
976 * @param db a returned database handle.
977 * @param filename filename
979 * @return 0 on success, an error code if not
982 krb5_error_code
983 hdb_sqlite_create(krb5_context context, HDB **db, const char *filename)
985 krb5_error_code ret;
986 hdb_sqlite_db *hsdb;
988 *db = calloc(1, sizeof (**db));
989 if (*db == NULL)
990 return krb5_enomem(context);
992 (*db)->hdb_name = strdup(filename);
993 if ((*db)->hdb_name == NULL) {
994 free(*db);
995 *db = NULL;
996 return krb5_enomem(context);
999 hsdb = (hdb_sqlite_db*) calloc(1, sizeof (*hsdb));
1000 if (hsdb == NULL) {
1001 free((*db)->hdb_name);
1002 free(*db);
1003 *db = NULL;
1004 return krb5_enomem(context);
1007 (*db)->hdb_db = hsdb;
1009 /* XXX make_database should make sure everything else is freed on error */
1010 ret = hdb_sqlite_make_database(context, *db, filename);
1011 if (ret) {
1012 free((*db)->hdb_db);
1013 free(*db);
1015 return ret;
1018 (*db)->hdb_master_key_set = 0;
1019 (*db)->hdb_openp = 0;
1020 (*db)->hdb_capability_flags = 0;
1022 (*db)->hdb_open = hdb_sqlite_open;
1023 (*db)->hdb_close = hdb_sqlite_close;
1025 (*db)->hdb_lock = hdb_sqlite_lock;
1026 (*db)->hdb_unlock = hdb_sqlite_unlock;
1027 (*db)->hdb_firstkey = hdb_sqlite_firstkey;
1028 (*db)->hdb_nextkey = hdb_sqlite_nextkey;
1029 (*db)->hdb_fetch_kvno = hdb_sqlite_fetch_kvno;
1030 (*db)->hdb_store = hdb_sqlite_store;
1031 (*db)->hdb_remove = hdb_sqlite_remove;
1032 (*db)->hdb_destroy = hdb_sqlite_destroy;
1033 (*db)->hdb_rename = hdb_sqlite_rename;
1034 (*db)->hdb__get = NULL;
1035 (*db)->hdb__put = NULL;
1036 (*db)->hdb__del = NULL;
1038 return 0;