src/pEpEngine.c
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 10 Sep 2019 15:04:36 +0200
branchENGINE-630
changeset 4065 7f0cf93bc2af
parent 3982 c4162e447585
child 4071 68f242584e18
permissions -rw-r--r--
import_key surgery at least doesn't BREAK anything now. Needs to be tested on its explicitly added functionality, however.
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@125
     4
#include "pEp_internal.h"
vb@98
     5
#include "dynamic_api.h"
vb@28
     6
#include "cryptotech.h"
vb@28
     7
#include "transport.h"
vb@515
     8
#include "blacklist.h"
vb@2880
     9
#include "KeySync_fsm.h"
vb@0
    10
krista@2176
    11
#include <time.h>
krista@2176
    12
#include <stdlib.h>
krista@2487
    13
krista@3977
    14
#define _PEP_SQLITE_DEBUG 0
krista@2488
    15
#if _PEP_SQLITE_DEBUG
krista@2479
    16
#include <sqlite3.h>
krista@2487
    17
#endif
krista@2480
    18
krista@2125
    19
static volatile int init_count = -1;
vb@62
    20
krista@1884
    21
// sql overloaded functions - modified from sqlite3.c
krista@1884
    22
static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
krista@1884
    23
    const char *z2;
vb@3090
    24
    int n;
krista@1884
    25
    z2 = (char*)sqlite3_value_text(argv[0]);
krista@1884
    26
    n = sqlite3_value_bytes(argv[0]);
krista@1884
    27
    /* Verify that the call to _bytes() does not invalidate the _text() pointer */
krista@1884
    28
    assert( z2==(char*)sqlite3_value_text(argv[0]) );
krista@1884
    29
    if( z2 ){
vb@3090
    30
        char *z1 = (char*)sqlite3_malloc(n+1);
krista@1884
    31
        if( z1 ){
vb@3090
    32
            for(int i=0; i<n; i++){
krista@1884
    33
                char c = z2[i];
krista@1884
    34
                char c_mod = c | 0x20;
krista@1884
    35
                if (c_mod < 0x61 || c_mod > 0x7a)
krista@1884
    36
                    c_mod = c;
krista@1884
    37
                z1[i] = c_mod;
krista@1884
    38
            }
krista@1884
    39
            z1[n] = '\0';
krista@1884
    40
            sqlite3_result_text(ctx, z1, n, sqlite3_free);
krista@1884
    41
        }
krista@1884
    42
    }
krista@1884
    43
}
krista@1884
    44
krista@2480
    45
#if _PEP_SQLITE_DEBUG
krista@2479
    46
int sql_trace_callback (unsigned trace_constant, 
krista@2479
    47
                        void* context_ptr,
krista@2479
    48
                        void* P,
krista@2479
    49
                        void* X) {
krista@2479
    50
    switch (trace_constant) {
krista@2479
    51
        case SQLITE_TRACE_STMT:
krista@2479
    52
            fprintf(stderr, "SQL_DEBUG: STMT - ");
krista@2479
    53
            const char* X_str = (const char*) X;
krista@2479
    54
            if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
krista@2479
    55
                fprintf(stderr, "%s\n", X_str);
krista@2479
    56
            else
krista@2479
    57
                fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
krista@2479
    58
            break;
krista@2479
    59
        case SQLITE_TRACE_ROW:
krista@2479
    60
            fprintf(stderr, "SQL_DEBUG: ROW - ");
krista@2479
    61
            fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
krista@2479
    62
            break;            
krista@2479
    63
        case SQLITE_TRACE_CLOSE:
krista@2479
    64
            fprintf(stderr, "SQL_DEBUG: CLOSE - ");
krista@2479
    65
            break;
krista@2479
    66
        default:
krista@2479
    67
            break;
krista@2479
    68
    }
krista@2479
    69
    return 0;
krista@2479
    70
}
krista@2480
    71
#endif
krista@1884
    72
edouard@1518
    73
// sql manipulation statements
edouard@1518
    74
static const char *sql_log = 
edouard@1518
    75
    "insert into log (title, entity, description, comment)"
edouard@1518
    76
     "values (?1, ?2, ?3, ?4);";
edouard@1518
    77
edouard@1518
    78
static const char *sql_trustword = 
edouard@1518
    79
    "select id, word from wordlist where lang = lower(?1) "
edouard@1518
    80
    "and id = ?2 ;";
edouard@1518
    81
krista@3350
    82
// FIXME?: problems if we don't have a key for the user - we get nothing
edouard@1518
    83
static const char *sql_get_identity =  
edouard@1518
    84
    "select fpr, username, comm_type, lang,"
krista@2461
    85
    "   identity.flags | pgp_keypair.flags,"
krista@3730
    86
    "   is_own, pEp_version_major, pEp_version_minor"
edouard@1518
    87
    "   from identity"
edouard@1518
    88
    "   join person on id = identity.user_id"
edouard@1518
    89
    "   join pgp_keypair on fpr = identity.main_key_id"
edouard@1518
    90
    "   join trust on id = trust.user_id"
krista@1884
    91
    "       and pgp_keypair_fpr = identity.main_key_id"    
krista@1884
    92
    "   where (case when (address = ?1) then (1)"
krista@1884
    93
    "               when (lower(address) = lower(?1)) then (1)"
krista@1884
    94
    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
krista@1884
    95
    "               else 0"
krista@1884
    96
    "          end) = 1"
krista@2505
    97
    "   and identity.user_id = ?2" 
krista@2506
    98
    "   order by is_own desc, "
krista@2506
    99
    "   timestamp desc; ";
edouard@1518
   100
krista@2893
   101
static const char *sql_get_identities_by_main_key_id =  
krista@2917
   102
    "select address, identity.user_id, username, comm_type, lang,"
krista@2893
   103
    "   identity.flags | pgp_keypair.flags,"
krista@3730
   104
    "   is_own, pEp_version_major, pEp_version_minor"
krista@2893
   105
    "   from identity"
krista@2893
   106
    "   join person on id = identity.user_id"
krista@2893
   107
    "   join pgp_keypair on fpr = identity.main_key_id"
krista@2893
   108
    "   join trust on id = trust.user_id"
krista@2893
   109
    "       and pgp_keypair_fpr = identity.main_key_id"    
krista@2917
   110
    "   where identity.main_key_id = ?1" 
krista@2893
   111
    "   order by is_own desc, "
krista@2893
   112
    "   timestamp desc; ";
krista@2893
   113
krista@2461
   114
static const char *sql_get_identity_without_trust_check =  
krista@2461
   115
    "select identity.main_key_id, username, lang,"
krista@3730
   116
    "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
krista@2461
   117
    "   from identity"
krista@2461
   118
    "   join person on id = identity.user_id"
krista@2461
   119
    "   where (case when (address = ?1) then (1)"
krista@2461
   120
    "               when (lower(address) = lower(?1)) then (1)"
krista@2461
   121
    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
krista@2461
   122
    "               else 0"
krista@2461
   123
    "          end) = 1"
krista@2505
   124
    "   and identity.user_id = ?2 "
krista@2506
   125
    "   order by is_own desc, "
krista@2506
   126
    "   timestamp desc; ";
krista@2461
   127
krista@2461
   128
static const char *sql_get_identities_by_address =  
krista@2461
   129
    "select user_id, identity.main_key_id, username, lang,"
krista@3730
   130
    "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
krista@2461
   131
    "   from identity"
krista@2461
   132
    "   join person on id = identity.user_id"
krista@2461
   133
    "   where (case when (address = ?1) then (1)"
krista@2461
   134
    "               when (lower(address) = lower(?1)) then (1)"
krista@2461
   135
    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
krista@2461
   136
    "               else 0"
krista@2505
   137
    "          end) = 1 "
krista@2506
   138
    "   order by is_own desc, "
krista@2506
   139
    "   timestamp desc; ";
krista@2966
   140
    
krista@3001
   141
