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