src/pEpEngine.c
author Edouard Tisserant
Wed, 25 May 2016 13:44:44 +0200
changeset 684 23c6795d6de1
parent 659 37bdde68b914
child 687 0b3214f5331a
permissions -rw-r--r--
Trust table now use primary key instead of separate index
vb@125
     1
#include "pEp_internal.h"
vb@98
     2
#include "dynamic_api.h"
vb@28
     3
#include "cryptotech.h"
vb@28
     4
#include "transport.h"
vb@515
     5
#include "blacklist.h"
vb@0
     6
vb@221
     7
static int init_count = -1;
vb@62
     8
vb@0
     9
DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
vb@0
    10
{
vb@65
    11
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
    12
    int int_result;
roker@529
    13
    static const char *sql_log;
roker@529
    14
    static const char *sql_trustword;
roker@529
    15
    static const char *sql_get_identity;
roker@529
    16
    static const char *sql_set_person;
roker@529
    17
    static const char *sql_set_pgp_keypair;
roker@529
    18
    static const char *sql_set_identity;
roker@529
    19
    static const char *sql_set_trust;
vb@62
    20
    static const char *sql_get_trust;
vb@251
    21
    static const char *sql_least_trust;
vb@357
    22
    static const char *sql_mark_as_compromized;
vb@450
    23
    static const char *sql_crashdump;
vb@458
    24
    static const char *sql_languagelist;
vb@458
    25
    static const char *sql_i18n_token;
fdik@494
    26
fdik@494
    27
    // blacklist
fdik@494
    28
    static const char *sql_blacklist_add;
fdik@494
    29
    static const char *sql_blacklist_delete;
fdik@494
    30
    static const char *sql_blacklist_is_listed;
fdik@494
    31
    static const char *sql_blacklist_retrieve;
vb@450
    32
Edouard@584
    33
    // Own keys
Edouard@584
    34
    static const char *sql_own_key_is_listed;
Edouard@584
    35
    static const char *sql_own_key_retrieve;
Edouard@584
    36
vb@633
    37
    static const char *sql_sequence_value1;
vb@633
    38
    static const char *sql_sequence_value2;
vb@632
    39
vb@62
    40
    bool in_first = false;
vb@8
    41
vb@62
    42
    assert(sqlite3_threadsafe());
vb@62
    43
    if (!sqlite3_threadsafe())
vb@62
    44
        return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
vb@62
    45
vb@62
    46
    // a little race condition - but still a race condition
vb@113
    47
    // mitigated by calling caveat (see documentation)
vb@62
    48
vb@62
    49
    ++init_count;
vb@67
    50
    if (init_count == 0)
vb@62
    51
        in_first = true;
vb@0
    52
roker@529
    53
    assert(session);
vb@191
    54
    if (session == NULL)
vb@191
    55
        return PEP_ILLEGAL_VALUE;
vb@191
    56
roker@529
    57
    *session = NULL;
vb@0
    58
vb@107
    59
    pEpSession *_session = calloc(1, sizeof(pEpSession));
roker@529
    60
    assert(_session);
roker@529
    61
    if (_session == NULL)
roker@529
    62
        goto enomem;
vb@62
    63
roker@529
    64
    _session->version = PEP_ENGINE_VERSION;
vb@0
    65
vb@0
    66
    assert(LOCAL_DB);
vb@0
    67
    if (LOCAL_DB == NULL) {
vb@65
    68
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    69
        goto pep_error;
vb@0
    70
    }
vb@0
    71
roker@529
    72
    int_result = sqlite3_open_v2(
roker@529
    73
            LOCAL_DB,
roker@529
    74
            &_session->db,
roker@529
    75
            SQLITE_OPEN_READWRITE
roker@529
    76
                | SQLITE_OPEN_CREATE
roker@529
    77
                | SQLITE_OPEN_FULLMUTEX
roker@529
    78
                | SQLITE_OPEN_PRIVATECACHE,
roker@529
    79
            NULL 
roker@529
    80
        );
vb@0
    81
roker@529
    82
    if (int_result != SQLITE_OK) {
roker@529
    83
        status = PEP_INIT_CANNOT_OPEN_DB;
vb@65
    84
        goto pep_error;
roker@529
    85
    }
vb@0
    86
roker@529
    87
    sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
vb@0
    88
vb@0
    89
    assert(SYSTEM_DB);
vb@0
    90
    if (SYSTEM_DB == NULL) {
roker@529
    91
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
    92
        goto pep_error;
vb@0
    93
    }
vb@0
    94
roker@529
    95
    int_result = sqlite3_open_v2(
roker@529
    96
            SYSTEM_DB, &_session->system_db,
roker@529
    97
            SQLITE_OPEN_READONLY
roker@529
    98
                | SQLITE_OPEN_FULLMUTEX
roker@529
    99
                | SQLITE_OPEN_SHAREDCACHE,
roker@529
   100
            NULL
roker@529
   101
        );
vb@0
   102
roker@529
   103
    if (int_result != SQLITE_OK) {
roker@529
   104
        status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
vb@65
   105
        goto pep_error;
roker@529
   106
    }
vb@0
   107
roker@529
   108
    sqlite3_busy_timeout(_session->system_db, 1000);
vb@0
   109
vb@62
   110
    if (in_first) {
vb@62
   111
        int_result = sqlite3_exec(
vb@62
   112
            _session->db,
vb@456
   113
                "create table if not exists version_info (\n"
vb@456
   114
                "   id integer primary key,\n"
vb@456
   115
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   116
                "   version text,\n"
vb@456
   117
                "   comment text\n"
vb@456
   118
                ");\n"
vb@456
   119
                "create table if not exists log (\n"
vb@456
   120
                "   timestamp integer default (datetime('now')) ,\n"
vb@456
   121
                "   title text not null,\n"
vb@456
   122
                "   entity text not null,\n"
vb@456
   123
                "   description text,\n"
vb@456
   124
                "   comment text\n"
vb@456
   125
                ");\n"
vb@456
   126
                "create index if not exists log_timestamp on log (\n"
vb@456
   127
                "   timestamp\n"
vb@456
   128
                ");\n"
vb@456
   129
                "create table if not exists pgp_keypair (\n"
vb@456
   130
                "   fpr text primary key,\n"
vb@456
   131
                "   public_id text unique,\n"
vb@456
   132
                "   private_id text,\n"
vb@456
   133
                "   created integer,\n"
vb@456
   134
                "   expires integer,\n"
vb@456
   135
                "   comment text\n"
vb@456
   136
                ");\n"
vb@456
   137
                "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
vb@456
   138
                "   expires\n"
vb@456
   139
                ");\n"
vb@456
   140
                "create table if not exists person (\n"
vb@456
   141
                "   id text primary key,\n"
vb@456
   142
                "   username text not null,\n"
vb@456
   143
                "   main_key_id text\n"
vb@456
   144
                "       references pgp_keypair (fpr)\n"
vb@456
   145
                "       on delete set null,\n"
vb@456
   146
                "   lang text,\n"
vb@456
   147
                "   comment text\n"
vb@456
   148
                ");\n"
vb@456
   149
                "create table if not exists identity (\n"
Edouard@559
   150
                "   address text,\n"
vb@456
   151
                "   user_id text\n"
vb@456
   152
                "       references person (id)\n"
vb@456
   153
                "       on delete cascade,\n"
vb@456
   154
                "   main_key_id text\n"
vb@456
   155
                "       references pgp_keypair (fpr)\n"
vb@456
   156
                "       on delete set null,\n"
Edouard@559
   157
                "   comment text,\n"
Edouard@559
   158
                "   primary key (address, user_id)\n"
vb@456
   159
                ");\n"
vb@456
   160
                "create table if not exists trust (\n"
vb@456
   161
                "   user_id text not null\n"
vb@456
   162
                "       references person (id)\n"
vb@456
   163
                "       on delete cascade,\n"
vb@456
   164
                "   pgp_keypair_fpr text not null\n"
vb@456
   165
                "       references pgp_keypair (fpr)\n"
vb@456
   166
                "       on delete cascade,\n"
vb@456
   167
                "   comm_type integer not null,\n"
Edouard@684
   168
                "   comment text,\n"
Edouard@684
   169
                "   primary key (user_id, pgp_keypair_fpr)\n"
vb@456
   170
                ");\n"
fdik@494
   171
                // blacklist
vb@456
   172
                "create table if not exists blacklist_keys (\n"
vb@456
   173
                "   fpr text primary key\n"
vb@456
   174
                ");\n"
vb@632
   175
                // sequences
vb@632
   176
                "create table if not exists sequences(\n"
vb@632
   177
                "   name text primary key,\n"
vb@632
   178
                "   value integer default 0\n"
vb@632
   179
                ");\n"
vb@456
   180
                ,
vb@62
   181
            NULL,
vb@62
   182
            NULL,
vb@62
   183
            NULL
vb@62
   184
        );
vb@62
   185
        assert(int_result == SQLITE_OK);
vb@0
   186
vb@62
   187
        int_result = sqlite3_exec(
vb@62
   188
            _session->db,
fdik@494
   189
            "insert or replace into version_info (id, version) values (1, '1.1');",
vb@62
   190
            NULL,
vb@62
   191
            NULL,
vb@62
   192
            NULL
vb@62
   193
        );
vb@62
   194
        assert(int_result == SQLITE_OK);
vb@0
   195
vb@62
   196
        sql_log = "insert into log (title, entity, description, comment)"
vb@62
   197
                  "values (?1, ?2, ?3, ?4);";
vb@62
   198
Edouard@584
   199
        sql_get_identity =  "select fpr, username, comm_type, lang"
vb@62
   200
                            "   from identity"
vb@62
   201
                            "   join person on id = identity.user_id"
vb@62
   202
                            "   join pgp_keypair on fpr = identity.main_key_id"
vb@62
   203
                            "   join trust on id = trust.user_id"
vb@62
   204
                            "       and pgp_keypair_fpr = identity.main_key_id"
Edouard@559
   205
                            "   where address = ?1 and identity.user_id = ?2;";
Edouard@559
   206
vb@233
   207
        sql_trustword = "select id, word from wordlist where lang = lower(?1) "
vb@62
   208
                       "and id = ?2 ;";
vb@62
   209
Edouard@641
   210
        // Set person, but if already exist, only update.
Edouard@641
   211
        // if main_key_id already set, don't touch.
Edouard@641
   212
        sql_set_person = "insert or replace into person (id, username, lang, main_key_id)"
Edouard@641
   213
                         "  values (?1, ?2, ?3,"
Edouard@641
   214
                         "    (select coalesce((select main_key_id from person "
Edouard@644
   215
                         "      where id = ?1), upper(replace(?4,' ',''))))) ;";
vb@62
   216
vb@62
   217
        sql_set_pgp_keypair = "insert or replace into pgp_keypair (fpr) "
Edouard@559
   218
                              "values (upper(replace(?1,' ',''))) ;";
vb@62
   219
vb@62
   220
        sql_set_identity = "insert or replace into identity (address, main_key_id, "
Edouard@559
   221
                           "user_id) values (?1, upper(replace(?2,' ','')), ?3) ;";
vb@62
   222
vb@62
   223
        sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
Edouard@559
   224
                        "values (?1, upper(replace(?2,' ','')), ?3) ;";
vb@62
   225
Edouard@559
   226
        sql_get_trust = "select comm_type from trust where user_id = ?1 "
Edouard@559
   227
                        "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
vb@251
   228
Edouard@559
   229
        sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@357
   230
vb@457
   231
        sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
Edouard@559
   232
                                  " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
vb@450
   233
vb@457
   234
        sql_crashdump = "select timestamp, title, entity, description, comment"
vb@457
   235
                        " from log order by timestamp desc limit ?1 ;";
vb@458
   236
vb@619
   237
        sql_languagelist = "select i18n_language.lang, name, phrase from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
vb@458
   238
vb@461
   239
        sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
vb@482
   240
fdik@494
   241
        // blacklist
fdik@494
   242
Edouard@559
   243
        sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
Edouard@559
   244
                            "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
Edouard@559
   245
                            "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
vb@514
   246
Edouard@559
   247
        sql_blacklist_delete = "delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   248
Edouard@559
   249
        sql_blacklist_is_listed = "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
vb@514
   250
fdik@494
   251
        sql_blacklist_retrieve = "select * from blacklist_keys ;";
Edouard@584
   252
        
Edouard@584
   253
        // Own keys
Edouard@584
   254
        
Edouard@658
   255
        sql_own_key_is_listed =
Edouard@658
   256
                                "select count(*) from ("
Edouard@658
   257
                                " select main_key_id from person "
Edouard@658
   258
                                "   where main_key_id = upper(replace(?1,' ',''))"
Edouard@658
   259
                                "    and id = '" PEP_OWN_USERID "' "
Edouard@658
   260
                                " union "
Edouard@658
   261
                                "  select main_key_id from identity "
Edouard@658
   262
                                "   where main_key_id = upper(replace(?1,' ',''))"
Edouard@658
   263
                                "    and user_id = '" PEP_OWN_USERID "' );";
Edouard@658
   264
Edouard@658
   265
        sql_own_key_retrieve = "select main_key_id from person "
Edouard@658
   266
                               "  where main_key_id is not null"
Edouard@658
   267
                               "   and id = '" PEP_OWN_USERID "' "
Edouard@658
   268
                               "union "
Edouard@658
   269
                               " select main_key_id from identity "
Edouard@658
   270
                               "  where main_key_id is not null"
Edouard@658
   271
                               "   and user_id = '" PEP_OWN_USERID "' ;";
Edouard@584
   272
        
vb@633
   273
        sql_sequence_value1 = "insert or replace into sequences (name, value) "
vb@633
   274
                              "values (?1, "
vb@633
   275
                              "(select coalesce((select value + 1 from sequences "
vb@633
   276
                              "where name = ?1), 1 ))) ; ";
vb@633
   277
        sql_sequence_value2 = "select value from sequences where name = ?1 ;";
vb@62
   278
    }
vb@62
   279
Edouard@417
   280
    int_result = sqlite3_prepare_v2(_session->db, sql_log, (int)strlen(sql_log),
vb@0
   281
            &_session->log, NULL);
roker@529
   282
    assert(int_result == SQLITE_OK);
vb@0
   283
vb@233
   284
    int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
Edouard@417
   285
            (int)strlen(sql_trustword), &_session->trustword, NULL);
