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