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