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