src/pEpEngine.c
author Krista Bennett <krista@pep-project.org>
Wed, 24 Jan 2018 18:23:21 +0100
branchENGINE-332
changeset 2445 c061bd2500bd
parent 2441 eef4dbecb648
parent 2443 154269655c12
child 2458 2dfe65bd3613
permissions -rw-r--r--
merged in default
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,' ','')) ;";
krista@2438
   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@1752
  1017
    if (very_first)
edouard@1752
  1018
    {
edouard@1752
  1019
        // On first run, all private keys already present in PGP keyring 
edouard@1752
  1020
        // are taken as own in order to seamlessly integrate with
edouard@1752
  1021
        // pre-existing GPG setup.
edouard@1752
  1022
edouard@1752
  1023
        ////////////////////////////// WARNING: ///////////////////////////
edouard@1752
  1024
        // Considering all PGP priv keys as own is dangerous in case of 
edouard@1752
  1025
        // re-initialization of pEp DB, while keeping PGP keyring as-is!
edouard@1752
  1026
        //
edouard@1752
  1027
        // Indeed, if pEpEngine did import spoofed private keys in previous
edouard@1752
  1028
        // install, then those keys become automatically trusted in case 
vb@2443
  1029
        // management.db is deleted.
edouard@1752
  1030
        //
edouard@1752
  1031
        // A solution to distinguish bare GPG keyring from pEp keyring is
edouard@1752
  1032
        // needed here. Then keys managed by pEpEngine wouldn't be
edouard@1752
  1033
        // confused with GPG keys managed by the user through GPA.
edouard@1752
  1034
        ///////////////////////////////////////////////////////////////////
edouard@1752
  1035
        
edouard@1752
  1036
        stringlist_t *keylist = NULL;
edouard@1752
  1037
edouard@1760
  1038
        status = find_private_keys(_session, NULL, &keylist);
edouard@1752
  1039
        assert(status != PEP_OUT_OF_MEMORY);
edouard@1752
  1040
        if (status == PEP_OUT_OF_MEMORY)
edouard@1752
  1041
            return PEP_OUT_OF_MEMORY;
edouard@1752
  1042
        
edouard@1752
  1043
        if (keylist != NULL && keylist->value != NULL)
edouard@1752
  1044
        {
edouard@1752
  1045
            stringlist_t *_keylist;
edouard@1752
  1046
            for (_keylist = keylist; _keylist && _keylist->value; _keylist = _keylist->next) {
edouard@1752
  1047
                status = set_own_key(_session, 
edouard@1752
  1048
                                     "" /* address is unused in own_keys */,
edouard@1752
  1049
                                     _keylist->value);
edouard@1752
  1050
            }
edouard@1752
  1051
        }
edouard@1752
  1052
    }
vb@464
  1053
edouard@1603
  1054
    // sync_session set to own session by default
edouard@1603
  1055
    // sync_session is then never null on a valid session
edouard@1603
  1056
    _session->sync_session = _session;
edouard@1603
  1057
roker@529
  1058
    *session = _session;
krista@2176
  1059
    
krista@2176
  1060
    // Note: Following statement is NOT for any cryptographic/secure functionality; it is
krista@2176
  1061
    //       ONLY used for some randomness in generated outer message ID, which are
krista@2176
  1062
    //       required by the RFC to be globally unique!
krista@2176
  1063
    srand(time(NULL));
krista@2176
  1064
    
roker@529
  1065
    return PEP_STATUS_OK;
vb@65
  1066
vb@65
  1067
enomem:
vb@65
  1068
    status = PEP_OUT_OF_MEMORY;
vb@65
  1069
vb@65
  1070
pep_error:
vb@65
  1071
    release(_session);
vb@65
  1072
    return status;
vb@0
  1073
}
vb@0
  1074
vb@0
  1075
DYNAMIC_API void release(PEP_SESSION session)
vb@0
  1076
{
vb@62
  1077
    bool out_last = false;
krista@2124
  1078
    int _count = --init_count;
edouard@2112
  1079
    
edouard@2112
  1080
    assert(_count >= -1);
roker@529
  1081
    assert(session);
vb@0
  1082
edouard@2112
  1083
    if (!((_count >= -1) && session))
vb@191
  1084
        return;
vb@191
  1085
vb@62
  1086
    // a small race condition but still a race condition
vb@113
  1087
    // mitigated by calling caveat (see documentation)
krista@2125
  1088
    // (release() is to be guarded by a mutex by the caller)
edouard@2112
  1089
    if (_count == -1)
vb@62
  1090
        out_last = true;
vb@62
  1091
roker@529
  1092
    if (session) {
vb@986
  1093
roker@529
  1094
        if (session->db) {
vb@517
  1095
            if (session->log)
vb@517
  1096
                sqlite3_finalize(session->log);
vb@233
  1097
            if (session->trustword)
vb@233
  1098
                sqlite3_finalize(session->trustword);
vb@65
  1099
            if (session->get_identity)
vb@65
  1100
                sqlite3_finalize(session->get_identity);
krista@2324
  1101
            if (session->get_identity_without_trust_check)
krista@2324
  1102
                sqlite3_finalize(session->get_identity_without_trust_check);
krista@2317
  1103
            if (session->get_identities_by_address)
krista@2317
  1104
                sqlite3_finalize(session->get_identities_by_address);            
krista@2311
  1105
            if (session->get_user_default_key)
krista@2311
  1106
                sqlite3_finalize(session->get_user_default_key);    
krista@2386
  1107
            if (session->get_default_own_userid)
krista@2386
  1108
                sqlite3_finalize(session->get_default_own_userid);
krista@2397
  1109
            if (session->get_userid_alias_default)
krista@2397
  1110
                sqlite3_finalize(session->get_userid_alias_default);
krista@2397
  1111
            if (session->add_userid_alias)
krista@2397
  1112
                sqlite3_finalize(session->add_userid_alias);
krista@1799
  1113
            if (session->replace_identities_fpr)
krista@1799
  1114
                sqlite3_finalize(session->replace_identities_fpr);        
krista@2326
  1115
            if (session->remove_fpr_as_default)
krista@2326
  1116
                sqlite3_finalize(session->remove_fpr_as_default);            
vb@65
  1117
            if (session->set_person)
vb@65
  1118
                sqlite3_finalize(session->set_person);
edouard@1234
  1119
            if (session->set_device_group)
edouard@1234
  1120
                sqlite3_finalize(session->set_device_group);
edouard@1235
  1121
            if (session->get_device_group)
edouard@1235
  1122
                sqlite3_finalize(session->get_device_group);
vb@65
  1123
            if (session->set_pgp_keypair)
vb@65
  1124
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
  1125
            if (session->set_identity)
vb@517
  1126
                sqlite3_finalize(session->set_identity);
vb@932
  1127
            if (session->set_identity_flags)
vb@932
  1128
                sqlite3_finalize(session->set_identity_flags);
edouard@1394
  1129
            if (session->unset_identity_flags)
edouard@1394
  1130
                sqlite3_finalize(session->unset_identity_flags);
vb@65
  1131
            if (session->set_trust)
vb@65
  1132
                sqlite3_finalize(session->set_trust);
krista@1799
  1133
            if (session->update_trust_for_fpr)
krista@1799
  1134
                sqlite3_finalize(session->update_trust_for_fpr);
vb@65
  1135
            if (session->get_trust)
vb@65
  1136
                sqlite3_finalize(session->get_trust);
vb@251
  1137
            if (session->least_trust)
vb@251
  1138
                sqlite3_finalize(session->least_trust);
vb@517
  1139
            if (session->mark_compromized)
vb@517
  1140
                sqlite3_finalize(session->mark_compromized);
vb@517
  1141
            if (session->crashdump)
vb@517
  1142
                sqlite3_finalize(session->crashdump);
vb@517
  1143
            if (session->languagelist)
vb@517
  1144
                sqlite3_finalize(session->languagelist);
vb@517
  1145
            if (session->i18n_token)
vb@517
  1146
                sqlite3_finalize(session->i18n_token);
krista@2317
  1147
            if (session->replace_userid)
krista@2317
  1148
                sqlite3_finalize(session->replace_userid);
krista@2433
  1149
            if (session->replace_main_user_fpr)
krista@2433
  1150
                sqlite3_finalize(session->replace_main_user_fpr);                
krista@2433
  1151
            if (session->get_main_user_fpr)
krista@2435
  1152
                sqlite3_finalize(session->get_main_user_fpr);
krista@2435
  1153
            if (session->refresh_userid_default_key)
krista@2435
  1154
                sqlite3_finalize(session->refresh_userid_default_key);
vb@517
  1155
            if (session->blacklist_add)
vb@517
  1156
                sqlite3_finalize(session->blacklist_add);
vb@517
  1157
            if (session->blacklist_delete)
vb@517
  1158
                sqlite3_finalize(session->blacklist_delete);
vb@517
  1159
            if (session->blacklist_is_listed)
vb@517
  1160
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
  1161
            if (session->blacklist_retrieve)
vb@517
  1162
                sqlite3_finalize(session->blacklist_retrieve);
krista@957
  1163
            if (session->own_key_is_listed)
krista@957
  1164
                sqlite3_finalize(session->own_key_is_listed);
roker@1002
  1165
            if (session->own_identities_retrieve)
roker@1002
  1166
                sqlite3_finalize(session->own_identities_retrieve);
edouard@1394
  1167
            if (session->own_keys_retrieve)
edouard@1394
  1168
                sqlite3_finalize(session->own_keys_retrieve);
krista@2303
  1169
            // if (session->set_own_key)
krista@2303
  1170
            //     sqlite3_finalize(session->set_own_key);
krista@957
  1171
            if (session->sequence_value1)
krista@957
  1172
                sqlite3_finalize(session->sequence_value1);
krista@957
  1173
            if (session->sequence_value2)
krista@960
  1174
                sqlite3_finalize(session->sequence_value2);
vb@1085
  1175
            if (session->sequence_value3)
vb@1085
  1176
                sqlite3_finalize(session->sequence_value3);
krista@957
  1177
            if (session->set_revoked)
krista@957
  1178
                sqlite3_finalize(session->set_revoked);
krista@957
  1179
            if (session->get_revoked)
krista@957
  1180
                sqlite3_finalize(session->get_revoked);
vb@26
  1181
vb@65
  1182
            if (session->db)
vb@65
  1183
                sqlite3_close_v2(session->db);
vb@65
  1184
            if (session->system_db)
vb@65
  1185
                sqlite3_close_v2(session->system_db);
roker@529
  1186
        }
vb@65
  1187
vb@65
  1188
        release_transport_system(session, out_last);
vb@65
  1189
        release_cryptotech(session, out_last);
vb@65
  1190
roker@1722
  1191
#ifdef DEBUG_ERRORSTACK
roker@1722
  1192
        free_stringlist(session->errorstack);
roker@1722
  1193
#endif
vb@65
  1194
        free(session);
vb@62
  1195
    }
vb@0
  1196
}
vb@0
  1197