roker@529
   286
    assert(int_result == SQLITE_OK);
vb@0
   287
vb@0
   288
    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
Edouard@417
   289
            (int)strlen(sql_get_identity), &_session->get_identity, NULL);
roker@529
   290
    assert(int_result == SQLITE_OK);
vb@0
   291
vb@0
   292
    int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
Edouard@417
   293
            (int)strlen(sql_set_person), &_session->set_person, NULL);
vb@0
   294
    assert(int_result == SQLITE_OK);
vb@62
   295
vb@0
   296
    int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
Edouard@417
   297
            (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair, NULL);
roker@529
   298
    assert(int_result == SQLITE_OK);
vb@62
   299
vb@0
   300
    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
Edouard@417
   301
            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
roker@529
   302
    assert(int_result == SQLITE_OK);
vb@62
   303
vb@0
   304
    int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
Edouard@417
   305
            (int)strlen(sql_set_trust), &_session->set_trust, NULL);
roker@529
   306
    assert(int_result == SQLITE_OK);
vb@62
   307
vb@8
   308
    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
Edouard@417
   309
            (int)strlen(sql_get_trust), &_session->get_trust, NULL);
vb@8
   310
    assert(int_result == SQLITE_OK);
vb@0
   311
vb@251
   312
    int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
Edouard@417
   313
            (int)strlen(sql_least_trust), &_session->least_trust, NULL);
vb@251
   314
    assert(int_result == SQLITE_OK);
vb@251
   315
vb@357
   316
    int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromized,
Edouard@417
   317
            (int)strlen(sql_mark_as_compromized), &_session->mark_compromized, NULL);