static const char *sql_get_identities_by_userid =  
krista@3001
   142
    "select address, fpr, username, comm_type, lang,"
krista@3001
   143
    "   identity.flags | pgp_keypair.flags,"
krista@3730
   144
    "   is_own, pEp_version_major, pEp_version_minor"
krista@2966
   145
    "   from identity"
krista@2966
   146
    "   join person on id = identity.user_id"
krista@3001
   147
    "   join pgp_keypair on fpr = identity.main_key_id"
krista@3001
   148
    "   join trust on id = trust.user_id"
krista@3001
   149
    "       and pgp_keypair_fpr = identity.main_key_id"    
krista@3001
   150
    "   where identity.user_id = ?1" 
krista@2966
   151
    "   order by is_own desc, "
krista@2966
   152
    "   timestamp desc; ";
krista@2461
   153
krista@1799
   154
static const char *sql_replace_identities_fpr =  
krista@1799
   155
    "update identity"
krista@1799
   156
    "   set main_key_id = ?1 "
krista@1799
   157
    "   where main_key_id = ?2 ;";
krista@2461
   158
    
krista@2461
   159
static const char *sql_remove_fpr_as_default =
krista@2461
   160
    "update person set main_key_id = NULL where main_key_id = ?1 ;"
krista@2461
   161
    "update identity set main_key_id = NULL where main_key_id = ?1 ;";
krista@1793
   162
edouard@1518
   163
// Set person, but if already exist, only update.
edouard@1518
   164
// if main_key_id already set, don't touch.
edouard@1518
   165
static const char *sql_set_person = 
krista@3420
   166
     "insert into person (id, username, lang, main_key_id)"
krista@3420
   167
     "  values (?1, ?2, ?3, ?4) ;";
krista@2478
   168
krista@2478
   169
static const char *sql_update_person = 
krista@2477
   170
    "update person "
krista@2477
   171
    "   set username = ?2, "
krista@2477
   172
    "       lang = ?3, "
krista@2477
   173
    "       main_key_id =  "
krista@2477
   174
    "           (select coalesce( "
krista@2477
   175
    "               (select main_key_id from person where id = ?1), " 
krista@3420
   176
    "                upper(replace(?4,' ',''))))"         
krista@2477
   177
    "   where id = ?1 ;";
krista@2965
   178
krista@2965
   179
// Will cascade.
krista@2965
   180
static const char *sql_delete_person = 
krista@2965
   181
     "delete from person where id = ?1 ;";
krista@2965
   182
vb@2833
   183
static const char *sql_set_as_pEp_user =
vb@2833
   184
    "update person set is_pEp_user = 1 "
krista@2468
   185
    "   where id = ?1 ; ";
krista@2468
   186
vb@2833
   187
static const char *sql_is_pEp_user =
vb@2833
   188
    "select is_pEp_user from person "
krista@2468
   189
    "   where id = ?1 ; ";
krista@2468
   190
krista@2468
   191
static const char* sql_exists_person = 
krista@2468
   192
    "select count(*) from person "
krista@2468
   193
    "   where id = ?1 ;";
krista@2468
   194
krista@2461
   195
// This will cascade to identity and trust
krista@2461
   196
static const char* sql_replace_userid =
krista@2461
   197
    "update person set id = ?1 " 
krista@2468
   198
    "   where id = ?2;";
krista@2461
   199
krista@2948
   200
// Hopefully this cascades and removes trust entries...
krista@2948
   201
static const char *sql_delete_key =
krista@2948
   202
    "delete from pgp_keypair "
krista@2948
   203
    "   where fpr = ?1 ; ";
krista@2948
   204
krista@2461
   205
static const char *sql_replace_main_user_fpr =  
krista@2461
   206
    "update person "
krista@2461
   207
    "   set main_key_id = ?1 "
krista@2461
   208
    "   where id = ?2 ;";
krista@2461
   209
krista@2461
   210
static const char *sql_get_main_user_fpr =  
krista@2461
   211
    "select main_key_id from person"
krista@2461
   212
    "   where id = ?1 ;";
krista@2461
   213
krista@2461
   214
static const char *sql_refresh_userid_default_key =
krista@2461
   215
    "update person "
krista@2461
   216
    "   set main_key_id = "
krista@2461
   217
    "       (select identity.main_key_id from identity "
krista@2461
   218
    "           join trust on trust.user_id = identity.user_id "
krista@2461
   219
    "               and trust.pgp_keypair_fpr = identity.main_key_id "
krista@2461
   220
    "           join person on identity.user_id = identity.user_id "
krista@2461
   221
    "       where identity.user_id = ?1 "
krista@2461
   222
    "       order by trust.comm_type desc "
krista@2461
   223
    "       limit 1) "
krista@2461
   224
    "where id = ?1 ; ";
edouard@1518
   225
edouard@1518
   226
static const char *sql_set_pgp_keypair = 
krista@2477
   227
    "insert or ignore into pgp_keypair (fpr) "
edouard@1518
   228
    "values (upper(replace(?1,' ',''))) ;";
edouard@1518
   229
krista@2480
   230
static const char* sql_exists_identity_entry = 
krista@2480
   231
    "select count(*) from identity "
krista@2481
   232
    "   where (case when (address = ?1) then (1)"
krista@2481
   233
    "               when (lower(address) = lower(?1)) then (1)"
krista@2481
   234
    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
krista@2481
   235
    "               else 0"
krista@2481
   236
    "          end) = 1"
krista@2481
   237
    "    and user_id = ?2;";
krista@2481
   238
 
krista@2478
   239
static const char *sql_set_identity_entry = 
krista@2479
   240
    "insert into identity ("
krista@2477
   241
    "       address, main_key_id, "
krista@3730
   242
    "       user_id, flags, is_own,"
krista@3730
   243
    "       pEp_version_major, pEp_version_minor"
krista@2477
   244
    "   ) values ("
krista@2477
   245
    "       ?1,"
krista@2477
   246
    "       upper(replace(?2,' ','')),"
krista@2477
   247
    "       ?3,"
krista@2477
   248
    "       ?4,"
krista@3730
   249
    "       ?5,"
krista@3730
   250
    "       ?6,"
krista@3730
   251
    "       ?7"
krista@2478
   252
    "   );";
krista@2478
   253
    
krista@2478
   254
static const char* sql_update_identity_entry =    
krista@2477
   255
    "update identity "
krista@2477
   256
    "   set main_key_id = upper(replace(?2,' ','')), "
krista@2477
   257
    "       flags = ?4, " 
krista@3730
   258
    "       is_own = ?5, "
krista@3730
   259
    "       pEp_version_major = ?6, "
krista@3976
   260
    "       pEp_version_minor = ?7 "    
krista@2481
   261
    "   where (case when (address = ?1) then (1)"
krista@2481
   262
    "               when (lower(address) = lower(?1)) then (1)"
krista@2481
   263
    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
krista@2481
   264
    "               else 0 "
krista@2481
   265
    "          end) = 1 "
krista@2481
   266
    "          and user_id = ?3 ;";
krista@2477
   267
edouard@1518
   268
    // " (select"
edouard@1518
   269
    // "   coalesce("
edouard@1518
   270
    // "    (select flags from identity"
edouard@1518
   271
    // "     where address = ?1 and"
edouard@1518
   272
    // "           user_id = ?3),"
edouard@1518
   273
    // "    0)"
edouard@1518
   274
    // " ) | (?4 & 255)"
edouard@1518
   275
    /* set_identity ignores previous flags, and doesn't filter machine flags */
edouard@1518
   276
        
edouard@1518
   277
static const char *sql_set_identity_flags = 
edouard@1518
   278
    "update identity set flags = "
krista@3422
   279
    "    ((?1 & 65535) | (select flags from identity"
krista@2481
   280
    "                    where (case when (address = ?2) then (1)"
krista@2481
   281
    "                                when (lower(address) = lower(?2)) then (1)"
krista@2481
   282
    "                                when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
krista@2481
   283
    "                                else 0 "    
