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