src/pEpEngine.c
author Krista Bennett <krista@pep-project.org>
Tue, 01 Aug 2017 16:40:24 +0200
branchmessage-2.0
changeset 1979 3d0a778d035a
parent 1887 3feebd18c35f
child 1903 c2a9a756b1f5
child 1945 9503c962f746
permissions -rw-r--r--
Changed name to ENGINE-214 for JIRA tracking - please use that branch instead.
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@37
  1149
    dup->me = src->me;
vb@930
  1150
    dup->flags = src->flags;
vb@37
  1151
vb@37
  1152
    return dup;
vb@37
  1153
}
vb@37
  1154
vb@0
  1155
void free_identity(pEp_identity *identity)
vb@0
  1156
{
vb@0
  1157
    if (identity) {
vb@0
  1158
        free(identity->address);
vb@0
  1159
        free(identity->fpr);
vb@0
  1160
        free(identity->user_id);
vb@0
  1161
        free(identity->username);
vb@0
  1162
        free(identity);
vb@0
  1163
    }
vb@0
  1164
}
vb@0
  1165
vb@0
  1166
DYNAMIC_API PEP_STATUS get_identity(
Edouard@559
  1167
        PEP_SESSION session,
Edouard@559
  1168
        const char *address,
Edouard@559
  1169
        const char *user_id,
vb@0
  1170
        pEp_identity **identity
vb@0
  1171
    )
vb@0
  1172
{
roker@529
  1173
    PEP_STATUS status = PEP_STATUS_OK;
roker@529
  1174
    static pEp_identity *_identity;
vb@0
  1175
roker@529
  1176
    assert(session);
roker@529
  1177
    assert(address);
vb@8
  1178
    assert(address[0]);
vb@632
  1179
    assert(identity);
vb@0
  1180
vb@632
  1181
    if (!(session && address && address[0] && identity))
vb@191
  1182
        return PEP_ILLEGAL_VALUE;
vb@191
  1183
vb@632
  1184
    *identity = NULL;
vb@632
  1185
vb@46
  1186
    sqlite3_reset(session->get_identity);
vb@46
  1187
    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
Edouard@559
  1188
    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
vb@0
  1189
roker@876
  1190
    const int result = sqlite3_step(session->get_identity);
roker@529
  1191
    switch (result) {
roker@529
  1192
    case SQLITE_ROW:
vb@0
  1193
        _identity = new_identity(
vb@0
  1194
                address,
vb@46
  1195
                (const char *) sqlite3_column_text(session->get_identity, 0),
Edouard@559
  1196
                user_id,
Edouard@559
  1197
                (const char *) sqlite3_column_text(session->get_identity, 1)
vb@0
  1198
                );
vb@0
  1199
        assert(_identity);
vb@0
  1200
        if (_identity == NULL)
vb@0
  1201
            return PEP_OUT_OF_MEMORY;
vb@0
  1202
vb@951
  1203
        _identity->comm_type = (PEP_comm_type)
vb@951
  1204
            sqlite3_column_int(session->get_identity, 2);
vb@951
  1205
        const char* const _lang = (const char *)
vb@951
  1206
            sqlite3_column_text(session->get_identity, 3);
vb@0
  1207
        if (_lang && _lang[0]) {
roker@529
  1208
            assert(_lang[0] >= 'a' && _lang[0] <= 'z');
roker@529
  1209
            assert(_lang[1] >= 'a' && _lang[1] <= 'z');
roker@529
  1210
            assert(_lang[2] == 0);
roker@529
  1211
            _identity->lang[0] = _lang[0];
roker@529
  1212
            _identity->lang[1] = _lang[1];
vb@0
  1213
            _identity->lang[2] = 0;
roker@529
  1214
        }
vb@951
  1215
        _identity->flags = (unsigned int)
vb@951
  1216
            sqlite3_column_int(session->get_identity, 4);
roker@529
  1217
        *identity = _identity;
roker@529
  1218
        break;
roker@529
  1219
    default:
vb@0
  1220
        status = PEP_CANNOT_FIND_IDENTITY;
roker@529
  1221
        *identity = NULL;
roker@529
  1222
    }
vb@0
  1223
vb@46
  1224
    sqlite3_reset(session->get_identity);
roker@529
  1225
    return status;
vb@0
  1226
}
vb@0
  1227
vb@0
  1228
DYNAMIC_API PEP_STATUS set_identity(
vb@0
  1229
        PEP_SESSION session, const pEp_identity *identity
vb@0
  1230
    )
vb@0
  1231
{
roker@529
  1232
    int result;
vb@0
  1233
roker@529
  1234
    assert(session);
roker@529
  1235
    assert(identity);
roker@529
  1236
    assert(identity->address);
roker@529
  1237
    assert(identity->user_id);
roker@529
  1238
    assert(identity->username);
vb@0
  1239
krista@1223
  1240
    if (!(session && identity && identity->address &&
vb@191
  1241
                identity->user_id && identity->username))
vb@191
  1242
        return PEP_ILLEGAL_VALUE;
vb@191
  1243
krista@1791
  1244
    PEP_STATUS status = PEP_STATUS_OK;
krista@1791
  1245
    
vb@515
  1246
    bool listed;
krista@1449
  1247
krista@1449
  1248
    bool has_fpr = (identity->fpr && identity->fpr[0] != '\0');
krista@1222
  1249
    
krista@1449
  1250
    if (has_fpr) {    
krista@1275
  1251
        // blacklist check
krista@1791
  1252
        status = blacklist_is_listed(session, identity->fpr, &listed);
krista@1223
  1253
        assert(status == PEP_STATUS_OK);
krista@1223
  1254
        if (status != PEP_STATUS_OK)
krista@1223
  1255
            return status;
vb@515
  1256
krista@1223
  1257
        if (listed)
krista@1223
  1258
            return PEP_KEY_BLACKLISTED;
krista@1223
  1259
    }
vb@515
  1260
roker@529
  1261
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@0
  1262
vb@1079
  1263
    if (identity->lang[0]) {
vb@1079
  1264
        assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
vb@1079
  1265
        assert(identity->lang[1] >= 'a' && identity->lang[1] <= 'z');
vb@1079
  1266
        assert(identity->lang[2] == 0);
vb@1079
  1267
    }
vb@1079
  1268
roker@529
  1269
    sqlite3_reset(session->set_person);
vb@46
  1270
    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
vb@0
  1271
            SQLITE_STATIC);
vb@46
  1272
    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
vb@0
  1273
            SQLITE_STATIC);
roker@529
  1274
    if (identity->lang[0])
vb@1081
  1275
        sqlite3_bind_text(session->set_person, 3, identity->lang, 2,
vb@0
  1276
                SQLITE_STATIC);
roker@529
  1277
    else
roker@529
  1278
        sqlite3_bind_null(session->set_person, 3);
Edouard@641
  1279
    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
Edouard@641
  1280
                      SQLITE_STATIC);
roker@529
  1281
    result = sqlite3_step(session->set_person);
roker@529
  1282
    sqlite3_reset(session->set_person);
roker@529
  1283
    if (result != SQLITE_DONE) {
roker@529
  1284
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1285
        return PEP_CANNOT_SET_PERSON;
roker@529
  1286
    }
vb@0
  1287
krista@1449
  1288
    if (has_fpr) {
krista@1449
  1289
        sqlite3_reset(session->set_pgp_keypair);
krista@1449
  1290
        sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
krista@1449
  1291
                SQLITE_STATIC);
krista@1449
  1292
        result = sqlite3_step(session->set_pgp_keypair);
krista@1449
  1293
        sqlite3_reset(session->set_pgp_keypair);
krista@1449
  1294
        if (result != SQLITE_DONE) {
krista@1449
  1295
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
krista@1449
  1296
            return PEP_CANNOT_SET_PGP_KEYPAIR;
krista@1449
  1297
        }
roker@529
  1298
    }
vb@0
  1299
edouard@1394
  1300
    sqlite3_reset(session->set_identity);
edouard@1394
  1301
    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
vb@0
  1302
            SQLITE_STATIC);
edouard@1394
  1303
    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
vb@0
  1304
            SQLITE_STATIC);
edouard@1394
  1305
    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
vb@0
  1306
            SQLITE_STATIC);
edouard@1394
  1307
    sqlite3_bind_int(session->set_identity, 4, identity->flags);
