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