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