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