You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

5562 lines
184 KiB

// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "pEp_internal.h"
#include "dynamic_api.h"
#include "cryptotech.h"
#include "transport.h"
#include "blacklist.h"
#include "KeySync_fsm.h"
#include <time.h>
#include <stdlib.h>
#ifdef _PEP_SQLITE_DEBUG
#include <sqlite3.h>
#endif
static volatile int init_count = -1;
// sql overloaded functions - modified from sqlite3.c
static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
const char *z2;
int n;
z2 = (char*)sqlite3_value_text(argv[0]);
n = sqlite3_value_bytes(argv[0]);
/* Verify that the call to _bytes() does not invalidate the _text() pointer */
assert( z2==(char*)sqlite3_value_text(argv[0]) );
if( z2 ){
char *z1 = (char*)sqlite3_malloc(n+1);
if( z1 ){
for(int i=0; i<n; i++){
char c = z2[i];
char c_mod = c | 0x20;
if (c_mod < 0x61 || c_mod > 0x7a)
c_mod = c;
z1[i] = c_mod;
}
z1[n] = '\0';
sqlite3_result_text(ctx, z1, n, sqlite3_free);
}
}
}
#ifdef _PEP_SQLITE_DEBUG
int sql_trace_callback (unsigned trace_constant,
void* context_ptr,
void* P,
void* X) {
switch (trace_constant) {
case SQLITE_TRACE_STMT:
fprintf(stderr, "SQL_DEBUG: STMT - ");
const char* X_str = (const char*) X;
if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
fprintf(stderr, "%s\n", X_str);
else
fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
break;
case SQLITE_TRACE_ROW:
fprintf(stderr, "SQL_DEBUG: ROW - ");
fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
break;
case SQLITE_TRACE_CLOSE:
fprintf(stderr, "SQL_DEBUG: CLOSE - ");
break;
default:
break;
}
return 0;
}
#endif
// sql manipulation statements
static const char *sql_log =
"insert into log (title, entity, description, comment)"
"values (?1, ?2, ?3, ?4);";
static const char *sql_trustword =
"select id, word from wordlist where lang = lower(?1) "
"and id = ?2 ;";
// FIXME?: problems if we don't have a key for the user - we get nothing
static const char *sql_get_identity =
"select fpr, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
" join trust on id = trust.user_id"
" and pgp_keypair_fpr = identity.main_key_id"
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
" else 0"
" end) = 1"
" and identity.user_id = ?2"
" order by is_own desc, "
" timestamp desc; ";
static const char *sql_get_identities_by_main_key_id =
"select address, identity.user_id, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
" join trust on id = trust.user_id"
" and pgp_keypair_fpr = identity.main_key_id"
" where identity.main_key_id = ?1"
" order by is_own desc, "
" timestamp desc; ";
static const char *sql_get_identity_without_trust_check =
"select identity.main_key_id, username, lang,"
" identity.flags, is_own, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
" else 0"
" end) = 1"
" and identity.user_id = ?2 "
" order by is_own desc, "
" timestamp desc; ";
static const char *sql_get_identities_by_address =
"select user_id, identity.main_key_id, username, lang,"
" identity.flags, is_own, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
" else 0"
" end) = 1 "
" order by is_own desc, "
" timestamp desc; ";
static const char *sql_get_identities_by_userid =
"select address, fpr, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
" join trust on id = trust.user_id"
" and pgp_keypair_fpr = identity.main_key_id"
" where identity.user_id = ?1"
" order by is_own desc, "
" timestamp desc; ";
static const char *sql_replace_identities_fpr =
"update identity"
" set main_key_id = ?1 "
" where main_key_id = ?2 ;";
static const char *sql_remove_fpr_as_default =
"update person set main_key_id = NULL where main_key_id = ?1 ;"
"update identity set main_key_id = NULL where main_key_id = ?1 ;";
// Set person, but if already exist, only update.
// if main_key_id already set, don't touch.
static const char *sql_set_person =
"insert into person (id, username, lang, main_key_id)"
" values (?1, ?2, ?3, ?4) ;";
static const char *sql_update_person =
"update person "
" set username = ?2, "
" lang = ?3, "
" main_key_id = "
" (select coalesce( "
" (select main_key_id from person where id = ?1), "
" upper(replace(?4,' ',''))))"
" where id = ?1 ;";
// Will cascade.
static const char *sql_delete_person =
"delete from person where id = ?1 ;";
static const char *sql_set_as_pEp_user =
"update person set is_pEp_user = 1 "
" where id = ?1 ; ";
static const char *sql_is_pEp_user =
"select is_pEp_user from person "
" where id = ?1 ; ";
static const char* sql_exists_person =
"select count(*) from person "
" where id = ?1 ;";
// This will cascade to identity and trust
static const char* sql_replace_userid =
"update person set id = ?1 "
" where id = ?2;";
// Hopefully this cascades and removes trust entries...
static const char *sql_delete_key =
"delete from pgp_keypair "
" where fpr = ?1 ; ";
static const char *sql_replace_main_user_fpr =
"update person "
" set main_key_id = ?1 "
" where id = ?2 ;";
static const char *sql_get_main_user_fpr =
"select main_key_id from person"
" where id = ?1 ;";
static const char *sql_refresh_userid_default_key =
"update person "
" set main_key_id = "
" (select identity.main_key_id from identity "
" join trust on trust.user_id = identity.user_id "
" and trust.pgp_keypair_fpr = identity.main_key_id "
" join person on identity.user_id = identity.user_id "
" where identity.user_id = ?1 "
" order by trust.comm_type desc "
" limit 1) "
"where id = ?1 ; ";
static const char *sql_set_pgp_keypair =
"insert or ignore into pgp_keypair (fpr) "
"values (upper(replace(?1,' ',''))) ;";
static const char* sql_exists_identity_entry =
"select count(*) from identity "
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
" else 0"
" end) = 1"
" and user_id = ?2;";
static const char *sql_set_identity_entry =
"insert into identity ("
" address, main_key_id, "
" user_id, flags, is_own,"
" pEp_version_major, pEp_version_minor"
" ) values ("
" ?1,"
" upper(replace(?2,' ','')),"
" ?3,"
" ?4,"
" ?5,"
" ?6,"
" ?7"
" );";
static const char* sql_update_identity_entry =
"update identity "
" set main_key_id = upper(replace(?2,' ','')), "
" flags = ?4, "
" is_own = ?5, "
" pEp_version_major = ?6, "
" pEp_version_minor = ?7 "
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
" else 0 "
" end) = 1 "
" and user_id = ?3 ;";
// " (select"
// " coalesce("
// " (select flags from identity"
// " where address = ?1 and"
// " user_id = ?3),"
// " 0)"
// " ) | (?4 & 255)"
/* set_identity ignores previous flags, and doesn't filter machine flags */
static const char *sql_set_identity_flags =
"update identity set flags = "
" ((?1 & 65535) | (select flags from identity"
" where (case when (address = ?2) then (1)"
" when (lower(address) = lower(?2)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
" else 0 "
" end) = 1 "
" and user_id = ?3)) "
" where (case when (address = ?2) then (1)"
" when (lower(address) = lower(?2)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
" else 0"
" end) = 1"
" and user_id = ?3 ;";
static const char *sql_unset_identity_flags =
"update identity set flags = "
" ( ~(?1 & 65535) & (select flags from identity"
" where (case when (address = ?2) then (1)"
" when (lower(address) = lower(?2)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
" else 0 "
" end) = 1 "
" and user_id = ?3)) "
" where (case when (address = ?2) then (1)"
" when (lower(address) = lower(?2)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
" else 0"
" end) = 1"
" and user_id = ?3 ;";
static const char *sql_set_pEp_version =
"update identity "
" set pEp_version_major = ?1, "
" pEp_version_minor = ?2 "
" where (case when (address = ?3) then (1)"
" when (lower(address) = lower(?3)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
" else 0 "
" end) = 1 "
" and user_id = ?4 ;";
static const char *sql_upgrade_pEp_version_by_user_id =
"update identity "
" set pEp_version_major = ?1, "
" pEp_version_minor = ?2 "
" where user_id = ?3 "
" and (case when (pEp_version_major < ?1) then (1)"
" when (pEp_version_major > ?1) then (0)"
" when (pEp_version_minor < ?2) then (1)"
" else 0 "
" end) = 1 ;";
static const char *sql_set_trust =
"insert into trust (user_id, pgp_keypair_fpr, comm_type) "
"values (?1, upper(replace(?2,' ','')), ?3) ;";
static const char *sql_update_trust =
"update trust set comm_type = ?3 "
" where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
static const char *sql_clear_trust_info =
"delete from trust "
" where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
static const char *sql_update_trust_to_pEp =
"update trust set comm_type = comm_type + 71 "
" where (user_id = ?1 "
" and (case when (comm_type = 56) then (1) "
" when (comm_type = 184) then (1) "
" else 0"
" end) = 1); ";
static const char* sql_exists_trust_entry =
"select count(*) from trust "
" where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
static const char *sql_update_trust_for_fpr =
"update trust "
"set comm_type = ?1 "
"where pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
static const char *sql_get_trust =
"select comm_type from trust where user_id = ?1 "
"and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
static const char *sql_get_trust_by_userid =
"select pgp_keypair_fpr, comm_type from trust where user_id = ?1 ";
static const char *sql_least_trust =
"select min(comm_type) from trust where"
" pgp_keypair_fpr = upper(replace(?1,' ',''))"
" and comm_type != 0;"; // ignores PEP_ct_unknown
// returns PEP_ct_unknown only when no known trust is recorded
static const char *sql_mark_as_compromised =
"update trust not indexed set comm_type = 15"
" where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
static const char *sql_crashdump =
"select timestamp, title, entity, description, comment"
" from log order by timestamp desc limit ?1 ;";
static const char *sql_languagelist =
"select i18n_language.lang, name, phrase"
" from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
static const char *sql_i18n_token =
"select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
// blacklist
static const char *sql_blacklist_add =
"insert or ignore into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
"delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
"delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
static const char *sql_blacklist_delete =
"delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
static const char *sql_blacklist_is_listed =
"select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
static const char *sql_blacklist_retrieve =
"select * from blacklist_keys ;";
// Own keys
// We only care if it's 0 or non-zero
static const char *sql_own_key_is_listed =
"select count(*) from ("
" select pgp_keypair_fpr from trust"
" join identity on trust.user_id = identity.user_id"
" where pgp_keypair_fpr = upper(replace(?1,' ',''))"
" and identity.is_own = 1"
");";
static const char *sql_is_own_address =
"select count(*) from ("
" select address from identity"
" where (case when (address = ?1) then (1)"
" when (lower(address) = lower(?1)) then (1)"
" when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
" else 0"
" end) = 1 "
" and identity.is_own = 1"
");";
static const char *sql_own_identities_retrieve =
"select address, fpr, identity.user_id, username,"
" lang, identity.flags | pgp_keypair.flags, pEp_version_major, pEp_version_minor"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
" join trust on id = trust.user_id"
" and pgp_keypair_fpr = identity.main_key_id"
" where identity.is_own = 1"
" and (identity.flags & ?1) = 0;";
static const char *sql_own_keys_retrieve =
"select pgp_keypair_fpr from trust"
" join identity on trust.user_id = identity.user_id"
" where identity.is_own = 1";
static const char* sql_get_user_default_key =
"select main_key_id from person"
" where id = ?1;";
static const char* sql_get_all_keys_for_user =
"select pgp_keypair_fpr from trust"
" where user_id = ?1; ";
static const char* sql_get_default_own_userid =
"select id from person"
" join identity on id = identity.user_id"
" where identity.is_own = 1";
// Sequence
static const char *sql_sequence_value1 =
"insert or replace into sequences (name, value) "
"values (?1, "
" (select coalesce((select value + 1 from sequences "
" where name = ?1), 1 ))); ";
static const char *sql_sequence_value2 =
"select value from sequences where name = ?1 ;";
// Revocation tracking
static const char *sql_set_revoked =
"insert or replace into revoked_keys ("
" revoked_fpr, replacement_fpr, revocation_date) "
"values (upper(replace(?1,' ','')),"
" upper(replace(?2,' ','')),"
" ?3) ;";
static const char *sql_get_revoked =
"select revoked_fpr, revocation_date from revoked_keys"
" where replacement_fpr = upper(replace(?1,' ','')) ;";
static const char *sql_get_replacement_fpr =
"select replacement_fpr, revocation_date from revoked_keys"
" where revoked_fpr = upper(replace(?1,' ','')) ;";
static const char *sql_get_userid_alias_default =
"select default_id from alternate_user_id "
" where alternate_id = ?1 ; ";
// Revocation tracking
static const char *sql_add_mistrusted_key =
"insert or replace into mistrusted_keys (fpr) "
" values (upper(replace(?1,' ',''))) ;";
static const char *sql_delete_mistrusted_key =
"delete from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
static const char *sql_is_mistrusted_key =
"select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
static const char *sql_add_userid_alias =
"insert or replace into alternate_user_id (alternate_id, default_id) "
"values (?2, ?1) ;";
static const char *sql_add_into_social_graph =
"insert or replace into social_graph(own_userid, own_address, contact_userid) "
"values (?1, ?2, ?3) ;";
static const char *sql_get_own_address_binding_from_contact =
"select own_address from social_graph where own_userid = ?1 and contact_userid = ?2 ;";
static const char *sql_set_revoke_contact_as_notified =
"insert or replace into revocation_contact_list(fpr, own_address, contact_id) values (?1, ?2, ?3) ;";
static const char *sql_get_contacted_ids_from_revoke_fpr =
"select * from revocation_contact_list where fpr = ?1 ;";
static const char *sql_was_id_for_revoke_contacted =
"select count(*) from revocation_contact_list where fpr = ?1 and own_address = ?2 and contact_id = ?3 ;";
static const char *sql_has_id_contacted_address =
"select count(*) from social_graph where own_address = ?1 and contact_userid = ?2 ;";
// We only need user_id and address, since in the main usage, we'll call update_identity
// on this anyway when sending out messages.
static const char *sql_get_last_contacted =
"select user_id, address from identity where datetime('now') < datetime(timestamp, '+14 days') ; ";
static int user_version(void *_version, int count, char **text, char **name)
{
assert(_version);
assert(count == 1);
assert(text && text[0]);
if (!(_version && count == 1 && text && text[0]))
return -1;
int *version = (int *) _version;
*version = atoi(text[0]);
return 0;
}
// TODO: refactor and generalise these two functions if possible.
static int db_contains_table(PEP_SESSION session, const char* table_name) {
if (!session || !table_name)
return -1;
// Table names can't be SQL parameters, so we do it this way.
// these two must be the same number.
char sql_buf[500];
const size_t max_q_len = 500;
size_t t_size, q_size;
const char* q1 = "SELECT name FROM sqlite_master WHERE type='table' AND name='{"; // 61
const char* q2 = "}';"; // 3
q_size = 64;
t_size = strlen(table_name);
size_t query_len = q_size + t_size + 1;
if (query_len > max_q_len)
return -1;
strlcpy(sql_buf, q1, max_q_len);
strlcat(sql_buf, table_name, max_q_len);
strlcat(sql_buf, q2, max_q_len);
sqlite3_stmt *stmt;
sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
int retval = 0;
int rc = sqlite3_step(stmt);
if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
retval = 1;
}
sqlite3_finalize(stmt);
return retval;
}
static int table_contains_column(PEP_SESSION session, const char* table_name,
const char* col_name) {
if (!session || !table_name || !col_name)
return -1;
// Table names can't be SQL parameters, so we do it this way.
// these two must be the same number.
char sql_buf[500];
const size_t max_q_len = 500;
size_t t_size, c_size, q_size;
const char* q1 = "SELECT "; // 7
const char* q2 = " from "; // 6
const char* q3 = ";"; // 1
q_size = 14;
t_size = strlen(table_name);
c_size = strlen(col_name);
size_t query_len = q_size + c_size + t_size + 1;
if (query_len > max_q_len)
return -1;
strlcpy(sql_buf, q1, max_q_len);
strlcat(sql_buf, col_name, max_q_len);
strlcat(sql_buf, q2, max_q_len);
strlcat(sql_buf, table_name, max_q_len);
strlcat(sql_buf, q3, max_q_len);
sqlite3_stmt *stmt;
sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
int retval = 0;
int rc = sqlite3_step(stmt);
if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
retval = 1;
}
sqlite3_finalize(stmt);
return retval;
}
PEP_STATUS repair_altered_tables(PEP_SESSION session) {
PEP_STATUS status = PEP_STATUS_OK;
const int _PEP_MAX_AFFECTED = 5;
char** table_names = calloc(_PEP_MAX_AFFECTED, sizeof(char*));
if (!table_names)
return PEP_OUT_OF_MEMORY;
const char* sql_query = "select tbl_name from sqlite_master WHERE sql LIKE '%REFERENCES%' AND sql LIKE '%_old%';";
sqlite3_stmt *stmt;
sqlite3_prepare_v2(session->db, sql_query, -1, &stmt, NULL);
int i = 0;
int int_result = 0;
while ((int_result = sqlite3_step(stmt)) == SQLITE_ROW && i < _PEP_MAX_AFFECTED) {
table_names[i++] = strdup((const char*)(sqlite3_column_text(stmt, 0)));
}
sqlite3_finalize(stmt);
if ((int_result != SQLITE_DONE && int_result != SQLITE_OK) || i > (_PEP_MAX_AFFECTED + 1)) {
status = PEP_UNKNOWN_DB_ERROR;
goto pEp_free;
}
for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
const char* table_name = table_names[i];
if (!table_name)
break;
if (strcmp(table_name, "identity") == 0) {
int_result = sqlite3_exec(session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _identity_new (\n"
" address text,\n"
" user_id text\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" comment text,\n"
" flags integer default 0,\n"
" is_own integer default 0,\n"
" timestamp integer default (datetime('now')),\n"
" primary key (address, user_id)\n"
");\n"
"INSERT INTO _identity_new SELECT * from identity;\n"
"DROP TABLE identity;\n"
"ALTER TABLE _identity_new RENAME TO identity;\n"
"COMMIT;\n"
"PRAGMA foreign_keys=on;"
,
NULL,
NULL,
NULL
);
assert(int_result == PEP_STATUS_OK);
}
else if (strcmp(table_name, "trust") == 0) {
int_result = sqlite3_exec(session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _trust_new (\n"
" user_id text not null\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" pgp_keypair_fpr text not null\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" comm_type integer not null,\n"
" comment text,\n"
" primary key (user_id, pgp_keypair_fpr)\n"
");\n"
"INSERT INTO _trust_new SELECT * from trust;\n"
"DROP TABLE trust;\n"
"ALTER TABLE _trust_new RENAME TO trust;\n"
"COMMIT;\n"
"PRAGMA foreign_keys=on;"
,
NULL,
NULL,
NULL
);
assert(int_result == PEP_STATUS_OK);
}
else if (strcmp(table_name, "alternate_user_id") == 0) {
int_result = sqlite3_exec(session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _alternate_user_id_new (\n"
" default_id text references person (id)\n"
" on delete cascade on update cascade,\n"
" alternate_id text primary key\n"
");\n"
"INSERT INTO _alternate_user_id_new SELECT * from alternate_user_id;\n"
"DROP TABLE alternate_user_id;\n"
"ALTER TABLE _alternate_user_id_new RENAME TO alternate_user_id;\n"
"COMMIT;\n"
"PRAGMA foreign_keys=on;"
,
NULL,
NULL,
NULL
);
assert(int_result == PEP_STATUS_OK);
}
else if (strcmp(table_name, "revocation_contact_list") == 0) {
int_result = sqlite3_exec(session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _revocation_contact_list_new (\n"
" fpr text not null references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" contact_id text not null references person (id)\n"
" on delete cascade on update cascade,\n"
" timestamp integer default (datetime('now')),\n"
" PRIMARY KEY(fpr, contact_id)\n"
");\n"
"INSERT INTO _revocation_contact_list_new SELECT * from revocation_contact_list;\n"
"DROP TABLE revocation_contact_list;\n"
"ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
"COMMIT;\n"
"PRAGMA foreign_keys=on;"
,
NULL,
NULL,
NULL
);
assert(int_result == PEP_STATUS_OK);
}
else if (strcmp(table_name, "social_graph")) {
int_result = sqlite3_exec(session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _social_new (\n"
" own_userid text,\n"
" own_address text,\n"
" contact_userid text,\n"
" CONSTRAINT fk_own_identity\n"
" FOREIGN KEY(own_address, own_userid)\n"
" REFERENCES identity(address, user_id)\n"
" ON DELETE CASCADE ON UPDATE CASCADE\n"
");\n"
"INSERT INTO _social_graph_new SELECT * from social_graph;\n"
"DROP TABLE social_graph;\n"
"ALTER TABLE _social_graph_new RENAME TO social_graph;\n"
"COMMIT;\n"
"PRAGMA foreign_keys=on;"
,
NULL,
NULL,
NULL
);
assert(int_result == PEP_STATUS_OK);
}
}
int_result = sqlite3_exec(
session->db,
"PRAGMA foreign_key_check;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
pEp_free:
for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
if (table_names[i])
free(table_names[i]);
else
break;
}
free(table_names);
return status;
}
void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
}
static PEP_STATUS upgrade_revoc_contact_to_13(PEP_SESSION session) {
// I HATE SQLITE.
PEP_STATUS status = PEP_STATUS_OK;
int int_result = 0;
// Ok, first we ADD the column so we can USE it.
// We will end up propagating the "error" this first time
// (one-to-one revoke-replace relationships), but since key reset
// hasn't been used in production, this is not a customer-facing
// issue.
int_result = sqlite3_exec(
session->db,
"alter table revocation_contact_list\n"
" add column own_address text\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
sqlite3_stmt* update_revoked_w_addr_stmt = NULL;
const char* sql_query = "update revocation_contact_list set own_address = ?1 where fpr = ?2;";
sqlite3_prepare_v2(session->db, sql_query, -1, &update_revoked_w_addr_stmt, NULL);
// the best we can do here is search per address, since these
// are no longer associated with an identity. For now, if we find
// something we can't add an address to, we'll delete the record.
// this should not, in the current environment, ever happen, but
// since we need to make the address part of the primary key, it's
// the right thing to do. sqlite does support null fields in a primary
// key for a weird version compatibility reason, but that doesn't
// mean we should use it, and we should be *safe*, not relying
// on an implementation-specific quirk which might be sanely removed
// in a future sqlite version.
stringpair_t* revoked_key_to_own_address = NULL;
identity_list* id_list = NULL;
status = own_identities_retrieve(session, &id_list);
if (!status || !id_list)
return PEP_STATUS_OK; // it's empty AFAIK (FIXME)
identity_list* curr_own = id_list;
// Ok, go through and find any keys associated with this address
for ( ; curr_own && curr_own->ident; curr_own = curr_own->next) {
if (EMPTYSTR(curr_own->ident->address)) // shouldn't happen
continue;
stringlist_t* keylist = NULL;
status = find_keys(session, curr_own->ident->address, &keylist);
stringlist_t* curr_key = keylist;
for ( ; curr_key && curr_key->value; curr_key = curr_key->next) {
if (EMPTYSTR(curr_key->value))
continue;
// We just do this lazily - if this isn't a revoked key, it
// won't do anything.
sqlite3_bind_text(update_revoked_w_addr_stmt, 1, curr_own->ident->address, -1,
SQLITE_STATIC);
sqlite3_bind_text(update_revoked_w_addr_stmt, 2, curr_key->value, -1,
SQLITE_STATIC);
int_result = sqlite3_step(update_revoked_w_addr_stmt);
assert(int_result == SQLITE_DONE);
sqlite3_reset(update_revoked_w_addr_stmt);
}
}
sqlite3_finalize(update_revoked_w_addr_stmt);
int_result = sqlite3_exec(
session->db,
"delete from revocation_contact_list where own_address is NULL;\n"
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table if not exists _revocation_contact_list_new (\n"
" fpr text not null references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" own_address text,\n"
" contact_id text not null references person (id)\n"
" on delete cascade on update cascade,\n"
" timestamp integer default (datetime('now')),\n"
" PRIMARY KEY(fpr, own_address, contact_id)\n"
");\n"
"INSERT INTO _revocation_contact_list_new (fpr, "
" own_address, "
" contact_id) "
" SELECT revocation_contact_list.fpr, "
" revocation_contact_list.own_address, "
" revocation_contact_list.contact_id "
" FROM revocation_contact_list "
" WHERE 1;\n"
"DROP TABLE revocation_contact_list;\n"
"ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
"COMMIT;\n"
"\n"
"PRAGMA foreign_keys=on;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
return status;
}
#ifdef USE_GPG
PEP_STATUS pgp_import_ultimately_trusted_keypairs(PEP_SESSION session);
#endif // USE_GPG
DYNAMIC_API PEP_STATUS init(
PEP_SESSION *session,
messageToSend_t messageToSend,
inject_sync_event_t inject_sync_event
)
{
PEP_STATUS status = PEP_STATUS_OK;
int int_result;
bool in_first = false;
bool very_first = false;
assert(sqlite3_threadsafe());
if (!sqlite3_threadsafe())
return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
// a little race condition - but still a race condition
// mitigated by calling caveat (see documentation)
// this increment is made atomic IN THE ADAPTERS by
// guarding the call to init with the appropriate mutex.
int _count = ++init_count;
if (_count == 0)
in_first = true;
// Race condition mitigated by calling caveat starts here :
// If another call to init() preempts right now, then preemptive call
// will have in_first false, will not create SQL tables, and following
// calls relying on those tables will fail.
//
// Therefore, as above, adapters MUST guard init() with a mutex.
//
// Therefore, first session
// is to be created and last session to be deleted alone, and not
// concurently to other sessions creation or deletion.
// We expect adapters to enforce this either by implicitely creating a
// client session, or by using synchronization primitive to protect
// creation/deletion of first/last session from the app.
assert(session);
if (session == NULL)
return PEP_ILLEGAL_VALUE;
*session = NULL;
pEpSession *_session = calloc(1, sizeof(pEpSession));
assert(_session);
if (_session == NULL)
goto enomem;
_session->version = PEP_ENGINE_VERSION;
_session->messageToSend = messageToSend;
_session->inject_sync_event = inject_sync_event;
#ifdef DEBUG_ERRORSTACK
_session->errorstack = new_stringlist("init()");
#endif
assert(LOCAL_DB);
if (LOCAL_DB == NULL) {
status = PEP_INIT_CANNOT_OPEN_DB;
goto pEp_error;
}
#ifdef _PEP_SQLITE_DEBUG
sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
#endif
int_result = sqlite3_open_v2(
LOCAL_DB,
&_session->db,
SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
| SQLITE_OPEN_FULLMUTEX
| SQLITE_OPEN_PRIVATECACHE,
NULL
);
if (int_result != SQLITE_OK) {
status = PEP_INIT_CANNOT_OPEN_DB;
goto pEp_error;
}
int_result = sqlite3_exec(
_session->db,
"PRAGMA locking_mode=NORMAL;\n"
"PRAGMA journal_mode=WAL;\n",
NULL,
NULL,
NULL
);
sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
#ifdef _PEP_SQLITE_DEBUG
sqlite3_trace_v2(_session->db,
SQLITE_TRACE_STMT | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
sql_trace_callback,
NULL);
#endif
assert(SYSTEM_DB);
if (SYSTEM_DB == NULL) {
status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
goto pEp_error;
}
int_result = sqlite3_open_v2(
SYSTEM_DB, &_session->system_db,
SQLITE_OPEN_READONLY
| SQLITE_OPEN_FULLMUTEX
| SQLITE_OPEN_SHAREDCACHE,
NULL
);
if (int_result != SQLITE_OK) {
status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
goto pEp_error;
}
sqlite3_busy_timeout(_session->system_db, 1000);
// increment this when patching DDL
#define _DDL_USER_VERSION "13"
if (in_first) {
int_result = sqlite3_exec(
_session->db,
"create table if not exists version_info (\n"
" id integer primary key,\n"
" timestamp integer default (datetime('now')),\n"
" version text,\n"
" comment text\n"
");\n",
NULL,
NULL,
NULL
);
int_result = sqlite3_exec(
_session->db,
"PRAGMA application_id = 0x23423423;\n"
"create table if not exists log (\n"
" timestamp integer default (datetime('now')),\n"
" title text not null,\n"
" description text,\n"
" entity text not null,\n"
" comment text\n"
");\n"
"create index if not exists log_timestamp on log (\n"
" timestamp\n"
");\n"
"create table if not exists pgp_keypair (\n"
" fpr text primary key,\n"
" created integer,\n"
" expires integer,\n"
" comment text,\n"
" flags integer default 0\n"
");\n"
"create index if not exists pgp_keypair_expires on pgp_keypair (\n"
" expires\n"
");\n"
"create table if not exists person (\n"
" id text primary key,\n"
" username text not null,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" lang text,\n"
" comment text,\n"
// " device_group text,\n"
" is_pEp_user integer default 0\n"
");\n"
"create table if not exists identity (\n"
" address text,\n"
" user_id text\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" comment text,\n"
" flags integer default 0,\n"
" is_own integer default 0,\n"
" pEp_version_major integer default 0,\n"
" pEp_version_minor integer default 0,\n"
" timestamp integer default (datetime('now')),\n"
" primary key (address, user_id)\n"
");\n"
"create index if not exists identity_userid_addr on identity(address, user_id);\n"
"create table if not exists trust (\n"
" user_id text not null\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" pgp_keypair_fpr text not null\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" comm_type integer not null,\n"
" comment text,\n"
" primary key (user_id, pgp_keypair_fpr)\n"
");\n"
// blacklist
"create table if not exists blacklist_keys (\n"
" fpr text primary key\n"
");\n"
// sequences
"create table if not exists sequences(\n"
" name text primary key,\n"
" value integer default 0\n"
");\n"
"create table if not exists revoked_keys (\n"
" revoked_fpr text primary key,\n"
" replacement_fpr text not null\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" revocation_date integer\n"
");\n"
// user id aliases
"create table if not exists alternate_user_id (\n"
" default_id text references person (id)\n"
" on delete cascade on update cascade,\n"
" alternate_id text primary key\n"
");\n"
// mistrusted keys
"create table if not exists mistrusted_keys (\n"
" fpr text primary key\n"
");\n"
// social graph for key resets
"create table if not exists social_graph (\n"
" own_userid text,\n"
" own_address text,\n"
" contact_userid text,\n"
" CONSTRAINT fk_own_identity\n"
" FOREIGN KEY(own_address, own_userid)\n"
" REFERENCES identity(address, user_id)\n"
" ON DELETE CASCADE ON UPDATE CASCADE\n"
");\n"
// list of user_ids sent revocation
"create table if not exists revocation_contact_list (\n"
" fpr text not null references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" own_address text,\n"
" contact_id text not null references person (id)\n"
" on delete cascade on update cascade,\n"
" timestamp integer default (datetime('now')),\n"
" PRIMARY KEY(fpr, own_address, contact_id)\n"
");\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int version;
int_result = sqlite3_exec(
_session->db,
"pragma user_version;",
user_version,
&version,
NULL
);
assert(int_result == SQLITE_OK);
void (*xFunc_lower)(sqlite3_context*,int,sqlite3_value**) = &_sql_lower;
int_result = sqlite3_create_function_v2(
_session->db,
"lower",
1,
SQLITE_UTF8 | SQLITE_DETERMINISTIC,
NULL,
xFunc_lower,
NULL,
NULL,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"pragma foreign_keys=ON;\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
// Sometimes the user_version wasn't set correctly.
if (version == 1) {
bool version_changed = true;
if (table_contains_column(_session, "revocation_contact_list", "own_address")) {
version = 13;
}
else if (table_contains_column(_session, "identity", "pEp_version_major")) {
version = 12;
}
else if (db_contains_table(_session, "social_graph") > 0) {
if (!table_contains_column(_session, "person", "device_group"))
version = 10;
else
version = 9;
}
else if (table_contains_column(_session, "identity", "timestamp") > 0) {
version = 8;
}
else if (table_contains_column(_session, "person", "is_pEp_user") > 0) {
version = 7;
}
else if (table_contains_column(_session, "identity", "is_own") > 0) {
version = 6;
}
else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
version = 2;
}
else {
version_changed = false;
}
if (version_changed) {
// set it in the DB, finally. Yeesh.
char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon.
sprintf(verbuf,"%d",version);
size_t query_size = strlen(verbuf) + 25;
char* query = calloc(query_size, 1);
strlcpy(query, "pragma user_version = ", query_size);
strlcat(query, verbuf, query_size);
strlcat(query, ";", query_size);
int_result = sqlite3_exec(
_session->db,
query,
user_version,
&version,
NULL
);
free(query);
}
}
if(version != 0) {
// Version has been already set
// Early mistake : version 0 shouldn't have existed.
// Numbering should have started at 1 to detect newly created DB.
// Version 0 DBs are not anymore compatible.
// // Was version 0 compat code.
// if (version < 1) {
// int_result = sqlite3_exec(
// _session->db,
// "alter table identity\n"
// " add column flags integer default 0;\n",
// NULL,
// NULL,
// NULL
// );
// assert(int_result == SQLITE_OK);
// }
if (version < 2) {
// N.B. addition of device_group column removed in DDL v10
int_result = sqlite3_exec(
_session->db,
"alter table pgp_keypair\n"
" add column flags integer default 0;\n",
// "alter table person\n"
// " add column device_group text;\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
if (version < 5) {
int_result = sqlite3_exec(
_session->db,
"delete from pgp_keypair where fpr = '';",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"delete from trust where pgp_keypair_fpr = '';",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
if (version < 6) {
int_result = sqlite3_exec(
_session->db,
"alter table identity\n"
" add column is_own integer default 0;\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"update identity\n"
" set is_own = 1\n"
" where (user_id = '" PEP_OWN_USERID "');\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
// Turns out that just adding "on update cascade" in
// sqlite is a PITA. We need to be able to cascade
// person->id replacements (for temp ids like "TOFU_")
// so here we go...
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _identity_new (\n"
" address text,\n"
" user_id text\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" comment text,\n"
" flags integer default 0,\n"
" is_own integer default 0,\n"
" primary key (address, user_id)\n"
");\n"
"INSERT INTO _identity_new SELECT * FROM identity;\n"
"DROP TABLE identity;\n"
"ALTER TABLE _identity_new RENAME TO identity;\n"
"COMMIT;\n"
"\n"
"BEGIN TRANSACTION;\n"
"create table _trust_new (\n"
" user_id text not null\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" pgp_keypair_fpr text not null\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" comm_type integer not null,\n"
" comment text,\n"
" primary key (user_id, pgp_keypair_fpr)\n"
");\n"
"INSERT INTO _trust_new SELECT * FROM trust;\n"
"DROP TABLE trust;\n"
"ALTER TABLE _trust_new RENAME TO trust;\n"
"COMMIT;\n"
"\n"
"PRAGMA foreign_keys=on;\n"
"create table if not exists alternate_user_id (\n"
" default_id text references person (id)\n"
" on delete cascade on update cascade,\n"
" alternate_id text primary key\n"
");\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_key_check;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
// FIXME: foreign key check here
}
if (version < 7) {
int_result = sqlite3_exec(
_session->db,
"alter table person\n"
" add column is_pEp_user integer default 0;\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"update person\n"
" set is_pEp_user = 1\n"
" where id = "
" (select distinct id from person "
" join trust on id = user_id "
" where (case when (comm_type = 127) then (id) "
" when (comm_type = 255) then (id) "
" else 0"
" end) = id );\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"create table if not exists mistrusted_keys (\n"
" fpr text primary key\n"
");\n",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
if (version < 8) {
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _identity_new (\n"
" address text,\n"
" user_id text\n"
" references person (id)\n"
" on delete cascade on update cascade,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" comment text,\n"
" flags integer default 0,\n"
" is_own integer default 0,\n"
" timestamp integer default (datetime('now')),\n"
" primary key (address, user_id)\n"
");\n"
"INSERT INTO _identity_new (address, user_id, main_key_id, "
" comment, flags, is_own) "
" SELECT identity.address, identity.user_id, "
" identity.main_key_id, identity.comment, "
" identity.flags, identity.is_own "
" FROM identity "
" WHERE 1;\n"
"DROP TABLE identity;\n"
"ALTER TABLE _identity_new RENAME TO identity;\n"
"COMMIT;\n"
"\n"
"PRAGMA foreign_keys=on;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_key_check;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
// FIXME: foreign key check
}
if (version < 9) {
int_result = sqlite3_exec(
_session->db,
"create table if not exists social_graph (\n"
" own_userid text,\n"
" own_address text,\n"
" contact_userid text,\n"
" CONSTRAINT fk_own_identity\n"
" FOREIGN KEY(own_address, own_userid)\n"
" REFERENCES identity(address, user_id)\n"
" ON DELETE CASCADE ON UPDATE CASCADE\n"
");\n"
"create table if not exists revocation_contact_list (\n"
" fpr text not null references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" contact_id text not null references person (id)\n"
" on delete cascade on update cascade,\n"
" timestamp integer default (datetime('now')),\n"
" PRIMARY KEY(fpr, contact_id)\n"
");\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
if (version < 10 && version > 1) {
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_keys=off;\n"
"BEGIN TRANSACTION;\n"
"create table _person_new (\n"
" id text primary key,\n"
" username text not null,\n"
" main_key_id text\n"
" references pgp_keypair (fpr)\n"
" on delete set null,\n"
" lang text,\n"
" comment text,\n"
" is_pEp_user integer default 0\n"
");\n"
"INSERT INTO _person_new (id, username, main_key_id, "
" lang, comment, is_pEp_user) "
" SELECT person.id, person.username, "
" person.main_key_id, person.lang, "
" person.comment, person.is_pEp_user "
" FROM person "
" WHERE 1;\n"
"DROP TABLE person;\n"
"ALTER TABLE _person_new RENAME TO person;\n"
"COMMIT;\n"
"\n"
"PRAGMA foreign_keys=on;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"PRAGMA foreign_key_check;\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
if (version < 11) {
status = repair_altered_tables(_session);
assert(status == PEP_STATUS_OK);
if (status != PEP_STATUS_OK)
return status;
}
if (version < 12) {
int_result = sqlite3_exec(
_session->db,
"create index if not exists identity_userid_addr on identity(address, user_id);\n"
,
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"alter table identity\n"
" add column pEp_version_major integer default 0;\n"
"alter table identity\n"
" add column pEp_version_minor integer default 0;\n",
NULL,
NULL,
NULL
);
if (status != PEP_STATUS_OK)
return status;
int_result = sqlite3_exec(
_session->db,
"update identity\n"
" set pEp_version_major = 2\n"
" where exists (select * from person\n"
" where identity.user_id = person.id\n"
" and identity.is_own = 0\n"
" and person.is_pEp_user = 1);\n",
NULL,
NULL,
NULL
);
if (status != PEP_STATUS_OK)
return status;
// N.B. WE DEFINE PEP_VERSION - IF WE'RE AT 9-DIGIT MAJOR OR MINOR VERSIONS, ER, BAD.
char major_buf[10];
char minor_buf[10];
// Guess we were abusing sscanf here, so we'll do it this way:
const char* cptr = PEP_VERSION;
size_t major_len = 0;
size_t minor_len = 0;
char* bufptr = major_buf;
while (*cptr != '.' && *cptr != '\0') {
*bufptr++ = *cptr++;
major_len++;
}
*bufptr = '\0';
bufptr = minor_buf;
if (*cptr == '.') {
cptr++;
while (*cptr != '\0') {
*bufptr++ = *cptr++;
minor_len++;
}
}
else {
*bufptr++ = '0';
}
*bufptr = '\0';
const char* _ver_12_startstr =
"update identity\n"
" set pEp_version_major = ";
const char* _ver_12_midstr = ",\n"
" pEp_version_minor = ";
const char* _ver_12_endstr =
"\n"
" where identity.is_own = 1;\n";
size_t new_stringlen = strlen(_ver_12_startstr) + major_len +
strlen(_ver_12_midstr) + minor_len +
strlen(_ver_12_endstr);
char* _ver_12_stmt = calloc(new_stringlen + 1, 1);
snprintf(_ver_12_stmt, new_stringlen + 1, "%s%s%s%s%s",
_ver_12_startstr, major_buf, _ver_12_midstr, minor_buf, _ver_12_endstr);
int_result = sqlite3_exec(
_session->db,
_ver_12_stmt,
NULL,
NULL,
NULL
);
free(_ver_12_stmt);
if (status != PEP_STATUS_OK)
return status;
}
if (version < 13) {
status = upgrade_revoc_contact_to_13(_session);
assert(status == PEP_STATUS_OK);
if (status != PEP_STATUS_OK)
return status;
}
}
else {
// Version from DB was 0, it means this is initial setup.
// DB has just been created, and all tables are empty.
very_first = true;
}
if (version < atoi(_DDL_USER_VERSION)) {
int_result = sqlite3_exec(
_session->db,
"pragma user_version = "_DDL_USER_VERSION";\n"
"insert or replace into version_info (id, version)"
"values (1, '" PEP_ENGINE_VERSION "');",
NULL,
NULL,
NULL
);
assert(int_result == SQLITE_OK);
}
// We need to init a few globals for message id that we'd rather not
// calculate more than once.
_init_globals();
}
int_result = sqlite3_prepare_v2(_session->db, sql_log,
(int)strlen(sql_log), &_session->log, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
(int)strlen(sql_trustword), &_session->trustword, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
(int)strlen(sql_get_identity), &_session->get_identity, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_trust_check,
(int)strlen(sql_get_identity_without_trust_check),
&_session->get_identity_without_trust_check, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_address,
(int)strlen(sql_get_identities_by_address),
&_session->get_identities_by_address, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
(int)strlen(sql_get_identities_by_userid),
&_session->get_identities_by_userid, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_main_key_id,
(int)strlen(sql_get_identities_by_main_key_id),
&_session->get_identities_by_main_key_id, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
(int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_all_keys_for_user,
(int)strlen(sql_get_all_keys_for_user), &_session->get_all_keys_for_user, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_default_own_userid,
(int)strlen(sql_get_default_own_userid), &_session->get_default_own_userid, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_userid_alias_default,
(int)strlen(sql_get_userid_alias_default), &_session->get_userid_alias_default, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_add_userid_alias,
(int)strlen(sql_add_userid_alias), &_session->add_userid_alias, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_replace_userid,
(int)strlen(sql_replace_userid), &_session->replace_userid, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_delete_key,
(int)strlen(sql_delete_key), &_session->delete_key, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr,
(int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
(int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_refresh_userid_default_key,
(int)strlen(sql_refresh_userid_default_key), &_session->refresh_userid_default_key, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
(int)strlen(sql_replace_identities_fpr),
&_session->replace_identities_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
(int)strlen(sql_remove_fpr_as_default),
&_session->remove_fpr_as_default, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
(int)strlen(sql_set_person), &_session->set_person, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
(int)strlen(sql_update_person), &_session->update_person, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_delete_person,
(int)strlen(sql_delete_person), &_session->delete_person, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_exists_person,
(int)strlen(sql_exists_person), &_session->exists_person, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pEp_user,
(int)strlen(sql_set_as_pEp_user), &_session->set_as_pEp_user, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_is_pEp_user,
(int)strlen(sql_is_pEp_user), &_session->is_pEp_user, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
(int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_get_own_address_binding_from_contact,
(int)strlen(sql_get_own_address_binding_from_contact),
&_session->get_own_address_binding_from_contact, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_set_revoke_contact_as_notified,
(int)strlen(sql_set_revoke_contact_as_notified),
&_session->set_revoke_contact_as_notified, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_get_contacted_ids_from_revoke_fpr,
(int)strlen(sql_get_contacted_ids_from_revoke_fpr),
&_session->get_contacted_ids_from_revoke_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_was_id_for_revoke_contacted,
(int)strlen(sql_was_id_for_revoke_contacted),
&_session->was_id_for_revoke_contacted, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_has_id_contacted_address,
(int)strlen(sql_has_id_contacted_address),
&_session->has_id_contacted_address, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_get_last_contacted,
(int)strlen(sql_get_last_contacted),
&_session->get_last_contacted, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db,
sql_get_own_address_binding_from_contact,
(int)strlen(sql_get_own_address_binding_from_contact),
&_session->get_own_address_binding_from_contact, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
(int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
(int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
(int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_exists_identity_entry,
(int)strlen(sql_exists_identity_entry), &_session->exists_identity_entry, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
(int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_unset_identity_flags,
(int)strlen(sql_unset_identity_flags), &_session->unset_identity_flags,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_pEp_version,
(int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_upgrade_pEp_version_by_user_id,
(int)strlen(sql_upgrade_pEp_version_by_user_id), &_session->upgrade_pEp_version_by_user_id,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_clear_trust_info,
(int)strlen(sql_clear_trust_info), &_session->clear_trust_info, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
(int)strlen(sql_set_trust), &_session->set_trust, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
(int)strlen(sql_update_trust), &_session->update_trust, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_to_pEp,
(int)strlen(sql_update_trust_to_pEp), &_session->update_trust_to_pEp, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_exists_trust_entry,
(int)strlen(sql_exists_trust_entry), &_session->exists_trust_entry, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
(int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
(int)strlen(sql_get_trust), &_session->get_trust, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_trust_by_userid,
(int)strlen(sql_get_trust_by_userid), &_session->get_trust_by_userid, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
(int)strlen(sql_least_trust), &_session->least_trust, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromised,
(int)strlen(sql_mark_as_compromised), &_session->mark_compromised,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
(int)strlen(sql_crashdump), &_session->crashdump, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
(int)strlen(sql_languagelist), &_session->languagelist, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
(int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
assert(int_result == SQLITE_OK);
// blacklist
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
(int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
(int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
(int)strlen(sql_blacklist_is_listed),
&_session->blacklist_is_listed, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
(int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
NULL);
assert(int_result == SQLITE_OK);
// Own keys
int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
(int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
(int)strlen(sql_is_own_address), &_session->is_own_address,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
(int)strlen(sql_own_identities_retrieve),
&_session->own_identities_retrieve, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_own_keys_retrieve,
(int)strlen(sql_own_keys_retrieve),
&_session->own_keys_retrieve, NULL);
assert(int_result == SQLITE_OK);
// int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
// (int)strlen(sql_set_own_key),
// &_session->set_own_key, NULL);
// assert(int_result == SQLITE_OK);
// Sequence
int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
(int)strlen(sql_sequence_value1), &_session->sequence_value1,
NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
(int)strlen(sql_sequence_value2), &_session->sequence_value2,
NULL);
assert(int_result == SQLITE_OK);
// Revocation tracking
int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
(int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
(int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_replacement_fpr,
(int)strlen(sql_get_replacement_fpr), &_session->get_replacement_fpr, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_add_mistrusted_key,
(int)strlen(sql_add_mistrusted_key), &_session->add_mistrusted_key, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_delete_mistrusted_key,
(int)strlen(sql_delete_mistrusted_key), &_session->delete_mistrusted_key, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_is_mistrusted_key,
(int)strlen(sql_is_mistrusted_key), &_session->is_mistrusted_key, NULL);
assert(int_result == SQLITE_OK);
status = init_cryptotech(_session, in_first);
if (status != PEP_STATUS_OK)
goto pEp_error;
status = init_transport_system(_session, in_first);
if (status != PEP_STATUS_OK)
goto pEp_error;
status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
if (status != PEP_STATUS_OK)
goto pEp_error;
// runtime config
if (very_first)
{
#ifdef USE_GPG
// On first run, all private keys already present in PGP keyring
// are taken as own in order to seamlessly integrate with
// pre-existing GPG setup.
// Note: earlier fears about danger because of DB reinitialisation should
// be a non-issue here, as we ONLY take the ultimately trusted keys now.
// Thus, unless the user has assigned ultimate trust through PGP, there is
// no chance of automatically imported pEp keys from a previous run making
// their way into PEP trusted status without explicit action (Bare imported
// private keys have an 'unknown' trust designation in PGP).
// We don't really worry about the status here.
status = pgp_import_ultimately_trusted_keypairs(_session);
#endif // USE_GPG
}
*session = _session;
// Note: Following statement is NOT for any cryptographic/secure functionality; it is
// ONLY used for some randomness in generated outer message ID, which are
// required by the RFC to be globally unique!
srand((unsigned int) time(NULL));
return PEP_STATUS_OK;
enomem:
status = PEP_OUT_OF_MEMORY;
pEp_error:
release(_session);
return status;
}
DYNAMIC_API void release(PEP_SESSION session)
{
bool out_last = false;
int _count = --init_count;
assert(_count >= -1);
assert(session);
if (!((_count >= -1) && session))
return;
// a small race condition but still a race condition
// mitigated by calling caveat (see documentation)
// (release() is to be guarded by a mutex by the caller)
if (_count == -1)
out_last = true;
if (session) {
free_Sync_state(session);
if (session->db) {
if (session->log)
sqlite3_finalize(session->log);
if (session->trustword)
sqlite3_finalize(session->trustword);
if (session->get_identity)
sqlite3_finalize(session->get_identity);
if (session->get_identity_without_trust_check)
sqlite3_finalize(session->get_identity_without_trust_check);
if (session->get_identities_by_address)
sqlite3_finalize(session->get_identities_by_address);
if (session->get_identities_by_userid)
sqlite3_finalize(session->get_identities_by_userid);
if (session->get_identities_by_main_key_id)
sqlite3_finalize(session->get_identities_by_main_key_id);
if (session->get_user_default_key)
sqlite3_finalize(session->get_user_default_key);
if (session->get_all_keys_for_user)
sqlite3_finalize(session->get_all_keys_for_user);
if (session->get_default_own_userid)
sqlite3_finalize(session->get_default_own_userid);
if (session->get_userid_alias_default)
sqlite3_finalize(session->get_userid_alias_default);
if (session->add_userid_alias)
sqlite3_finalize(session->add_userid_alias);
if (session->replace_identities_fpr)
sqlite3_finalize(session->replace_identities_fpr);
if (session->remove_fpr_as_default)
sqlite3_finalize(session->remove_fpr_as_default);
if (session->set_person)
sqlite3_finalize(session->set_person);
if (session->delete_person)
sqlite3_finalize(session->delete_person);
if (session->set_as_pEp_user)
sqlite3_finalize(session->set_as_pEp_user);
if (session->upgrade_pEp_version_by_user_id)
sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
if (session->is_pEp_user)
sqlite3_finalize(session->is_pEp_user);
if (session->exists_person)
sqlite3_finalize(session->exists_person);
if (session->add_into_social_graph)
sqlite3_finalize(session->add_into_social_graph);
if (session->get_own_address_binding_from_contact)
sqlite3_finalize(session->get_own_address_binding_from_contact);
if (session->set_revoke_contact_as_notified)
sqlite3_finalize(session->set_revoke_contact_as_notified);
if (session->get_contacted_ids_from_revoke_fpr)
sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);
if (session->was_id_for_revoke_contacted)
sqlite3_finalize(session->was_id_for_revoke_contacted);
if (session->has_id_contacted_address)
sqlite3_finalize(session->has_id_contacted_address);
if (session->get_last_contacted)
sqlite3_finalize(session->get_last_contacted);
if (session->set_pgp_keypair)
sqlite3_finalize(session->set_pgp_keypair);
if (session->exists_identity_entry)
sqlite3_finalize(session->exists_identity_entry);
if (session->set_identity_entry)
sqlite3_finalize(session->set_identity_entry);
if (session->update_identity_entry)
sqlite3_finalize(session->update_identity_entry);
if (session->set_identity_flags)
sqlite3_finalize(session->set_identity_flags);
if (session->unset_identity_flags)
sqlite3_finalize(session->unset_identity_flags);
if (session->set_pEp_version)
sqlite3_finalize(session->set_pEp_version);
if (session->exists_trust_entry)
sqlite3_finalize(session->exists_trust_entry);
if (session->clear_trust_info)
sqlite3_finalize(session->clear_trust_info);
if (session->set_trust)
sqlite3_finalize(session->set_trust);
if (session->update_trust)
sqlite3_finalize(session->update_trust);
if (session->update_trust_to_pEp)
sqlite3_finalize(session->update_trust_to_pEp);
if (session->update_trust_for_fpr)
sqlite3_finalize(session->update_trust_for_fpr);
if (session->get_trust)
sqlite3_finalize(session->get_trust);
if (session->get_trust_by_userid)
sqlite3_finalize(session->get_trust_by_userid);
if (session->least_trust)
sqlite3_finalize(session->least_trust);
if (session->mark_compromised)
sqlite3_finalize(session->mark_compromised);
if (session->crashdump)
sqlite3_finalize(session->crashdump);
if (session->languagelist)
sqlite3_finalize(session->languagelist);
if (session->i18n_token)
sqlite3_finalize(session->i18n_token);
if (session->replace_userid)
sqlite3_finalize(session->replace_userid);
if (session->delete_key)
sqlite3_finalize(session->delete_key);
if (session->replace_main_user_fpr)
sqlite3_finalize(session->replace_main_user_fpr);
if (session->get_main_user_fpr)
sqlite3_finalize(session->get_main_user_fpr);
if (session->refresh_userid_default_key)
sqlite3_finalize(session->refresh_userid_default_key);
if (session->blacklist_add)
sqlite3_finalize(session->blacklist_add);
if (session->blacklist_delete)
sqlite3_finalize(session->blacklist_delete);
if (session->blacklist_is_listed)
sqlite3_finalize(session->blacklist_is_listed);
if (session->blacklist_retrieve)
sqlite3_finalize(session->blacklist_retrieve);
if (session->own_key_is_listed)
sqlite3_finalize(session->own_key_is_listed);
if (session->is_own_address)
sqlite3_finalize(session->is_own_address);
if (session->own_identities_retrieve)
sqlite3_finalize(session->own_identities_retrieve);
if (session->own_keys_retrieve)
sqlite3_finalize(session->own_keys_retrieve);
// if (session->set_own_key)
// sqlite3_finalize(session->set_own_key);
if (session->sequence_value1)
sqlite3_finalize(session->sequence_value1);
if (session->sequence_value2)
sqlite3_finalize(session->sequence_value2);
if (session->set_revoked)
sqlite3_finalize(session->set_revoked);
if (session->get_revoked)
sqlite3_finalize(session->get_revoked);
if (session->get_replacement_fpr)
sqlite3_finalize(session->get_replacement_fpr);
if (session->add_mistrusted_key)
sqlite3_finalize(session->add_mistrusted_key);
if (session->delete_mistrusted_key)
sqlite3_finalize(session->delete_mistrusted_key);
if (session->is_mistrusted_key)
sqlite3_finalize(session->is_mistrusted_key);
if (session->db) {
if (out_last) {
sqlite3_exec(
session->db,
"PRAGMA optimize;\n",
NULL,
NULL,
NULL
);
}
sqlite3_close_v2(session->db);
}
if (session->system_db)
sqlite3_close_v2(session->system_db);
}
release_transport_system(session, out_last);
release_cryptotech(session, out_last);
#ifdef DEBUG_ERRORSTACK
free_stringlist(session->errorstack);
#endif
free(session);
}
}
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
{
assert(session);
if (session)
session->passive_mode = enable;
}
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
{
assert(session);
if (session)
session->unencrypted_subject = enable;
}
DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
{
assert(session);
if (session)
session->service_log = enable;
}
DYNAMIC_API PEP_STATUS log_event(
PEP_SESSION session,
const char *title,
const char *entity,
const char *description,
const char *comment
)
{
#if defined(_WIN32) && !defined(NDEBUG)
log_output_debug(title, entity, description, comment);
#endif
// N.B. If testing (so NDEBUG not defined) but this message is spam,
// put -D_PEP_SERVICE_LOG_OFF into CFLAGS/CXXFLAGS
#if !defined(NDEBUG) && !defined(_PEP_SERVICE_LOG_OFF)
#ifndef NDEBUG
printf("\x1b[%dm", session->debug_color);
#endif
fprintf(stdout, "\n*** %s %s %s %s\n", title, entity, description, comment);
#ifndef NDEBUG
printf("\x1b[0m");
#endif
session->service_log = true;
int result;
assert(session);
assert(title);
assert(entity);
if (!(session && title && entity))
return PEP_ILLEGAL_VALUE;
sqlite3_reset(session->log);
sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
if (description)
sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
else
sqlite3_bind_null(session->log, 3);
if (comment)
sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
else
sqlite3_bind_null(session->log, 4);
result = sqlite3_step(session->log);
sqlite3_reset(session->log);
#endif
return PEP_STATUS_OK; // We ignore errors for this function.
}
DYNAMIC_API PEP_STATUS log_service(
PEP_SESSION session,
const char *title,
const char *entity,
const char *description,
const char *comment
)
{
assert(session);
if (!session)
return PEP_ILLEGAL_VALUE;
if (session->service_log)
return log_event(session, title, entity, description, comment);
else
return PEP_STATUS_OK;
}
DYNAMIC_API PEP_STATUS trustword(
PEP_SESSION session, uint16_t value, const char *lang,
char **word, size_t *wsize
)
{
PEP_STATUS status = PEP_STATUS_OK;
assert(session);
assert(word);
assert(wsize);
if (!(session && word && wsize))
return PEP_ILLEGAL_VALUE;
*word = NULL;
*wsize = 0;
if (lang == NULL)
lang = "en";
assert((lang[0] >= 'A' && lang[0] <= 'Z')
|| (lang[0] >= 'a' && lang[0] <= 'z'));
assert((lang[1] >= 'A' && lang[1] <= 'Z')
|| (lang[1] >= 'a' && lang[1] <= 'z'));
assert(lang[2] == 0);
sqlite3_reset(session->trustword);
sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
sqlite3_bind_int(session->trustword, 2, value);
const int result = sqlite3_step(session->trustword);
if (result == SQLITE_ROW) {
*word = strdup((const char *) sqlite3_column_text(session->trustword,
1));
if (*word)
*wsize = sqlite3_column_bytes(session->trustword, 1);
else
status = PEP_OUT_OF_MEMORY;
} else
status = PEP_TRUSTWORD_NOT_FOUND;
sqlite3_reset(session->trustword);
return status;
}
DYNAMIC_API PEP_STATUS trustwords(
PEP_SESSION session, const char *fingerprint, const char *lang,
char **words, size_t *wsize, int max_words
)
{
const char *source = fingerprint;
assert(session);
assert(fingerprint);
assert(words);
assert(wsize);
assert(max_words >= 0);
if (!(session && fingerprint && words && wsize && max_words >= 0))
return PEP_ILLEGAL_VALUE;
*words = NULL;
*wsize = 0;
char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
assert(buffer);
if (buffer == NULL)
return PEP_OUT_OF_MEMORY;
char *dest = buffer;
const size_t fsize = strlen(fingerprint);
if (!lang || !lang[0])
lang = "en";
assert((lang[0] >= 'A' && lang[0] <= 'Z')
|| (lang[0] >= 'a' && lang[0] <= 'z'));
assert((lang[1] >= 'A' && lang[1] <= 'Z')
|| (lang[1] >= 'a' && lang[1] <= 'z'));
assert(lang[2] == 0);
int n_words = 0;
while (source < fingerprint + fsize) {
PEP_STATUS _status;
uint16_t value;
char *word = NULL;
size_t _wsize = 0;
int j;
for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
if (*source >= 'a' && *source <= 'f')
value += (*source - 'a' + 10) << (3 - j++) * 4;
else if (*source >= 'A' && *source <= 'F')
value += (*source - 'A' + 10) << (3 - j++) * 4;
else if (*source >= '0' && *source <= '9')
value += (*source - '0') << (3 - j++) * 4;
source++;
}
_status = trustword(session, value, lang, &word, &_wsize);
if (_status == PEP_OUT_OF_MEMORY) {
free(buffer);
return PEP_OUT_OF_MEMORY;
}
if (word == NULL) {
free(buffer);
return PEP_TRUSTWORD_NOT_FOUND;
}
if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
strncpy(dest, word, _wsize);
free(word);
dest += _wsize;
}
else {
free(word);
break; // buffer full
}
if (source < fingerprint + fsize
&& dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
*dest++ = ' ';
++n_words;
if (max_words && n_words >= max_words)
break;