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