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