krista@2481
   284
    "                           end) = 1 "
krista@2481
   285
    "                           and user_id = ?3)) "
krista@2481
   286
    "   where (case when (address = ?2) then (1)"
krista@2481
   287
    "               when (lower(address) = lower(?2)) then (1)"
krista@2481
   288
    "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
krista@2481
   289
    "               else 0"
krista@2481
   290
    "          end) = 1"
krista@2481
   291
    "          and user_id = ?3 ;";
edouard@1518
   292
edouard@1518
   293
static const char *sql_unset_identity_flags = 
edouard@1518
   294
    "update identity set flags = "
krista@3422
   295
    "    ( ~(?1 & 65535) & (select flags from identity"
krista@2481
   296
    "                    where (case when (address = ?2) then (1)"
krista@2481
   297
    "                                when (lower(address) = lower(?2)) then (1)"
krista@2481
   298
    "                                when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
krista@2481
   299
    "                                else 0 "    
krista@2481
   300
    "                           end) = 1 "
krista@2481
   301
    "                           and user_id = ?3)) "
krista@2481
   302
    "   where (case when (address = ?2) then (1)"
krista@2481
   303
    "               when (lower(address) = lower(?2)) then (1)"
krista@2481
   304
    "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
krista@2481
   305
    "               else 0"
krista@2481
   306
    "          end) = 1"
krista@2481
   307
    "          and user_id = ?3 ;";
edouard@1518
   308
krista@3729
   309
static const char *sql_set_pEp_version =
krista@3729
   310
    "update identity "
krista@3730
   311
    "   set pEp_version_major = ?1, "
krista@3730
   312
    "       pEp_version_minor = ?2 "
krista@3730
   313
    "   where (case when (address = ?3) then (1)"
krista@3730
   314
    "               when (lower(address) = lower(?3)) then (1)"
krista@3730
   315
    "               when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
krista@3729
   316
    "               else 0 "
krista@3729
   317
    "          end) = 1 "
krista@3730
   318
    "          and user_id = ?4 ;";
krista@3730
   319
krista@3730
   320
static const char *sql_upgrade_pEp_version_by_user_id =
krista@3730
   321
    "update identity "
krista@3730
   322
    "   set pEp_version_major = ?1, "
krista@3730
   323
    "       pEp_version_minor = ?2 "
krista@3730
   324
    "       where user_id = ?3 "
krista@3730
   325
    "           and (case when (pEp_version_major < ?1) then (1)"
krista@3730
   326
    "                     when (pEp_version_major > ?1) then (0)"
krista@3730
   327
    "                     when (pEp_version_minor < ?2) then (1)"
krista@3730
   328
    "                     else 0 "
krista@3730
   329
    "           end) = 1 ;";
krista@3729
   330
edouard@1518
   331
static const char *sql_set_trust =
krista@2479
   332
    "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
krista@2478
   333
    "values (?1, upper(replace(?2,' ','')), ?3) ;";
krista@2478
   334
krista@2478
   335
static const char *sql_update_trust =
krista@2477
   336
    "update trust set comm_type = ?3 " 
krista@2477
   337
    "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
krista@2480
   338
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 ;";
krista@2787
   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@3976
   934
#define _DDL_USER_VERSION "12"
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@0
  1151
edouard@1604
  1152
        if(version != 0) { 
edouard@1604
  1153
            // Version has been already set
vb@934
  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.
krista@1032
  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
            }
edouard@1604
  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@3727
  1476
                    NULL,
krista@3727
  1477
                    NULL,
krista@3727
  1478
                    NULL
krista@3727
  1479
                );
krista@3729
  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@3730
  1491
                    NULL,
krista@3730
  1492
                    NULL,
krista@3730
  1493
                    NULL
krista@3730
  1494
                );
krista@3730
  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@3972
  1501
                
krista@3972
  1502
                // Guess we were abusing sscanf here, so we'll do it this way:
krista@3972
  1503
                const char* cptr = PEP_VERSION;
krista@3972
  1504
                size_t major_len = 0;
krista@3972
  1505
                size_t minor_len = 0;
krista@3972
  1506
                
krista@3972
  1507
                char* bufptr = major_buf;
krista@3972
  1508
                while (*cptr != '.' && *cptr != '\0') {
krista@3972
  1509
                    *bufptr++ = *cptr++;
krista@3972
  1510
                    major_len++;
krista@3972
  1511
                }
krista@3972
  1512
                *bufptr = '\0';
krista@3972
  1513
                bufptr = minor_buf;
krista@3972
  1514
krista@3972
  1515
                if (*cptr == '.') {
krista@3972
  1516
                    cptr++;
krista@3972
  1517
                    while (*cptr != '\0') {
krista@3972
  1518
                        *bufptr++ = *cptr++;
krista@3972
  1519
                        minor_len++;
krista@3972
  1520
                    }
krista@3972
  1521
                }
krista@3972
  1522
                else {
krista@3972
  1523
                    *bufptr++ = '0';
krista@3972
  1524
                }
krista@3972
  1525
                *bufptr = '\0';
krista@3972
  1526
                                    
krista@3730
  1527
                const char* _ver_12_startstr =                     
krista@3730
  1528
                    "update identity\n"
krista@3730
  1529
                    "    set pEp_version_major = ";
krista@3730
  1530
                const char* _ver_12_midstr = ",\n"
krista@3730
  1531
                    "        pEp_version_minor = ";
krista@3730
  1532
                const char* _ver_12_endstr =     
krista@3730
  1533
                    "\n"
krista@3730
  1534
                    "    where identity.is_own = 1;\n";
krista@3730
  1535
                    
krista@3730
  1536
                size_t new_stringlen = strlen(_ver_12_startstr) + major_len +
krista@3730
  1537
                                       strlen(_ver_12_midstr) + minor_len +
krista@3730
  1538
                                       strlen(_ver_12_endstr);
krista@3730
  1539
                                       
krista@3730
  1540
                char* _ver_12_stmt = calloc(new_stringlen + 1, 1);
krista@3730
  1541
                snprintf(_ver_12_stmt, new_stringlen + 1, "%s%s%s%s%s",
krista@3730
  1542
                         _ver_12_startstr, major_buf, _ver_12_midstr, minor_buf, _ver_12_endstr);
krista@3730
  1543
                
krista@3730
  1544
                int_result = sqlite3_exec(
krista@3730
  1545
                    _session->db,
krista@3730
  1546
                    _ver_12_stmt,
krista@3730
  1547
                    NULL,
krista@3730
  1548
                    NULL,
krista@3730
  1549
                    NULL
krista@3730
  1550
                );
krista@3730
  1551
                free(_ver_12_stmt);
krista@3730
  1552
                if (status != PEP_STATUS_OK)
krista@3730
  1553
                    return status;                      
krista@3806
  1554
            }
krista@2468
  1555
        }        
edouard@1752
  1556
        else { 
edouard@1752
  1557
            // Version from DB was 0, it means this is initial setup.
edouard@1752
  1558
            // DB has just been created, and all tables are empty.
edouard@1752
  1559
            very_first = true;
edouard@1752
  1560
        }
vb@1453
  1561
vb@928
  1562
        if (version < atoi(_DDL_USER_VERSION)) {
vb@928
  1563
            int_result = sqlite3_exec(
vb@928
  1564
                _session->db,
krista@2787
  1565
                "pragma user_version = "_DDL_USER_VERSION";\n"
krista@2787
  1566
                "insert or replace into version_info (id, version)"
krista@2787
  1567
                    "values (1, '" PEP_ENGINE_VERSION "');",
vb@928
  1568
                NULL,
vb@928
  1569
                NULL,
vb@928
  1570
                NULL
vb@928
  1571
            );
vb@928
  1572
            assert(int_result == SQLITE_OK);
vb@928
  1573
        }
krista@2176
  1574
        
krista@2176
  1575
        // We need to init a few globals for message id that we'd rather not