vb@357
   318
    assert(int_result == SQLITE_OK);
vb@357
   319
vb@453
   320
    int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
vb@453
   321
            (int)strlen(sql_crashdump), &_session->crashdump, NULL);
vb@453
   322
    assert(int_result == SQLITE_OK);
vb@453
   323
vb@458
   324
    int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
vb@458
   325
            (int)strlen(sql_languagelist), &_session->languagelist, NULL);
roker@529
   326
    assert(int_result == SQLITE_OK);
vb@458
   327
vb@458
   328
    int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
vb@458
   329
            (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
roker@529
   330
    assert(int_result == SQLITE_OK);
vb@458
   331
fdik@494
   332
    // blacklist
fdik@494
   333
fdik@494
   334
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
fdik@494
   335
            (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
fdik@494
   336
    assert(int_result == SQLITE_OK);
fdik@494
   337
fdik@494
   338
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
fdik@494
   339
            (int)strlen(sql_blacklist_delete), &_session->blacklist_delete, NULL);
fdik@494
   340
    assert(int_result == SQLITE_OK);
fdik@494
   341
fdik@494
   342
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
fdik@494
   343
            (int)strlen(sql_blacklist_is_listed), &_session->blacklist_is_listed, NULL);
fdik@494
   344
    assert(int_result == SQLITE_OK);
fdik@494
   345
fdik@494
   346
    int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
fdik@494
   347
            (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve, NULL);
vb@482
   348
    assert(int_result == SQLITE_OK);
vb@482
   349
Edouard@584
   350
    // Own keys
Edouard@584
   351
    
Edouard@584
   352
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
vb@633
   353
            (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed, NULL);
Edouard@584
   354
    assert(int_result == SQLITE_OK);
Edouard@584
   355
    
Edouard@584
   356
    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
vb@633
   357
            (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve, NULL);
Edouard@584
   358
    assert(int_result == SQLITE_OK);
vb@633
   359
 
vb@633
   360
    // Sequence
vb@633
   361
vb@633
   362
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
vb@633
   363
            (int)strlen(sql_sequence_value1), &_session->sequence_value1, NULL);
vb@633
   364
    assert(int_result == SQLITE_OK);
vb@633
   365
vb@633
   366
    int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
vb@633
   367
            (int)strlen(sql_sequence_value2), &_session->sequence_value2, NULL);
vb@633
   368
    assert(int_result == SQLITE_OK);
vb@633
   369
vb@65
   370
    status = init_cryptotech(_session, in_first);
vb@65
   371
    if (status != PEP_STATUS_OK)
vb@65
   372
        goto pep_error;
vb@0
   373
vb@65
   374
    status = init_transport_system(_session, in_first);
vb@65
   375
    if (status != PEP_STATUS_OK)
vb@65
   376
        goto pep_error;
vb@62
   377
vb@65
   378
    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
vb@65
   379
    if (status != PEP_STATUS_OK)
vb@65
   380
        goto pep_error;
vb@65
   381
vb@464
   382
    // runtime config
vb@464
   383
vb@464
   384
    _session->passive_mode = false;
vb@464
   385
    _session->unencrypted_subject = false;
vb@464
   386
roker@529
   387
    *session = _session;
roker@529
   388
    return PEP_STATUS_OK;
vb@65
   389
vb@65
   390
enomem:
vb@65
   391
    status = PEP_OUT_OF_MEMORY;
vb@65
   392
vb@65
   393
pep_error:
vb@65
   394
    release(_session);
vb@65
   395
    return status;
vb@0
   396
}
vb@0
   397
vb@0
   398
DYNAMIC_API void release(PEP_SESSION session)
vb@0
   399
{
vb@62
   400
    bool out_last = false;
vb@62
   401
vb@62
   402
    assert(init_count >= 0);
roker@529
   403
    assert(session);
vb@0
   404
Edouard@385
   405
    if (!((init_count >= 0) && session))
vb@191
   406
        return;
vb@191
   407
vb@62
   408
    // a small race condition but still a race condition
vb@113
   409
    // mitigated by calling caveat (see documentation)
vb@62
   410
vb@62
   411
    if (init_count == 0)
vb@62
   412
        out_last = true;
vb@62
   413
    --init_count;
vb@62
   414
roker@529
   415
    if (session) {
roker@529
   416
        if (session->db) {
vb@517
   417
            if (session->log)
vb@517
   418
                sqlite3_finalize(session->log);
vb@233
   419
            if (session->trustword)
vb@233
   420
                sqlite3_finalize(session->trustword);
vb@65
   421
            if (session->get_identity)
vb@65
   422
                sqlite3_finalize(session->get_identity);
vb@65
   423
            if (session->set_person)
vb@65
   424
                sqlite3_finalize(session->set_person);
vb@65
   425
            if (session->set_pgp_keypair)
vb@65
   426
                sqlite3_finalize(session->set_pgp_keypair);
vb@517
   427
            if (session->set_identity)
vb@517
   428
                sqlite3_finalize(session->set_identity);
vb@65
   429
            if (session->set_trust)
vb@65
   430
                sqlite3_finalize(session->set_trust);
vb@65
   431
            if (session->get_trust)
vb@65
   432
                sqlite3_finalize(session->get_trust);
vb@251
   433
            if (session->least_trust)
vb@251
   434
                sqlite3_finalize(session->least_trust);
vb@517
   435
            if (session->mark_compromized)
vb@517
   436
                sqlite3_finalize(session->mark_compromized);
vb@517
   437
            if (session->crashdump)
vb@517
   438
                sqlite3_finalize(session->crashdump);
vb@517
   439
            if (session->languagelist)
vb@517
   440
                sqlite3_finalize(session->languagelist);
vb@517
   441
            if (session->i18n_token)
vb@517
   442
                sqlite3_finalize(session->i18n_token);
vb@517
   443
            if (session->blacklist_add)
vb@517
   444
                sqlite3_finalize(session->blacklist_add);
vb@517
   445
            if (session->blacklist_delete)
vb@517
   446
                sqlite3_finalize(session->blacklist_delete);
vb@517
   447
            if (session->blacklist_is_listed)
vb@517
   448
                sqlite3_finalize(session->blacklist_is_listed);
vb@517
   449
            if (session->blacklist_retrieve)
vb@517
   450
                sqlite3_finalize(session->blacklist_retrieve);
vb@26
   451
vb@65
   452
            if (session->db)
vb@65
   453
                sqlite3_close_v2(session->db);
vb@65
   454
            if (session->system_db)
vb@65
   455
                sqlite3_close_v2(session->system_db);
roker@529
   456
        }
vb@65
   457
vb@65
   458
        release_transport_system(session, out_last);
vb@65
   459
        release_cryptotech(session, out_last);
vb@65
   460
vb@65
   461
        free(session);
vb@62
   462
    }
vb@0
   463
}
vb@0
   464
vb@467
   465
DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
vb@464
   466
{
vb@464
   467
    assert(session);
vb@467
   468
    session->passive_mode = enable;
vb@464
   469
}
vb@464
   470
vb@467
   471
DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
vb@464
   472
{
vb@464
   473
    assert(session);
vb@467
   474
    session->unencrypted_subject = enable;
vb@464
   475
}
vb@464
   476
vb@0
   477
DYNAMIC_API PEP_STATUS log_event(
vb@450
   478
        PEP_SESSION session,
vb@451
   479
        const char *title,
vb@451
   480
        const char *entity,
vb@451
   481
        const char *description,
vb@451
   482
        const char *comment
vb@0
   483
    )