vb@467
  1198
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
  1199
{
vb@464
  1200
    assert(session);
vb@467
  1201
    session->passive_mode = enable;
vb@464
  1202
}
vb@464
  1203
vb@467
  1204
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
  1205
{
vb@464
  1206
    assert(session);
vb@467
  1207
    session->unencrypted_subject = enable;
vb@464
  1208
}
vb@464
  1209
vb@1110
  1210
DYNAMIC_API void config_keep_sync_msg(PEP_SESSION session, bool enable)
vb@1109
  1211
{
vb@1109
  1212
    assert(session);
vb@1110
  1213
    session->keep_sync_msg = enable;
vb@1109
  1214
}
vb@1109
  1215
vb@1819
  1216
DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
vb@1819
  1217
{
vb@1819
  1218
    assert(session);
vb@1819
  1219
    session->service_log = enable;
vb@1819
  1220
}
vb@1819
  1221
vb@0
  1222
DYNAMIC_API PEP_STATUS log_event(
vb@450
  1223
        PEP_SESSION session,
vb@451
  1224
        const char *title,
vb@451
  1225
        const char *entity,
vb@451
  1226
        const char *description,
vb@451
  1227
        const char *comment
vb@0
  1228
    )
vb@0
  1229
{
roker@529
  1230
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
  1231
    int result;
vb@0
  1232
roker@529
  1233
    assert(session);
roker@529
  1234
    assert(title);
roker@529
  1235
    assert(entity);
vb@0
  1236
vb@191
  1237
    if (!(session && title && entity))
vb@191
  1238
        return PEP_ILLEGAL_VALUE;
vb@191
  1239
roker@529
  1240
    sqlite3_reset(session->log);
roker@529
  1241
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
  1242
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
  1243
    if (description)
vb@46
  1244
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
  1245
    else
roker@529
  1246
        sqlite3_bind_null(session->log, 3);
roker@529
  1247
    if (comment)
roker@529
  1248
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
  1249
    else
roker@529
  1250
        sqlite3_bind_null(session->log, 4);
roker@529
  1251
    do {
roker@529
  1252
        result = sqlite3_step(session->log);
roker@529
  1253
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
  1254
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
  1255
            status = PEP_UNKNOWN_ERROR;
roker@529
  1256
    } while (result == SQLITE_BUSY);
roker@529
  1257
    sqlite3_reset(session->log);
vb@0
  1258
roker@1853
  1259
    return ADD_TO_LOG(status);
vb@0
  1260
}
vb@0
  1261
vb@1819
  1262
DYNAMIC_API PEP_STATUS log_service(
vb@1819
  1263
        PEP_SESSION session,
vb@1819
  1264
        const char *title,
vb@1819
  1265
        const char *entity,
vb@1819
  1266
        const char *description,
vb@1819
  1267
        const char *comment
vb@1819
  1268
    )
vb@1819
  1269
{
vb@1819
  1270
    assert(session);
vb@1819
  1271
    if (!session)
vb@1819
  1272
        return PEP_ILLEGAL_VALUE;
vb@1819
  1273
vb@1819
  1274
    if (session->service_log)
vb@1819
  1275
        return log_event(session, title, entity, description, comment);
vb@1819
  1276
    else
vb@1819
  1277
        return PEP_STATUS_OK;
vb@1819
  1278
}
vb@1819
  1279
vb@233
  1280
DYNAMIC_API PEP_STATUS trustword(
vb@0
  1281
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
  1282
            char **word, size_t *wsize
vb@0
  1283
        )
vb@0
  1284
{
roker@529
  1285
    PEP_STATUS status = PEP_STATUS_OK;
vb@0
  1286
roker@529
  1287
    assert(session);
roker@529
  1288
    assert(word);
roker@529
  1289
    assert(wsize);
vb@0
  1290
vb@191
  1291
    if (!(session && word && wsize))
vb@191
  1292
        return PEP_ILLEGAL_VALUE;
vb@191
  1293
roker@529
  1294
    *word = NULL;
roker@529
  1295
    *wsize = 0;
vb@0
  1296
roker@529
  1297
    if (lang == NULL)
roker@529
  1298
        lang = "en";
vb@0
  1299
roker@529
  1300
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  1301
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  1302
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  1303
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  1304
    assert(lang[2] == 0);
vb@0
  1305
roker@529
  1306
    sqlite3_reset(session->trustword);
vb@233
  1307
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
  1308
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
  1309
roker@877
  1310
    const int result = sqlite3_step(session->trustword);
roker@529
  1311
    if (result == SQLITE_ROW) {
vb@233
  1312
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
  1313
                    1));
roker@529
  1314
        if (*word)
vb@233
  1315
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
  1316
        else
Edouard@693
  1317
            status = PEP_OUT_OF_MEMORY;
roker@529
  1318
    } else
roker@529
  1319
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
  1320
roker@529
  1321
    sqlite3_reset(session->trustword);
roker@529
  1322
    return status;
vb@0
  1323
}
vb@0
  1324
vb@233
  1325
DYNAMIC_API PEP_STATUS trustwords(
vb@0
  1326
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
  1327
        char **words, size_t *wsize, int max_words
vb@0
  1328
    )
vb@0
  1329
{
roker@529
  1330
    const char *source = fingerprint;
vb@0
  1331
roker@529
  1332
    assert(session);
roker@529
  1333
    assert(fingerprint);
roker@529
  1334
    assert(words);
roker@529
  1335
    assert(wsize);
roker@529
  1336
    assert(max_words >= 0);
vb@0
  1337
vb@191
  1338
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
  1339
        return PEP_ILLEGAL_VALUE;
vb@191
  1340
roker@529
  1341
    *words = NULL;
roker@529
  1342
    *wsize = 0;
vb@0
  1343
roker@1559
  1344
    char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
  1345
    assert(buffer);
vb@0
  1346
    if (buffer == NULL)
vb@0
  1347
        return PEP_OUT_OF_MEMORY;
roker@1559
  1348
    char *dest = buffer;
vb@0
  1349
roker@1559
  1350
    const size_t fsize = strlen(fingerprint);
vb@0
  1351
roker@529
  1352
    if (!lang || !lang[0])
roker@529
  1353
        lang = "en";
vb@0
  1354
roker@529
  1355
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
  1356
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
  1357
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
  1358
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
  1359
    assert(lang[2] == 0);
vb@0
  1360
roker@529
  1361
    int n_words = 0;
roker@529
  1362
    while (source < fingerprint + fsize) {
vb@939
  1363
        PEP_STATUS _status;
roker@529
  1364
        uint16_t value;
roker@1559
  1365
        char *word = NULL;
roker@1559
  1366
        size_t _wsize = 0;
roker@529
  1367
        int j;
vb@0
  1368
vb@0
  1369
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
  1370
            if (*source >= 'a' && *source <= 'f')
roker@529
  1371
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
  1372
            else if (*source >= 'A' && *source <= 'F')
roker@529
  1373
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
  1374
            else if (*source >= '0' && *source <= '9')
roker@529
  1375
                value += (*source - '0') << (3 - j++) * 4;
roker@529
  1376
            
roker@529
  1377
            source++;
roker@529
  1378
        }
vb@0
  1379
roker@529
  1380
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
  1381
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
  1382
            free(buffer);
vb@0
  1383
            return PEP_OUT_OF_MEMORY;
vb@0
  1384
        }