krista@2176
  1576
        // calculate more than once.
krista@2176
  1577
        _init_globals();
vb@62
  1578
    }
vb@62
  1579
vb@951
  1580
    int_result = sqlite3_prepare_v2(_session->db, sql_log,
vb@951
  1581
            (int)strlen(sql_log), &_session->log, NULL);
roker@529
  1582
    assert(int_result == SQLITE_OK);
vb@0
  1583
vb@233
  1584
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
  1585
            (int)strlen(sql_trustword), &_session->trustword, NULL);
roker@529
  1586
    assert(int_result == SQLITE_OK);
vb@0
  1587
vb@0
  1588
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
  1589
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
roker@529
  1590
    assert(int_result == SQLITE_OK);
vb@0
  1591
krista@2461
  1592
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_trust_check,
krista@2461
  1593
            (int)strlen(sql_get_identity_without_trust_check), 
krista@2461
  1594
            &_session->get_identity_without_trust_check, NULL);
krista@2461
  1595
    assert(int_result == SQLITE_OK);
krista@2461
  1596
krista@2461
  1597
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_address,
krista@2461
  1598
            (int)strlen(sql_get_identities_by_address), 
krista@2461
  1599
            &_session->get_identities_by_address, NULL);
krista@2461
  1600
    assert(int_result == SQLITE_OK);
krista@2966
  1601
    
krista@2966
  1602
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
krista@2966
  1603
            (int)strlen(sql_get_identities_by_userid), 
krista@2966
  1604
            &_session->get_identities_by_userid, NULL);
krista@2966
  1605
    assert(int_result == SQLITE_OK);
krista@2461
  1606
krista@2893
  1607
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_main_key_id,
krista@2893
  1608
            (int)strlen(sql_get_identities_by_main_key_id), 
krista@2893
  1609
            &_session->get_identities_by_main_key_id, NULL);
krista@2893
  1610
    assert(int_result == SQLITE_OK);
krista@2893
  1611
krista@2461
  1612
    int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
krista@2461
  1613
            (int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
krista@2461
  1614
    assert(int_result == SQLITE_OK);
krista@2461
  1615
krista@2893
  1616
    int_result = sqlite3_prepare_v2(_session->db, sql_get_all_keys_for_user,
krista@2893
  1617
            (int)strlen(sql_get_all_keys_for_user), &_session->get_all_keys_for_user, NULL);
krista@2893
  1618
    assert(int_result == SQLITE_OK);
krista@2893
  1619
krista@2461
  1620
    int_result = sqlite3_prepare_v2(_session->db, sql_get_default_own_userid,
krista@2461
  1621
            (int)strlen(sql_get_default_own_userid), &_session->get_default_own_userid, NULL);
krista@2461
  1622
    assert(int_result == SQLITE_OK);
krista@2461
  1623
    
krista@2461
  1624
    int_result = sqlite3_prepare_v2(_session->db, sql_get_userid_alias_default,
krista@2461
  1625
            (int)strlen(sql_get_userid_alias_default), &_session->get_userid_alias_default, NULL);
krista@2461
  1626
    assert(int_result == SQLITE_OK);
krista@2461
  1627
krista@2461
  1628
    int_result = sqlite3_prepare_v2(_session->db, sql_add_userid_alias,
krista@2461
  1629
            (int)strlen(sql_add_userid_alias), &_session->add_userid_alias, NULL);
krista@2461
  1630
    assert(int_result == SQLITE_OK);
krista@2461
  1631
krista@2461
  1632
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_userid,
krista@2461
  1633
            (int)strlen(sql_replace_userid), &_session->replace_userid, NULL);
krista@2461
  1634
    assert(int_result == SQLITE_OK);
krista@2461
  1635
krista@2948
  1636
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_key,
krista@2948
  1637
            (int)strlen(sql_delete_key), &_session->delete_key, NULL);
krista@2948
  1638
    assert(int_result == SQLITE_OK);
krista@2948
  1639
krista@2461
  1640
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr,
krista@2461
  1641
            (int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
krista@2461
  1642
    assert(int_result == SQLITE_OK);
krista@2461
  1643
krista@2461
  1644
    int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
krista@2461
  1645
            (int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
krista@2461
  1646
    assert(int_result == SQLITE_OK);
krista@2461
  1647
krista@2461
  1648
    int_result = sqlite3_prepare_v2(_session->db, sql_refresh_userid_default_key,
krista@2461
  1649
            (int)strlen(sql_refresh_userid_default_key), &_session->refresh_userid_default_key, NULL);
krista@2461
  1650
    assert(int_result == SQLITE_OK);
krista@2461
  1651
krista@1799
  1652
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
krista@1799
  1653
            (int)strlen(sql_replace_identities_fpr), 
krista@1799
  1654
            &_session->replace_identities_fpr, NULL);
krista@1793
  1655
    assert(int_result == SQLITE_OK);
krista@2461
  1656
    
krista@2461
  1657
    int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
krista@2461
  1658
            (int)strlen(sql_remove_fpr_as_default), 
krista@2461
  1659
            &_session->remove_fpr_as_default, NULL);
krista@2461
  1660
    assert(int_result == SQLITE_OK);
krista@1793
  1661
vb@0
  1662
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
  1663
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
  1664
    assert(int_result == SQLITE_OK);
vb@62
  1665
krista@2478
  1666
    int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
krista@2478
  1667
            (int)strlen(sql_update_person), &_session->update_person, NULL);
krista@2478
  1668
    assert(int_result == SQLITE_OK);
krista@2478
  1669
krista@2965
  1670
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_person,
krista@2965
  1671
            (int)strlen(sql_delete_person), &_session->delete_person, NULL);
krista@2965
  1672
    assert(int_result == SQLITE_OK);
krista@2965
  1673
krista@2480
  1674
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_person,
krista@2480
  1675
            (int)strlen(sql_exists_person), &_session->exists_person, NULL);
krista@2480
  1676
    assert(int_result == SQLITE_OK);
krista@2480
  1677
vb@2833
  1678
    int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pEp_user,
vb@2833
  1679
            (int)strlen(sql_set_as_pEp_user), &_session->set_as_pEp_user, NULL);
krista@2468
  1680
    assert(int_result == SQLITE_OK);
krista@2468
  1681
    
vb@2833
  1682
    int_result = sqlite3_prepare_v2(_session->db, sql_is_pEp_user,
vb@2833
  1683
            (int)strlen(sql_is_pEp_user), &_session->is_pEp_user, NULL);
krista@2787
  1684
    assert(int_result == SQLITE_OK);
krista@2787
  1685
krista@2747
  1686
    int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
krista@2747
  1687
            (int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
krista@2747
  1688
    assert(int_result == SQLITE_OK);
krista@2747
  1689
krista@2752
  1690
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1691
            sql_get_own_address_binding_from_contact,
krista@2752
  1692
            (int)strlen(sql_get_own_address_binding_from_contact), 
krista@2752
  1693
            &_session->get_own_address_binding_from_contact, NULL);
krista@2752
  1694
    assert(int_result == SQLITE_OK);
krista@2752
  1695
krista@2752
  1696
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1697
            sql_set_revoke_contact_as_notified,
krista@2752
  1698
            (int)strlen(sql_set_revoke_contact_as_notified), 
krista@2752
  1699
            &_session->set_revoke_contact_as_notified, NULL);
krista@2752
  1700
    assert(int_result == SQLITE_OK);
krista@2752
  1701
krista@2752
  1702
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1703
            sql_get_contacted_ids_from_revoke_fpr,
krista@2752
  1704
            (int)strlen(sql_get_contacted_ids_from_revoke_fpr), 
krista@2752
  1705
            &_session->get_contacted_ids_from_revoke_fpr, NULL);
krista@2752
  1706
    assert(int_result == SQLITE_OK);
krista@2752
  1707
krista@2752
  1708
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2800
  1709
            sql_was_id_for_revoke_contacted,
krista@2800
  1710
            (int)strlen(sql_was_id_for_revoke_contacted), 
