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