edouard@1394
  1308
    result = sqlite3_step(session->set_identity);
edouard@1394
  1309
    sqlite3_reset(session->set_identity);
roker@529
  1310
    if (result != SQLITE_DONE) {
roker@529
  1311
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
roker@529
  1312
        return PEP_CANNOT_SET_IDENTITY;
roker@529
  1313
    }
vb@0
  1314
krista@1449
  1315
    if (has_fpr) {
krista@1449
  1316
        if(strcmp(identity->user_id, PEP_OWN_USERID) == 0) {
krista@1449
  1317
            sqlite3_reset(session->set_own_key);
krista@1449
  1318
            sqlite3_bind_text(session->set_own_key, 1, identity->address, -1,
krista@1449
  1319
                SQLITE_STATIC);
krista@1449
  1320
            sqlite3_bind_text(session->set_own_key, 2, identity->fpr, -1,
krista@1449
  1321
                SQLITE_STATIC);
krista@1449
  1322
            result = sqlite3_step(session->set_own_key);
krista@1449
  1323
            sqlite3_reset(session->set_own_key);
krista@1449
  1324
            if (result != SQLITE_DONE) {
krista@1449
  1325
                sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
krista@1449
  1326
                return PEP_CANNOT_SET_PGP_KEYPAIR;
krista@1449
  1327
            }
krista@1449
  1328
        }
krista@1449
  1329
krista@1797
  1330
        // status = set_trust(session, identity->user_id, identity->fpr,
krista@1797
  1331
        //                    identity->comm_type)
krista@1449
  1332
        sqlite3_reset(session->set_trust);
krista@1449
  1333
        sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
krista@1449
  1334
                SQLITE_STATIC);
krista@1449
  1335
        sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
krista@1449
  1336
                SQLITE_STATIC);
krista@1449
  1337
        sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
krista@1449
  1338
        result = sqlite3_step(session->set_trust);
krista@1449
  1339
        sqlite3_reset(session->set_trust);
edouard@1394
  1340
        if (result != SQLITE_DONE) {
edouard@1394
  1341
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
krista@1449
  1342
            return PEP_CANNOT_SET_TRUST;
edouard@1394
  1343
        }
edouard@1394
  1344
    }
krista@1449
  1345
    
vb@46
  1346
    result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
roker@529
  1347
    if (result == SQLITE_OK)
roker@529
  1348
        return PEP_STATUS_OK;
roker@529
  1349
    else
roker@529
  1350
        return PEP_COMMIT_FAILED;
vb@0
  1351
}
vb@0
  1352
krista@1799
  1353
PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
krista@1799
  1354
                                 const char* old_fpr, 
krista@1799
  1355
                                 const char* new_fpr) 
krista@1793
  1356
{
krista@1799
  1357
    assert(old_fpr);
krista@1799
  1358
    assert(new_fpr);
krista@1798
  1359
    
krista@1799
  1360
    if (!old_fpr || !new_fpr)
krista@1799
  1361
        return PEP_ILLEGAL_VALUE;
krista@1799
  1362
            
krista@1799
  1363
    sqlite3_reset(session->replace_identities_fpr);
krista@1799
  1364
    sqlite3_bind_text(session->replace_identities_fpr, 1, new_fpr, -1,
krista@1799
  1365
                      SQLITE_STATIC);
krista@1799
  1366
    sqlite3_bind_text(session->replace_identities_fpr, 2, old_fpr, -1,
krista@1799
  1367
                      SQLITE_STATIC);
krista@1799
  1368
krista@1799
  1369
    int result = sqlite3_step(session->replace_identities_fpr);
krista@1799
  1370
    sqlite3_reset(session->replace_identities_fpr);
krista@1799
  1371
    
krista@1799
  1372
    if (result != SQLITE_DONE)
krista@1799
  1373
        return PEP_CANNOT_SET_IDENTITY;
krista@1799
  1374
krista@1799
  1375
    return PEP_STATUS_OK;
krista@1799
  1376
}
krista@1799
  1377
krista@1799
  1378
krista@1799
  1379
PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
krista@1799
  1380
                                const char* fpr, 
krista@1799
  1381
                                PEP_comm_type comm_type)
krista@1799
  1382
{
krista@1799
  1383
    if (!fpr)
krista@1798
  1384
        return PEP_ILLEGAL_VALUE;
krista@1798
  1385
        
krista@1799
  1386
    sqlite3_reset(session->update_trust_for_fpr);
krista@1805
  1387
    sqlite3_bind_int(session->update_trust_for_fpr, 1, comm_type);
krista@1799
  1388
    sqlite3_bind_text(session->update_trust_for_fpr, 2, fpr, -1,
krista@1791
  1389
            SQLITE_STATIC);
krista@1799
  1390
    int result = sqlite3_step(session->update_trust_for_fpr);
krista@1799
  1391
    sqlite3_reset(session->update_trust_for_fpr);
krista@1791
  1392
    if (result != SQLITE_DONE) {
krista@1791
  1393
        return PEP_CANNOT_SET_TRUST;
krista@1791
  1394
    }
krista@1791
  1395
    
krista@1791
  1396
    return PEP_STATUS_OK;
krista@1791
  1397
}
krista@1791
  1398
edouard@1234
  1399
DYNAMIC_API PEP_STATUS set_device_group(
edouard@1234
  1400
        PEP_SESSION session,
edouard@1234
  1401
        const char *group_name
edouard@1234
  1402
    )
edouard@1234
  1403
{
edouard@1234
  1404
    int result;
edouard@1234
  1405
edouard@1234
  1406
    assert(session);
edouard@1234
  1407
edouard@1234
  1408
    if (!(session && group_name))
edouard@1234
  1409
        return PEP_ILLEGAL_VALUE;
edouard@1234
  1410
edouard@1234
  1411
    sqlite3_reset(session->set_device_group);
edouard@1574
  1412
    if(group_name){
edouard@1574
  1413
        sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
edouard@1574
  1414
                SQLITE_STATIC);
edouard@1574
  1415
    } else {
edouard@1574
  1416
        sqlite3_bind_null(session->set_device_group, 1);
edouard@1574
  1417
    }
edouard@1574
  1418
edouard@1234
  1419
    result = sqlite3_step(session->set_device_group);
edouard@1234
  1420
    sqlite3_reset(session->set_device_group);
edouard@1234
  1421
    if (result != SQLITE_DONE)
edouard@1234
  1422
        return PEP_CANNOT_SET_PERSON;
edouard@1234
  1423
edouard@1234
  1424
    return PEP_STATUS_OK;
edouard@1234
  1425
}
edouard@1234
  1426
edouard@1235
  1427
DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
edouard@1235
  1428
{
edouard@1235
  1429
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1235
  1430
    int result;
edouard@1235
  1431
edouard@1235
  1432
    assert(session);
edouard@1235
  1433
    assert(group_name);
edouard@1235
  1434
edouard@1235
  1435
    if (!(session && group_name))
edouard@1235
  1436
        return PEP_ILLEGAL_VALUE;
edouard@1235
  1437
edouard@1235
  1438
    sqlite3_reset(session->get_device_group);
edouard@1235
  1439
edouard@1235
  1440
    result = sqlite3_step(session->get_device_group);
edouard@1235
  1441
    switch (result) {
edouard@1235
  1442
    case SQLITE_ROW: {
edouard@1574
  1443
        const char *_group_name = (const char *)sqlite3_column_text(session->get_device_group, 0);
edouard@1574
  1444
        if(_group_name){
edouard@1574
  1445
            *group_name = strdup(_group_name);
edouard@1574
  1446
                if(*group_name == NULL)
edouard@1574
  1447
                    status = PEP_OUT_OF_MEMORY;
edouard@1574
  1448
        }
edouard@1235
  1449
        break;
edouard@1235
  1450
    }
edouard@1235
  1451
 
edouard@1235
  1452
    default:
edouard@1235
  1453
        status = PEP_RECORD_NOT_FOUND;
edouard@1235
  1454
    }
edouard@1235
  1455
edouard@1235
  1456
    sqlite3_reset(session->get_device_group);
edouard@1235
  1457
    return status;
edouard@1235
  1458
}
edouard@1235
  1459
vb@932
  1460
