src/pEpEngine.c
author Volker Birk <vb@pep-project.org>
Tue, 21 May 2019 07:04:51 +0200
branchsync
changeset 3739 523fb9710660
parent 3720 9ed76a79d784
child 3740 248166aa4ab2
child 3747 82fe58af7b53
child 3750 318c86bc30b0
permissions -rw-r--r--
spinlock implementation for dealing with write locks
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@125
     4
#include "pEp_internal.h"
vb@98
     5
#include "dynamic_api.h"
vb@28
     6
#include "cryptotech.h"
vb@28
     7
#include "transport.h"
vb@515
     8
#include "blacklist.h"
vb@2880
     9
#include "KeySync_fsm.h"
vb@0
    10
krista@2176
    11
#include <time.h>
krista@2176
    12
#include <stdlib.h>
krista@2487
    13
krista@3539
    14
#define _PEP_SQLITE_DEBUG 0
krista@2488
    15
#if _PEP_SQLITE_DEBUG
krista@2479
    16
#include <sqlite3.h>
krista@2487
    17
#endif
krista@2480
    18
krista@2125
    19
static volatile int init_count = -1;
vb@62
    20
krista@1884
    21
// sql overloaded functions - modified from sqlite3.c
krista@1884
    22
static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
krista@1884
    23
    const char *z2;
vb@3090
    24
    int n;
krista@1884
    25
    z2 = (char*)sqlite3_value_text(argv[0]);
krista@1884
    26
    n = sqlite3_value_bytes(argv[0]);
krista@1884
    27
    /* Verify that the call to _bytes() does not invalidate the _text() pointer */
krista@1884
    28
    assert( z2==(char*)sqlite3_value_text(argv[0]) );
krista@1884
    29
    if( z2 ){
vb@3090
    30
        char *z1 = (char*)sqlite3_malloc(n+1);
krista@1884
    31
        if( z1 ){
vb@3090
    32
            for(int i=0; i<n; i++){
krista@1884
    33
                char c = z2[i];
krista@1884
    34
                char c_mod = c | 0x20;
krista@1884
    35
                if (c_mod < 0x61 || c_mod > 0x7a)
krista@1884
    36
                    c_mod = c;
krista@1884
    37
                z1[i] = c_mod;
krista@1884
    38
            }
krista@1884
    39
            z1[n] = '\0';
krista@1884
    40
            sqlite3_result_text(ctx, z1, n, sqlite3_free);
krista@1884
    41
        }
krista@1884
    42
    }
krista@1884
    43
}
krista@1884
    44
krista@2480
    45
#if _PEP_SQLITE_DEBUG
krista@2479
    46
int sql_trace_callback (unsigned trace_constant, 
krista@2479
    47
                        void* context_ptr,
krista@2479
    48
                        void* P,
krista@2479
    49
                        void* X) {
krista@2479
    50
    switch (trace_constant) {
krista@2479
    51
        case SQLITE_TRACE_STMT:
krista@2479
    52
            fprintf(stderr, "SQL_DEBUG: STMT - ");
krista@2479
    53
            const char* X_str = (const char*) X;
krista@2479
    54
            if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
krista@2479
    55
                fprintf(stderr, "%s\n", X_str);
krista@2479
    56
            else
krista@2479
    57
                fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
krista@2479
    58
            break;
krista@2479
    59
        case SQLITE_TRACE_ROW:
krista@2479
    60
            fprintf(stderr, "SQL_DEBUG: ROW - ");
krista@2479
    61
            fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
krista@2479
    62
            break;            
krista@2479
    63
        case SQLITE_TRACE_CLOSE:
krista@2479
    64
            fprintf(stderr, "SQL_DEBUG: CLOSE - ");
krista@2479
    65
            break;
krista@2479
    66
        default:
krista@2479
    67
            break;
krista@2479
    68
    }
krista@2479
    69
    return 0;
krista@2479
    70
}
krista@2480
    71
#endif
krista@1884
    72
edouard@1518
    73
// sql manipulation statements
edouard@1518
    74
static const char *sql_log = 
edouard@1518
    75
    "insert into log (title, entity, description, comment)"
edouard@1518
    76
     "values (?1, ?2, ?3, ?4);";
edouard@1518
    77
edouard@1518
    78
static const char *sql_trustword = 
edouard@1518
    79
    "select id, word from wordlist where lang = lower(?1) "
edouard@1518
    80
    "and id = ?2 ;";
edouard@1518
    81
krista@3350
    82
// FIXME?: problems if we don't have a key for the user - we get nothing
edouard@1518
    83
static const char *sql_get_identity =  
edouard@1518
    84
    "select fpr, username, comm_type, lang,"
krista@2461
    85
    "   identity.flags | pgp_keypair.flags,"
krista@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 ;";
krista@2787
   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"
vb@456
   971
                "create table if not exists trust (\n"
vb@456
   972
                "   user_id text not null\n"
vb@456
   973
                "       references person (id)\n"
krista@2461
   974
                "       on delete cascade on update cascade,\n"
vb@456
   975
                "   pgp_keypair_fpr text not null\n"
vb@456
   976
                "       references pgp_keypair (fpr)\n"
vb@456
   977
                "       on delete cascade,\n"
vb@456
   978
                "   comm_type integer not null,\n"
Edouard@684
   979
                "   comment text,\n"
Edouard@684
   980
                "   primary key (user_id, pgp_keypair_fpr)\n"
vb@456
   981
                ");\n"
fdik@494
   982
                // blacklist
vb@456
   983
                "create table if not exists blacklist_keys (\n"
vb@456
   984
                "   fpr text primary key\n"
vb@456
   985
                ");\n"
vb@632
   986
                // sequences
vb@632
   987
                "create table if not exists sequences(\n"
vb@632
   988
                "   name text primary key,\n"
vb@2830
   989
                "   value integer default 0\n"
vb@632
   990
                ");\n"
Edouard@693
   991
                "create table if not exists revoked_keys (\n"
Edouard@693
   992
                "   revoked_fpr text primary key,\n"
Edouard@693
   993
                "   replacement_fpr text not null\n"
Edouard@693
   994
                "       references pgp_keypair (fpr)\n"
Edouard@693
   995
                "       on delete cascade,\n"
Edouard@693
   996
                "   revocation_date integer\n"
Edouard@693
   997
                ");\n"
krista@2461
   998
                // user id aliases
krista@2461
   999
                "create table if not exists alternate_user_id (\n"
krista@2478
  1000
                "    default_id text references person (id)\n"
krista@2478
  1001
                "       on delete cascade on update cascade,\n"
krista@2461
  1002
                "    alternate_id text primary key\n"
krista@2461
  1003
                ");\n"
krista@2467
  1004
                // mistrusted keys
krista@2467
  1005
                "create table if not exists mistrusted_keys (\n"
krista@2467
  1006
                "    fpr text primary key\n"
krista@2467
  1007
                ");\n"
krista@2739
  1008
                // social graph for key resets
krista@2739
  1009
                "create table if not exists social_graph (\n"
krista@2747
  1010
                "    own_userid text,\n"
krista@2739
  1011
                "    own_address text,\n"
krista@2752
  1012
                "    contact_userid text,\n"
krista@2752
  1013
                "    CONSTRAINT fk_own_identity\n"
krista@2747
  1014
                "       FOREIGN KEY(own_address, own_userid)\n" 
krista@2747
  1015
                "       REFERENCES identity(address, user_id)\n"
krista@2862
  1016
                "       ON DELETE CASCADE ON UPDATE CASCADE\n"
krista@2752
  1017
                ");\n"
krista@2752
  1018
                // list of user_ids sent revocation
krista@2752
  1019
                "create table if not exists revocation_contact_list (\n"
krista@2752
  1020
                "   fpr text not null references pgp_keypair (fpr)\n"
krista@2752
  1021
                "       on delete cascade,\n"
krista@2752
  1022
                "   contact_id text not null references person (id)\n"
krista@2752
  1023
                "       on delete cascade on update cascade,\n"
krista@2752
  1024
                "   timestamp integer default (datetime('now')),\n"
krista@2752
  1025
                "   PRIMARY KEY(fpr, contact_id)\n"
krista@2739
  1026
                ");\n"
vb@456
  1027
                ,
vb@62
  1028
            NULL,
vb@62
  1029
            NULL,
vb@62
  1030
            NULL
vb@62
  1031
        );
vb@62
  1032
        assert(int_result == SQLITE_OK);
vb@0
  1033
vb@928
  1034
        int version;
vb@62
  1035
        int_result = sqlite3_exec(
vb@62
  1036
            _session->db,
vb@928
  1037
            "pragma user_version;",
vb@928
  1038
            user_version,
vb@928
  1039
            &version,
vb@62
  1040
            NULL
vb@62
  1041
        );
krista@1884
  1042
krista@1884
  1043
        assert(int_result == SQLITE_OK);
krista@1884
  1044
        
krista@1884
  1045
        void (*xFunc_lower)(sqlite3_context*,int,sqlite3_value**) = &_sql_lower;
krista@1884
  1046
        
krista@1884
  1047
        int_result = sqlite3_create_function_v2(
krista@1884
  1048
            _session->db,
krista@1884
  1049
            "lower",
krista@1884
  1050
            1,
krista@1884
  1051
            SQLITE_UTF8 | SQLITE_DETERMINISTIC,
krista@1884
  1052
            NULL,
krista@1884
  1053
            xFunc_lower,
krista@1884
  1054
            NULL,
krista@1884
  1055
            NULL,
krista@1884
  1056
            NULL);
vb@62
  1057
        assert(int_result == SQLITE_OK);
krista@2477
  1058
krista@2477
  1059
        int_result = sqlite3_exec(
krista@2477
  1060
            _session->db,
krista@2477
  1061
            "pragma foreign_keys=ON;\n",
krista@2477
  1062
            NULL,
krista@2477
  1063
            NULL,
krista@2477
  1064
            NULL
krista@2477
  1065
        );
