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