DYNAMIC_API PEP_STATUS set_identity_flags(
vb@934
  1461
        PEP_SESSION session,
vb@934
  1462
        pEp_identity *identity,
vb@934
  1463
        unsigned int flags
vb@932
  1464
    )
vb@932
  1465
{
vb@932
  1466
    int result;
vb@932
  1467
vb@932
  1468
    assert(session);
vb@932
  1469
    assert(identity);
vb@932
  1470
    assert(identity->address);
vb@932
  1471
    assert(identity->user_id);
vb@932
  1472
vb@932
  1473
    if (!(session && identity && identity->address && identity->user_id))
vb@932
  1474
        return PEP_ILLEGAL_VALUE;
vb@932
  1475
vb@932
  1476
    sqlite3_reset(session->set_identity_flags);
vb@934
  1477
    sqlite3_bind_int(session->set_identity_flags, 1, flags);
vb@932
  1478
    sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
vb@932
  1479
            SQLITE_STATIC);
vb@932
  1480
    sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
vb@932
  1481
            SQLITE_STATIC);
vb@932
  1482
    result = sqlite3_step(session->set_identity_flags);
vb@932
  1483
    sqlite3_reset(session->set_identity_flags);
vb@932
  1484
    if (result != SQLITE_DONE)
vb@932
  1485
        return PEP_CANNOT_SET_IDENTITY;
vb@932
  1486
edouard@1406
  1487
    identity->flags |= flags;
vb@932
  1488
    return PEP_STATUS_OK;
vb@932
  1489
}
vb@932
  1490
edouard@1394
  1491
DYNAMIC_API PEP_STATUS unset_identity_flags(
edouard@1394
  1492
        PEP_SESSION session,
edouard@1394
  1493
        pEp_identity *identity,
edouard@1394
  1494
        unsigned int flags
edouard@1394
  1495
    )
edouard@1394
  1496
{
edouard@1394
  1497
    int result;
edouard@1394
  1498
edouard@1394
  1499
    assert(session);
edouard@1394
  1500
    assert(identity);
edouard@1394
  1501
    assert(identity->address);
edouard@1394
  1502
    assert(identity->user_id);
edouard@1394
  1503
edouard@1394
  1504
    if (!(session && identity && identity->address && identity->user_id))
edouard@1394
  1505
        return PEP_ILLEGAL_VALUE;
edouard@1394
  1506
edouard@1394
  1507
    sqlite3_reset(session->unset_identity_flags);
edouard@1394
  1508
    sqlite3_bind_int(session->unset_identity_flags, 1, flags);
edouard@1394
  1509
    sqlite3_bind_text(session->unset_identity_flags, 2, identity->address, -1,
edouard@1394
  1510
            SQLITE_STATIC);
edouard@1394
  1511
    sqlite3_bind_text(session->unset_identity_flags, 3, identity->user_id, -1,
edouard@1394
  1512
            SQLITE_STATIC);
edouard@1394
  1513
    result = sqlite3_step(session->unset_identity_flags);
edouard@1394
  1514
    sqlite3_reset(session->unset_identity_flags);
edouard@1394
  1515
    if (result != SQLITE_DONE)
edouard@1394
  1516
        return PEP_CANNOT_SET_IDENTITY;
edouard@1394
  1517
edouard@1406
  1518
    identity->flags &= ~flags;
edouard@1394
  1519
    return PEP_STATUS_OK;
edouard@1394
  1520
}
edouard@1394
  1521
vb@357
  1522
DYNAMIC_API PEP_STATUS mark_as_compromized(
vb@357
  1523
        PEP_SESSION session,
vb@357
  1524
        const char *fpr
vb@357
  1525
    )
vb@357
  1526
{
roker@529
  1527
    int result;
vb@357
  1528
roker@529
  1529
    assert(session);
vb@357
  1530
    assert(fpr && fpr[0]);
vb@357
  1531
vb@357
  1532
    if (!(session && fpr && fpr[0]))
vb@357
  1533
        return PEP_ILLEGAL_VALUE;
vb@357
  1534
roker@529
  1535
    sqlite3_reset(session->mark_compromized);
vb@357
  1536
    sqlite3_bind_text(session->mark_compromized, 1, fpr, -1,
vb@357
  1537
            SQLITE_STATIC);
vb@357
  1538
    result = sqlite3_step(session->mark_compromized);
roker@529
  1539
    sqlite3_reset(session->mark_compromized);
vb@357
  1540
vb@357
  1541
    if (result != SQLITE_DONE)
Edouard@584
  1542
        return PEP_CANNOT_SET_TRUST;
vb@357
  1543
vb@357
  1544
    return PEP_STATUS_OK;
vb@357
  1545
}
vb@357
  1546
vb@0
  1547
void pEp_free(void *p)
vb@0
  1548
{
vb@0
  1549
    free(p);
vb@0
  1550
}
vb@0
  1551
vb@8
  1552
DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
vb@8
  1553
{
vb@8
  1554
    PEP_STATUS status = PEP_STATUS_OK;
vb@8
  1555
    int result;
vb@8
  1556
vb@8
  1557
    assert(session);
vb@8
  1558
    assert(identity);
vb@8
  1559
    assert(identity->user_id);
vb@8
  1560
    assert(identity->user_id[0]);
vb@8
  1561
    assert(identity->fpr);
vb@8
  1562
    assert(identity->fpr[0]);
vb@8
  1563
vb@191
  1564
    if (!(session && identity && identity->user_id && identity->user_id[0] &&
vb@191
  1565
                identity->fpr && identity->fpr[0]))
vb@191
  1566
        return PEP_ILLEGAL_VALUE;
vb@191
  1567
vb@8
  1568
    identity->comm_type = PEP_ct_unknown;
vb@8
  1569
vb@46
  1570
    sqlite3_reset(session->get_trust);
vb@951
  1571
    sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
vb@951
  1572
            SQLITE_STATIC);
vb@46
  1573
    sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
vb@8
  1574
vb@46
  1575
    result = sqlite3_step(session->get_trust);
vb@8
  1576
    switch (result) {
vb@8
  1577
    case SQLITE_ROW: {
vb@951
  1578
        int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
vb@951
  1579
                0);
vb@8
  1580
        identity->comm_type = comm_type;
vb@8
  1581
        break;
vb@8
  1582
    }
vb@8
  1583
 
vb@8
  1584
    default:
vb@8
  1585
        status = PEP_CANNOT_FIND_IDENTITY;
vb@8
  1586
    }
vb@8
  1587
vb@46
  1588
    sqlite3_reset(session->get_trust);
vb@8
  1589
    return status;
vb@9
  1590
}
vb@9
  1591
vb@251
  1592
DYNAMIC_API PEP_STATUS least_trust(
vb@251
  1593
        PEP_SESSION session,
vb@251
  1594
        const char *fpr,
vb@251
  1595
        PEP_comm_type *comm_type
vb@251
  1596
    )
vb@251
  1597
{
vb@251
  1598
    PEP_STATUS status = PEP_STATUS_OK;
vb@251
  1599
    int result;
vb@251
  1600
vb@251
  1601
    assert(session);
vb@251
  1602
    assert(fpr);
vb@251
  1603
    assert(comm_type);
vb@251
  1604
vb@251
  1605
    if (!(session && fpr && comm_type))
vb@251
  1606
        return PEP_ILLEGAL_VALUE;
vb@251
  1607
vb@632
  1608
    *comm_type = PEP_ct_unknown;
vb@632
  1609
vb@251
  1610
    sqlite3_reset(session->least_trust);
vb@251
  1611
    sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
vb@251
  1612
vb@251
  1613
    result = sqlite3_step(session->least_trust);
vb@251
  1614
    switch (result) {
vb@251
  1615
        case SQLITE_ROW: {
vb@259
  1616
            int _comm_type = sqlite3_column_int(session->least_trust, 0);
vb@259
  1617
            *comm_type = (PEP_comm_type) _comm_type;
vb@251
  1618
            break;
vb@251
  1619
        }
vb@251
  1620
        default:
edouard@1632
  1621
            // never reached because of sql min()
vb@251
  1622
            status = PEP_CANNOT_FIND_IDENTITY;
vb@251
  1623
    }
vb@251
  1624
vb@251
  1625
    sqlite3_reset(session->least_trust);
vb@251
  1626
    return status;
vb@251
  1627
}
vb@251
  1628