vb@0
   484
{
roker@529
   485
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   486
    int result;
vb@0
   487
roker@529
   488
    assert(session);
roker@529
   489
    assert(title);
roker@529
   490
    assert(entity);
vb@0
   491
vb@191
   492
    if (!(session && title && entity))
vb@191
   493
        return PEP_ILLEGAL_VALUE;
vb@191
   494
roker@529
   495
    sqlite3_reset(session->log);
roker@529
   496
    sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
roker@529
   497
    sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
roker@529
   498
    if (description)
vb@46
   499
        sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
roker@529
   500
    else
roker@529
   501
        sqlite3_bind_null(session->log, 3);
roker@529
   502
    if (comment)
roker@529
   503
        sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
roker@529
   504
    else
roker@529
   505
        sqlite3_bind_null(session->log, 4);
roker@529
   506
    do {
roker@529
   507
        result = sqlite3_step(session->log);
roker@529
   508
        assert(result == SQLITE_DONE || result == SQLITE_BUSY);
roker@529
   509
        if (result != SQLITE_DONE && result != SQLITE_BUSY)
roker@529
   510
            status = PEP_UNKNOWN_ERROR;
roker@529
   511
    } while (result == SQLITE_BUSY);
roker@529
   512
    sqlite3_reset(session->log);
vb@0
   513
roker@529
   514
    return status;
vb@0
   515
}
vb@0
   516
vb@233
   517
DYNAMIC_API PEP_STATUS trustword(
vb@0
   518
            PEP_SESSION session, uint16_t value, const char *lang,
vb@0
   519
            char **word, size_t *wsize
vb@0
   520
        )
vb@0
   521
{
roker@529
   522
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   523
    int result;
vb@0
   524
roker@529
   525
    assert(session);
roker@529
   526
    assert(word);
roker@529
   527
    assert(wsize);
vb@0
   528
vb@191
   529
    if (!(session && word && wsize))
vb@191
   530
        return PEP_ILLEGAL_VALUE;
vb@191
   531
roker@529
   532
    *word = NULL;
roker@529
   533
    *wsize = 0;
vb@0
   534
roker@529
   535
    if (lang == NULL)
roker@529
   536
        lang = "en";
vb@0
   537
roker@529
   538
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   539
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   540
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   541
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   542
    assert(lang[2] == 0);
vb@0
   543
roker@529
   544
    sqlite3_reset(session->trustword);
vb@233
   545
    sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
roker@529
   546
    sqlite3_bind_int(session->trustword, 2, value);
vb@0
   547
roker@529
   548
    result = sqlite3_step(session->trustword);
roker@529
   549
    if (result == SQLITE_ROW) {
vb@233
   550
        *word = strdup((const char *) sqlite3_column_text(session->trustword,
vb@0
   551
                    1));
roker@529
   552
        if (*word)
vb@233
   553
            *wsize = sqlite3_column_bytes(session->trustword, 1);
roker@529
   554
        else
roker@529
   555
            status = PEP_TRUSTWORD_NOT_FOUND;
roker@529
   556
    } else
roker@529
   557
        status = PEP_TRUSTWORD_NOT_FOUND;
vb@0
   558
roker@529
   559
    sqlite3_reset(session->trustword);
roker@529
   560
    return status;
vb@0
   561
}
vb@0
   562
vb@233
   563
DYNAMIC_API PEP_STATUS trustwords(
vb@0
   564
        PEP_SESSION session, const char *fingerprint, const char *lang,
vb@0
   565
        char **words, size_t *wsize, int max_words
vb@0
   566
    )
vb@0
   567
{
roker@529
   568
    const char *source = fingerprint;
roker@529
   569
    char *buffer;
roker@529
   570
    char *dest;
roker@529
   571
    size_t fsize;
vb@0
   572
    PEP_STATUS _status;
vb@0
   573
roker@529
   574
    assert(session);
roker@529
   575
    assert(fingerprint);
roker@529
   576
    assert(words);
roker@529
   577
    assert(wsize);
roker@529
   578
    assert(max_words >= 0);
vb@0
   579
vb@191
   580
    if (!(session && fingerprint && words && wsize && max_words >= 0))
vb@191
   581
        return PEP_ILLEGAL_VALUE;
vb@191
   582
roker@529
   583
    *words = NULL;
roker@529
   584
    *wsize = 0;
vb@0
   585
vb@251
   586
    buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
vb@0
   587
    assert(buffer);
vb@0
   588
    if (buffer == NULL)
vb@0
   589
        return PEP_OUT_OF_MEMORY;
vb@125
   590
    dest = buffer;
vb@0
   591
roker@529
   592
    fsize = strlen(fingerprint);
vb@0
   593
roker@529
   594
    if (!lang || !lang[0])
roker@529
   595
        lang = "en";
vb@0
   596
roker@529
   597
    assert((lang[0] >= 'A' && lang[0] <= 'Z')
vb@0
   598
            || (lang[0] >= 'a' && lang[0] <= 'z'));
roker@529
   599
    assert((lang[1] >= 'A' && lang[1] <= 'Z')
vb@0
   600
            || (lang[1] >= 'a' && lang[1] <= 'z'));
roker@529
   601
    assert(lang[2] == 0);
vb@0
   602
roker@529
   603
    int n_words = 0;
roker@529
   604
    while (source < fingerprint + fsize) {
roker@529
   605
        uint16_t value;
roker@529
   606
        char *word;
roker@529
   607
        size_t _wsize;
roker@529
   608
        int j;
vb@0
   609
vb@0
   610
        for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
roker@529
   611
            if (*source >= 'a' && *source <= 'f')
roker@529
   612
                value += (*source - 'a' + 10) << (3 - j++) * 4;
roker@529
   613
            else if (*source >= 'A' && *source <= 'F')
roker@529
   614
                value += (*source - 'A' + 10) << (3 - j++) * 4;
roker@529
   615
            else if (*source >= '0' && *source <= '9')
roker@529
   616
                value += (*source - '0') << (3 - j++) * 4;
roker@529
   617
            
roker@529
   618
            source++;
roker@529
   619
        }
vb@0
   620
roker@529
   621
        _status = trustword(session, value, lang, &word, &_wsize);
vb@0
   622
        if (_status == PEP_OUT_OF_MEMORY) {
vb@0
   623
            free(buffer);
vb@0
   624
            return PEP_OUT_OF_MEMORY;
vb@0
   625
        }
roker@529
   626
        if (word == NULL) {
vb@0
   627
            free(buffer);
roker@529
   628
            return PEP_TRUSTWORD_NOT_FOUND;
vb@0
   629
        }
vb@0
   630
roker@529
   631
        if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
roker@529
   632
            strncpy(dest, word, _wsize);
vb@0
   633
            free(word);
roker@529
   634
            dest += _wsize;
roker@529
   635
        }
roker@529
   636
        else {
vb@0
   637
            free(word);
roker@529
   638
            break; // buffer full
vb@0
   639
        }
vb@0
   640
roker@529
   641
        if (source < fingerprint + fsize
vb@251
   642
                && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
roker@529
   643
            *dest++ = ' ';
vb@0
   644
roker@529
   645
        ++n_words;
roker@529
   646
        if (max_words && n_words >= max_words)
roker@529
   647
            break;
roker@529
   648
    }
vb@0
   649
roker@529
   650
    *words = buffer;
roker@529
   651
    *wsize = dest - buffer;
roker@529
   652
    return PEP_STATUS_OK;
vb@0
   653
}
vb@0
   654
vb@0
   655
pEp_identity *new_identity(
vb@0
   656
        const char *address, const char *fpr, const char *user_id,
vb@0
   657
        const char *username
vb@0
   658
    )
vb@0
   659
{
vb@0
   660
    pEp_identity *result = calloc(1, sizeof(pEp_identity));
vb@0
   661
    assert(result);
vb@0
   662
    if (result) {
vb@0
   663
        if (address) {
vb@0
   664
            result->address = strdup(address);
vb@0
   665
            assert(result->address);
vb@0
   666
            if (result->address == NULL) {
vb@0
   667
                free(result);
vb@0
   668
                return NULL;
vb@0
   669
            }
vb@0
   670
        }
vb@0
   671
        if (fpr) {
vb@0
   672
            result->fpr = strdup(fpr);
vb@0
   673
            assert(result->fpr);
vb@0
   674
            if (result->fpr == NULL) {
vb@0
   675
                free_identity(result);
vb@0
   676
                return NULL;
vb@0
   677
            }
vb@0
   678
        }
vb@0
   679
        if (user_id) {
vb@0
   680
            result->user_id = strdup(user_id);
vb@0
   681
            assert(result->user_id);
vb@0
   682
            if (result->user_id == NULL) {
vb@0
   683
                free_identity(result);
vb@0
   684
                return NULL;
vb@0
   685
            }
vb@0
   686
        }
vb@0
   687
        if (username) {
vb@0
   688
            result->username = strdup(username);
vb@0
   689
            assert(result->username);
vb@0
   690
            if (result->username == NULL) {
vb@0
   691
                free_identity(result);
vb@0
   692
                return NULL;
vb@0
   693
            }
vb@0
   694
        }
vb@0
   695
    }
vb@0
   696
    return result;
vb@0
   697
}
vb@0
   698