roker@529
  1385
        if (word == NULL) {
vb@0
  1386
            free(buffer);
roker@529
  1387
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
  1388
        }
vb@0
  1389
roker@529
  1390
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
  1391
            strncpy(dest, word, _wsize);
vb@0
  1392
            free(word);
roker@529
  1393
            dest += _wsize;
roker@529
  1394
        }
roker@529
  1395
        else {
vb@0
  1396
            free(word);
roker@529
  1397
            break; // buffer full
vb@0
  1398
        }
vb@0
  1399
roker@529
  1400
        if (source < fingerprint + fsize
vb@251
  1401
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
  1402
            *dest++ = ' ';
vb@0
  1403
roker@529
  1404
        ++n_words;
roker@529
  1405
        if (max_words && n_words >= max_words)
roker@529
  1406
            break;
roker@529
  1407
    }
vb@0
  1408
roker@529
  1409
    *words = buffer;
roker@529
  1410
    *wsize = dest - buffer;
roker@529
  1411
    return PEP_STATUS_OK;
vb@0
  1412
}
vb@0
  1413
vb@0
  1414
pEp_identity *new_identity(
vb@0
  1415
        const char *address, const char *fpr, const char *user_id,
vb@0
  1416
        const char *username
vb@0
  1417
    )
vb@0
  1418
{
vb@0
  1419
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
  1420
    assert(result);
vb@0
  1421
    if (result) {
vb@0
  1422
        if (address) {
vb@0
  1423
            result->address = strdup(address);
vb@0
  1424
            assert(result->address);
vb@0
  1425
            if (result->address == NULL) {
vb@0
  1426
                free(result);
vb@0
  1427
                return NULL;
vb@0
  1428
            }
vb@0
  1429
        }
vb@0
  1430
        if (fpr) {
vb@0
  1431
            result->fpr = strdup(fpr);
vb@0
  1432
            assert(result->fpr);
vb@0
  1433
            if (result->fpr == NULL) {
vb@0
  1434
                free_identity(result);
vb@0
  1435
                return NULL;
vb@0
  1436
            }
vb@0
  1437
        }
vb@0
  1438
        if (user_id) {
vb@0
  1439
            result->user_id = strdup(user_id);
vb@0
  1440
            assert(result->user_id);
vb@0
  1441
            if (result->user_id == NULL) {
vb@0
  1442
                free_identity(result);
vb@0
  1443
                return NULL;
vb@0
  1444
            }
vb@0
  1445
        }
vb@0
  1446
        if (username) {
vb@0
  1447
            result->username = strdup(username);
vb@0
  1448
            assert(result->username);
vb@0
  1449
            if (result->username == NULL) {
vb@0
  1450
                free_identity(result);
vb@0
  1451
                return NULL;
vb@0
  1452
            }
vb@0
  1453
        }
vb@0
  1454
    }
vb@0
  1455
    return result;
vb@0
  1456
}
vb@0
  1457
vb@37
  1458
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
  1459
{
vb@37
  1460
    assert(src);
vb@37
  1461
vb@951
  1462
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
vb@951
  1463
            src->username);
vb@37
  1464
    assert(dup);
vb@37
  1465
    if (dup == NULL)
vb@37
  1466
        return NULL;
vb@37
  1467
    
vb@37
  1468
    dup->comm_type = src->comm_type;
vb@37
  1469
    dup->lang[0] = src->lang[0];
vb@37
  1470
    dup->lang[1] = src->lang[1];
vb@37
  1471
    dup->lang[2] = 0;
vb@930
  1472
    dup->flags = src->flags;
krista@2288
  1473
    dup->me = src->me;
krista@2288
  1474
    
vb@37
  1475
    return dup;
vb@37
  1476
}
vb@37
  1477
vb@0
  1478
void free_identity(pEp_identity *identity)
vb@0
  1479
{
vb@0
  1480
    if (identity) {
vb@0
  1481
        free(identity->address);
vb@0
  1482
        free(identity->fpr);
vb@0
  1483
        free(identity->user_id);
vb@0
  1484
        free(identity->username);
vb@0
  1485
        free(identity);
vb@0
  1486
    }
vb@0
  1487
}
vb@0
  1488
krista@2386
  1489
DYNAMIC_API PEP_STATUS get_default_own_userid(
krista@2303
  1490
        PEP_SESSION session, 
krista@2303
  1491
        char** userid
krista@2303
  1492
    )
krista@2303
  1493
{
krista@2303
  1494
    assert(session);
krista@2303
  1495
    assert(userid);
krista@2303
  1496
    
krista@2303
  1497
    if (!session || !userid)
krista@2303
  1498
        return PEP_ILLEGAL_VALUE;
krista@2303
  1499
        
krista@2303
  1500
    PEP_STATUS status = PEP_STATUS_OK;
krista@2303
  1501
    char* retval = NULL;
krista@2303
  1502
    
krista@2386
  1503
    sqlite3_reset(session->get_default_own_userid);
krista@2386
  1504
krista@2386
  1505
    const int result = sqlite3_step(session->get_default_own_userid);
krista@2304
  1506
    const char* id;
krista@2304
  1507
    
krista@2303
  1508
    switch (result) {
krista@2303
  1509
        case SQLITE_ROW:
krista@2386
  1510
            id = (const char *) sqlite3_column_text(session->get_default_own_userid, 0);
krista@2303
  1511
            if (!id) {
krista@2303
  1512
                // Shouldn't happen.
krista@2303
  1513
                status = PEP_UNKNOWN_ERROR;
krista@2303
  1514
            }
krista@2303
  1515
            else {
krista@2303
  1516
                retval = strdup(id);
krista@2303
  1517
                if (!retval)
krista@2303
  1518
                    status = PEP_OUT_OF_MEMORY;
krista@2303
  1519
            }
krista@2303
  1520
            break;
krista@2303
  1521
        default:
krista@2303
  1522
            // Technically true, given how we find it, but FIXME we need a more descriptive error
krista@2303
  1523
            status = PEP_CANNOT_FIND_IDENTITY;
krista@2304
  1524
            *userid = NULL;
krista@2303
  1525
    }
krista@2303
  1526
krista@2303
  1527
    *userid = retval;
krista@2303
  1528
krista@2386
  1529
    sqlite3_reset(session->get_default_own_userid);
krista@2303
  1530
    
krista@2303
  1531
    return status;
krista@2303
  1532
}
krista@2303
  1533
krista@2398
  1534
DYNAMIC_API PEP_STATUS get_userid_alias_default(
krista@2398
  1535
        PEP_SESSION session, 
krista@2401
  1536
        const char* alias_id,
krista@2398
  1537
        char** default_id) {
krista@2398
  1538
            
krista@2398
  1539
    assert(session);
krista@2398
  1540
    assert(alias_id);
krista@2398
  1541
    assert(alias_id[0]);
krista@2398
  1542
    assert(default_id);
krista@2398
  1543
krista@2398
  1544
    if (!(session && alias_id && alias_id[0] && default_id))
krista@2398
  1545
        return PEP_ILLEGAL_VALUE;
krista@2398
  1546
krista@2398
  1547
    PEP_STATUS status = PEP_STATUS_OK;
krista@2398
  1548
    char* retval = NULL;
krista@2398
  1549
krista@2398
  1550
    sqlite3_reset(session->get_userid_alias_default);
krista@2398
  1551
    sqlite3_bind_text(session->get_userid_alias_default, 1, alias_id, -1, SQLITE_STATIC);
krista@2398
  1552
krista@2398
  1553
    const char* tempid;
krista@2398
  1554
    
krista@2398
  1555
    const int result = sqlite3_step(session->get_userid_alias_default);
krista@2398
  1556
    switch (result) {
krista@2398
  1557
    case SQLITE_ROW:
krista@2398
  1558
        tempid = (const char *) sqlite3_column_text(session->get_userid_alias_default, 0);
krista@2398
  1559
        if (tempid) {
krista@2398
  1560
            retval = strdup(tempid);
krista@2398
  1561
            assert(retval);
krista@2398
  1562
            if (retval == NULL)
krista@2398
  1563
                return PEP_OUT_OF_MEMORY;
krista@2398
  1564
        }
krista@2398
  1565
    
krista@2398
  1566
        *default_id = retval;
krista@2398
  1567
        break;
krista@2398
  1568
    default:
krista@2398
  1569
        status = PEP_CANNOT_FIND_ALIAS;
krista@2398
  1570
        *default_id = NULL;
krista@2398
  1571
    }
krista@2398
  1572
krista@2398
  1573
    sqlite3_reset(session->get_userid_alias_default);
krista@2398
  1574
    return status;            
krista@2398
  1575
}
krista@2398
  1576
krista@2398
  1577
