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