vb@37
   699
pEp_identity *identity_dup(const pEp_identity *src)
vb@37
   700
{
vb@37
   701
    assert(src);
vb@37
   702
vb@37
   703
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@37
   704
    assert(dup);
vb@37
   705
    if (dup == NULL)
vb@37
   706
        return NULL;
vb@37
   707
    
vb@37
   708
    dup->comm_type = src->comm_type;
vb@37
   709
    dup->lang[0] = src->lang[0];
vb@37
   710
    dup->lang[1] = src->lang[1];
vb@37
   711
    dup->lang[2] = 0;
vb@37
   712
    dup->me = src->me;
vb@37
   713
vb@37
   714
    return dup;
vb@37
   715
}
vb@37
   716
vb@0
   717
void free_identity(pEp_identity *identity)
vb@0
   718
{
vb@0
   719
    if (identity) {
vb@0
   720
        free(identity->address);
vb@0
   721
        free(identity->fpr);
vb@0
   722
        free(identity->user_id);
vb@0
   723
        free(identity->username);
vb@0
   724
        free(identity);
vb@0
   725
    }
vb@0
   726
}
vb@0
   727
vb@0
   728
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
   729
        PEP_SESSION session,
Edouard@559
   730
        const char *address,
Edouard@559
   731
        const char *user_id,
vb@0
   732
        pEp_identity **identity
vb@0
   733
    )
vb@0
   734
{
roker@529
   735
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
   736
    static pEp_identity *_identity;
roker@529
   737
    int result;
roker@529
   738
    const char *_lang;
vb@0
   739
roker@529
   740
    assert(session);
roker@529
   741
    assert(address);
vb@8
   742
    assert(address[0]);
vb@632
   743
    assert(identity);
vb@0
   744
vb@632
   745
    if (!(session && address && address[0] && identity))
vb@191
   746
        return PEP_ILLEGAL_VALUE;
vb@191
   747
vb@632
   748
    *identity = NULL;
vb@632
   749
vb@46
   750
    sqlite3_reset(session->get_identity);
vb@46
   751
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
   752
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
   753
vb@46
   754
    result = sqlite3_step(session->get_identity);
roker@529
   755
    switch (result) {
roker@529
   756
    case SQLITE_ROW:
vb@0
   757
        _identity = new_identity(
vb@0
   758
                address,
vb@46
   759
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
   760
                user_id,
Edouard@559
   761
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
   762
                );
vb@0
   763
        assert(_identity);
vb@0
   764
        if (_identity == NULL)
vb@0
   765
            return PEP_OUT_OF_MEMORY;
vb@0
   766
Edouard@559
   767
        _identity->comm_type = (PEP_comm_type) sqlite3_column_int(session->get_identity, 2);
Edouard@559
   768
        _lang = (const char *) sqlite3_column_text(session->get_identity, 3);
vb@0
   769
        if (_lang && _lang[0]) {
roker@529
   770
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
   771
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
   772
            assert(_lang[2] == 0);
roker@529
   773
            _identity->lang[0] = _lang[0];
roker@529
   774
            _identity->lang[1] = _lang[1];
vb@0
   775
            _identity->lang[2] = 0;
roker@529
   776
        }
roker@529
   777
        *identity = _identity;
roker@529
   778
        break;
roker@529
   779
    default:
vb@0
   780
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
   781
        *identity = NULL;
roker@529
   782
    }
vb@0
   783
vb@46
   784
    sqlite3_reset(session->get_identity);
roker@529
   785
    return status;
vb@0
   786
}
vb@0
   787
vb@0
   788
DYNAMIC_API PEP_STATUS set_identity(
vb@0
   789
        PEP_SESSION session, const pEp_identity *identity
vb@0
   790
    )
vb@0
   791
{
roker@529
   792
    int result;
vb@0
   793
roker@529
   794
    assert(session);
roker@529
   795
    assert(identity);
roker@529
   796
    assert(identity->address);
roker@529
   797
    assert(identity->fpr);
roker@529
   798
    assert(identity->user_id);
roker@529
   799
    assert(identity->username);
vb@0
   800
vb@191
   801
    if (!(session && identity && identity->address && identity->fpr &&
vb@191
   802
                identity->user_id && identity->username))
vb@191
   803
        return PEP_ILLEGAL_VALUE;
vb@191
   804
vb@515
   805
    bool listed;
vb@515
   806
    PEP_STATUS status = blacklist_is_listed(session, identity->fpr, &listed);
vb@515
   807
    assert(status == PEP_STATUS_OK);
vb@515
   808
    if (status != PEP_STATUS_OK)
vb@515
   809
        return status;
vb@515
   810
vb@515
   811
    if (listed)
vb@515
   812
        return PEP_KEY_BLACKLISTED;
vb@515
   813
roker@529
   814
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
   815
roker@529
   816
    sqlite3_reset(session->set_person);
vb@46
   817
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
   818
            SQLITE_STATIC);
vb@46
   819
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
   820
            SQLITE_STATIC);
roker@529
   821
    if (identity->lang[0])
vb@46
   822
        sqlite3_bind_text(session->set_person, 3, identity->lang, 1,
vb@0
   823
                SQLITE_STATIC);
roker@529
   824
    else
roker@529
   825
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
   826
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
   827
                      SQLITE_STATIC);
roker@529
   828
    result = sqlite3_step(session->set_person);
roker@529
   829
    sqlite3_reset(session->set_person);
roker@529
   830
    if (result != SQLITE_DONE) {
roker@529
   831
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   832
        return PEP_CANNOT_SET_PERSON;
roker@529
   833
    }
vb@0
   834
roker@529
   835
    sqlite3_reset(session->set_pgp_keypair);
vb@46
   836
    sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
vb@0
   837
            SQLITE_STATIC);
roker@529
   838
    result = sqlite3_step(session->set_pgp_keypair);
roker@529
   839
    sqlite3_reset(session->set_pgp_keypair);
roker@529
   840
    if (result != SQLITE_DONE) {
roker@529
   841
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   842
        return PEP_CANNOT_SET_PGP_KEYPAIR;
roker@529
   843
    }
vb@0
   844
roker@529
   845
    sqlite3_reset(session->set_identity);
vb@46
   846
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
   847
            SQLITE_STATIC);
vb@46
   848
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
   849
            SQLITE_STATIC);
vb@46
   850
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
   851
            SQLITE_STATIC);
roker@529
   852
    result = sqlite3_step(session->set_identity);
roker@529
   853
    sqlite3_reset(session->set_identity);
roker@529
   854
    if (result != SQLITE_DONE) {
roker@529
   855
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
   856
        return PEP_CANNOT_SET_IDENTITY;
roker@529
   857
    }
vb@0
   858
roker@529
   859
    sqlite3_reset(session->set_trust);
vb@46
   860
    sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
vb@0
   861
            SQLITE_STATIC);
vb@46
   862
    sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
vb@0
   863
            SQLITE_STATIC);
roker@529
   864
    sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
roker@529
   865
    result = sqlite3_step(session->set_trust);
roker@529
   866
    sqlite3_reset(session->set_trust);
roker@529
   867
    if (result != SQLITE_DONE) {
roker@529
   868
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
Edouard@584
   869
        return PEP_CANNOT_SET_TRUST;
roker@529
   870
    }
vb@0
   871
vb@46
   872
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
   873
    if (result == SQLITE_OK)
