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