vb@24
  1629
DYNAMIC_API PEP_STATUS decrypt_and_verify(
vb@24
  1630
    PEP_SESSION session, const char *ctext, size_t csize,
krista@1397
  1631
    const char *dsigtext, size_t dsigsize,
vb@24
  1632
    char **ptext, size_t *psize, stringlist_t **keylist
vb@24
  1633
    )
vb@24
  1634
{
vb@191
  1635
    assert(session);
vb@191
  1636
    assert(ctext);
vb@191
  1637
    assert(csize);
vb@191
  1638
    assert(ptext);
vb@191
  1639
    assert(psize);
vb@191
  1640
    assert(keylist);
vb@191
  1641
vb@939
  1642
    if (!(session && ctext && csize && ptext && psize && keylist))
vb@191
  1643
        return PEP_ILLEGAL_VALUE;
vb@191
  1644
vb@939
  1645
    return session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
krista@1397
  1646
            session, ctext, csize, dsigtext, dsigsize, ptext, psize, keylist);
vb@24
  1647
}
vb@24
  1648
vb@24
  1649
DYNAMIC_API PEP_STATUS encrypt_and_sign(
vb@24
  1650
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
vb@24
  1651
    size_t psize, char **ctext, size_t *csize
vb@24
  1652
    )
vb@24
  1653
{
vb@191
  1654
    assert(session);
vb@191
  1655
    assert(keylist);
vb@191
  1656
    assert(ptext);
vb@191
  1657
    assert(psize);
vb@191
  1658
    assert(ctext);
vb@191
  1659
    assert(csize);
vb@191
  1660
vb@191
  1661
    if (!(session && keylist && ptext && psize && ctext && csize))
vb@191
  1662
        return PEP_ILLEGAL_VALUE;
vb@191
  1663
vb@951
  1664
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
vb@951
  1665
            keylist, ptext, psize, ctext, csize);
vb@24
  1666
}
vb@24
  1667
krista@1639
  1668
PEP_STATUS encrypt_only(
krista@1639
  1669
    PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
krista@1639
  1670
    size_t psize, char **ctext, size_t *csize
krista@1639
  1671
    )
krista@1639
  1672
{
krista@1639
  1673
    assert(session);
krista@1639
  1674
    assert(keylist);
krista@1639
  1675
    assert(ptext);
krista@1639
  1676
    assert(psize);
krista@1639
  1677
    assert(ctext);
krista@1639
  1678
    assert(csize);
krista@1639
  1679
krista@1639
  1680
    if (!(session && keylist && ptext && psize && ctext && csize))
krista@1639
  1681
        return PEP_ILLEGAL_VALUE;
krista@1639
  1682
krista@1639
  1683
    return session->cryptotech[PEP_crypt_OpenPGP].encrypt_only(session,
krista@1639
  1684
            keylist, ptext, psize, ctext, csize);
krista@1639
  1685
}
krista@1639
  1686
krista@1639
  1687
vb@24
  1688
DYNAMIC_API PEP_STATUS verify_text(
vb@24
  1689
    PEP_SESSION session, const char *text, size_t size,
vb@24
  1690
    const char *signature, size_t sig_size, stringlist_t **keylist
vb@24
  1691
    )
vb@24
  1692
{
vb@191
  1693
    assert(session);
vb@191
  1694
    assert(text);
vb@191
  1695
    assert(size);
vb@191
  1696
    assert(signature);
vb@191
  1697
    assert(sig_size);
vb@191
  1698
    assert(keylist);
vb@191
  1699
vb@191
  1700
    if (!(session && text && size && signature && sig_size && keylist))
vb@191
  1701
        return PEP_ILLEGAL_VALUE;
vb@191
  1702
vb@951
  1703
    return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
vb@951
  1704
            size, signature, sig_size, keylist);
vb@24
  1705
}
vb@24
  1706
vb@24
  1707
DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
vb@24
  1708
{
vb@191
  1709
    assert(session);
vb@191
  1710
    assert(fpr);
vb@191
  1711
vb@191
  1712
    if (!(session && fpr))
vb@191
  1713
        return PEP_ILLEGAL_VALUE;
vb@191
  1714
vb@46
  1715
    return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
vb@24
  1716
}
vb@24
  1717
vb@24
  1718
DYNAMIC_API PEP_STATUS export_key(
vb@46
  1719
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@24
  1720
    )
vb@24
  1721
{
vb@191
  1722
    assert(session);
vb@191
  1723
    assert(fpr);
vb@191
  1724
    assert(key_data);
vb@191
  1725
    assert(size);
vb@191
  1726
vb@191
  1727
    if (!(session && fpr && key_data && size))
vb@191
  1728
        return PEP_ILLEGAL_VALUE;
vb@191
  1729
vb@951
  1730
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@1103
  1731
            key_data, size, false);
vb@1103
  1732
}
vb@1103
  1733
vb@1133
  1734
DYNAMIC_API PEP_STATUS export_secrect_key(
vb@1103
  1735
        PEP_SESSION session, const char *fpr, char **key_data, size_t *size
vb@1103
  1736
    )
vb@1103
  1737
{
vb@1103
  1738
    assert(session);
vb@1103
  1739
    assert(fpr);
vb@1103
  1740
    assert(key_data);
vb@1103
  1741
    assert(size);
vb@1103
  1742
vb@1103
  1743
    if (!(session && fpr && key_data && size))
vb@1103
  1744
        return PEP_ILLEGAL_VALUE;
vb@1103
  1745
vb@1103
  1746
    // don't accept key IDs but full fingerprints only
vb@1103
  1747
    if (strlen(fpr) < 16)
vb@1103
  1748
        return PEP_ILLEGAL_VALUE;
vb@1103
  1749
vb@1103
  1750
    return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
vb@1103
  1751
            key_data, size, true);
vb@24
  1752
}
vb@24
  1753
vb@24
  1754
DYNAMIC_API PEP_STATUS find_keys(
vb@46
  1755
        PEP_SESSION session, const char *pattern, stringlist_t **keylist
vb@24
  1756
    )
vb@24
  1757
{
vb@191
  1758
    assert(session);
vb@191
  1759
    assert(pattern);
vb@191
  1760
    assert(keylist);
vb@191
  1761
vb@191
  1762
    if (!(session && pattern && keylist))
vb@191
  1763
        return PEP_ILLEGAL_VALUE;
vb@191
  1764
vb@951
  1765
    return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern,
vb@951
  1766
            keylist);
vb@24
  1767
}
vb@24
  1768
krista@1005
  1769
vb@24
  1770
DYNAMIC_API PEP_STATUS generate_keypair(
vb@46
  1771
        PEP_SESSION session, pEp_identity *identity
vb@24
  1772
    )
vb@24
  1773
{
vb@191
  1774
    assert(session);
vb@191
  1775
    assert(identity);
vb@191
  1776
    assert(identity->address);
vb@298
  1777
    assert(identity->fpr == NULL || identity->fpr[0] == 0);
vb@191
  1778
    assert(identity->username);
vb@191
  1779
vb@298
  1780
    if (!(session && identity && identity->address &&
vb@951
  1781
            (identity->fpr == NULL || identity->fpr[0] == 0) &&
vb@951
  1782
            identity->username))
vb@191
  1783
        return PEP_ILLEGAL_VALUE;
vb@191
  1784
vb@944
  1785
    PEP_STATUS status =
vb@944
  1786
        session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session,
vb@944
  1787
                identity);
vb@944
  1788
    if (status != PEP_STATUS_OK)
vb@944
  1789
        return status;
vb@944
  1790
vb@944
  1791
    return status;
vb@24
  1792
}
vb@24
  1793
vb@9
  1794
DYNAMIC_API PEP_STATUS get_key_rating(
vb@46
  1795
        PEP_SESSION session,
vb@46
  1796
        const char *fpr,
vb@46
  1797
        PEP_comm_type *comm_type
vb@9
  1798
    )
vb@9
  1799
{
vb@191
  1800
    assert(session);
vb@191
  1801
    assert(fpr);
vb@191
  1802
    assert(comm_type);
vb@191
  1803
vb@191
  1804
    if (!(session && fpr && comm_type))
vb@191
  1805
        return PEP_ILLEGAL_VALUE;
vb@191
  1806
vb@951
  1807
    return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr,
vb@951
  1808
            comm_type);