roker@529
   874
        return PEP_STATUS_OK;
roker@529
   875
    else
roker@529
   876
        return PEP_COMMIT_FAILED;
vb@0
   877
}
vb@0
   878
vb@357
   879
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
   880
        PEP_SESSION session,
vb@357
   881
        const char *fpr
vb@357
   882
    )
vb@357
   883
{
roker@529
   884
    int result;
vb@357
   885
roker@529
   886
    assert(session);
vb@357
   887
    assert(fpr && fpr[0]);
vb@357
   888
vb@357
   889
    if (!(session && fpr && fpr[0]))
vb@357
   890
        return PEP_ILLEGAL_VALUE;
vb@357
   891
roker@529
   892
    sqlite3_reset(session->mark_compromized);
vb@357
   893
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
   894
            SQLITE_STATIC);
vb@357
   895
    result = sqlite3_step(session->mark_compromized);
roker@529
   896
    sqlite3_reset(session->mark_compromized);
vb@357
   897
vb@357
   898
    if (result != SQLITE_DONE)
Edouard@584
   899
        return PEP_CANNOT_SET_TRUST;
vb@357
   900
vb@357
   901
    return PEP_STATUS_OK;
vb@357
   902
}
vb@357
   903
vb@0
   904
void pEp_free(void *p)
vb@0
   905
{
vb@0
   906
    free(p);
vb@0
   907
}
vb@0
   908
vb@8
   909
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
   910
{
vb@8
   911
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
   912
    int result;
vb@8
   913
vb@8
   914
    assert(session);
vb@8
   915
    assert(identity);
vb@8
   916
    assert(identity->user_id);
vb@8
   917
    assert(identity->user_id[0]);
vb@8
   918
    assert(identity->fpr);
vb@8
   919
    assert(identity->fpr[0]);
vb@8
   920
vb@191
   921
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
   922
                identity->fpr && identity->fpr[0]))
vb@191
   923
        return PEP_ILLEGAL_VALUE;
vb@191
   924
vb@8
   925
    identity->comm_type = PEP_ct_unknown;
vb@8
   926
vb@46
   927
    sqlite3_reset(session->get_trust);
vb@46
   928
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
vb@46
   929
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
   930
vb@46
   931
    result = sqlite3_step(session->get_trust);
vb@8
   932
    switch (result) {
vb@8
   933
    case SQLITE_ROW: {
Edouard@559
   934
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust, 0);
vb@8
   935
        identity->comm_type = comm_type;
vb@8
   936
        break;
vb@8
   937
    }
vb@8
   938
 
vb@8
   939
    default:
vb@8
   940
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
   941
    }
vb@8
   942
vb@46
   943
    sqlite3_reset(session->get_trust);
vb@8
   944
    return status;
vb@9
   945
}
vb@9
   946
vb@251
   947
DYNAMIC_API PEP_STATUS least_trust(
vb@251
   948
        PEP_SESSION session,
vb@251
   949
        const char *fpr,
vb@251
   950
        PEP_comm_type *comm_type
vb@251
   951
    )
vb@251
   952
{
vb@251
   953
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
   954
    int result;
vb@251
   955
vb@251
   956
    assert(session);
vb@251
   957
    assert(fpr);
vb@251
   958
    assert(comm_type);
vb@251
   959
vb@251
   960
    if (!(session && fpr && comm_type))
vb@251
   961
        return PEP_ILLEGAL_VALUE;
vb@251
   962
vb@632
   963
    *comm_type = PEP_ct_unknown;
vb@632
   964
vb@251
   965
    sqlite3_reset(session->least_trust);
vb@251
   966
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
   967
vb@251
   968
    result = sqlite3_step(session->least_trust);
vb@251
   969
    switch (result) {
vb@251
   970
        case SQLITE_ROW: {
vb@259
   971
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
   972
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
   973
            break;
vb@251
   974
        }
vb@251
   975
        default:
vb@251
   976
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
   977
    }
vb@251
   978
vb@251
   979
    sqlite3_reset(session->least_trust);
vb@251
   980
    return status;
vb@251
   981
}
vb@251
   982
vb@24
   983
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
   984
    PEP_SESSION session, const char *ctext, size_t csize,
vb@24
   985
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
   986
    )
vb@24
   987
{
vb@191
   988
    assert(session);
vb@191
   989
    assert(ctext);
vb@191
   990
    assert(csize);
vb@191
   991
    assert(ptext);
vb@191
   992
    assert(psize);
vb@191
   993
    assert(keylist);
vb@191
   994
vb@191
   995
    if (!(session && ctext && csize && ptext && psize && keylist && keylist))
vb@191
   996
        return PEP_ILLEGAL_VALUE;
vb@191
   997
vb@46
   998
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(session, ctext, csize, ptext, psize, keylist);
vb@24
   999
}
vb@24
  1000
vb@24
  1001
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1002
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1003
    size_t psize, char **ctext, size_t *csize
vb@24
  1004
    )
vb@24
  1005
{
vb@191
  1006
    assert(session);
vb@191
  1007
    assert(keylist);
vb@191
  1008
    assert(ptext);
vb@191
  1009
    assert(psize);
vb@191
  1010
    assert(ctext);
vb@191
  1011
    assert(csize);
vb@191
  1012
vb@191
  1013
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1014
        return PEP_ILLEGAL_VALUE;
vb@191
  1015
vb@46
  1016
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session, keylist, ptext, psize, ctext, csize);
vb@24
  1017
}
vb@24
  1018
vb@24
  1019
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1020
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1021
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1022
    )
vb@24
  1023
{
vb@191
  1024
    assert(session);
vb@191
  1025
    assert(text);
vb@191
  1026
    assert(size);
vb@191
  1027
    assert(signature);
vb@191
  1028
    assert(sig_size);
vb@191
  1029
    assert(keylist);
vb@191
  1030
vb@191
  1031
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1032
        return PEP_ILLEGAL_VALUE;
vb@191
  1033
vb@46
  1034
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text, size, signature, sig_size, keylist);
vb@24
  1035
}
vb@24
  1036
vb@24
  1037
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1038
{
vb@191
  1039
    assert(session);
vb@191
  1040
    assert(fpr);
vb@191
  1041
vb@191
  1042
    if (!(session && fpr))
vb@191
  1043
        return PEP_ILLEGAL_VALUE;
vb@191
  1044
vb@46
  1045
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1046
}
vb@24
  1047
vb@24
  1048
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1049
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1050
    )
vb@24
  1051
{
vb@191
  1052
    assert(session);
vb@191
  1053
    assert(fpr);
vb@191
  1054
    assert(key_data);
vb@191
  1055
    assert(size);
vb@191
  1056
vb@191
  1057
    if (!(session && fpr && key_data && size))
vb@191
  1058
        return PEP_ILLEGAL_VALUE;
vb@191
  1059
vb@46
  1060
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr, key_data, size);
vb@24
  1061
}
vb@24
  1062
vb@24
  1063
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1064
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1065
    )
vb@24
  1066
{
vb@191
  1067
    assert(session);
vb@191
  1068
    assert(pattern);
vb@191
  1069
    assert(keylist);
vb@191
  1070
vb@191
  1071
    if (!(session && pattern && keylist))
vb@191
  1072
        return PEP_ILLEGAL_VALUE;
vb@191
  1073
vb@46
  1074
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern, keylist);
vb@24
  1075
}
vb@24
  1076
vb@24
  1077
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1078
        PEP_SESSION session, pEp_identity *identity
vb@24
  1079
    )
vb@24
  1080
{
vb@191
  1081
    assert(session);
vb@191
  1082
    assert(identity);
vb@191
  1083
    assert(identity->address);
vb@298
  1084
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1085
    assert(identity->username);
vb@191
  1086
vb@298
  1087
    if (!(session && identity && identity->address &&
vb@298
  1088
        (identity->fpr == NULL || identity->fpr[0] == 0) && identity->username))
vb@191
  1089
        return PEP_ILLEGAL_VALUE;
vb@191
  1090
vb@46
  1091
    return session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session, identity);
