static krb5_error_code prep_stmts(krb5_context context, hdb_sqlite_db *hsdb) { int ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_version, HDBSQLITE_GET_VERSION); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->fetch, HDBSQLITE_FETCH); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_ids, HDBSQLITE_GET_IDS); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_entry, HDBSQLITE_ADD_ENTRY); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_principal, HDBSQLITE_ADD_PRINCIPAL); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_alias, HDBSQLITE_ADD_ALIAS); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->delete_aliases, HDBSQLITE_DELETE_ALIASES); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->update_entry, HDBSQLITE_UPDATE_ENTRY); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->remove, HDBSQLITE_REMOVE); if (ret) return ret; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_all_entries, HDBSQLITE_GET_ALL_ENTRIES); return ret; }
/** * Opens an sqlite database file and prepares it for use. * If the file does not exist it will be created. * * @param context The current krb5_context * @param db The heimdal database handle * @param filename Where to store the database file * * @return 0 if everything worked, an error code if not */ static krb5_error_code hdb_sqlite_make_database(krb5_context context, HDB *db, const char *filename) { int ret; int created_file = 0; hdb_sqlite_db *hsdb = (hdb_sqlite_db *) db->hdb_db; hsdb->db_file = strdup(filename); if(hsdb->db_file == NULL) return ENOMEM; ret = hdb_sqlite_open_database(context, db, 0); if (ret) { ret = hdb_sqlite_open_database(context, db, SQLITE_OPEN_CREATE); if (ret) goto out; created_file = 1; ret = hdb_sqlite_exec_stmt(context, hsdb->db, HDBSQLITE_CREATE_TABLES, EINVAL); if (ret) goto out; ret = hdb_sqlite_exec_stmt(context, hsdb->db, HDBSQLITE_CREATE_TRIGGERS, EINVAL); if (ret) goto out; } ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_version, HDBSQLITE_GET_VERSION); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->fetch, HDBSQLITE_FETCH); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_ids, HDBSQLITE_GET_IDS); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_entry, HDBSQLITE_ADD_ENTRY); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_principal, HDBSQLITE_ADD_PRINCIPAL); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->add_alias, HDBSQLITE_ADD_ALIAS); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->delete_aliases, HDBSQLITE_DELETE_ALIASES); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->update_entry, HDBSQLITE_UPDATE_ENTRY); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->remove, HDBSQLITE_REMOVE); if (ret) goto out; ret = hdb_sqlite_prepare_stmt(context, hsdb->db, &hsdb->get_all_entries, HDBSQLITE_GET_ALL_ENTRIES); if (ret) goto out; ret = hdb_sqlite_step(context, hsdb->db, hsdb->get_version); if(ret == SQLITE_ROW) { hsdb->version = sqlite3_column_double(hsdb->get_version, 0); } sqlite3_reset(hsdb->get_version); ret = 0; if(hsdb->version != HDBSQLITE_VERSION) { krb5_set_error_string(context, "HDBSQLITE_VERSION mismatch"); ret = EINVAL; } if(ret) goto out; return 0; out: if (hsdb->db) sqlite3_close(hsdb->db); if (created_file) unlink(hsdb->db_file); return ret; }