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