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