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