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