DYNAMIC_API PEP_STATUS set_userid_alias (
krista@2398
  1578
        PEP_SESSION session, 
krista@2401
  1579
        const char* default_id,
krista@2401
  1580
        const char* alias_id) {
krista@2398
  1581
            
krista@2398
  1582
    int result;
krista@2398
  1583
krista@2398
  1584
    assert(session);
krista@2398
  1585
    assert(default_id);
krista@2398
  1586
    assert(alias_id);
krista@2398
  1587
krista@2398
  1588
    if (!(session && default_id && alias_id && 
krista@2398
  1589
          default_id[0] != '\0' && alias_id[0] != '\0'))
krista@2398
  1590
        return PEP_ILLEGAL_VALUE;
krista@2398
  1591
krista@2398
  1592
    sqlite3_reset(session->add_userid_alias);
krista@2398
  1593
    sqlite3_bind_text(session->add_userid_alias, 1, default_id, -1,
krista@2398
  1594
            SQLITE_STATIC);
krista@2398
  1595
    sqlite3_bind_text(session->add_userid_alias, 2, alias_id, -1,
krista@2398
  1596
            SQLITE_STATIC);
krista@2398
  1597
        
krista@2398
  1598
    result = sqlite3_step(session->add_userid_alias);
krista@2398
  1599
krista@2398
  1600
    sqlite3_reset(session->add_userid_alias);
krista@2398
  1601
    if (result != SQLITE_DONE)
krista@2398
  1602
        return PEP_CANNOT_SET_ALIAS;
krista@2398
  1603
    
krista@2398
  1604
    return PEP_STATUS_OK;
krista@2398
  1605
}
krista@2398
  1606
krista@2329
  1607
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
  1608
        PEP_SESSION session,
Edouard@559
  1609
        const char *address,
Edouard@559
  1610
        const char *user_id,
krista@2329
  1611
        pEp_identity **identity
vb@0
  1612
    )
vb@0
  1613
{
roker@529
  1614
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
  1615
    static pEp_identity *_identity;
vb@0
  1616
roker@529
  1617
    assert(session);
roker@529
  1618
    assert(address);
vb@8
  1619
    assert(address[0]);
vb@632
  1620
    assert(identity);
vb@0
  1621
vb@632
  1622
    if (!(session && address && address[0] && identity))
vb@191
  1623
        return PEP_ILLEGAL_VALUE;
vb@191
  1624
vb@632
  1625
    *identity = NULL;
vb@632
  1626
krista@2329
  1627
    sqlite3_reset(session->get_identity);
krista@2329
  1628
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
krista@2329
  1629
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
krista@2329
  1630
krista@2329
  1631
    const int result = sqlite3_step(session->get_identity);
roker@529
  1632
    switch (result) {
roker@529
  1633
    case SQLITE_ROW:
vb@0
  1634
        _identity = new_identity(
vb@0
  1635
                address,
krista@2329
  1636
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
  1637
                user_id,
krista@2329
  1638
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
  1639
                );
vb@0
  1640
        assert(_identity);
vb@0
  1641
        if (_identity == NULL)
vb@0
  1642
            return PEP_OUT_OF_MEMORY;
vb@0
  1643
vb@951
  1644
        _identity->comm_type = (PEP_comm_type)
krista@2329
  1645
            sqlite3_column_int(session->get_identity, 2);
vb@951
  1646
        const char* const _lang = (const char *)
krista@2329
  1647
            sqlite3_column_text(session->get_identity, 3);
vb@0
  1648
        if (_lang && _lang[0]) {
roker@529
  1649
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
  1650
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
  1651
            assert(_lang[2] == 0);
roker@529
  1652
            _identity->lang[0] = _lang[0];
roker@529
  1653
            _identity->lang[1] = _lang[1];
vb@0
  1654
            _identity->lang[2] = 0;
roker@529
  1655
        }
vb@951
  1656
        _identity->flags = (unsigned int)
krista@2329
  1657
            sqlite3_column_int(session->get_identity, 4);
krista@2303
  1658
        _identity->me = (unsigned int)
krista@2329
  1659
            sqlite3_column_int(session->get_identity, 5);
krista@2303
  1660
    
roker@529
  1661
        *identity = _identity;
roker@529
  1662
        break;
roker@529
  1663
    default:
vb@0
  1664
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
  1665
        *identity = NULL;
roker@529
  1666
    }
vb@0
  1667
krista@2329
  1668
    sqlite3_reset(session->get_identity);
roker@529
  1669
    return status;
vb@0
  1670
}
vb@0
  1671
krista@2324
  1672
PEP_STATUS get_identity_without_trust_check(
krista@2308
  1673
        PEP_SESSION session,
krista@2308
  1674
        const char *address,
krista@2308
  1675
        const char *user_id,
krista@2308
  1676
        pEp_identity **identity
krista@2308
  1677
    )
krista@2308
  1678
{
krista@2329
  1679
    PEP_STATUS status = PEP_STATUS_OK;
krista@2329
  1680
    static pEp_identity *_identity;
krista@2329
  1681
krista@2329
  1682
    assert(session);
krista@2329
  1683
    assert(address);
krista@2329
  1684
    assert(address[0]);
krista@2329
  1685
    assert(identity);
krista@2329
  1686
krista@2329
  1687
    if (!(session && address && address[0] && identity))
krista@2329
  1688
        return PEP_ILLEGAL_VALUE;
krista@2329
  1689
krista@2329
  1690
    *identity = NULL;
krista@2329
  1691
krista@2329
  1692
    sqlite3_reset(session->get_identity_without_trust_check);
krista@2329
  1693
    sqlite3_bind_text(session->get_identity_without_trust_check, 1, address, -1, SQLITE_STATIC);
krista@2329
  1694
    sqlite3_bind_text(session->get_identity_without_trust_check, 2, user_id, -1, SQLITE_STATIC);
krista@2329
  1695
krista@2329
  1696
    const int result = sqlite3_step(session->get_identity_without_trust_check);
krista@2329
  1697
    switch (result) {
krista@2329
  1698
    case SQLITE_ROW:
krista@2329
  1699
        _identity = new_identity(
krista@2329
  1700
                address,
krista@2329
  1701
                (const char *) sqlite3_column_text(session->get_identity_without_trust_check, 0),
krista@2329
  1702
                user_id,
krista@2329
  1703
                (const char *) sqlite3_column_text(session->get_identity_without_trust_check, 1)
krista@2329
  1704
                );
krista@2329
  1705
        assert(_identity);
krista@2329
  1706
        if (_identity == NULL)
krista@2329
  1707
            return PEP_OUT_OF_MEMORY;
krista@2329
  1708
krista@2329
  1709
        _identity->comm_type = PEP_ct_unknown;
krista@2329
  1710
        const char* const _lang = (const char *)
krista@2329
  1711
            sqlite3_column_text(session->get_identity_without_trust_check, 2);
krista@2329
  1712
        if (_lang && _lang[0]) {
krista@2329
  1713
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
krista@2329
  1714
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
krista@2329
  1715
            assert(_lang[2] == 0);
krista@2329
  1716
            _identity->lang[0] = _lang[0];
krista@2329
  1717
            _identity->lang[1] = _lang[1];
krista@2329
  1718
            _identity->lang[2] = 0;
krista@2329
  1719
        }
krista@2329
  1720
        _identity->flags = (unsigned int)
krista@2329
  1721
            sqlite3_column_int(session->get_identity_without_trust_check, 3);
krista@2329
  1722
        _identity->me = (unsigned int)
krista@2329
  1723
            sqlite3_column_int(session->get_identity_without_trust_check, 4);
krista@2329
  1724
    
krista@2329
  1725
        *identity = _identity;
krista@2329
  1726
        break;
krista@2329
  1727
    default:
krista@2329
  1728
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2329
  1729
        *identity = NULL;
krista@2329
  1730
    }
krista@2329
  1731
krista@2329
  1732
    sqlite3_reset(session->get_identity_without_trust_check);
krista@2329
  1733
    return status;
krista@2308
  1734
}
krista@2308
  1735
krista@2317
  1736
PEP_STATUS get_identities_by_address(
krista@2317
  1737
        PEP_SESSION session,
krista@2317
  1738
        const char *address,
krista@2317
  1739
        identity_list** id_list
krista@2317
  1740
    )