krista@2477
  1066
krista@2477
  1067
        assert(int_result == SQLITE_OK);
krista@2477
  1068
krista@2461
  1069
        
krista@3331
  1070
        // Sometimes the user_version wasn't set correctly. 
krista@2461
  1071
        if (version == 1) {
krista@2461
  1072
            bool version_changed = true;
krista@3331
  1073
            if (db_contains_table(_session, "social_graph") > 0) {
krista@3420
  1074
                if (!table_contains_column(_session, "person", "device_group"))
krista@3420
  1075
                    version = 10;
krista@3420
  1076
                else
krista@3420
  1077
                    version = 9;
krista@3331
  1078
            }            
krista@3331
  1079
            else if (table_contains_column(_session, "identity", "timestamp") > 0) {
krista@2506
  1080
                version = 8;
krista@2506
  1081
            }            
krista@3417
  1082
            else if (table_contains_column(_session, "person", "is_pEp_user") > 0) {
krista@2468
  1083
                version = 7;
krista@2468
  1084
            }            
krista@2468
  1085
            else if (table_contains_column(_session, "identity", "is_own") > 0) {
krista@2461
  1086
                version = 6;
krista@2461
  1087
            }
krista@2461
  1088
            else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
krista@2461
  1089
                version = 2;
krista@2461
  1090
            }
krista@2461
  1091
            else {
krista@2461
  1092
                version_changed = false;
krista@2461
  1093
            }
krista@2461
  1094
            
krista@2461
  1095
            if (version_changed) {
krista@2461
  1096
                // set it in the DB, finally. Yeesh.
krista@2461
  1097
                char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon. 
krista@2461
  1098
                sprintf(verbuf,"%d",version);
krista@2461
  1099
                
krista@2461
  1100
                size_t query_size = strlen(verbuf) + 25;
krista@2461
  1101
                char* query = calloc(query_size, 1);
krista@2461
  1102
                
krista@2461
  1103
                strlcpy(query, "pragma user_version = ", query_size);
krista@2461
  1104
                strlcat(query, verbuf, query_size);
krista@2461
  1105
                strlcat(query, ";", query_size);
krista@2461
  1106
                
krista@2461
  1107
                int_result = sqlite3_exec(
krista@2461
  1108
                    _session->db,
krista@2461
  1109
                    query,
krista@2461
  1110
                    user_version,
krista@2461
  1111
                    &version,
krista@2461
  1112
                    NULL
krista@2461
  1113
                );
krista@2461
  1114
                free(query);
krista@2461
  1115
            }
krista@2461
  1116
        }
krista@2461
  1117
vb@0
  1118
