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