vb@24
  1092
}
vb@24
  1093
vb@9
  1094
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1095
        PEP_SESSION session,
vb@46
  1096
        const char *fpr,
vb@46
  1097
        PEP_comm_type *comm_type
vb@9
  1098
    )
vb@9
  1099
{
vb@191
  1100
    assert(session);
vb@191
  1101
    assert(fpr);
vb@191
  1102
    assert(comm_type);
vb@191
  1103
vb@191
  1104
    if (!(session && fpr && comm_type))
vb@191
  1105
        return PEP_ILLEGAL_VALUE;
vb@191
  1106
vb@46
  1107
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr, comm_type);
vb@24
  1108
}
vb@9
  1109
vb@24
  1110
DYNAMIC_API PEP_STATUS import_key(PEP_SESSION session, const char *key_data, size_t size)
vb@24
  1111
{
vb@191
  1112
    assert(session);
vb@191
  1113
    assert(key_data);
vb@191
  1114
vb@191
  1115
    if (!(session && key_data))
vb@191
  1116
        return PEP_ILLEGAL_VALUE;
vb@191
  1117
vb@46
  1118
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data, size);
vb@24
  1119
}
vb@9
  1120
vb@24
  1121
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1122
{
vb@191
  1123
    assert(session);
vb@191
  1124
    assert(pattern);
vb@191
  1125
vb@191
  1126
    if (!(session && pattern))
vb@191
  1127
        return PEP_ILLEGAL_VALUE;
vb@191
  1128
vb@46
  1129
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1130
}
vb@9
  1131
vb@24
  1132
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1133
{
vb@191
  1134
    assert(session);
vb@191
  1135
    assert(pattern);
vb@191
  1136
vb@191
  1137
    if (!(session && pattern))
vb@191
  1138
        return PEP_ILLEGAL_VALUE;
vb@191
  1139
vb@46
  1140
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1141
}
vb@196
  1142
vb@201
  1143
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1144
        PEP_SESSION session,
vb@201
  1145
        const char *fpr,
vb@201
  1146
        const timestamp *ts
vb@201
  1147
    )
vb@196
  1148
{
vb@196
  1149
    assert(session);
vb@200
  1150
    assert(fpr);
vb@196
  1151
vb@200
  1152
    if (!(session && fpr))
vb@196
  1153
        return PEP_ILLEGAL_VALUE;
vb@196
  1154
vb@201
  1155
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1156
}
vb@196
  1157
vb@211
  1158
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1159
        PEP_SESSION session,
vb@211
  1160
        const char *fpr,
vb@211
  1161
        const char *reason
vb@211
  1162
    )
vb@197
  1163
{
vb@197
  1164
    assert(session);
vb@200
  1165
    assert(fpr);
vb@197
  1166
vb@200
  1167
    if (!(session && fpr))
vb@197
  1168
        return PEP_ILLEGAL_VALUE;
vb@197
  1169
vb@214
  1170
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1171
            reason);
vb@197
  1172
}
vb@197
  1173
vb@214
  1174
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1175
        PEP_SESSION session,
vb@214
  1176
        const char *fpr,
vb@214
  1177
        bool *expired
vb@214
  1178
    )
vb@214
  1179
{
vb@214
  1180
    assert(session);
vb@214
  1181
    assert(fpr);
vb@214
  1182
    assert(expired);
vb@214
  1183
vb@214
  1184
    if (!(session && fpr && expired))
vb@214
  1185
        return PEP_ILLEGAL_VALUE;
vb@214
  1186
vb@214
  1187
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
vb@214
  1188
            expired);
vb@214
  1189
}
vb@214
  1190
vb@451
  1191
static void _clean_log_value(char *text)
vb@451
  1192
{
vb@451
  1193
    if (text) {
vb@451
  1194
        for (char *c = text; *c; c++) {
vb@451
  1195
            if (*c < 32)
vb@451
  1196
                *c = 32;
vb@451
  1197
            else if (*c == '"')
vb@451
  1198
                *c = '\'';
vb@451
  1199
        }
vb@451
  1200
    }
vb@451
  1201
}
vb@451
  1202
vb@450
  1203
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1204
{
vb@454
  1205
    str2 = str2 ? str2 : "";
vb@450
  1206
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1207
    size_t len2 = strlen(str2);
vb@450
  1208
    size_t len = len1 + len2 + 3;
vb@450
  1209
    char * result = realloc(str1, len + 1);
vb@450
  1210
vb@450
  1211
    if (result) {
vb@450
  1212
        result[len1] = '"';
vb@450
  1213
        strcpy(result + len1 + 1, str2);
vb@450
  1214
        _clean_log_value(result + len1 + 1);
vb@450
  1215
        result[len - 2] = '"';
vb@450
  1216
        result[len - 1] = delim;
vb@450
  1217
        result[len] = 0;
vb@450
  1218
    }
vb@450
  1219
    else {
vb@450
  1220
        free(str1);
vb@450
  1221
    }
vb@450
  1222
vb@450
  1223
    return result;
vb@450
  1224
}
vb@450
  1225
vb@450
  1226
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1227
        PEP_SESSION session,
vb@450
  1228
        int maxlines,
vb@450
  1229
        char **logdata
vb@450
  1230
    )
vb@450
  1231
{
vb@450
  1232
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1233
    char *_logdata= NULL;
vb@450
  1234
vb@450
  1235
    assert(session);
vb@450
  1236
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1237
    assert(logdata);
vb@450
  1238
vb@450
  1239
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1240
            CRASHDUMP_MAX_LINES))
vb@450
  1241
        return PEP_ILLEGAL_VALUE;
vb@450
  1242
vb@632
  1243
    *logdata = NULL;
vb@632
  1244
vb@450
  1245
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1246
    const char *timestamp = NULL;
vb@458
  1247
    const char *title = NULL;
vb@458
  1248
    const char *entity = NULL;
vb@458
  1249
    const char *desc = NULL;
vb@458
  1250
    const char *comment = NULL;
vb@450
  1251
vb@450
  1252
    sqlite3_reset(session->crashdump);
vb@452
  1253
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1254
vb@450
  1255
    int result;
vb@450
  1256
vb@450
  1257
    do {
vb@450
  1258
        result = sqlite3_step(session->crashdump);
vb@450
  1259
        switch (result) {
vb@450
  1260
        case SQLITE_ROW:
vb@452
  1261
            timestamp = (const char *) sqlite3_column_text(session->crashdump, 0);
vb@452
  1262
            title   = (const char *) sqlite3_column_text(session->crashdump, 1);
vb@452
  1263
            entity  = (const char *) sqlite3_column_text(session->crashdump, 2);
vb@452
  1264
            desc    = (const char *) sqlite3_column_text(session->crashdump, 3);
vb@452
  1265
            comment = (const char *) sqlite3_column_text(session->crashdump, 4);
vb@452
  1266
vb@452
  1267
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1268
            if (_logdata == NULL)
vb@452
  1269
                goto enomem;
vb@450
  1270
vb@450
  1271
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  1272
            if (_logdata == NULL)
vb@450
  1273
                goto enomem;
vb@450
  1274
vb@450
  1275
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  1276
            if (_logdata == NULL)
vb@450
  1277
                goto enomem;
vb@450
  1278
vb@450
  1279
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  1280
            if (_logdata == NULL)
vb@450
  1281
                goto enomem;
vb@450
  1282
vb@450
  1283
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  1284
            if (_logdata == NULL)
vb@450
  1285
                goto enomem;
vb@450
  1286
vb@450
  1287
            break;
vb@450
  1288
vb@450
  1289
        case SQLITE_DONE:
vb@450
  1290
            break;
vb@450
  1291
vb@450
  1292
        default:
vb@450
  1293
            status = PEP_UNKNOWN_ERROR;
vb@450
  1294
            result = SQLITE_DONE;
vb@450
  1295
        }
vb@450
  1296
    } while (result != SQLITE_DONE);
vb@450
  1297
vb@450
  1298
    sqlite3_reset(session->crashdump);