edouard@1604
  1119
        if(version != 0) { 
edouard@1604
  1120
            // Version has been already set
vb@934
  1121
edouard@1604
  1122
            // Early mistake : version 0 shouldn't have existed.
edouard@1604
  1123
            // Numbering should have started at 1 to detect newly created DB.
edouard@1604
  1124
            // Version 0 DBs are not anymore compatible.
krista@1032
  1125
edouard@1604
  1126
            // // Was version 0 compat code.
edouard@1604
  1127
            // if (version < 1) {
edouard@1604
  1128
            //     int_result = sqlite3_exec(
edouard@1604
  1129
            //         _session->db,
edouard@1604
  1130
            //         "alter table identity\n"
edouard@1604
  1131
            //         "   add column flags integer default 0;\n",
edouard@1604
  1132
            //         NULL,
edouard@1604
  1133
            //         NULL,
edouard@1604
  1134
            //         NULL
edouard@1604
  1135
            //     );
edouard@1604
  1136
            //     assert(int_result == SQLITE_OK);
edouard@1604
  1137
            // }
krista@2461
  1138
            
edouard@1604
  1139
            if (version < 2) {
krista@3420
  1140
                // N.B. addition of device_group column removed in DDL v10
edouard@1604
  1141
                int_result = sqlite3_exec(
edouard@1604
  1142
                    _session->db,
edouard@1604
  1143
                    "alter table pgp_keypair\n"
krista@3420
  1144
                    "   add column flags integer default 0;\n",
krista@3420
  1145
                    // "alter table person\n"
krista@3420
  1146
                    // "   add column device_group text;\n",
edouard@1604
  1147
                    NULL,
edouard@1604
  1148
                    NULL,
edouard@1604
  1149
                    NULL
edouard@1604
  1150
                );
edouard@1604
  1151
                assert(int_result == SQLITE_OK);
edouard@1604
  1152
            }
edouard@1604
  1153
edouard@1604
  1154
            if (version < 5) {
edouard@1604
  1155
                int_result = sqlite3_exec(
edouard@1604
  1156
                    _session->db,
edouard@1604
  1157
                    "delete from pgp_keypair where fpr = '';",
edouard@1604
  1158
                    NULL,
edouard@1604
  1159
                    NULL,
edouard@1604
  1160
                    NULL
edouard@1604
  1161
                );
edouard@1604
  1162
                assert(int_result == SQLITE_OK);
edouard@1604
  1163
                int_result = sqlite3_exec(
edouard@1604
  1164
                    _session->db,
edouard@1604
  1165
                    "delete from trust where pgp_keypair_fpr = '';",
edouard@1604
  1166
                    NULL,
edouard@1604
  1167
                    NULL,
edouard@1604
  1168
                    NULL
edouard@1604
  1169
                );
edouard@1604
  1170
                assert(int_result == SQLITE_OK);
edouard@1604
  1171
            }
krista@2461
  1172
            
krista@2461
  1173
            if (version < 6) {
krista@2461
  1174
                int_result = sqlite3_exec(
krista@2461
  1175
                    _session->db,
krista@2461
  1176
                    "alter table identity\n"
krista@2461
  1177
                    "   add column is_own integer default 0;\n",
krista@2461
  1178
                    NULL,
krista@2461
  1179
                    NULL,
krista@2461
  1180
                    NULL
krista@2461
  1181
                );
krista@2461
  1182
                assert(int_result == SQLITE_OK);                
krista@2461
  1183
                int_result = sqlite3_exec(
krista@2461
  1184
                    _session->db,
krista@2461
  1185
                    "update identity\n"
krista@2461
  1186
                    "   set is_own = 1\n"
krista@2461
  1187
                    "   where (user_id = '" PEP_OWN_USERID "');\n",
krista@2461
  1188
                    NULL,
krista@2461
  1189
                    NULL,
krista@2461
  1190
                    NULL
krista@2461
  1191
                );
krista@2461
  1192
                assert(int_result == SQLITE_OK);    
krista@2461
  1193
krista@2461
  1194
                // Turns out that just adding "on update cascade" in
krista@2461
  1195
                // sqlite is a PITA. We need to be able to cascade
krista@2461
  1196
                // person->id replacements (for temp ids like "TOFU_")
krista@2461
  1197
                // so here we go...
krista@2461
  1198
                int_result = sqlite3_exec(
krista@2461
  1199
                    _session->db,
krista@2461
  1200
                    "PRAGMA foreign_keys=off;\n"
krista@2461
  1201
                    "BEGIN TRANSACTION;\n"
krista@3462
  1202
                    "create table _identity_new (\n"
krista@2461
  1203
                    "   address text,\n"
krista@2461
  1204
                    "   user_id text\n"
krista@2461
  1205
                    "       references person (id)\n"
krista@2461
  1206
                    "       on delete cascade on update cascade,\n"
krista@2461
  1207
                    "   main_key_id text\n"
krista@2461
  1208
                    "       references pgp_keypair (fpr)\n"
krista@2461
  1209
                    "       on delete set null,\n"
krista@2461
  1210
                    "   comment text,\n"
krista@2461
  1211
                    "   flags integer default 0,\n"
krista@2461
  1212
                    "   is_own integer default 0,\n"
krista@2461
  1213
                    "   primary key (address, user_id)\n"
krista@3462
  1214
                    ");\n"                    
krista@3462
  1215
                    "INSERT INTO _identity_new SELECT * FROM identity;\n"
krista@3462
  1216
                    "DROP TABLE identity;\n"
krista@3462
  1217
                    "ALTER TABLE _identity_new RENAME TO identity;\n"
krista@3462
  1218
                    "COMMIT;\n"
krista@3462
  1219
                    "\n"
krista@3462
  1220
                    "BEGIN TRANSACTION;\n"
krista@3462
  1221
                    "create table _trust_new (\n"
krista@2461
  1222
                    "   user_id text not null\n"
krista@2461
  1223
                    "       references person (id)\n"
krista@2461
  1224
                    "       on delete cascade on update cascade,\n"
krista@2461
  1225
                    "   pgp_keypair_fpr text not null\n"
krista@2461
  1226
                    "       references pgp_keypair (fpr)\n"
krista@2461
  1227
                    "       on delete cascade,\n"
krista@2461
  1228
                    "   comm_type integer not null,\n"
krista@2461
  1229
                    "   comment text,\n"
krista@2461
  1230
                    "   primary key (user_id, pgp_keypair_fpr)\n"
krista@2461
  1231
                    ");\n"
krista@3462
  1232
                    "INSERT INTO _trust_new SELECT * FROM trust;\n"
krista@3462
  1233
                    "DROP TABLE trust;\n"
krista@3462
  1234
                    "ALTER TABLE _trust_new RENAME TO trust;\n"
krista@2461
  1235
                    "COMMIT;\n"
krista@2461
  1236
                    "\n"
krista@2461
  1237
                    "PRAGMA foreign_keys=on;\n"
krista@2461
  1238
                    "create table if not exists alternate_user_id (\n"
krista@2478
  1239
                    "    default_id text references person (id)\n"
krista@2478
  1240
                    "       on delete cascade on update cascade,\n"
krista@2461
  1241
                    "    alternate_id text primary key\n"
krista@2461
  1242
                    ");\n"
krista@2461
  1243
                    ,
krista@2461
  1244
                    NULL,
krista@2461
  1245
                    NULL,
krista@2461
  1246
                    NULL
krista@2461
  1247
                );
krista@3462
  1248
                assert(int_result == SQLITE_OK);   
krista@3462
  1249
                
krista@3462
  1250
                int_result = sqlite3_exec(
krista@3462
  1251
                    _session->db,
krista@3462
  1252
                    "PRAGMA foreign_key_check;\n"
krista@3462
  1253
                    ,
krista@3462
  1254
                    NULL,
krista@3462
  1255
                    NULL,
krista@3462
  1256
                    NULL
krista@3462
  1257
                );
krista@3462
  1258
                assert(int_result == SQLITE_OK);
krista@3462
  1259
krista@3462
  1260
                // FIXME: foreign key check here 
krista@2461
  1261
            }
krista@2468
  1262
            if (version < 7) {
krista@2468
  1263
                int_result = sqlite3_exec(
krista@2468
  1264
                    _session->db,
krista@2468
  1265
                    "alter table person\n"
vb@2833
  1266
                    "   add column is_pEp_user integer default 0;\n",
krista@2468
  1267
                    NULL,
krista@2468
  1268
                    NULL,
krista@2468
  1269
                    NULL
krista@2468
  1270
                );
krista@2468
  1271
                assert(int_result == SQLITE_OK);
krista@2468
  1272
                int_result = sqlite3_exec(
krista@2468
  1273
                    _session->db,
krista@2468
  1274
                    "update person\n"
vb@2833
  1275
                    "   set is_pEp_user = 1\n"
krista@2468
  1276
                    "   where id = "
krista@2468
  1277
                    "       (select distinct id from person "
krista@2468
  1278
                    "               join trust on id = user_id "
krista@2468
  1279
                    "               where (case when (comm_type = 127) then (id) "
krista@2468
  1280
                    "                           when (comm_type = 255) then (id) "
krista@2468
  1281
                    "                           else 0"
krista@2468
  1282
                    "                      end) = id );\n",
krista@2468
  1283
                    NULL,
krista@2468
  1284
                    NULL,
krista@2468
  1285
                    NULL
krista@2468
  1286
                );
krista@2473
  1287
                assert(int_result == SQLITE_OK);
krista@2473
  1288
                
krista@2473
  1289
                int_result = sqlite3_exec(
krista@2473
  1290
                    _session->db,
krista@2471
  1291
                    "create table if not exists mistrusted_keys (\n"
krista@2471
  1292
                    "    fpr text primary key\n"
krista@2471
  1293
                    ");\n",            
krista@2471
  1294
                    NULL,
krista@2471
  1295
                    NULL,
krista@2471
  1296
                    NULL
krista@2471
  1297
                );
krista@2468
  1298
                assert(int_result == SQLITE_OK);    
krista@2468
  1299
            }
krista@2506
  1300
            if (version < 8) {
krista@2506
  1301
                int_result = sqlite3_exec(
krista@2506
  1302
                    _session->db,
krista@2509
  1303
                    "PRAGMA foreign_keys=off;\n"
krista@2509
  1304
                    "BEGIN TRANSACTION;\n"
krista@3462
  1305
                    "create table _identity_new (\n"
krista@2509
  1306
                    "   address text,\n"
krista@2509
  1307
                    "   user_id text\n"
krista@2509
  1308
                    "       references person (id)\n"
krista@2509
  1309
                    "       on delete cascade on update cascade,\n"
krista@2509
  1310
                    "   main_key_id text\n"
krista@2509
  1311
                    "       references pgp_keypair (fpr)\n"
krista@2509
  1312
                    "       on delete set null,\n"
krista@2509
  1313
                    "   comment text,\n"
krista@2509
  1314
                    "   flags integer default 0,\n"
krista@2509
  1315
                    "   is_own integer default 0,\n"
krista@2509
  1316
                    "   timestamp integer default (datetime('now')),\n"
krista@2509
  1317
                    "   primary key (address, user_id)\n"
krista@2509
  1318
                    ");\n"
krista@3462
  1319
                    "INSERT INTO _identity_new (address, user_id, main_key_id, "
krista@2514
  1320
                    "                      comment, flags, is_own) "
krista@3463
  1321
                    "   SELECT identity.address, identity.user_id, "
krista@3463
  1322
                    "          identity.main_key_id, identity.comment, "
krista@3463
  1323
                    "          identity.flags, identity.is_own "
krista@3462
  1324
                    "   FROM identity "
krista@2514
  1325
                    "   WHERE 1;\n"
krista@3462
  1326
                    "DROP TABLE identity;\n"
krista@3462
  1327
                    "ALTER TABLE _identity_new RENAME TO identity;\n"
krista@2509
  1328
                    "COMMIT;\n"
krista@2509
  1329
                    "\n"
krista@2509
  1330
                    "PRAGMA foreign_keys=on;\n"
krista@2509
  1331
                    ,
krista@2506
  1332
                    NULL,
krista@2506
  1333
                    NULL,
krista@2506
  1334
                    NULL
krista@2506
  1335
                );
krista@2509
  1336
                assert(int_result == SQLITE_OK);    
krista@3462
  1337
                
krista@3462
  1338
                int_result = sqlite3_exec(
krista@3462
  1339
                    _session->db,
krista@3462
  1340
                    "PRAGMA foreign_key_check;\n"
krista@3462
  1341
                    ,
krista@3462
  1342
                    NULL,
krista@3462
  1343
                    NULL,
krista@3462
  1344
                    NULL
krista@3462
  1345
                );
krista@3462
  1346
                assert(int_result == SQLITE_OK);
krista@3462
  1347
krista@3462
  1348
                // FIXME: foreign key check
krista@2506
  1349
            }
krista@2747
  1350
            if (version < 9) {
krista@2747
  1351
                int_result = sqlite3_exec(
krista@2747
  1352
                    _session->db,
krista@2747
  1353
                    "create table if not exists social_graph (\n"
krista@2747
  1354
                    "    own_userid text,\n"
krista@2747
  1355
                    "    own_address text,\n"
krista@2752
  1356
                    "    contact_userid text,\n"
krista@2752
  1357
                    "    CONSTRAINT fk_own_identity\n"
krista@2747
  1358
                    "       FOREIGN KEY(own_address, own_userid)\n" 
krista@2747
  1359
                    "       REFERENCES identity(address, user_id)\n"
krista@3331
  1360
                    "       ON DELETE CASCADE ON UPDATE CASCADE\n"
krista@2752
  1361
                    ");\n"
krista@2752
  1362
                    "create table if not exists revocation_contact_list (\n"
krista@2752
  1363
                    "   fpr text not null references pgp_keypair (fpr)\n"
krista@2752
  1364
                    "       on delete cascade,\n"
krista@2752
  1365
                    "   contact_id text not null references person (id)\n"
krista@2752
  1366
                    "       on delete cascade on update cascade,\n"
krista@2752
  1367
                    "   timestamp integer default (datetime('now')),\n"
krista@2752
  1368
                    "   PRIMARY KEY(fpr, contact_id)\n"
krista@2747
  1369
                    ");\n"
krista@2747
  1370
                    ,
krista@2747
  1371
                    NULL,
krista@2747
  1372
                    NULL,
krista@2747
  1373
                    NULL
krista@2747
  1374
                );
krista@2747
  1375
                assert(int_result == SQLITE_OK);    
krista@2747
  1376
            }
krista@3420
  1377
            if (version < 10 && version > 1) {
krista@3420
  1378
                int_result = sqlite3_exec(
krista@3460
  1379
                    _session->db,
krista@3420
  1380
                    "PRAGMA foreign_keys=off;\n"
krista@3420
  1381
                    "BEGIN TRANSACTION;\n"
krista@3462
  1382
                    "create table _person_new (\n"
krista@3420
  1383
                    "   id text primary key,\n"
krista@3420
  1384
                    "   username text not null,\n"
krista@3420
  1385
                    "   main_key_id text\n"
krista@3420
  1386
                    "       references pgp_keypair (fpr)\n"
krista@3420
  1387
                    "       on delete set null,\n"
krista@3420
  1388
                    "   lang text,\n"
krista@3420
  1389
                    "   comment text,\n"
krista@3420
  1390
                    "   is_pEp_user integer default 0\n"
krista@3420
  1391
                    ");\n"
krista@3462
  1392
                    "INSERT INTO _person_new (id, username, main_key_id, "
krista@3420
  1393
                    "                    lang, comment, is_pEp_user) "
krista@3462
  1394
                    "   SELECT person.id, person.username, "
krista@3462
  1395
                    "          person.main_key_id, person.lang, "
krista@3462
  1396
                    "          person.comment, person.is_pEp_user "
krista@3462
  1397
                    "   FROM person "
krista@3420
  1398
                    "   WHERE 1;\n"
krista@3462
  1399
                    "DROP TABLE person;\n"
krista@3462
  1400
                    "ALTER TABLE _person_new RENAME TO person;\n"
krista@3420
  1401
                    "COMMIT;\n"
krista@3420
  1402
                    "\n"
krista@3420
  1403
                    "PRAGMA foreign_keys=on;\n"
krista@3420
  1404
                    ,
krista@3420
  1405
                    NULL,
krista@3420
  1406
                    NULL,
krista@3420
  1407
                    NULL
krista@3420
  1408
                );
krista@3420
  1409
                assert(int_result == SQLITE_OK);    
krista@3462
  1410
                int_result = sqlite3_exec(
krista@3462
  1411
                    _session->db,
krista@3462
  1412
                    "PRAGMA foreign_key_check;\n"
krista@3462
  1413
                    ,
krista@3462
  1414
                    NULL,
krista@3462
  1415
                    NULL,
krista@3462
  1416
                    NULL
krista@3462
  1417
                );
krista@3462
  1418
                assert(int_result == SQLITE_OK);
krista@3420
  1419
            }
krista@3463
  1420
            if (version < 11) {
krista@3463
  1421
                status = repair_altered_tables(_session);
krista@3463
  1422
                assert(status == PEP_STATUS_OK);
krista@3463
  1423
                if (status != PEP_STATUS_OK)
krista@3463
  1424
                    return status;
krista@3463
  1425
            }
krista@2468
  1426
        }        
