hdb: hdb_ldap_common NULL dereference
[heimdal.git] / lib / hdb / hdb-sqlite.c
blobd3461d0182aa55c32b0aff517769c47b003921b9
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 if (ret)
503 return ret;
505 krb5_free_principal(context, enterprise_principal);
507 sqlite_error = hdb_sqlite_step(context, hsdb->db, fetch);
508 if (sqlite_error != SQLITE_ROW) {
509 if(sqlite_error == SQLITE_DONE) {
510 ret = HDB_ERR_NOENTRY;
511 goto out;
512 } else {
513 ret = HDB_ERR_UK_RERROR;
514 krb5_set_error_message(context, ret,
515 "sqlite fetch failed: %d",
516 sqlite_error);
517 goto out;
521 value.length = sqlite3_column_bytes(fetch, 0);
522 value.data = (void *) sqlite3_column_blob(fetch, 0);
524 ret = hdb_value2entry(context, &value, &entry->entry);
525 if(ret)
526 goto out;
528 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
529 ret = hdb_unseal_keys(context, db, &entry->entry);
530 if(ret) {
531 hdb_free_entry(context, entry);
532 goto out;
536 ret = 0;
538 out:
540 sqlite3_clear_bindings(fetch);
541 sqlite3_reset(fetch);
544 return ret;
548 * Convenience function to step a prepared statement with no
549 * value once.
551 * @param context The current krb5_context
552 * @param statement A prepared sqlite3 statement
554 * @return 0 if everything worked, an error code if not
556 static krb5_error_code
557 hdb_sqlite_step_once(krb5_context context, HDB *db, sqlite3_stmt *statement)
559 int ret;
560 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
562 ret = hdb_sqlite_step(context, hsdb->db, statement);
563 sqlite3_clear_bindings(statement);
564 sqlite3_reset(statement);
566 return ret;
571 * Stores an hdb_entry in the database. If flags contains HDB_F_REPLACE
572 * a previous entry may be replaced.
574 * @param context The current krb5_context
575 * @param db Heimdal database handle
576 * @param flags May currently only contain HDB_F_REPLACE
577 * @param entry The data to store
579 * @return 0 if everything worked, an error code if not
581 static krb5_error_code
582 hdb_sqlite_store(krb5_context context, HDB *db, unsigned flags,
583 hdb_entry_ex *entry)
585 int ret;
586 int i;
587 sqlite_int64 entry_id;
588 const HDB_Ext_Aliases *aliases;
590 hdb_sqlite_db *hsdb = (hdb_sqlite_db *)(db->hdb_db);
591 krb5_data value;
592 sqlite3_stmt *get_ids = hsdb->get_ids;
594 krb5_data_zero(&value);
596 ret = hdb_sqlite_exec_stmt(context, hsdb,
597 "BEGIN IMMEDIATE TRANSACTION",
598 HDB_ERR_UK_SERROR);
599 if(ret != SQLITE_OK) {
600 ret = HDB_ERR_UK_SERROR;
601 krb5_set_error_message(context, ret,
602 "SQLite BEGIN TRANSACTION failed: %s",
603 sqlite3_errmsg(hsdb->db));
604 goto rollback;
607 ret = hdb_seal_keys(context, db, &entry->entry);
608 if(ret) {
609 goto rollback;
612 ret = hdb_entry2value(context, &entry->entry, &value);
613 if(ret) {
614 goto rollback;
617 ret = bind_principal(context, entry->entry.principal, get_ids, 1);
618 if (ret)
619 goto rollback;
621 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
623 if(ret == SQLITE_DONE) { /* No such principal */
625 sqlite3_bind_blob(hsdb->add_entry, 1,
626 value.data, value.length, SQLITE_STATIC);
627 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_entry);
628 sqlite3_clear_bindings(hsdb->add_entry);
629 sqlite3_reset(hsdb->add_entry);
630 if (ret != SQLITE_DONE && ret != SQLITE_CONSTRAINT) {
631 ret = HDB_ERR_UK_SERROR;
632 goto rollback;
634 if (ret == SQLITE_CONSTRAINT) {
635 ret = HDB_ERR_EXISTS;
636 goto rollback;
638 ret = 0;
640 ret = bind_principal(context, entry->entry.principal, hsdb->add_principal, 1);
641 if (ret)
642 goto rollback;
644 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_principal);
645 sqlite3_clear_bindings(hsdb->add_principal);
646 sqlite3_reset(hsdb->add_principal);
647 if (ret != SQLITE_DONE && ret != SQLITE_CONSTRAINT) {
648 ret = HDB_ERR_UK_SERROR;
649 goto rollback;
651 if (ret == SQLITE_CONSTRAINT) {
652 ret = HDB_ERR_EXISTS;
653 goto rollback;
656 /* Now let's learn what Entry ID we got for the new principal */
657 sqlite3_reset(get_ids);
658 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
659 if (ret != SQLITE_ROW) {
660 ret = HDB_ERR_UK_SERROR;
661 goto rollback;
664 entry_id = sqlite3_column_int64(get_ids, 1);
666 ret = 0;
668 } else if(ret == SQLITE_ROW) { /* Found a principal */
670 if(! (flags & HDB_F_REPLACE)) /* Not allowed to replace it */
671 goto rollback;
673 entry_id = sqlite3_column_int64(get_ids, 1);
675 sqlite3_bind_int64(hsdb->delete_aliases, 1, entry_id);
676 ret = hdb_sqlite_step_once(context, db, hsdb->delete_aliases);
677 if (ret != SQLITE_DONE) {
678 ret = HDB_ERR_UK_SERROR;
679 goto rollback;
682 sqlite3_bind_blob(hsdb->update_entry, 1,
683 value.data, value.length, SQLITE_STATIC);
684 sqlite3_bind_int64(hsdb->update_entry, 2, entry_id);
685 ret = hdb_sqlite_step_once(context, db, hsdb->update_entry);
686 if (ret != SQLITE_DONE) {
687 ret = HDB_ERR_UK_SERROR;
688 goto rollback;
691 } else {
692 /* Error! */
693 ret = HDB_ERR_UK_SERROR;
694 goto rollback;
697 ret = hdb_entry_get_aliases(&entry->entry, &aliases);
698 if(ret || aliases == NULL)
699 goto commit;
701 for(i = 0; i < aliases->aliases.len; i++) {
703 ret = bind_principal(context, &aliases->aliases.val[i], hsdb->add_alias, 1);
704 if (ret)
705 goto rollback;
707 sqlite3_bind_int64(hsdb->add_alias, 2, entry_id);
708 ret = hdb_sqlite_step_once(context, db, hsdb->add_alias);
709 if (ret == SQLITE_CONSTRAINT) {
710 ret = HDB_ERR_EXISTS;
711 goto rollback;
713 if (ret != SQLITE_DONE) {
714 ret = HDB_ERR_UK_SERROR;
715 goto rollback;
719 commit:
720 krb5_data_free(&value);
721 sqlite3_clear_bindings(get_ids);
722 sqlite3_reset(get_ids);
724 if ((flags & HDB_F_PRECHECK)) {
725 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
726 return 0;
729 ret = hdb_sqlite_exec_stmt(context, hsdb, "COMMIT", HDB_ERR_UK_SERROR);
730 if(ret != SQLITE_OK)
731 krb5_warnx(context, "hdb-sqlite: COMMIT problem: %ld: %s",
732 (long)HDB_ERR_UK_SERROR, sqlite3_errmsg(hsdb->db));
734 return ret == SQLITE_OK ? 0 : HDB_ERR_UK_SERROR;
736 rollback:
737 krb5_data_free(&value);
738 sqlite3_clear_bindings(get_ids);
739 sqlite3_reset(get_ids);
740 krb5_warnx(context, "hdb-sqlite: store rollback problem: %d: %s",
741 ret, sqlite3_errmsg(hsdb->db));
743 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
744 return ret;
748 * This may be called often by other code, since the BDB backends
749 * can not have several open connections. SQLite can handle
750 * many processes with open handles to the database file
751 * and closing/opening the handle is an expensive operation.
752 * Hence, this function does nothing.
754 * @param context The current krb5 context
755 * @param db Heimdal database handle
757 * @return Always returns 0
759 static krb5_error_code
760 hdb_sqlite_close(krb5_context context, HDB *db)
762 return 0;
766 * The opposite of hdb_sqlite_close. Since SQLite accepts
767 * many open handles to the database file the handle does not
768 * need to be closed, or reopened.
770 * @param context The current krb5 context
771 * @param db Heimdal database handle
772 * @param flags
773 * @param mode_t
775 * @return Always returns 0
777 static krb5_error_code
778 hdb_sqlite_open(krb5_context context, HDB *db, int flags, mode_t mode)
780 return 0;
784 * Closes the databse and frees all resources.
786 * @param context The current krb5 context
787 * @param db Heimdal database handle
789 * @return 0 on success, an error code if not
791 static krb5_error_code
792 hdb_sqlite_destroy(krb5_context context, HDB *db)
794 int ret, ret2;
795 hdb_sqlite_db *hsdb;
797 ret = hdb_clear_master_key(context, db);
799 ret2 = hdb_sqlite_close_database(context, db);
801 hsdb = (hdb_sqlite_db*)(db->hdb_db);
803 free(hsdb->db_file);
804 free(db->hdb_db);
805 free(db);
807 return ret ? ret : ret2;
811 * Not sure if this is needed.
813 static krb5_error_code
814 hdb_sqlite_lock(krb5_context context, HDB *db, int operation)
816 krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
817 "lock not implemented");
818 return HDB_ERR_CANT_LOCK_DB;
822 * Not sure if this is needed.
824 static krb5_error_code
825 hdb_sqlite_unlock(krb5_context context, HDB *db)
827 krb5_set_error_message(context, HDB_ERR_CANT_LOCK_DB,
828 "unlock not implemented");
829 return HDB_ERR_CANT_LOCK_DB;
833 * Should get the next entry, to allow iteration over all entries.
835 static krb5_error_code
836 hdb_sqlite_nextkey(krb5_context context, HDB *db, unsigned flags,
837 hdb_entry_ex *entry)
839 krb5_error_code ret = 0;
840 int sqlite_error;
841 krb5_data value;
843 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
845 sqlite_error = hdb_sqlite_step(context, hsdb->db, hsdb->get_all_entries);
846 if(sqlite_error == SQLITE_ROW) {
847 /* Found an entry */
848 value.length = sqlite3_column_bytes(hsdb->get_all_entries, 0);
849 value.data = (void *) sqlite3_column_blob(hsdb->get_all_entries, 0);
850 memset(entry, 0, sizeof(*entry));
851 ret = hdb_value2entry(context, &value, &entry->entry);
853 else if(sqlite_error == SQLITE_DONE) {
854 /* No more entries */
855 ret = HDB_ERR_NOENTRY;
856 sqlite3_reset(hsdb->get_all_entries);
858 else {
859 ret = HDB_ERR_UK_RERROR;
860 krb5_set_error_message(context, HDB_ERR_UK_RERROR,
861 "SELECT failed after returning one or "
862 "more rows: %s", sqlite3_errmsg(hsdb->db));
866 return ret;
870 * Should get the first entry in the database.
871 * What is flags used for?
873 static krb5_error_code
874 hdb_sqlite_firstkey(krb5_context context, HDB *db, unsigned flags,
875 hdb_entry_ex *entry)
877 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
878 krb5_error_code ret;
880 sqlite3_reset(hsdb->get_all_entries);
882 ret = hdb_sqlite_nextkey(context, db, flags, entry);
883 if(ret)
884 return ret;
886 return 0;
890 * Renames the database file.
892 static krb5_error_code
893 hdb_sqlite_rename(krb5_context context, HDB *db, const char *new_name)
895 krb5_error_code ret, ret2;
896 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
898 krb5_warnx(context, "hdb_sqlite_rename");
900 if (strncasecmp(new_name, "sqlite:", 7) == 0)
901 new_name += 7;
903 ret = hdb_sqlite_close_database(context, db);
905 if (rename(hsdb->db_file, new_name) == -1)
906 return errno;
908 free(hsdb->db_file);
909 ret2 = hdb_sqlite_make_database(context, db, new_name);
910 return ret ? ret : ret2;
914 * Removes a principal, including aliases and associated entry.
916 static krb5_error_code
917 hdb_sqlite_remove(krb5_context context, HDB *db,
918 unsigned flags, krb5_const_principal principal)
920 krb5_error_code ret;
921 hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
922 sqlite3_stmt *get_ids = hsdb->get_ids;
923 sqlite3_stmt *rm = hsdb->remove;
925 bind_principal(context, principal, rm, 1);
927 ret = hdb_sqlite_exec_stmt(context, hsdb,
928 "BEGIN IMMEDIATE TRANSACTION",
929 HDB_ERR_UK_SERROR);
930 if (ret != SQLITE_OK) {
931 ret = HDB_ERR_UK_SERROR;
932 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
933 krb5_set_error_message(context, ret,
934 "SQLite BEGIN TRANSACTION failed: %s",
935 sqlite3_errmsg(hsdb->db));
936 return ret;
939 if ((flags & HDB_F_PRECHECK)) {
940 ret = bind_principal(context, principal, get_ids, 1);
941 if (ret)
942 return ret;
944 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
945 sqlite3_clear_bindings(get_ids);
946 sqlite3_reset(get_ids);
947 if (ret == SQLITE_DONE) {
948 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
949 return HDB_ERR_NOENTRY;
953 ret = hdb_sqlite_step(context, hsdb->db, rm);
954 sqlite3_clear_bindings(rm);
955 sqlite3_reset(rm);
956 if (ret != SQLITE_DONE) {
957 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
958 ret = HDB_ERR_UK_SERROR;
959 krb5_set_error_message(context, ret, "sqlite remove failed: %d", ret);
960 return ret;
963 if ((flags & HDB_F_PRECHECK)) {
964 (void) hdb_sqlite_exec_stmt(context, hsdb, "ROLLBACK", 0);
965 return 0;
968 ret = hdb_sqlite_exec_stmt(context, hsdb, "COMMIT", HDB_ERR_UK_SERROR);
969 if (ret != SQLITE_OK)
970 krb5_warnx(context, "hdb-sqlite: COMMIT problem: %ld: %s",
971 (long)HDB_ERR_UK_SERROR, sqlite3_errmsg(hsdb->db));
973 return 0;
977 * Create SQLITE object, and creates the on disk database if its doesn't exists.
979 * @param context A Kerberos 5 context.
980 * @param db a returned database handle.
981 * @param filename filename
983 * @return 0 on success, an error code if not
986 krb5_error_code
987 hdb_sqlite_create(krb5_context context, HDB **db, const char *filename)
989 krb5_error_code ret;
990 hdb_sqlite_db *hsdb;
992 *db = calloc(1, sizeof (**db));
993 if (*db == NULL)
994 return krb5_enomem(context);
996 (*db)->hdb_name = strdup(filename);
997 if ((*db)->hdb_name == NULL) {
998 free(*db);
999 *db = NULL;
1000 return krb5_enomem(context);
1003 hsdb = (hdb_sqlite_db*) calloc(1, sizeof (*hsdb));
1004 if (hsdb == NULL) {
1005 free((*db)->hdb_name);
1006 free(*db);
1007 *db = NULL;
1008 return krb5_enomem(context);
1011 (*db)->hdb_db = hsdb;
1013 /* XXX make_database should make sure everything else is freed on error */
1014 ret = hdb_sqlite_make_database(context, *db, filename);
1015 if (ret) {
1016 free((*db)->hdb_db);
1017 free(*db);
1019 return ret;
1022 (*db)->hdb_master_key_set = 0;
1023 (*db)->hdb_openp = 0;
1024 (*db)->hdb_capability_flags = 0;
1026 (*db)->hdb_open = hdb_sqlite_open;
1027 (*db)->hdb_close = hdb_sqlite_close;
1029 (*db)->hdb_lock = hdb_sqlite_lock;
1030 (*db)->hdb_unlock = hdb_sqlite_unlock;
1031 (*db)->hdb_firstkey = hdb_sqlite_firstkey;
1032 (*db)->hdb_nextkey = hdb_sqlite_nextkey;
1033 (*db)->hdb_fetch_kvno = hdb_sqlite_fetch_kvno;
1034 (*db)->hdb_store = hdb_sqlite_store;
1035 (*db)->hdb_remove = hdb_sqlite_remove;
1036 (*db)->hdb_destroy = hdb_sqlite_destroy;
1037 (*db)->hdb_rename = hdb_sqlite_rename;
1038 (*db)->hdb__get = NULL;
1039 (*db)->hdb__put = NULL;
1040 (*db)->hdb__del = NULL;
1042 return 0;