vb@24
  1809
}
vb@9
  1810
Edouard@728
  1811
DYNAMIC_API PEP_STATUS import_key(
Edouard@728
  1812
        PEP_SESSION session,
Edouard@728
  1813
        const char *key_data,
Edouard@728
  1814
        size_t size,
Edouard@728
  1815
        identity_list **private_keys
Edouard@728
  1816
    )
vb@24
  1817
{
vb@191
  1818
    assert(session);
vb@191
  1819
    assert(key_data);
vb@191
  1820
vb@191
  1821
    if (!(session && key_data))
vb@191
  1822
        return PEP_ILLEGAL_VALUE;
vb@191
  1823
vb@951
  1824
    return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data,
vb@951
  1825
            size, private_keys);
vb@24
  1826
}
vb@9
  1827
vb@24
  1828
DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
vb@24
  1829
{
vb@191
  1830
    assert(session);
vb@191
  1831
    assert(pattern);
vb@191
  1832
vb@191
  1833
    if (!(session && pattern))
vb@191
  1834
        return PEP_ILLEGAL_VALUE;
vb@191
  1835
vb@46
  1836
    return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
vb@24
  1837
}
vb@9
  1838
vb@24
  1839
DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
vb@24
  1840
{
vb@191
  1841
    assert(session);
vb@191
  1842
    assert(pattern);
vb@191
  1843
vb@191
  1844
    if (!(session && pattern))
vb@191
  1845
        return PEP_ILLEGAL_VALUE;
vb@191
  1846
vb@46
  1847
    return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
vb@14
  1848
}
vb@196
  1849
vb@201
  1850
DYNAMIC_API PEP_STATUS renew_key(
vb@201
  1851
        PEP_SESSION session,
vb@201
  1852
        const char *fpr,
vb@201
  1853
        const timestamp *ts
vb@201
  1854
    )
vb@196
  1855
{
vb@196
  1856
    assert(session);
vb@200
  1857
    assert(fpr);
vb@196
  1858
vb@200
  1859
    if (!(session && fpr))
vb@196
  1860
        return PEP_ILLEGAL_VALUE;
vb@196
  1861
vb@201
  1862
    return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
vb@196
  1863
}
vb@196
  1864
vb@211
  1865
DYNAMIC_API PEP_STATUS revoke_key(
vb@211
  1866
        PEP_SESSION session,
vb@211
  1867
        const char *fpr,
vb@211
  1868
        const char *reason
vb@211
  1869
    )
vb@197
  1870
{
vb@197
  1871
    assert(session);
vb@200
  1872
    assert(fpr);
vb@197
  1873
vb@200
  1874
    if (!(session && fpr))
vb@197
  1875
        return PEP_ILLEGAL_VALUE;
vb@197
  1876
vb@214
  1877
    return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
vb@214
  1878
            reason);
vb@197
  1879
}
vb@197
  1880
vb@214
  1881
DYNAMIC_API PEP_STATUS key_expired(
vb@214
  1882
        PEP_SESSION session,
vb@214
  1883
        const char *fpr,
Edouard@701
  1884
        const time_t when,
vb@214
  1885
        bool *expired
vb@214
  1886
    )
vb@214
  1887
{
vb@214
  1888
    assert(session);
vb@214
  1889
    assert(fpr);
vb@214
  1890
    assert(expired);
vb@214
  1891
vb@214
  1892
    if (!(session && fpr && expired))
vb@214
  1893
        return PEP_ILLEGAL_VALUE;
vb@214
  1894
vb@214
  1895
    return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
Edouard@701
  1896
            when, expired);
vb@214
  1897
}
vb@214
  1898
Edouard@694
  1899
DYNAMIC_API PEP_STATUS key_revoked(
vb@951
  1900
       PEP_SESSION session,
vb@951
  1901
       const char *fpr,
vb@951
  1902
       bool *revoked
vb@951
  1903
   )
Edouard@694
  1904
{
Edouard@694
  1905
    assert(session);
Edouard@694
  1906
    assert(fpr);
Edouard@694
  1907
    assert(revoked);
Edouard@694
  1908
    
Edouard@694
  1909
    if (!(session && fpr && revoked))
Edouard@694
  1910
        return PEP_ILLEGAL_VALUE;
Edouard@694
  1911
    
Edouard@694
  1912
    return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
vb@951
  1913
            revoked);
Edouard@694
  1914
}
Edouard@694
  1915
vb@451
  1916
static void _clean_log_value(char *text)
vb@451
  1917
{
vb@451
  1918
    if (text) {
vb@451
  1919
        for (char *c = text; *c; c++) {
vb@722
  1920
            if (*c < 32 && *c != '\n')
vb@451
  1921
                *c = 32;
vb@451
  1922
            else if (*c == '"')
vb@451
  1923
                *c = '\'';
vb@451
  1924
        }
vb@451
  1925
    }
vb@451
  1926
}
vb@451
  1927
vb@450
  1928
static char *_concat_string(char *str1, const char *str2, char delim)
vb@450
  1929
{
vb@454
  1930
    str2 = str2 ? str2 : "";
vb@450
  1931
    size_t len1 = str1 ? strlen(str1) : 0;
vb@450
  1932
    size_t len2 = strlen(str2);
vb@450
  1933
    size_t len = len1 + len2 + 3;
vb@450
  1934
    char * result = realloc(str1, len + 1);
vb@450
  1935
vb@450
  1936
    if (result) {
vb@450
  1937
        result[len1] = '"';
vb@450
  1938
        strcpy(result + len1 + 1, str2);
vb@450
  1939
        result[len - 2] = '"';
vb@450
  1940
        result[len - 1] = delim;
vb@450
  1941
        result[len] = 0;
vb@450
  1942
    }
vb@450
  1943
    else {
vb@450
  1944
        free(str1);
vb@450
  1945
    }
vb@450
  1946
vb@450
  1947
    return result;
vb@450
  1948
}
vb@450
  1949
vb@450
  1950
DYNAMIC_API PEP_STATUS get_crashdump_log(
vb@450
  1951
        PEP_SESSION session,
vb@450
  1952
        int maxlines,
vb@450
  1953
        char **logdata
vb@450
  1954
    )
vb@450
  1955
{
vb@450
  1956
    PEP_STATUS status = PEP_STATUS_OK;
vb@450
  1957
    char *_logdata= NULL;
vb@450
  1958
vb@450
  1959
    assert(session);
vb@450
  1960
    assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
vb@450
  1961
    assert(logdata);
vb@450
  1962
vb@450
  1963
    if (!(session && logdata && maxlines >= 0 && maxlines <=
vb@450
  1964
            CRASHDUMP_MAX_LINES))
vb@450
  1965
        return PEP_ILLEGAL_VALUE;
vb@450
  1966
vb@632
  1967
    *logdata = NULL;
vb@632
  1968
vb@450
  1969
    int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
vb@458
  1970
    const char *timestamp = NULL;
vb@458
  1971
    const char *title = NULL;
vb@458
  1972
    const char *entity = NULL;
vb@458
  1973
    const char *desc = NULL;
vb@458
  1974
    const char *comment = NULL;
vb@450
  1975
vb@450
  1976
    sqlite3_reset(session->crashdump);
vb@452
  1977
    sqlite3_bind_int(session->crashdump, 1, limit);
vb@450
  1978
vb@450
  1979
    int result;
vb@450
  1980
vb@450
  1981
    do {
vb@450
  1982
        result = sqlite3_step(session->crashdump);
vb@450
  1983
        switch (result) {
vb@450
  1984
        case SQLITE_ROW:
vb@951
  1985
            timestamp = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1986
                    0);
vb@951
  1987
            title   = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1988
                    1);
vb@951
  1989
            entity  = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1990
                    2);
vb@951
  1991
            desc    = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1992
                    3);
vb@951
  1993
            comment = (const char *) sqlite3_column_text(session->crashdump,
vb@951
  1994
                    4);
vb@452
  1995
vb@452
  1996
            _logdata = _concat_string(_logdata, timestamp, ',');
vb@452
  1997
            if (_logdata == NULL)
vb@452
  1998
                goto enomem;
vb@450
  1999
vb@450
  2000
            _logdata = _concat_string(_logdata, title, ',');
vb@450
  2001
            if (_logdata == NULL)