krista@2317
  1741
{
krista@2317
  1742
    pEp_identity* ident;
krista@2317
  1743
krista@2317
  1744
    assert(session);
krista@2317
  1745
    assert(address);
krista@2317
  1746
    assert(address[0]);
krista@2317
  1747
    assert(id_list);
krista@2317
  1748
krista@2317
  1749
    if (!(session && address && address[0] && id_list))
krista@2317
  1750
        return PEP_ILLEGAL_VALUE;
krista@2317
  1751
krista@2317
  1752
    *id_list = NULL;
krista@2317
  1753
    identity_list* ident_list = NULL;
krista@2317
  1754
krista@2326
  1755
    sqlite3_reset(session->get_identities_by_address);
krista@2326
  1756
    sqlite3_bind_text(session->get_identities_by_address, 1, address, -1, SQLITE_STATIC);
krista@2317
  1757
    int result;
krista@2317
  1758
krista@2326
  1759
    while ((result = sqlite3_step(session->get_identities_by_address)) == SQLITE_ROW) {
krista@2326
  1760
        //"select user_id, main_key_id, username, comm_type, lang,"
krista@2326
  1761
        //"   identity.flags, is_own"
krista@2317
  1762
        ident = new_identity(
krista@2317
  1763
                address,
krista@2326
  1764
                (const char *) sqlite3_column_text(session->get_identities_by_address, 1),
krista@2326
  1765
                (const char *) sqlite3_column_text(session->get_identities_by_address, 0),
krista@2326
  1766
                (const char *) sqlite3_column_text(session->get_identities_by_address, 2)
krista@2317
  1767
                );
krista@2326
  1768
        assert(ident);
krista@2317
  1769
        if (ident == NULL)
krista@2317
  1770
            return PEP_OUT_OF_MEMORY;
krista@2317
  1771
krista@2329
  1772
        ident->comm_type = PEP_ct_unknown;
krista@2329
  1773
        
krista@2317
  1774
        const char* const _lang = (const char *)
krista@2329
  1775
            sqlite3_column_text(session->get_identities_by_address, 3);
krista@2317
  1776
        if (_lang && _lang[0]) {
krista@2317
  1777
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
krista@2317
  1778
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
krista@2317
  1779
            assert(_lang[2] == 0);
krista@2326
  1780
            ident->lang[0] = _lang[0];
krista@2326
  1781
            ident->lang[1] = _lang[1];
krista@2326
  1782
            ident->lang[2] = 0;
krista@2317
  1783
        }
krista@2317
  1784
        ident->flags = (unsigned int)
krista@2329
  1785
            sqlite3_column_int(session->get_identities_by_address, 4);
krista@2329
  1786
        ident->me = (unsigned int)
krista@2326
  1787
            sqlite3_column_int(session->get_identities_by_address, 5);
krista@2317
  1788
    
krista@2317
  1789
        if (ident_list)
krista@2317
  1790
            identity_list_add(ident_list, ident);
krista@2317
  1791
        else
krista@2317
  1792
            ident_list = new_identity_list(ident);
krista@2317
  1793
    }
krista@2317
  1794
krista@2326
  1795
    sqlite3_reset(session->get_identities_by_address);
krista@2317
  1796
    
krista@2317
  1797
    *id_list = ident_list;
krista@2317
  1798
    
krista@2317
  1799
    if (!ident_list)
krista@2317
  1800
        return PEP_CANNOT_FIND_IDENTITY;
krista@2317
  1801
    
krista@2317
  1802
    return PEP_STATUS_OK;
krista@2317
  1803
}
krista@2317
  1804
krista@2308
  1805
vb@0
  1806
DYNAMIC_API PEP_STATUS set_identity(
vb@0
  1807
        PEP_SESSION session, const pEp_identity *identity
vb@0
  1808
    )
vb@0
  1809
{
roker@529
  1810
    int result;
vb@0
  1811
roker@529
  1812
    assert(session);
roker@529
  1813
    assert(identity);
roker@529
  1814
    assert(identity->address);
roker@529
  1815
    assert(identity->user_id);
roker@529
  1816
    assert(identity->username);
vb@0
  1817
krista@1223
  1818
    if (!(session && identity && identity->address &&
vb@191
  1819
                identity->user_id && identity->username))
vb@191
  1820
        return PEP_ILLEGAL_VALUE;
vb@191
  1821
krista@1791
  1822
    PEP_STATUS status = PEP_STATUS_OK;
krista@1791
  1823
    
vb@515
  1824
    bool listed;
krista@1449
  1825
krista@1449
  1826
    bool has_fpr = (identity->fpr && identity->fpr[0] != '\0');
krista@1222
  1827
    
krista@1449
  1828
    if (has_fpr) {    
krista@2433
  1829
        // blacklist check - FIXME: ENGINE-294 will remove
krista@1791
  1830
        status = blacklist_is_listed(session, identity->fpr, &listed);
krista@1223
  1831
        assert(status == PEP_STATUS_OK);
krista@1223
  1832
        if (status != PEP_STATUS_OK)
krista@1223
  1833
            return status;
vb@515
  1834
krista@1223
  1835
        if (listed)
krista@1223
  1836
            return PEP_KEY_BLACKLISTED;
krista@1223
  1837
    }
vb@515
  1838
roker@529
  1839
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
  1840
vb@1079
  1841
    if (identity->lang[0]) {
vb@1079
  1842
        assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
vb@1079
  1843
        assert(identity->lang[1] >= 'a' && identity->lang[1] <= 'z');
vb@1079
  1844
        assert(identity->lang[2] == 0);
vb@1079
  1845
    }
vb@1079
  1846
roker@529
  1847
    sqlite3_reset(session->set_person);
vb@46
  1848
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
  1849
            SQLITE_STATIC);
vb@46
  1850
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
  1851
            SQLITE_STATIC);
roker@529
  1852
    if (identity->lang[0])
vb@1081
  1853
        sqlite3_bind_text(session->set_person, 3, identity->lang, 2,
vb@0
  1854
                SQLITE_STATIC);
roker@529
  1855
    else
roker@529
  1856
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
  1857
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
  1858
                      SQLITE_STATIC);
roker@529
  1859
    result = sqlite3_step(session->set_person);
roker@529
  1860
    sqlite3_reset(session->set_person);
roker@529
  1861
    if (result != SQLITE_DONE) {
roker@529
  1862
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1863
        return PEP_CANNOT_SET_PERSON;
roker@529
  1864
    }
vb@0
  1865
krista@1449
  1866
    if (has_fpr) {
krista@1449
  1867
        sqlite3_reset(session->set_pgp_keypair);
krista@1449
  1868
        sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
krista@1449
  1869
                SQLITE_STATIC);
krista@1449
  1870
        result = sqlite3_step(session->set_pgp_keypair);
krista@1449
  1871
        sqlite3_reset(session->set_pgp_keypair);
krista@1449
  1872
        if (result != SQLITE_DONE) {
krista@1449
  1873
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
krista@1449
  1874
            return PEP_CANNOT_SET_PGP_KEYPAIR;
krista@1449
  1875
        }
roker@529
  1876
    }
vb@0
  1877
edouard@1394
  1878
    sqlite3_reset(session->set_identity);
edouard@1394
  1879
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
  1880
            SQLITE_STATIC);
edouard@1394
  1881
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
  1882
            SQLITE_STATIC);
edouard@1394
  1883
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
  1884
            SQLITE_STATIC);
edouard@1394
  1885
    sqlite3_bind_int(session->set_identity, 4, identity->flags);
krista@2303
  1886
    sqlite3_bind_int(session->set_identity, 5, identity->me);
edouard@1394
  1887
    result = sqlite3_step(session->set_identity);
edouard@1394
  1888
    sqlite3_reset(session->set_identity);
roker@529
  1889
    if (result != SQLITE_DONE) {
roker@529
  1890
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1891
        return PEP_CANNOT_SET_IDENTITY;
roker@529
  1892
    }
vb@0
  1893
krista@1449
  1894
    if (has_fpr) {
krista@1449
  1895
        sqlite3_reset(session->set_trust);
krista@1449
  1896
        sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
krista@1449
  1897
                SQLITE_STATIC);
krista@1449
  1898
        sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
krista@1449
  1899
                SQLITE_STATIC);
krista@1449
  1900
        sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
krista@1449
  1901
        result = sqlite3_step(session->set_trust);
krista@1449
  1902
        sqlite3_reset(session->set_trust);
edouard@1394
  1903
        if (result != SQLITE_DONE) {
edouard@1394
  1904
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
krista@1449
  1905
            return PEP_CANNOT_SET_TRUST;
edouard@1394
  1906
        }
edouard@1394
  1907
    }
krista@1449
  1908
    
vb@46
  1909
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
  1910
    if (result == SQLITE_OK)
roker@529
  1911
        return PEP_STATUS_OK;
roker@529
  1912
    else
roker@529
  1913
        return PEP_COMMIT_FAILED;
vb@0
  1914
}
vb@0
  1915
krista@2326
  1916
PEP_STATUS remove_fpr_as_default(PEP_SESSION session, 
krista@2326
  1917
                                 const char* fpr) 
krista@2326
  1918
{
krista@2326
  1919
    assert(fpr);
krista@2326
  1920
    
krista@2326
  1921
    if (!session || !fpr)
krista@2326
  1922
        return PEP_ILLEGAL_VALUE;
krista@2326
  1923
            
krista@2326
  1924
    sqlite3_reset(session->remove_fpr_as_default);
krista@2326
  1925
    sqlite3_bind_text(session->remove_fpr_as_default, 1, fpr, -1,
krista@2326
  1926
                      SQLITE_STATIC);
krista@2326
  1927
krista@2326
  1928
    int result = sqlite3_step(session->remove_fpr_as_default);
krista@2326
  1929
    sqlite3_reset(session->remove_fpr_as_default);
krista@2326
  1930
    
krista@2326
  1931
    if (result != SQLITE_DONE)
krista@2326
  1932
        return PEP_CANNOT_SET_IDENTITY; // misleading - could also be person
krista@2326
  1933
krista@2326
  1934
    return PEP_STATUS_OK;
krista@2326
  1935
}
krista@2326
  1936
krista@2326
  1937
krista@1799
  1938
PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
krista@1799
  1939
                                 const char* old_fpr, 
krista@1799
  1940
                                 const char* new_fpr) 
