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