Browse Source

Added sql separate from ENGINE-606/ENGINE-822 so that the DDLs don't diverge any further (merges are difficult without this being separated this way in master as well). This means that some of the groups sql logic is present here, but it won't get used until the branch code makes it in here. If you have problems merging a branch, let me know. Better now than later.

ENGINE-820
Krista Bennett 2 years ago
parent
commit
c52f4d39fb
5 changed files with 3305 additions and 2695 deletions
  1. +2642
    -0
      src/engine_sql.c
  2. +560
    -0
      src/engine_sql.h
  3. +0
    -15
      src/key_reset.c
  4. +57
    -2678
      src/pEpEngine.c
  5. +46
    -2
      src/pEp_internal.h

+ 2642
- 0
src/engine_sql.c
File diff suppressed because it is too large
View File


+ 560
- 0
src/engine_sql.h View File

@ -0,0 +1,560 @@
#pragma once
#include "pEp_internal.h"
// increment this when patching DDL
#define _DDL_USER_VERSION "16"
PEP_STATUS init_databases(PEP_SESSION session);
PEP_STATUS pEp_sql_init(PEP_SESSION session);
PEP_STATUS pEp_prepare_sql_stmts(PEP_SESSION session);
PEP_STATUS pEp_finalize_sql_stmts(PEP_SESSION session);
/**
* Strings to feed into prepared 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 identity.main_key_id, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own, pEp_version_major, pEp_version_minor, enc_format"
" from identity"
" join person on id = identity.user_id"
" left join pgp_keypair on fpr = identity.main_key_id"
" left 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, enc_format"
" from identity"
" join person on id = identity.user_id"
" left join pgp_keypair on fpr = identity.main_key_id"
" left 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, enc_format"
" 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, enc_format"
" 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, identity.main_key_id, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags,"
" is_own, pEp_version_major, pEp_version_minor, enc_format"
" from identity"
" join person on id = identity.user_id"
" left join pgp_keypair on fpr = identity.main_key_id"
" left 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_identity_default =
"update identity set main_key_id = NULL where main_key_id = ?1 ;";
static const char *sql_remove_fpr_as_user_default =
"update person 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_replace_main_user_fpr_if_equal =
"update person "
" set main_key_id = ?1 "
" where id = ?2 and main_key_id = ?3;";
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 person.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_ident_enc_format =
"update identity "
" set enc_format = ?1 "
" 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, identity.main_key_id, 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"
" left join pgp_keypair on fpr = identity.main_key_id"
" left 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 distinct 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 const char *sql_create_group =
"insert into groups (group_id, group_address, manager_userid, manager_address) "
"VALUES (?1, ?2, ?3, ?4) ;";
static const char *sql_enable_group =
"update groups set active = 1 "
" where group_id = ?1 and group_address = ?2 ;";
static const char *sql_disable_group =
"update groups set active = 0 "
" where group_id = ?1 and group_address = ?2 ;";
static const char *sql_exists_group_entry =
"select count(*) from groups "
" where group_id = ?1 and group_address = ?2;";
static const char *sql_group_add_member =
"insert or ignore into own_groups_members (group_id, group_address, member_id, member_address) "
" values (?1, ?2, ?3, ?4) ;";
static const char *sql_set_group_member_status =
"update own_groups_members set active_member = ?1 "
" where group_id = ?2 and group_address = ?3 and "
" member_id = ?4 and member_address = ?5; ";
static const char *sql_join_group =
"update own_memberships set have_joined = 1 "
" where group_id = ?1 and group_address = ?2 and "
" own_id = ?3 and own_address = ?4; ";
static const char *sql_leave_group =
"update own_memberships set have_joined = 0 "
" where group_id = ?1 and group_address = ?2 and "
" own_id = ?3 and own_address = ?4; ";
static const char *sql_get_all_members =
"select member_id, member_address, active_member from own_groups_members "
" where group_id = ?1 and group_address = ?2; ";
static const char *sql_get_active_members =
"select member_id, member_address from own_groups_members "
" where group_id = ?1 and group_address = ?2 and active_member = 1; ";
static const char *sql_get_group_manager =
"select manager_userid, manager_address from groups "
" where group_id = ?1 and group_address = ?2; ";
static const char *sql_is_invited_group_member =
"select count(*) from own_groups_members "
" where group_id = ?1 and group_address = ?2 and member_id = ?3 and member_address = ?4; ";
static const char *sql_get_all_groups =
"select group_id, group_address from own_memberships; ";
static const char *sql_get_active_groups =
"select group_id, group_address from own_memberships where have_joined = 1; ";
static const char *sql_add_own_membership_entry =
"insert or replace into own_memberships (group_id, group_address, own_id, own_address, have_joined) "
" values (?1, ?2, ?3, ?4, 0) ; ";
static const char *sql_is_group_active =
"select count(*) from groups "
" where group_id = ?1 and group_address = ?2 and active = 1; ";
// This below can return multiple entries for multiple idents in same group
// FIXME: decide what we really need here
static const char *sql_retrieve_own_membership_info_for_group =
"select own_id, own_address, have_joined "
" from own_memberships "
" inner join groups using (group_id, group_address) "
" where group_id = ?1 and group_address = ?2; ";
static const char *sql_retrieve_own_membership_info_for_group_and_ident =
"select have_joined, manager_userid, manager_address, active "
" from own_memberships "
" inner join groups using (group_id, group_address) "
" where group_id = ?1 and group_address = ?2 and own_id = ?3 and own_address = ?4; ";
// This will return all membership info for all identities
static const char *sql_retrieve_all_own_membership_info =
"select group_id, group_address, own_id, own_address, have_joined, manager_id, manager_address, active "
" from own_memberships "
" inner join using (group_id, group_address); ";
static const char* sql_get_own_membership_status =
"select have_joined from own_memberships "
" where group_id = ?1 and group_address = ?2 and "
" own_id = ?3 and own_address = ?4; ";

+ 0
- 15
src/key_reset.c View File

@ -23,21 +23,6 @@
#define KEY_RESET_MAJOR_VERSION 1L
#define KEY_RESET_MINOR_VERSION 0L
/**
* @internal
*
* <!-- _add_auto_consume() -->
*
* @brief TODO
*
* @param[in] *msg message
*
*/
static void _add_auto_consume(message* msg) {
add_opt_field(msg, "pEp-auto-consume", "yes");
msg->in_reply_to = stringlist_add(msg->in_reply_to, "pEp-auto-consume@pEp.foundation");
}
/**
* @internal
*


+ 57
- 2678
src/pEpEngine.c
File diff suppressed because it is too large
View File


+ 46
- 2
src/pEp_internal.h View File

@ -22,6 +22,7 @@
#include "commit_hash.h" // We need this everywhere. So.
// maximum attachment size to import as key 25MB, maximum of 20 attachments
#define MAX_KEY_SIZE (25 * 1024 * 1024)
#define MAX_KEYS_TO_IMPORT 20
@ -121,6 +122,9 @@
#include "pgp_sequoia_internal.h"
#endif
#include "../asn.1/Distribution.h"
#include "../asn.1/Sync.h"
#include "keymanagement.h"
#include "cryptotech.h"
#include "transport.h"
@ -250,6 +254,26 @@ struct _pEpSession {
sqlite3_stmt *get_default_own_userid;
// groups
sqlite3_stmt *create_group;
sqlite3_stmt *enable_group;
sqlite3_stmt *disable_group;
sqlite3_stmt *exists_group_entry;
sqlite3_stmt *group_add_member;
sqlite3_stmt *join_group;
sqlite3_stmt *leave_group;
sqlite3_stmt *set_group_member_status;
sqlite3_stmt *get_all_members;
sqlite3_stmt *get_active_members;
sqlite3_stmt *get_active_groups;
sqlite3_stmt *get_all_groups;
sqlite3_stmt *add_own_membership_entry;
sqlite3_stmt *get_own_membership_status;
sqlite3_stmt *retrieve_own_membership_info_for_group_and_ident;
sqlite3_stmt *retrieve_own_membership_info_for_group;
sqlite3_stmt *get_group_manager;
sqlite3_stmt *is_invited_group_member;
sqlite3_stmt *is_group_active;
// sqlite3_stmt *set_own_key;
@ -749,16 +773,20 @@ extern double _pEp_log2_36;
/**
* <!-- _init_globals() -->
*
*
* @internal
*
* @brief TODO
*
*
* Please leave _patch_asn1_codec COMMENTED OUT unless you're working
* in a branch or patching the asn1 is a solution
*/
static inline void _init_globals() {
_pEp_rand_max_bits = (int) ceil(log2((double) RAND_MAX));
_pEp_log2_36 = log2(36);
}
// spinlock implementation
/**
@ -778,4 +806,20 @@ static inline int Sqlite3_step(sqlite3_stmt* stmt)
return rc;
}
/**
* @internal
*
* <!-- _add_auto_consume() -->
*
* @brief TODO
*
* @param[in] *msg message
*
*/
static inline void _add_auto_consume(message* msg) {
add_opt_field(msg, "pEp-auto-consume", "yes");
msg->in_reply_to = stringlist_add(msg->in_reply_to, "pEp-auto-consume@pEp.foundation");
}
#endif

Loading…
Cancel
Save