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.

2649 lines
91 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. #include "pEp_internal.h"
  2. #include "engine_sql.h"
  3. // sql overloaded functions - modified from sqlite3.c
  4. /**
  5. * @internal
  6. *
  7. * <!-- _sql_lower() -->
  8. *
  9. * @brief TODO
  10. *
  11. * @param[in] *ctx sqlite3_context
  12. * @param[in] argc int
  13. * @param[in] **argv sqlite3_value
  14. *
  15. */
  16. static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
  17. const char *z2;
  18. int n;
  19. z2 = (char*)sqlite3_value_text(argv[0]);
  20. n = sqlite3_value_bytes(argv[0]);
  21. /* Verify that the call to _bytes() does not invalidate the _text() pointer */
  22. assert( z2==(char*)sqlite3_value_text(argv[0]) );
  23. if( z2 ){
  24. char *z1 = (char*)sqlite3_malloc(n+1);
  25. if( z1 ){
  26. for(int i=0; i<n; i++){
  27. char c = z2[i];
  28. char c_mod = c | 0x20;
  29. if (c_mod < 0x61 || c_mod > 0x7a)
  30. c_mod = c;
  31. z1[i] = c_mod;
  32. }
  33. z1[n] = '\0';
  34. sqlite3_result_text(ctx, z1, n, sqlite3_free);
  35. }
  36. }
  37. }
  38. #ifdef _PEP_SQLITE_DEBUG
  39. /**
  40. * @internal
  41. *
  42. * <!-- sql_trace_callback() -->
  43. *
  44. * @brief TODO
  45. *
  46. * @param[in] trace_constant unsigned
  47. * @param[in] *context_ptr void
  48. * @param[in] *P void
  49. * @param[in] *X void
  50. *
  51. */
  52. int sql_trace_callback (unsigned trace_constant,
  53. void* context_ptr,
  54. void* P,
  55. void* X) {
  56. switch (trace_constant) {
  57. case SQLITE_TRACE_STMT:
  58. fprintf(stderr, "SQL_DEBUG: STMT - ");
  59. const char* X_str = (const char*) X;
  60. if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
  61. fprintf(stderr, "%s\n", X_str);
  62. else
  63. fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
  64. break;
  65. case SQLITE_TRACE_ROW:
  66. fprintf(stderr, "SQL_DEBUG: ROW - ");
  67. fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
  68. break;
  69. case SQLITE_TRACE_CLOSE:
  70. fprintf(stderr, "SQL_DEBUG: CLOSE - ");
  71. break;
  72. default:
  73. break;
  74. }
  75. return 0;
  76. }
  77. #endif
  78. /**
  79. * @internal
  80. *
  81. * <!-- errorLogCallback() -->
  82. *
  83. * @brief TODO
  84. *
  85. * @param[in] *pArg void
  86. * @param[in] iErrCode int
  87. * @param[in] *zMsg constchar
  88. *
  89. */
  90. void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
  91. fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
  92. }
  93. // TODO: refactor and generalise these two functions if possible.
  94. /**
  95. * @internal
  96. *
  97. * <!-- db_contains_table() -->
  98. *
  99. * @brief TODO
  100. *
  101. * @param[in] session PEP_SESSION
  102. * @param[in] *table_name constchar
  103. *
  104. */
  105. static int db_contains_table(PEP_SESSION session, const char* table_name) {
  106. if (!session || !table_name)
  107. return -1;
  108. // Table names can't be SQL parameters, so we do it this way.
  109. // these two must be the same number.
  110. char sql_buf[500];
  111. const size_t max_q_len = 500;
  112. size_t t_size, q_size;
  113. const char* q1 = "SELECT name FROM sqlite_master WHERE type='table' AND name='{"; // 61
  114. const char* q2 = "}';"; // 3
  115. q_size = 64;
  116. t_size = strlen(table_name);
  117. size_t query_len = q_size + t_size + 1;
  118. if (query_len > max_q_len)
  119. return -1;
  120. strlcpy(sql_buf, q1, max_q_len);
  121. strlcat(sql_buf, table_name, max_q_len);
  122. strlcat(sql_buf, q2, max_q_len);
  123. sqlite3_stmt *stmt;
  124. sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
  125. int retval = 0;
  126. int rc = sqlite3_step(stmt);
  127. if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
  128. retval = 1;
  129. }
  130. sqlite3_finalize(stmt);
  131. return retval;
  132. }
  133. /**
  134. * @internal
  135. *
  136. * <!-- table_contains_column() -->
  137. *
  138. * @brief TODO
  139. *
  140. * @param[in] session PEP_SESSION
  141. * @param[in] *table_name constchar
  142. * @param[in] *col_name constchar
  143. *
  144. */
  145. static int table_contains_column(PEP_SESSION session, const char* table_name,
  146. const char* col_name) {
  147. if (!session || !table_name || !col_name)
  148. return -1;
  149. // Table names can't be SQL parameters, so we do it this way.
  150. // these two must be the same number.
  151. char sql_buf[500];
  152. const size_t max_q_len = 500;
  153. size_t t_size, c_size, q_size;
  154. const char* q1 = "SELECT "; // 7
  155. const char* q2 = " from "; // 6
  156. const char* q3 = ";"; // 1
  157. q_size = 14;
  158. t_size = strlen(table_name);
  159. c_size = strlen(col_name);
  160. size_t query_len = q_size + c_size + t_size + 1;
  161. if (query_len > max_q_len)
  162. return -1;
  163. strlcpy(sql_buf, q1, max_q_len);
  164. strlcat(sql_buf, col_name, max_q_len);
  165. strlcat(sql_buf, q2, max_q_len);
  166. strlcat(sql_buf, table_name, max_q_len);
  167. strlcat(sql_buf, q3, max_q_len);
  168. sqlite3_stmt *stmt;
  169. sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
  170. int retval = 0;
  171. int rc = sqlite3_step(stmt);
  172. if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
  173. retval = 1;
  174. }
  175. sqlite3_finalize(stmt);
  176. return retval;
  177. }
  178. /**
  179. * @internal
  180. *
  181. * <!-- repair_altered_tables() -->
  182. *
  183. * @brief TODO
  184. *
  185. * @param[in] session PEP_SESSION
  186. *
  187. */
  188. #define _PEP_MAX_AFFECTED 5
  189. PEP_STATUS repair_altered_tables(PEP_SESSION session) {
  190. PEP_STATUS status = PEP_STATUS_OK;
  191. char* table_names[_PEP_MAX_AFFECTED] = {0};
  192. const char* sql_query = "select tbl_name from sqlite_master WHERE sql LIKE '%REFERENCES%' AND sql LIKE '%_old%';";
  193. sqlite3_stmt *stmt;
  194. sqlite3_prepare_v2(session->db, sql_query, -1, &stmt, NULL);
  195. int i = 0;
  196. int int_result = 0;
  197. while ((int_result = sqlite3_step(stmt)) == SQLITE_ROW && i < _PEP_MAX_AFFECTED) {
  198. table_names[i++] = strdup((const char*)(sqlite3_column_text(stmt, 0)));
  199. }
  200. sqlite3_finalize(stmt);
  201. if ((int_result != SQLITE_DONE && int_result != SQLITE_OK) || i > (_PEP_MAX_AFFECTED + 1)) {
  202. status = PEP_UNKNOWN_DB_ERROR;
  203. goto pEp_free;
  204. }
  205. for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
  206. const char* table_name = table_names[i];
  207. if (!table_name)
  208. break;
  209. if (strcmp(table_name, "identity") == 0) {
  210. int_result = sqlite3_exec(session->db,
  211. "PRAGMA foreign_keys=off;\n"
  212. "BEGIN TRANSACTION;\n"
  213. "create table _identity_new (\n"
  214. " address text,\n"
  215. " user_id text\n"
  216. " references person (id)\n"
  217. " on delete cascade on update cascade,\n"
  218. " main_key_id text\n"
  219. " references pgp_keypair (fpr)\n"
  220. " on delete set null,\n"
  221. " comment text,\n"
  222. " flags integer default 0,\n"
  223. " is_own integer default 0,\n"
  224. " timestamp integer default (datetime('now')),\n"
  225. " primary key (address, user_id)\n"
  226. ");\n"
  227. "INSERT INTO _identity_new SELECT * from identity;\n"
  228. "DROP TABLE identity;\n"
  229. "ALTER TABLE _identity_new RENAME TO identity;\n"
  230. "COMMIT;\n"
  231. "PRAGMA foreign_keys=on;"
  232. ,
  233. NULL,
  234. NULL,
  235. NULL
  236. );
  237. if (int_result != SQLITE_OK)
  238. return PEP_UNKNOWN_DB_ERROR;
  239. }
  240. else if (strcmp(table_name, "trust") == 0) {
  241. int_result = sqlite3_exec(session->db,
  242. "PRAGMA foreign_keys=off;\n"
  243. "BEGIN TRANSACTION;\n"
  244. "create table _trust_new (\n"
  245. " user_id text not null\n"
  246. " references person (id)\n"
  247. " on delete cascade on update cascade,\n"
  248. " pgp_keypair_fpr text not null\n"
  249. " references pgp_keypair (fpr)\n"
  250. " on delete cascade,\n"
  251. " comm_type integer not null,\n"
  252. " comment text,\n"
  253. " primary key (user_id, pgp_keypair_fpr)\n"
  254. ");\n"
  255. "INSERT INTO _trust_new SELECT * from trust;\n"
  256. "DROP TABLE trust;\n"
  257. "ALTER TABLE _trust_new RENAME TO trust;\n"
  258. "COMMIT;\n"
  259. "PRAGMA foreign_keys=on;"
  260. ,
  261. NULL,
  262. NULL,
  263. NULL
  264. );
  265. if (int_result != SQLITE_OK)
  266. return PEP_UNKNOWN_DB_ERROR;
  267. }
  268. else if (strcmp(table_name, "alternate_user_id") == 0) {
  269. int_result = sqlite3_exec(session->db,
  270. "PRAGMA foreign_keys=off;\n"
  271. "BEGIN TRANSACTION;\n"
  272. "create table _alternate_user_id_new (\n"
  273. " default_id text references person (id)\n"
  274. " on delete cascade on update cascade,\n"
  275. " alternate_id text primary key\n"
  276. ");\n"
  277. "INSERT INTO _alternate_user_id_new SELECT * from alternate_user_id;\n"
  278. "DROP TABLE alternate_user_id;\n"
  279. "ALTER TABLE _alternate_user_id_new RENAME TO alternate_user_id;\n"
  280. "COMMIT;\n"
  281. "PRAGMA foreign_keys=on;"
  282. ,
  283. NULL,
  284. NULL,
  285. NULL
  286. );
  287. if (int_result != SQLITE_OK)
  288. return PEP_UNKNOWN_DB_ERROR;
  289. }
  290. else if (strcmp(table_name, "revocation_contact_list") == 0) {
  291. int_result = sqlite3_exec(session->db,
  292. "PRAGMA foreign_keys=off;\n"
  293. "BEGIN TRANSACTION;\n"
  294. "create table _revocation_contact_list_new (\n"
  295. " fpr text not null references pgp_keypair (fpr)\n"
  296. " on delete cascade,\n"
  297. " contact_id text not null references person (id)\n"
  298. " on delete cascade on update cascade,\n"
  299. " timestamp integer default (datetime('now')),\n"
  300. " PRIMARY KEY(fpr, contact_id)\n"
  301. ");\n"
  302. "INSERT INTO _revocation_contact_list_new SELECT * from revocation_contact_list;\n"
  303. "DROP TABLE revocation_contact_list;\n"
  304. "ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
  305. "COMMIT;\n"
  306. "PRAGMA foreign_keys=on;"
  307. ,
  308. NULL,
  309. NULL,
  310. NULL
  311. );
  312. if (int_result != SQLITE_OK)
  313. return PEP_UNKNOWN_DB_ERROR;
  314. }
  315. else if (strcmp(table_name, "social_graph")) {
  316. int_result = sqlite3_exec(session->db,
  317. "PRAGMA foreign_keys=off;\n"
  318. "BEGIN TRANSACTION;\n"
  319. "create table _social_new (\n"
  320. " own_userid text,\n"
  321. " own_address text,\n"
  322. " contact_userid text,\n"
  323. " CONSTRAINT fk_own_identity\n"
  324. " FOREIGN KEY(own_address, own_userid)\n"
  325. " REFERENCES identity(address, user_id)\n"
  326. " ON DELETE CASCADE ON UPDATE CASCADE\n"
  327. ");\n"
  328. "INSERT INTO _social_graph_new SELECT * from social_graph;\n"
  329. "DROP TABLE social_graph;\n"
  330. "ALTER TABLE _social_graph_new RENAME TO social_graph;\n"
  331. "COMMIT;\n"
  332. "PRAGMA foreign_keys=on;"
  333. ,
  334. NULL,
  335. NULL,
  336. NULL
  337. );
  338. if (int_result != SQLITE_OK)
  339. return PEP_UNKNOWN_DB_ERROR;
  340. }
  341. }
  342. int_result = sqlite3_exec(
  343. session->db,
  344. "PRAGMA foreign_key_check;\n"
  345. ,
  346. NULL,
  347. NULL,
  348. NULL
  349. );
  350. if (int_result != SQLITE_OK)
  351. return PEP_UNKNOWN_DB_ERROR;
  352. pEp_free:
  353. for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
  354. free(table_names[i]);
  355. }
  356. return status;
  357. }
  358. /**
  359. * @internal
  360. *
  361. * <!-- upgrade_revoc_contact_to_13() -->
  362. *
  363. * @brief TODO
  364. *
  365. * @param[in] session PEP_SESSION
  366. *
  367. */
  368. static PEP_STATUS upgrade_revoc_contact_to_13(PEP_SESSION session) {
  369. // I HATE SQLITE.
  370. PEP_STATUS status = PEP_STATUS_OK;
  371. int int_result = 0;
  372. // Ok, first we ADD the column so we can USE it.
  373. // We will end up propagating the "error" this first time
  374. // (one-to-one revoke-replace relationships), but since key reset
  375. // hasn't been used in production, this is not a customer-facing
  376. // issue.
  377. // Note: the check upfront is to deal with partially-upgraded DB issues
  378. if (!table_contains_column(session, "revocation_contact_list", "own_address")) {
  379. int_result = sqlite3_exec(
  380. session->db,
  381. "alter table revocation_contact_list\n"
  382. " add column own_address text;\n",
  383. NULL,
  384. NULL,
  385. NULL
  386. );
  387. assert(int_result == SQLITE_OK);
  388. if (int_result != SQLITE_OK)
  389. return PEP_UNKNOWN_DB_ERROR;
  390. }
  391. // the best we can do here is search per address, since these
  392. // are no longer associated with an identity. For now, if we find
  393. // something we can't add an address to, we'll delete the record.
  394. // this should not, in the current environment, ever happen, but
  395. // since we need to make the address part of the primary key, it's
  396. // the right thing to do. sqlite does support null fields in a primary
  397. // key for a weird version compatibility reason, but that doesn't
  398. // mean we should use it, and we should be *safe*, not relying
  399. // on an implementation-specific quirk which might be sanely removed
  400. // in a future sqlite version.
  401. identity_list* id_list = NULL;
  402. sqlite3_stmt* tmp_own_id_retrieve = NULL;
  403. sqlite3_prepare_v2(session->db, sql_own_identities_retrieve, -1, &tmp_own_id_retrieve, NULL);
  404. // Kludgey - put the stmt in temporarily, and then remove again, so less code dup.
  405. // FIXME LATER: refactor if possible, but... chicken and egg, and thiis case rarely happens.
  406. session->own_identities_retrieve = tmp_own_id_retrieve;
  407. status = own_identities_retrieve(session, &id_list);
  408. sqlite3_finalize(tmp_own_id_retrieve);
  409. session->own_identities_retrieve = NULL;
  410. if (!status || !id_list)
  411. return PEP_STATUS_OK; // it's empty AFAIK (FIXME)
  412. identity_list* curr_own = id_list;
  413. sqlite3_stmt* update_revoked_w_addr_stmt = NULL;
  414. const char* sql_query = "update revocation_contact_list set own_address = ?1 where fpr = ?2;";
  415. sqlite3_prepare_v2(session->db, sql_query, -1, &update_revoked_w_addr_stmt, NULL);
  416. // Ok, go through and find any keys associated with this address
  417. for ( ; curr_own && curr_own->ident; curr_own = curr_own->next) {
  418. if (EMPTYSTR(curr_own->ident->address)) // shouldn't happen
  419. continue;
  420. stringlist_t* keylist = NULL;
  421. status = find_keys(session, curr_own->ident->address, &keylist);
  422. stringlist_t* curr_key = keylist;
  423. for ( ; curr_key && curr_key->value; curr_key = curr_key->next) {
  424. if (EMPTYSTR(curr_key->value))
  425. continue;
  426. // We just do this lazily - if this isn't a revoked key, it
  427. // won't do anything.
  428. sqlite3_bind_text(update_revoked_w_addr_stmt, 1, curr_own->ident->address, -1,
  429. SQLITE_STATIC);
  430. sqlite3_bind_text(update_revoked_w_addr_stmt, 2, curr_key->value, -1,
  431. SQLITE_STATIC);
  432. int_result = sqlite3_step(update_revoked_w_addr_stmt);
  433. assert(int_result == SQLITE_DONE);
  434. sqlite3_reset(update_revoked_w_addr_stmt);
  435. if (int_result != SQLITE_DONE)
  436. return PEP_UNKNOWN_DB_ERROR;
  437. }
  438. }
  439. sqlite3_finalize(update_revoked_w_addr_stmt);
  440. int_result = sqlite3_exec(
  441. session->db,
  442. "delete from revocation_contact_list where own_address is NULL;\n"
  443. "PRAGMA foreign_keys=off;\n"
  444. "BEGIN TRANSACTION;\n"
  445. "create table if not exists _revocation_contact_list_new (\n"
  446. " fpr text not null references pgp_keypair (fpr)\n"
  447. " on delete cascade,\n"
  448. " own_address text,\n"
  449. " contact_id text not null references person (id)\n"
  450. " on delete cascade on update cascade,\n"
  451. " timestamp integer default (datetime('now')),\n"
  452. " PRIMARY KEY(fpr, own_address, contact_id)\n"
  453. ");\n"
  454. "INSERT INTO _revocation_contact_list_new (fpr, "
  455. " own_address, "
  456. " contact_id) "
  457. " SELECT revocation_contact_list.fpr, "
  458. " revocation_contact_list.own_address, "
  459. " revocation_contact_list.contact_id "
  460. " FROM revocation_contact_list "
  461. " WHERE 1;\n"
  462. "DROP TABLE revocation_contact_list;\n"
  463. "ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
  464. "COMMIT;\n"
  465. "\n"
  466. "PRAGMA foreign_keys=on;\n"
  467. ,
  468. NULL,
  469. NULL,
  470. NULL
  471. );
  472. if (int_result != SQLITE_OK)
  473. return PEP_UNKNOWN_DB_ERROR;
  474. return status;
  475. }
  476. /**
  477. * @internal
  478. *
  479. * <!-- user_version() -->
  480. *
  481. * @brief TODO
  482. *
  483. * @param[in] *_version void
  484. * @param[in] count int
  485. * @param[in] **text char
  486. * @param[in] **name char
  487. *
  488. */
  489. static int user_version(void *_version, int count, char **text, char **name)
  490. {
  491. if (!(_version && count == 1 && text && text[0]))
  492. return -1;
  493. int *version = (int *) _version;
  494. *version = atoi(text[0]);
  495. return 0;
  496. }
  497. PEP_STATUS init_databases(PEP_SESSION session) {
  498. assert(LOCAL_DB);
  499. if (LOCAL_DB == NULL)
  500. return PEP_INIT_CANNOT_OPEN_DB;
  501. #ifdef _PEP_SQLITE_DEBUG
  502. sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
  503. #endif
  504. int int_result = sqlite3_open_v2(
  505. LOCAL_DB,
  506. &session->db,
  507. SQLITE_OPEN_READWRITE
  508. | SQLITE_OPEN_CREATE
  509. | SQLITE_OPEN_FULLMUTEX
  510. | SQLITE_OPEN_PRIVATECACHE,
  511. NULL
  512. );
  513. if (int_result != SQLITE_OK)
  514. return PEP_INIT_CANNOT_OPEN_DB;
  515. int_result = sqlite3_exec(
  516. session->db,
  517. "PRAGMA locking_mode=NORMAL;\n"
  518. "PRAGMA journal_mode=WAL;\n",
  519. NULL,
  520. NULL,
  521. NULL
  522. );
  523. sqlite3_busy_timeout(session->db, BUSY_WAIT_TIME);
  524. #ifdef _PEP_SQLITE_DEBUG
  525. sqlite3_trace_v2(session->db,
  526. SQLITE_TRACE_STMT | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
  527. sql_trace_callback,
  528. NULL);
  529. #endif
  530. assert(SYSTEM_DB);
  531. if (SYSTEM_DB == NULL)
  532. return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
  533. int_result = sqlite3_open_v2(
  534. SYSTEM_DB, &session->system_db,
  535. SQLITE_OPEN_READONLY
  536. | SQLITE_OPEN_FULLMUTEX
  537. | SQLITE_OPEN_SHAREDCACHE,
  538. NULL
  539. );
  540. if (int_result != SQLITE_OK)
  541. return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
  542. sqlite3_busy_timeout(session->system_db, 1000);
  543. return PEP_STATUS_OK;
  544. }
  545. static PEP_STATUS _create_initial_tables(PEP_SESSION session) {
  546. int int_result = sqlite3_exec(
  547. session->db,
  548. "create table if not exists version_info (\n"
  549. " id integer primary key,\n"
  550. " timestamp integer default (datetime('now')),\n"
  551. " version text,\n"
  552. " comment text\n"
  553. ");\n",
  554. NULL,
  555. NULL,
  556. NULL
  557. );
  558. assert(int_result == SQLITE_OK);
  559. if (int_result != SQLITE_OK)
  560. return PEP_UNKNOWN_DB_ERROR;
  561. // This string is now too large for the C standard, so we're going to break it up some.
  562. // I presume we use the enormous string for performance purposes... terrible for debugging purposes, but OK.
  563. int_result = sqlite3_exec(
  564. session->db,
  565. "PRAGMA application_id = 0x23423423;\n"
  566. "create table if not exists log (\n"
  567. " timestamp integer default (datetime('now')),\n"
  568. " title text not null,\n"
  569. " description text,\n"
  570. " entity text not null,\n"
  571. " comment text\n"
  572. ");\n"
  573. "create index if not exists log_timestamp on log (\n"
  574. " timestamp\n"
  575. ");\n"
  576. ,
  577. NULL,
  578. NULL,
  579. NULL
  580. );
  581. assert(int_result == SQLITE_OK);
  582. if (int_result != SQLITE_OK)
  583. return PEP_UNKNOWN_DB_ERROR;
  584. return PEP_STATUS_OK;
  585. }
  586. static PEP_STATUS _create_core_tables(PEP_SESSION session) {
  587. int int_result = sqlite3_exec(
  588. session->db,
  589. "create table if not exists pgp_keypair (\n"
  590. " fpr text primary key,\n"
  591. " created integer,\n"
  592. " expires integer,\n"
  593. " comment text,\n"
  594. " flags integer default 0\n"
  595. ");\n"
  596. "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
  597. " expires\n"
  598. ");\n"
  599. "create table if not exists person (\n"
  600. " id text primary key,\n"
  601. " username text not null,\n"
  602. " main_key_id text\n"
  603. " references pgp_keypair (fpr)\n"
  604. " on delete set null,\n"
  605. " lang text,\n"
  606. " comment text,\n"
  607. // " device_group text,\n"
  608. " is_pEp_user integer default 0\n"
  609. ");\n"
  610. "create table if not exists identity (\n"
  611. " address text,\n"
  612. " user_id text\n"
  613. " references person (id)\n"
  614. " on delete cascade on update cascade,\n"
  615. " main_key_id text\n"
  616. " references pgp_keypair (fpr)\n"
  617. " on delete set null,\n"
  618. " username text,\n"
  619. " comment text,\n"
  620. " flags integer default 0,\n"
  621. " is_own integer default 0,\n"
  622. " pEp_version_major integer default 0,\n"
  623. " pEp_version_minor integer default 0,\n"
  624. " enc_format integer default 0,\n"
  625. " timestamp integer default (datetime('now')),\n"
  626. " primary key (address, user_id)\n"
  627. ");\n"
  628. "create index if not exists identity_userid on identity (user_id);\n"
  629. "create table if not exists trust (\n"
  630. " user_id text not null\n"
  631. " references person (id)\n"
  632. " on delete cascade on update cascade,\n"
  633. " pgp_keypair_fpr text not null\n"
  634. " references pgp_keypair (fpr)\n"
  635. " on delete cascade,\n"
  636. " comm_type integer not null,\n"
  637. " comment text,\n"
  638. " sticky integer default 0,\n"
  639. " primary key (user_id, pgp_keypair_fpr)\n"
  640. ");\n"
  641. ,
  642. NULL,
  643. NULL,
  644. NULL
  645. );
  646. assert(int_result == SQLITE_OK);
  647. if (int_result != SQLITE_OK)
  648. return PEP_UNKNOWN_DB_ERROR;
  649. return PEP_STATUS_OK;
  650. }
  651. static PEP_STATUS _create_group_tables(PEP_SESSION session) {
  652. if (!session)
  653. return PEP_ILLEGAL_VALUE;
  654. int int_result = sqlite3_exec(
  655. session->db,
  656. // group information
  657. "create table if not exists groups (\n"
  658. " group_id text,\n"
  659. " group_address text,\n"
  660. " manager_userid text,\n"
  661. " manager_address text,\n"
  662. " active integer default 0,\n"
  663. " constraint groups_pk\n"
  664. " primary key (group_address, group_id),\n"
  665. " constraint group_identity_fk\n"
  666. " foreign key (group_address, group_id) references identity\n"
  667. " on update cascade on delete cascade,\n"
  668. " constraint manager_identity_fk\n"
  669. " foreign key (manager_address, manager_userid) references identity\n"
  670. " on update cascade on delete cascade\n"
  671. ");\n"
  672. "create table if not exists own_memberships (\n"
  673. " group_id text,\n"
  674. " group_address text,\n"
  675. " own_id text,\n"
  676. " own_address text,\n"
  677. " have_joined int default 0,\n"
  678. " constraint own_memberships_pk\n"
  679. " primary key (group_address, group_id),\n"
  680. " constraint own_memberships_own_id_fk\n"
  681. " foreign key (own_address, own_id) references identity\n"
  682. " on update cascade on delete cascade,\n"
  683. " constraint own_memberships_group_fk\n"
  684. " foreign key (group_address, group_id) references groups\n"
  685. " on update cascade on delete cascade\n"
  686. ");\n"
  687. "create table if not exists own_groups_members (\n"
  688. " group_id text,\n"
  689. " group_address text,\n"
  690. " member_id text,\n"
  691. " member_address text,\n"
  692. " active_member int default 0,\n"
  693. " constraint own_groups_members_pk\n"
  694. " primary key (group_address, group_id, member_address, member_id),\n"
  695. " constraint group_ident_fk\n"
  696. " foreign key (group_address, group_id) references groups\n"
  697. " on update cascade on delete cascade,\n"
  698. " constraint member_ident_fk\n"
  699. " foreign key (member_address, member_id) references identity\n"
  700. " on update cascade on delete cascade\n"
  701. ");\n"
  702. ,
  703. NULL,
  704. NULL,
  705. NULL
  706. );
  707. assert(int_result == SQLITE_OK);
  708. if (int_result != SQLITE_OK)
  709. return PEP_UNKNOWN_DB_ERROR;
  710. return PEP_STATUS_OK;
  711. }
  712. static PEP_STATUS _create_supplementary_key_tables(PEP_SESSION session) {
  713. int int_result = sqlite3_exec(
  714. session->db,
  715. "create table if not exists revoked_keys (\n"
  716. " revoked_fpr text primary key,\n"
  717. " replacement_fpr text not null\n"
  718. " references pgp_keypair (fpr)\n"
  719. " on delete cascade,\n"
  720. " revocation_date integer\n"
  721. ");\n"
  722. // mistrusted keys
  723. "create table if not exists mistrusted_keys (\n"
  724. " fpr text primary key\n"
  725. ");\n"
  726. // social graph for key resets
  727. "create table if not exists social_graph (\n"
  728. " own_userid text,\n"
  729. " own_address text,\n"
  730. " contact_userid text,\n"
  731. " CONSTRAINT fk_own_identity\n"
  732. " FOREIGN KEY(own_address, own_userid)\n"
  733. " REFERENCES identity(address, user_id)\n"
  734. " ON DELETE CASCADE ON UPDATE CASCADE\n"
  735. ");\n"
  736. // list of user_ids sent revocation
  737. "create table if not exists revocation_contact_list (\n"
  738. " fpr text not null references pgp_keypair (fpr)\n"
  739. " on delete cascade,\n"
  740. " own_address text,\n"
  741. " contact_id text not null references person (id)\n"
  742. " on delete cascade on update cascade,\n"
  743. " timestamp integer default (datetime('now')),\n"
  744. " PRIMARY KEY(fpr, own_address, contact_id)\n"
  745. ");\n"
  746. ,
  747. NULL,
  748. NULL,
  749. NULL
  750. );
  751. assert(int_result == SQLITE_OK);
  752. if (int_result != SQLITE_OK)
  753. return PEP_UNKNOWN_DB_ERROR;
  754. return PEP_STATUS_OK;
  755. }
  756. static PEP_STATUS _create_misc_admin_tables(PEP_SESSION session) {
  757. int int_result = sqlite3_exec(
  758. session->db,
  759. // sequences
  760. "create table if not exists sequences(\n"
  761. " name text primary key,\n"
  762. " value integer default 0\n"
  763. ");\n"
  764. // user id aliases
  765. "create table if not exists alternate_user_id (\n"
  766. " default_id text references person (id)\n"
  767. " on delete cascade on update cascade,\n"
  768. " alternate_id text primary key\n"
  769. ");\n"
  770. ,
  771. NULL,
  772. NULL,
  773. NULL
  774. );
  775. assert(int_result == SQLITE_OK);
  776. if (int_result != SQLITE_OK)
  777. return PEP_UNKNOWN_DB_ERROR;
  778. return PEP_STATUS_OK;
  779. }
  780. // The create tables string is now too large for the C standard, so we're going to break it up some.
  781. // I presume we use the enormous string for performance purposes... terrible for debugging purposes, but OK.
  782. PEP_STATUS create_tables(PEP_SESSION session) {
  783. if (!session)
  784. return PEP_ILLEGAL_VALUE;
  785. PEP_STATUS status = PEP_STATUS_OK;
  786. status = _create_initial_tables(session);
  787. if (status != PEP_STATUS_OK)
  788. return status;
  789. status = _create_core_tables(session);
  790. if (status != PEP_STATUS_OK)
  791. return status;
  792. status = _create_group_tables(session);
  793. if (status != PEP_STATUS_OK)
  794. return status;
  795. status = _create_supplementary_key_tables(session);
  796. if (status != PEP_STATUS_OK)
  797. return status;
  798. status = _create_misc_admin_tables(session);
  799. return status;
  800. }
  801. PEP_STATUS get_db_user_version(PEP_SESSION session, int* version) {
  802. int int_result = sqlite3_exec(
  803. session->db,
  804. "pragma user_version;",
  805. user_version,
  806. version,
  807. NULL
  808. );
  809. assert(int_result == SQLITE_OK);
  810. if (int_result != SQLITE_OK)
  811. return PEP_UNKNOWN_DB_ERROR;
  812. return PEP_STATUS_OK;
  813. }
  814. // Only called if input version is 1
  815. static PEP_STATUS _verify_version(PEP_SESSION session, int* version) {
  816. // Sometimes the user_version wasn't set correctly.
  817. bool version_changed = true;
  818. int int_result;
  819. if (table_contains_column(session, "identity", "username")) {
  820. *version = 17;
  821. }
  822. else if (table_contains_column(session, "trust", "sticky")) {
  823. *version = 16;
  824. }
  825. else if (table_contains_column(session, "groups", "group_identity")) {
  826. *version = 15;
  827. }
  828. else if (table_contains_column(session, "identity", "enc_format")) {
  829. *version = 14;
  830. }
  831. else if (table_contains_column(session, "revocation_contact_list", "own_address")) {
  832. *version = 13;
  833. }
  834. else if (table_contains_column(session, "identity", "pEp_version_major")) {
  835. *version = 12;
  836. }
  837. else if (db_contains_table(session, "social_graph") > 0) {
  838. if (!table_contains_column(session, "person", "device_group"))
  839. *version = 10;
  840. else
  841. *version = 9;
  842. }
  843. else if (table_contains_column(session, "identity", "timestamp") > 0) {
  844. *version = 8;
  845. }
  846. else if (table_contains_column(session, "person", "is_pEp_user") > 0) {
  847. *version = 7;
  848. }
  849. else if (table_contains_column(session, "identity", "is_own") > 0) {
  850. *version = 6;
  851. }
  852. else if (table_contains_column(session, "pgp_keypair", "flags") > 0) {
  853. *version = 2;
  854. }
  855. else {
  856. version_changed = false;
  857. }
  858. if (version_changed) {
  859. // set it in the DB, finally. Yeesh.
  860. char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon.
  861. sprintf(verbuf, "%d", *version);
  862. size_t query_size = strlen(verbuf) + 25;
  863. char *query = calloc(query_size, 1);
  864. strlcpy(query, "pragma user_version = ", query_size);
  865. strlcat(query, verbuf, query_size);
  866. strlcat(query, ";", query_size);
  867. int_result = sqlite3_exec(
  868. session->db,
  869. query,
  870. user_version,
  871. &*version,
  872. NULL
  873. );
  874. free(query);
  875. }
  876. // FIXME: status
  877. return PEP_STATUS_OK;
  878. }
  879. static PEP_STATUS _upgrade_DB_to_ver_2(PEP_SESSION session) {
  880. // N.B. addition of device_group column removed in DDL v10
  881. int int_result = sqlite3_exec(
  882. session->db,
  883. "alter table pgp_keypair\n"
  884. " add column flags integer default 0;\n",
  885. // "alter table person\n"
  886. // " add column device_group text;\n",
  887. NULL,
  888. NULL,
  889. NULL
  890. );
  891. assert(int_result == SQLITE_OK);
  892. if (int_result != SQLITE_OK)
  893. return PEP_UNKNOWN_DB_ERROR;
  894. return PEP_STATUS_OK;
  895. }
  896. static PEP_STATUS _upgrade_DB_to_ver_5(PEP_SESSION session) {
  897. int int_result = sqlite3_exec(
  898. session->db,
  899. "delete from pgp_keypair where fpr = '';",
  900. NULL,
  901. NULL,
  902. NULL
  903. );
  904. assert(int_result == SQLITE_OK);
  905. if (int_result != SQLITE_OK)
  906. return PEP_UNKNOWN_DB_ERROR;
  907. int_result = sqlite3_exec(
  908. session->db,
  909. "delete from trust where pgp_keypair_fpr = '';",
  910. NULL,
  911. NULL,
  912. NULL
  913. );
  914. assert(int_result == SQLITE_OK);
  915. if (int_result != SQLITE_OK)
  916. return PEP_UNKNOWN_DB_ERROR;
  917. return PEP_STATUS_OK;
  918. }
  919. static PEP_STATUS _upgrade_DB_to_ver_6(PEP_SESSION session) {
  920. int int_result = sqlite3_exec(
  921. session->db,
  922. "alter table identity\n"
  923. " add column is_own integer default 0;\n",
  924. NULL,
  925. NULL,
  926. NULL
  927. );
  928. assert(int_result == SQLITE_OK);
  929. if (int_result != SQLITE_OK)
  930. return PEP_UNKNOWN_DB_ERROR;
  931. int_result = sqlite3_exec(
  932. session->db,
  933. "update identity\n"
  934. " set is_own = 1\n"
  935. " where (user_id = '" PEP_OWN_USERID "');\n",
  936. NULL,
  937. NULL,
  938. NULL
  939. );
  940. assert(int_result == SQLITE_OK);
  941. if (int_result != SQLITE_OK)
  942. return PEP_UNKNOWN_DB_ERROR;
  943. // Turns out that just adding "on update cascade" in
  944. // sqlite is a PITA. We need to be able to cascade
  945. // person->id replacements (for temp ids like "TOFU_")
  946. // so here we go...
  947. int_result = sqlite3_exec(
  948. session->db,
  949. "PRAGMA foreign_keys=off;\n"
  950. "BEGIN TRANSACTION;\n"
  951. "create table _identity_new (\n"
  952. " address text,\n"
  953. " user_id text\n"
  954. " references person (id)\n"
  955. " on delete cascade on update cascade,\n"
  956. " main_key_id text\n"
  957. " references pgp_keypair (fpr)\n"
  958. " on delete set null,\n"
  959. " comment text,\n"
  960. " flags integer default 0,\n"
  961. " is_own integer default 0,\n"
  962. " primary key (address, user_id)\n"
  963. ");\n"
  964. "INSERT INTO _identity_new SELECT * FROM identity;\n"
  965. "DROP TABLE identity;\n"
  966. "ALTER TABLE _identity_new RENAME TO identity;\n"
  967. "COMMIT;\n"
  968. "\n"
  969. "BEGIN TRANSACTION;\n"
  970. "create table _trust_new (\n"
  971. " user_id text not null\n"
  972. " references person (id)\n"
  973. " on delete cascade on update cascade,\n"
  974. " pgp_keypair_fpr text not null\n"
  975. " references pgp_keypair (fpr)\n"
  976. " on delete cascade,\n"
  977. " comm_type integer not null,\n"
  978. " comment text,\n"
  979. " primary key (user_id, pgp_keypair_fpr)\n"
  980. ");\n"
  981. "INSERT INTO _trust_new SELECT * FROM trust;\n"
  982. "DROP TABLE trust;\n"
  983. "ALTER TABLE _trust_new RENAME TO trust;\n"
  984. "COMMIT;\n"
  985. "\n"
  986. "PRAGMA foreign_keys=on;\n"
  987. "create table if not exists alternate_user_id (\n"
  988. " default_id text references person (id)\n"
  989. " on delete cascade on update cascade,\n"
  990. " alternate_id text primary key\n"
  991. ");\n",
  992. NULL,
  993. NULL,
  994. NULL
  995. );
  996. assert(int_result == SQLITE_OK);
  997. if (int_result != SQLITE_OK)
  998. return PEP_UNKNOWN_DB_ERROR;
  999. int_result = sqlite3_exec(
  1000. session->db,
  1001. "PRAGMA foreign_key_check;\n",
  1002. NULL,
  1003. NULL,
  1004. NULL
  1005. );
  1006. assert(int_result == SQLITE_OK);
  1007. if (int_result != SQLITE_OK)
  1008. return PEP_UNKNOWN_DB_ERROR;
  1009. // FIXME: foreign key check here
  1010. assert(int_result == SQLITE_OK);
  1011. if (int_result != SQLITE_OK)
  1012. return PEP_UNKNOWN_DB_ERROR;
  1013. return PEP_STATUS_OK;
  1014. }
  1015. static PEP_STATUS _upgrade_DB_to_ver_7(PEP_SESSION session) {
  1016. int int_result = sqlite3_exec(
  1017. session->db,
  1018. "alter table person\n"
  1019. " add column is_pEp_user integer default 0;\n",
  1020. NULL,
  1021. NULL,
  1022. NULL
  1023. );
  1024. assert(int_result == SQLITE_OK);
  1025. if (int_result != SQLITE_OK)
  1026. return PEP_UNKNOWN_DB_ERROR;
  1027. int_result = sqlite3_exec(
  1028. session->db,
  1029. "update person\n"
  1030. " set is_pEp_user = 1\n"
  1031. " where id = "
  1032. " (select distinct id from person "
  1033. " join trust on id = user_id "
  1034. " where (case when (comm_type = 127) then (id) "
  1035. " when (comm_type = 255) then (id) "
  1036. " else 0"
  1037. " end) = id );\n",
  1038. NULL,
  1039. NULL,
  1040. NULL
  1041. );
  1042. assert(int_result == SQLITE_OK);
  1043. if (int_result != SQLITE_OK)
  1044. return PEP_UNKNOWN_DB_ERROR;
  1045. int_result = sqlite3_exec(
  1046. session->db,
  1047. "create table if not exists mistrusted_keys (\n"
  1048. " fpr text primary key\n"
  1049. ");\n",
  1050. NULL,
  1051. NULL,
  1052. NULL
  1053. );
  1054. assert(int_result == SQLITE_OK);
  1055. if (int_result != SQLITE_OK)
  1056. return PEP_UNKNOWN_DB_ERROR;
  1057. assert(int_result == SQLITE_OK);
  1058. if (int_result != SQLITE_OK)
  1059. return PEP_UNKNOWN_DB_ERROR;
  1060. return PEP_STATUS_OK;
  1061. }
  1062. static PEP_STATUS _upgrade_DB_to_ver_8(PEP_SESSION session) {
  1063. int int_result = sqlite3_exec(
  1064. session->db,
  1065. "PRAGMA foreign_keys=off;\n"
  1066. "BEGIN TRANSACTION;\n"
  1067. "create table _identity_new (\n"
  1068. " address text,\n"
  1069. " user_id text\n"
  1070. " references person (id)\n"
  1071. " on delete cascade on update cascade,\n"
  1072. " main_key_id text\n"
  1073. " references pgp_keypair (fpr)\n"
  1074. " on delete set null,\n"
  1075. " comment text,\n"
  1076. " flags integer default 0,\n"
  1077. " is_own integer default 0,\n"
  1078. " timestamp integer default (datetime('now')),\n"
  1079. " primary key (address, user_id)\n"
  1080. ");\n"
  1081. "INSERT INTO _identity_new (address, user_id, main_key_id, "
  1082. " comment, flags, is_own) "
  1083. " SELECT identity.address, identity.user_id, "
  1084. " identity.main_key_id, identity.comment, "
  1085. " identity.flags, identity.is_own "
  1086. " FROM identity "
  1087. " WHERE 1;\n"
  1088. "DROP TABLE identity;\n"
  1089. "ALTER TABLE _identity_new RENAME TO identity;\n"
  1090. "COMMIT;\n"
  1091. "\n"
  1092. "PRAGMA foreign_keys=on;\n",
  1093. NULL,
  1094. NULL,
  1095. NULL
  1096. );
  1097. assert(int_result == SQLITE_OK);
  1098. if (int_result != SQLITE_OK)
  1099. return PEP_UNKNOWN_DB_ERROR;
  1100. int_result = sqlite3_exec(
  1101. session->db,
  1102. "PRAGMA foreign_key_check;\n",
  1103. NULL,
  1104. NULL,
  1105. NULL
  1106. );
  1107. assert(int_result == SQLITE_OK);
  1108. // FIXME: foreign key check
  1109. if (int_result != SQLITE_OK)
  1110. return PEP_UNKNOWN_DB_ERROR;
  1111. return PEP_STATUS_OK;
  1112. }
  1113. static PEP_STATUS _upgrade_DB_to_ver_9(PEP_SESSION session) {
  1114. int int_result = sqlite3_exec(
  1115. session->db,
  1116. "create table if not exists social_graph (\n"
  1117. " own_userid text,\n"
  1118. " own_address text,\n"
  1119. " contact_userid text,\n"
  1120. " CONSTRAINT fk_own_identity\n"
  1121. " FOREIGN KEY(own_address, own_userid)\n"
  1122. " REFERENCES identity(address, user_id)\n"
  1123. " ON DELETE CASCADE ON UPDATE CASCADE\n"
  1124. ");\n"
  1125. "create table if not exists revocation_contact_list (\n"
  1126. " fpr text not null references pgp_keypair (fpr)\n"
  1127. " on delete cascade,\n"
  1128. " contact_id text not null references person (id)\n"
  1129. " on delete cascade on update cascade,\n"
  1130. " timestamp integer default (datetime('now')),\n"
  1131. " PRIMARY KEY(fpr, contact_id)\n"
  1132. ");\n",
  1133. NULL,
  1134. NULL,
  1135. NULL
  1136. );
  1137. assert(int_result == SQLITE_OK);
  1138. if (int_result != SQLITE_OK)
  1139. return PEP_UNKNOWN_DB_ERROR;
  1140. return PEP_STATUS_OK;
  1141. }
  1142. static PEP_STATUS _upgrade_DB_to_ver_10(PEP_SESSION session) {
  1143. int int_result = sqlite3_exec(
  1144. session->db,
  1145. "PRAGMA foreign_keys=off;\n"
  1146. "BEGIN TRANSACTION;\n"
  1147. "create table _person_new (\n"
  1148. " id text primary key,\n"
  1149. " username text not null,\n"
  1150. " main_key_id text\n"
  1151. " references pgp_keypair (fpr)\n"
  1152. " on delete set null,\n"
  1153. " lang text,\n"
  1154. " comment text,\n"
  1155. " is_pEp_user integer default 0\n"
  1156. ");\n"
  1157. "INSERT INTO _person_new (id, username, main_key_id, "
  1158. " lang, comment, is_pEp_user) "
  1159. " SELECT person.id, person.username, "
  1160. " person.main_key_id, person.lang, "
  1161. " person.comment, person.is_pEp_user "
  1162. " FROM person "
  1163. " WHERE 1;\n"
  1164. "DROP TABLE person;\n"
  1165. "ALTER TABLE _person_new RENAME TO person;\n"
  1166. "COMMIT;\n"
  1167. "\n"
  1168. "PRAGMA foreign_keys=on;\n",
  1169. NULL,
  1170. NULL,
  1171. NULL
  1172. );
  1173. assert(int_result == SQLITE_OK);
  1174. if (int_result != SQLITE_OK)
  1175. return PEP_UNKNOWN_DB_ERROR;
  1176. int_result = sqlite3_exec(
  1177. session->db,
  1178. "PRAGMA foreign_key_check;\n",
  1179. NULL,
  1180. NULL,
  1181. NULL
  1182. );
  1183. assert(int_result == SQLITE_OK);
  1184. if (int_result != SQLITE_OK)
  1185. return PEP_UNKNOWN_DB_ERROR;
  1186. return PEP_STATUS_OK;
  1187. }
  1188. static PEP_STATUS _force_upgrade_own_latest_message_version(PEP_SESSION session) {
  1189. // N.B. WE DEFINE PEP_VERSION - IF WE'RE AT 9-DIGIT MAJOR OR MINOR VERSIONS, ER, BAD.
  1190. char major_buf[10];
  1191. char minor_buf[10];
  1192. // Guess we were abusing sscanf here, so we'll do it this way:
  1193. const char *cptr = PEP_VERSION;
  1194. size_t major_len = 0;
  1195. size_t minor_len = 0;
  1196. char *bufptr = major_buf;
  1197. while (*cptr != '.' && *cptr != '\0') {
  1198. *bufptr++ = *cptr++;
  1199. major_len++;
  1200. }
  1201. *bufptr = '\0';
  1202. bufptr = minor_buf;
  1203. if (*cptr == '.') {
  1204. cptr++;
  1205. while (*cptr != '\0') {
  1206. *bufptr++ = *cptr++;
  1207. minor_len++;
  1208. }
  1209. } else {
  1210. *bufptr++ = '0';
  1211. }
  1212. *bufptr = '\0';
  1213. const char *version_upgrade_startstr =
  1214. "update identity\n"
  1215. " set pEp_version_major = ";
  1216. const char *version_upgrade_midstr = ",\n"
  1217. " pEp_version_minor = ";
  1218. const char *version_upgrade_endstr =
  1219. "\n"
  1220. " where identity.is_own = 1;\n"; // FIXME: Group idents?
  1221. size_t new_stringlen = strlen(version_upgrade_startstr) + major_len +
  1222. strlen(version_upgrade_midstr) + minor_len +
  1223. strlen(version_upgrade_endstr);
  1224. char *version_upgrade_stmt = calloc(new_stringlen + 1, 1);
  1225. snprintf(version_upgrade_stmt, new_stringlen + 1, "%s%s%s%s%s",
  1226. version_upgrade_startstr, major_buf, version_upgrade_midstr, minor_buf, version_upgrade_endstr);
  1227. int int_result = sqlite3_exec(
  1228. session->db,
  1229. version_upgrade_stmt,
  1230. NULL,
  1231. NULL,
  1232. NULL
  1233. );
  1234. free(version_upgrade_stmt);
  1235. if (int_result != SQLITE_OK)
  1236. return PEP_UNKNOWN_DB_ERROR;
  1237. return PEP_STATUS_OK;
  1238. }
  1239. static PEP_STATUS _upgrade_DB_to_ver_12(PEP_SESSION session) {
  1240. int int_result = sqlite3_exec(
  1241. session->db,
  1242. "create index if not exists identity_userid_addr on identity(address, user_id);\n",
  1243. NULL,
  1244. NULL,
  1245. NULL
  1246. );
  1247. assert(int_result == SQLITE_OK);
  1248. if (int_result != SQLITE_OK)
  1249. return PEP_UNKNOWN_DB_ERROR;
  1250. int_result = sqlite3_exec(
  1251. session->db,
  1252. "alter table identity\n"
  1253. " add column pEp_version_major integer default 0;\n"
  1254. "alter table identity\n"
  1255. " add column pEp_version_minor integer default 0;\n",
  1256. NULL,
  1257. NULL,
  1258. NULL
  1259. );
  1260. assert(int_result == SQLITE_OK);
  1261. if (int_result != SQLITE_OK)
  1262. return PEP_UNKNOWN_DB_ERROR;
  1263. int_result = sqlite3_exec(
  1264. session->db,
  1265. "update identity\n"
  1266. " set pEp_version_major = 2,\n"
  1267. " pEp_version_minor = 1\n"
  1268. " where exists (select * from person\n"
  1269. " where identity.user_id = person.id\n"
  1270. " and identity.is_own = 0\n"
  1271. " and person.is_pEp_user = 1);\n",
  1272. NULL,
  1273. NULL,
  1274. NULL
  1275. );
  1276. assert(int_result == SQLITE_OK);
  1277. if (int_result != SQLITE_OK)
  1278. return PEP_UNKNOWN_DB_ERROR;
  1279. return PEP_STATUS_OK;
  1280. // Superceded by the -to-version-18 upgrade.
  1281. // return _force_upgrade_own_latest_message_version(session);
  1282. }
  1283. static PEP_STATUS _upgrade_DB_to_ver_14(PEP_SESSION session) {
  1284. int int_result = sqlite3_exec(
  1285. session->db,
  1286. "alter table identity\n"
  1287. " add column enc_format integer default 0;\n",
  1288. NULL,
  1289. NULL,
  1290. NULL
  1291. );
  1292. assert(int_result == SQLITE_OK);
  1293. if (int_result != SQLITE_OK)
  1294. return PEP_UNKNOWN_DB_ERROR;
  1295. return PEP_STATUS_OK;
  1296. }
  1297. static PEP_STATUS _upgrade_DB_to_ver_15(PEP_SESSION session) {
  1298. return _create_group_tables(session);
  1299. }
  1300. static PEP_STATUS _upgrade_DB_to_ver_16(PEP_SESSION session) {
  1301. int int_result = sqlite3_exec(
  1302. session->db,
  1303. "alter table trust\n"
  1304. " add column sticky integer default 0;\n",
  1305. NULL,
  1306. NULL,
  1307. NULL
  1308. );
  1309. assert(int_result == SQLITE_OK);
  1310. if (int_result != SQLITE_OK)
  1311. return PEP_UNKNOWN_DB_ERROR;
  1312. return PEP_STATUS_OK;
  1313. }
  1314. static PEP_STATUS _upgrade_DB_to_ver_17(PEP_SESSION session) {
  1315. int int_result = sqlite3_exec(
  1316. session->db,
  1317. "alter table identity\n"
  1318. " add column username;\n",
  1319. NULL,
  1320. NULL,
  1321. NULL
  1322. );
  1323. assert(int_result == SQLITE_OK);
  1324. if (int_result != SQLITE_OK)
  1325. return PEP_UNKNOWN_DB_ERROR;
  1326. return PEP_STATUS_OK;
  1327. }
  1328. // Version 2.0 and earlier will now no longer be supported with other
  1329. // pEp users.
  1330. static PEP_STATUS _upgrade_DB_to_ver_18(PEP_SESSION session) {
  1331. int int_result = sqlite3_exec(
  1332. session->db,
  1333. "update identity\n"
  1334. " set pEp_version_major = 2,\n"
  1335. " pEp_version_minor = 1\n"
  1336. " where exists (select * from person\n"
  1337. " where identity.user_id = person.id\n"
  1338. " and identity.pEp_version_major = 2\n"
  1339. " and identity.pEp_version_minor = 0\n"
  1340. " and person.is_pEp_user = 1);\n",
  1341. NULL,
  1342. NULL,
  1343. NULL
  1344. );
  1345. assert(int_result == SQLITE_OK);
  1346. if (int_result != SQLITE_OK)
  1347. return PEP_UNKNOWN_DB_ERROR;
  1348. return _force_upgrade_own_latest_message_version(session);
  1349. }
  1350. static PEP_STATUS _upgrade_DB_to_ver_19(PEP_SESSION session) {
  1351. int int_result = sqlite3_exec(
  1352. session->db,
  1353. /* This index was useless: it was an index on the (multi-column)
  1354. primary key, always implemented using an index which gets also
  1355. used in queries. */
  1356. "drop index if exists identity_userid_addr;\n"
  1357. "\n"
  1358. "create index if not exists identity_userid on identity (user_id);\n"
  1359. NULL,
  1360. NULL,
  1361. NULL
  1362. );
  1363. assert(int_result == SQLITE_OK);
  1364. if (int_result != SQLITE_OK)
  1365. return PEP_UNKNOWN_DB_ERROR;
  1366. return PEP_STATUS_OK;
  1367. }
  1368. // Honestly, the upgrades should be redone in a transaction IMHO.
  1369. static PEP_STATUS _check_and_execute_upgrades(PEP_SESSION session, int version) {
  1370. PEP_STATUS status = PEP_STATUS_OK;
  1371. switch(version) {
  1372. case 1:
  1373. status = _upgrade_DB_to_ver_2(session);
  1374. if (status != PEP_STATUS_OK)
  1375. return status;
  1376. case 2:
  1377. case 3:
  1378. case 4:
  1379. status = _upgrade_DB_to_ver_5(session);
  1380. if (status != PEP_STATUS_OK)
  1381. return status;
  1382. case 5:
  1383. status = _upgrade_DB_to_ver_6(session);
  1384. if (status != PEP_STATUS_OK)
  1385. return status;
  1386. case 6:
  1387. status = _upgrade_DB_to_ver_7(session);
  1388. if (status != PEP_STATUS_OK)
  1389. return status;
  1390. case 7:
  1391. status = _upgrade_DB_to_ver_8(session);
  1392. if (status != PEP_STATUS_OK)
  1393. return status;
  1394. case 8:
  1395. status = _upgrade_DB_to_ver_9(session);
  1396. if (status != PEP_STATUS_OK)
  1397. return status;
  1398. case 9:
  1399. if (version > 1) {
  1400. status = _upgrade_DB_to_ver_10(session);
  1401. if (status != PEP_STATUS_OK)
  1402. return status;
  1403. }
  1404. case 10:
  1405. status = repair_altered_tables(session);
  1406. assert(status == PEP_STATUS_OK);
  1407. if (status != PEP_STATUS_OK)
  1408. return status;
  1409. case 11:
  1410. status = _upgrade_DB_to_ver_12(session);
  1411. if (status != PEP_STATUS_OK)
  1412. return status;
  1413. case 12:
  1414. status = upgrade_revoc_contact_to_13(session);
  1415. assert(status == PEP_STATUS_OK);
  1416. if (status != PEP_STATUS_OK)
  1417. return status;
  1418. case 13:
  1419. status = _upgrade_DB_to_ver_14(session);
  1420. if (status != PEP_STATUS_OK)
  1421. return status;
  1422. case 14:
  1423. status = _upgrade_DB_to_ver_15(session);
  1424. if (status != PEP_STATUS_OK)
  1425. return status;
  1426. case 15:
  1427. status = _upgrade_DB_to_ver_16(session);
  1428. if (status != PEP_STATUS_OK)
  1429. return status;
  1430. case 16:
  1431. status = _upgrade_DB_to_ver_17(session);
  1432. if (status != PEP_STATUS_OK)
  1433. return status;
  1434. case 17:
  1435. status = _upgrade_DB_to_ver_18(session);
  1436. if (status != PEP_STATUS_OK)
  1437. return status;
  1438. case 18:
  1439. status = _upgrade_DB_to_ver_19(session);
  1440. if (status != PEP_STATUS_OK)
  1441. return status;
  1442. case 19:
  1443. break;
  1444. default:
  1445. return PEP_ILLEGAL_VALUE;
  1446. }
  1447. return PEP_STATUS_OK;
  1448. }
  1449. PEP_STATUS pEp_sql_init(PEP_SESSION session) {
  1450. bool very_first = false;
  1451. PEP_STATUS status = create_tables(session);
  1452. if (status != PEP_STATUS_OK)
  1453. return status;
  1454. int version = 0;
  1455. status = get_db_user_version(session, &version);
  1456. if (status != PEP_STATUS_OK)
  1457. return status;
  1458. void (*xFunc_lower)(sqlite3_context *, int, sqlite3_value **) = &_sql_lower;
  1459. int int_result = sqlite3_create_function_v2(
  1460. session->db,
  1461. "lower",
  1462. 1,
  1463. SQLITE_UTF8 | SQLITE_DETERMINISTIC,
  1464. NULL,
  1465. xFunc_lower,
  1466. NULL,
  1467. NULL,
  1468. NULL);
  1469. assert(int_result == SQLITE_OK);
  1470. if (int_result != SQLITE_OK)
  1471. return PEP_UNKNOWN_DB_ERROR;
  1472. int_result = sqlite3_exec(
  1473. session->db,
  1474. "pragma foreign_keys=ON;\n",
  1475. NULL,
  1476. NULL,
  1477. NULL
  1478. );
  1479. assert(int_result == SQLITE_OK);
  1480. if (int_result != SQLITE_OK)
  1481. return PEP_UNKNOWN_DB_ERROR;
  1482. if (version > atoi(_DDL_USER_VERSION)) {
  1483. // This is *explicitly* not allowed.
  1484. return PEP_INIT_DB_DOWNGRADE_VIOLATION;
  1485. }
  1486. if (version == 1) {
  1487. // Sometimes the user_version wasn't set correctly.
  1488. status = _verify_version(session, &version);
  1489. if (status != PEP_STATUS_OK)
  1490. return PEP_ILLEGAL_VALUE;
  1491. }
  1492. if (version != 0) {
  1493. // Version has been already set
  1494. // Early mistake : version 0 shouldn't have existed.
  1495. // Numbering should have started at 1 to detect newly created DB.
  1496. // Version 0 DBs are not anymore compatible.
  1497. status = _check_and_execute_upgrades(session, version);
  1498. if (status != PEP_STATUS_OK)
  1499. return PEP_ILLEGAL_VALUE;
  1500. } else {
  1501. // Version from DB was 0, it means this is initial setup.
  1502. // DB has just been created, and all tables are empty.
  1503. very_first = true;
  1504. }
  1505. if (version < atoi(_DDL_USER_VERSION)) {
  1506. int_result = sqlite3_exec(
  1507. session->db,
  1508. "pragma user_version = "_DDL_USER_VERSION";\n"
  1509. "insert or replace into version_info (id, version)"
  1510. "values (1, '" PEP_ENGINE_VERSION "');",
  1511. NULL,
  1512. NULL,
  1513. NULL
  1514. );
  1515. assert(int_result == SQLITE_OK);
  1516. if (int_result != SQLITE_OK)
  1517. return PEP_UNKNOWN_DB_ERROR;
  1518. }
  1519. return PEP_STATUS_OK;
  1520. }
  1521. // This whole mess really does need to be generated somewhere.
  1522. PEP_STATUS pEp_prepare_sql_stmts(PEP_SESSION session) {
  1523. int int_result = sqlite3_prepare_v2(session->system_db, sql_trustword,
  1524. (int)strlen(sql_trustword), &session->trustword, NULL);
  1525. assert(int_result == SQLITE_OK);
  1526. if (int_result != SQLITE_OK)
  1527. return PEP_UNKNOWN_DB_ERROR;
  1528. int_result = sqlite3_prepare_v2(session->db, sql_get_identity,
  1529. (int)strlen(sql_get_identity), &session->get_identity, NULL);
  1530. assert(int_result == SQLITE_OK);
  1531. if (int_result != SQLITE_OK)
  1532. return PEP_UNKNOWN_DB_ERROR;
  1533. int_result = sqlite3_prepare_v2(session->db, sql_get_identity_without_trust_check,
  1534. (int)strlen(sql_get_identity_without_trust_check),
  1535. &session->get_identity_without_trust_check, NULL);
  1536. assert(int_result == SQLITE_OK);
  1537. if (int_result != SQLITE_OK)
  1538. return PEP_UNKNOWN_DB_ERROR;
  1539. int_result = sqlite3_prepare_v2(session->db, sql_get_identities_by_address,
  1540. (int)strlen(sql_get_identities_by_address),
  1541. &session->get_identities_by_address, NULL);
  1542. assert(int_result == SQLITE_OK);
  1543. if (int_result != SQLITE_OK)
  1544. return PEP_UNKNOWN_DB_ERROR;
  1545. int_result = sqlite3_prepare_v2(session->db, sql_get_identities_by_userid,
  1546. (int)strlen(sql_get_identities_by_userid),
  1547. &session->get_identities_by_userid, NULL);
  1548. assert(int_result == SQLITE_OK);
  1549. if (int_result != SQLITE_OK)
  1550. return PEP_UNKNOWN_DB_ERROR;
  1551. int_result = sqlite3_prepare_v2(session->db, sql_get_identities_by_main_key_id,
  1552. (int)strlen(sql_get_identities_by_main_key_id),
  1553. &session->get_identities_by_main_key_id, NULL);
  1554. assert(int_result == SQLITE_OK);
  1555. if (int_result != SQLITE_OK)
  1556. return PEP_UNKNOWN_DB_ERROR;
  1557. int_result = sqlite3_prepare_v2(session->db, sql_set_default_identity_fpr,
  1558. (int)strlen(sql_set_default_identity_fpr), &session->set_default_identity_fpr, NULL);
  1559. assert(int_result == SQLITE_OK);
  1560. if (int_result != SQLITE_OK)
  1561. return PEP_UNKNOWN_DB_ERROR;
  1562. int_result = sqlite3_prepare_v2(session->db, sql_get_default_identity_fpr,
  1563. (int)strlen(sql_get_default_identity_fpr), &session->get_default_identity_fpr, NULL);
  1564. assert(int_result == SQLITE_OK);
  1565. if (int_result != SQLITE_OK)
  1566. return PEP_UNKNOWN_DB_ERROR;
  1567. int_result = sqlite3_prepare_v2(session->db, sql_get_user_default_key,
  1568. (int)strlen(sql_get_user_default_key), &session->get_user_default_key, NULL);
  1569. assert(int_result == SQLITE_OK);
  1570. if (int_result != SQLITE_OK)
  1571. return PEP_UNKNOWN_DB_ERROR;
  1572. int_result = sqlite3_prepare_v2(session->db, sql_get_all_keys_for_user,
  1573. (int)strlen(sql_get_all_keys_for_user), &session->get_all_keys_for_user, NULL);
  1574. assert(int_result == SQLITE_OK);
  1575. if (int_result != SQLITE_OK)
  1576. return PEP_UNKNOWN_DB_ERROR;
  1577. int_result = sqlite3_prepare_v2(session->db, sql_get_default_own_userid,
  1578. (int)strlen(sql_get_default_own_userid), &session->get_default_own_userid, NULL);
  1579. assert(int_result == SQLITE_OK);
  1580. if (int_result != SQLITE_OK)
  1581. return PEP_UNKNOWN_DB_ERROR;
  1582. int_result = sqlite3_prepare_v2(session->db, sql_get_userid_alias_default,
  1583. (int)strlen(sql_get_userid_alias_default), &session->get_userid_alias_default, NULL);
  1584. assert(int_result == SQLITE_OK);
  1585. if (int_result != SQLITE_OK)
  1586. return PEP_UNKNOWN_DB_ERROR;
  1587. int_result = sqlite3_prepare_v2(session->db, sql_add_userid_alias,
  1588. (int)strlen(sql_add_userid_alias), &session->add_userid_alias, NULL);
  1589. assert(int_result == SQLITE_OK);
  1590. if (int_result != SQLITE_OK)
  1591. return PEP_UNKNOWN_DB_ERROR;
  1592. int_result = sqlite3_prepare_v2(session->db, sql_replace_userid,
  1593. (int)strlen(sql_replace_userid), &session->replace_userid, NULL);
  1594. assert(int_result == SQLITE_OK);
  1595. if (int_result != SQLITE_OK)
  1596. return PEP_UNKNOWN_DB_ERROR;
  1597. int_result = sqlite3_prepare_v2(session->db, sql_delete_key,
  1598. (int)strlen(sql_delete_key), &session->delete_key, NULL);
  1599. assert(int_result == SQLITE_OK);
  1600. if (int_result != SQLITE_OK)
  1601. return PEP_UNKNOWN_DB_ERROR;
  1602. int_result = sqlite3_prepare_v2(session->db, sql_replace_main_user_fpr,
  1603. (int)strlen(sql_replace_main_user_fpr), &session->replace_main_user_fpr, NULL);
  1604. assert(int_result == SQLITE_OK);
  1605. if (int_result != SQLITE_OK)
  1606. return PEP_UNKNOWN_DB_ERROR;
  1607. int_result = sqlite3_prepare_v2(session->db, sql_replace_main_user_fpr_if_equal,
  1608. (int)strlen(sql_replace_main_user_fpr_if_equal), &session->replace_main_user_fpr_if_equal, NULL);
  1609. assert(int_result == SQLITE_OK);
  1610. if (int_result != SQLITE_OK)
  1611. return PEP_UNKNOWN_DB_ERROR;
  1612. int_result = sqlite3_prepare_v2(session->db, sql_get_main_user_fpr,
  1613. (int)strlen(sql_get_main_user_fpr), &session->get_main_user_fpr, NULL);
  1614. assert(int_result == SQLITE_OK);
  1615. if (int_result != SQLITE_OK)
  1616. return PEP_UNKNOWN_DB_ERROR;
  1617. int_result = sqlite3_prepare_v2(session->db, sql_refresh_userid_default_key,
  1618. (int)strlen(sql_refresh_userid_default_key), &session->refresh_userid_default_key, NULL);
  1619. assert(int_result == SQLITE_OK);
  1620. if (int_result != SQLITE_OK)
  1621. return PEP_UNKNOWN_DB_ERROR;
  1622. int_result = sqlite3_prepare_v2(session->db, sql_replace_identities_fpr,
  1623. (int)strlen(sql_replace_identities_fpr),
  1624. &session->replace_identities_fpr, NULL);
  1625. assert(int_result == SQLITE_OK);
  1626. if (int_result != SQLITE_OK)
  1627. return PEP_UNKNOWN_DB_ERROR;
  1628. int_result = sqlite3_prepare_v2(session->db, sql_remove_fpr_as_identity_default,
  1629. (int)strlen(sql_remove_fpr_as_identity_default),
  1630. &session->remove_fpr_as_identity_default, NULL);
  1631. assert(int_result == SQLITE_OK);
  1632. if (int_result != SQLITE_OK)
  1633. return PEP_UNKNOWN_DB_ERROR;
  1634. int_result = sqlite3_prepare_v2(session->db, sql_remove_fpr_as_user_default,
  1635. (int)strlen(sql_remove_fpr_as_user_default),
  1636. &session->remove_fpr_as_user_default, NULL);
  1637. assert(int_result == SQLITE_OK);
  1638. if (int_result != SQLITE_OK)
  1639. return PEP_UNKNOWN_DB_ERROR;
  1640. int_result = sqlite3_prepare_v2(session->db, sql_set_person,
  1641. (int)strlen(sql_set_person), &session->set_person, NULL);
  1642. assert(int_result == SQLITE_OK);
  1643. if (int_result != SQLITE_OK)
  1644. return PEP_UNKNOWN_DB_ERROR;
  1645. int_result = sqlite3_prepare_v2(session->db, sql_update_person,
  1646. (int)strlen(sql_update_person), &session->update_person, NULL);
  1647. assert(int_result == SQLITE_OK);
  1648. if (int_result != SQLITE_OK)
  1649. return PEP_UNKNOWN_DB_ERROR;
  1650. int_result = sqlite3_prepare_v2(session->db, sql_delete_person,
  1651. (int)strlen(sql_delete_person), &session->delete_person, NULL);
  1652. assert(int_result == SQLITE_OK);
  1653. if (int_result != SQLITE_OK)
  1654. return PEP_UNKNOWN_DB_ERROR;
  1655. int_result = sqlite3_prepare_v2(session->db, sql_exists_person,
  1656. (int)strlen(sql_exists_person), &session->exists_person, NULL);
  1657. assert(int_result == SQLITE_OK);
  1658. if (int_result != SQLITE_OK)
  1659. return PEP_UNKNOWN_DB_ERROR;
  1660. int_result = sqlite3_prepare_v2(session->db, sql_set_as_pEp_user,
  1661. (int)strlen(sql_set_as_pEp_user), &session->set_as_pEp_user, NULL);
  1662. assert(int_result == SQLITE_OK);
  1663. if (int_result != SQLITE_OK)
  1664. return PEP_UNKNOWN_DB_ERROR;
  1665. int_result = sqlite3_prepare_v2(session->db, sql_is_pEp_user,
  1666. (int)strlen(sql_is_pEp_user), &session->is_pEp_user, NULL);
  1667. assert(int_result == SQLITE_OK);
  1668. if (int_result != SQLITE_OK)
  1669. return PEP_UNKNOWN_DB_ERROR;
  1670. int_result = sqlite3_prepare_v2(session->db, sql_add_into_social_graph,
  1671. (int)strlen(sql_add_into_social_graph), &session->add_into_social_graph, NULL);
  1672. assert(int_result == SQLITE_OK);
  1673. if (int_result != SQLITE_OK)
  1674. return PEP_UNKNOWN_DB_ERROR;
  1675. int_result = sqlite3_prepare_v2(session->db,
  1676. sql_get_own_address_binding_from_contact,
  1677. (int)strlen(sql_get_own_address_binding_from_contact),
  1678. &session->get_own_address_binding_from_contact, NULL);
  1679. assert(int_result == SQLITE_OK);
  1680. if (int_result != SQLITE_OK)
  1681. return PEP_UNKNOWN_DB_ERROR;
  1682. int_result = sqlite3_prepare_v2(session->db,
  1683. sql_set_revoke_contact_as_notified,
  1684. (int)strlen(sql_set_revoke_contact_as_notified),
  1685. &session->set_revoke_contact_as_notified, NULL);
  1686. assert(int_result == SQLITE_OK);
  1687. if (int_result != SQLITE_OK)
  1688. return PEP_UNKNOWN_DB_ERROR;
  1689. int_result = sqlite3_prepare_v2(session->db,
  1690. sql_get_contacted_ids_from_revoke_fpr,
  1691. (int)strlen(sql_get_contacted_ids_from_revoke_fpr),
  1692. &session->get_contacted_ids_from_revoke_fpr, NULL);
  1693. assert(int_result == SQLITE_OK);
  1694. if (int_result != SQLITE_OK)
  1695. return PEP_UNKNOWN_DB_ERROR;
  1696. int_result = sqlite3_prepare_v2(session->db,
  1697. sql_was_id_for_revoke_contacted,
  1698. (int)strlen(sql_was_id_for_revoke_contacted),
  1699. &session->was_id_for_revoke_contacted, NULL);
  1700. assert(int_result == SQLITE_OK);
  1701. if (int_result != SQLITE_OK)
  1702. return PEP_UNKNOWN_DB_ERROR;
  1703. int_result = sqlite3_prepare_v2(session->db,
  1704. sql_has_id_contacted_address,
  1705. (int)strlen(sql_has_id_contacted_address),
  1706. &session->has_id_contacted_address, NULL);
  1707. assert(int_result == SQLITE_OK);
  1708. if (int_result != SQLITE_OK)
  1709. return PEP_UNKNOWN_DB_ERROR;
  1710. int_result = sqlite3_prepare_v2(session->db,
  1711. sql_get_last_contacted,
  1712. (int)strlen(sql_get_last_contacted),
  1713. &session->get_last_contacted, NULL);
  1714. assert(int_result == SQLITE_OK);
  1715. if (int_result != SQLITE_OK)
  1716. return PEP_UNKNOWN_DB_ERROR;
  1717. int_result = sqlite3_prepare_v2(session->db, sql_set_pgp_keypair,
  1718. (int)strlen(sql_set_pgp_keypair), &session->set_pgp_keypair,
  1719. NULL);
  1720. assert(int_result == SQLITE_OK);
  1721. if (int_result != SQLITE_OK)
  1722. return PEP_UNKNOWN_DB_ERROR;
  1723. int_result = sqlite3_prepare_v2(session->db, sql_set_pgp_keypair_flags,
  1724. (int)strlen(sql_set_pgp_keypair_flags), &session->set_pgp_keypair_flags,
  1725. NULL);
  1726. assert(int_result == SQLITE_OK);
  1727. if (int_result != SQLITE_OK)
  1728. return PEP_UNKNOWN_DB_ERROR;
  1729. int_result = sqlite3_prepare_v2(session->db, sql_unset_pgp_keypair_flags,
  1730. (int)strlen(sql_unset_pgp_keypair_flags), &session->unset_pgp_keypair_flags,
  1731. NULL);
  1732. assert(int_result == SQLITE_OK);
  1733. if (int_result != SQLITE_OK)
  1734. return PEP_UNKNOWN_DB_ERROR;
  1735. int_result = sqlite3_prepare_v2(session->db, sql_set_identity_entry,
  1736. (int)strlen(sql_set_identity_entry), &session->set_identity_entry, NULL);
  1737. assert(int_result == SQLITE_OK);
  1738. if (int_result != SQLITE_OK)
  1739. return PEP_UNKNOWN_DB_ERROR;
  1740. int_result = sqlite3_prepare_v2(session->db, sql_update_identity_entry,
  1741. (int)strlen(sql_update_identity_entry), &session->update_identity_entry, NULL);
  1742. assert(int_result == SQLITE_OK);
  1743. if (int_result != SQLITE_OK)
  1744. return PEP_UNKNOWN_DB_ERROR;
  1745. int_result = sqlite3_prepare_v2(session->db, sql_exists_identity_entry,
  1746. (int)strlen(sql_exists_identity_entry), &session->exists_identity_entry, NULL);
  1747. assert(int_result == SQLITE_OK);
  1748. if (int_result != SQLITE_OK)
  1749. return PEP_UNKNOWN_DB_ERROR;
  1750. int_result = sqlite3_prepare_v2(session->db, sql_force_set_identity_username,
  1751. (int)strlen(sql_force_set_identity_username), &session->force_set_identity_username, NULL);
  1752. assert(int_result == SQLITE_OK);
  1753. if (int_result != SQLITE_OK)
  1754. return PEP_UNKNOWN_DB_ERROR;
  1755. int_result = sqlite3_prepare_v2(session->db, sql_set_identity_flags,
  1756. (int)strlen(sql_set_identity_flags), &session->set_identity_flags,
  1757. NULL);
  1758. assert(int_result == SQLITE_OK);
  1759. if (int_result != SQLITE_OK)
  1760. return PEP_UNKNOWN_DB_ERROR;
  1761. int_result = sqlite3_prepare_v2(session->db, sql_unset_identity_flags,
  1762. (int)strlen(sql_unset_identity_flags), &session->unset_identity_flags,
  1763. NULL);
  1764. assert(int_result == SQLITE_OK);
  1765. if (int_result != SQLITE_OK)
  1766. return PEP_UNKNOWN_DB_ERROR;
  1767. int_result = sqlite3_prepare_v2(session->db, sql_set_ident_enc_format,
  1768. (int)strlen(sql_set_ident_enc_format), &session->set_ident_enc_format,
  1769. NULL);
  1770. assert(int_result == SQLITE_OK);
  1771. if (int_result != SQLITE_OK)
  1772. return PEP_UNKNOWN_DB_ERROR;
  1773. int_result = sqlite3_prepare_v2(session->db, sql_set_pEp_version,
  1774. (int)strlen(sql_set_pEp_version), &session->set_pEp_version,
  1775. NULL);
  1776. assert(int_result == SQLITE_OK);
  1777. if (int_result != SQLITE_OK)
  1778. return PEP_UNKNOWN_DB_ERROR;
  1779. int_result = sqlite3_prepare_v2(session->db, sql_upgrade_pEp_version_by_user_id,
  1780. (int)strlen(sql_upgrade_pEp_version_by_user_id), &session->upgrade_pEp_version_by_user_id,
  1781. NULL);
  1782. assert(int_result == SQLITE_OK);
  1783. if (int_result != SQLITE_OK)
  1784. return PEP_UNKNOWN_DB_ERROR;
  1785. int_result = sqlite3_prepare_v2(session->db, sql_clear_trust_info,
  1786. (int)strlen(sql_clear_trust_info), &session->clear_trust_info, NULL);
  1787. assert(int_result == SQLITE_OK);
  1788. if (int_result != SQLITE_OK)
  1789. return PEP_UNKNOWN_DB_ERROR;
  1790. int_result = sqlite3_prepare_v2(session->db, sql_set_trust,
  1791. (int)strlen(sql_set_trust), &session->set_trust, NULL);
  1792. assert(int_result == SQLITE_OK);
  1793. if (int_result != SQLITE_OK)
  1794. return PEP_UNKNOWN_DB_ERROR;
  1795. int_result = sqlite3_prepare_v2(session->db, sql_update_trust,
  1796. (int)strlen(sql_update_trust), &session->update_trust, NULL);
  1797. assert(int_result == SQLITE_OK);
  1798. if (int_result != SQLITE_OK)
  1799. return PEP_UNKNOWN_DB_ERROR;
  1800. int_result = sqlite3_prepare_v2(session->db, sql_update_trust_to_pEp,
  1801. (int)strlen(sql_update_trust_to_pEp), &session->update_trust_to_pEp, NULL);
  1802. assert(int_result == SQLITE_OK);
  1803. if (int_result != SQLITE_OK)
  1804. return PEP_UNKNOWN_DB_ERROR;
  1805. int_result = sqlite3_prepare_v2(session->db, sql_exists_trust_entry,
  1806. (int)strlen(sql_exists_trust_entry), &session->exists_trust_entry, NULL);
  1807. assert(int_result == SQLITE_OK);
  1808. if (int_result != SQLITE_OK)
  1809. return PEP_UNKNOWN_DB_ERROR;
  1810. int_result = sqlite3_prepare_v2(session->db, sql_update_trust_for_fpr,
  1811. (int)strlen(sql_update_trust_for_fpr), &session->update_trust_for_fpr, NULL);
  1812. assert(int_result == SQLITE_OK);
  1813. if (int_result != SQLITE_OK)
  1814. return PEP_UNKNOWN_DB_ERROR;
  1815. int_result = sqlite3_prepare_v2(session->db, sql_get_trust,
  1816. (int)strlen(sql_get_trust), &session->get_trust, NULL);
  1817. assert(int_result == SQLITE_OK);
  1818. if (int_result != SQLITE_OK)
  1819. return PEP_UNKNOWN_DB_ERROR;
  1820. int_result = sqlite3_prepare_v2(session->db, sql_get_trust_by_userid,
  1821. (int)strlen(sql_get_trust_by_userid), &session->get_trust_by_userid, NULL);
  1822. assert(int_result == SQLITE_OK);
  1823. if (int_result != SQLITE_OK)
  1824. return PEP_UNKNOWN_DB_ERROR;
  1825. int_result = sqlite3_prepare_v2(session->db, sql_least_trust,
  1826. (int)strlen(sql_least_trust), &session->least_trust, NULL);
  1827. assert(int_result == SQLITE_OK);
  1828. if (int_result != SQLITE_OK)
  1829. return PEP_UNKNOWN_DB_ERROR;
  1830. int_result = sqlite3_prepare_v2(session->db, sql_update_key_sticky_bit_for_user,
  1831. (int)strlen(sql_update_key_sticky_bit_for_user),
  1832. &session->update_key_sticky_bit_for_user, NULL);
  1833. assert(int_result == SQLITE_OK);
  1834. if (int_result != SQLITE_OK)
  1835. return PEP_UNKNOWN_DB_ERROR;
  1836. int_result = sqlite3_prepare_v2(session->db, sql_is_key_sticky_for_user,
  1837. (int)strlen(sql_is_key_sticky_for_user),
  1838. &session->is_key_sticky_for_user, NULL);
  1839. assert(int_result == SQLITE_OK);
  1840. if (int_result != SQLITE_OK)
  1841. return PEP_UNKNOWN_DB_ERROR;
  1842. int_result = sqlite3_prepare_v2(session->db, sql_mark_as_compromised,
  1843. (int)strlen(sql_mark_as_compromised), &session->mark_compromised,
  1844. NULL);
  1845. assert(int_result == SQLITE_OK);
  1846. if (int_result != SQLITE_OK)
  1847. return PEP_UNKNOWN_DB_ERROR;
  1848. int_result = sqlite3_prepare_v2(session->db, sql_crashdump,
  1849. (int)strlen(sql_crashdump), &session->crashdump, NULL);
  1850. assert(int_result == SQLITE_OK);
  1851. if (int_result != SQLITE_OK)
  1852. return PEP_UNKNOWN_DB_ERROR;
  1853. int_result = sqlite3_prepare_v2(session->system_db, sql_languagelist,
  1854. (int)strlen(sql_languagelist), &session->languagelist, NULL);
  1855. assert(int_result == SQLITE_OK);
  1856. if (int_result != SQLITE_OK)
  1857. return PEP_UNKNOWN_DB_ERROR;
  1858. int_result = sqlite3_prepare_v2(session->system_db, sql_i18n_token,
  1859. (int)strlen(sql_i18n_token), &session->i18n_token, NULL);
  1860. assert(int_result == SQLITE_OK);
  1861. if (int_result != SQLITE_OK)
  1862. return PEP_UNKNOWN_DB_ERROR;
  1863. // Own keys
  1864. int_result = sqlite3_prepare_v2(session->db, sql_own_key_is_listed,
  1865. (int)strlen(sql_own_key_is_listed), &session->own_key_is_listed,
  1866. NULL);
  1867. assert(int_result == SQLITE_OK);
  1868. if (int_result != SQLITE_OK)
  1869. return PEP_UNKNOWN_DB_ERROR;
  1870. int_result = sqlite3_prepare_v2(session->db, sql_is_own_address,
  1871. (int)strlen(sql_is_own_address), &session->is_own_address,
  1872. NULL);
  1873. assert(int_result == SQLITE_OK);
  1874. if (int_result != SQLITE_OK)
  1875. return PEP_UNKNOWN_DB_ERROR;
  1876. int_result = sqlite3_prepare_v2(session->db, sql_own_identities_retrieve,
  1877. (int)strlen(sql_own_identities_retrieve),
  1878. &session->own_identities_retrieve, NULL);
  1879. assert(int_result == SQLITE_OK);
  1880. if (int_result != SQLITE_OK)
  1881. return PEP_UNKNOWN_DB_ERROR;
  1882. int_result = sqlite3_prepare_v2(session->db, sql_own_keys_retrieve,
  1883. (int)strlen(sql_own_keys_retrieve),
  1884. &session->own_keys_retrieve, NULL);
  1885. assert(int_result == SQLITE_OK);
  1886. if (int_result != SQLITE_OK)
  1887. return PEP_UNKNOWN_DB_ERROR;
  1888. // int_result = sqlite3_prepare_v2(session->db, sql_set_own_key,
  1889. // (int)strlen(sql_set_own_key),
  1890. // &session->set_own_key, NULL);
  1891. // assert(int_result == SQLITE_OK);
  1892. // Sequence
  1893. int_result = sqlite3_prepare_v2(session->db, sql_sequence_value1,
  1894. (int)strlen(sql_sequence_value1), &session->sequence_value1,
  1895. NULL);
  1896. assert(int_result == SQLITE_OK);
  1897. if (int_result != SQLITE_OK)
  1898. return PEP_UNKNOWN_DB_ERROR;
  1899. int_result = sqlite3_prepare_v2(session->db, sql_sequence_value2,
  1900. (int)strlen(sql_sequence_value2), &session->sequence_value2,
  1901. NULL);
  1902. assert(int_result == SQLITE_OK);
  1903. if (int_result != SQLITE_OK)
  1904. return PEP_UNKNOWN_DB_ERROR;
  1905. // Revocation tracking
  1906. int_result = sqlite3_prepare_v2(session->db, sql_set_revoked,
  1907. (int)strlen(sql_set_revoked), &session->set_revoked, NULL);
  1908. assert(int_result == SQLITE_OK);
  1909. if (int_result != SQLITE_OK)
  1910. return PEP_UNKNOWN_DB_ERROR;
  1911. int_result = sqlite3_prepare_v2(session->db, sql_get_revoked,
  1912. (int)strlen(sql_get_revoked), &session->get_revoked, NULL);
  1913. assert(int_result == SQLITE_OK);
  1914. if (int_result != SQLITE_OK)
  1915. return PEP_UNKNOWN_DB_ERROR;
  1916. int_result = sqlite3_prepare_v2(session->db, sql_get_replacement_fpr,
  1917. (int)strlen(sql_get_replacement_fpr), &session->get_replacement_fpr, NULL);
  1918. assert(int_result == SQLITE_OK);
  1919. if (int_result != SQLITE_OK)
  1920. return PEP_UNKNOWN_DB_ERROR;
  1921. int_result = sqlite3_prepare_v2(session->db, sql_add_mistrusted_key,
  1922. (int)strlen(sql_add_mistrusted_key), &session->add_mistrusted_key, NULL);
  1923. assert(int_result == SQLITE_OK);
  1924. if (int_result != SQLITE_OK)
  1925. return PEP_UNKNOWN_DB_ERROR;
  1926. int_result = sqlite3_prepare_v2(session->db, sql_delete_mistrusted_key,
  1927. (int)strlen(sql_delete_mistrusted_key), &session->delete_mistrusted_key, NULL);
  1928. assert(int_result == SQLITE_OK);
  1929. if (int_result != SQLITE_OK)
  1930. return PEP_UNKNOWN_DB_ERROR;
  1931. int_result = sqlite3_prepare_v2(session->db, sql_is_mistrusted_key,
  1932. (int)strlen(sql_is_mistrusted_key), &session->is_mistrusted_key, NULL);
  1933. assert(int_result == SQLITE_OK);
  1934. if (int_result != SQLITE_OK)
  1935. return PEP_UNKNOWN_DB_ERROR;
  1936. /* Groups */
  1937. int_result = sqlite3_prepare_v2(session->db, sql_create_group,
  1938. (int)strlen(sql_create_group), &session->create_group, NULL);
  1939. assert(int_result == SQLITE_OK);
  1940. if (int_result != SQLITE_OK)
  1941. return PEP_UNKNOWN_DB_ERROR;
  1942. int_result = sqlite3_prepare_v2(session->db, sql_enable_group,
  1943. (int)strlen(sql_enable_group), &session->enable_group, NULL);
  1944. assert(int_result == SQLITE_OK);
  1945. if (int_result != SQLITE_OK)
  1946. return PEP_UNKNOWN_DB_ERROR;
  1947. int_result = sqlite3_prepare_v2(session->db, sql_disable_group,
  1948. (int)strlen(sql_disable_group), &session->disable_group, NULL);
  1949. assert(int_result == SQLITE_OK);
  1950. if (int_result != SQLITE_OK)
  1951. return PEP_UNKNOWN_DB_ERROR;
  1952. int_result = sqlite3_prepare_v2(session->db, sql_exists_group_entry,
  1953. (int)strlen(sql_exists_group_entry), &session->exists_group_entry, NULL);
  1954. assert(int_result == SQLITE_OK);
  1955. if (int_result != SQLITE_OK)
  1956. return PEP_UNKNOWN_DB_ERROR;
  1957. int_result = sqlite3_prepare_v2(session->db, sql_group_add_member,
  1958. (int)strlen(sql_group_add_member), &session->group_add_member, NULL);
  1959. assert(int_result == SQLITE_OK);
  1960. if (int_result != SQLITE_OK)
  1961. return PEP_UNKNOWN_DB_ERROR;
  1962. int_result = sqlite3_prepare_v2(session->db, sql_group_delete_member,
  1963. (int)strlen(sql_group_delete_member), &session->group_delete_member, NULL);
  1964. assert(int_result == SQLITE_OK);
  1965. if (int_result != SQLITE_OK)
  1966. return PEP_UNKNOWN_DB_ERROR;
  1967. int_result = sqlite3_prepare_v2(session->db, sql_set_group_member_status,
  1968. (int)strlen(sql_set_group_member_status), &session->set_group_member_status, NULL);
  1969. assert(int_result == SQLITE_OK);
  1970. if (int_result != SQLITE_OK)
  1971. return PEP_UNKNOWN_DB_ERROR;
  1972. int_result = sqlite3_prepare_v2(session->db, sql_group_join,
  1973. (int)strlen(sql_group_join), &session->group_join, NULL);
  1974. assert(int_result == SQLITE_OK);
  1975. if (int_result != SQLITE_OK)
  1976. return PEP_UNKNOWN_DB_ERROR;
  1977. int_result = sqlite3_prepare_v2(session->db, sql_leave_group,
  1978. (int)strlen(sql_leave_group), &session->leave_group, NULL);
  1979. assert(int_result == SQLITE_OK);
  1980. if (int_result != SQLITE_OK)
  1981. return PEP_UNKNOWN_DB_ERROR;
  1982. int_result = sqlite3_prepare_v2(session->db, sql_get_all_members,
  1983. (int)strlen(sql_get_all_members), &session->get_all_members, NULL);
  1984. assert(int_result == SQLITE_OK);
  1985. if (int_result != SQLITE_OK)
  1986. return PEP_UNKNOWN_DB_ERROR;
  1987. int_result = sqlite3_prepare_v2(session->db, sql_get_active_members,
  1988. (int)strlen(sql_get_active_members), &session->get_active_members, NULL);
  1989. assert(int_result == SQLITE_OK);
  1990. if (int_result != SQLITE_OK)
  1991. return PEP_UNKNOWN_DB_ERROR;
  1992. int_result = sqlite3_prepare_v2(session->db, sql_get_all_groups,
  1993. (int)strlen(sql_get_all_groups), &session->get_all_groups, NULL);
  1994. assert(int_result == SQLITE_OK);
  1995. if (int_result != SQLITE_OK)
  1996. return PEP_UNKNOWN_DB_ERROR;
  1997. int_result = sqlite3_prepare_v2(session->db, sql_get_active_groups,
  1998. (int)strlen(sql_get_active_groups), &session->get_active_groups, NULL);
  1999. assert(int_result == SQLITE_OK);
  2000. if (int_result != SQLITE_OK)
  2001. return PEP_UNKNOWN_DB_ERROR;
  2002. int_result = sqlite3_prepare_v2(session->db, sql_add_own_membership_entry,
  2003. (int)strlen(sql_add_own_membership_entry), &session->add_own_membership_entry, NULL);
  2004. assert(int_result == SQLITE_OK);
  2005. if (int_result != SQLITE_OK)
  2006. return PEP_UNKNOWN_DB_ERROR;
  2007. int_result = sqlite3_prepare_v2(session->db, sql_get_own_membership_status,
  2008. (int)strlen(sql_get_own_membership_status), &session->get_own_membership_status, NULL);
  2009. assert(int_result == SQLITE_OK);
  2010. if (int_result != SQLITE_OK)
  2011. return PEP_UNKNOWN_DB_ERROR;
  2012. int_result = sqlite3_prepare_v2(session->db, sql_retrieve_own_membership_info_for_group_and_ident,
  2013. (int)strlen(sql_retrieve_own_membership_info_for_group_and_ident), &session->retrieve_own_membership_info_for_group_and_ident, NULL);
  2014. assert(int_result == SQLITE_OK);
  2015. if (int_result != SQLITE_OK)
  2016. return PEP_UNKNOWN_DB_ERROR;
  2017. int_result = sqlite3_prepare_v2(session->db, sql_retrieve_own_membership_info_for_group,
  2018. (int)strlen(sql_retrieve_own_membership_info_for_group), &session->retrieve_own_membership_info_for_group, NULL);
  2019. assert(int_result == SQLITE_OK);
  2020. if (int_result != SQLITE_OK)
  2021. return PEP_UNKNOWN_DB_ERROR;
  2022. int_result = sqlite3_prepare_v2(session->db, sql_get_group_manager,
  2023. (int)strlen(sql_get_group_manager), &session->get_group_manager, NULL);
  2024. assert(int_result == SQLITE_OK);
  2025. if (int_result != SQLITE_OK)
  2026. return PEP_UNKNOWN_DB_ERROR;
  2027. int_result = sqlite3_prepare_v2(session->db, sql_is_invited_group_member,
  2028. (int)strlen(sql_is_invited_group_member), &session->is_invited_group_member, NULL);
  2029. assert(int_result == SQLITE_OK);
  2030. if (int_result != SQLITE_OK)
  2031. return PEP_UNKNOWN_DB_ERROR;
  2032. int_result = sqlite3_prepare_v2(session->db, sql_is_active_group_member,
  2033. (int)strlen(sql_is_active_group_member), &session->is_active_group_member, NULL);
  2034. assert(int_result == SQLITE_OK);
  2035. if (int_result != SQLITE_OK)
  2036. return PEP_UNKNOWN_DB_ERROR;
  2037. int_result = sqlite3_prepare_v2(session->db, sql_is_group_active,
  2038. (int)strlen(sql_is_group_active), &session->is_group_active, NULL);
  2039. assert(int_result == SQLITE_OK);
  2040. if (int_result != SQLITE_OK)
  2041. return PEP_UNKNOWN_DB_ERROR;
  2042. // int_result = sqlite3_prepare_v2(session->db, sql_group_invite_exists,
  2043. // (int)strlen(sql_group_invite_exists), &session->group_invite_exists, NULL);
  2044. // assert(int_result == SQLITE_OK);
  2045. //
  2046. // if (int_result != SQLITE_OK)
  2047. // return PEP_UNKNOWN_DB_ERROR;
  2048. int_result = sqlite3_prepare_v2(session->db, sql_log,
  2049. (int)strlen(sql_log), &session->log, NULL);
  2050. assert(int_result == SQLITE_OK);
  2051. if (int_result != SQLITE_OK)
  2052. return PEP_UNKNOWN_DB_ERROR;
  2053. /* End groups */
  2054. return PEP_STATUS_OK;
  2055. }
  2056. PEP_STATUS pEp_finalize_sql_stmts(PEP_SESSION session) {
  2057. sqlite3_finalize(session->log);
  2058. sqlite3_finalize(session->trustword);
  2059. sqlite3_finalize(session->get_identity);
  2060. sqlite3_finalize(session->get_identity_without_trust_check);
  2061. sqlite3_finalize(session->get_identities_by_address);
  2062. sqlite3_finalize(session->get_identities_by_userid);
  2063. sqlite3_finalize(session->get_identities_by_main_key_id);
  2064. sqlite3_finalize(session->get_user_default_key);
  2065. sqlite3_finalize(session->get_all_keys_for_user);
  2066. sqlite3_finalize(session->get_default_own_userid);
  2067. sqlite3_finalize(session->get_userid_alias_default);
  2068. sqlite3_finalize(session->add_userid_alias);
  2069. sqlite3_finalize(session->replace_identities_fpr);
  2070. sqlite3_finalize(session->get_default_identity_fpr);
  2071. sqlite3_finalize(session->remove_fpr_as_identity_default);
  2072. sqlite3_finalize(session->remove_fpr_as_user_default);
  2073. sqlite3_finalize(session->set_person);
  2074. sqlite3_finalize(session->delete_person);
  2075. sqlite3_finalize(session->update_person);
  2076. sqlite3_finalize(session->set_as_pEp_user);
  2077. sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
  2078. sqlite3_finalize(session->is_pEp_user);
  2079. sqlite3_finalize(session->exists_person);
  2080. sqlite3_finalize(session->add_into_social_graph);
  2081. sqlite3_finalize(session->get_own_address_binding_from_contact);
  2082. sqlite3_finalize(session->set_revoke_contact_as_notified);
  2083. sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);
  2084. sqlite3_finalize(session->was_id_for_revoke_contacted);
  2085. sqlite3_finalize(session->has_id_contacted_address);
  2086. sqlite3_finalize(session->get_last_contacted);
  2087. sqlite3_finalize(session->set_pgp_keypair);
  2088. sqlite3_finalize(session->exists_identity_entry);
  2089. sqlite3_finalize(session->set_identity_entry);
  2090. sqlite3_finalize(session->update_identity_entry);
  2091. sqlite3_finalize(session->force_set_identity_username);
  2092. sqlite3_finalize(session->set_identity_flags);
  2093. sqlite3_finalize(session->unset_identity_flags);
  2094. sqlite3_finalize(session->set_ident_enc_format);
  2095. sqlite3_finalize(session->set_pEp_version);
  2096. sqlite3_finalize(session->exists_trust_entry);
  2097. sqlite3_finalize(session->clear_trust_info);
  2098. sqlite3_finalize(session->set_trust);
  2099. sqlite3_finalize(session->update_trust);
  2100. sqlite3_finalize(session->update_trust_to_pEp);
  2101. sqlite3_finalize(session->update_trust_for_fpr);
  2102. sqlite3_finalize(session->get_trust);
  2103. sqlite3_finalize(session->get_trust_by_userid);
  2104. sqlite3_finalize(session->least_trust);
  2105. sqlite3_finalize(session->update_key_sticky_bit_for_user);
  2106. sqlite3_finalize(session->is_key_sticky_for_user);
  2107. sqlite3_finalize(session->mark_compromised);
  2108. sqlite3_finalize(session->crashdump);
  2109. sqlite3_finalize(session->languagelist);
  2110. sqlite3_finalize(session->i18n_token);
  2111. sqlite3_finalize(session->replace_userid);
  2112. sqlite3_finalize(session->delete_key);
  2113. sqlite3_finalize(session->replace_main_user_fpr);
  2114. sqlite3_finalize(session->replace_main_user_fpr_if_equal);
  2115. sqlite3_finalize(session->get_main_user_fpr);
  2116. sqlite3_finalize(session->refresh_userid_default_key);
  2117. sqlite3_finalize(session->own_key_is_listed);
  2118. sqlite3_finalize(session->is_own_address);
  2119. sqlite3_finalize(session->own_identities_retrieve);
  2120. sqlite3_finalize(session->own_keys_retrieve);
  2121. // sqlite3_finalize(session->set_own_key);
  2122. sqlite3_finalize(session->sequence_value1);
  2123. sqlite3_finalize(session->sequence_value2);
  2124. sqlite3_finalize(session->set_revoked);
  2125. sqlite3_finalize(session->get_revoked);
  2126. sqlite3_finalize(session->get_replacement_fpr);
  2127. sqlite3_finalize(session->add_mistrusted_key);
  2128. sqlite3_finalize(session->delete_mistrusted_key);
  2129. sqlite3_finalize(session->is_mistrusted_key);
  2130. sqlite3_finalize(session->create_group);
  2131. sqlite3_finalize(session->enable_group);
  2132. sqlite3_finalize(session->disable_group);
  2133. sqlite3_finalize(session->exists_group_entry);
  2134. sqlite3_finalize(session->group_add_member);
  2135. sqlite3_finalize(session->group_delete_member);
  2136. sqlite3_finalize(session->set_group_member_status);
  2137. sqlite3_finalize(session->group_join);
  2138. sqlite3_finalize(session->leave_group);
  2139. sqlite3_finalize(session->get_all_members);
  2140. sqlite3_finalize(session->get_active_members);
  2141. sqlite3_finalize(session->get_active_groups);
  2142. sqlite3_finalize(session->get_all_groups);
  2143. sqlite3_finalize(session->add_own_membership_entry);
  2144. sqlite3_finalize(session->get_own_membership_status);
  2145. sqlite3_finalize(session->retrieve_own_membership_info_for_group_and_ident);
  2146. sqlite3_finalize(session->retrieve_own_membership_info_for_group);
  2147. sqlite3_finalize(session->get_group_manager);
  2148. sqlite3_finalize(session->is_invited_group_member);
  2149. sqlite3_finalize(session->is_active_group_member);
  2150. sqlite3_finalize(session->is_group_active);
  2151. sqlite3_finalize(session->set_pgp_keypair_flags);
  2152. sqlite3_finalize(session->unset_pgp_keypair_flags);
  2153. // retrieve_own_membership_info_for_group_and_ident
  2154. // sqlite3_finalize(session->group_invite_exists);
  2155. return PEP_STATUS_OK;
  2156. }