edouard@1752
  1427
        else { 
edouard@1752
  1428
            // Version from DB was 0, it means this is initial setup.
edouard@1752
  1429
            // DB has just been created, and all tables are empty.
edouard@1752
  1430
            very_first = true;
edouard@1752
  1431
        }
vb@1453
  1432
vb@928
  1433
        if (version < atoi(_DDL_USER_VERSION)) {
vb@928
  1434
            int_result = sqlite3_exec(
vb@928
  1435
                _session->db,
krista@2787
  1436
                "pragma user_version = "_DDL_USER_VERSION";\n"
krista@2787
  1437
                "insert or replace into version_info (id, version)"
krista@2787
  1438
                    "values (1, '" PEP_ENGINE_VERSION "');",
vb@928
  1439
                NULL,
vb@928
  1440
                NULL,
vb@928
  1441
                NULL
vb@928
  1442
            );
vb@928
  1443
            assert(int_result == SQLITE_OK);
vb@928
  1444
        }
krista@2176
  1445
        
krista@2176
  1446
        // We need to init a few globals for message id that we'd rather not
krista@2176
  1447
        // calculate more than once.
krista@2176
  1448
        _init_globals();
vb@62
  1449
    }
vb@62
  1450
vb@951
  1451
    int_result = sqlite3_prepare_v2(_session->db, sql_log,
vb@951
  1452
            (int)strlen(sql_log), &_session->log, NULL);
roker@529
  1453
    assert(int_result == SQLITE_OK);
vb@0
  1454
vb@233
  1455
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
  1456
            (int)strlen(sql_trustword), &_session->trustword, NULL);
roker@529
  1457
    assert(int_result == SQLITE_OK);
vb@0
  1458
vb@0
  1459
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
  1460
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
roker@529
  1461
    assert(int_result == SQLITE_OK);
vb@0
  1462
krista@2461
  1463
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_trust_check,
krista@2461
  1464
            (int)strlen(sql_get_identity_without_trust_check), 
krista@2461
  1465
            &_session->get_identity_without_trust_check, NULL);
krista@2461
  1466
    assert(int_result == SQLITE_OK);
krista@2461
  1467
krista@2461
  1468
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_address,
krista@2461
  1469
            (int)strlen(sql_get_identities_by_address), 
krista@2461
  1470
            &_session->get_identities_by_address, NULL);
krista@2461
  1471
    assert(int_result == SQLITE_OK);
krista@2966
  1472
    
krista@2966
  1473
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
krista@2966
  1474
            (int)strlen(sql_get_identities_by_userid), 
krista@2966
  1475
            &_session->get_identities_by_userid, NULL);
krista@2966
  1476
    assert(int_result == SQLITE_OK);
krista@2461
  1477
krista@2893
  1478
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_main_key_id,
krista@2893
  1479
            (int)strlen(sql_get_identities_by_main_key_id), 
krista@2893
  1480
            &_session->get_identities_by_main_key_id, NULL);
krista@2893
  1481
    assert(int_result == SQLITE_OK);
krista@2893
  1482
krista@2461
  1483
    int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
krista@2461
  1484
            (int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
krista@2461
  1485
    assert(int_result == SQLITE_OK);
krista@2461
  1486
krista@2893
  1487
    int_result = sqlite3_prepare_v2(_session->db, sql_get_all_keys_for_user,
krista@2893
  1488
            (int)strlen(sql_get_all_keys_for_user), &_session->get_all_keys_for_user, NULL);
krista@2893
  1489
    assert(int_result == SQLITE_OK);
krista@2893
  1490
krista@2461
  1491
    int_result = sqlite3_prepare_v2(_session->db, sql_get_default_own_userid,
krista@2461
  1492
            (int)strlen(sql_get_default_own_userid), &_session->get_default_own_userid, NULL);
krista@2461
  1493
    assert(int_result == SQLITE_OK);
krista@2461
  1494
    
krista@2461
  1495
    int_result = sqlite3_prepare_v2(_session->db, sql_get_userid_alias_default,
krista@2461
  1496
            (int)strlen(sql_get_userid_alias_default), &_session->get_userid_alias_default, NULL);
krista@2461
  1497
    assert(int_result == SQLITE_OK);
krista@2461
  1498
krista@2461
  1499
    int_result = sqlite3_prepare_v2(_session->db, sql_add_userid_alias,
krista@2461
  1500
            (int)strlen(sql_add_userid_alias), &_session->add_userid_alias, NULL);
krista@2461
  1501
    assert(int_result == SQLITE_OK);
krista@2461
  1502
krista@2461
  1503
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_userid,
krista@2461
  1504
            (int)strlen(sql_replace_userid), &_session->replace_userid, NULL);
krista@2461
  1505
    assert(int_result == SQLITE_OK);
krista@2461
  1506
krista@2948
  1507
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_key,
krista@2948
  1508
            (int)strlen(sql_delete_key), &_session->delete_key, NULL);
krista@2948
  1509
    assert(int_result == SQLITE_OK);
krista@2948
  1510
krista@2461
  1511
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr,
krista@2461
  1512
            (int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
krista@2461
  1513
    assert(int_result == SQLITE_OK);
krista@2461
  1514
krista@2461
  1515
    int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
krista@2461
  1516
            (int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
krista@2461
  1517
    assert(int_result == SQLITE_OK);
krista@2461
  1518
krista@2461
  1519
    int_result = sqlite3_prepare_v2(_session->db, sql_refresh_userid_default_key,
krista@2461
  1520
            (int)strlen(sql_refresh_userid_default_key), &_session->refresh_userid_default_key, NULL);
krista@2461
  1521
    assert(int_result == SQLITE_OK);
krista@2461
  1522
krista@1799
  1523
    int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
krista@1799
  1524
            (int)strlen(sql_replace_identities_fpr), 
krista@1799
  1525
            &_session->replace_identities_fpr, NULL);
krista@1793
  1526
    assert(int_result == SQLITE_OK);
krista@2461
  1527
    
krista@2461
  1528
    int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
krista@2461
  1529
            (int)strlen(sql_remove_fpr_as_default), 
krista@2461
  1530
            &_session->remove_fpr_as_default, NULL);
krista@2461
  1531
    assert(int_result == SQLITE_OK);
krista@1793
  1532
vb@0
  1533
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
  1534
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
  1535
    assert(int_result == SQLITE_OK);
vb@62
  1536
krista@2478
  1537
    int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
krista@2478
  1538
            (int)strlen(sql_update_person), &_session->update_person, NULL);
krista@2478
  1539
    assert(int_result == SQLITE_OK);
krista@2478
  1540
krista@2965
  1541
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_person,
krista@2965
  1542
            (int)strlen(sql_delete_person), &_session->delete_person, NULL);
krista@2965
  1543
    assert(int_result == SQLITE_OK);
krista@2965
  1544
krista@2480
  1545
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_person,
krista@2480
  1546
            (int)strlen(sql_exists_person), &_session->exists_person, NULL);
krista@2480
  1547
    assert(int_result == SQLITE_OK);
krista@2480
  1548
vb@2833
  1549
    int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pEp_user,
vb@2833
  1550
            (int)strlen(sql_set_as_pEp_user), &_session->set_as_pEp_user, NULL);
krista@2468
  1551
    assert(int_result == SQLITE_OK);
krista@2468
  1552
    
vb@2833
  1553
    int_result = sqlite3_prepare_v2(_session->db, sql_is_pEp_user,
vb@2833
  1554
            (int)strlen(sql_is_pEp_user), &_session->is_pEp_user, NULL);
krista@2787
  1555
    assert(int_result == SQLITE_OK);
krista@2787
  1556
krista@2747
  1557
    int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
krista@2747
  1558
            (int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
krista@2747
  1559
    assert(int_result == SQLITE_OK);
krista@2747
  1560
krista@2752
  1561
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1562
            sql_get_own_address_binding_from_contact,
krista@2752
  1563
            (int)strlen(sql_get_own_address_binding_from_contact), 
krista@2752
  1564
            &_session->get_own_address_binding_from_contact, NULL);
krista@2752
  1565
    assert(int_result == SQLITE_OK);
krista@2752
  1566
krista@2752
  1567
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1568
            sql_set_revoke_contact_as_notified,
krista@2752
  1569
            (int)strlen(sql_set_revoke_contact_as_notified), 
krista@2752
  1570
            &_session->set_revoke_contact_as_notified, NULL);
krista@2752
  1571
    assert(int_result == SQLITE_OK);
krista@2752
  1572
krista@2752
  1573
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1574
            sql_get_contacted_ids_from_revoke_fpr,
krista@2752
  1575
            (int)strlen(sql_get_contacted_ids_from_revoke_fpr), 
krista@2752
  1576
            &_session->get_contacted_ids_from_revoke_fpr, NULL);
krista@2752
  1577
    assert(int_result == SQLITE_OK);
krista@2752
  1578
krista@2752
  1579
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2800
  1580
            sql_was_id_for_revoke_contacted,
krista@2800
  1581
            (int)strlen(sql_was_id_for_revoke_contacted), 
krista@2800
  1582
            &_session->was_id_for_revoke_contacted, NULL);
krista@2800
  1583
    assert(int_result == SQLITE_OK);
krista@2800
  1584
krista@2800
  1585
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2800
  1586
            sql_get_last_contacted,
krista@2800
  1587
            (int)strlen(sql_get_last_contacted), 
krista@2800
  1588
            &_session->get_last_contacted, NULL);
krista@2800
  1589
    assert(int_result == SQLITE_OK);
krista@2800
  1590
