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