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