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