krista@2800
  1591
    int_result = sqlite3_prepare_v2(_session->db, 
krista@2752
  1592
            sql_get_own_address_binding_from_contact,
krista@2752
  1593
            (int)strlen(sql_get_own_address_binding_from_contact), 
krista@2752
  1594
            &_session->get_own_address_binding_from_contact, NULL);
krista@2468
  1595
    assert(int_result == SQLITE_OK);
krista@2468
  1596
vb@0
  1597
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
vb@951
  1598
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
vb@951
  1599
            NULL);
roker@529
  1600
    assert(int_result == SQLITE_OK);
vb@62
  1601
krista@2478
  1602
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
krista@2478
  1603
            (int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
krista@2478
  1604
    assert(int_result == SQLITE_OK);
krista@2478
  1605
krista@2478
  1606
    int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
krista@2478
  1607
            (int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
roker@529
  1608
    assert(int_result == SQLITE_OK);
vb@62
  1609
krista@2480
  1610
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_identity_entry,
krista@2480
  1611
            (int)strlen(sql_exists_identity_entry), &_session->exists_identity_entry, NULL);
krista@2480
  1612
    assert(int_result == SQLITE_OK);
krista@2480
  1613
vb@932
  1614
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
vb@951
  1615
            (int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
vb@951
  1616
            NULL);
vb@932
  1617
    assert(int_result == SQLITE_OK);
vb@932
  1618
edouard@1394
  1619
    int_result = sqlite3_prepare_v2(_session->db, sql_unset_identity_flags,
edouard@1394
  1620
            (int)strlen(sql_unset_identity_flags), &_session->unset_identity_flags,
edouard@1394
  1621
            NULL);
edouard@1394
  1622
    assert(int_result == SQLITE_OK);
edouard@1394
  1623
vb@0
  1624
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
  1625
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
roker@529
  1626
    assert(int_result == SQLITE_OK);
vb@62
  1627
krista@2478
  1628
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
krista@2478
  1629
            (int)strlen(sql_update_trust), &_session->update_trust, NULL);
krista@2478
  1630
    assert(int_result == SQLITE_OK);
krista@2478
  1631
vb@2834
  1632
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_to_pEp,
vb@2834
  1633
            (int)strlen(sql_update_trust_to_pEp), &_session->update_trust_to_pEp, NULL);
krista@2543
  1634
    assert(int_result == SQLITE_OK);
krista@2543
  1635
krista@2480
  1636
    int_result = sqlite3_prepare_v2(_session->db, sql_exists_trust_entry,
krista@2480
  1637
                 (int)strlen(sql_exists_trust_entry), &_session->exists_trust_entry, NULL);
krista@2480
  1638
    assert(int_result == SQLITE_OK);
krista@2480
  1639
krista@1799
  1640
    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
krista@1799
  1641
            (int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
krista@1799
  1642
    assert(int_result == SQLITE_OK);
krista@1799
  1643
vb@8
  1644
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
  1645
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
  1646
    assert(int_result == SQLITE_OK);
vb@0
  1647
krista@2967
  1648
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust_by_userid,
krista@2967
  1649
            (int)strlen(sql_get_trust_by_userid), &_session->get_trust_by_userid, NULL);
krista@2967
  1650
    assert(int_result == SQLITE_OK);
krista@2967
  1651
vb@251
  1652
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
  1653
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
  1654
    assert(int_result == SQLITE_OK);
vb@251
  1655
krista@2593
  1656
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromised,
krista@2593
  1657
            (int)strlen(sql_mark_as_compromised), &_session->mark_compromised,
vb@951
  1658
            NULL);
vb@357
  1659
    assert(int_result == SQLITE_OK);
vb@357
  1660
vb@453
  1661
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
  1662
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
  1663
    assert(int_result == SQLITE_OK);
vb@453
  1664
vb@458
  1665
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
  1666
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
roker@529
  1667
    assert(int_result == SQLITE_OK);
vb@458
  1668
vb@458
  1669
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
  1670
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
roker@529
  1671
    assert(int_result == SQLITE_OK);
krista@2461
  1672
    
fdik@494
  1673
    // blacklist
fdik@494
  1674
fdik@494
  1675
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
  1676
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
  1677
    assert(int_result == SQLITE_OK);
fdik@494
  1678
fdik@494
  1679
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
vb@951
  1680
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
vb@951
  1681
            NULL);
fdik@494
  1682
    assert(int_result == SQLITE_OK);
fdik@494
  1683
fdik@494
  1684
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
vb@951
  1685
            (int)strlen(sql_blacklist_is_listed),
vb@951
  1686
            &_session->blacklist_is_listed, NULL);
fdik@494
  1687
    assert(int_result == SQLITE_OK);
fdik@494
  1688
fdik@494
  1689
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
vb@951
  1690
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
vb@951
  1691
            NULL);
vb@482
  1692
    assert(int_result == SQLITE_OK);
krista@1275
  1693
    
Edouard@584
  1694
    // Own keys
Edouard@584
  1695
    
Edouard@584
  1696
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
vb@951
  1697
            (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
vb@951
  1698
            NULL);
Edouard@584
  1699
    assert(int_result == SQLITE_OK);
krista@3346
  1700
krista@3346
  1701
    int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
krista@3346
  1702
            (int)strlen(sql_is_own_address), &_session->is_own_address,
krista@3346
  1703
            NULL);
krista@3346
  1704
    assert(int_result == SQLITE_OK);
Edouard@584
  1705
    
vb@955
  1706
    int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
vb@955
  1707
            (int)strlen(sql_own_identities_retrieve),
vb@955
  1708
            &_session->own_identities_retrieve, NULL);
Edouard@584
  1709
    assert(int_result == SQLITE_OK);
vb@633
  1710
 
edouard@1394
  1711
    int_result = sqlite3_prepare_v2(_session->db, sql_own_keys_retrieve,
edouard@1394
  1712
            (int)strlen(sql_own_keys_retrieve),
edouard@1394
  1713
            &_session->own_keys_retrieve, NULL);
edouard@1394
  1714
    assert(int_result == SQLITE_OK);
edouard@1394
  1715
 
krista@2461
  1716
    // int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
krista@2461
  1717
    //         (int)strlen(sql_set_own_key),
krista@2461
  1718
    //         &_session->set_own_key, NULL);
krista@2461
  1719
    // assert(int_result == SQLITE_OK);
edouard@1370
  1720
 
vb@633
  1721
    // Sequence
vb@633
  1722
vb@633
  1723
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
vb@951
  1724
            (int)strlen(sql_sequence_value1), &_session->sequence_value1,
vb@951
  1725
            NULL);
vb@633
  1726
    assert(int_result == SQLITE_OK);
vb@633
  1727
vb@633
  1728
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
vb@951
  1729
            (int)strlen(sql_sequence_value2), &_session->sequence_value2,
vb@951
  1730
            NULL);
krista@1032
  1731
    assert(int_result == SQLITE_OK);
krista@1032
  1732
Edouard@693
  1733
    // Revocation tracking
Edouard@693
  1734
    
Edouard@693
  1735
    int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
