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