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