vb@951
  1736
            (int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
Edouard@693
  1737
    assert(int_result == SQLITE_OK);
Edouard@693
  1738
    
Edouard@693
  1739
    int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
vb@951
  1740
            (int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
Edouard@693
  1741
    assert(int_result == SQLITE_OK);
Edouard@693
  1742
    
krista@2752
  1743
    int_result = sqlite3_prepare_v2(_session->db, sql_get_replacement_fpr,
krista@2752
  1744
            (int)strlen(sql_get_replacement_fpr), &_session->get_replacement_fpr, NULL);
krista@2752
  1745
    assert(int_result == SQLITE_OK);
krista@2752
  1746
krista@2471
  1747
    int_result = sqlite3_prepare_v2(_session->db, sql_add_mistrusted_key,
krista@2471
  1748
            (int)strlen(sql_add_mistrusted_key), &_session->add_mistrusted_key, NULL);
krista@2471
  1749
    assert(int_result == SQLITE_OK);
krista@2471
  1750
krista@2471
  1751
    int_result = sqlite3_prepare_v2(_session->db, sql_delete_mistrusted_key,
krista@2471
  1752
            (int)strlen(sql_delete_mistrusted_key), &_session->delete_mistrusted_key, NULL);
krista@2471
  1753
    assert(int_result == SQLITE_OK);
krista@2471
  1754
krista@2471
  1755
    int_result = sqlite3_prepare_v2(_session->db, sql_is_mistrusted_key,
krista@2471
  1756
            (int)strlen(sql_is_mistrusted_key), &_session->is_mistrusted_key, NULL);
krista@2471
  1757
    assert(int_result == SQLITE_OK);
krista@2471
  1758
    
vb@65
  1759
    status = init_cryptotech(_session, in_first);
vb@65
  1760
    if (status != PEP_STATUS_OK)
vb@2834
  1761
        goto pEp_error;
vb@0
  1762
vb@65
  1763
    status = init_transport_system(_session, in_first);
vb@65
  1764
    if (status != PEP_STATUS_OK)
vb@2834
  1765
        goto pEp_error;
vb@62
  1766
vb@65
  1767
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
  1768
    if (status != PEP_STATUS_OK)
vb@2834
  1769
        goto pEp_error;
vb@65
  1770
vb@464
  1771
    // runtime config
vb@464
  1772
edouard@1752
  1773
    if (very_first)
edouard@1752
  1774
    {
vb@2539
  1775
#ifdef USE_GPG
edouard@1752
  1776
        // On first run, all private keys already present in PGP keyring 
edouard@1752
  1777
        // are taken as own in order to seamlessly integrate with
edouard@1752
  1778
        // pre-existing GPG setup.
edouard@1752
  1779
krista@2458
  1780
        // Note: earlier fears about danger because of DB reinitialisation should
krista@2458
  1781
        // be a non-issue here, as we ONLY take the ultimately trusted keys now.
krista@2458
  1782
        // Thus, unless the user has assigned ultimate trust through PGP, there is
krista@2458
  1783
        // no chance of automatically imported pEp keys from a previous run making
krista@2458
  1784
        // their way into PEP trusted status without explicit action (Bare imported
krista@2458
  1785
        // private keys have an 'unknown' trust designation in PGP).
krista@2458
  1786
krista@2458
  1787
        // We don't really worry about the status here.
vb@2539
  1788
        status = pgp_import_ultimately_trusted_keypairs(_session);        
vb@2539
  1789
#endif // USE_GPG
edouard@1752
  1790
    }
vb@464
  1791
roker@529
  1792
    *session = _session;
krista@2176
  1793
    
krista@2176
  1794
    // Note: Following statement is NOT for any cryptographic/secure functionality; it is
krista@2176
  1795
    //       ONLY used for some randomness in generated outer message ID, which are
krista@2176
  1796
    //       required by the RFC to be globally unique!
vb@2571
  1797
    srand((unsigned int) time(NULL));
krista@2176
  1798
    
roker@529
  1799
    return PEP_STATUS_OK;
vb@65
  1800
vb@65
  1801
enomem:
vb@65
  1802
    status = PEP_OUT_OF_MEMORY;
vb@65
  1803
vb@2834
  1804
pEp_error:
vb@65
  1805
    release(_session);
vb@65
  1806
    return status;
vb@0
  1807
}
vb@0
  1808
vb@0
  1809
DYNAMIC_API void release(PEP_SESSION session)
vb@0
  1810
{
vb@62
  1811
    bool out_last = false;
krista@2124
  1812
    int _count = --init_count;
edouard@2112
  1813
    
edouard@2112
  1814
    assert(_count >= -1);
roker@529
  1815
    assert(session);
vb@0
  1816
edouard@2112
  1817
    if (!((_count >= -1) && session))
vb@191
  1818
        return;
vb@191
  1819
vb@62
  1820
    // a small race condition but still a race condition
vb@113
  1821
    // mitigated by calling caveat (see documentation)
krista@2125
  1822
    // (release() is to be guarded by a mutex by the caller)
edouard@2112
  1823
    if (_count == -1)
vb@62
  1824
        out_last = true;
vb@62
  1825
roker@529
  1826
    if (session) {
vb@3391
  1827
        free_Sync_state(session);
vb@986
  1828
roker@529
  1829
        if (session->db) {
vb@517
  1830
            if (session->log)
vb@517
  1831
                sqlite3_finalize(session->log);
vb@233
  1832
            if (session->trustword)
vb@233
  1833
                sqlite3_finalize(session->trustword);
vb@65
  1834
            if (session->get_identity)
vb@65
  1835
                sqlite3_finalize(session->get_identity);
krista@2461
  1836
            if (session->get_identity_without_trust_check)
krista@2461
  1837
                sqlite3_finalize(session->get_identity_without_trust_check);
krista@2461
  1838
            if (session->get_identities_by_address)
krista@2461
  1839
                sqlite3_finalize(session->get_identities_by_address);            
krista@2966
  1840
            if (session->get_identities_by_userid)
krista@2893
  1841
                sqlite3_finalize(session->get_identities_by_userid);                
krista@2893
  1842
            if (session->get_identities_by_main_key_id)
krista@2893
  1843
                sqlite3_finalize(session->get_identities_by_main_key_id);                                
krista@2461
  1844
            if (session->get_user_default_key)
krista@2893
  1845
                sqlite3_finalize(session->get_user_default_key);
krista@2893
  1846
            if (session->get_all_keys_for_user)
krista@2893
  1847
                sqlite3_finalize(session->get_all_keys_for_user);                        
krista@2461
  1848
            if (session->get_default_own_userid)
krista@2461
  1849
                sqlite3_finalize(session->get_default_own_userid);
krista@2461
  1850
            if (session->get_userid_alias_default)
krista@2461
  1851
                sqlite3_finalize(session->get_userid_alias_default);
krista@2461
  1852
            if (session->add_userid_alias)
krista@2461
  1853
                sqlite3_finalize(session->add_userid_alias);
krista@1799
  1854
            if (session->replace_identities_fpr)
krista@1799
  1855
                sqlite3_finalize(session->replace_identities_fpr);        
krista@2461
  1856
            if (session->remove_fpr_as_default)
krista@2461
  1857
                sqlite3_finalize(session->remove_fpr_as_default);            
vb@65
  1858
            if (session->set_person)
vb@65
  1859
                sqlite3_finalize(session->set_person);
krista@2965
  1860
            if (session->delete_person)
krista@2965
  1861
                sqlite3_finalize(session->delete_person);                
vb@2833
  1862
            if (session->set_as_pEp_user)
vb@2833
  1863
                sqlite3_finalize(session->set_as_pEp_user);
vb@2833
  1864
            if (session->is_pEp_user)
vb@2833
  1865
                sqlite3_finalize(session->is_pEp_user);
krista@2468
  1866
            if (session->exists_person)
krista@2747
  1867
                sqlite3_finalize(session->exists_person);
krista@2747
  1868
            if (session->add_into_social_graph)
krista@2752
  1869
                sqlite3_finalize(session->add_into_social_graph);  
krista@2752
  1870
            if (session->get_own_address_binding_from_contact)
krista@2752
  1871
                sqlite3_finalize(session->get_own_address_binding_from_contact);  
krista@2752
  1872
            if (session->set_revoke_contact_as_notified)
krista@2752
  1873
                sqlite3_finalize(session->set_revoke_contact_as_notified);  
krista@2752
  1874
            if (session->get_contacted_ids_from_revoke_fpr)
krista@2752
  1875
                sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);  
krista@2752
  1876
            if (session->was_id_for_revoke_contacted)
krista@2800
  1877
                sqlite3_finalize(session->was_id_for_revoke_contacted);   
krista@2800
  1878
            if (session->get_last_contacted)
krista@2800
  1879
                sqlite3_finalize(session->get_last_contacted);                                       
vb@65
  1880
            if (session->set_pgp_keypair)
vb@65
  1881
                sqlite3_finalize(session->set_pgp_keypair);
krista@2480
  1882
            if (session->exists_identity_entry)
krista@2480
  1883
                sqlite3_finalize(session->exists_identity_entry);                
krista@2478
  1884
            if (session->set_identity_entry)
krista@2478
  1885
                sqlite3_finalize(session->set_identity_entry);
krista@2478
  1886
            if (session->update_identity_entry)
krista@2478
  1887
                sqlite3_finalize(session->update_identity_entry);    
vb@932
  1888
            if (session->set_identity_flags)
vb@932
  1889
                sqlite3_finalize(session->set_identity_flags);
edouard@1394
  1890
            if (session->unset_identity_flags)
edouard@1394
  1891
                sqlite3_finalize(session->unset_identity_flags);
krista@2480
  1892
            if (session->exists_trust_entry)
krista@2480
  1893
                sqlite3_finalize(session->exists_trust_entry);                                
vb@65
  1894
            if (session->set_trust)
vb@65
  1895
                sqlite3_finalize(session->set_trust);
krista@2478
  1896
            if (session->update_trust)
krista@2543
  1897
                sqlite3_finalize(session->update_trust);
vb@2834
  1898
            if (session->update_trust_to_pEp)
vb@2834
  1899
                sqlite3_finalize(session->update_trust_to_pEp);                                                
krista@1799
  1900
            if (session->update_trust_for_fpr)
krista@1799
  1901
                sqlite3_finalize(session->update_trust_for_fpr);
vb@65
  1902
            if (session->get_trust)
vb@65
  1903
                sqlite3_finalize(session->get_trust);
krista@2967
  1904
            if (session->get_trust_by_userid)
krista@2967
  1905
                sqlite3_finalize(session->get_trust_by_userid);                
vb@251
  1906
            if (session->least_trust)
vb@251
  1907
                sqlite3_finalize(session->least_trust);
krista@2593
  1908
            if (session->mark_compromised)
krista@2593
  1909
                sqlite3_finalize(session->mark_compromised);
vb@517
  1910
            if (session->crashdump)
vb@517
  1911
                sqlite3_finalize(session->crashdump);
vb@517
  1912
            if (session->languagelist)
vb@517
  1913
                sqlite3_finalize(session->languagelist);
vb@517
  1914
            if (session->i18n_token)
vb@517
  1915
                sqlite3_finalize(session->i18n_token);
krista@2461
  1916
            if (session->replace_userid)
krista@2461
  1917
                sqlite3_finalize(session->replace_userid);
krista@2948
  1918
            if (session->delete_key)
krista@2948
  1919
                sqlite3_finalize(session->delete_key);                
krista@2461
  1920
            if (session->replace_main_user_fpr)
krista@2461
  1921
                sqlite3_finalize(session->replace_main_user_fpr);                
krista@2461
  1922
            if (session->get_main_user_fpr)
krista@2461
  1923
                sqlite3_finalize(session->get_main_user_fpr);
krista@2461
  1924
            if (session->refresh_userid_default_key)
krista@2461
  1925
                sqlite3_finalize(session->refresh_userid_default_key);
vb@517
  1926
            if (session->blacklist_add)
vb@517
  1927
                sqlite3_finalize(session->blacklist_add);
vb@517
  1928
            if (session->blacklist_delete)
vb@517
  1929
                sqlite3_finalize(session->blacklist_delete);
vb@517
  1930
            if (session->blacklist_is_listed)
vb@517
  1931
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
  1932
            if (session->blacklist_retrieve)
vb@517
  1933
                sqlite3_finalize(session->blacklist_retrieve);
krista@957
  1934
            if (session->own_key_is_listed)
krista@957
  1935
                sqlite3_finalize(session->own_key_is_listed);
krista@3346
  1936
            if (session->is_own_address)
krista@3346
  1937
                sqlite3_finalize(session->is_own_address);
roker@1002
  1938
            if (session->own_identities_retrieve)
roker@1002
  1939
                sqlite3_finalize(session->own_identities_retrieve);
edouard@1394
  1940
            if (session->own_keys_retrieve)
edouard@1394
  1941
                sqlite3_finalize(session->own_keys_retrieve);
krista@2461
  1942
            // if (session->set_own_key)
krista@2461
  1943
            //     sqlite3_finalize(session->set_own_key);
krista@957
  1944
            if (session->sequence_value1)
krista@957
  1945
                sqlite3_finalize(session->sequence_value1);
krista@957
  1946
            if (session->sequence_value2)
krista@960
  1947
                sqlite3_finalize(session->sequence_value2);
krista@957
  1948
            if (session->set_revoked)
krista@957
  1949
                sqlite3_finalize(session->set_revoked);
krista@957
  1950
            if (session->get_revoked)
krista@957
  1951
                sqlite3_finalize(session->get_revoked);
krista@2752
  1952
            if (session->get_replacement_fpr)
krista@2752
  1953
                sqlite3_finalize(session->get_replacement_fpr);                
krista@2471
  1954
            if (session->add_mistrusted_key)
krista@2471
  1955
                sqlite3_finalize(session->add_mistrusted_key);
krista@2471
  1956
            if (session->delete_mistrusted_key)
krista@2471
  1957
                sqlite3_finalize(session->delete_mistrusted_key);
krista@2471
  1958
            if (session->is_mistrusted_key)
krista@2471
  1959
                sqlite3_finalize(session->is_mistrusted_key);
krista@2526
  1960
                
krista@2526
  1961
            if (session->db) {
krista@2526
  1962
                sqlite3_exec(        
krista@2526
  1963
                    session->db,
krista@2526
  1964
                    "PRAGMA optimize;\n",
krista@2526
  1965
                    NULL,
krista@2526
  1966
                    NULL,
krista@2526
  1967
                    NULL
krista@2526
  1968
                );
vb@65
  1969
                sqlite3_close_v2(session->db);
krista@2526
  1970
            }
vb@65
  1971
            if (session->system_db)
vb@65
  1972
                sqlite3_close_v2(session->system_db);
roker@529
  1973
        }
vb@65
  1974
vb@65
  1975
        release_transport_system(session, out_last);
vb@65
  1976
        release_cryptotech(session, out_last);
vb@65
  1977
roker@1722
  1978
#ifdef DEBUG_ERRORSTACK
roker@1722
  1979
        free_stringlist(session->errorstack);
roker@1722
  1980
#endif
vb@65
  1981
        free(session);
vb@62
  1982
    }
vb@0
  1983
}
vb@0
  1984