vb@450
  2002
                goto enomem;
vb@450
  2003
vb@450
  2004
            _logdata = _concat_string(_logdata, entity, ',');
vb@450
  2005
            if (_logdata == NULL)
vb@450
  2006
                goto enomem;
vb@450
  2007
vb@450
  2008
            _logdata = _concat_string(_logdata, desc, ',');
vb@450
  2009
            if (_logdata == NULL)
vb@450
  2010
                goto enomem;
vb@450
  2011
vb@450
  2012
            _logdata = _concat_string(_logdata, comment, '\n');
vb@450
  2013
            if (_logdata == NULL)
vb@450
  2014
                goto enomem;
vb@450
  2015
vb@687
  2016
            _clean_log_value(_logdata);
vb@450
  2017
            break;
vb@450
  2018
vb@450
  2019
        case SQLITE_DONE:
vb@450
  2020
            break;
vb@450
  2021
vb@450
  2022
        default:
vb@450
  2023
            status = PEP_UNKNOWN_ERROR;
vb@450
  2024
            result = SQLITE_DONE;
vb@450
  2025
        }
vb@450
  2026
    } while (result != SQLITE_DONE);
vb@450
  2027
vb@450
  2028
    sqlite3_reset(session->crashdump);
vb@450
  2029
    if (status == PEP_STATUS_OK)
vb@450
  2030
        *logdata = _logdata;
vb@450
  2031
vb@450
  2032
    goto the_end;
vb@450
  2033
vb@450
  2034
enomem:
vb@450
  2035
    status = PEP_OUT_OF_MEMORY;
vb@450
  2036
vb@450
  2037
the_end:
roker@1853
  2038
    return ADD_TO_LOG(status);
vb@450
  2039
}
vb@450
  2040
vb@458
  2041
DYNAMIC_API PEP_STATUS get_languagelist(
vb@458
  2042
        PEP_SESSION session,
vb@458
  2043
        char **languages
vb@458
  2044
    )
vb@458
  2045
{
vb@458
  2046
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  2047
    char *_languages= NULL;
vb@458
  2048
vb@458
  2049
    assert(session);
vb@458
  2050
    assert(languages);
vb@458
  2051
vb@458
  2052
    if (!(session && languages))
vb@458
  2053
        return PEP_ILLEGAL_VALUE;
vb@458
  2054
vb@632
  2055
    *languages = NULL;
vb@632
  2056
vb@458
  2057
    const char *lang = NULL;
vb@458
  2058
    const char *name = NULL;
vb@617
  2059
    const char *phrase = NULL;
vb@458
  2060
vb@458
  2061
    sqlite3_reset(session->languagelist);
vb@458
  2062
vb@458
  2063
    int result;
vb@458
  2064
vb@458
  2065
    do {
vb@458
  2066
        result = sqlite3_step(session->languagelist);
vb@458
  2067
        switch (result) {
vb@458
  2068
        case SQLITE_ROW:
vb@951
  2069
            lang = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  2070
                    0);
vb@951
  2071
            name = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  2072
                    1);
vb@951
  2073
            phrase = (const char *) sqlite3_column_text(session->languagelist,
vb@951
  2074
                    2);
vb@458
  2075
vb@458
  2076
            _languages = _concat_string(_languages, lang, ',');
vb@458
  2077
            if (_languages == NULL)
vb@458
  2078
                goto enomem;
vb@458
  2079
vb@617
  2080
            _languages = _concat_string(_languages, name, ',');
vb@617
  2081
            if (_languages == NULL)
vb@617
  2082
                goto enomem;
vb@617
  2083
vb@617
  2084
            _languages = _concat_string(_languages, phrase, '\n');
vb@458
  2085
            if (_languages == NULL)
vb@458
  2086
                goto enomem;
vb@458
  2087
vb@458
  2088
            break;
vb@458
  2089
vb@458
  2090
        case SQLITE_DONE:
vb@458
  2091
            break;
vb@458
  2092
vb@458
  2093
        default:
vb@458
  2094
            status = PEP_UNKNOWN_ERROR;
vb@458
  2095
            result = SQLITE_DONE;
vb@458
  2096
        }
vb@458
  2097
    } while (result != SQLITE_DONE);
vb@458
  2098
vb@458
  2099
    sqlite3_reset(session->languagelist);
vb@458
  2100
    if (status == PEP_STATUS_OK)
vb@458
  2101
        *languages = _languages;
vb@458
  2102
vb@458
  2103
    goto the_end;
vb@458
  2104
vb@458
  2105
enomem:
vb@458
  2106
    status = PEP_OUT_OF_MEMORY;
vb@458
  2107
vb@458
  2108
the_end:
vb@458
  2109
    return status;
vb@458
  2110
}
vb@458
  2111
vb@458
  2112
DYNAMIC_API PEP_STATUS get_phrase(
vb@458
  2113
        PEP_SESSION session,
vb@458
  2114
        const char *lang,
vb@458
  2115
        int phrase_id,
vb@458
  2116
        char **phrase
vb@458
  2117
    )
vb@458
  2118
{
vb@458
  2119
    PEP_STATUS status = PEP_STATUS_OK;
vb@458
  2120
vb@479
  2121
    assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
vb@479
  2122
    if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
vb@458
  2123
        return PEP_ILLEGAL_VALUE;
vb@458
  2124
vb@632
  2125
    *phrase = NULL;
vb@632
  2126
vb@458
  2127
    sqlite3_reset(session->i18n_token);
roker@529
  2128
    sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
roker@529
  2129
    sqlite3_bind_int(session->i18n_token, 2, phrase_id);
vb@458
  2130
vb@458
  2131
    const char *_phrase = NULL;
vb@458
  2132
    int result;
vb@458
  2133
vb@458
  2134
    result = sqlite3_step(session->i18n_token);
vb@458
  2135
    switch (result) {
vb@458
  2136
    case SQLITE_ROW:
vb@458
  2137
        _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
vb@458
  2138
        break;
vb@458
  2139
vb@458
  2140
    case SQLITE_DONE:
vb@458
  2141
        status = PEP_PHRASE_NOT_FOUND;
vb@458
  2142
        break;
vb@458
  2143
vb@458
  2144
    default:
vb@458
  2145
        status = PEP_UNKNOWN_ERROR;
vb@458
  2146
    }
vb@458
  2147
vb@458
  2148
    if (status == PEP_STATUS_OK) {
vb@458
  2149
        *phrase = strdup(_phrase);
vb@458
  2150
        if (*phrase == NULL)
vb@458
  2151
            goto enomem;
vb@458
  2152
    }
vb@459
  2153
vb@459
  2154
    sqlite3_reset(session->i18n_token);
vb@458
  2155
    goto the_end;
vb@458
  2156
vb@458
  2157
enomem:
vb@458
  2158
    status = PEP_OUT_OF_MEMORY;
vb@458
  2159
vb@458
  2160
the_end:
vb@458
  2161
    return status;
vb@458
  2162
}
vb@458
  2163
vb@1085
  2164
static PEP_STATUS _get_sequence_value(PEP_SESSION session, const char *name,
vb@1085
  2165
        int32_t *value)
vb@1085
  2166
{
vb@1086
  2167
    assert(session && name && value);
vb@1086
  2168
    if (!(session && name && value))
vb@1086
  2169
        return PEP_ILLEGAL_VALUE;
vb@1086
  2170
vb@1085
  2171
    PEP_STATUS status = PEP_STATUS_OK;
vb@1085
  2172
vb@1085
  2173
    sqlite3_reset(session->sequence_value2);
vb@1085
  2174
    sqlite3_bind_text(session->sequence_value2, 1, name, -1,
vb@1085
  2175
            SQLITE_STATIC);
vb@1085
  2176
    int result = sqlite3_step(session->sequence_value2);
vb@1085
  2177
    switch (result) {
vb@1085
  2178
        case SQLITE_ROW: {
vb@1085
  2179
            int32_t _value = (int32_t)
vb@1086
  2180
                    sqlite3_column_int(session->sequence_value2, 0);
vb@1086
  2181
            int _own = (int)
vb@1086
  2182
                    sqlite3_column_int(session->sequence_value2, 1);
vb@1085
  2183
            *value = _value;
vb@1086
  2184
            if (_own)
vb@1086
  2185
                status = PEP_OWN_SEQUENCE;
vb@1085
  2186
            break;
vb@1085
  2187
        }
vb@1091
  2188
        case SQLITE_DONE:
vb@1091
  2189
            status = PEP_RECORD_NOT_FOUND;
vb@1091
  2190
            break;
vb@1085
  2191
        default:
vb@1085
  2192
            status = PEP_UNKNOWN_ERROR;
vb@1085
  2193
    }
vb@1085
  2194
    sqlite3_reset(session->sequence_value2);
vb@1085
  2195
vb@1085
  2196
    return status;
vb@1085
  2197
}
vb@1085
  2198
