wait for dead children, and then abandon the live ones
[heimdal.git] / lib / hdb / hdb-sqlite.c
blobac12bd808d25495f1102092f3f101162c92823b1
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) {
145 krb5_set_error_string(context, "Failed to prepare stmt %s: %s",
146 str, sqlite3_errmsg(db));
147 return EINVAL;
150 return 0;
154 * A wrapper around sqlite3_exec.
156 * @param context The current krb5 context
157 * @param database An open sqlite3 database handle
158 * @param statement SQL code to execute
159 * @param error_code What to return if the statement fails
161 * @return 0 if OK, else error_code
163 static krb5_error_code
164 hdb_sqlite_exec_stmt(krb5_context context,
165 sqlite3 *database,
166 const char *statement,
167 krb5_error_code error_code)
169 int ret;
171 ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
173 while(((ret == SQLITE_BUSY) ||
174 (ret == SQLITE_IOERR_BLOCKED) ||
175 (ret == SQLITE_LOCKED))) {
176 krb5_warnx(context, "hdb-sqlite: exec busy: %d", (int)getpid());
177 sleep(1);
178 ret = sqlite3_exec(database, statement, NULL, NULL, NULL);
181 if (ret != SQLITE_OK && error_code) {
182 krb5_set_error_string(context, "Execute %s: %s", statement,
183 sqlite3_errmsg(database));
184 return error_code;
187 return 0;
191 * Opens an sqlite3 database handle to a file, may create the
192 * database file depending on flags.
194 * @param context The current krb5 context
195 * @param db Heimdal database handle
196 * @param flags Controls whether or not the file may be created,
197 * may be 0 or SQLITE_OPEN_CREATE
199 static krb5_error_code
200 hdb_sqlite_open_database(krb5_context context, HDB *db, int flags)
202 int ret;
203 hdb_sqlite_db *hsdb = (hdb_sqlite_db*) db->hdb_db;
205 ret = sqlite3_open_v2(hsdb->db_file, &hsdb->db,
206 SQLITE_OPEN_READWRITE | flags, NULL);
208 if (ret) {
209 if (hsdb->db) {
210 krb5_set_error_string(context,
211 "Error opening sqlite database %s: %s",
212 hsdb->db_file, sqlite3_errmsg(hsdb->db));
213 sqlite3_close(hsdb->db);
214 hsdb->db = NULL;
216 else
217 krb5_set_error_string(context, "out of memory");
218 return ENOENT;
221 return 0;
224 static int
225 hdb_sqlite_step(krb5_context context, sqlite3 *db, sqlite3_stmt *stmt)
227 int ret;
229 ret = sqlite3_step(stmt);
230 while(((ret == SQLITE_BUSY) ||
231 (ret == SQLITE_IOERR_BLOCKED) ||
232 (ret == SQLITE_LOCKED))) {
233 krb5_warnx(context, "hdb-sqlite: step busy: %d", (int)getpid());
234 sleep(1);
235 ret = sqlite3_step(stmt);
237 return ret;
241 * Closes the database and frees memory allocated for statements.
243 * @param context The current krb5 context
244 * @param db Heimdal database handle
246 static krb5_error_code
247 hdb_sqlite_close_database(krb5_context context, HDB *db)
249 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
251 sqlite3_finalize(hsdb->get_version);
252 sqlite3_finalize(hsdb->fetch);
253 sqlite3_finalize(hsdb->get_ids);
254 sqlite3_finalize(hsdb->add_entry);
255 sqlite3_finalize(hsdb->add_principal);
256 sqlite3_finalize(hsdb->add_alias);
257 sqlite3_finalize(hsdb->delete_aliases);
258 sqlite3_finalize(hsdb->update_entry);
259 sqlite3_finalize(hsdb->remove);
260 sqlite3_finalize(hsdb->get_all_entries);
262 sqlite3_close(hsdb->db);
264 return 0;
268 * Opens an sqlite database file and prepares it for use.
269 * If the file does not exist it will be created.
271 * @param context The current krb5_context
272 * @param db The heimdal database handle
273 * @param filename Where to store the database file
275 * @return 0 if everything worked, an error code if not
277 static krb5_error_code
278 hdb_sqlite_make_database(krb5_context context, HDB *db, const char *filename)
280 int ret;
281 int created_file = 0;
282 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
284 hsdb->db_file = strdup(filename);
285 if(hsdb->db_file == NULL)
286 return ENOMEM;
288 ret = hdb_sqlite_open_database(context, db, 0);
289 if (ret) {
290 ret = hdb_sqlite_open_database(context, db, SQLITE_OPEN_CREATE);
291 if (ret) goto out;
293 created_file = 1;
295 ret = hdb_sqlite_exec_stmt(context, hsdb->db,
296 HDBSQLITE_CREATE_TABLES,
297 EINVAL);
298 if (ret) goto out;
300 ret = hdb_sqlite_exec_stmt(context, hsdb->db,
301 HDBSQLITE_CREATE_TRIGGERS,
302 EINVAL);
303 if (ret) goto out;
306 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
307 &hsdb->get_version,
308 HDBSQLITE_GET_VERSION);
309 if (ret) goto out;
310 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
311 &hsdb->fetch,
312 HDBSQLITE_FETCH);
313 if (ret) goto out;
314 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
315 &hsdb->get_ids,
316 HDBSQLITE_GET_IDS);
317 if (ret) goto out;
318 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
319 &hsdb->add_entry,
320 HDBSQLITE_ADD_ENTRY);
321 if (ret) goto out;
322 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
323 &hsdb->add_principal,
324 HDBSQLITE_ADD_PRINCIPAL);
325 if (ret) goto out;
326 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
327 &hsdb->add_alias,
328 HDBSQLITE_ADD_ALIAS);
329 if (ret) goto out;
330 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
331 &hsdb->delete_aliases,
332 HDBSQLITE_DELETE_ALIASES);
333 if (ret) goto out;
334 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
335 &hsdb->update_entry,
336 HDBSQLITE_UPDATE_ENTRY);
337 if (ret) goto out;
338 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
339 &hsdb->remove,
340 HDBSQLITE_REMOVE);
341 if (ret) goto out;
342 ret = hdb_sqlite_prepare_stmt(context, hsdb->db,
343 &hsdb->get_all_entries,
344 HDBSQLITE_GET_ALL_ENTRIES);
345 if (ret) goto out;
347 ret = hdb_sqlite_step(context, hsdb->db, hsdb->get_version);
348 if(ret == SQLITE_ROW) {
349 hsdb->version = sqlite3_column_double(hsdb->get_version, 0);
351 sqlite3_reset(hsdb->get_version);
352 ret = 0;
354 if(hsdb->version != HDBSQLITE_VERSION) {
355 krb5_set_error_string(context, "HDBSQLITE_VERSION mismatch");
356 ret = EINVAL;
359 if(ret) goto out;
361 return 0;
363 out:
364 if (hsdb->db)
365 sqlite3_close(hsdb->db);
366 if (created_file)
367 unlink(hsdb->db_file);
369 return ret;
373 * Retrieves an entry by searching for the given
374 * principal in the Principal database table, both
375 * for canonical principals and aliases.
377 * @param context The current krb5_context
378 * @param db Heimdal database handle
379 * @param principal The principal whose entry to search for
380 * @param flags Currently only for HDB_F_DECRYPT
382 * @return 0 if everything worked, an error code if not
384 static krb5_error_code
385 hdb_sqlite_fetch(krb5_context context, HDB *db, krb5_const_principal principal,
386 unsigned flags, hdb_entry_ex *entry)
388 int sqlite_error;
389 krb5_error_code ret;
390 char *principal_string;
391 hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
392 sqlite3_stmt *fetch = hsdb->fetch;
393 krb5_data value;
395 ret = krb5_unparse_name(context, principal, &principal_string);
396 if (ret) {
397 free(principal_string);
398 return ret;
401 sqlite3_bind_text(fetch, 1, principal_string, -1, SQLITE_STATIC);
403 sqlite_error = hdb_sqlite_step(context, hsdb->db, fetch);
404 if (sqlite_error != SQLITE_ROW) {
405 if(sqlite_error == SQLITE_DONE) {
406 ret = HDB_ERR_NOENTRY;
407 goto out;
408 } else {
409 krb5_set_error_string(context,
410 "sqlite fetch failed: %d",
411 sqlite_error);
412 ret = EINVAL;
413 goto out;
417 if (db->hdb_master_key_set && (flags & HDB_F_DECRYPT)) {
418 ret = hdb_unseal_keys(context, db, &entry->entry);
419 if(ret) {
420 hdb_free_entry(context, entry);
421 goto out;
425 value.length = sqlite3_column_bytes(fetch, 0);
426 value.data = (void *) sqlite3_column_blob(fetch, 0);
428 ret = hdb_value2entry(context, &value, &entry->entry);
429 if(ret)
430 goto out;
432 ret = 0;
434 out:
436 sqlite3_clear_bindings(fetch);
437 sqlite3_reset(fetch);
439 free(principal_string);
441 return ret;
445 * Convenience function to step a prepared statement with no
446 * value once.
448 * @param context The current krb5_context
449 * @param statement A prepared sqlite3 statement
451 * @return 0 if everything worked, an error code if not
453 static krb5_error_code
454 hdb_sqlite_step_once(krb5_context context, HDB *db, sqlite3_stmt *statement)
456 int ret;
457 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
459 ret = hdb_sqlite_step(context, hsdb->db, statement);
460 sqlite3_clear_bindings(statement);
461 sqlite3_reset(statement);
463 return ret;
468 * Stores an hdb_entry in the database. If flags contains HDB_F_REPLACE
469 * a previous entry may be replaced.
471 * @param context The current krb5_context
472 * @param db Heimdal database handle
473 * @param flags May currently only contain HDB_F_REPLACE
474 * @param entry The data to store
476 * @return 0 if everything worked, an error code if not
478 static krb5_error_code
479 hdb_sqlite_store(krb5_context context, HDB *db, unsigned flags,
480 hdb_entry_ex *entry)
482 int ret;
483 int i;
484 sqlite_int64 entry_id;
485 char *principal_string = NULL;
486 char *alias_string;
487 const HDB_Ext_Aliases *aliases;
489 hdb_sqlite_db *hsdb = (hdb_sqlite_db *)(db->hdb_db);
490 krb5_data value;
491 sqlite3_stmt *get_ids = hsdb->get_ids;
493 ret = hdb_sqlite_exec_stmt(context, hsdb->db,
494 "BEGIN IMMEDIATE TRANSACTION", EINVAL);
495 if(ret != SQLITE_OK) {
496 krb5_set_error_string(context, "SQLite BEGIN TRANSACTION failed: %s",
497 sqlite3_errmsg(hsdb->db));
498 goto rollback;
501 ret = krb5_unparse_name(context,
502 entry->entry.principal, &principal_string);
503 if (ret) {
504 goto rollback;
507 ret = hdb_seal_keys(context, db, &entry->entry);
508 if(ret) {
509 goto rollback;
512 ret = hdb_entry2value(context, &entry->entry, &value);
513 if(ret) {
514 goto rollback;
517 sqlite3_bind_text(get_ids, 1, principal_string, -1, SQLITE_STATIC);
518 ret = hdb_sqlite_step(context, hsdb->db, get_ids);
520 if(ret == SQLITE_DONE) { /* No such principal */
522 sqlite3_bind_blob(hsdb->add_entry, 1,
523 value.data, value.length, SQLITE_STATIC);
524 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_entry);
525 sqlite3_clear_bindings(hsdb->add_entry);
526 sqlite3_reset(hsdb->add_entry);
527 if(ret != SQLITE_DONE)
528 goto rollback;
530 sqlite3_bind_text(hsdb->add_principal, 1,
531 principal_string, -1, SQLITE_STATIC);
532 ret = hdb_sqlite_step(context, hsdb->db, hsdb->add_principal);
533 sqlite3_clear_bindings(hsdb->add_principal);
534 sqlite3_reset(hsdb->add_principal);
535 if(ret != SQLITE_DONE)
536 goto rollback;
538 entry_id = sqlite3_column_int64(get_ids, 1);
540 } else if(ret == SQLITE_ROW) { /* Found a principal */
542 if(! (flags & HDB_F_REPLACE)) /* Not allowed to replace it */
543 goto rollback;
545 entry_id = sqlite3_column_int64(get_ids, 1);
547 sqlite3_bind_int64(hsdb->delete_aliases, 1, entry_id);
548 ret = hdb_sqlite_step_once(context, db, hsdb->delete_aliases);
549 if(ret != SQLITE_DONE)
550 goto rollback;
552 sqlite3_bind_blob(hsdb->update_entry, 1,
553 value.data, value.length, SQLITE_STATIC);
554 sqlite3_bind_int64(hsdb->update_entry, 2, entry_id);
555 ret = hdb_sqlite_step_once(context, db, hsdb->update_entry);
556 if(ret != SQLITE_DONE)
557 goto rollback;
559 } else {
560 /* Error! */
561 goto rollback;
564 ret = hdb_entry_get_aliases(&entry->entry, &aliases);
565 if(ret || aliases == NULL)
566 goto commit;
568 for(i = 0; i < aliases->aliases.len; i++) {
570 ret = krb5_unparse_name(context, &aliases->aliases.val[i],
571 &alias_string);
572 if (ret) {
573 free(alias_string);
574 goto rollback;
577 sqlite3_bind_text(hsdb->add_alias, 1, alias_string,
578 -1, SQLITE_STATIC);
579 sqlite3_bind_int64(hsdb->add_alias, 2, entry_id);
580 ret = hdb_sqlite_step_once(context, db, hsdb->add_alias);
582 free(alias_string);
584 if(ret != SQLITE_DONE)
585 goto rollback;
588 ret = 0;
590 commit:
592 free(principal_string);
594 krb5_data_free(&value);
596 sqlite3_clear_bindings(get_ids);
597 sqlite3_reset(get_ids);
599 ret = hdb_sqlite_exec_stmt(context, hsdb->db, "COMMIT", EINVAL);
600 if(ret != SQLITE_OK)
601 krb5_warnx(context, "hdb-sqlite: COMMIT problem: %d: %s",
602 ret, sqlite3_errmsg(hsdb->db));
604 return ret;
606 rollback:
608 krb5_warnx(context, "hdb-sqlite: store rollback problem: %d: %s",
609 ret, sqlite3_errmsg(hsdb->db));
611 free(principal_string);
613 ret = hdb_sqlite_exec_stmt(context, hsdb->db,
614 "ROLLBACK", EINVAL);
615 return ret;
619 * This may be called often by other code, since the BDB backends
620 * can not have several open connections. SQLite can handle
621 * many processes with open handles to the database file
622 * and closing/opening the handle is an expensive operation.
623 * Hence, this function does nothing.
625 * @param context The current krb5 context
626 * @param db Heimdal database handle
628 * @return Always returns 0
630 static krb5_error_code
631 hdb_sqlite_close(krb5_context context, HDB *db)
633 return 0;
637 * The opposite of hdb_sqlite_close. Since SQLite accepts
638 * many open handles to the database file the handle does not
639 * need to be closed, or reopened.
641 * @param context The current krb5 context
642 * @param db Heimdal database handle
643 * @param flags
644 * @param mode_t
646 * @return Always returns 0
648 static krb5_error_code
649 hdb_sqlite_open(krb5_context context, HDB *db, int flags, mode_t mode)
651 return 0;
655 * Closes the databse and frees all resources.
657 * @param context The current krb5 context
658 * @param db Heimdal database handle
660 * @return 0 on success, an error code if not
662 static krb5_error_code
663 hdb_sqlite_destroy(krb5_context context, HDB *db)
665 int ret;
666 hdb_sqlite_db *hsdb;
668 ret = hdb_clear_master_key(context, db);
670 hdb_sqlite_close_database(context, db);
672 hsdb = (hdb_sqlite_db*)(db->hdb_db);
674 free(hsdb->db_file);
675 free(db->hdb_db);
676 free(db);
678 return ret;
682 * Not sure if this is needed.
684 static krb5_error_code
685 hdb_sqlite_lock(krb5_context context, HDB *db, int operation)
687 krb5_set_error_string(context, "lock not implemented");
688 return HDB_ERR_CANT_LOCK_DB;
692 * Not sure if this is needed.
694 static krb5_error_code
695 hdb_sqlite_unlock(krb5_context context, HDB *db)
697 krb5_set_error_string(context, "unlock not implemented");
698 return HDB_ERR_CANT_LOCK_DB;
702 * Should get the next entry, to allow iteration over all entries.
704 static krb5_error_code
705 hdb_sqlite_nextkey(krb5_context context, HDB *db, unsigned flags,
706 hdb_entry_ex *entry)
708 krb5_error_code ret = 0;
709 int sqlite_error;
710 krb5_data value;
712 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
714 sqlite_error = hdb_sqlite_step(context, hsdb->db, hsdb->get_all_entries);
715 if(sqlite_error == SQLITE_ROW) {
716 /* Found an entry */
717 value.length = sqlite3_column_bytes(hsdb->get_all_entries, 0);
718 value.data = (void *) sqlite3_column_blob(hsdb->get_all_entries, 0);
719 memset(entry, 0, sizeof(*entry));
720 ret = hdb_value2entry(context, &value, &entry->entry);
722 else if(sqlite_error == SQLITE_DONE) {
723 /* No more entries */
724 ret = HDB_ERR_NOENTRY;
725 sqlite3_reset(hsdb->get_all_entries);
727 else {
728 /* XXX SQLite error. Should be handled in some way. */
729 ret = EINVAL;
732 return ret;
736 * Should get the first entry in the database.
737 * What is flags used for?
739 static krb5_error_code
740 hdb_sqlite_firstkey(krb5_context context, HDB *db, unsigned flags,
741 hdb_entry_ex *entry)
743 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
744 krb5_error_code ret;
746 sqlite3_reset(hsdb->get_all_entries);
748 ret = hdb_sqlite_nextkey(context, db, flags, entry);
749 if(ret)
750 return ret;
752 return 0;
756 * Renames the database file.
758 static krb5_error_code
759 hdb_sqlite_rename(krb5_context context, HDB *db, const char *new_name)
761 hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db;
762 int ret;
764 krb5_warnx(context, "hdb_sqlite_rename");
766 if (strncasecmp(new_name, "sqlite:", 7) == 0)
767 new_name += 7;
769 hdb_sqlite_close_database(context, db);
771 ret = rename(hsdb->db_file, new_name);
772 free(hsdb->db_file);
774 hdb_sqlite_make_database(context, db, new_name);
776 return ret;
780 * Removes a principal, including aliases and associated entry.
782 static krb5_error_code
783 hdb_sqlite_remove(krb5_context context, HDB *db,
784 krb5_const_principal principal)
786 krb5_error_code ret;
787 char *principal_string;
788 hdb_sqlite_db *hsdb = (hdb_sqlite_db*)(db->hdb_db);
789 sqlite3_stmt *remove = hsdb->remove;
791 ret = krb5_unparse_name(context, principal, &principal_string);
792 if (ret) {
793 free(principal_string);
794 return ret;
797 sqlite3_bind_text(remove, 1, principal_string, -1, SQLITE_STATIC);
799 ret = hdb_sqlite_step(context, hsdb->db, remove);
800 if (ret != SQLITE_DONE) {
801 krb5_set_error_string(context,
802 "sqlite remove failed: %d",
803 ret);
804 } else
805 ret = 0;
807 sqlite3_clear_bindings(remove);
808 sqlite3_reset(remove);
810 return ret;
814 * Create SQLITE object, and creates the on disk database if its doesn't exists.
816 * @param context A Kerberos 5 context.
817 * @param db a returned database handle.
818 * @param argument filename
820 * @return 0 on success, an error code if not
823 krb5_error_code
824 hdb_sqlite_create(krb5_context context, HDB **db, const char *argument)
826 krb5_error_code ret;
827 hdb_sqlite_db *hsdb;
829 *db = calloc(1, sizeof (**db));
830 if (*db == NULL) {
831 krb5_set_error_string(context, "calloc: out of memory");
832 return ENOMEM;
835 hsdb = (hdb_sqlite_db*) calloc(1, sizeof (*hsdb));
836 if (hsdb == NULL) {
837 free(*db);
838 *db = NULL;
839 krb5_set_error_string(context, "malloc: out of memory");
840 return ENOMEM;
843 (*db)->hdb_db = hsdb;
845 /* XXX make_database should make sure everything else is freed on error */
846 ret = hdb_sqlite_make_database(context, *db, argument);
847 if (ret) {
848 free((*db)->hdb_db);
849 free(*db);
851 return ret;
854 (*db)->hdb_master_key_set = 0;
855 (*db)->hdb_openp = 0;
856 (*db)->hdb_capability_flags = 0;
858 (*db)->hdb_open = hdb_sqlite_open;
859 (*db)->hdb_close = hdb_sqlite_close;
861 (*db)->hdb_lock = hdb_sqlite_lock;
862 (*db)->hdb_unlock = hdb_sqlite_unlock;
863 (*db)->hdb_firstkey = hdb_sqlite_firstkey;
864 (*db)->hdb_nextkey = hdb_sqlite_nextkey;
865 (*db)->hdb_fetch = hdb_sqlite_fetch;
866 (*db)->hdb_store = hdb_sqlite_store;
867 (*db)->hdb_remove = hdb_sqlite_remove;
868 (*db)->hdb_destroy = hdb_sqlite_destroy;
869 (*db)->hdb_rename = hdb_sqlite_rename;
870 (*db)->hdb__get = NULL;
871 (*db)->hdb__put = NULL;
872 (*db)->hdb__del = NULL;
874 return 0;