krista@1793
  1941
{
krista@1799
  1942
    assert(old_fpr);
krista@1799
  1943
    assert(new_fpr);
krista@1798
  1944
    
krista@1799
  1945
    if (!old_fpr || !new_fpr)
krista@1799
  1946
        return PEP_ILLEGAL_VALUE;
krista@1799
  1947
            
krista@1799
  1948
    sqlite3_reset(session->replace_identities_fpr);
krista@1799
  1949
    sqlite3_bind_text(session->replace_identities_fpr, 1, new_fpr, -1,
krista@1799
  1950
                      SQLITE_STATIC);
krista@1799
  1951
    sqlite3_bind_text(session->replace_identities_fpr, 2, old_fpr, -1,
krista@1799
  1952
                      SQLITE_STATIC);
krista@1799
  1953
krista@1799
  1954
    int result = sqlite3_step(session->replace_identities_fpr);
krista@1799
  1955
    sqlite3_reset(session->replace_identities_fpr);
krista@1799
  1956
    
krista@1799
  1957
    if (result != SQLITE_DONE)
krista@1799
  1958
        return PEP_CANNOT_SET_IDENTITY;
krista@1799
  1959
krista@1799
  1960
    return PEP_STATUS_OK;
krista@1799
  1961
}
krista@1799
  1962
krista@1799
  1963
PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
krista@1799
  1964
                                const char* fpr, 
krista@1799
  1965
                                PEP_comm_type comm_type)
krista@1799
  1966
{
krista@1799
  1967
    if (!fpr)
krista@1798
  1968
        return PEP_ILLEGAL_VALUE;
krista@1798
  1969
        
krista@1799
  1970
    sqlite3_reset(session->update_trust_for_fpr);
krista@1805
  1971
    sqlite3_bind_int(session->update_trust_for_fpr, 1, comm_type);
krista@1799
  1972
    sqlite3_bind_text(session->update_trust_for_fpr, 2, fpr, -1,
krista@1791
  1973
            SQLITE_STATIC);
krista@1799
  1974
    int result = sqlite3_step(session->update_trust_for_fpr);
krista@1799
  1975
    sqlite3_reset(session->update_trust_for_fpr);
krista@1791
  1976
    if (result != SQLITE_DONE) {
krista@1791
  1977
        return PEP_CANNOT_SET_TRUST;
krista@1791
  1978
    }
krista@1791
  1979
    
krista@1791
  1980
    return PEP_STATUS_OK;
krista@1791
  1981
}
krista@1791
  1982
edouard@1234
  1983
DYNAMIC_API PEP_STATUS set_device_group(
edouard@1234
  1984
        PEP_SESSION session,
edouard@1234
  1985
        const char *group_name
edouard@1234
  1986
    )
edouard@1234
  1987
{
edouard@1234
  1988
    int result;
edouard@1234
  1989
edouard@1234
  1990
    assert(session);
edouard@1234
  1991
edouard@1234
  1992
    if (!(session && group_name))
edouard@1234
  1993
        return PEP_ILLEGAL_VALUE;
edouard@1234
  1994
krista@2303
  1995
    // 1. Get own user_id
krista@2303
  1996
    char* user_id = NULL;
krista@2386
  1997
    PEP_STATUS status = get_default_own_userid(session, &user_id);
krista@2303
  1998
    
krista@2303
  1999
    // No user_id is returned in this case, no need to free;
krista@2303
  2000
    if (status != PEP_STATUS_OK)
krista@2303
  2001
        return status;
krista@2303
  2002
        
krista@2303
  2003
    // 2. Set device group
edouard@1234
  2004
    sqlite3_reset(session->set_device_group);
edouard@1574
  2005
    if(group_name){
edouard@1574
  2006
        sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
edouard@1574
  2007
                SQLITE_STATIC);
edouard@1574
  2008
    } else {
edouard@1574
  2009
        sqlite3_bind_null(session->set_device_group, 1);
edouard@1574
  2010
    }
krista@2303
  2011
    
krista@2303
  2012
    sqlite3_bind_text(session->set_device_group, 2, user_id, -1,
krista@2303
  2013
            SQLITE_STATIC);
edouard@1574
  2014
edouard@1234
  2015
    result = sqlite3_step(session->set_device_group);
edouard@1234
  2016
    sqlite3_reset(session->set_device_group);
krista@2303
  2017
    
krista@2303
  2018
    free(user_id);
krista@2303
  2019
    
edouard@1234
  2020
    if (result != SQLITE_DONE)
edouard@1234
  2021
        return PEP_CANNOT_SET_PERSON;
edouard@1234
  2022
edouard@1234
  2023
    return PEP_STATUS_OK;
edouard@1234
  2024
}
edouard@1234
  2025
edouard@1235
  2026
DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
edouard@1235
  2027
{
edouard@1235
  2028
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1235
  2029
    int result;
edouard@1235
  2030
edouard@1235
  2031
    assert(session);
edouard@1235
  2032
    assert(group_name);
edouard@1235
  2033
edouard@1235
  2034
    if (!(session && group_name))
edouard@1235
  2035
        return PEP_ILLEGAL_VALUE;
edouard@1235
  2036
krista@2303
  2037
    // 1. Get own user_id
krista@2303
  2038
    char* user_id = NULL;
krista@2386
  2039
    status = get_default_own_userid(session, &user_id);
krista@2303
  2040
    
krista@2303
  2041
    // No user_id is returned in this case, no need to free;
krista@2303
  2042
    if (status != PEP_STATUS_OK)
krista@2303
  2043
        return status;
krista@2303
  2044
krista@2303
  2045
    // 2. get device group
edouard@1235
  2046
    sqlite3_reset(session->get_device_group);
krista@2303
  2047
    sqlite3_bind_text(session->get_device_group, 1, user_id, -1,
krista@2303
  2048
            SQLITE_STATIC);
edouard@1235
  2049
edouard@1235
  2050
    result = sqlite3_step(session->get_device_group);
edouard@1235
  2051
    switch (result) {
edouard@1235
  2052
    case SQLITE_ROW: {
edouard@1574
  2053
        const char *_group_name = (const char *)sqlite3_column_text(session->get_device_group, 0);
edouard@1574
  2054
        if(_group_name){
edouard@1574
  2055
            *group_name = strdup(_group_name);
edouard@1574
  2056
                if(*group_name == NULL)
edouard@1574
  2057
                    status = PEP_OUT_OF_MEMORY;
edouard@1574
  2058
        }
edouard@1235
  2059
        break;
edouard@1235
  2060
    }
edouard@1235
  2061
 
edouard@1235
  2062
    default:
edouard@1235
  2063
        status = PEP_RECORD_NOT_FOUND;
edouard@1235
  2064
    }
edouard@1235
  2065
krista@2303
  2066
    free(user_id);
edouard@1235
  2067
    sqlite3_reset(session->get_device_group);
edouard@1235
  2068
    return status;
edouard@1235
  2069
}
edouard@1235
  2070
vb@932
  2071
DYNAMIC_API PEP_STATUS set_identity_flags(
vb@934
  2072
        PEP_SESSION session,
vb@934
  2073
        pEp_identity *identity,
vb@934
  2074
        unsigned int flags
vb@932
  2075
    )
vb@932
  2076
{
vb@932
  2077
    int result;
vb@932
  2078
vb@932
  2079
    assert(session);
vb@932
  2080
    assert(identity);
vb@932
  2081
    assert(identity->address);
vb@932
  2082
    assert(identity->user_id);
vb@932
  2083
vb@932
  2084
    if (!(session && identity && identity->address && identity->user_id))
vb@932
  2085
        return PEP_ILLEGAL_VALUE;
vb@932
  2086
vb@932
  2087
    sqlite3_reset(session->set_identity_flags);
vb@934
  2088
    sqlite3_bind_int(session->set_identity_flags, 1, flags);
vb@932
  2089
    sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
vb@932
  2090
            SQLITE_STATIC);
vb@932
  2091
    sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
krista@2326
  2092
        SQLITE_STATIC);
krista@2326
  2093
        
krista@2326
  2094
    result = sqlite3_step(session->set_identity_flags);
krista@2326
  2095
vb@932
  2096
    sqlite3_reset(session->set_identity_flags);
vb@932
  2097
    if (result != SQLITE_DONE)
vb@932
  2098
        return PEP_CANNOT_SET_IDENTITY;
vb@932
  2099
edouard@1406
  2100
    identity->flags |= flags;
vb@932
  2101
    return PEP_STATUS_OK;
vb@932
  2102
}
vb@932
  2103
edouard@1394
  2104
DYNAMIC_API PEP_STATUS unset_identity_flags(
edouard@1394
  2105
        PEP_SESSION session,
edouard@1394
  2106
        pEp_identity *identity,
edouard@1394
  2107
        unsigned int flags
edouard@1394
  2108
    )