krista@2800
  1711
            &_session->was_id_for_revoke_contacted, NULL);
krista@2800
  1712
    assert(int_result == SQLITE_OK);
krista@2800
  1713
krista@2800
  1714
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2800
  1715
            sql_get_last_contacted,
krista@2800
  1716
            (int)strlen(sql_get_last_contacted), 
krista@2800
  1717
            &_session->get_last_contacted, NULL);
krista@2800
  1718
    assert(int_result == SQLITE_OK);
krista@2800
  1719
krista@2800
  1720
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1721
            sql_get_own_address_binding_from_contact,
krista@2752
  1722
            (int)strlen(sql_get_own_address_binding_from_contact), 
krista@2752
  1723
            &_session->get_own_address_binding_from_contact, NULL);
krista@2468
  1724
    assert(int_result == SQLITE_OK);
krista@2468
  1725
vb@0
  1726
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
vb@951
  1727
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
vb@951
  1728
            NULL);
roker@529
  1729
    assert(int_result == SQLITE_OK);
vb@62
  1730
krista@2478
  1731
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
krista@2478
  1732
            (int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
krista@2478
  1733
    assert(int_result == SQLITE_OK);
krista@2478
  1734
krista@2478
  1735
    int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
krista@2478
  1736
            (int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
roker@529
  1737
    assert(int_result == SQLITE_OK);
vb@62
  1738
krista@2480
  1739
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_identity_entry,
krista@2480
  1740
            (int)strlen(sql_exists_identity_entry), &_session->exists_identity_entry, NULL);
krista@2480
  1741
    assert(int_result == SQLITE_OK);
krista@2480
  1742
vb@932
  1743
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
vb@951
  1744
            (int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
vb@951
  1745
            NULL);
vb@932
  1746
    assert(int_result == SQLITE_OK);
vb@932
  1747
edouard@1394
  1748
    int_result = sqlite3_prepare_v2(_session->db, sql_unset_identity_flags,
edouard@1394
  1749
            (int)strlen(sql_unset_identity_flags), &_session->unset_identity_flags,
edouard@1394
  1750
            NULL);
edouard@1394
  1751
    assert(int_result == SQLITE_OK);
edouard@1394
  1752
krista@3729
  1753
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pEp_version,
krista@3729
  1754
            (int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
krista@3729
  1755
            NULL);
krista@3729
  1756
    assert(int_result == SQLITE_OK);
krista@3730
  1757
    
krista@3730
  1758
    int_result = sqlite3_prepare_v2(_session->db, sql_upgrade_pEp_version_by_user_id,
krista@3730
  1759
            (int)strlen(sql_upgrade_pEp_version_by_user_id), &_session->upgrade_pEp_version_by_user_id,
krista@3730
  1760
            NULL);
krista@3730
  1761
    assert(int_result == SQLITE_OK);
krista@3729
  1762
vb@0
  1763
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
  1764
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
roker@529
  1765
    assert(int_result == SQLITE_OK);
vb@62
  1766
krista@2478
  1767
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
krista@2478
  1768
            (int)strlen(sql_update_trust), &_session->update_trust, NULL);
krista@2478
  1769
    assert(int_result == SQLITE_OK);
krista@2478
  1770
vb@2834
  1771
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_to_pEp,
vb@2834
  1772
            (int)strlen(sql_update_trust_to_pEp), &_session->update_trust_to_pEp, NULL);
krista@2543
  1773
    assert(int_result == SQLITE_OK);
krista@2543
  1774
krista@2480
  1775
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_trust_entry,
krista@2480
  1776
                 (int)strlen(sql_exists_trust_entry), &_session->exists_trust_entry, NULL);
krista@2480
  1777
    assert(int_result == SQLITE_OK);
krista@2480
  1778
krista@1799
  1779
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
krista@1799
  1780
            (int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
krista@1799
  1781
    assert(int_result == SQLITE_OK);
krista@1799
  1782
vb@8
  1783
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
  1784
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
  1785
    assert(int_result == SQLITE_OK);
vb@0
  1786
krista@2967
  1787
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust_by_userid,
krista@2967
  1788
            (int)strlen(sql_get_trust_by_userid), &_session->get_trust_by_userid, NULL);
krista@2967
  1789
    assert(int_result == SQLITE_OK);
krista@2967
  1790
vb@251
  1791
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
  1792
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
  1793
    assert(int_result == SQLITE_OK);
vb@251
  1794
krista@2593
  1795
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromised,
krista@2593
  1796
            (int)strlen(sql_mark_as_compromised), &_session->mark_compromised,
vb@951
  1797
            NULL);
vb@357
  1798
    assert(int_result == SQLITE_OK);
vb@357
  1799
vb@453
  1800
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
  1801
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
  1802
    assert(int_result == SQLITE_OK);
vb@453
  1803
vb@458
  1804
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
  1805
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
roker@529
  1806
    assert(int_result == SQLITE_OK);
vb@458
  1807
vb@458
  1808
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
  1809
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
roker@529
  1810
    assert(int_result == SQLITE_OK);
krista@2461
  1811
    
fdik@494
  1812
    // blacklist
fdik@494
  1813
fdik@494
  1814
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
  1815
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
  1816
    assert(int_result == SQLITE_OK);
fdik@494
  1817
fdik@494
  1818
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
vb@951
  1819
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
vb@951
  1820
            NULL);
fdik@494
  1821
    assert(int_result == SQLITE_OK);
fdik@494
  1822
fdik@494
  1823
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
vb@951
  1824
            (int)strlen(sql_blacklist_is_listed),
vb@951
  1825
            &_session->blacklist_is_listed, NULL);
fdik@494
  1826
    assert(int_result == SQLITE_OK);
fdik@494
  1827
fdik@494
  1828
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
vb@951
  1829
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
vb@951
  1830
            NULL);
vb@482
  1831
    assert(int_result == SQLITE_OK);
krista@1275
  1832
    
Edouard@584
  1833
    // Own keys
Edouard@584
  1834
    
Edouard@584
  1835
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
vb@951
  1836
            (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
vb@951
  1837
            NULL);
Edouard@584
  1838
    assert(int_result == SQLITE_OK);
krista@3346
  1839
krista@3346
  1840
    int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
krista@3346
  1841
            (int)strlen(sql_is_own_address), &_session->is_own_address,
krista@3346
  1842
            NULL);
krista@3346
  1843
    assert(int_result == SQLITE_OK);
Edouard@584
  1844
    
vb@955
  1845
    int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
vb@955
  1846
            (int)strlen(sql_own_identities_retrieve),
vb@955
  1847
            &_session->own_identities_retrieve, NULL);
Edouard@584
  1848
    assert(int_result == SQLITE_OK);
vb@633
  1849
 
edouard@1394
  1850
    int_result = sqlite3_prepare_v2(_session->db, sql_own_keys_retrieve,
edouard@1394
  1851
            (int)strlen(sql_own_keys_retrieve),
edouard@1394
  1852
            &_session->own_keys_retrieve, NULL);
edouard@1394
  1853
    assert(int_result == SQLITE_OK);
edouard@1394
  1854
 
krista@2461
  1855
    // int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
krista@2461
  1856
    //         (int)strlen(sql_set_own_key),
krista@2461
  1857
    //         &_session->set_own_key, NULL);
krista@2461
  1858
    // assert(int_result == SQLITE_OK);
edouard@1370
  1859
 
vb@633
  1860
    // Sequence
vb@633
  1861
vb@633
  1862
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
vb@951
  1863
            (int)strlen(sql_sequence_value1), &_session->sequence_value1,
vb@951
  1864
            NULL);
vb@633
  1865
    assert(int_result == SQLITE_OK);
vb@633
  1866
vb@633
  1867
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
vb@951
  1868
            (int)strlen(sql_sequence_value2), &_session->sequence_value2,
vb@951
  1869
            NULL);
krista@1032
  1870
    assert(int_result == SQLITE_OK);
