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