vb@467
  1985
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
  1986
{
vb@464
  1987
    assert(session);
vb@3691
  1988
    if (session)
vb@3691
  1989
        session->passive_mode = enable;
vb@464
  1990
}
vb@464
  1991
vb@467
  1992
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
  1993
{
vb@464
  1994
    assert(session);
vb@3691
  1995
    if (session)
vb@3691
  1996
        session->unencrypted_subject = enable;
vb@464
  1997
}
vb@464
  1998
vb@1819
  1999
DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
vb@1819
  2000
{
vb@1819
  2001
    assert(session);
vb@3691
  2002
    if (session)
vb@3691
  2003
        session->service_log = enable;
vb@1819
  2004
}
vb@1819
  2005
vb@0
  2006
DYNAMIC_API PEP_STATUS log_event(
vb@450
  2007
        PEP_SESSION session,
vb@451
  2008
        const char *title,
vb@451
  2009
        const char *entity,
vb@451
  2010
        const char *description,
vb@451
  2011
        const char *comment
vb@0
  2012
    )
vb@0
  2013
{
krista@3436
  2014
krista@3436
  2015
// N.B. If testing (so NDEBUG not defined) but this message is spam,
krista@3436
  2016
//      put -D_PEP_SERVICE_LOG_OFF into CFLAGS/CXXFLAGS     
krista@3436
  2017
#if !defined(NDEBUG) && !defined(_PEP_SERVICE_LOG_OFF)
vb@3367
  2018
    fprintf(stdout, "\n*** %s %s %s %s\n", title, entity, description, comment);
vb@3367
  2019
    session->service_log = true;
vb@3367
  2020
#endif
vb@3367
  2021
vb@3367
  2022
    // PEP_STATUS status = PEP_STATUS_OK;
krista@2518
  2023
    // int result;
krista@2518
  2024
    // 
krista@2518
  2025
    // assert(session);
krista@2518
  2026
    // assert(title);
krista@2518
  2027
    // assert(entity);
krista@2518
  2028
    // 
krista@2518
  2029
    // if (!(session && title && entity))
krista@2518
  2030
    //     return PEP_ILLEGAL_VALUE;
krista@2518
  2031
    // 
krista@2518
  2032
    // sqlite3_reset(session->log);
krista@2518
  2033
    // sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
krista@2518
  2034
    // sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
krista@2518
  2035
    // if (description)
krista@2518
  2036
    //     sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
krista@2518
  2037
    // else
krista@2518
  2038
    //     sqlite3_bind_null(session->log, 3);
krista@2518
  2039
    // if (comment)
krista@2518
  2040
    //     sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
krista@2518
  2041
    // else
krista@2518
  2042
    //     sqlite3_bind_null(session->log, 4);
vb@3739
  2043
    // result = Sqlite3_step(session->log);
krista@2518
  2044
    // sqlite3_reset(session->log);
krista@2518
  2045
    // 
krista@2499
  2046
    return PEP_STATUS_OK; // We ignore errors for this function.
vb@0
  2047
}
vb@0
  2048
vb@1819
  2049
DYNAMIC_API PEP_STATUS log_service(
vb@1819
  2050
        PEP_SESSION session,
vb@1819
  2051
        const char *title,
vb@1819
  2052
        const char *entity,
vb@1819
  2053
        const char *description,
vb@1819
  2054
        const char *comment
vb@1819
  2055
    )
vb@1819
  2056
{
vb@1819
  2057
    assert(session);
vb@1819
  2058
    if (!session)
vb@1819
  2059
        return PEP_ILLEGAL_VALUE;
vb@1819
  2060
vb@1819
  2061
    if (session->service_log)
vb@1819
  2062
        return log_event(session, title, entity, description, comment);
vb@1819
  2063
    else
vb@1819
  2064
        return PEP_STATUS_OK;
vb@1819
  2065
}
vb@1819
  2066
vb@233
  2067
DYNAMIC_API PEP_STATUS trustword(
vb@0
  2068
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
  2069
            char **word, size_t *wsize
vb@0
  2070
        )
vb@0
  2071
{
roker@529
  2072
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
  2073
roker@529
  2074
    assert(session);
roker@529
  2075
    assert(word);
roker@529
  2076
    assert(wsize);
vb@0
  2077
vb@191
  2078
    if (!(session && word && wsize))
vb@191
  2079
        return PEP_ILLEGAL_VALUE;
vb@191
  2080
roker@529
  2081
    *word = NULL;
roker@529
  2082
    *wsize = 0;
vb@0
  2083
roker@529
  2084
    if (lang == NULL)
roker@529
  2085
        lang = "en";
vb@0
  2086
roker@529
  2087
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  2088
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  2089
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  2090
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  2091
    assert(lang[2] == 0);
vb@0
  2092
roker@529
  2093
    sqlite3_reset(session->trustword);
vb@233
  2094
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
  2095
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
  2096
vb@3739
  2097
    const int result = Sqlite3_step(session->trustword);
roker@529
  2098
    if (result == SQLITE_ROW) {
vb@233
  2099
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
  2100
                    1));
roker@529
  2101
        if (*word)
vb@233
  2102
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
  2103
        else
Edouard@693
  2104
            status = PEP_OUT_OF_MEMORY;
roker@529
  2105
    } else
roker@529
  2106
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
  2107
roker@529
  2108
    sqlite3_reset(session->trustword);
roker@529
  2109
    return status;
vb@0
  2110
}
vb@0
  2111
vb@233
  2112
DYNAMIC_API PEP_STATUS trustwords(
vb@0
  2113
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
  2114
        char **words, size_t *wsize, int max_words
vb@0
  2115
    )
vb@0
  2116
{
roker@529
  2117
    const char *source = fingerprint;
vb@0
  2118
roker@529
  2119
    assert(session);
roker@529
  2120
    assert(fingerprint);
roker@529
  2121
    assert(words);
roker@529
  2122
    assert(wsize);
roker@529
  2123
    assert(max_words >= 0);
vb@0
  2124
vb@191
  2125
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
  2126
        return PEP_ILLEGAL_VALUE;
vb@191
  2127
roker@529
  2128
    *words = NULL;
roker@529
  2129
    *wsize = 0;
vb@0
  2130
roker@1559
  2131
    char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
  2132
    assert(buffer);
vb@0
  2133
    if (buffer == NULL)
vb@0
  2134
        return PEP_OUT_OF_MEMORY;
roker@1559
  2135
    char *dest = buffer;
vb@0
  2136
roker@1559
  2137
    const size_t fsize = strlen(fingerprint);
vb@0
  2138
roker@529
  2139
    if (!lang || !lang[0])
roker@529
  2140
        lang = "en";
vb@0
  2141
roker@529
  2142
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  2143
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  2144
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  2145
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  2146
    assert(lang[2] == 0);
vb@0
  2147
roker@529
  2148
    int n_words = 0;
roker@529
  2149
    while (source < fingerprint + fsize) {
vb@939
  2150
        PEP_STATUS _status;
roker@529
  2151
        uint16_t value;
roker@1559
  2152
        char *word = NULL;
roker@1559
  2153
        size_t _wsize = 0;
roker@529
  2154
        int j;
vb@0
  2155
vb@0
  2156
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
  2157
            if (*source >= 'a' && *source <= 'f')
roker@529
  2158
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
  2159
            else if (*source >= 'A' && *source <= 'F')
roker@529
  2160
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
  2161
            else if (*source >= '0' && *source <= '9')
roker@529
  2162
                value += (*source - '0') << (3 - j++) * 4;
roker@529
  2163
            
roker@529
  2164
            source++;
roker@529
  2165
        }
vb@0
  2166
roker@529
  2167
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
  2168
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
  2169
            free(buffer);
vb@0
  2170
            return PEP_OUT_OF_MEMORY;
vb@0
  2171
        }