krista@1032
  1871
Edouard@693
  1872
    // Revocation tracking
Edouard@693
  1873
    
Edouard@693
  1874
    int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
vb@951
  1875
            (int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
Edouard@693
  1876
    assert(int_result == SQLITE_OK);
Edouard@693
  1877
    
Edouard@693
  1878
    int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
vb@951
  1879
            (int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
Edouard@693
  1880
    assert(int_result == SQLITE_OK);
Edouard@693
  1881
    
krista@2752
  1882
    int_result = sqlite3_prepare_v2(_session->db, sql_get_replacement_fpr,
krista@2752
  1883
            (int)strlen(sql_get_replacement_fpr), &_session->get_replacement_fpr, NULL);
krista@2752
  1884
    assert(int_result == SQLITE_OK);
krista@2752
  1885
krista@2471
  1886
    int_result = sqlite3_prepare_v2(_session->db, sql_add_mistrusted_key,
krista@2471
  1887
            (int)strlen(sql_add_mistrusted_key), &_session->add_mistrusted_key, NULL);
krista@2471
  1888
    assert(int_result == SQLITE_OK);
krista@2471
  1889
krista@2471
  1890
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_mistrusted_key,
krista@2471
  1891
            (int)strlen(sql_delete_mistrusted_key), &_session->delete_mistrusted_key, NULL);
krista@2471
  1892
    assert(int_result == SQLITE_OK);
krista@2471
  1893
krista@2471
  1894
    int_result = sqlite3_prepare_v2(_session->db, sql_is_mistrusted_key,
krista@2471
  1895
            (int)strlen(sql_is_mistrusted_key), &_session->is_mistrusted_key, NULL);
krista@2471
  1896
    assert(int_result == SQLITE_OK);
krista@2471
  1897
    
vb@65
  1898
    status = init_cryptotech(_session, in_first);
vb@65
  1899
    if (status != PEP_STATUS_OK)
vb@2834
  1900
        goto pEp_error;
vb@0
  1901
vb@65
  1902
    status = init_transport_system(_session, in_first);
vb@65
  1903
    if (status != PEP_STATUS_OK)
vb@2834
  1904
        goto pEp_error;
vb@62
  1905
vb@65
  1906
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
  1907
    if (status != PEP_STATUS_OK)
vb@2834
  1908
        goto pEp_error;
vb@65
  1909
vb@464
  1910
    // runtime config
vb@464
  1911
edouard@1752
  1912
    if (very_first)
edouard@1752
  1913
    {
vb@2539
  1914
#ifdef USE_GPG
edouard@1752
  1915
        // On first run, all private keys already present in PGP keyring 
edouard@1752
  1916
        // are taken as own in order to seamlessly integrate with
edouard@1752
  1917
        // pre-existing GPG setup.
edouard@1752
  1918
krista@2458
  1919
        // Note: earlier fears about danger because of DB reinitialisation should
krista@2458
  1920
        // be a non-issue here, as we ONLY take the ultimately trusted keys now.
krista@2458
  1921
        // Thus, unless the user has assigned ultimate trust through PGP, there is
krista@2458
  1922
        // no chance of automatically imported pEp keys from a previous run making
krista@2458
  1923
        // their way into PEP trusted status without explicit action (Bare imported
krista@2458
  1924
        // private keys have an 'unknown' trust designation in PGP).
krista@2458
  1925
krista@2458
  1926
        // We don't really worry about the status here.
vb@2539
  1927
        status = pgp_import_ultimately_trusted_keypairs(_session);        
vb@2539
  1928
#endif // USE_GPG
edouard@1752
  1929
    }
vb@464
  1930
roker@529
  1931
    *session = _session;
krista@2176
  1932
    
krista@2176
  1933
    // Note: Following statement is NOT for any cryptographic/secure functionality; it is
krista@2176
  1934
    //       ONLY used for some randomness in generated outer message ID, which are
krista@2176
  1935
    //       required by the RFC to be globally unique!
vb@2571
  1936
    srand((unsigned int) time(NULL));
krista@2176
  1937
    
roker@529
  1938
    return PEP_STATUS_OK;
vb@65
  1939
vb@65
  1940
enomem:
vb@65
  1941
    status = PEP_OUT_OF_MEMORY;
vb@65
  1942
vb@2834
  1943
pEp_error:
vb@65
  1944
    release(_session);
vb@65
  1945
    return status;
vb@0
  1946
}
vb@0
  1947
vb@0
  1948
