p≡p engine
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.

616 lines
26 KiB

ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
ENGINE-866 feature branch merge (squashed commit) of functionality to set the sticky bit for manually imported keys, to query for that bit in the trust database, and prevention of automatic reset of sticky keys by key reset when devices leave a device group. Squashed commit of the following: commit c64d850dc4bfe5a9dfd54aa94eea08a75ff69191 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:29:32 2021 +0100 ENGINE-866: doc'd bit getter function commit ad725b5b7c742300a6a182ad8b058db23dbc3cfb Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:23:49 2021 +0100 ENGINE-866: Key reset tested on mixed sticky and not sticky keys and does what it should. commit 0ffbdde7b598c7c3fff5d797e732dec07685f9be Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 15:13:53 2021 +0100 ENGINE-866: Add boolean for whether to set the sticky bit or not with set_own_imported_key. the adapter should filter this out for apps, I guess, according to Volker commit 23fec59a9a4ede0682a9ebcb9a61e78456e7d8d4 Author: Krista Bennett <krista@pep.foundation> Date: Fri Feb 26 14:53:19 2021 +0100 ENGINE-866: Test and use the sticky bit commit 562239fda874623c40893c382a8f82df9e002ef5 Author: Krista Bennett <krista@pep.foundation> Date: Thu Feb 25 16:47:47 2021 +0100 ENGINE-866: moved bit from key to trust, created set_own_imported_key to replace set_own_key FOR MAIL APPS (does NOT replace it for key reset, as the new function can generate a passphrase error, whereas set_own_key cannot), and did an initial test to ensure the setter/getter functions work on the DB. commit 594133cfdee966adbaa66c62133ede1ca917bca0 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 11:16:21 2021 +0100 Commented out the or'd identity.flags / pgp_keypair.flags in the sql code for the get_identity functions; we've never HAD a pgp_keypair flag before, so it never hurt before, but at this point, we're going to introduce them, and I don't want trouble. If fdik wants them or'd, fine, we'll have to change the values in the keyflags to be disjoint from the identity flags so they can coexist, but for now, they are out. commit 99831445b3e22e1386aa0f86414fdb6939e5ebaf Merge: 8ba53ece d1664cf5 Author: Krista Bennett <krista@pep.foundation> Date: Wed Feb 24 10:15:53 2021 +0100 Merge branch 'master' into ENGINE-866 commit 8ba53ece06773168a9188373d1be5f13d99b2f6e Merge: 168e2cf9 c52f4d39 Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 20:06:08 2021 +0100 Merged in engine_sql changes commit 168e2cf9578b12157b98da8b26e598f0a1448d9e Author: Krista Bennett <krista@pep.foundation> Date: Mon Feb 22 19:03:35 2021 +0100 ENGINE-866: Added sticky bit in database for manually set keys
1 year ago
  1. #pragma once
  2. #include "pEp_internal.h"
  3. // increment this when patching DDL
  4. #define _DDL_USER_VERSION "19"
  5. PEP_STATUS init_databases(PEP_SESSION session);
  6. PEP_STATUS pEp_sql_init(PEP_SESSION session);
  7. PEP_STATUS pEp_prepare_sql_stmts(PEP_SESSION session);
  8. PEP_STATUS pEp_finalize_sql_stmts(PEP_SESSION session);
  9. /**
  10. * Strings to feed into prepared statements
  11. */
  12. static const char *sql_log =
  13. "insert into log (title, entity, description, comment)"
  14. "values (?1, ?2, ?3, ?4);";
  15. static const char *sql_trustword =
  16. "select id, word from wordlist where lang = lower(?1) "
  17. "and id = ?2 ;";
  18. // FIXME?: problems if we don't have a key for the user - we get nothing
  19. // Also: we've never used pgp_keypair.flags before now, but it seems to me that
  20. // having combination of those flags is a road to ruin. Changing this for now.
  21. static const char *sql_get_identity =
  22. "select identity.main_key_id,"
  23. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  24. " else identity.username end),"
  25. " comm_type, lang, identity.flags,"
  26. // " identity.flags | pgp_keypair.flags,"
  27. " is_own, pEp_version_major, pEp_version_minor, enc_format"
  28. " from identity"
  29. " join person on id = identity.user_id"
  30. " left join pgp_keypair on fpr = identity.main_key_id"
  31. " left join trust on id = trust.user_id"
  32. " and pgp_keypair_fpr = identity.main_key_id"
  33. " where (case when (address = ?1) then (1)"
  34. " when (lower(address) = lower(?1)) then (1)"
  35. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
  36. " else 0"
  37. " end) = 1"
  38. " and identity.user_id = ?2"
  39. " order by is_own desc, "
  40. " timestamp desc; ";
  41. static const char *sql_get_identities_by_main_key_id =
  42. "select address, identity.user_id,"
  43. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  44. " else identity.username end),"
  45. " comm_type, lang, identity.flags,"
  46. // " identity.flags | pgp_keypair.flags,"
  47. " is_own, pEp_version_major, pEp_version_minor, enc_format"
  48. " from identity"
  49. " join person on id = identity.user_id"
  50. " left join pgp_keypair on fpr = identity.main_key_id"
  51. " left join trust on id = trust.user_id"
  52. " and pgp_keypair_fpr = identity.main_key_id"
  53. " where identity.main_key_id = ?1"
  54. " order by is_own desc, "
  55. " timestamp desc; ";
  56. static const char *sql_get_identity_without_trust_check =
  57. "select identity.main_key_id,"
  58. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  59. " else identity.username end),"
  60. " lang, identity.flags, is_own, pEp_version_major, pEp_version_minor, enc_format"
  61. " from identity"
  62. " join person on id = identity.user_id"
  63. " where (case when (address = ?1) then (1)"
  64. " when (lower(address) = lower(?1)) then (1)"
  65. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
  66. " else 0"
  67. " end) = 1"
  68. " and identity.user_id = ?2 "
  69. " order by is_own desc, "
  70. " timestamp desc; ";
  71. static const char *sql_get_identities_by_address =
  72. "select user_id, identity.main_key_id,"
  73. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  74. " else identity.username end),"
  75. " lang, identity.flags, is_own, pEp_version_major, pEp_version_minor, enc_format"
  76. " from identity"
  77. " join person on id = identity.user_id"
  78. " where (case when (address = ?1) then (1)"
  79. " when (lower(address) = lower(?1)) then (1)"
  80. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
  81. " else 0"
  82. " end) = 1 "
  83. " order by is_own desc, "
  84. " timestamp desc; ";
  85. static const char *sql_get_identities_by_userid =
  86. "select address, identity.main_key_id,"
  87. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  88. " else identity.username end),"
  89. " comm_type, lang, identity.flags,"
  90. // " identity.flags | pgp_keypair.flags,"
  91. " is_own, pEp_version_major, pEp_version_minor, enc_format"
  92. " from identity"
  93. " join person on id = identity.user_id"
  94. " left join pgp_keypair on fpr = identity.main_key_id"
  95. " left join trust on id = trust.user_id"
  96. " and pgp_keypair_fpr = identity.main_key_id"
  97. " where identity.user_id = ?1"
  98. " order by is_own desc, "
  99. " timestamp desc; ";
  100. static const char *sql_replace_identities_fpr =
  101. "update identity"
  102. " set main_key_id = ?1 "
  103. " where main_key_id = ?2 ;";
  104. static const char* sql_set_default_identity_fpr =
  105. "update identity set main_key_id = ?3 "
  106. " where user_id = ?1 and address = ?2; ";
  107. static const char *sql_get_default_identity_fpr =
  108. "select main_key_id from identity"
  109. " where (case when (address = ?1) then (1)"
  110. " when (lower(address) = lower(?1)) then (1)"
  111. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
  112. " else 0 "
  113. " end) = 1 "
  114. " and user_id = ?2 ;";
  115. static const char *sql_remove_fpr_as_identity_default =
  116. "update identity set main_key_id = NULL where main_key_id = ?1 ;";
  117. static const char *sql_remove_fpr_as_user_default =
  118. "update person set main_key_id = NULL where main_key_id = ?1 ;";
  119. // Set person, but if already exist, only update.
  120. // if main_key_id already set, don't touch.
  121. static const char *sql_set_person =
  122. "insert into person (id, username, lang, main_key_id)"
  123. " values (?1, ?2, ?3, ?4) ;";
  124. static const char *sql_update_person =
  125. "update person "
  126. " set username = ?2, "
  127. " lang = ?3, "
  128. " main_key_id = "
  129. " (select coalesce( "
  130. " (select main_key_id from person where id = ?1), "
  131. " upper(replace(?4,' ',''))))"
  132. " where id = ?1 ;";
  133. // Will cascade.
  134. static const char *sql_delete_person =
  135. "delete from person where id = ?1 ;";
  136. static const char *sql_set_as_pEp_user =
  137. "update person set is_pEp_user = 1 "
  138. " where id = ?1 ; ";
  139. static const char *sql_is_pEp_user =
  140. "select is_pEp_user from person "
  141. " where id = ?1 ; ";
  142. static const char* sql_exists_person =
  143. "select count(*) from person "
  144. " where id = ?1 ;";
  145. // This will cascade to identity and trust
  146. static const char* sql_replace_userid =
  147. "update person set id = ?1 "
  148. " where id = ?2;";
  149. // Hopefully this cascades and removes trust entries...
  150. static const char *sql_delete_key =
  151. "delete from pgp_keypair "
  152. " where fpr = ?1 ; ";
  153. static const char *sql_replace_main_user_fpr =
  154. "update person "
  155. " set main_key_id = ?1 "
  156. " where id = ?2 ;";
  157. static const char *sql_get_main_user_fpr =
  158. "select main_key_id from person"
  159. " where id = ?1 ;";
  160. static const char *sql_replace_main_user_fpr_if_equal =
  161. "update person "
  162. " set main_key_id = ?1 "
  163. " where id = ?2 and main_key_id = ?3;";
  164. static const char *sql_refresh_userid_default_key =
  165. "update person "
  166. " set main_key_id = "
  167. " (select identity.main_key_id from identity "
  168. " join trust on trust.user_id = identity.user_id "
  169. " and trust.pgp_keypair_fpr = identity.main_key_id "
  170. " join person on person.id = identity.user_id "
  171. " where identity.user_id = ?1 "
  172. " order by trust.comm_type desc "
  173. " limit 1) "
  174. "where id = ?1 ; ";
  175. static const char *sql_set_pgp_keypair =
  176. "insert or ignore into pgp_keypair (fpr) "
  177. "values (upper(replace(?1,' ',''))) ;";
  178. static const char *sql_set_pgp_keypair_flags =
  179. "update pgp_keypair set flags = "
  180. " ((?1 & 65535) | (select flags from pgp_keypair "
  181. " where fpr = (upper(replace(?2,' ',''))))) "
  182. " where fpr = (upper(replace(?2,' ',''))) ;";
  183. static const char *sql_unset_pgp_keypair_flags =
  184. "update pgp_keypair set flags = "
  185. " ( ~(?1 & 65535) & (select flags from pgp_keypair"
  186. " where fpr = (upper(replace(?2,' ',''))))) "
  187. " where fpr = (upper(replace(?2,' ',''))) ;";
  188. static const char* sql_exists_identity_entry =
  189. "select count(*) from identity "
  190. " where (case when (address = ?1) then (1)"
  191. " when (lower(address) = lower(?1)) then (1)"
  192. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
  193. " else 0"
  194. " end) = 1"
  195. " and user_id = ?2;";
  196. static const char *sql_set_identity_entry =
  197. "insert into identity ("
  198. " address, main_key_id, "
  199. " user_id, "
  200. " username, "
  201. " flags, is_own,"
  202. " pEp_version_major, pEp_version_minor"
  203. " ) values ("
  204. " ?1,"
  205. " upper(replace(?2,' ','')),"
  206. " ?3,"
  207. " ?4,"
  208. " ?5,"
  209. " ?6,"
  210. " ?7,"
  211. " ?8 "
  212. " );";
  213. static const char* sql_update_identity_entry =
  214. "update identity "
  215. " set main_key_id = upper(replace(?2,' ','')), "
  216. " username = coalesce(username, ?4), "
  217. " flags = ?5, "
  218. " is_own = ?6, "
  219. " pEp_version_major = ?7, "
  220. " pEp_version_minor = ?8 "
  221. " where (case when (address = ?1) then (1)"
  222. " when (lower(address) = lower(?1)) then (1)"
  223. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
  224. " else 0 "
  225. " end) = 1 "
  226. " and user_id = ?3 ;";
  227. static const char* sql_force_set_identity_username =
  228. "update identity "
  229. " set username = coalesce(username, ?3) "
  230. " where (case when (address = ?1) then (1)"
  231. " when (lower(address) = lower(?1)) then (1)"
  232. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
  233. " else 0 "
  234. " end) = 1 "
  235. " and user_id = ?2 ;";
  236. // " (select"
  237. // " coalesce("
  238. // " (select flags from identity"
  239. // " where address = ?1 and"
  240. // " user_id = ?3),"
  241. // " 0)"
  242. // " ) | (?4 & 255)"
  243. /* set_identity ignores previous flags, and doesn't filter machine flags */
  244. static const char *sql_set_identity_flags =
  245. "update identity set flags = "
  246. " ((?1 & 65535) | (select flags from identity"
  247. " where (case when (address = ?2) then (1)"
  248. " when (lower(address) = lower(?2)) then (1)"
  249. " when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
  250. " else 0 "
  251. " end) = 1 "
  252. " and user_id = ?3)) "
  253. " where (case when (address = ?2) then (1)"
  254. " when (lower(address) = lower(?2)) then (1)"
  255. " when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
  256. " else 0"
  257. " end) = 1"
  258. " and user_id = ?3 ;";
  259. static const char *sql_unset_identity_flags =
  260. "update identity set flags = "
  261. " ( ~(?1 & 65535) & (select flags from identity"
  262. " where (case when (address = ?2) then (1)"
  263. " when (lower(address) = lower(?2)) then (1)"
  264. " when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
  265. " else 0 "
  266. " end) = 1 "
  267. " and user_id = ?3)) "
  268. " where (case when (address = ?2) then (1)"
  269. " when (lower(address) = lower(?2)) then (1)"
  270. " when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
  271. " else 0"
  272. " end) = 1"
  273. " and user_id = ?3 ;";
  274. static const char *sql_set_ident_enc_format =
  275. "update identity "
  276. " set enc_format = ?1 "
  277. " where (case when (address = ?2) then (1)"
  278. " when (lower(address) = lower(?2)) then (1)"
  279. " when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1) "
  280. " else 0 "
  281. " end) = 1 "
  282. " and user_id = ?3 ;";
  283. static const char *sql_set_pEp_version =
  284. "update identity "
  285. " set pEp_version_major = ?1, "
  286. " pEp_version_minor = ?2 "
  287. " where (case when (address = ?3) then (1)"
  288. " when (lower(address) = lower(?3)) then (1)"
  289. " when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
  290. " else 0 "
  291. " end) = 1 "
  292. " and user_id = ?4 ;";
  293. static const char *sql_upgrade_pEp_version_by_user_id =
  294. "update identity "
  295. " set pEp_version_major = ?1, "
  296. " pEp_version_minor = ?2 "
  297. " where user_id = ?3 "
  298. " and (case when (pEp_version_major < ?1) then (1)"
  299. " when (pEp_version_major > ?1) then (0)"
  300. " when (pEp_version_minor < ?2) then (1)"
  301. " else 0 "
  302. " end) = 1 ;";
  303. static const char *sql_set_trust =
  304. "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
  305. "values (?1, upper(replace(?2,' ','')), ?3) ;";
  306. static const char *sql_update_trust =
  307. "update trust set comm_type = ?3 "
  308. " where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
  309. static const char *sql_clear_trust_info =
  310. "delete from trust "
  311. " where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
  312. static const char *sql_update_trust_to_pEp =
  313. "update trust set comm_type = comm_type + 71 "
  314. " where (user_id = ?1 "
  315. " and (case when (comm_type = 56) then (1) "
  316. " when (comm_type = 184) then (1) "
  317. " else 0"
  318. " end) = 1); ";
  319. static const char* sql_exists_trust_entry =
  320. "select count(*) from trust "
  321. " where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
  322. static const char *sql_update_trust_for_fpr =
  323. "update trust "
  324. "set comm_type = ?1 "
  325. "where pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
  326. static const char *sql_get_trust =
  327. "select comm_type from trust where user_id = ?1 "
  328. "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
  329. static const char *sql_get_trust_by_userid =
  330. "select pgp_keypair_fpr, comm_type from trust where user_id = ?1 ";
  331. static const char *sql_least_trust =
  332. "select min(comm_type) from trust where"
  333. " pgp_keypair_fpr = upper(replace(?1,' ',''))"
  334. " and comm_type != 0;"; // ignores PEP_ct_unknown
  335. // returns PEP_ct_unknown only when no known trust is recorded
  336. static const char *sql_update_key_sticky_bit_for_user =
  337. "update trust set sticky = ?1 "
  338. " where user_id = ?2 and pgp_keypair_fpr = upper(replace(?3,' ','')) ;";
  339. static const char *sql_is_key_sticky_for_user =
  340. "select sticky from trust "
  341. " where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ','')) ; ";
  342. static const char *sql_mark_as_compromised =
  343. "update trust not indexed set comm_type = 15"
  344. " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
  345. static const char *sql_crashdump =
  346. "select timestamp, title, entity, description, comment"
  347. " from log order by timestamp desc limit ?1 ;";
  348. static const char *sql_languagelist =
  349. "select i18n_language.lang, name, phrase"
  350. " from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
  351. static const char *sql_i18n_token =
  352. "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
  353. // Own keys
  354. // We only care if it's 0 or non-zero
  355. static const char *sql_own_key_is_listed =
  356. "select count(*) from ("
  357. " select pgp_keypair_fpr from trust"
  358. " join identity on trust.user_id = identity.user_id"
  359. " where pgp_keypair_fpr = upper(replace(?1,' ',''))"
  360. " and identity.is_own = 1"
  361. ");";
  362. static const char *sql_is_own_address =
  363. "select count(*) from ("
  364. " select address from identity"
  365. " where (case when (address = ?1) then (1)"
  366. " when (lower(address) = lower(?1)) then (1)"
  367. " when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
  368. " else 0"
  369. " end) = 1 "
  370. " and identity.is_own = 1"
  371. ");";
  372. static const char *sql_own_identities_retrieve =
  373. "select address, identity.main_key_id, identity.user_id,"
  374. " (case when (identity.flags & 1024 = 0) then ifnull(identity.username, person.username) "
  375. " else identity.username end),"
  376. " lang, identity.flags,"
  377. // " identity.flags | pgp_keypair.flags,"
  378. " pEp_version_major, pEp_version_minor"
  379. " from identity"
  380. " join person on id = identity.user_id"
  381. " left join pgp_keypair on fpr = identity.main_key_id"
  382. " left join trust on id = trust.user_id"
  383. " and pgp_keypair_fpr = identity.main_key_id"
  384. " where identity.is_own = 1"
  385. " and (identity.flags & ?1) = 0;";
  386. static const char *sql_own_keys_retrieve =
  387. "select distinct pgp_keypair_fpr from trust"
  388. " join identity on trust.user_id = identity.user_id"
  389. " where identity.is_own = 1";
  390. static const char* sql_get_user_default_key =
  391. "select main_key_id from person"
  392. " where id = ?1;";
  393. static const char* sql_get_all_keys_for_user =
  394. "select pgp_keypair_fpr from trust"
  395. " where user_id = ?1; ";
  396. static const char* sql_get_default_own_userid =
  397. "select id from person"
  398. " join identity on id = identity.user_id"
  399. " where identity.is_own = 1";
  400. // Sequence
  401. static const char *sql_sequence_value1 =
  402. "insert or replace into sequences (name, value) "
  403. "values (?1, "
  404. " (select coalesce((select value + 1 from sequences "
  405. " where name = ?1), 1 ))); ";
  406. static const char *sql_sequence_value2 =
  407. "select value from sequences where name = ?1 ;";
  408. // Revocation tracking
  409. static const char *sql_set_revoked =
  410. "insert or replace into revoked_keys ("
  411. " revoked_fpr, replacement_fpr, revocation_date) "
  412. "values (upper(replace(?1,' ','')),"
  413. " upper(replace(?2,' ','')),"
  414. " ?3) ;";
  415. static const char *sql_get_revoked =
  416. "select revoked_fpr, revocation_date from revoked_keys"
  417. " where replacement_fpr = upper(replace(?1,' ','')) ;";
  418. static const char *sql_get_replacement_fpr =
  419. "select replacement_fpr, revocation_date from revoked_keys"
  420. " where revoked_fpr = upper(replace(?1,' ','')) ;";
  421. static const char *sql_get_userid_alias_default =
  422. "select default_id from alternate_user_id "
  423. " where alternate_id = ?1 ; ";
  424. // Revocation tracking
  425. static const char *sql_add_mistrusted_key =
  426. "insert or replace into mistrusted_keys (fpr) "
  427. " values (upper(replace(?1,' ',''))) ;";
  428. static const char *sql_delete_mistrusted_key =
  429. "delete from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
  430. static const char *sql_is_mistrusted_key =
  431. "select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
  432. static const char *sql_add_userid_alias =
  433. "insert or replace into alternate_user_id (alternate_id, default_id) "
  434. "values (?2, ?1) ;";
  435. static const char *sql_add_into_social_graph =
  436. "insert or replace into social_graph(own_userid, own_address, contact_userid) "
  437. "values (?1, ?2, ?3) ;";
  438. static const char *sql_get_own_address_binding_from_contact =
  439. "select own_address from social_graph where own_userid = ?1 and contact_userid = ?2 ;";
  440. static const char *sql_set_revoke_contact_as_notified =
  441. "insert or replace into revocation_contact_list(fpr, own_address, contact_id) values (?1, ?2, ?3) ;";
  442. static const char *sql_get_contacted_ids_from_revoke_fpr =
  443. "select * from revocation_contact_list where fpr = ?1 ;";
  444. static const char *sql_was_id_for_revoke_contacted =
  445. "select count(*) from revocation_contact_list where fpr = ?1 and own_address = ?2 and contact_id = ?3 ;";
  446. static const char *sql_has_id_contacted_address =
  447. "select count(*) from social_graph where own_address = ?1 and contact_userid = ?2 ;";
  448. // We only need user_id and address, since in the main usage, we'll call update_identity
  449. // on this anyway when sending out messages.
  450. static const char *sql_get_last_contacted =
  451. "select user_id, address from identity where datetime('now') < datetime(timestamp, '+14 days') ; ";
  452. static const char *sql_create_group =
  453. "insert into groups (group_id, group_address, manager_userid, manager_address) "
  454. "VALUES (?1, ?2, ?3, ?4) ;";
  455. static const char *sql_enable_group =
  456. "update groups set active = 1 "
  457. " where group_id = ?1 and group_address = ?2 ;";
  458. static const char *sql_disable_group =
  459. "update groups set active = 0 "
  460. " where group_id = ?1 and group_address = ?2 ;";
  461. static const char *sql_exists_group_entry =
  462. "select count(*) from groups "
  463. " where group_id = ?1 and group_address = ?2;";
  464. static const char *sql_group_add_member =
  465. "insert or ignore into own_groups_members (group_id, group_address, member_id, member_address) "
  466. " values (?1, ?2, ?3, ?4) ;";
  467. static const char *sql_group_delete_member =
  468. "delete from own_groups_members "
  469. " where group_id = ?1 and group_address = ?2 and "
  470. " member_id = ?3 and member_address = ?4 ;";
  471. static const char *sql_set_group_member_status =
  472. "update own_groups_members set active_member = ?1 "
  473. " where group_id = ?2 and group_address = ?3 and "
  474. " member_id = ?4 and member_address = ?5; ";
  475. static const char *sql_group_join =
  476. "update own_memberships set have_joined = 1 "
  477. " where group_id = ?1 and group_address = ?2 and "
  478. " own_id = ?3 and own_address = ?4; ";
  479. static const char *sql_leave_group =
  480. "update own_memberships set have_joined = 0 "
  481. " where group_id = ?1 and group_address = ?2 and "
  482. " own_id = ?3 and own_address = ?4; ";
  483. static const char *sql_get_all_members =
  484. "select member_id, member_address, active_member from own_groups_members "
  485. " where group_id = ?1 and group_address = ?2; ";
  486. static const char *sql_get_active_members =
  487. "select member_id, member_address from own_groups_members "
  488. " where group_id = ?1 and group_address = ?2 and active_member = 1; ";
  489. static const char *sql_get_group_manager =
  490. "select manager_userid, manager_address from groups "
  491. " where group_id = ?1 and group_address = ?2; ";
  492. static const char *sql_is_invited_group_member =
  493. "select count(*) from own_groups_members "
  494. " where group_id = ?1 and group_address = ?2 and member_id = ?3 and member_address = ?4; ";
  495. static const char *sql_is_active_group_member =
  496. "select active_member from own_groups_members "
  497. " where group_id = ?1 and group_address = ?2 and member_id = ?3 and member_address = ?4; ";
  498. static const char *sql_get_all_groups =
  499. "select group_id, group_address from own_memberships; ";
  500. static const char *sql_get_active_groups =
  501. "select group_id, group_address from own_memberships where have_joined = 1; ";
  502. static const char *sql_add_own_membership_entry =
  503. "insert or replace into own_memberships (group_id, group_address, own_id, own_address, have_joined) "
  504. " values (?1, ?2, ?3, ?4, 0) ; ";
  505. static const char *sql_is_group_active =
  506. "select count(*) from groups "
  507. " where group_id = ?1 and group_address = ?2 and active = 1; ";
  508. // This below can return multiple entries for multiple idents in same group
  509. // FIXME: decide what we really need here
  510. static const char *sql_retrieve_own_membership_info_for_group =
  511. "select own_id, own_address, have_joined "
  512. " from own_memberships "
  513. " inner join groups using (group_id, group_address) "
  514. " where group_id = ?1 and group_address = ?2; ";
  515. static const char *sql_retrieve_own_membership_info_for_group_and_ident =
  516. "select have_joined, manager_userid, manager_address, active "
  517. " from own_memberships "
  518. " inner join groups using (group_id, group_address) "
  519. " where group_id = ?1 and group_address = ?2 and own_id = ?3 and own_address = ?4; ";
  520. // This will return all membership info for all identities
  521. static const char *sql_retrieve_all_own_membership_info =
  522. "select group_id, group_address, own_id, own_address, have_joined, manager_id, manager_address, active "
  523. " from own_memberships "
  524. " inner join using (group_id, group_address); ";
  525. static const char* sql_get_own_membership_status =
  526. "select have_joined from own_memberships "
  527. " where group_id = ?1 and group_address = ?2 and "
  528. " own_id = ?3 and own_address = ?4; ";