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);
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
;
513 ret
= HDB_ERR_UK_RERROR
;
514 krb5_set_error_message(context
, ret
,
515 "sqlite fetch failed: %d",
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
);
528 if (db
->hdb_master_key_set
&& (flags
& HDB_F_DECRYPT
)) {
529 ret
= hdb_unseal_keys(context
, db
, &entry
->entry
);
531 hdb_free_entry(context
, entry
);
540 sqlite3_clear_bindings(fetch
);
541 sqlite3_reset(fetch
);
548 * Convenience function to step a prepared statement with no
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
)
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
);
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
,
587 sqlite_int64 entry_id
;
588 const HDB_Ext_Aliases
*aliases
;
590 hdb_sqlite_db
*hsdb
= (hdb_sqlite_db
*)(db
->hdb_db
);
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",
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
));
607 ret
= hdb_seal_keys(context
, db
, &entry
->entry
);
612 ret
= hdb_entry2value(context
, &entry
->entry
, &value
);
617 ret
= bind_principal(context
, entry
->entry
.principal
, get_ids
, 1);
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
;
634 if (ret
== SQLITE_CONSTRAINT
) {
635 ret
= HDB_ERR_EXISTS
;
640 ret
= bind_principal(context
, entry
->entry
.principal
, hsdb
->add_principal
, 1);
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
;
651 if (ret
== SQLITE_CONSTRAINT
) {
652 ret
= HDB_ERR_EXISTS
;
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
;
664 entry_id
= sqlite3_column_int64(get_ids
, 1);
668 } else if(ret
== SQLITE_ROW
) { /* Found a principal */
670 if(! (flags
& HDB_F_REPLACE
)) /* Not allowed to replace it */
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
;
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
;
693 ret
= HDB_ERR_UK_SERROR
;
697 ret
= hdb_entry_get_aliases(&entry
->entry
, &aliases
);
698 if(ret
|| aliases
== NULL
)
701 for(i
= 0; i
< aliases
->aliases
.len
; i
++) {
703 ret
= bind_principal(context
, &aliases
->aliases
.val
[i
], hsdb
->add_alias
, 1);
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
;
713 if (ret
!= SQLITE_DONE
) {
714 ret
= HDB_ERR_UK_SERROR
;
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);
729 ret
= hdb_sqlite_exec_stmt(context
, hsdb
, "COMMIT", HDB_ERR_UK_SERROR
);
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
;
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);
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
)
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
775 * @return Always returns 0
777 static krb5_error_code
778 hdb_sqlite_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
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
)
797 ret
= hdb_clear_master_key(context
, db
);
799 ret2
= hdb_sqlite_close_database(context
, db
);
801 hsdb
= (hdb_sqlite_db
*)(db
->hdb_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
,
839 krb5_error_code ret
= 0;
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
) {
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
);
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
));
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
,
877 hdb_sqlite_db
*hsdb
= (hdb_sqlite_db
*) db
->hdb_db
;
880 sqlite3_reset(hsdb
->get_all_entries
);
882 ret
= hdb_sqlite_nextkey(context
, db
, flags
, entry
);
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)
903 ret
= hdb_sqlite_close_database(context
, db
);
905 if (rename(hsdb
->db_file
, new_name
) == -1)
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
)
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",
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
));
939 if ((flags
& HDB_F_PRECHECK
)) {
940 ret
= bind_principal(context
, principal
, get_ids
, 1);
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
);
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
);
963 if ((flags
& HDB_F_PRECHECK
)) {
964 (void) hdb_sqlite_exec_stmt(context
, hsdb
, "ROLLBACK", 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
));
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
987 hdb_sqlite_create(krb5_context context
, HDB
**db
, const char *filename
)
992 *db
= calloc(1, sizeof (**db
));
994 return krb5_enomem(context
);
996 (*db
)->hdb_name
= strdup(filename
);
997 if ((*db
)->hdb_name
== NULL
) {
1000 return krb5_enomem(context
);
1003 hsdb
= (hdb_sqlite_db
*) calloc(1, sizeof (*hsdb
));
1005 free((*db
)->hdb_name
);
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
);
1016 free((*db
)->hdb_db
);
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
;