DYNAMIC_API void release(PEP_SESSION session)
vb@0
  1949
{
vb@62
  1950
    bool out_last = false;
krista@2124
  1951
    int _count = --init_count;
edouard@2112
  1952
    
edouard@2112
  1953
    assert(_count >= -1);
roker@529
  1954
    assert(session);
vb@0
  1955
edouard@2112
  1956
    if (!((_count >= -1) && session))
vb@191
  1957
        return;
vb@191
  1958
vb@62
  1959
    // a small race condition but still a race condition
vb@113
  1960
    // mitigated by calling caveat (see documentation)
krista@2125
  1961
    // (release() is to be guarded by a mutex by the caller)
edouard@2112
  1962
    if (_count == -1)
vb@62
  1963
        out_last = true;
vb@62
  1964
roker@529
  1965
    if (session) {
vb@3391
  1966
        free_Sync_state(session);
vb@986
  1967
roker@529
  1968
        if (session->db) {
vb@517
  1969
            if (session->log)
vb@517
  1970
                sqlite3_finalize(session->log);
vb@233
  1971
            if (session->trustword)
vb@233
  1972
                sqlite3_finalize(session->trustword);
vb@65
  1973
            if (session->get_identity)
vb@65
  1974
                sqlite3_finalize(session->get_identity);
krista@2461
  1975
            if (session->get_identity_without_trust_check)
krista@2461
  1976
                sqlite3_finalize(session->get_identity_without_trust_check);
krista@2461
  1977
            if (session->get_identities_by_address)
krista@2461
  1978
                sqlite3_finalize(session->get_identities_by_address);            
krista@2966
  1979
            if (session->get_identities_by_userid)
krista@2893
  1980
                sqlite3_finalize(session->get_identities_by_userid);                
krista@2893
  1981
            if (session->get_identities_by_main_key_id)
krista@2893
  1982
                sqlite3_finalize(session->get_identities_by_main_key_id);                                
krista@2461
  1983
            if (session->get_user_default_key)
krista@2893
  1984
                sqlite3_finalize(session->get_user_default_key);
krista@2893
  1985
            if (session->get_all_keys_for_user)
krista@2893
  1986
                sqlite3_finalize(session->get_all_keys_for_user);                        
krista@2461
  1987
            if (session->get_default_own_userid)
krista@2461
  1988
                sqlite3_finalize(session->get_default_own_userid);
krista@2461
  1989
            if (session->get_userid_alias_default)
krista@2461
  1990
                sqlite3_finalize(session->get_userid_alias_default);
krista@2461
  1991
            if (session->add_userid_alias)
krista@2461
  1992
                sqlite3_finalize(session->add_userid_alias);
krista@1799
  1993
            if (session->replace_identities_fpr)
krista@1799
  1994
                sqlite3_finalize(session->replace_identities_fpr);        
krista@2461
  1995
            if (session->remove_fpr_as_default)
krista@2461
  1996
                sqlite3_finalize(session->remove_fpr_as_default);            
vb@65
  1997
            if (session->set_person)
vb@65
  1998
                sqlite3_finalize(session->set_person);
krista@2965
  1999
            if (session->delete_person)
krista@2965
  2000
                sqlite3_finalize(session->delete_person);                
vb@2833
  2001
            if (session->set_as_pEp_user)
vb@2833
  2002
                sqlite3_finalize(session->set_as_pEp_user);
krista@3730
  2003
            if (session->upgrade_pEp_version_by_user_id)
krista@3730
  2004
                sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
vb@2833
  2005
            if (session->is_pEp_user)
vb@2833
  2006
                sqlite3_finalize(session->is_pEp_user);
krista@2468
  2007
            if (session->exists_person)
krista@2747
  2008
                sqlite3_finalize(session->exists_person);
krista@2747
  2009
            if (session->add_into_social_graph)
krista@2752
  2010
                sqlite3_finalize(session->add_into_social_graph);  
krista@2752
  2011
            if (session->get_own_address_binding_from_contact)
krista@2752
  2012
                sqlite3_finalize(session->get_own_address_binding_from_contact);  
krista@2752
  2013
            if (session->set_revoke_contact_as_notified)
krista@2752
  2014
                sqlite3_finalize(session->set_revoke_contact_as_notified);  
krista@2752
  2015
            if (session->get_contacted_ids_from_revoke_fpr)
krista@2752
  2016
                sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);  
krista@2752
  2017
            if (session->was_id_for_revoke_contacted)
krista@2800
  2018
                sqlite3_finalize(session->was_id_for_revoke_contacted);   
krista@2800
  2019
            if (session->get_last_contacted)
krista@2800
  2020
                sqlite3_finalize(session->get_last_contacted);                                       
vb@65
  2021
            if (session->set_pgp_keypair)
vb@65
  2022
                sqlite3_finalize(session->set_pgp_keypair);
krista@2480
  2023
            if (session->exists_identity_entry)
krista@2480
  2024
                sqlite3_finalize(session->exists_identity_entry);                
krista@2478
  2025
            if (session->set_identity_entry)
krista@2478
  2026
                sqlite3_finalize(session->set_identity_entry);
krista@2478
  2027
            if (session->update_identity_entry)
krista@2478
  2028
                sqlite3_finalize(session->update_identity_entry);    
vb@932
  2029
            if (session->set_identity_flags)
vb@932
  2030
                sqlite3_finalize(session->set_identity_flags);
edouard@1394
  2031
            if (session->unset_identity_flags)
edouard@1394
  2032
                sqlite3_finalize(session->unset_identity_flags);
krista@3730
  2033
            if (session->set_pEp_version)
krista@3730
  2034
                sqlite3_finalize(session->set_pEp_version);                
krista@2480
  2035
            if (session->exists_trust_entry)
krista@2480
  2036
                sqlite3_finalize(session->exists_trust_entry);                                
vb@65
  2037
            if (session->set_trust)
vb@65
  2038
                sqlite3_finalize(session->set_trust);
krista@2478
  2039
            if (session->update_trust)
krista@2543
  2040
                sqlite3_finalize(session->update_trust);
vb@2834
  2041
            if (session->update_trust_to_pEp)
vb@2834
  2042
                sqlite3_finalize(session->update_trust_to_pEp);                                                
krista@1799
  2043
            if (session->update_trust_for_fpr)
krista@1799
  2044
                sqlite3_finalize(session->update_trust_for_fpr);
vb@65
  2045
            if (session->get_trust)
vb@65
  2046
                sqlite3_finalize(session->get_trust);
krista@2967
  2047
            if (session->get_trust_by_userid)
krista@2967
  2048
                sqlite3_finalize(session->get_trust_by_userid);                
vb@251
  2049
            if (session->least_trust)
vb@251
  2050
                sqlite3_finalize(session->least_trust);
krista@2593
  2051
            if (session->mark_compromised)
krista@2593
  2052
                sqlite3_finalize(session->mark_compromised);
vb@517
  2053
            if (session->crashdump)
vb@517
  2054
                sqlite3_finalize(session->crashdump);
vb@517
  2055
            if (session->languagelist)
vb@517
  2056
                sqlite3_finalize(session->languagelist);
vb@517
  2057
            if (session->i18n_token)
vb@517
  2058
                sqlite3_finalize(session->i18n_token);
krista@2461
  2059
            if (session->replace_userid)
krista@2461
  2060
                sqlite3_finalize(session->replace_userid);
krista@2948
  2061
            if (session->delete_key)
krista@2948
  2062
                sqlite3_finalize(session->delete_key);                
krista@2461
  2063
            if (session->replace_main_user_fpr)
krista@2461
  2064
                sqlite3_finalize(session->replace_main_user_fpr);                
krista@2461
  2065
            if (session->get_main_user_fpr)
krista@2461
  2066
                sqlite3_finalize(session->get_main_user_fpr);
krista@2461
  2067
            if (session->refresh_userid_default_key)
krista@2461
  2068
                sqlite3_finalize(session->refresh_userid_default_key);
vb@517
  2069
            if (session->blacklist_add)
vb@517
  2070
                sqlite3_finalize(session->blacklist_add);
vb@517
  2071
            if (session->blacklist_delete)
vb@517
  2072
                sqlite3_finalize(session->blacklist_delete);
vb@517
  2073
            if (session->blacklist_is_listed)
vb@517
  2074
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
  2075
            if (session->blacklist_retrieve)
vb@517
  2076
                sqlite3_finalize(session->blacklist_retrieve);
krista@957
  2077
            if (session->own_key_is_listed)
krista@957
  2078
                sqlite3_finalize(session->own_key_is_listed);
krista@3346
  2079
            if (session->is_own_address)
krista@3346
  2080
                sqlite3_finalize(session->is_own_address);
roker@1002
  2081
            if (session->own_identities_retrieve)
roker@1002
  2082
                sqlite3_finalize(session->own_identities_retrieve);
edouard@1394
  2083
            if (session->own_keys_retrieve)
edouard@1394
  2084
                sqlite3_finalize(session->own_keys_retrieve);
krista@2461
  2085
            // if (session->set_own_key)
krista@2461
  2086
            //     sqlite3_finalize(session->set_own_key);
krista@957
  2087
            if (session->sequence_value1)
krista@957
  2088
                sqlite3_finalize(session->sequence_value1);
krista@957
  2089
            if (session->sequence_value2)
krista@960
  2090
                sqlite3_finalize(session->sequence_value2);
krista@957
  2091
            if (session->set_revoked)
krista@957
  2092
                sqlite3_finalize(session->set_revoked);
krista@957
  2093
            if (session->get_revoked)
krista@957
  2094
                sqlite3_finalize(session->get_revoked);
krista@2752
  2095
            if (session->get_replacement_fpr)
krista@2752
  2096
                sqlite3_finalize(session->get_replacement_fpr);                
krista@2471
  2097
            if (session->add_mistrusted_key)
krista@2471
  2098
                sqlite3_finalize(session->add_mistrusted_key);
krista@2471
  2099
            if (session->delete_mistrusted_key)
krista@2471
  2100
                sqlite3_finalize(session->delete_mistrusted_key);
krista@2471
  2101
            if (session->is_mistrusted_key)
krista@2471
  2102
                sqlite3_finalize(session->is_mistrusted_key);
krista@2526
  2103
                
krista@2526
  2104
            if (session->db) {
krista@2526
  2105
                sqlite3_exec(        
krista@2526
  2106
                    session->db,
krista@2526
  2107
                    "PRAGMA optimize;\n",
krista@2526
  2108
                    NULL,
krista@2526
  2109
                    NULL,
krista@2526
  2110
                    NULL
krista@2526
  2111
                );
vb@65
  2112
                sqlite3_close_v2(session->db);
krista@2526
  2113
            }
vb@65
  2114
            if (session->system_db)
vb@65
  2115
                sqlite3_close_v2(session->system_db);
roker@529
  2116
        }
vb@65
  2117
vb@65
  2118
        release_transport_system(session, out_last);
vb@65
  2119
        release_cryptotech(session, out_last);
vb@65
  2120
roker@1722
  2121
#ifdef DEBUG_ERRORSTACK
roker@1722
  2122
        free_stringlist(session->errorstack);
roker@1722
  2123
#endif
vb@65
  2124
        free(session);
vb@62
  2125
    }
vb@0
  2126
}
vb@0
  2127