roker@529
  2172
        if (word == NULL) {
vb@0
  2173
            free(buffer);
roker@529
  2174
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
  2175
        }
vb@0
  2176
roker@529
  2177
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
  2178
            strncpy(dest, word, _wsize);
vb@0
  2179
            free(word);
roker@529
  2180
            dest += _wsize;
roker@529
  2181
        }
roker@529
  2182
        else {
vb@0
  2183
            free(word);
roker@529
  2184
            break; // buffer full
vb@0
  2185
        }
vb@0
  2186
roker@529
  2187
        if (source < fingerprint + fsize
vb@251
  2188
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
  2189
            *dest++ = ' ';
vb@0
  2190
roker@529
  2191
        ++n_words;
roker@529
  2192
        if (max_words && n_words >= max_words)
roker@529
  2193
            break;
roker@529
  2194
    }
vb@0
  2195
roker@529
  2196
    *words = buffer;
roker@529
  2197
    *wsize = dest - buffer;
roker@529
  2198
    return PEP_STATUS_OK;
vb@0
  2199
}
vb@0
  2200
vb@0
  2201
pEp_identity *new_identity(
vb@0
  2202
        const char *address, const char *fpr, const char *user_id,
vb@0
  2203
        const char *username
vb@0
  2204
    )
vb@0
  2205
{
vb@0
  2206
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
  2207
    assert(result);
vb@0
  2208
    if (result) {
vb@0
  2209
        if (address) {
vb@0
  2210
            result->address = strdup(address);
vb@0
  2211
            assert(result->address);
vb@0
  2212
            if (result->address == NULL) {
vb@0
  2213
                free(result);
vb@0
  2214
                return NULL;
vb@0
  2215
            }
vb@0
  2216
        }
vb@0
  2217
        if (fpr) {
vb@0
  2218
            result->fpr = strdup(fpr);
vb@0
  2219
            assert(result->fpr);
vb@0
  2220
            if (result->fpr == NULL) {
vb@0
  2221
                free_identity(result);
vb@0
  2222
                return NULL;
vb@0
  2223
            }
vb@0
  2224
        }
vb@0
  2225
        if (user_id) {
vb@0
  2226
            result->user_id = strdup(user_id);
vb@0
  2227
            assert(result->user_id);
vb@0
  2228
            if (result->user_id == NULL) {
vb@0
  2229
                free_identity(result);
vb@0
  2230
                return NULL;
vb@0
  2231
            }
vb@0
  2232
        }
vb@0
  2233
        if (username) {
vb@0
  2234
            result->username = strdup(username);
vb@0
  2235
            assert(result->username);
vb@0
  2236
            if (result->username == NULL) {
vb@0
  2237
                free_identity(result);
vb@0
  2238
                return NULL;
vb@0
  2239
            }
vb@0
  2240
        }
vb@0
  2241
    }
vb@0
  2242
    return result;
vb@0
  2243
}
vb@0
  2244
vb@37
  2245
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
  2246
{
vb@37
  2247
    assert(src);
vb@37
  2248
vb@951
  2249
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
vb@951
  2250
            src->username);
vb@37
  2251
    assert(dup);
vb@37
  2252
    if (dup == NULL)
vb@37
  2253
        return NULL;
vb@37
  2254
    
vb@37
  2255
    dup->comm_type = src->comm_type;
vb@37
  2256
    dup->lang[0] = src->lang[0];
vb@37
  2257
    dup->lang[1] = src->lang[1];
vb@37
  2258
    dup->lang[2] = 0;
vb@930
  2259
    dup->flags = src->flags;
krista@2461
  2260
    dup->me = src->me;
krista@2461
  2261
    
vb@37
  2262
    return dup;
vb@37
  2263
}
vb@37
  2264
vb@0
  2265
void free_identity(pEp_identity *identity)
vb@0
  2266
{
vb@0
  2267
    if (identity) {
vb@0
  2268
        free(identity->address);
vb@0
  2269
        free(identity->fpr);
vb@0
  2270
        free(identity->user_id);
vb@0
  2271
        free(identity->username);
vb@0
  2272
        free(identity);
vb@0
  2273
    }
vb@0
  2274
}
vb@0
  2275
krista@2461
  2276
DYNAMIC_API PEP_STATUS get_default_own_userid(
krista@2461
  2277
        PEP_SESSION session, 
krista@2461
  2278
        char** userid
krista@2461
  2279
    )
krista@2461
  2280
{
krista@2461
  2281
    assert(session);
krista@2461
  2282
    assert(userid);
krista@2461
  2283
    
krista@2461
  2284
    if (!session || !userid)
krista@2461
  2285
        return PEP_ILLEGAL_VALUE;
krista@2461
  2286
        
krista@2461
  2287
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
  2288
    char* retval = NULL;
krista@2461
  2289
    
krista@2461
  2290
    sqlite3_reset(session->get_default_own_userid);
krista@2461
  2291
vb@3739
  2292
    const int result = Sqlite3_step(session->get_default_own_userid);
krista@2461
  2293
    const char* id;
krista@2461
  2294
    
krista@2461
  2295
    switch (result) {
krista@2461
  2296
        case SQLITE_ROW:
krista@2461
  2297
            id = (const char *) sqlite3_column_text(session->get_default_own_userid, 0);
krista@2461
  2298
            if (!id) {
krista@2461
  2299
                // Shouldn't happen.
krista@2461
  2300
                status = PEP_UNKNOWN_ERROR;
krista@2461
  2301
            }
krista@2461
  2302
            else {
krista@2461
  2303
                retval = strdup(id);
krista@2461
  2304
                if (!retval)
krista@2461
  2305
                    status = PEP_OUT_OF_MEMORY;
krista@2461
  2306
            }
krista@2461
  2307
            break;
krista@2461
  2308
        default:
krista@2461
  2309
            // Technically true, given how we find it, but FIXME we need a more descriptive error
krista@2461
  2310
            status = PEP_CANNOT_FIND_IDENTITY;
krista@2461
  2311
    }
krista@2461
  2312
krista@2461
  2313
    *userid = retval;
krista@2461
  2314
krista@2461
  2315
    sqlite3_reset(session->get_default_own_userid);
krista@2461
  2316
    
krista@2461
  2317
    return status;
krista@2461
  2318
}
krista@2461
  2319
krista@2461
  2320
DYNAMIC_API PEP_STATUS get_userid_alias_default(
krista@2461
  2321
        PEP_SESSION session, 
krista@2461
  2322
        const char* alias_id,
krista@2461
  2323
        char** default_id) {
krista@2461
  2324
            
krista@2461
  2325
    assert(session);
krista@2461
  2326
    assert(alias_id);
krista@2461
  2327
    assert(alias_id[0]);
krista@2461
  2328
    assert(default_id);
krista@2461
  2329
krista@2461
  2330
    if (!(session && alias_id && alias_id[0] && default_id))
krista@2461
  2331
        return PEP_ILLEGAL_VALUE;
krista@2461
  2332
krista@2461
  2333
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
  2334
    char* retval = NULL;
krista@2461
  2335
krista@2461
  2336
    sqlite3_reset(session->get_userid_alias_default);
krista@2461
  2337
    sqlite3_bind_text(session->get_userid_alias_default, 1, alias_id, -1, SQLITE_STATIC);
krista@2461
  2338
krista@2461
  2339
    const char* tempid;
krista@2461
  2340
    
vb@3739
  2341
    const int result = Sqlite3_step(session->get_userid_alias_default);
krista@2461
  2342
    switch (result) {
krista@2461
  2343
    case SQLITE_ROW:
krista@2461
  2344
        tempid = (const char *) sqlite3_column_text(session->get_userid_alias_default, 0);
krista@2461
  2345
        if (tempid) {
krista@2461
  2346
            retval = strdup(tempid);
krista@2461
  2347
            assert(retval);
krista@2461
  2348
            if (retval == NULL)
krista@2461
  2349
                return PEP_OUT_OF_MEMORY;
krista@2461
  2350
        }
krista@2461
  2351
    
krista@2461
  2352
        *default_id = retval;
krista@2461
  2353
        break;
krista@2461
  2354
    default:
krista@2461
  2355
        status = PEP_CANNOT_FIND_ALIAS;
krista@2461
  2356
        *default_id = NULL;
krista@2461
  2357
    }
krista@2461
  2358
krista@2461
  2359
    sqlite3_reset(session->get_userid_alias_default);
krista@2461
  2360
    return status;            
krista@2461
  2361
}
krista@2461
  2362
krista@2461
  2363
DYNAMIC_API PEP_STATUS set_userid_alias (
krista@2461
  2364
        PEP_SESSION session, 
krista@2461
  2365
        const char* default_id,
krista@2461
  2366
        const char* alias_id) {
krista@2461
  2367
            
krista@2461
  2368
    int result;
krista@2461
  2369
krista@2461
  2370
    assert(session);
krista@2461
  2371
    assert(default_id);
krista@2461
  2372
    assert(alias_id);
krista@2461
  2373
krista@2461
  2374
    if (!(session && default_id && alias_id && 
krista@2461
  2375
          default_id[0] != '\0' && alias_id[0] != '\0'))
krista@2461
  2376
        return PEP_ILLEGAL_VALUE;
krista@2478
  2377
    
krista@2479
  2378
    sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
krista@2461
  2379
krista@2461
  2380
    sqlite3_reset(session->add_userid_alias);
krista@2461
  2381
    sqlite3_bind_text(session->add_userid_alias, 1, default_id, -1,