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