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