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