vb@467
  2128
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
  2129
{
vb@464
  2130
    assert(session);
vb@3691
  2131
    if (session)
vb@3691
  2132
        session->passive_mode = enable;
vb@464
  2133
}
vb@464
  2134
vb@467
  2135
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
  2136
{
vb@464
  2137
    assert(session);
vb@3691
  2138
    if (session)
vb@3691
  2139
        session->unencrypted_subject = enable;
vb@464
  2140
}
vb@464
  2141
vb@1819
  2142
DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
vb@1819
  2143
{
vb@1819
  2144
    assert(session);
vb@3691
  2145
    if (session)
vb@3691
  2146
        session->service_log = enable;
vb@1819
  2147
}
vb@1819
  2148
vb@0
  2149
DYNAMIC_API PEP_STATUS log_event(
vb@450
  2150
        PEP_SESSION session,
vb@451
  2151
        const char *title,
vb@451
  2152
        const char *entity,
vb@451
  2153
        const char *description,
vb@451
  2154
        const char *comment
vb@0
  2155
    )
vb@0
  2156
{
krista@3436
  2157
Thomas@3843
  2158
#if defined(_WIN32) && !defined(NDEBUG)
Thomas@3843
  2159
	log_output_debug(title, entity, description, comment);
Thomas@3843
  2160
#endif
Thomas@3843
  2161
krista@3436
  2162
// N.B. If testing (so NDEBUG not defined) but this message is spam,
krista@3436
  2163
//      put -D_PEP_SERVICE_LOG_OFF into CFLAGS/CXXFLAGS     
krista@3436
  2164
#if !defined(NDEBUG) && !defined(_PEP_SERVICE_LOG_OFF)
vb@3367
  2165
    fprintf(stdout, "\n*** %s %s %s %s\n", title, entity, description, comment);
vb@3367
  2166
    session->service_log = true;
vb@3747
  2167
vb@3747
  2168
    int result;
vb@3747
  2169
    
vb@3747
  2170
    assert(session);
vb@3747
  2171
    assert(title);
vb@3747
  2172
    assert(entity);
vb@3747
  2173
    
vb@3747
  2174
    if (!(session && title && entity))
vb@3747
  2175
        return PEP_ILLEGAL_VALUE;
vb@3747
  2176
    
vb@3747
  2177
    sqlite3_reset(session->log);
vb@3747
  2178
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
vb@3747
  2179
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
vb@3747
  2180
    if (description)
vb@3747
  2181
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
vb@3747
  2182
    else
vb@3747
  2183
        sqlite3_bind_null(session->log, 3);
vb@3747
  2184
    if (comment)
vb@3747
  2185
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
vb@3747
  2186
    else
vb@3747
  2187
        sqlite3_bind_null(session->log, 4);
vb@3747
  2188
    result = Sqlite3_step(session->log);
vb@3747
  2189
    sqlite3_reset(session->log);
vb@3747
  2190
    
vb@3367
  2191
#endif
krista@2499
  2192
    return PEP_STATUS_OK; // We ignore errors for this function.
vb@0
  2193
}
vb@0
  2194
vb@1819
  2195
DYNAMIC_API PEP_STATUS log_service(
vb@1819
  2196
        PEP_SESSION session,
vb@1819
  2197
        const char *title,
vb@1819
  2198
        const char *entity,
vb@1819
  2199
        const char *description,
vb@1819
  2200
        const char *comment
vb@1819
  2201
    )
vb@1819
  2202
{
vb@1819
  2203
    assert(session);
vb@1819
  2204
    if (!session)
vb@1819
  2205
        return PEP_ILLEGAL_VALUE;
vb@1819
  2206
vb@1819
  2207
    if (session->service_log)
vb@1819
  2208
        return log_event(session, title, entity, description, comment);
vb@1819
  2209
    else
vb@1819
  2210
        return PEP_STATUS_OK;
vb@1819
  2211
}
vb@1819
  2212
vb@233
  2213
DYNAMIC_API PEP_STATUS trustword(
vb@0
  2214
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
  2215
            char **word, size_t *wsize
vb@0
  2216
        )
vb@0
  2217
{
roker@529
  2218
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
  2219
roker@529
  2220
    assert(session);
roker@529
  2221
    assert(word);
roker@529
  2222
    assert(wsize);
vb@0
  2223
vb@191
  2224
    if (!(session && word && wsize))
vb@191
  2225
        return PEP_ILLEGAL_VALUE;
vb@191
  2226
roker@529
  2227
    *word = NULL;
roker@529
  2228
    *wsize = 0;
vb@0
  2229
roker@529
  2230
    if (lang == NULL)
roker@529
  2231
        lang = "en";
vb@0
  2232
roker@529
  2233
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  2234
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  2235
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  2236
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  2237
    assert(lang[2] == 0);
vb@0
  2238
roker@529
  2239
    sqlite3_reset(session->trustword);
vb@233
  2240
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
  2241
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
  2242
vb@3739
  2243
    const int result = Sqlite3_step(session->trustword);
roker@529
  2244
    if (result == SQLITE_ROW) {
vb@233
  2245
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
  2246
                    1));
roker@529
  2247
        if (*word)
vb@233
  2248
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
  2249
        else
Edouard@693
  2250
            status = PEP_OUT_OF_MEMORY;
roker@529
  2251
    } else
roker@529
  2252
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
  2253
roker@529
  2254
    sqlite3_reset(session->trustword);
roker@529
  2255
    return status;
vb@0
  2256
}
vb@0
  2257
vb@233
  2258
DYNAMIC_API PEP_STATUS trustwords(
vb@0
  2259
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
  2260
        char **words, size_t *wsize, int max_words
vb@0
  2261
    )
vb@0
  2262
{
roker@529
  2263
    const char *source = fingerprint;
vb@0
  2264
roker@529
  2265
    assert(session);
roker@529
  2266
    assert(fingerprint);
roker@529
  2267
    assert(words);
roker@529
  2268
    assert(wsize);
roker@529
  2269
    assert(max_words >= 0);
vb@0
  2270
vb@191
  2271
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
  2272
        return PEP_ILLEGAL_VALUE;
vb@191
  2273
roker@529
  2274
    *words = NULL;
roker@529
  2275
    *wsize = 0;
vb@0
  2276
roker@1559
  2277
    char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
  2278
    assert(buffer);
vb@0
  2279
    if (buffer == NULL)
vb@0
  2280
        return PEP_OUT_OF_MEMORY;
roker@1559
  2281
    char *dest = buffer;
vb@0
  2282
roker@1559
  2283
    const size_t fsize = strlen(fingerprint);
vb@0
  2284
roker@529
  2285
    if (!lang || !lang[0])
roker@529
  2286
        lang = "en";
vb@0
  2287
roker@529
  2288
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  2289
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  2290
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  2291
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  2292
    assert(lang[2] == 0);
vb@0
  2293
roker@529
  2294
    int n_words = 0;
roker@529
  2295
    while (source < fingerprint + fsize) {
vb@939
  2296
        PEP_STATUS _status;
roker@529
  2297
        uint16_t value;
roker@1559
  2298
        char *word = NULL;
roker@1559
  2299
        size_t _wsize = 0;
roker@529
  2300
        int j;
vb@0
  2301
vb@0
  2302
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
  2303
            if (*source >= 'a' && *source <= 'f')
roker@529
  2304
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
  2305
            else if (*source >= 'A' && *source <= 'F')
roker@529
  2306
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
  2307
            else if (*source >= '0' && *source <= '9')
roker@529
  2308
                value += (*source - '0') << (3 - j++) * 4;
roker@529
  2309
            
roker@529
  2310
            source++;
roker@529
  2311
        }
vb@0
  2312
roker@529
  2313
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
  2314
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
  2315
            free(buffer);
vb@0
  2316
            return PEP_OUT_OF_MEMORY;
vb@0
  2317
        }
roker@529
  2318
        if (word == NULL) {
vb@0
  2319
            free(buffer);
roker@529
  2320
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
  2321
        }