vb@450
  1299
    if (status == PEP_STATUS_OK)
vb@450
  1300
        *logdata = _logdata;
vb@450
  1301
vb@450
  1302
    goto the_end;
vb@450
  1303
vb@450
  1304
enomem:
vb@450
  1305
    status = PEP_OUT_OF_MEMORY;
vb@450
  1306
vb@450
  1307
the_end:
vb@450
  1308
    return status;
vb@450
  1309
}
vb@450
  1310
vb@458
  1311
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  1312
        PEP_SESSION session,
vb@458
  1313
        char **languages
vb@458
  1314
    )
vb@458
  1315
{
vb@458
  1316
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1317
    char *_languages= NULL;
vb@458
  1318
vb@458
  1319
    assert(session);
vb@458
  1320
    assert(languages);
vb@458
  1321
vb@458
  1322
    if (!(session && languages))
vb@458
  1323
        return PEP_ILLEGAL_VALUE;
vb@458
  1324
vb@632
  1325
    *languages = NULL;
vb@632
  1326
vb@458
  1327
    const char *lang = NULL;
vb@458
  1328
    const char *name = NULL;
vb@617
  1329
    const char *phrase = NULL;
vb@458
  1330
vb@458
  1331
    sqlite3_reset(session->languagelist);
vb@458
  1332
vb@458
  1333
    int result;
vb@458
  1334
vb@458
  1335
    do {
vb@458
  1336
        result = sqlite3_step(session->languagelist);
vb@458
  1337
        switch (result) {
vb@458
  1338
        case SQLITE_ROW:
vb@458
  1339
            lang = (const char *) sqlite3_column_text(session->languagelist, 0);
vb@458
  1340
            name = (const char *) sqlite3_column_text(session->languagelist, 1);
vb@617
  1341
            phrase = (const char *) sqlite3_column_text(session->languagelist, 2);
vb@458
  1342
vb@458
  1343
            _languages = _concat_string(_languages, lang, ',');
vb@458
  1344
            if (_languages == NULL)
vb@458
  1345
                goto enomem;
vb@458
  1346
vb@617
  1347
            _languages = _concat_string(_languages, name, ',');
vb@617
  1348
            if (_languages == NULL)
vb@617
  1349
                goto enomem;
vb@617
  1350
vb@617
  1351
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  1352
            if (_languages == NULL)
vb@458
  1353
                goto enomem;
vb@458
  1354
vb@458
  1355
            break;
vb@458
  1356
vb@458
  1357
        case SQLITE_DONE:
vb@458
  1358
            break;
vb@458
  1359
vb@458
  1360
        default:
vb@458
  1361
            status = PEP_UNKNOWN_ERROR;
vb@458
  1362
            result = SQLITE_DONE;
vb@458
  1363
        }
vb@458
  1364
    } while (result != SQLITE_DONE);
vb@458
  1365
vb@458
  1366
    sqlite3_reset(session->languagelist);
vb@458
  1367
    if (status == PEP_STATUS_OK)
vb@458
  1368
        *languages = _languages;
vb@458
  1369
vb@458
  1370
    goto the_end;
vb@458
  1371
vb@458
  1372
enomem:
vb@458
  1373
    status = PEP_OUT_OF_MEMORY;
vb@458
  1374
vb@458
  1375
the_end:
vb@458
  1376
    return status;
vb@458
  1377
}
vb@458
  1378
vb@458
  1379
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  1380
        PEP_SESSION session,
vb@458
  1381
        const char *lang,
vb@458
  1382
        int phrase_id,
vb@458
  1383
        char **phrase
vb@458
  1384
    )
vb@458
  1385
{
vb@458
  1386
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  1387
vb@479
  1388
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  1389
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  1390
        return PEP_ILLEGAL_VALUE;
vb@458
  1391
vb@632
  1392
    *phrase = NULL;
vb@632
  1393
vb@458
  1394
    sqlite3_reset(session->i18n_token);
roker@529
  1395
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  1396
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  1397
vb@458
  1398
    const char *_phrase = NULL;
vb@458
  1399
    int result;
vb@458
  1400
vb@458
  1401
    result = sqlite3_step(session->i18n_token);
vb@458
  1402
    switch (result) {
vb@458
  1403
    case SQLITE_ROW:
vb@458
  1404
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  1405
        break;
vb@458
  1406
vb@458
  1407
    case SQLITE_DONE:
vb@458
  1408
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  1409
        break;
vb@458
  1410
vb@458
  1411
    default:
vb@458
  1412
        status = PEP_UNKNOWN_ERROR;
vb@458
  1413
    }
vb@458
  1414
vb@458
  1415
    if (status == PEP_STATUS_OK) {
vb@458
  1416
        *phrase = strdup(_phrase);
vb@458
  1417
        if (*phrase == NULL)
vb@458
  1418
            goto enomem;
vb@458
  1419
    }
vb@459
  1420
vb@459
  1421
    sqlite3_reset(session->i18n_token);
vb@458
  1422
    goto the_end;
vb@458
  1423
vb@458
  1424
enomem:
vb@458
  1425
    status = PEP_OUT_OF_MEMORY;
vb@458
  1426
vb@458
  1427
the_end:
vb@458
  1428
    return status;
vb@458
  1429
}
vb@458
  1430
vb@632
  1431
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  1432
        PEP_SESSION session,
vb@632
  1433
        const char *name,
vb@652
  1434
        int32_t *value
vb@632
  1435
    )
vb@632
  1436
{
vb@632
  1437
    PEP_STATUS status = PEP_STATUS_OK;
vb@632
  1438
    int result;
vb@632
  1439
vb@632
  1440
    assert(session);
vb@632
  1441
    assert(name);
vb@632
  1442
    assert(value);
vb@632
  1443
vb@632
  1444
    if (!(session && name && value))
vb@632
  1445
        return PEP_ILLEGAL_VALUE;
vb@632
  1446
vb@632
  1447
    *value = 0;
vb@632
  1448
vb@633
  1449
    sqlite3_reset(session->sequence_value1);
vb@633
  1450
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@633
  1451
    result = sqlite3_step(session->sequence_value1);
vb@633
  1452
    assert(result == SQLITE_DONE);
vb@633
  1453
    sqlite3_reset(session->sequence_value1);
vb@633
  1454
    if (result != SQLITE_DONE) {
vb@633
  1455
        status = PEP_UNKNOWN_ERROR;
vb@633
  1456
    }
vb@633
  1457
    else {
vb@633
  1458
        sqlite3_reset(session->sequence_value2);
vb@633
  1459
        sqlite3_bind_text(session->sequence_value2, 1, name, -1, SQLITE_STATIC);
vb@633
  1460
        result = sqlite3_step(session->sequence_value2);
vb@633
  1461
        switch (result) {
vb@633
  1462
            case SQLITE_ROW: {
vb@652
  1463
                int32_t _value = (int32_t)
vb@633
  1464
                        sqlite3_column_int64(session->sequence_value2, 0);
vb@633
  1465
                *value = _value;
vb@633
  1466
                break;
vb@633
  1467
            }
vb@633
  1468
            default:
vb@633
  1469
                status = PEP_UNKNOWN_ERROR;
vb@632
  1470
        }
vb@633
  1471
        sqlite3_reset(session->sequence_value2);
vb@632
  1472
    }
vb@632
  1473
    return status;
vb@632
  1474
}
vb@632
  1475
vb@482
  1476
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  1477
{
vb@482
  1478
    assert(session);
vb@482
  1479
vb@482
  1480
    if (!session)
vb@482
  1481
        return PEP_ILLEGAL_VALUE;
vb@482
  1482
fdik@494
  1483
    int int_result = sqlite3_exec(
fdik@494
  1484
        session->db,
fdik@494
  1485
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  1486
        NULL,
fdik@494
  1487
        NULL,
fdik@494
  1488
        NULL
fdik@494
  1489
    );
fdik@494
  1490
    assert(int_result == SQLITE_OK);
vb@482
  1491
fdik@494
  1492
    return PEP_STATUS_OK;
vb@482
  1493
}
vb@482
  1494