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