edouard@1394
  2109
{
edouard@1394
  2110
    int result;
edouard@1394
  2111
edouard@1394
  2112
    assert(session);
edouard@1394
  2113
    assert(identity);
edouard@1394
  2114
    assert(identity->address);
edouard@1394
  2115
    assert(identity->user_id);
edouard@1394
  2116
edouard@1394
  2117
    if (!(session && identity && identity->address && identity->user_id))
edouard@1394
  2118
        return PEP_ILLEGAL_VALUE;
edouard@1394
  2119
edouard@1394
  2120
    sqlite3_reset(session->unset_identity_flags);
edouard@1394
  2121
    sqlite3_bind_int(session->unset_identity_flags, 1, flags);
edouard@1394
  2122
    sqlite3_bind_text(session->unset_identity_flags, 2, identity->address, -1,
edouard@1394
  2123
            SQLITE_STATIC);
edouard@1394
  2124
    sqlite3_bind_text(session->unset_identity_flags, 3, identity->user_id, -1,
edouard@1394
  2125
            SQLITE_STATIC);
edouard@1394
  2126
    result = sqlite3_step(session->unset_identity_flags);
edouard@1394
  2127
    sqlite3_reset(session->unset_identity_flags);
edouard@1394
  2128
    if (result != SQLITE_DONE)
edouard@1394
  2129
        return PEP_CANNOT_SET_IDENTITY;
krista@2435
  2130
        identity->flags &= ~flags;
krista@2435
  2131
edouard@1394
  2132
    return PEP_STATUS_OK;
edouard@1394
  2133
}
edouard@1394
  2134
krista@2317
  2135
krista@2317
  2136
PEP_STATUS replace_userid(PEP_SESSION session, const char* old_uid,
krista@2317
  2137
                              const char* new_uid) {
krista@2317
  2138
    assert(session);
krista@2317
  2139
    assert(old_uid);
krista@2317
  2140
    assert(new_uid);
krista@2317
  2141
    
krista@2317
  2142
    if (!session || !old_uid || !new_uid)
krista@2317
  2143
        return PEP_ILLEGAL_VALUE;
krista@2317
  2144
krista@2317
  2145
krista@2317
  2146
    int result;
krista@2317
  2147
krista@2317
  2148
    sqlite3_reset(session->replace_userid);
krista@2326
  2149
    sqlite3_bind_text(session->replace_userid, 1, new_uid, -1,
krista@2317
  2150
            SQLITE_STATIC);
krista@2326
  2151
    sqlite3_bind_text(session->replace_userid, 2, old_uid, -1,
krista@2317
  2152
            SQLITE_STATIC);
krista@2326
  2153
    result = sqlite3_step(session->replace_userid);
krista@2326
  2154
    sqlite3_reset(session->replace_userid);
krista@2317
  2155
    if (result != SQLITE_DONE)
krista@2317
  2156
        return PEP_CANNOT_SET_PERSON; // May need clearer retval
krista@2317
  2157
krista@2317
  2158
    return PEP_STATUS_OK;
krista@2317
  2159
}
krista@2317
  2160
krista@2435
  2161
PEP_STATUS refresh_userid_default_key(PEP_SESSION session, const char* user_id) {
krista@2435
  2162
    assert(session);
krista@2435
  2163
    assert(user_id);
krista@2435
  2164
    
krista@2435
  2165
    if (!session || !user_id)
krista@2435
  2166
        return PEP_ILLEGAL_VALUE;
krista@2435
  2167
krista@2435
  2168
    int result;
krista@2435
  2169
krista@2435
  2170
    sqlite3_reset(session->refresh_userid_default_key);
krista@2435
  2171
    sqlite3_bind_text(session->refresh_userid_default_key, 1, user_id, -1,
krista@2435
  2172
            SQLITE_STATIC);
krista@2435
  2173
    result = sqlite3_step(session->refresh_userid_default_key);
krista@2435
  2174
    sqlite3_reset(session->refresh_userid_default_key);
krista@2435
  2175
    if (result != SQLITE_DONE)
krista@2435
  2176
        return PEP_CANNOT_SET_PERSON;
krista@2435
  2177
krista@2435
  2178
    return PEP_STATUS_OK;    
krista@2435
  2179
}
krista@2435
  2180
krista@2433
  2181
PEP_STATUS replace_main_user_fpr(PEP_SESSION session, const char* user_id,
krista@2433
  2182
                                 const char* new_fpr) {
krista@2433
  2183
    assert(session);
krista@2433
  2184
    assert(user_id);
krista@2433
  2185
    assert(new_fpr);
krista@2433
  2186
    
krista@2433
  2187
    if (!session || !user_id || !new_fpr)
krista@2433
  2188
        return PEP_ILLEGAL_VALUE;
krista@2433
  2189
krista@2433
  2190
    int result;
krista@2433
  2191
krista@2433
  2192
    sqlite3_reset(session->replace_main_user_fpr);
krista@2433
  2193
    sqlite3_bind_text(session->replace_main_user_fpr, 1, new_fpr, -1,
krista@2433
  2194
            SQLITE_STATIC);
krista@2433
  2195
    sqlite3_bind_text(session->replace_main_user_fpr, 2, user_id, -1,
krista@2433
  2196
            SQLITE_STATIC);
krista@2433
  2197
    result = sqlite3_step(session->replace_main_user_fpr);
krista@2433
  2198
    sqlite3_reset(session->replace_main_user_fpr);
krista@2433
  2199
    if (result != SQLITE_DONE)
krista@2433
  2200
        return PEP_CANNOT_SET_PERSON;
krista@2433
  2201
krista@2433
  2202
    return PEP_STATUS_OK;
krista@2433
  2203
}
krista@2433
  2204
krista@2433
  2205
PEP_STATUS get_main_user_fpr(PEP_SESSION session, 
krista@2435
  2206
                             const char* user_id,
krista@2435
  2207
                             char** main_fpr)
krista@2433
  2208
{
krista@2433
  2209
    PEP_STATUS status = PEP_STATUS_OK;
krista@2433
  2210
    int result;
krista@2433
  2211
    
krista@2433
  2212
    assert(session);
krista@2433
  2213
    assert(user_id);
krista@2433
  2214
    assert(main_fpr);
krista@2433
  2215
    
krista@2433
  2216
    if (!(session && user_id && user_id[0] && main_fpr))
krista@2433
  2217
        return PEP_ILLEGAL_VALUE;
krista@2433
  2218
        
krista@2433
  2219
    *main_fpr = NULL;
krista@2433
  2220
    
krista@2433
  2221
    sqlite3_reset(session->get_main_user_fpr);
krista@2433
  2222
    sqlite3_bind_text(session->get_main_user_fpr, 1, user_id, -1,
krista@2433
  2223
                      SQLITE_STATIC);
krista@2433
  2224
    result = sqlite3_step(session->get_main_user_fpr);
krista@2433
  2225
    switch (result) {
krista@2433
  2226
    case SQLITE_ROW: {
krista@2433
  2227
        const char* _fpr = 
krista@2433
  2228
            (const char *) sqlite3_column_text(session->get_main_user_fpr, 0);
krista@2433
  2229
        if (_fpr)
krista@2433
  2230
            *main_fpr = strdup(_fpr);
krista@2433
  2231
        if (!(*main_fpr))
krista@2433
  2232
            status = PEP_OUT_OF_MEMORY;
krista@2433
  2233
        break;
krista@2433
  2234
    }
krista@2433
  2235
    default:
krista@2433
  2236
        status = PEP_CANNOT_FIND_PERSON;
krista@2433
  2237
    }
krista@2433
  2238
krista@2433
  2239
    sqlite3_reset(session->get_main_user_fpr);
krista@2433
  2240
    return status;
krista@2433
  2241
}
krista@2433
  2242
krista@2433
  2243
vb@357
  2244
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
  2245
        PEP_SESSION session,
vb@357
  2246
        const char *fpr
vb@357
  2247
    )
vb@357
  2248
{
roker@529
  2249
    int result;
vb@357
  2250
roker@529
  2251
    assert(session);
vb@357
  2252
    assert(fpr && fpr[0]);
vb@357
  2253
vb@357
  2254
    if (!(session && fpr && fpr[0]))
vb@357
  2255
        return PEP_ILLEGAL_VALUE;
vb@357
  2256
roker@529
  2257
    sqlite3_reset(session->mark_compromized);
vb@357
  2258
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
  2259
            SQLITE_STATIC);
vb@357
  2260
    result = sqlite3_step(session->mark_compromized);
roker@529
  2261
    sqlite3_reset(session->mark_compromized);
vb@357
  2262
vb@357
  2263
    if (result != SQLITE_DONE)
Edouard@584
  2264
        return PEP_CANNOT_SET_TRUST;
vb@357
  2265
vb@357
  2266
    return PEP_STATUS_OK;
vb@357
  2267
}
vb@357
  2268
vb@0
  2269
void pEp_free(void *p)
vb@0
  2270
{
vb@0
  2271
    free(p);
vb@0
  2272
}
vb@0
  2273
krista@2433
  2274
PEP_STATUS set_trust(PEP_SESSION session, 
krista@2433
  2275
                     const char* user_id,
krista@2433
  2276
                     const char* fpr, 
krista@2433
  2277
                     PEP_comm_type comm_type) 
