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