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