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