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