krista@2433
  2278
{
krista@2433
  2279
    assert(session);
krista@2433
  2280
    assert(user_id);
krista@2433
  2281
    assert(fpr);
krista@2433
  2282
    
krista@2433
  2283
    if (!session || !user_id || user_id[0] == '\0' || !fpr || fpr[0] == '\0')
krista@2433
  2284
        return PEP_ILLEGAL_VALUE;
krista@2433
  2285
        
krista@2433
  2286
    int result;
krista@2433
  2287
                
krista@2433
  2288
    sqlite3_reset(session->set_trust);
krista@2433
  2289
    sqlite3_bind_text(session->set_trust, 1, user_id, -1,
krista@2433
  2290
            SQLITE_STATIC);
krista@2433
  2291
    sqlite3_bind_text(session->set_trust, 2, fpr, -1,
krista@2433
  2292
            SQLITE_STATIC);
krista@2433
  2293
    sqlite3_bind_int(session->set_trust, 3, comm_type);
krista@2433
  2294
    result = sqlite3_step(session->set_trust);
krista@2433
  2295
    assert(result == SQLITE_DONE);
krista@2433
  2296
    sqlite3_reset(session->set_trust);
krista@2433
  2297
    if (result != SQLITE_DONE)
krista@2433
  2298
        return PEP_CANNOT_SET_TRUST;
krista@2433
  2299
krista@2433
  2300
    return PEP_STATUS_OK;
krista@2433
  2301
}
krista@2433
  2302
krista@2433
  2303
vb@8
  2304
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
  2305
{
vb@8
  2306
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
  2307
    int result;
vb@8
  2308
krista@2371
  2309
    // We need to be able to test that we break correctly without shutting
krista@2371
  2310
    // asserts off everywhere.
krista@2371
  2311
    // assert(session);
krista@2371
  2312
    // assert(identity);
krista@2371
  2313
    // assert(identity->user_id);
krista@2371
  2314
    // assert(identity->user_id[0]);
krista@2371
  2315
    // assert(identity->fpr);
krista@2371
  2316
    // assert(identity->fpr[0]);
vb@8
  2317
vb@191
  2318
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
  2319
                identity->fpr && identity->fpr[0]))
vb@191
  2320
        return PEP_ILLEGAL_VALUE;
vb@191
  2321
vb@8
  2322
    identity->comm_type = PEP_ct_unknown;
vb@8
  2323
vb@46
  2324
    sqlite3_reset(session->get_trust);
vb@951
  2325
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
vb@951
  2326
            SQLITE_STATIC);
vb@46
  2327
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
  2328
vb@46
  2329
    result = sqlite3_step(session->get_trust);
vb@8
  2330
    switch (result) {
vb@8
  2331
    case SQLITE_ROW: {
vb@951
  2332
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
vb@951
  2333
                0);
vb@8
  2334
        identity->comm_type = comm_type;
vb@8
  2335
        break;
vb@8
  2336
    }
vb@8
  2337
 
vb@8
  2338
    default:
vb@8
  2339
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
  2340
    }
vb@8
  2341
vb@46
  2342
    sqlite3_reset(session->get_trust);
vb@8
  2343
    return status;
vb@9
  2344
}
vb@9
  2345
vb@251
  2346
DYNAMIC_API PEP_STATUS least_trust(
vb@251
  2347
        PEP_SESSION session,
vb@251
  2348
        const char *fpr,
vb@251
  2349
        PEP_comm_type *comm_type
vb@251
  2350
    )
vb@251
  2351
{
vb@251
  2352
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
  2353
    int result;
vb@251
  2354
vb@251
  2355
    assert(session);
vb@251
  2356
    assert(fpr);
vb@251
  2357
    assert(comm_type);
vb@251
  2358
vb@251
  2359
    if (!(session && fpr && comm_type))
vb@251
  2360
        return PEP_ILLEGAL_VALUE;
vb@251
  2361
vb@632
  2362
    *comm_type = PEP_ct_unknown;
vb@632
  2363
vb@251
  2364
    sqlite3_reset(session->least_trust);
vb@251
  2365
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  2366
vb@251
  2367
    result = sqlite3_step(session->least_trust);
vb@251
  2368
    switch (result) {
vb@251
  2369
        case SQLITE_ROW: {
vb@259
  2370
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  2371
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  2372
            break;
vb@251
  2373
        }
vb@251
  2374
        default:
edouard@1632
  2375
            // never reached because of sql min()
vb@251
  2376
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  2377
    }
vb@251
  2378
vb@251
  2379
    sqlite3_reset(session->least_trust);
vb@251
  2380
    return status;
vb@251
  2381
}
vb@251
  2382
vb@24
  2383
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  2384
    PEP_SESSION session, const char *ctext, size_t csize,
krista@1397
  2385
    const char *dsigtext, size_t dsigsize,
vb@24
  2386
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  2387
    )
vb@24
  2388
{
vb@191
  2389
    assert(session);
vb@191
  2390
    assert(ctext);
vb@191
  2391
    assert(csize);
vb@191
  2392
    assert(ptext);
vb@191
  2393
    assert(psize);
vb@191
  2394
    assert(keylist);
vb@191
  2395
vb@939
  2396
    if (!(session && ctext && csize && ptext && psize && keylist))
vb@191
  2397
        return PEP_ILLEGAL_VALUE;
vb@191
  2398
vb@939
  2399
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
krista@1397
  2400
            session, ctext, csize, dsigtext, dsigsize, ptext, psize, keylist);
vb@24
  2401
}
vb@24
  2402
vb@24
  2403
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  2404
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  2405
    size_t psize, char **ctext, size_t *csize
vb@24
  2406
    )
vb@24
  2407
{
vb@191
  2408
    assert(session);
vb@191
  2409
    assert(keylist);
vb@191
  2410
    assert(ptext);
vb@191
  2411
    assert(psize);
vb@191
  2412
    assert(ctext);
vb@191
  2413
    assert(csize);
vb@191
  2414
vb@191
  2415
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  2416
        return PEP_ILLEGAL_VALUE;
vb@191
  2417
vb@951
  2418
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
vb@951
  2419
            keylist, ptext, psize, ctext, csize);
vb@24
  2420
}
vb@24
  2421
krista@1639
  2422
PEP_STATUS encrypt_only(
krista@1639
  2423
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  2424
    size_t psize, char **ctext, size_t *csize
krista@1639
  2425
    )
krista@1639
  2426
{
krista@1639
  2427
    assert(session);
krista@1639
  2428
    assert(keylist);
krista@1639
  2429
    assert(ptext);
krista@1639
  2430
    assert(psize);
krista@1639
  2431
    assert(ctext);
krista@1639
  2432
    assert(csize);
krista@1639
  2433
krista@1639
  2434
    if (!(session && keylist && ptext && psize && ctext && csize))
krista@1639
  2435
        return PEP_ILLEGAL_VALUE;
krista@1639
  2436
krista@1639
  2437
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_only(session,
krista@1639
  2438
            keylist, ptext, psize, ctext, csize);
krista@1639
  2439
}
krista@1639
  2440
krista@1639
  2441
vb@24
  2442
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  2443
    PEP_SESSION session, const char *text, size_t size,
vb@24
  2444
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  2445
    )
vb@24
  2446
{
vb@191
  2447
    assert(session);
vb@191
  2448
    assert(text);
vb@191
  2449
    assert(size);
vb@191
  2450
    assert(signature);
vb@191
  2451
    assert(sig_size);
vb@191
  2452
    assert(keylist);
vb@191
  2453
vb@191
  2454
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  2455
        return PEP_ILLEGAL_VALUE;
vb@191
  2456
vb@951
  2457
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
vb@951
  2458
            size, signature, sig_size, keylist);
vb@24
  2459
}
vb@24
  2460
vb@24
  2461
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  2462
{
vb@191
  2463
    assert(session);
vb@191
  2464
    assert(fpr);
vb@191
  2465
vb@191
  2466
    if (!(session && fpr))
vb@191
  2467
        return PEP_ILLEGAL_VALUE;
vb@191
  2468
vb@46
  2469
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  2470
}
vb@24
  2471
vb@24
  2472
DYNAMIC_API PEP_STATUS export_key(
vb@46
  2473
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  2474
    )
vb@24
  2475
{
vb@191
  2476
    assert(session);
vb@191
  2477
    assert(fpr);
vb@191
  2478
    assert(key_data);
vb@191
  2479
    assert(size);
vb@191
  2480
vb@191
  2481
    if (!(session && fpr && key_data && size))
vb@191
  2482
        return PEP_ILLEGAL_VALUE;
vb@191
  2483
vb@951
  2484
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@1103
  2485
            key_data, size, false);
vb@1103
  2486
}
vb@1103
  2487
vb@1133
  2488
DYNAMIC_API PEP_STATUS export_secrect_key(
vb@1103
  2489
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@1103
  2490
    )
vb@1103
  2491
{
vb@1103
  2492
    assert(session);
vb@1103
  2493
    assert(fpr);
vb@1103
  2494
    assert(key_data);
vb@1103
  2495
    assert(size);
vb@1103
  2496
vb@1103
  2497
    if (!(session && fpr && key_data && size))
vb@1103
  2498
        return PEP_ILLEGAL_VALUE;
vb@1103
  2499
vb@1103
  2500
    // don't accept key IDs but full fingerprints only
vb@1103
  2501
    if (strlen(fpr) < 16)
vb@1103
  2502
        return PEP_ILLEGAL_VALUE;
vb@1103
  2503