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