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