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