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