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