vb@1086
  2199
static PEP_STATUS _increment_sequence_value(PEP_SESSION session,
vb@1086
  2200
        const char *name, int own)
vb@1085
  2201
{
vb@1086
  2202
    assert(session && name);
vb@1086
  2203
    if (!(session && name))
vb@1086
  2204
        return PEP_ILLEGAL_VALUE;
vb@1086
  2205
vb@1085
  2206
    sqlite3_reset(session->sequence_value1);
vb@1085
  2207
    sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
vb@1086
  2208
    sqlite3_bind_int(session->sequence_value1, 2, own);
vb@1085
  2209
    int result = sqlite3_step(session->sequence_value1);
vb@1085
  2210
    assert(result == SQLITE_DONE);
vb@1085
  2211
    sqlite3_reset(session->sequence_value1);
vb@1085
  2212
    if (result == SQLITE_DONE)
vb@1085
  2213
        return PEP_STATUS_OK;
vb@1085
  2214
    else
vb@1085
  2215
        return PEP_CANNOT_INCREASE_SEQUENCE;
vb@1085
  2216
}
vb@1085
  2217
vb@1087
  2218
static PEP_STATUS _set_sequence_value(PEP_SESSION session,
vb@1091
  2219
        const char *name, int32_t value, int own)
vb@1087
  2220
{
vb@1087
  2221
    assert(session && name && value > 0);
vb@1087
  2222
    if (!(session && name && value > 0))
vb@1087
  2223
        return PEP_ILLEGAL_VALUE;
vb@1087
  2224
vb@1087
  2225
    sqlite3_reset(session->sequence_value3);
vb@1087
  2226
    sqlite3_bind_text(session->sequence_value3, 1, name, -1, SQLITE_STATIC);
vb@1087
  2227
    sqlite3_bind_int(session->sequence_value3, 2, value);
vb@1091
  2228
    sqlite3_bind_int(session->sequence_value3, 3, own);
vb@1087
  2229
    int result = sqlite3_step(session->sequence_value3);
vb@1087
  2230
    assert(result == SQLITE_DONE);
vb@1087
  2231
    sqlite3_reset(session->sequence_value3);
vb@1087
  2232
    if (result == SQLITE_DONE)
vb@1087
  2233
        return PEP_STATUS_OK;
vb@1087
  2234
    else
vb@1087
  2235
        return PEP_CANNOT_SET_SEQUENCE_VALUE;
vb@1087
  2236
}
vb@1087
  2237
vb@632
  2238
DYNAMIC_API PEP_STATUS sequence_value(
vb@632
  2239
        PEP_SESSION session,
vb@1085
  2240
        char *name,
vb@652
  2241
        int32_t *value
vb@632
  2242
    )
vb@632
  2243
{
vb@632
  2244
    PEP_STATUS status = PEP_STATUS_OK;
edouard@1568
  2245
    int result;
vb@632
  2246
vb@632
  2247
    assert(session);
vb@1085
  2248
    assert(name && value && *value >= 0);
vb@632
  2249
vb@1085
  2250
    if (!(session && name && value && *value >= 0))
vb@632
  2251
        return PEP_ILLEGAL_VALUE;
vb@632
  2252
vb@1086
  2253
    int own = 0;
vb@1085
  2254
    if (!name[0]) {
markus@1259
  2255
        pEpUUID uuid;
vb@1085
  2256
        uuid_generate_random(uuid);
vb@1085
  2257
        uuid_unparse_upper(uuid, name);
vb@1086
  2258
        own = 1;
vb@633
  2259
    }
vb@633
  2260
    else {
edouard@1603
  2261
        if (name == session->sync_session->sync_uuid || 
edouard@1603
  2262
            strcmp(name, session->sync_session->sync_uuid) == 0)
vb@1091
  2263
            own = 1;
vb@1091
  2264
    }
krista@1032
  2265
vb@1085
  2266
    if (*value) {
edouard@1568
  2267
        sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@1085
  2268
        int32_t old_value = 0;
vb@1085
  2269
        status = _get_sequence_value(session, name, &old_value);
vb@1091
  2270
        if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
edouard@1568
  2271
        {
edouard@1568
  2272
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@1085
  2273
            return status;
edouard@1568
  2274
        }
vb@1085
  2275
vb@1087
  2276
        if (old_value >= *value) {
edouard@1568
  2277
            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
vb@1085
  2278
            return PEP_SEQUENCE_VIOLATED;
vb@632
  2279
        }
vb@1087
  2280
        else {
vb@1091
  2281
            status = _set_sequence_value(session, name, *value, own);
edouard@1568
  2282
            if (status == PEP_STATUS_OK) {
edouard@1568
  2283
                result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
edouard@1568
  2284
                if (result == SQLITE_OK)
edouard@1568
  2285
                    return PEP_STATUS_OK;
edouard@1568
  2286
                else
edouard@1568
  2287
                    return PEP_COMMIT_FAILED;
edouard@1568
  2288
            } else {
edouard@1568
  2289
                sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
edouard@1568
  2290
                return status;
edouard@1568
  2291
            }
vb@1087
  2292
        }
krista@1032
  2293
    }
vb@1085
  2294
vb@1085
  2295
    assert(*value == 0);
edouard@1568
  2296
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
vb@1086
  2297
    status = _increment_sequence_value(session, name, own);
vb@1086
  2298
    if (status == PEP_STATUS_OK) {
vb@1085
  2299
        status = _get_sequence_value(session, name, value);
edouard@1572
  2300
    }
edouard@1572
  2301
    if (status == PEP_STATUS_OK || status == PEP_OWN_SEQUENCE) {
edouard@1568
  2302
        result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
edouard@1568
  2303
        if (result == SQLITE_OK){
edouard@1568
  2304
            assert(*value < INT32_MAX);
edouard@1568
  2305
            if (*value == INT32_MAX){
edouard@1568
  2306
                return PEP_CANNOT_INCREASE_SEQUENCE;
edouard@1568
  2307
            }
edouard@1572
  2308
            return status;
edouard@1568
  2309
        } else {
edouard@1568
  2310
            return PEP_COMMIT_FAILED;
edouard@1568
  2311
        }
edouard@1568
  2312
    } else {
edouard@1568
  2313
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
edouard@1568
  2314
        return status;
vb@632
  2315
    }
vb@632
  2316
    return status;
vb@632
  2317
}
vb@632
  2318
Edouard@693
  2319
DYNAMIC_API PEP_STATUS set_revoked(
Edouard@693
  2320
       PEP_SESSION session,
Edouard@693
  2321
       const char *revoked_fpr,
Edouard@693
  2322
       const char *replacement_fpr,
Edouard@693
  2323
       const uint64_t revocation_date
Edouard@693
  2324
    )
Edouard@693
  2325
{
Edouard@693
  2326
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  2327
    
Edouard@693
  2328
    assert(session &&
Edouard@693
  2329
           revoked_fpr && revoked_fpr[0] &&
Edouard@693
  2330
           replacement_fpr && replacement_fpr[0]
Edouard@693
  2331
          );
Edouard@693
  2332
    
Edouard@693
  2333
    if (!(session &&
Edouard@693
  2334
          revoked_fpr && revoked_fpr[0] &&
Edouard@693
  2335
          replacement_fpr && replacement_fpr[0]
Edouard@693
  2336
         ))
Edouard@693
  2337
        return PEP_ILLEGAL_VALUE;
Edouard@693
  2338
    
Edouard@693
  2339
    sqlite3_reset(session->set_revoked);
Edouard@693
  2340
    sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
vb@951
  2341
    sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1,
vb@951
  2342
            SQLITE_STATIC);
Edouard@693
  2343
    sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
Edouard@693
  2344
Edouard@693
  2345
    int result;
