2 * Copyright (c) 2009 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
37 #define MAX_RETRIES 10
39 typedef struct hdb_sqlite_db
{
44 sqlite3_stmt
*get_version
;
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
;
53 sqlite3_stmt
*get_all_entries
;
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," \
75 " CREATE TABLE Entry" \
76 " (id INTEGER PRIMARY KEY," \
79 #define HDBSQLITE_CREATE_TRIGGERS \
80 " CREATE TRIGGER remove_principals AFTER DELETE ON Entry" \
82 " DELETE FROM Principal" \
83 " WHERE entry = OLD.id;" \
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)" \
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 = ?" \
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
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
,
128 sqlite3_stmt
**statement
,
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");
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
;
153 static krb5_error_code
154 prep_stmts(krb5_context context
, hdb_sqlite_db
*hsdb
)
158 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
160 HDBSQLITE_GET_VERSION
);
163 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
168 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
173 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
175 HDBSQLITE_ADD_ENTRY
);
178 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
179 &hsdb
->add_principal
,
180 HDBSQLITE_ADD_PRINCIPAL
);
183 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
185 HDBSQLITE_ADD_ALIAS
);
188 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
189 &hsdb
->delete_aliases
,
190 HDBSQLITE_DELETE_ALIASES
);
193 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
195 HDBSQLITE_UPDATE_ENTRY
);
198 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
203 ret
= hdb_sqlite_prepare_stmt(context
, hsdb
->db
,
204 &hsdb
->get_all_entries
,
205 HDBSQLITE_GET_ALL_ENTRIES
);
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
);
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
);
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
,
266 const char *statement
,
267 krb5_error_code error_code
)
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
);
282 krb5_warnx(context
, "hdb-sqlite: exec busy: %d", (int)getpid());
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
));
295 return prep_stmts(context
, hsdb
);
304 static krb5_error_code
305 bind_principal(krb5_context context
, krb5_const_principal principal
, sqlite3_stmt
*stmt
, int key
)
310 ret
= krb5_unparse_name(context
, principal
, &str
);
314 sqlite3_bind_text(stmt
, key
, str
, -1, SQLITE_TRANSIENT
);
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
)
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
);
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
);
346 ret
= krb5_enomem(context
);
354 hdb_sqlite_step(krb5_context context
, sqlite3
*db
, sqlite3_stmt
*stmt
)
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());
364 ret
= sqlite3_step(stmt
);
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
;
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
)
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
)
414 ret
= hdb_sqlite_open_database(context
, db
, 0);
416 ret
= hdb_sqlite_open_database(context
, db
, SQLITE_OPEN_CREATE
);
421 ret
= hdb_sqlite_exec_stmt(context
, hsdb
,
422 HDBSQLITE_CREATE_TABLES
,
426 ret
= hdb_sqlite_exec_stmt(context
, hsdb
,
427 HDBSQLITE_CREATE_TRIGGERS
,
432 ret
= prep_stmts(context
, hsdb
);
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
);
442 if(hsdb
->version
!= HDBSQLITE_VERSION
) {
443 ret
= HDB_ERR_UK_SERROR
;
444 krb5_set_error_message(context
, ret
, "HDBSQLITE_VERSION mismatch");
453 sqlite3_close(hsdb
->db
);
455 unlink(hsdb
->db_file
);
457 hsdb
->db_file
= NULL
;
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
)
481 hdb_sqlite_db
*hsdb
= (hdb_sqlite_db
*)(db
->hdb_db
);
482 sqlite3_stmt
*fetch
= hsdb
->fetch
;
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
);
494 ret
= krb5_parse_name(context
, principal
->name
.name_string
.val
[0],
495 &enterprise_principal
);
498 principal
= enterprise_principal
;
501 ret
= bind_principal(context
, principal
, fetch
, 1);
502 krb5_free_principal(context
, enterprise_principal
);
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
;
512 ret
= HDB_ERR_UK_RERROR
;
513 krb5_set_error_message(context
, ret
,
514 "sqlite fetch failed: %d",
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
);
527 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
528 ret
= hdb_unseal_keys(context
, db
, &entry
->entry
);
530 hdb_free_entry(context
, entry
);
539 sqlite3_clear_bindings(fetch
);
540 sqlite3_reset(fetch
);
547 * Convenience function to step a prepared statement with no
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
)
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
);
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
,
586 sqlite_int64 entry_id
;
587 const HDB_Ext_Aliases
*aliases
;
589 hdb_sqlite_db
*hsdb
= (hdb_sqlite_db
*)(db
->hdb_db
);
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",
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
));
606 ret
= hdb_seal_keys(context
, db
, &entry
->entry
);
611 ret
= hdb_entry2value(context
, &entry
->entry
, &value
);
616 ret
= bind_principal(context
, entry
->entry
.principal
, get_ids
, 1);
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
;
633 if (ret
== SQLITE_CONSTRAINT
) {
634 ret
= HDB_ERR_EXISTS
;
638 ret
= bind_principal(context
, entry
->entry
.principal
, hsdb
->add_principal
, 1);
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
;
649 if (ret
== SQLITE_CONSTRAINT
) {
650 ret
= HDB_ERR_EXISTS
;
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
;
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 */
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
;
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
;
689 ret
= HDB_ERR_UK_SERROR
;
693 ret
= hdb_entry_get_aliases(&entry
->entry
, &aliases
);
694 if(ret
|| aliases
== NULL
)
697 for(i
= 0; i
< aliases
->aliases
.len
; i
++) {
699 ret
= bind_principal(context
, &aliases
->aliases
.val
[i
], hsdb
->add_alias
, 1);
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
;
709 if (ret
!= SQLITE_DONE
) {
710 ret
= HDB_ERR_UK_SERROR
;
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);
725 ret
= hdb_sqlite_exec_stmt(context
, hsdb
, "COMMIT", HDB_ERR_UK_SERROR
);
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
;
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);
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
)
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
771 * @return Always returns 0
773 static krb5_error_code
774 hdb_sqlite_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
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
)
793 ret
= hdb_clear_master_key(context
, db
);
795 ret2
= hdb_sqlite_close_database(context
, db
);
797 hsdb
= (hdb_sqlite_db
*)(db
->hdb_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
,
835 krb5_error_code ret
= 0;
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
) {
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
);
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
));
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
,
873 hdb_sqlite_db
*hsdb
= (hdb_sqlite_db
*) db
->hdb_db
;
876 sqlite3_reset(hsdb
->get_all_entries
);
878 ret
= hdb_sqlite_nextkey(context
, db
, flags
, entry
);
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)
899 ret
= hdb_sqlite_close_database(context
, db
);
901 if (rename(hsdb
->db_file
, new_name
) == -1)
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
)
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",
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
));
935 if ((flags
& HDB_F_PRECHECK
)) {
936 ret
= bind_principal(context
, principal
, get_ids
, 1);
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
);
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
);
959 if ((flags
& HDB_F_PRECHECK
)) {
960 (void) hdb_sqlite_exec_stmt(context
, hsdb
, "ROLLBACK", 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
));
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
983 hdb_sqlite_create(krb5_context context
, HDB
**db
, const char *filename
)
988 *db
= calloc(1, sizeof (**db
));
990 return krb5_enomem(context
);
992 (*db
)->hdb_name
= strdup(filename
);
993 if ((*db
)->hdb_name
== NULL
) {
996 return krb5_enomem(context
);
999 hsdb
= (hdb_sqlite_db
*) calloc(1, sizeof (*hsdb
));
1001 free((*db
)->hdb_name
);
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
);
1012 free((*db
)->hdb_db
);
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
;