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