Edouard@693
  2346
    
Edouard@693
  2347
    result = sqlite3_step(session->set_revoked);
Edouard@693
  2348
    switch (result) {
Edouard@693
  2349
        case SQLITE_DONE:
Edouard@693
  2350
            status = PEP_STATUS_OK;
Edouard@693
  2351
            break;
Edouard@693
  2352
            
Edouard@693
  2353
        default:
Edouard@693
  2354
            status = PEP_UNKNOWN_ERROR;
Edouard@693
  2355
    }
Edouard@693
  2356
    
Edouard@693
  2357
    sqlite3_reset(session->set_revoked);
Edouard@693
  2358
    return status;
Edouard@693
  2359
}
Edouard@693
  2360
Edouard@693
  2361
DYNAMIC_API PEP_STATUS get_revoked(
Edouard@693
  2362
        PEP_SESSION session,
Edouard@693
  2363
        const char *fpr,
Edouard@693
  2364
        char **revoked_fpr,
Edouard@693
  2365
        uint64_t *revocation_date
Edouard@693
  2366
    )
Edouard@693
  2367
{
Edouard@693
  2368
    PEP_STATUS status = PEP_STATUS_OK;
Edouard@693
  2369
Edouard@693
  2370
    assert(session &&
Edouard@693
  2371
           revoked_fpr &&
Edouard@693
  2372
           fpr && fpr[0]
Edouard@693
  2373
          );
Edouard@693
  2374
    
Edouard@693
  2375
    if (!(session &&
Edouard@693
  2376
           revoked_fpr &&
Edouard@693
  2377
           fpr && fpr[0]
Edouard@693
  2378
          ))
Edouard@693
  2379
        return PEP_ILLEGAL_VALUE;
Edouard@693
  2380
Edouard@693
  2381
    *revoked_fpr = NULL;
Edouard@693
  2382
    *revocation_date = 0;
Edouard@693
  2383
Edouard@693
  2384
    sqlite3_reset(session->get_revoked);
Edouard@693
  2385
    sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
Edouard@693
  2386
Edouard@693
  2387
    int result;
Edouard@693
  2388
    
Edouard@693
  2389
    result = sqlite3_step(session->get_revoked);
Edouard@693
  2390
    switch (result) {
Edouard@693
  2391
        case SQLITE_ROW: {
vb@951
  2392
            *revoked_fpr = strdup((const char *)
vb@951
  2393
                    sqlite3_column_text(session->get_revoked, 0));
Edouard@693
  2394
            if(*revoked_fpr)
vb@951
  2395
                *revocation_date = sqlite3_column_int64(session->get_revoked,
vb@951
  2396
                        1);
Edouard@693
  2397
            else
Edouard@693
  2398
                status = PEP_OUT_OF_MEMORY;
Edouard@693
  2399
Edouard@693
  2400
            break;
Edouard@693
  2401
        }
Edouard@693
  2402
        default:
Edouard@693
  2403
            status = PEP_CANNOT_FIND_IDENTITY;
Edouard@693
  2404
    }
Edouard@693
  2405
Edouard@693
  2406
    sqlite3_reset(session->get_revoked);
Edouard@693
  2407
Edouard@693
  2408
    return status;
Edouard@693
  2409
}
Edouard@693
  2410
vb@958
  2411
PEP_STATUS key_created(
vb@958
  2412
        PEP_SESSION session,
vb@958
  2413
        const char *fpr,
vb@958
  2414
        time_t *created
vb@958
  2415
    )
vb@958
  2416
{
vb@958
  2417
    assert(session && fpr && created);
vb@958
  2418
    if (!(session && fpr && created))
vb@958
  2419
        return PEP_ILLEGAL_VALUE;
vb@958
  2420
vb@958
  2421
    return session->cryptotech[PEP_crypt_OpenPGP].key_created(session, fpr,
vb@958
  2422
            created);
vb@958
  2423
}
vb@958
  2424
krista@1357
  2425
PEP_STATUS find_private_keys(PEP_SESSION session, const char* pattern,
krista@1357
  2426
                             stringlist_t **keylist) {
edouard@1760
  2427
    assert(session && keylist);
edouard@1760
  2428
    if (!(session && keylist))
krista@1357
  2429
        return PEP_ILLEGAL_VALUE;
krista@1357
  2430
    
krista@1357
  2431
    return session->cryptotech[PEP_crypt_OpenPGP].find_private_keys(session, pattern,
krista@1357
  2432
                                                                    keylist);
krista@1357
  2433
}
krista@1357
  2434
krista@1011
  2435
DYNAMIC_API const char* get_engine_version() {
krista@1011
  2436
    return PEP_ENGINE_VERSION;
krista@1011
  2437
}
krista@1011
  2438
krista@1011
  2439
vb@482
  2440
DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session)
vb@482
  2441
{
vb@482
  2442
    assert(session);
vb@482
  2443
vb@482
  2444
    if (!session)
vb@482
  2445
        return PEP_ILLEGAL_VALUE;
vb@482
  2446
fdik@494
  2447
    int int_result = sqlite3_exec(
fdik@494
  2448
        session->db,
fdik@494
  2449
        "delete from identity where address like '%@peptest.ch' ;",
fdik@494
  2450
        NULL,
fdik@494
  2451
        NULL,
fdik@494
  2452
        NULL
fdik@494
  2453
    );
fdik@494
  2454
    assert(int_result == SQLITE_OK);
vb@482
  2455
edouard@1670
  2456
    if (int_result != SQLITE_OK)
edouard@1670
  2457
        return PEP_UNKNOWN_ERROR;
edouard@1670
  2458
fdik@494
  2459
    return PEP_STATUS_OK;
vb@482
  2460
}
roker@1722
  2461
roker@1722
  2462
#ifdef DEBUG_ERRORSTACK
roker@1722
  2463
PEP_STATUS session_add_error(PEP_SESSION session, const char* file, unsigned line, PEP_STATUS status)
roker@1722
  2464
{
roker@1722
  2465
    char logline[48];
roker@1722
  2466
    if(status>0)
roker@1722
  2467
    {
roker@1734
  2468
        snprintf(logline,47, "%.24s:%u status=%u (0x%x)", file, line, status, status);
roker@1722
  2469
    }else{
roker@1734
  2470
        snprintf(logline,47, "%.24s:%u status=%i.", file, line, status);
roker@1722
  2471
    }
roker@1722
  2472
    stringlist_add(session->errorstack, logline); // logline is copied! :-)
roker@1722
  2473
    return status;
roker@1722
  2474
}
roker@1722
  2475
roker@1722
  2476
DYNAMIC_API const stringlist_t* get_errorstack(PEP_SESSION session)
roker@1722
  2477
{
roker@1722
  2478
    return session->errorstack;
roker@1722
  2479
}
roker@1722
  2480
roker@1777
  2481
DYNAMIC_API void clear_errorstack(PEP_SESSION session)
roker@1777
  2482
{
roker@1801
  2483
    const int old_len = stringlist_length(session->errorstack);
roker@1801
  2484
    char buf[48];
roker@1777
  2485
    free_stringlist(session->errorstack);
roker@1801
  2486
    snprintf(buf, 47, "(%i elements cleared)", old_len);
roker@1801
  2487
    session->errorstack = new_stringlist(buf);
roker@1777
  2488
}
roker@1777
  2489
roker@1722
  2490
#else
roker@1722
  2491
roker@1722
  2492
static stringlist_t* dummy_errorstack = NULL;
roker@1722
  2493
roker@1722
  2494
DYNAMIC_API const stringlist_t* get_errorstack(PEP_SESSION session)
roker@1722
  2495
{
roker@1777
  2496
    if(dummy_errorstack == NULL)
roker@1777
  2497
    {
roker@1777
  2498
        dummy_errorstack = new_stringlist("( Please recompile pEpEngine with -DDEBUG_ERRORSTACK )");
roker@1777
  2499
    }
roker@1777
  2500
roker@1722
  2501
    return dummy_errorstack;
roker@1722
  2502
}
roker@1722
  2503
roker@1777
  2504
DYNAMIC_API void clear_errorstack(PEP_SESSION session)
roker@1777
  2505
{
roker@1777
  2506
    // nothing to do here
roker@1777
  2507
}
roker@1777
  2508
roker@1722
  2509
#endif