src/pEpEngine.c
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 07 Jan 2020 22:35:39 +0100
branchkrista-local-7-Jan-2020
changeset 4312 1d1672a6dfb9
parent 4293 327480b07e3d
child 4315 afece9dedddf
permissions -rw-r--r--
A metric buttload of code that I finally need to stop shelving and unshelving - new key reset, with bugs.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 #include "dynamic_api.h"
     6 #include "cryptotech.h"
     7 #include "transport.h"
     8 #include "blacklist.h"
     9 #include "KeySync_fsm.h"
    10 
    11 #include <time.h>
    12 #include <stdlib.h>
    13 
    14 #ifdef _PEP_SQLITE_DEBUG
    15 #include <sqlite3.h>
    16 #endif
    17 
    18 static volatile int init_count = -1;
    19 
    20 // sql overloaded functions - modified from sqlite3.c
    21 static void _sql_lower(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
    22     const char *z2;
    23     int n;
    24     z2 = (char*)sqlite3_value_text(argv[0]);
    25     n = sqlite3_value_bytes(argv[0]);
    26     /* Verify that the call to _bytes() does not invalidate the _text() pointer */
    27     assert( z2==(char*)sqlite3_value_text(argv[0]) );
    28     if( z2 ){
    29         char *z1 = (char*)sqlite3_malloc(n+1);
    30         if( z1 ){
    31             for(int i=0; i<n; i++){
    32                 char c = z2[i];
    33                 char c_mod = c | 0x20;
    34                 if (c_mod < 0x61 || c_mod > 0x7a)
    35                     c_mod = c;
    36                 z1[i] = c_mod;
    37             }
    38             z1[n] = '\0';
    39             sqlite3_result_text(ctx, z1, n, sqlite3_free);
    40         }
    41     }
    42 }
    43 
    44 #ifdef _PEP_SQLITE_DEBUG
    45 int sql_trace_callback (unsigned trace_constant, 
    46                         void* context_ptr,
    47                         void* P,
    48                         void* X) {
    49     switch (trace_constant) {
    50         case SQLITE_TRACE_STMT:
    51             fprintf(stderr, "SQL_DEBUG: STMT - ");
    52             const char* X_str = (const char*) X;
    53             if (!EMPTYSTR(X_str) && X_str[0] == '-' && X_str[1] == '-')
    54                 fprintf(stderr, "%s\n", X_str);
    55             else
    56                 fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));    
    57             break;
    58         case SQLITE_TRACE_ROW:
    59             fprintf(stderr, "SQL_DEBUG: ROW - ");
    60             fprintf(stderr, "%s\n", sqlite3_expanded_sql((sqlite3_stmt*)P));
    61             break;            
    62         case SQLITE_TRACE_CLOSE:
    63             fprintf(stderr, "SQL_DEBUG: CLOSE - ");
    64             break;
    65         default:
    66             break;
    67     }
    68     return 0;
    69 }
    70 #endif
    71 
    72 // sql manipulation statements
    73 static const char *sql_log = 
    74     "insert into log (title, entity, description, comment)"
    75      "values (?1, ?2, ?3, ?4);";
    76 
    77 static const char *sql_trustword = 
    78     "select id, word from wordlist where lang = lower(?1) "
    79     "and id = ?2 ;";
    80 
    81 // FIXME?: problems if we don't have a key for the user - we get nothing
    82 static const char *sql_get_identity =  
    83     "select fpr, username, comm_type, lang,"
    84     "   identity.flags | pgp_keypair.flags,"
    85     "   is_own, pEp_version_major, pEp_version_minor"
    86     "   from identity"
    87     "   join person on id = identity.user_id"
    88     "   join pgp_keypair on fpr = identity.main_key_id"
    89     "   join trust on id = trust.user_id"
    90     "       and pgp_keypair_fpr = identity.main_key_id"    
    91     "   where (case when (address = ?1) then (1)"
    92     "               when (lower(address) = lower(?1)) then (1)"
    93     "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    94     "               else 0"
    95     "          end) = 1"
    96     "   and identity.user_id = ?2" 
    97     "   order by is_own desc, "
    98     "   timestamp desc; ";
    99 
   100 static const char *sql_get_identities_by_main_key_id =  
   101     "select address, identity.user_id, username, comm_type, lang,"
   102     "   identity.flags | pgp_keypair.flags,"
   103     "   is_own, pEp_version_major, pEp_version_minor"
   104     "   from identity"
   105     "   join person on id = identity.user_id"
   106     "   join pgp_keypair on fpr = identity.main_key_id"
   107     "   join trust on id = trust.user_id"
   108     "       and pgp_keypair_fpr = identity.main_key_id"    
   109     "   where identity.main_key_id = ?1" 
   110     "   order by is_own desc, "
   111     "   timestamp desc; ";
   112 
   113 static const char *sql_get_identity_without_trust_check =  
   114     "select identity.main_key_id, username, lang,"
   115     "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
   116     "   from identity"
   117     "   join person on id = identity.user_id"
   118     "   where (case when (address = ?1) then (1)"
   119     "               when (lower(address) = lower(?1)) then (1)"
   120     "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
   121     "               else 0"
   122     "          end) = 1"
   123     "   and identity.user_id = ?2 "
   124     "   order by is_own desc, "
   125     "   timestamp desc; ";
   126 
   127 static const char *sql_get_identities_by_address =  
   128     "select user_id, identity.main_key_id, username, lang,"
   129     "   identity.flags, is_own, pEp_version_major, pEp_version_minor"
   130     "   from identity"
   131     "   join person on id = identity.user_id"
   132     "   where (case when (address = ?1) then (1)"
   133     "               when (lower(address) = lower(?1)) then (1)"
   134     "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
   135     "               else 0"
   136     "          end) = 1 "
   137     "   order by is_own desc, "
   138     "   timestamp desc; ";
   139     
   140 static const char *sql_get_identities_by_userid =  
   141     "select address, fpr, username, comm_type, lang,"
   142     "   identity.flags | pgp_keypair.flags,"
   143     "   is_own, pEp_version_major, pEp_version_minor"
   144     "   from identity"
   145     "   join person on id = identity.user_id"
   146     "   join pgp_keypair on fpr = identity.main_key_id"
   147     "   join trust on id = trust.user_id"
   148     "       and pgp_keypair_fpr = identity.main_key_id"    
   149     "   where identity.user_id = ?1" 
   150     "   order by is_own desc, "
   151     "   timestamp desc; ";
   152 
   153 static const char *sql_replace_identities_fpr =  
   154     "update identity"
   155     "   set main_key_id = ?1 "
   156     "   where main_key_id = ?2 ;";
   157     
   158 static const char *sql_remove_fpr_as_default =
   159     "update person set main_key_id = NULL where main_key_id = ?1 ;"
   160     "update identity set main_key_id = NULL where main_key_id = ?1 ;";
   161 
   162 // Set person, but if already exist, only update.
   163 // if main_key_id already set, don't touch.
   164 static const char *sql_set_person = 
   165      "insert into person (id, username, lang, main_key_id)"
   166      "  values (?1, ?2, ?3, ?4) ;";
   167 
   168 static const char *sql_update_person = 
   169     "update person "
   170     "   set username = ?2, "
   171     "       lang = ?3, "
   172     "       main_key_id =  "
   173     "           (select coalesce( "
   174     "               (select main_key_id from person where id = ?1), " 
   175     "                upper(replace(?4,' ',''))))"         
   176     "   where id = ?1 ;";
   177 
   178 // Will cascade.
   179 static const char *sql_delete_person = 
   180      "delete from person where id = ?1 ;";
   181 
   182 static const char *sql_set_as_pEp_user =
   183     "update person set is_pEp_user = 1 "
   184     "   where id = ?1 ; ";
   185 
   186 static const char *sql_is_pEp_user =
   187     "select is_pEp_user from person "
   188     "   where id = ?1 ; ";
   189 
   190 static const char* sql_exists_person = 
   191     "select count(*) from person "
   192     "   where id = ?1 ;";
   193 
   194 // This will cascade to identity and trust
   195 static const char* sql_replace_userid =
   196     "update person set id = ?1 " 
   197     "   where id = ?2;";
   198 
   199 // Hopefully this cascades and removes trust entries...
   200 static const char *sql_delete_key =
   201     "delete from pgp_keypair "
   202     "   where fpr = ?1 ; ";
   203 
   204 static const char *sql_replace_main_user_fpr =  
   205     "update person "
   206     "   set main_key_id = ?1 "
   207     "   where id = ?2 ;";
   208 
   209 static const char *sql_get_main_user_fpr =  
   210     "select main_key_id from person"
   211     "   where id = ?1 ;";
   212 
   213 static const char *sql_refresh_userid_default_key =
   214     "update person "
   215     "   set main_key_id = "
   216     "       (select identity.main_key_id from identity "
   217     "           join trust on trust.user_id = identity.user_id "
   218     "               and trust.pgp_keypair_fpr = identity.main_key_id "
   219     "           join person on identity.user_id = identity.user_id "
   220     "       where identity.user_id = ?1 "
   221     "       order by trust.comm_type desc "
   222     "       limit 1) "
   223     "where id = ?1 ; ";
   224 
   225 static const char *sql_set_pgp_keypair = 
   226     "insert or ignore into pgp_keypair (fpr) "
   227     "values (upper(replace(?1,' ',''))) ;";
   228 
   229 static const char* sql_exists_identity_entry = 
   230     "select count(*) from identity "
   231     "   where (case when (address = ?1) then (1)"
   232     "               when (lower(address) = lower(?1)) then (1)"
   233     "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
   234     "               else 0"
   235     "          end) = 1"
   236     "    and user_id = ?2;";
   237  
   238 static const char *sql_set_identity_entry = 
   239     "insert into identity ("
   240     "       address, main_key_id, "
   241     "       user_id, flags, is_own,"
   242     "       pEp_version_major, pEp_version_minor"
   243     "   ) values ("
   244     "       ?1,"
   245     "       upper(replace(?2,' ','')),"
   246     "       ?3,"
   247     "       ?4,"
   248     "       ?5,"
   249     "       ?6,"
   250     "       ?7"
   251     "   );";
   252     
   253 static const char* sql_update_identity_entry =    
   254     "update identity "
   255     "   set main_key_id = upper(replace(?2,' ','')), "
   256     "       flags = ?4, " 
   257     "       is_own = ?5, "
   258     "       pEp_version_major = ?6, "
   259     "       pEp_version_minor = ?7 "    
   260     "   where (case when (address = ?1) then (1)"
   261     "               when (lower(address) = lower(?1)) then (1)"
   262     "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1) "
   263     "               else 0 "
   264     "          end) = 1 "
   265     "          and user_id = ?3 ;";
   266 
   267     // " (select"
   268     // "   coalesce("
   269     // "    (select flags from identity"
   270     // "     where address = ?1 and"
   271     // "           user_id = ?3),"
   272     // "    0)"
   273     // " ) | (?4 & 255)"
   274     /* set_identity ignores previous flags, and doesn't filter machine flags */
   275         
   276 static const char *sql_set_identity_flags = 
   277     "update identity set flags = "
   278     "    ((?1 & 65535) | (select flags from identity"
   279     "                    where (case when (address = ?2) then (1)"
   280     "                                when (lower(address) = lower(?2)) then (1)"
   281     "                                when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
   282     "                                else 0 "    
   283     "                           end) = 1 "
   284     "                           and user_id = ?3)) "
   285     "   where (case when (address = ?2) then (1)"
   286     "               when (lower(address) = lower(?2)) then (1)"
   287     "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
   288     "               else 0"
   289     "          end) = 1"
   290     "          and user_id = ?3 ;";
   291 
   292 static const char *sql_unset_identity_flags = 
   293     "update identity set flags = "
   294     "    ( ~(?1 & 65535) & (select flags from identity"
   295     "                    where (case when (address = ?2) then (1)"
   296     "                                when (lower(address) = lower(?2)) then (1)"
   297     "                                when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
   298     "                                else 0 "    
   299     "                           end) = 1 "
   300     "                           and user_id = ?3)) "
   301     "   where (case when (address = ?2) then (1)"
   302     "               when (lower(address) = lower(?2)) then (1)"
   303     "               when (replace(lower(address),'.','') = replace(lower(?2),'.','')) then (1)"
   304     "               else 0"
   305     "          end) = 1"
   306     "          and user_id = ?3 ;";
   307 
   308 static const char *sql_set_pEp_version =
   309     "update identity "
   310     "   set pEp_version_major = ?1, "
   311     "       pEp_version_minor = ?2 "
   312     "   where (case when (address = ?3) then (1)"
   313     "               when (lower(address) = lower(?3)) then (1)"
   314     "               when (replace(lower(address),'.','') = replace(lower(?3),'.','')) then (1) "
   315     "               else 0 "
   316     "          end) = 1 "
   317     "          and user_id = ?4 ;";
   318 
   319 static const char *sql_upgrade_pEp_version_by_user_id =
   320     "update identity "
   321     "   set pEp_version_major = ?1, "
   322     "       pEp_version_minor = ?2 "
   323     "       where user_id = ?3 "
   324     "           and (case when (pEp_version_major < ?1) then (1)"
   325     "                     when (pEp_version_major > ?1) then (0)"
   326     "                     when (pEp_version_minor < ?2) then (1)"
   327     "                     else 0 "
   328     "           end) = 1 ;";
   329 
   330 static const char *sql_set_trust =
   331     "insert into trust (user_id, pgp_keypair_fpr, comm_type) "
   332     "values (?1, upper(replace(?2,' ','')), ?3) ;";
   333 
   334 static const char *sql_update_trust =
   335     "update trust set comm_type = ?3 " 
   336     "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
   337 
   338 static const char *sql_clear_trust_info =
   339     "delete from trust "
   340     "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
   341 
   342 static const char *sql_update_trust_to_pEp =
   343     "update trust set comm_type = comm_type + 71 "
   344     "   where (user_id = ?1 "
   345     "          and (case when (comm_type = 56) then (1) "
   346     "                    when (comm_type = 184) then (1) "
   347     "                    else 0"
   348     "               end) = 1); ";
   349 
   350 static const char* sql_exists_trust_entry = 
   351     "select count(*) from trust "
   352     "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
   353     
   354 static const char *sql_update_trust_for_fpr =
   355     "update trust "
   356     "set comm_type = ?1 "
   357     "where pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
   358 
   359 static const char *sql_get_trust = 
   360     "select comm_type from trust where user_id = ?1 "
   361     "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
   362 
   363 static const char *sql_get_trust_by_userid = 
   364     "select pgp_keypair_fpr, comm_type from trust where user_id = ?1 ";
   365 
   366 static const char *sql_least_trust = 
   367     "select min(comm_type) from trust where"
   368     " pgp_keypair_fpr = upper(replace(?1,' ',''))"
   369     " and comm_type != 0;"; // ignores PEP_ct_unknown
   370     // returns PEP_ct_unknown only when no known trust is recorded
   371 
   372 static const char *sql_mark_as_compromised = 
   373     "update trust not indexed set comm_type = 15"
   374     " where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
   375     
   376 static const char *sql_crashdump = 
   377     "select timestamp, title, entity, description, comment"
   378     " from log order by timestamp desc limit ?1 ;";
   379 
   380 static const char *sql_languagelist = 
   381     "select i18n_language.lang, name, phrase" 
   382     " from i18n_language join i18n_token using (lang) where i18n_token.id = 1000;" ;
   383 
   384 static const char *sql_i18n_token = 
   385     "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
   386     
   387 // blacklist
   388 static const char *sql_blacklist_add = 
   389     "insert or ignore into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
   390     "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
   391     "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
   392 
   393 static const char *sql_blacklist_delete =
   394     "delete from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
   395 
   396 static const char *sql_blacklist_is_listed = 
   397     "select count(*) from blacklist_keys where fpr = upper(replace(?1,' ','')) ;";
   398 
   399 static const char *sql_blacklist_retrieve = 
   400     "select * from blacklist_keys ;";
   401                 
   402 
   403 // Own keys
   404 // We only care if it's 0 or non-zero
   405 static const char *sql_own_key_is_listed = 
   406     "select count(*) from ("
   407     "   select pgp_keypair_fpr from trust"
   408     "      join identity on trust.user_id = identity.user_id"
   409     "      where pgp_keypair_fpr = upper(replace(?1,' ',''))"
   410     "           and identity.is_own = 1"
   411     ");";
   412     
   413 static const char *sql_is_own_address =
   414     "select count(*) from ("
   415     "   select address from identity"
   416     "       where (case when (address = ?1) then (1)"
   417     "                   when (lower(address) = lower(?1)) then (1)"
   418     "                   when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
   419     "                   else 0"
   420     "           end) = 1 "
   421     "           and identity.is_own = 1"
   422     ");";
   423 
   424 static const char *sql_own_identities_retrieve =  
   425     "select address, fpr, identity.user_id, username,"
   426     "   lang, identity.flags | pgp_keypair.flags, pEp_version_major, pEp_version_minor"
   427     "   from identity"
   428     "   join person on id = identity.user_id"
   429     "   join pgp_keypair on fpr = identity.main_key_id"
   430     "   join trust on id = trust.user_id"
   431     "       and pgp_keypair_fpr = identity.main_key_id"
   432     "   where identity.is_own = 1"
   433     "       and (identity.flags & ?1) = 0;";
   434 
   435 static const char *sql_own_keys_retrieve = 
   436     "select pgp_keypair_fpr from trust"
   437     "   join identity on trust.user_id = identity.user_id"
   438     "   where identity.is_own = 1";
   439 
   440 static const char* sql_get_user_default_key =
   441     "select main_key_id from person" 
   442     "   where id = ?1;";
   443 
   444 static const char* sql_get_all_keys_for_user =
   445     "select pgp_keypair_fpr from trust"
   446     "   where user_id = ?1; ";
   447 
   448 static const char* sql_get_default_own_userid =
   449     "select id from person"
   450     "   join identity on id = identity.user_id"
   451     "   where identity.is_own = 1";
   452 
   453 // Sequence
   454 static const char *sql_sequence_value1 = 
   455     "insert or replace into sequences (name, value) "
   456     "values (?1, "
   457     "       (select coalesce((select value + 1 from sequences "
   458     "           where name = ?1), 1 ))); ";
   459 
   460 static const char *sql_sequence_value2 = 
   461     "select value from sequences where name = ?1 ;";
   462 
   463 // Revocation tracking
   464 static const char *sql_set_revoked =
   465     "insert or replace into revoked_keys ("
   466     "    revoked_fpr, replacement_fpr, revocation_date) "
   467     "values (upper(replace(?1,' ','')),"
   468     "        upper(replace(?2,' ','')),"
   469     "        ?3) ;";
   470         
   471 static const char *sql_get_revoked = 
   472     "select revoked_fpr, revocation_date from revoked_keys"
   473     "    where replacement_fpr = upper(replace(?1,' ','')) ;";
   474 
   475 static const char *sql_get_replacement_fpr = 
   476     "select replacement_fpr, revocation_date from revoked_keys"
   477     "    where revoked_fpr = upper(replace(?1,' ','')) ;";
   478 
   479 static const char *sql_get_userid_alias_default =
   480     "select default_id from alternate_user_id "
   481     "   where alternate_id = ?1 ; ";
   482 
   483 // Revocation tracking
   484 static const char *sql_add_mistrusted_key =
   485     "insert or replace into mistrusted_keys (fpr) "
   486     "   values (upper(replace(?1,' ',''))) ;";
   487         
   488 static const char *sql_delete_mistrusted_key = 
   489     "delete from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
   490 
   491 static const char *sql_is_mistrusted_key = 
   492     "select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
   493 
   494 static const char *sql_add_userid_alias =
   495     "insert or replace into alternate_user_id (alternate_id, default_id) "
   496     "values (?2, ?1) ;";
   497 
   498 static const char *sql_add_into_social_graph =
   499     "insert or replace into social_graph(own_userid, own_address, contact_userid) "
   500     "values (?1, ?2, ?3) ;";
   501 
   502 static const char *sql_get_own_address_binding_from_contact =
   503     "select own_address from social_graph where own_userid = ?1 and contact_userid = ?2 ;";
   504 
   505 static const char *sql_set_revoke_contact_as_notified =
   506     "insert or replace into revocation_contact_list(fpr, own_address, contact_id) values (?1, ?2, ?3) ;";
   507     
   508 static const char *sql_get_contacted_ids_from_revoke_fpr =
   509     "select * from revocation_contact_list where fpr = ?1 ;";
   510 
   511 static const char *sql_was_id_for_revoke_contacted = 
   512     "select count(*) from revocation_contact_list where fpr = ?1 and own_address = ?2 and contact_id = ?3 ;";
   513 
   514 static const char *sql_has_id_contacted_address =
   515     "select count(*) from social_graph where own_address = ?1 and contact_userid = ?2 ;";
   516 
   517 // We only need user_id and address, since in the main usage, we'll call update_identity
   518 // on this anyway when sending out messages.
   519 static const char *sql_get_last_contacted =
   520     "select user_id, address from identity where datetime('now') < datetime(timestamp, '+14 days') ; ";
   521         
   522 static int user_version(void *_version, int count, char **text, char **name)
   523 {
   524     assert(_version);
   525     assert(count == 1);
   526     assert(text && text[0]);
   527     if (!(_version && count == 1 && text && text[0]))
   528         return -1;
   529 
   530     int *version = (int *) _version;
   531     *version = atoi(text[0]);
   532     return 0;
   533 }
   534 
   535 // TODO: refactor and generalise these two functions if possible.
   536 static int db_contains_table(PEP_SESSION session, const char* table_name) {
   537     if (!session || !table_name)
   538         return -1;
   539     
   540     // Table names can't be SQL parameters, so we do it this way.
   541     
   542     // these two must be the same number.
   543     char sql_buf[500];
   544     const size_t max_q_len = 500;
   545     
   546     size_t t_size, q_size;
   547     
   548     const char* q1 = "SELECT name FROM sqlite_master WHERE type='table' AND name='{"; // 61
   549     const char* q2 = "}';";       // 3
   550     
   551     q_size = 64;
   552     t_size = strlen(table_name);
   553     
   554     size_t query_len = q_size + t_size + 1;
   555 
   556     if (query_len > max_q_len)
   557         return -1;
   558 
   559     strlcpy(sql_buf, q1, max_q_len);
   560     strlcat(sql_buf, table_name, max_q_len);
   561     strlcat(sql_buf, q2, max_q_len);
   562 
   563     sqlite3_stmt *stmt; 
   564 
   565     sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
   566 
   567     int retval = 0;
   568 
   569     int rc = sqlite3_step(stmt);  
   570     if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
   571         retval = 1;
   572     }
   573 
   574     sqlite3_finalize(stmt);      
   575         
   576     return retval;
   577         
   578 }
   579 
   580 static int table_contains_column(PEP_SESSION session, const char* table_name,
   581                                                       const char* col_name) {
   582 
   583 
   584     if (!session || !table_name || !col_name)
   585         return -1;
   586         
   587     // Table names can't be SQL parameters, so we do it this way.
   588     
   589     // these two must be the same number.
   590     char sql_buf[500];
   591     const size_t max_q_len = 500;
   592     
   593     size_t t_size, c_size, q_size;
   594     
   595     const char* q1 = "SELECT "; // 7
   596     const char* q2 = " from "; // 6
   597     const char* q3 = ";";       // 1
   598     
   599     q_size = 14;
   600     t_size = strlen(table_name);
   601     c_size = strlen(col_name);
   602     
   603     size_t query_len = q_size + c_size + t_size + 1;
   604     
   605     if (query_len > max_q_len)
   606         return -1;
   607 
   608     strlcpy(sql_buf, q1, max_q_len);
   609     strlcat(sql_buf, col_name, max_q_len);
   610     strlcat(sql_buf, q2, max_q_len);
   611     strlcat(sql_buf, table_name, max_q_len);
   612     strlcat(sql_buf, q3, max_q_len);
   613 
   614     sqlite3_stmt *stmt; 
   615 
   616     sqlite3_prepare_v2(session->db, sql_buf, -1, &stmt, NULL);
   617 
   618     int retval = 0;
   619 
   620     int rc = sqlite3_step(stmt);  
   621     if (rc == SQLITE_DONE || rc == SQLITE_OK || rc == SQLITE_ROW) {
   622         retval = 1;
   623     }
   624 
   625     sqlite3_finalize(stmt);      
   626         
   627     return retval;
   628 }
   629 
   630 PEP_STATUS repair_altered_tables(PEP_SESSION session) {
   631     PEP_STATUS status = PEP_STATUS_OK;
   632     
   633     const int _PEP_MAX_AFFECTED = 5;
   634     char** table_names = calloc(_PEP_MAX_AFFECTED, sizeof(char*));
   635     if (!table_names)
   636         return PEP_OUT_OF_MEMORY;
   637 
   638     const char* sql_query = "select tbl_name from sqlite_master WHERE sql LIKE '%REFERENCES%' AND sql LIKE '%_old%';";
   639     sqlite3_stmt *stmt; 
   640     sqlite3_prepare_v2(session->db, sql_query, -1, &stmt, NULL);
   641     int i = 0;
   642     int int_result = 0;
   643     while ((int_result = sqlite3_step(stmt)) == SQLITE_ROW && i < _PEP_MAX_AFFECTED) {
   644         table_names[i++] = strdup((const char*)(sqlite3_column_text(stmt, 0)));
   645     }
   646     
   647     sqlite3_finalize(stmt);      
   648 
   649     if ((int_result != SQLITE_DONE && int_result != SQLITE_OK) || i > (_PEP_MAX_AFFECTED + 1)) {
   650         status = PEP_UNKNOWN_DB_ERROR;
   651         goto pEp_free;
   652     }
   653         
   654     for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
   655         const char* table_name = table_names[i];
   656         if (!table_name)
   657             break;
   658             
   659         if (strcmp(table_name, "identity") == 0) {
   660             int_result = sqlite3_exec(session->db,
   661                 "PRAGMA foreign_keys=off;\n"
   662                 "BEGIN TRANSACTION;\n"
   663                 "create table _identity_new (\n"
   664                 "   address text,\n"
   665                 "   user_id text\n"
   666                 "       references person (id)\n"
   667                 "       on delete cascade on update cascade,\n"
   668                 "   main_key_id text\n"
   669                 "       references pgp_keypair (fpr)\n"
   670                 "       on delete set null,\n"
   671                 "   comment text,\n"
   672                 "   flags integer default 0,\n"
   673                 "   is_own integer default 0,\n"
   674                 "   timestamp integer default (datetime('now')),\n"
   675                 "   primary key (address, user_id)\n"
   676                 ");\n"
   677                 "INSERT INTO _identity_new SELECT * from identity;\n"
   678                 "DROP TABLE identity;\n"
   679                 "ALTER TABLE _identity_new RENAME TO identity;\n"
   680                 "COMMIT;\n"
   681                 "PRAGMA foreign_keys=on;"
   682                 ,
   683                 NULL,
   684                 NULL,
   685                 NULL
   686             );
   687             assert(int_result == PEP_STATUS_OK);
   688         }
   689         else if (strcmp(table_name, "trust") == 0) {
   690             int_result = sqlite3_exec(session->db,
   691                 "PRAGMA foreign_keys=off;\n"
   692                 "BEGIN TRANSACTION;\n"
   693                 "create table _trust_new (\n"
   694                 "   user_id text not null\n"
   695                 "       references person (id)\n"
   696                 "       on delete cascade on update cascade,\n"
   697                 "   pgp_keypair_fpr text not null\n"
   698                 "       references pgp_keypair (fpr)\n"
   699                 "       on delete cascade,\n"
   700                 "   comm_type integer not null,\n"
   701                 "   comment text,\n"
   702                 "   primary key (user_id, pgp_keypair_fpr)\n"                
   703                 ");\n"
   704                 "INSERT INTO _trust_new SELECT * from trust;\n"
   705                 "DROP TABLE trust;\n"
   706                 "ALTER TABLE _trust_new RENAME TO trust;\n"
   707                 "COMMIT;\n"
   708                 "PRAGMA foreign_keys=on;"
   709                 ,
   710                 NULL,
   711                 NULL,
   712                 NULL
   713             );             
   714             assert(int_result == PEP_STATUS_OK);                       
   715         }
   716         else if (strcmp(table_name, "alternate_user_id") == 0) {
   717             int_result = sqlite3_exec(session->db,
   718                 "PRAGMA foreign_keys=off;\n"
   719                 "BEGIN TRANSACTION;\n"
   720                 "create table _alternate_user_id_new (\n"
   721                 "    default_id text references person (id)\n"
   722                 "       on delete cascade on update cascade,\n"
   723                 "    alternate_id text primary key\n"
   724                 ");\n"
   725                 "INSERT INTO _alternate_user_id_new SELECT * from alternate_user_id;\n"
   726                 "DROP TABLE alternate_user_id;\n"
   727                 "ALTER TABLE _alternate_user_id_new RENAME TO alternate_user_id;\n"
   728                 "COMMIT;\n"
   729                 "PRAGMA foreign_keys=on;"                
   730                 ,
   731                 NULL,
   732                 NULL,
   733                 NULL
   734             );
   735             assert(int_result == PEP_STATUS_OK);
   736         }
   737         else if (strcmp(table_name, "revocation_contact_list") == 0) {
   738             int_result = sqlite3_exec(session->db,
   739                 "PRAGMA foreign_keys=off;\n"
   740                 "BEGIN TRANSACTION;\n"
   741                 "create table _revocation_contact_list_new (\n"
   742                 "   fpr text not null references pgp_keypair (fpr)\n"
   743                 "       on delete cascade,\n"
   744                 "   contact_id text not null references person (id)\n"
   745                 "       on delete cascade on update cascade,\n"
   746                 "   timestamp integer default (datetime('now')),\n"
   747                 "   PRIMARY KEY(fpr, contact_id)\n"            
   748                 ");\n"
   749                 "INSERT INTO _revocation_contact_list_new SELECT * from revocation_contact_list;\n"
   750                 "DROP TABLE revocation_contact_list;\n"
   751                 "ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
   752                 "COMMIT;\n"
   753                 "PRAGMA foreign_keys=on;"                
   754                 ,
   755                 NULL,
   756                 NULL,
   757                 NULL
   758             );      
   759             assert(int_result == PEP_STATUS_OK);                              
   760         }
   761         else if (strcmp(table_name, "social_graph")) {
   762             int_result = sqlite3_exec(session->db,
   763                 "PRAGMA foreign_keys=off;\n"
   764                 "BEGIN TRANSACTION;\n"
   765                 "create table _social_new (\n"
   766                 "    own_userid text,\n"
   767                 "    own_address text,\n"
   768                 "    contact_userid text,\n"
   769                 "    CONSTRAINT fk_own_identity\n"
   770                 "       FOREIGN KEY(own_address, own_userid)\n" 
   771                 "       REFERENCES identity(address, user_id)\n"
   772                 "       ON DELETE CASCADE ON UPDATE CASCADE\n"
   773                 ");\n"
   774                 "INSERT INTO _social_graph_new SELECT * from social_graph;\n"
   775                 "DROP TABLE social_graph;\n"
   776                 "ALTER TABLE _social_graph_new RENAME TO social_graph;\n"
   777                 "COMMIT;\n"
   778                 "PRAGMA foreign_keys=on;"                
   779                 ,
   780                 NULL,
   781                 NULL,
   782                 NULL
   783             );
   784             assert(int_result == PEP_STATUS_OK);                                    
   785         }        
   786     }
   787     
   788     int_result = sqlite3_exec(
   789         session->db,
   790         "PRAGMA foreign_key_check;\n"
   791         ,
   792         NULL,
   793         NULL,
   794         NULL
   795     );
   796     assert(int_result == SQLITE_OK);
   797 
   798 pEp_free:
   799     for (i = 0; i < _PEP_MAX_AFFECTED; i++) {
   800         if (table_names[i])
   801             free(table_names[i]);
   802         else
   803             break;
   804     }
   805     free(table_names);
   806     return status;
   807 }
   808 void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
   809   fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
   810 }
   811 
   812 static PEP_STATUS upgrade_revoc_contact_to_13(PEP_SESSION session) {
   813     // I HATE SQLITE.
   814     PEP_STATUS status = PEP_STATUS_OK;
   815     int int_result = 0;
   816 
   817     // Ok, first we ADD the column so we can USE it.
   818     // We will end up propagating the "error" this first time 
   819     // (one-to-one revoke-replace relationships), but since key reset
   820     // hasn't been used in production, this is not a customer-facing 
   821     // issue.
   822     int_result = sqlite3_exec(
   823         session->db,
   824         "alter table revocation_contact_list\n"
   825         "   add column own_address text\n",
   826         NULL,
   827         NULL,
   828         NULL
   829     );
   830     assert(int_result == SQLITE_OK);
   831 
   832     sqlite3_stmt* update_revoked_w_addr_stmt = NULL;
   833     const char* sql_query = "update revocation_contact_list set own_address = ?1 where fpr = ?2;";
   834     sqlite3_prepare_v2(session->db, sql_query, -1, &update_revoked_w_addr_stmt, NULL);
   835                 
   836     // the best we can do here is search per address, since these
   837     // are no longer associated with an identity. For now, if we find 
   838     // something we can't add an address to, we'll delete the record.
   839     // this should not, in the current environment, ever happen, but 
   840     // since we need to make the address part of the primary key, it's 
   841     // the right thing to do. sqlite does support null fields in a primary 
   842     // key for a weird version compatibility reason, but that doesn't 
   843     // mean we should use it, and we should be *safe*, not relying 
   844     // on an implementation-specific quirk which might be sanely removed 
   845     // in a future sqlite version.
   846     stringpair_t* revoked_key_to_own_address = NULL;
   847     
   848     identity_list* id_list = NULL;
   849     status = own_identities_retrieve(session, &id_list);
   850 
   851     if (!status || !id_list)
   852         return PEP_STATUS_OK; // it's empty AFAIK (FIXME)
   853     
   854     identity_list* curr_own = id_list;
   855     
   856     // Ok, go through and find any keys associated with this address  
   857     for ( ; curr_own && curr_own->ident; curr_own = curr_own->next) {
   858         if (EMPTYSTR(curr_own->ident->address)) // shouldn't happen
   859             continue;
   860         stringlist_t* keylist = NULL;
   861         status = find_keys(session, curr_own->ident->address, &keylist);
   862         stringlist_t* curr_key = keylist;
   863         for ( ; curr_key && curr_key->value; curr_key = curr_key->next) {
   864             if (EMPTYSTR(curr_key->value))
   865                 continue;
   866                 
   867             // We just do this lazily - if this isn't a revoked key, it 
   868             // won't do anything.
   869             sqlite3_bind_text(update_revoked_w_addr_stmt, 1, curr_own->ident->address, -1,
   870                               SQLITE_STATIC);
   871             sqlite3_bind_text(update_revoked_w_addr_stmt, 2, curr_key->value, -1,
   872                               SQLITE_STATIC);
   873                               
   874             int_result = sqlite3_step(update_revoked_w_addr_stmt);
   875             assert(int_result == SQLITE_DONE);                  
   876             sqlite3_reset(update_revoked_w_addr_stmt);                      
   877         }
   878     }  
   879     sqlite3_finalize(update_revoked_w_addr_stmt);
   880                 
   881     int_result = sqlite3_exec(
   882         session->db,
   883         "delete from revocation_contact_list where own_address is NULL;\n"        
   884         "PRAGMA foreign_keys=off;\n"
   885         "BEGIN TRANSACTION;\n"
   886         "create table if not exists _revocation_contact_list_new (\n"
   887         "   fpr text not null references pgp_keypair (fpr)\n"
   888         "       on delete cascade,\n"
   889         "   own_address text,\n"
   890         "   contact_id text not null references person (id)\n"
   891         "       on delete cascade on update cascade,\n"
   892         "   timestamp integer default (datetime('now')),\n"
   893         "   PRIMARY KEY(fpr, own_address, contact_id)\n"
   894         ");\n"
   895         "INSERT INTO _revocation_contact_list_new (fpr, "
   896         "                                          own_address, "
   897         "                                          contact_id) "
   898         "   SELECT revocation_contact_list.fpr, "
   899         "          revocation_contact_list.own_address, "
   900         "          revocation_contact_list.contact_id "
   901         "   FROM revocation_contact_list "
   902         "   WHERE 1;\n"
   903         "DROP TABLE revocation_contact_list;\n"
   904         "ALTER TABLE _revocation_contact_list_new RENAME TO revocation_contact_list;\n"
   905         "COMMIT;\n"
   906         "\n"
   907         "PRAGMA foreign_keys=on;\n"
   908         ,
   909         NULL,
   910         NULL,
   911         NULL
   912     );
   913     assert(int_result == SQLITE_OK);    
   914     
   915     return status;
   916 }
   917 
   918 #ifdef USE_GPG
   919 PEP_STATUS pgp_import_ultimately_trusted_keypairs(PEP_SESSION session);
   920 #endif // USE_GPG
   921 
   922 DYNAMIC_API PEP_STATUS init(
   923         PEP_SESSION *session,
   924         messageToSend_t messageToSend,
   925         inject_sync_event_t inject_sync_event
   926     )
   927 {
   928     PEP_STATUS status = PEP_STATUS_OK;
   929     int int_result;
   930     
   931     bool in_first = false;
   932     bool very_first = false;
   933 
   934     assert(sqlite3_threadsafe());
   935     if (!sqlite3_threadsafe())
   936         return PEP_INIT_SQLITE3_WITHOUT_MUTEX;
   937 
   938     // a little race condition - but still a race condition
   939     // mitigated by calling caveat (see documentation)
   940 
   941     // this increment is made atomic IN THE ADAPTERS by
   942     // guarding the call to init with the appropriate mutex.
   943     int _count = ++init_count;
   944     if (_count == 0)
   945         in_first = true;
   946     
   947     // Race condition mitigated by calling caveat starts here :
   948     // If another call to init() preempts right now, then preemptive call
   949     // will have in_first false, will not create SQL tables, and following
   950     // calls relying on those tables will fail.
   951     //
   952     // Therefore, as above, adapters MUST guard init() with a mutex.
   953     // 
   954     // Therefore, first session
   955     // is to be created and last session to be deleted alone, and not
   956     // concurently to other sessions creation or deletion.
   957     // We expect adapters to enforce this either by implicitely creating a
   958     // client session, or by using synchronization primitive to protect
   959     // creation/deletion of first/last session from the app.
   960 
   961     assert(session);
   962     if (session == NULL)
   963         return PEP_ILLEGAL_VALUE;
   964 
   965     *session = NULL;
   966 
   967     pEpSession *_session = calloc(1, sizeof(pEpSession));
   968     assert(_session);
   969     if (_session == NULL)
   970         goto enomem;
   971 
   972     _session->version = PEP_ENGINE_VERSION;
   973     _session->messageToSend = messageToSend;
   974     _session->inject_sync_event = inject_sync_event;
   975 
   976 #ifdef DEBUG_ERRORSTACK
   977     _session->errorstack = new_stringlist("init()");
   978 #endif
   979 
   980     assert(LOCAL_DB);
   981     if (LOCAL_DB == NULL) {
   982         status = PEP_INIT_CANNOT_OPEN_DB;
   983         goto pEp_error;
   984     }
   985     
   986 #ifdef _PEP_SQLITE_DEBUG    
   987     sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
   988 #endif
   989 
   990     int_result = sqlite3_open_v2(
   991             LOCAL_DB,
   992             &_session->db,
   993             SQLITE_OPEN_READWRITE
   994                 | SQLITE_OPEN_CREATE
   995                 | SQLITE_OPEN_FULLMUTEX
   996                 | SQLITE_OPEN_PRIVATECACHE,
   997             NULL 
   998         );
   999 
  1000     if (int_result != SQLITE_OK) {
  1001         status = PEP_INIT_CANNOT_OPEN_DB;
  1002         goto pEp_error;
  1003     }
  1004 
  1005     int_result = sqlite3_exec(
  1006             _session->db,
  1007             "PRAGMA locking_mode=NORMAL;\n"
  1008             "PRAGMA journal_mode=WAL;\n",
  1009             NULL,
  1010             NULL,
  1011             NULL
  1012         );
  1013 
  1014 
  1015     sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
  1016 
  1017 #ifdef _PEP_SQLITE_DEBUG
  1018     sqlite3_trace_v2(_session->db, 
  1019         SQLITE_TRACE_STMT | SQLITE_TRACE_ROW | SQLITE_TRACE_CLOSE,
  1020         sql_trace_callback,
  1021         NULL);
  1022 #endif
  1023 
  1024     assert(SYSTEM_DB);
  1025     if (SYSTEM_DB == NULL) {
  1026         status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
  1027         goto pEp_error;
  1028     }
  1029 
  1030     int_result = sqlite3_open_v2(
  1031             SYSTEM_DB, &_session->system_db,
  1032             SQLITE_OPEN_READONLY
  1033                 | SQLITE_OPEN_FULLMUTEX
  1034                 | SQLITE_OPEN_SHAREDCACHE,
  1035             NULL
  1036         );
  1037 
  1038     if (int_result != SQLITE_OK) {
  1039         status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
  1040         goto pEp_error;
  1041     }
  1042 
  1043     sqlite3_busy_timeout(_session->system_db, 1000);
  1044 
  1045 // increment this when patching DDL
  1046 #define _DDL_USER_VERSION "13"
  1047 
  1048     if (in_first) {
  1049 
  1050         int_result = sqlite3_exec(
  1051             _session->db,
  1052                 "create table if not exists version_info (\n"
  1053                 "   id integer primary key,\n"
  1054                 "   timestamp integer default (datetime('now')),\n"
  1055                 "   version text,\n"
  1056                 "   comment text\n"
  1057                 ");\n",
  1058                 NULL,
  1059                 NULL,
  1060                 NULL
  1061         );
  1062         int_result = sqlite3_exec(
  1063             _session->db,
  1064                 "PRAGMA application_id = 0x23423423;\n"
  1065                 "create table if not exists log (\n"
  1066                 "   timestamp integer default (datetime('now')),\n"
  1067                 "   title text not null,\n"
  1068                 "   description text,\n"
  1069                 "   entity text not null,\n"
  1070                 "   comment text\n"
  1071                 ");\n"
  1072                 "create index if not exists log_timestamp on log (\n"
  1073                 "   timestamp\n"
  1074                 ");\n"
  1075                 "create table if not exists pgp_keypair (\n"
  1076                 "   fpr text primary key,\n"
  1077                 "   created integer,\n"
  1078                 "   expires integer,\n"
  1079                 "   comment text,\n"
  1080                 "   flags integer default 0\n"
  1081                 ");\n"
  1082                 "create index if not exists pgp_keypair_expires on pgp_keypair (\n"
  1083                 "   expires\n"
  1084                 ");\n"
  1085                 "create table if not exists person (\n"
  1086                 "   id text primary key,\n"
  1087                 "   username text not null,\n"
  1088                 "   main_key_id text\n"
  1089                 "       references pgp_keypair (fpr)\n"
  1090                 "       on delete set null,\n"
  1091                 "   lang text,\n"
  1092                 "   comment text,\n"
  1093 //                "   device_group text,\n"
  1094                 "   is_pEp_user integer default 0\n"
  1095                 ");\n"
  1096                 "create table if not exists identity (\n"
  1097                 "   address text,\n"
  1098                 "   user_id text\n"
  1099                 "       references person (id)\n"
  1100                 "       on delete cascade on update cascade,\n"
  1101                 "   main_key_id text\n"
  1102                 "       references pgp_keypair (fpr)\n"
  1103                 "       on delete set null,\n"
  1104                 "   comment text,\n"
  1105                 "   flags integer default 0,\n"
  1106                 "   is_own integer default 0,\n"
  1107                 "   pEp_version_major integer default 0,\n"
  1108                 "   pEp_version_minor integer default 0,\n"                
  1109                 "   timestamp integer default (datetime('now')),\n"
  1110                 "   primary key (address, user_id)\n"
  1111                 ");\n"
  1112                 "create index if not exists identity_userid_addr on identity(address, user_id);\n"
  1113                 "create table if not exists trust (\n"
  1114                 "   user_id text not null\n"
  1115                 "       references person (id)\n"
  1116                 "       on delete cascade on update cascade,\n"
  1117                 "   pgp_keypair_fpr text not null\n"
  1118                 "       references pgp_keypair (fpr)\n"
  1119                 "       on delete cascade,\n"
  1120                 "   comm_type integer not null,\n"
  1121                 "   comment text,\n"
  1122                 "   primary key (user_id, pgp_keypair_fpr)\n"
  1123                 ");\n"
  1124                 // blacklist
  1125                 "create table if not exists blacklist_keys (\n"
  1126                 "   fpr text primary key\n"
  1127                 ");\n"
  1128                 // sequences
  1129                 "create table if not exists sequences(\n"
  1130                 "   name text primary key,\n"
  1131                 "   value integer default 0\n"
  1132                 ");\n"
  1133                 "create table if not exists revoked_keys (\n"
  1134                 "   revoked_fpr text primary key,\n"
  1135                 "   replacement_fpr text not null\n"
  1136                 "       references pgp_keypair (fpr)\n"
  1137                 "       on delete cascade,\n"
  1138                 "   revocation_date integer\n"
  1139                 ");\n"
  1140                 // user id aliases
  1141                 "create table if not exists alternate_user_id (\n"
  1142                 "    default_id text references person (id)\n"
  1143                 "       on delete cascade on update cascade,\n"
  1144                 "    alternate_id text primary key\n"
  1145                 ");\n"
  1146                 // mistrusted keys
  1147                 "create table if not exists mistrusted_keys (\n"
  1148                 "    fpr text primary key\n"
  1149                 ");\n"
  1150                 // social graph for key resets
  1151                 "create table if not exists social_graph (\n"
  1152                 "    own_userid text,\n"
  1153                 "    own_address text,\n"
  1154                 "    contact_userid text,\n"
  1155                 "    CONSTRAINT fk_own_identity\n"
  1156                 "       FOREIGN KEY(own_address, own_userid)\n" 
  1157                 "       REFERENCES identity(address, user_id)\n"
  1158                 "       ON DELETE CASCADE ON UPDATE CASCADE\n"
  1159                 ");\n"
  1160                 // list of user_ids sent revocation
  1161                 "create table if not exists revocation_contact_list (\n"
  1162                 "   fpr text not null references pgp_keypair (fpr)\n"
  1163                 "       on delete cascade,\n"
  1164                 "   own_address text,\n"
  1165                 "   contact_id text not null references person (id)\n"
  1166                 "       on delete cascade on update cascade,\n"
  1167                 "   timestamp integer default (datetime('now')),\n"
  1168                 "   PRIMARY KEY(fpr, own_address, contact_id)\n"
  1169                 ");\n"
  1170                 ,
  1171             NULL,
  1172             NULL,
  1173             NULL
  1174         );
  1175         assert(int_result == SQLITE_OK);
  1176 
  1177         int version;
  1178         int_result = sqlite3_exec(
  1179             _session->db,
  1180             "pragma user_version;",
  1181             user_version,
  1182             &version,
  1183             NULL
  1184         );
  1185 
  1186         assert(int_result == SQLITE_OK);
  1187         
  1188         void (*xFunc_lower)(sqlite3_context*,int,sqlite3_value**) = &_sql_lower;
  1189         
  1190         int_result = sqlite3_create_function_v2(
  1191             _session->db,
  1192             "lower",
  1193             1,
  1194             SQLITE_UTF8 | SQLITE_DETERMINISTIC,
  1195             NULL,
  1196             xFunc_lower,
  1197             NULL,
  1198             NULL,
  1199             NULL);
  1200         assert(int_result == SQLITE_OK);
  1201 
  1202         int_result = sqlite3_exec(
  1203             _session->db,
  1204             "pragma foreign_keys=ON;\n",
  1205             NULL,
  1206             NULL,
  1207             NULL
  1208         );
  1209 
  1210         assert(int_result == SQLITE_OK);
  1211 
  1212         
  1213         // Sometimes the user_version wasn't set correctly. 
  1214         if (version == 1) {
  1215             bool version_changed = true;
  1216             if (table_contains_column(_session, "revocation_contact_list", "own_address")) {
  1217                 version = 13;
  1218             }
  1219             else if (table_contains_column(_session, "identity", "pEp_version_major")) {
  1220                 version = 12;
  1221             }
  1222             else if (db_contains_table(_session, "social_graph") > 0) {
  1223                 if (!table_contains_column(_session, "person", "device_group"))
  1224                     version = 10;
  1225                 else
  1226                     version = 9;
  1227             }            
  1228             else if (table_contains_column(_session, "identity", "timestamp") > 0) {
  1229                 version = 8;
  1230             }            
  1231             else if (table_contains_column(_session, "person", "is_pEp_user") > 0) {
  1232                 version = 7;
  1233             }            
  1234             else if (table_contains_column(_session, "identity", "is_own") > 0) {
  1235                 version = 6;
  1236             }
  1237             else if (table_contains_column(_session, "pgp_keypair", "flags") > 0) {
  1238                 version = 2;
  1239             }
  1240             else {
  1241                 version_changed = false;
  1242             }
  1243             
  1244             if (version_changed) {
  1245                 // set it in the DB, finally. Yeesh.
  1246                 char verbuf[21]; // enough digits for a max-sized 64 bit int, cmon. 
  1247                 sprintf(verbuf,"%d",version);
  1248                 
  1249                 size_t query_size = strlen(verbuf) + 25;
  1250                 char* query = calloc(query_size, 1);
  1251                 
  1252                 strlcpy(query, "pragma user_version = ", query_size);
  1253                 strlcat(query, verbuf, query_size);
  1254                 strlcat(query, ";", query_size);
  1255                 
  1256                 int_result = sqlite3_exec(
  1257                     _session->db,
  1258                     query,
  1259                     user_version,
  1260                     &version,
  1261                     NULL
  1262                 );
  1263                 free(query);
  1264             }
  1265         }
  1266 
  1267 
  1268         if(version != 0) { 
  1269             // Version has been already set
  1270 
  1271             // Early mistake : version 0 shouldn't have existed.
  1272             // Numbering should have started at 1 to detect newly created DB.
  1273             // Version 0 DBs are not anymore compatible.
  1274 
  1275             // // Was version 0 compat code.
  1276             // if (version < 1) {
  1277             //     int_result = sqlite3_exec(
  1278             //         _session->db,
  1279             //         "alter table identity\n"
  1280             //         "   add column flags integer default 0;\n",
  1281             //         NULL,
  1282             //         NULL,
  1283             //         NULL
  1284             //     );
  1285             //     assert(int_result == SQLITE_OK);
  1286             // }
  1287             
  1288             if (version < 2) {
  1289                 // N.B. addition of device_group column removed in DDL v10
  1290                 int_result = sqlite3_exec(
  1291                     _session->db,
  1292                     "alter table pgp_keypair\n"
  1293                     "   add column flags integer default 0;\n",
  1294                     // "alter table person\n"
  1295                     // "   add column device_group text;\n",
  1296                     NULL,
  1297                     NULL,
  1298                     NULL
  1299                 );
  1300                 assert(int_result == SQLITE_OK);
  1301             }
  1302 
  1303             if (version < 5) {
  1304                 int_result = sqlite3_exec(
  1305                     _session->db,
  1306                     "delete from pgp_keypair where fpr = '';",
  1307                     NULL,
  1308                     NULL,
  1309                     NULL
  1310                 );
  1311                 assert(int_result == SQLITE_OK);
  1312                 int_result = sqlite3_exec(
  1313                     _session->db,
  1314                     "delete from trust where pgp_keypair_fpr = '';",
  1315                     NULL,
  1316                     NULL,
  1317                     NULL
  1318                 );
  1319                 assert(int_result == SQLITE_OK);
  1320             }
  1321             
  1322             if (version < 6) {
  1323                 int_result = sqlite3_exec(
  1324                     _session->db,
  1325                     "alter table identity\n"
  1326                     "   add column is_own integer default 0;\n",
  1327                     NULL,
  1328                     NULL,
  1329                     NULL
  1330                 );
  1331                 assert(int_result == SQLITE_OK);                
  1332                 int_result = sqlite3_exec(
  1333                     _session->db,
  1334                     "update identity\n"
  1335                     "   set is_own = 1\n"
  1336                     "   where (user_id = '" PEP_OWN_USERID "');\n",
  1337                     NULL,
  1338                     NULL,
  1339                     NULL
  1340                 );
  1341                 assert(int_result == SQLITE_OK);    
  1342 
  1343                 // Turns out that just adding "on update cascade" in
  1344                 // sqlite is a PITA. We need to be able to cascade
  1345                 // person->id replacements (for temp ids like "TOFU_")
  1346                 // so here we go...
  1347                 int_result = sqlite3_exec(
  1348                     _session->db,
  1349                     "PRAGMA foreign_keys=off;\n"
  1350                     "BEGIN TRANSACTION;\n"
  1351                     "create table _identity_new (\n"
  1352                     "   address text,\n"
  1353                     "   user_id text\n"
  1354                     "       references person (id)\n"
  1355                     "       on delete cascade on update cascade,\n"
  1356                     "   main_key_id text\n"
  1357                     "       references pgp_keypair (fpr)\n"
  1358                     "       on delete set null,\n"
  1359                     "   comment text,\n"
  1360                     "   flags integer default 0,\n"
  1361                     "   is_own integer default 0,\n"
  1362                     "   primary key (address, user_id)\n"
  1363                     ");\n"                    
  1364                     "INSERT INTO _identity_new SELECT * FROM identity;\n"
  1365                     "DROP TABLE identity;\n"
  1366                     "ALTER TABLE _identity_new RENAME TO identity;\n"
  1367                     "COMMIT;\n"
  1368                     "\n"
  1369                     "BEGIN TRANSACTION;\n"
  1370                     "create table _trust_new (\n"
  1371                     "   user_id text not null\n"
  1372                     "       references person (id)\n"
  1373                     "       on delete cascade on update cascade,\n"
  1374                     "   pgp_keypair_fpr text not null\n"
  1375                     "       references pgp_keypair (fpr)\n"
  1376                     "       on delete cascade,\n"
  1377                     "   comm_type integer not null,\n"
  1378                     "   comment text,\n"
  1379                     "   primary key (user_id, pgp_keypair_fpr)\n"
  1380                     ");\n"
  1381                     "INSERT INTO _trust_new SELECT * FROM trust;\n"
  1382                     "DROP TABLE trust;\n"
  1383                     "ALTER TABLE _trust_new RENAME TO trust;\n"
  1384                     "COMMIT;\n"
  1385                     "\n"
  1386                     "PRAGMA foreign_keys=on;\n"
  1387                     "create table if not exists alternate_user_id (\n"
  1388                     "    default_id text references person (id)\n"
  1389                     "       on delete cascade on update cascade,\n"
  1390                     "    alternate_id text primary key\n"
  1391                     ");\n"
  1392                     ,
  1393                     NULL,
  1394                     NULL,
  1395                     NULL
  1396                 );
  1397                 assert(int_result == SQLITE_OK);   
  1398                 
  1399                 int_result = sqlite3_exec(
  1400                     _session->db,
  1401                     "PRAGMA foreign_key_check;\n"
  1402                     ,
  1403                     NULL,
  1404                     NULL,
  1405                     NULL
  1406                 );
  1407                 assert(int_result == SQLITE_OK);
  1408 
  1409                 // FIXME: foreign key check here 
  1410             }
  1411             if (version < 7) {
  1412                 int_result = sqlite3_exec(
  1413                     _session->db,
  1414                     "alter table person\n"
  1415                     "   add column is_pEp_user integer default 0;\n",
  1416                     NULL,
  1417                     NULL,
  1418                     NULL
  1419                 );
  1420                 assert(int_result == SQLITE_OK);
  1421                 int_result = sqlite3_exec(
  1422                     _session->db,
  1423                     "update person\n"
  1424                     "   set is_pEp_user = 1\n"
  1425                     "   where id = "
  1426                     "       (select distinct id from person "
  1427                     "               join trust on id = user_id "
  1428                     "               where (case when (comm_type = 127) then (id) "
  1429                     "                           when (comm_type = 255) then (id) "
  1430                     "                           else 0"
  1431                     "                      end) = id );\n",
  1432                     NULL,
  1433                     NULL,
  1434                     NULL
  1435                 );
  1436                 assert(int_result == SQLITE_OK);
  1437                 
  1438                 int_result = sqlite3_exec(
  1439                     _session->db,
  1440                     "create table if not exists mistrusted_keys (\n"
  1441                     "    fpr text primary key\n"
  1442                     ");\n",            
  1443                     NULL,
  1444                     NULL,
  1445                     NULL
  1446                 );
  1447                 assert(int_result == SQLITE_OK);    
  1448             }
  1449             if (version < 8) {
  1450                 int_result = sqlite3_exec(
  1451                     _session->db,
  1452                     "PRAGMA foreign_keys=off;\n"
  1453                     "BEGIN TRANSACTION;\n"
  1454                     "create table _identity_new (\n"
  1455                     "   address text,\n"
  1456                     "   user_id text\n"
  1457                     "       references person (id)\n"
  1458                     "       on delete cascade on update cascade,\n"
  1459                     "   main_key_id text\n"
  1460                     "       references pgp_keypair (fpr)\n"
  1461                     "       on delete set null,\n"
  1462                     "   comment text,\n"
  1463                     "   flags integer default 0,\n"
  1464                     "   is_own integer default 0,\n"
  1465                     "   timestamp integer default (datetime('now')),\n"
  1466                     "   primary key (address, user_id)\n"
  1467                     ");\n"
  1468                     "INSERT INTO _identity_new (address, user_id, main_key_id, "
  1469                     "                      comment, flags, is_own) "
  1470                     "   SELECT identity.address, identity.user_id, "
  1471                     "          identity.main_key_id, identity.comment, "
  1472                     "          identity.flags, identity.is_own "
  1473                     "   FROM identity "
  1474                     "   WHERE 1;\n"
  1475                     "DROP TABLE identity;\n"
  1476                     "ALTER TABLE _identity_new RENAME TO identity;\n"
  1477                     "COMMIT;\n"
  1478                     "\n"
  1479                     "PRAGMA foreign_keys=on;\n"
  1480                     ,
  1481                     NULL,
  1482                     NULL,
  1483                     NULL
  1484                 );
  1485                 assert(int_result == SQLITE_OK);    
  1486                 
  1487                 int_result = sqlite3_exec(
  1488                     _session->db,
  1489                     "PRAGMA foreign_key_check;\n"
  1490                     ,
  1491                     NULL,
  1492                     NULL,
  1493                     NULL
  1494                 );
  1495                 assert(int_result == SQLITE_OK);
  1496 
  1497                 // FIXME: foreign key check
  1498             }
  1499             if (version < 9) {
  1500                 int_result = sqlite3_exec(
  1501                     _session->db,
  1502                     "create table if not exists social_graph (\n"
  1503                     "    own_userid text,\n"
  1504                     "    own_address text,\n"
  1505                     "    contact_userid text,\n"
  1506                     "    CONSTRAINT fk_own_identity\n"
  1507                     "       FOREIGN KEY(own_address, own_userid)\n" 
  1508                     "       REFERENCES identity(address, user_id)\n"
  1509                     "       ON DELETE CASCADE ON UPDATE CASCADE\n"
  1510                     ");\n"
  1511                     "create table if not exists revocation_contact_list (\n"
  1512                     "   fpr text not null references pgp_keypair (fpr)\n"
  1513                     "       on delete cascade,\n"
  1514                     "   contact_id text not null references person (id)\n"
  1515                     "       on delete cascade on update cascade,\n"
  1516                     "   timestamp integer default (datetime('now')),\n"
  1517                     "   PRIMARY KEY(fpr, contact_id)\n"
  1518                     ");\n"
  1519                     ,
  1520                     NULL,
  1521                     NULL,
  1522                     NULL
  1523                 );
  1524                 assert(int_result == SQLITE_OK);    
  1525             }
  1526             if (version < 10 && version > 1) {
  1527                 int_result = sqlite3_exec(
  1528                     _session->db,
  1529                     "PRAGMA foreign_keys=off;\n"
  1530                     "BEGIN TRANSACTION;\n"
  1531                     "create table _person_new (\n"
  1532                     "   id text primary key,\n"
  1533                     "   username text not null,\n"
  1534                     "   main_key_id text\n"
  1535                     "       references pgp_keypair (fpr)\n"
  1536                     "       on delete set null,\n"
  1537                     "   lang text,\n"
  1538                     "   comment text,\n"
  1539                     "   is_pEp_user integer default 0\n"
  1540                     ");\n"
  1541                     "INSERT INTO _person_new (id, username, main_key_id, "
  1542                     "                    lang, comment, is_pEp_user) "
  1543                     "   SELECT person.id, person.username, "
  1544                     "          person.main_key_id, person.lang, "
  1545                     "          person.comment, person.is_pEp_user "
  1546                     "   FROM person "
  1547                     "   WHERE 1;\n"
  1548                     "DROP TABLE person;\n"
  1549                     "ALTER TABLE _person_new RENAME TO person;\n"
  1550                     "COMMIT;\n"
  1551                     "\n"
  1552                     "PRAGMA foreign_keys=on;\n"
  1553                     ,
  1554                     NULL,
  1555                     NULL,
  1556                     NULL
  1557                 );
  1558                 assert(int_result == SQLITE_OK);    
  1559                 int_result = sqlite3_exec(
  1560                     _session->db,
  1561                     "PRAGMA foreign_key_check;\n"
  1562                     ,
  1563                     NULL,
  1564                     NULL,
  1565                     NULL
  1566                 );
  1567                 assert(int_result == SQLITE_OK);
  1568             }
  1569             if (version < 11) {
  1570                 status = repair_altered_tables(_session);
  1571                 assert(status == PEP_STATUS_OK);
  1572                 if (status != PEP_STATUS_OK)
  1573                     return status;
  1574             }
  1575             if (version < 12) {
  1576                 int_result = sqlite3_exec(
  1577                     _session->db,
  1578                     "create index if not exists identity_userid_addr on identity(address, user_id);\n"
  1579                     ,
  1580                     NULL,
  1581                     NULL,
  1582                     NULL
  1583                 );
  1584                 assert(int_result == SQLITE_OK);
  1585                 
  1586                 int_result = sqlite3_exec(
  1587                     _session->db,
  1588                     "alter table identity\n"
  1589                     "   add column pEp_version_major integer default 0;\n"
  1590                     "alter table identity\n"
  1591                     "   add column pEp_version_minor integer default 0;\n",                    
  1592                     NULL,
  1593                     NULL,
  1594                     NULL
  1595                 );
  1596                 if (status != PEP_STATUS_OK)
  1597                     return status;  
  1598       
  1599                 int_result = sqlite3_exec(
  1600                     _session->db,
  1601                     "update identity\n"
  1602                     "   set pEp_version_major = 2\n"
  1603                     "   where exists (select * from person\n"
  1604                     "                     where identity.user_id = person.id\n"
  1605                     "                     and identity.is_own = 0\n"
  1606                     "                     and person.is_pEp_user = 1);\n",
  1607                     NULL,
  1608                     NULL,
  1609                     NULL
  1610                 );
  1611                 if (status != PEP_STATUS_OK)
  1612                     return status;  
  1613                 
  1614                 // N.B. WE DEFINE PEP_VERSION - IF WE'RE AT 9-DIGIT MAJOR OR MINOR VERSIONS, ER, BAD.
  1615                 char major_buf[10];
  1616                 char minor_buf[10];
  1617                 
  1618                 // Guess we were abusing sscanf here, so we'll do it this way:
  1619                 const char* cptr = PEP_VERSION;
  1620                 size_t major_len = 0;
  1621                 size_t minor_len = 0;
  1622                 
  1623                 char* bufptr = major_buf;
  1624                 while (*cptr != '.' && *cptr != '\0') {
  1625                     *bufptr++ = *cptr++;
  1626                     major_len++;
  1627                 }
  1628                 *bufptr = '\0';
  1629                 bufptr = minor_buf;
  1630 
  1631                 if (*cptr == '.') {
  1632                     cptr++;
  1633                     while (*cptr != '\0') {
  1634                         *bufptr++ = *cptr++;
  1635                         minor_len++;
  1636                     }
  1637                 }
  1638                 else {
  1639                     *bufptr++ = '0';
  1640                 }
  1641                 *bufptr = '\0';
  1642                                     
  1643                 const char* _ver_12_startstr =                     
  1644                     "update identity\n"
  1645                     "    set pEp_version_major = ";
  1646                 const char* _ver_12_midstr = ",\n"
  1647                     "        pEp_version_minor = ";
  1648                 const char* _ver_12_endstr =     
  1649                     "\n"
  1650                     "    where identity.is_own = 1;\n";
  1651                     
  1652                 size_t new_stringlen = strlen(_ver_12_startstr) + major_len +
  1653                                        strlen(_ver_12_midstr) + minor_len +
  1654                                        strlen(_ver_12_endstr);
  1655                                        
  1656                 char* _ver_12_stmt = calloc(new_stringlen + 1, 1);
  1657                 snprintf(_ver_12_stmt, new_stringlen + 1, "%s%s%s%s%s",
  1658                          _ver_12_startstr, major_buf, _ver_12_midstr, minor_buf, _ver_12_endstr);
  1659                 
  1660                 int_result = sqlite3_exec(
  1661                     _session->db,
  1662                     _ver_12_stmt,
  1663                     NULL,
  1664                     NULL,
  1665                     NULL
  1666                 );
  1667                 free(_ver_12_stmt);
  1668                 if (status != PEP_STATUS_OK)
  1669                     return status;                      
  1670             }
  1671             if (version < 13) {
  1672                 status = upgrade_revoc_contact_to_13(_session);
  1673                 assert(status == PEP_STATUS_OK);
  1674                 if (status != PEP_STATUS_OK)
  1675                     return status;
  1676             }        
  1677         }        
  1678         else { 
  1679             // Version from DB was 0, it means this is initial setup.
  1680             // DB has just been created, and all tables are empty.
  1681             very_first = true;
  1682         }
  1683 
  1684         if (version < atoi(_DDL_USER_VERSION)) {
  1685             int_result = sqlite3_exec(
  1686                 _session->db,
  1687                 "pragma user_version = "_DDL_USER_VERSION";\n"
  1688                 "insert or replace into version_info (id, version)"
  1689                     "values (1, '" PEP_ENGINE_VERSION "');",
  1690                 NULL,
  1691                 NULL,
  1692                 NULL
  1693             );
  1694             assert(int_result == SQLITE_OK);
  1695         }
  1696         
  1697         // We need to init a few globals for message id that we'd rather not
  1698         // calculate more than once.
  1699         _init_globals();
  1700     }
  1701 
  1702     int_result = sqlite3_prepare_v2(_session->db, sql_log,
  1703             (int)strlen(sql_log), &_session->log, NULL);
  1704     assert(int_result == SQLITE_OK);
  1705 
  1706     int_result = sqlite3_prepare_v2(_session->system_db, sql_trustword,
  1707             (int)strlen(sql_trustword), &_session->trustword, NULL);
  1708     assert(int_result == SQLITE_OK);
  1709 
  1710     int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
  1711             (int)strlen(sql_get_identity), &_session->get_identity, NULL);
  1712     assert(int_result == SQLITE_OK);
  1713 
  1714     int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_trust_check,
  1715             (int)strlen(sql_get_identity_without_trust_check), 
  1716             &_session->get_identity_without_trust_check, NULL);
  1717     assert(int_result == SQLITE_OK);
  1718 
  1719     int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_address,
  1720             (int)strlen(sql_get_identities_by_address), 
  1721             &_session->get_identities_by_address, NULL);
  1722     assert(int_result == SQLITE_OK);
  1723     
  1724     int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_userid,
  1725             (int)strlen(sql_get_identities_by_userid), 
  1726             &_session->get_identities_by_userid, NULL);
  1727     assert(int_result == SQLITE_OK);
  1728 
  1729     int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_main_key_id,
  1730             (int)strlen(sql_get_identities_by_main_key_id), 
  1731             &_session->get_identities_by_main_key_id, NULL);
  1732     assert(int_result == SQLITE_OK);
  1733 
  1734     int_result = sqlite3_prepare_v2(_session->db, sql_get_user_default_key,
  1735             (int)strlen(sql_get_user_default_key), &_session->get_user_default_key, NULL);
  1736     assert(int_result == SQLITE_OK);
  1737 
  1738     int_result = sqlite3_prepare_v2(_session->db, sql_get_all_keys_for_user,
  1739             (int)strlen(sql_get_all_keys_for_user), &_session->get_all_keys_for_user, NULL);
  1740     assert(int_result == SQLITE_OK);
  1741 
  1742     int_result = sqlite3_prepare_v2(_session->db, sql_get_default_own_userid,
  1743             (int)strlen(sql_get_default_own_userid), &_session->get_default_own_userid, NULL);
  1744     assert(int_result == SQLITE_OK);
  1745     
  1746     int_result = sqlite3_prepare_v2(_session->db, sql_get_userid_alias_default,
  1747             (int)strlen(sql_get_userid_alias_default), &_session->get_userid_alias_default, NULL);
  1748     assert(int_result == SQLITE_OK);
  1749 
  1750     int_result = sqlite3_prepare_v2(_session->db, sql_add_userid_alias,
  1751             (int)strlen(sql_add_userid_alias), &_session->add_userid_alias, NULL);
  1752     assert(int_result == SQLITE_OK);
  1753 
  1754     int_result = sqlite3_prepare_v2(_session->db, sql_replace_userid,
  1755             (int)strlen(sql_replace_userid), &_session->replace_userid, NULL);
  1756     assert(int_result == SQLITE_OK);
  1757 
  1758     int_result = sqlite3_prepare_v2(_session->db, sql_delete_key,
  1759             (int)strlen(sql_delete_key), &_session->delete_key, NULL);
  1760     assert(int_result == SQLITE_OK);
  1761 
  1762     int_result = sqlite3_prepare_v2(_session->db, sql_replace_main_user_fpr,
  1763             (int)strlen(sql_replace_main_user_fpr), &_session->replace_main_user_fpr, NULL);
  1764     assert(int_result == SQLITE_OK);
  1765 
  1766     int_result = sqlite3_prepare_v2(_session->db, sql_get_main_user_fpr,
  1767             (int)strlen(sql_get_main_user_fpr), &_session->get_main_user_fpr, NULL);
  1768     assert(int_result == SQLITE_OK);
  1769 
  1770     int_result = sqlite3_prepare_v2(_session->db, sql_refresh_userid_default_key,
  1771             (int)strlen(sql_refresh_userid_default_key), &_session->refresh_userid_default_key, NULL);
  1772     assert(int_result == SQLITE_OK);
  1773 
  1774     int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
  1775             (int)strlen(sql_replace_identities_fpr), 
  1776             &_session->replace_identities_fpr, NULL);
  1777     assert(int_result == SQLITE_OK);
  1778     
  1779     int_result = sqlite3_prepare_v2(_session->db, sql_remove_fpr_as_default,
  1780             (int)strlen(sql_remove_fpr_as_default), 
  1781             &_session->remove_fpr_as_default, NULL);
  1782     assert(int_result == SQLITE_OK);
  1783 
  1784     int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
  1785             (int)strlen(sql_set_person), &_session->set_person, NULL);
  1786     assert(int_result == SQLITE_OK);
  1787 
  1788     int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
  1789             (int)strlen(sql_update_person), &_session->update_person, NULL);
  1790     assert(int_result == SQLITE_OK);
  1791 
  1792     int_result = sqlite3_prepare_v2(_session->db, sql_delete_person,
  1793             (int)strlen(sql_delete_person), &_session->delete_person, NULL);
  1794     assert(int_result == SQLITE_OK);
  1795 
  1796     int_result = sqlite3_prepare_v2(_session->db, sql_exists_person,
  1797             (int)strlen(sql_exists_person), &_session->exists_person, NULL);
  1798     assert(int_result == SQLITE_OK);
  1799 
  1800     int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pEp_user,
  1801             (int)strlen(sql_set_as_pEp_user), &_session->set_as_pEp_user, NULL);
  1802     assert(int_result == SQLITE_OK);
  1803     
  1804     int_result = sqlite3_prepare_v2(_session->db, sql_is_pEp_user,
  1805             (int)strlen(sql_is_pEp_user), &_session->is_pEp_user, NULL);
  1806     assert(int_result == SQLITE_OK);
  1807 
  1808     int_result = sqlite3_prepare_v2(_session->db, sql_add_into_social_graph,
  1809             (int)strlen(sql_add_into_social_graph), &_session->add_into_social_graph, NULL);
  1810     assert(int_result == SQLITE_OK);
  1811 
  1812     int_result = sqlite3_prepare_v2(_session->db, 
  1813             sql_get_own_address_binding_from_contact,
  1814             (int)strlen(sql_get_own_address_binding_from_contact), 
  1815             &_session->get_own_address_binding_from_contact, NULL);
  1816     assert(int_result == SQLITE_OK);
  1817 
  1818     int_result = sqlite3_prepare_v2(_session->db, 
  1819             sql_set_revoke_contact_as_notified,
  1820             (int)strlen(sql_set_revoke_contact_as_notified), 
  1821             &_session->set_revoke_contact_as_notified, NULL);
  1822     assert(int_result == SQLITE_OK);
  1823 
  1824     int_result = sqlite3_prepare_v2(_session->db, 
  1825             sql_get_contacted_ids_from_revoke_fpr,
  1826             (int)strlen(sql_get_contacted_ids_from_revoke_fpr), 
  1827             &_session->get_contacted_ids_from_revoke_fpr, NULL);
  1828     assert(int_result == SQLITE_OK);
  1829 
  1830     int_result = sqlite3_prepare_v2(_session->db, 
  1831             sql_was_id_for_revoke_contacted,
  1832             (int)strlen(sql_was_id_for_revoke_contacted), 
  1833             &_session->was_id_for_revoke_contacted, NULL);
  1834     assert(int_result == SQLITE_OK);
  1835 
  1836     int_result = sqlite3_prepare_v2(_session->db, 
  1837             sql_has_id_contacted_address,
  1838             (int)strlen(sql_has_id_contacted_address), 
  1839             &_session->has_id_contacted_address, NULL);
  1840     assert(int_result == SQLITE_OK);
  1841 
  1842     int_result = sqlite3_prepare_v2(_session->db, 
  1843             sql_get_last_contacted,
  1844             (int)strlen(sql_get_last_contacted), 
  1845             &_session->get_last_contacted, NULL);
  1846     assert(int_result == SQLITE_OK);
  1847 
  1848     int_result = sqlite3_prepare_v2(_session->db, 
  1849             sql_get_own_address_binding_from_contact,
  1850             (int)strlen(sql_get_own_address_binding_from_contact), 
  1851             &_session->get_own_address_binding_from_contact, NULL);
  1852     assert(int_result == SQLITE_OK);
  1853 
  1854     int_result = sqlite3_prepare_v2(_session->db, sql_set_pgp_keypair,
  1855             (int)strlen(sql_set_pgp_keypair), &_session->set_pgp_keypair,
  1856             NULL);
  1857     assert(int_result == SQLITE_OK);
  1858 
  1859     int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
  1860             (int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
  1861     assert(int_result == SQLITE_OK);
  1862 
  1863     int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
  1864             (int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
  1865     assert(int_result == SQLITE_OK);
  1866 
  1867     int_result = sqlite3_prepare_v2(_session->db, sql_exists_identity_entry,
  1868             (int)strlen(sql_exists_identity_entry), &_session->exists_identity_entry, NULL);
  1869     assert(int_result == SQLITE_OK);
  1870 
  1871     int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
  1872             (int)strlen(sql_set_identity_flags), &_session->set_identity_flags,
  1873             NULL);
  1874     assert(int_result == SQLITE_OK);
  1875 
  1876     int_result = sqlite3_prepare_v2(_session->db, sql_unset_identity_flags,
  1877             (int)strlen(sql_unset_identity_flags), &_session->unset_identity_flags,
  1878             NULL);
  1879     assert(int_result == SQLITE_OK);
  1880 
  1881     int_result = sqlite3_prepare_v2(_session->db, sql_set_pEp_version,
  1882             (int)strlen(sql_set_pEp_version), &_session->set_pEp_version,
  1883             NULL);
  1884     assert(int_result == SQLITE_OK);
  1885     
  1886     int_result = sqlite3_prepare_v2(_session->db, sql_upgrade_pEp_version_by_user_id,
  1887             (int)strlen(sql_upgrade_pEp_version_by_user_id), &_session->upgrade_pEp_version_by_user_id,
  1888             NULL);
  1889     assert(int_result == SQLITE_OK);
  1890 
  1891     int_result = sqlite3_prepare_v2(_session->db, sql_clear_trust_info,
  1892             (int)strlen(sql_clear_trust_info), &_session->clear_trust_info, NULL);
  1893     assert(int_result == SQLITE_OK);
  1894 
  1895     int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
  1896             (int)strlen(sql_set_trust), &_session->set_trust, NULL);
  1897     assert(int_result == SQLITE_OK);
  1898 
  1899     int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
  1900             (int)strlen(sql_update_trust), &_session->update_trust, NULL);
  1901     assert(int_result == SQLITE_OK);
  1902 
  1903     int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_to_pEp,
  1904             (int)strlen(sql_update_trust_to_pEp), &_session->update_trust_to_pEp, NULL);
  1905     assert(int_result == SQLITE_OK);
  1906 
  1907     int_result = sqlite3_prepare_v2(_session->db, sql_exists_trust_entry,
  1908                  (int)strlen(sql_exists_trust_entry), &_session->exists_trust_entry, NULL);
  1909     assert(int_result == SQLITE_OK);
  1910 
  1911     int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
  1912             (int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
  1913     assert(int_result == SQLITE_OK);
  1914 
  1915     int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
  1916             (int)strlen(sql_get_trust), &_session->get_trust, NULL);
  1917     assert(int_result == SQLITE_OK);
  1918 
  1919     int_result = sqlite3_prepare_v2(_session->db, sql_get_trust_by_userid,
  1920             (int)strlen(sql_get_trust_by_userid), &_session->get_trust_by_userid, NULL);
  1921     assert(int_result == SQLITE_OK);
  1922 
  1923     int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
  1924             (int)strlen(sql_least_trust), &_session->least_trust, NULL);
  1925     assert(int_result == SQLITE_OK);
  1926 
  1927     int_result = sqlite3_prepare_v2(_session->db, sql_mark_as_compromised,
  1928             (int)strlen(sql_mark_as_compromised), &_session->mark_compromised,
  1929             NULL);
  1930     assert(int_result == SQLITE_OK);
  1931 
  1932     int_result = sqlite3_prepare_v2(_session->db, sql_crashdump,
  1933             (int)strlen(sql_crashdump), &_session->crashdump, NULL);
  1934     assert(int_result == SQLITE_OK);
  1935 
  1936     int_result = sqlite3_prepare_v2(_session->system_db, sql_languagelist,
  1937             (int)strlen(sql_languagelist), &_session->languagelist, NULL);
  1938     assert(int_result == SQLITE_OK);
  1939 
  1940     int_result = sqlite3_prepare_v2(_session->system_db, sql_i18n_token,
  1941             (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
  1942     assert(int_result == SQLITE_OK);
  1943     
  1944     // blacklist
  1945 
  1946     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
  1947             (int)strlen(sql_blacklist_add), &_session->blacklist_add, NULL);
  1948     assert(int_result == SQLITE_OK);
  1949 
  1950     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_delete,
  1951             (int)strlen(sql_blacklist_delete), &_session->blacklist_delete,
  1952             NULL);
  1953     assert(int_result == SQLITE_OK);
  1954 
  1955     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_is_listed,
  1956             (int)strlen(sql_blacklist_is_listed),
  1957             &_session->blacklist_is_listed, NULL);
  1958     assert(int_result == SQLITE_OK);
  1959 
  1960     int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_retrieve,
  1961             (int)strlen(sql_blacklist_retrieve), &_session->blacklist_retrieve,
  1962             NULL);
  1963     assert(int_result == SQLITE_OK);
  1964     
  1965     // Own keys
  1966     
  1967     int_result = sqlite3_prepare_v2(_session->db, sql_own_key_is_listed,
  1968             (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
  1969             NULL);
  1970     assert(int_result == SQLITE_OK);
  1971 
  1972     int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
  1973             (int)strlen(sql_is_own_address), &_session->is_own_address,
  1974             NULL);
  1975     assert(int_result == SQLITE_OK);
  1976     
  1977     int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
  1978             (int)strlen(sql_own_identities_retrieve),
  1979             &_session->own_identities_retrieve, NULL);
  1980     assert(int_result == SQLITE_OK);
  1981  
  1982     int_result = sqlite3_prepare_v2(_session->db, sql_own_keys_retrieve,
  1983             (int)strlen(sql_own_keys_retrieve),
  1984             &_session->own_keys_retrieve, NULL);
  1985     assert(int_result == SQLITE_OK);
  1986  
  1987     // int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
  1988     //         (int)strlen(sql_set_own_key),
  1989     //         &_session->set_own_key, NULL);
  1990     // assert(int_result == SQLITE_OK);
  1991  
  1992     // Sequence
  1993 
  1994     int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value1,
  1995             (int)strlen(sql_sequence_value1), &_session->sequence_value1,
  1996             NULL);
  1997     assert(int_result == SQLITE_OK);
  1998 
  1999     int_result = sqlite3_prepare_v2(_session->db, sql_sequence_value2,
  2000             (int)strlen(sql_sequence_value2), &_session->sequence_value2,
  2001             NULL);
  2002     assert(int_result == SQLITE_OK);
  2003 
  2004     // Revocation tracking
  2005     
  2006     int_result = sqlite3_prepare_v2(_session->db, sql_set_revoked,
  2007             (int)strlen(sql_set_revoked), &_session->set_revoked, NULL);
  2008     assert(int_result == SQLITE_OK);
  2009     
  2010     int_result = sqlite3_prepare_v2(_session->db, sql_get_revoked,
  2011             (int)strlen(sql_get_revoked), &_session->get_revoked, NULL);
  2012     assert(int_result == SQLITE_OK);
  2013     
  2014     int_result = sqlite3_prepare_v2(_session->db, sql_get_replacement_fpr,
  2015             (int)strlen(sql_get_replacement_fpr), &_session->get_replacement_fpr, NULL);
  2016     assert(int_result == SQLITE_OK);
  2017 
  2018     int_result = sqlite3_prepare_v2(_session->db, sql_add_mistrusted_key,
  2019             (int)strlen(sql_add_mistrusted_key), &_session->add_mistrusted_key, NULL);
  2020     assert(int_result == SQLITE_OK);
  2021 
  2022     int_result = sqlite3_prepare_v2(_session->db, sql_delete_mistrusted_key,
  2023             (int)strlen(sql_delete_mistrusted_key), &_session->delete_mistrusted_key, NULL);
  2024     assert(int_result == SQLITE_OK);
  2025 
  2026     int_result = sqlite3_prepare_v2(_session->db, sql_is_mistrusted_key,
  2027             (int)strlen(sql_is_mistrusted_key), &_session->is_mistrusted_key, NULL);
  2028     assert(int_result == SQLITE_OK);
  2029     
  2030     status = init_cryptotech(_session, in_first);
  2031     if (status != PEP_STATUS_OK)
  2032         goto pEp_error;
  2033 
  2034     status = init_transport_system(_session, in_first);
  2035     if (status != PEP_STATUS_OK)
  2036         goto pEp_error;
  2037 
  2038     status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
  2039     if (status != PEP_STATUS_OK)
  2040         goto pEp_error;
  2041 
  2042     // runtime config
  2043 
  2044     if (very_first)
  2045     {
  2046 #ifdef USE_GPG
  2047         // On first run, all private keys already present in PGP keyring 
  2048         // are taken as own in order to seamlessly integrate with
  2049         // pre-existing GPG setup.
  2050 
  2051         // Note: earlier fears about danger because of DB reinitialisation should
  2052         // be a non-issue here, as we ONLY take the ultimately trusted keys now.
  2053         // Thus, unless the user has assigned ultimate trust through PGP, there is
  2054         // no chance of automatically imported pEp keys from a previous run making
  2055         // their way into PEP trusted status without explicit action (Bare imported
  2056         // private keys have an 'unknown' trust designation in PGP).
  2057 
  2058         // We don't really worry about the status here.
  2059         status = pgp_import_ultimately_trusted_keypairs(_session);        
  2060 #endif // USE_GPG
  2061     }
  2062 
  2063     *session = _session;
  2064     
  2065     // Note: Following statement is NOT for any cryptographic/secure functionality; it is
  2066     //       ONLY used for some randomness in generated outer message ID, which are
  2067     //       required by the RFC to be globally unique!
  2068     srand((unsigned int) time(NULL));
  2069     
  2070     return PEP_STATUS_OK;
  2071 
  2072 enomem:
  2073     status = PEP_OUT_OF_MEMORY;
  2074 
  2075 pEp_error:
  2076     release(_session);
  2077     return status;
  2078 }
  2079 
  2080 DYNAMIC_API void release(PEP_SESSION session)
  2081 {
  2082     bool out_last = false;
  2083     int _count = --init_count;
  2084     
  2085     assert(_count >= -1);
  2086     assert(session);
  2087 
  2088     if (!((_count >= -1) && session))
  2089         return;
  2090 
  2091     // a small race condition but still a race condition
  2092     // mitigated by calling caveat (see documentation)
  2093     // (release() is to be guarded by a mutex by the caller)
  2094     if (_count == -1)
  2095         out_last = true;
  2096 
  2097     if (session) {
  2098         free_Sync_state(session);
  2099 
  2100         if (session->db) {
  2101             if (session->log)
  2102                 sqlite3_finalize(session->log);
  2103             if (session->trustword)
  2104                 sqlite3_finalize(session->trustword);
  2105             if (session->get_identity)
  2106                 sqlite3_finalize(session->get_identity);
  2107             if (session->get_identity_without_trust_check)
  2108                 sqlite3_finalize(session->get_identity_without_trust_check);
  2109             if (session->get_identities_by_address)
  2110                 sqlite3_finalize(session->get_identities_by_address);            
  2111             if (session->get_identities_by_userid)
  2112                 sqlite3_finalize(session->get_identities_by_userid);                
  2113             if (session->get_identities_by_main_key_id)
  2114                 sqlite3_finalize(session->get_identities_by_main_key_id);                                
  2115             if (session->get_user_default_key)
  2116                 sqlite3_finalize(session->get_user_default_key);
  2117             if (session->get_all_keys_for_user)
  2118                 sqlite3_finalize(session->get_all_keys_for_user);                        
  2119             if (session->get_default_own_userid)
  2120                 sqlite3_finalize(session->get_default_own_userid);
  2121             if (session->get_userid_alias_default)
  2122                 sqlite3_finalize(session->get_userid_alias_default);
  2123             if (session->add_userid_alias)
  2124                 sqlite3_finalize(session->add_userid_alias);
  2125             if (session->replace_identities_fpr)
  2126                 sqlite3_finalize(session->replace_identities_fpr);        
  2127             if (session->remove_fpr_as_default)
  2128                 sqlite3_finalize(session->remove_fpr_as_default);            
  2129             if (session->set_person)
  2130                 sqlite3_finalize(session->set_person);
  2131             if (session->delete_person)
  2132                 sqlite3_finalize(session->delete_person);                
  2133             if (session->set_as_pEp_user)
  2134                 sqlite3_finalize(session->set_as_pEp_user);
  2135             if (session->upgrade_pEp_version_by_user_id)
  2136                 sqlite3_finalize(session->upgrade_pEp_version_by_user_id);
  2137             if (session->is_pEp_user)
  2138                 sqlite3_finalize(session->is_pEp_user);
  2139             if (session->exists_person)
  2140                 sqlite3_finalize(session->exists_person);
  2141             if (session->add_into_social_graph)
  2142                 sqlite3_finalize(session->add_into_social_graph);  
  2143             if (session->get_own_address_binding_from_contact)
  2144                 sqlite3_finalize(session->get_own_address_binding_from_contact);  
  2145             if (session->set_revoke_contact_as_notified)
  2146                 sqlite3_finalize(session->set_revoke_contact_as_notified);  
  2147             if (session->get_contacted_ids_from_revoke_fpr)
  2148                 sqlite3_finalize(session->get_contacted_ids_from_revoke_fpr);  
  2149             if (session->was_id_for_revoke_contacted)
  2150                 sqlite3_finalize(session->was_id_for_revoke_contacted);  
  2151             if (session->has_id_contacted_address)
  2152                 sqlite3_finalize(session->has_id_contacted_address);
  2153             if (session->get_last_contacted)
  2154                 sqlite3_finalize(session->get_last_contacted);                                       
  2155             if (session->set_pgp_keypair)
  2156                 sqlite3_finalize(session->set_pgp_keypair);
  2157             if (session->exists_identity_entry)
  2158                 sqlite3_finalize(session->exists_identity_entry);                
  2159             if (session->set_identity_entry)
  2160                 sqlite3_finalize(session->set_identity_entry);
  2161             if (session->update_identity_entry)
  2162                 sqlite3_finalize(session->update_identity_entry);    
  2163             if (session->set_identity_flags)
  2164                 sqlite3_finalize(session->set_identity_flags);
  2165             if (session->unset_identity_flags)
  2166                 sqlite3_finalize(session->unset_identity_flags);
  2167             if (session->set_pEp_version)
  2168                 sqlite3_finalize(session->set_pEp_version);                
  2169             if (session->exists_trust_entry)
  2170                 sqlite3_finalize(session->exists_trust_entry);                                
  2171             if (session->clear_trust_info)
  2172                 sqlite3_finalize(session->clear_trust_info);                
  2173             if (session->set_trust)
  2174                 sqlite3_finalize(session->set_trust);
  2175             if (session->update_trust)
  2176                 sqlite3_finalize(session->update_trust);
  2177             if (session->update_trust_to_pEp)
  2178                 sqlite3_finalize(session->update_trust_to_pEp);                                                
  2179             if (session->update_trust_for_fpr)
  2180                 sqlite3_finalize(session->update_trust_for_fpr);
  2181             if (session->get_trust)
  2182                 sqlite3_finalize(session->get_trust);
  2183             if (session->get_trust_by_userid)
  2184                 sqlite3_finalize(session->get_trust_by_userid);                
  2185             if (session->least_trust)
  2186                 sqlite3_finalize(session->least_trust);
  2187             if (session->mark_compromised)
  2188                 sqlite3_finalize(session->mark_compromised);
  2189             if (session->crashdump)
  2190                 sqlite3_finalize(session->crashdump);
  2191             if (session->languagelist)
  2192                 sqlite3_finalize(session->languagelist);
  2193             if (session->i18n_token)
  2194                 sqlite3_finalize(session->i18n_token);
  2195             if (session->replace_userid)
  2196                 sqlite3_finalize(session->replace_userid);
  2197             if (session->delete_key)
  2198                 sqlite3_finalize(session->delete_key);                
  2199             if (session->replace_main_user_fpr)
  2200                 sqlite3_finalize(session->replace_main_user_fpr);                
  2201             if (session->get_main_user_fpr)
  2202                 sqlite3_finalize(session->get_main_user_fpr);
  2203             if (session->refresh_userid_default_key)
  2204                 sqlite3_finalize(session->refresh_userid_default_key);
  2205             if (session->blacklist_add)
  2206                 sqlite3_finalize(session->blacklist_add);
  2207             if (session->blacklist_delete)
  2208                 sqlite3_finalize(session->blacklist_delete);
  2209             if (session->blacklist_is_listed)
  2210                 sqlite3_finalize(session->blacklist_is_listed);
  2211             if (session->blacklist_retrieve)
  2212                 sqlite3_finalize(session->blacklist_retrieve);
  2213             if (session->own_key_is_listed)
  2214                 sqlite3_finalize(session->own_key_is_listed);
  2215             if (session->is_own_address)
  2216                 sqlite3_finalize(session->is_own_address);
  2217             if (session->own_identities_retrieve)
  2218                 sqlite3_finalize(session->own_identities_retrieve);
  2219             if (session->own_keys_retrieve)
  2220                 sqlite3_finalize(session->own_keys_retrieve);
  2221             // if (session->set_own_key)
  2222             //     sqlite3_finalize(session->set_own_key);
  2223             if (session->sequence_value1)
  2224                 sqlite3_finalize(session->sequence_value1);
  2225             if (session->sequence_value2)
  2226                 sqlite3_finalize(session->sequence_value2);
  2227             if (session->set_revoked)
  2228                 sqlite3_finalize(session->set_revoked);
  2229             if (session->get_revoked)
  2230                 sqlite3_finalize(session->get_revoked);
  2231             if (session->get_replacement_fpr)
  2232                 sqlite3_finalize(session->get_replacement_fpr);                
  2233             if (session->add_mistrusted_key)
  2234                 sqlite3_finalize(session->add_mistrusted_key);
  2235             if (session->delete_mistrusted_key)
  2236                 sqlite3_finalize(session->delete_mistrusted_key);
  2237             if (session->is_mistrusted_key)
  2238                 sqlite3_finalize(session->is_mistrusted_key);
  2239                 
  2240             if (session->db) {
  2241                 if (out_last) {
  2242                     sqlite3_exec(        
  2243                         session->db,
  2244                         "PRAGMA optimize;\n",
  2245                         NULL,
  2246                         NULL,
  2247                         NULL
  2248                     );
  2249                 }    
  2250                 sqlite3_close_v2(session->db);
  2251             }
  2252             if (session->system_db)
  2253                 sqlite3_close_v2(session->system_db);
  2254         }
  2255 
  2256         release_transport_system(session, out_last);
  2257         release_cryptotech(session, out_last);
  2258 
  2259 #ifdef DEBUG_ERRORSTACK
  2260         free_stringlist(session->errorstack);
  2261 #endif
  2262         free(session);
  2263     }
  2264 }
  2265 
  2266 DYNAMIC_API void config_passive_mode(PEP_SESSION session, bool enable)
  2267 {
  2268     assert(session);
  2269     if (session)
  2270         session->passive_mode = enable;
  2271 }
  2272 
  2273 DYNAMIC_API void config_unencrypted_subject(PEP_SESSION session, bool enable)
  2274 {
  2275     assert(session);
  2276     if (session)
  2277         session->unencrypted_subject = enable;
  2278 }
  2279 
  2280 DYNAMIC_API void config_service_log(PEP_SESSION session, bool enable)
  2281 {
  2282     assert(session);
  2283     if (session)
  2284         session->service_log = enable;
  2285 }
  2286 
  2287 DYNAMIC_API PEP_STATUS log_event(
  2288         PEP_SESSION session,
  2289         const char *title,
  2290         const char *entity,
  2291         const char *description,
  2292         const char *comment
  2293     )
  2294 {
  2295 
  2296 #if defined(_WIN32) && !defined(NDEBUG)
  2297 	log_output_debug(title, entity, description, comment);
  2298 #endif
  2299 
  2300 // N.B. If testing (so NDEBUG not defined) but this message is spam,
  2301 //      put -D_PEP_SERVICE_LOG_OFF into CFLAGS/CXXFLAGS     
  2302 #if !defined(NDEBUG) && !defined(_PEP_SERVICE_LOG_OFF)
  2303 #ifndef NDEBUG
  2304     printf("\x1b[%dm", session->debug_color);
  2305 #endif
  2306     fprintf(stdout, "\n*** %s %s %s %s\n", title, entity, description, comment);
  2307 #ifndef NDEBUG
  2308     printf("\x1b[0m");
  2309 #endif
  2310     session->service_log = true;
  2311 
  2312     int result;
  2313     
  2314     assert(session);
  2315     assert(title);
  2316     assert(entity);
  2317     
  2318     if (!(session && title && entity))
  2319         return PEP_ILLEGAL_VALUE;
  2320     
  2321     sqlite3_reset(session->log);
  2322     sqlite3_bind_text(session->log, 1, title, -1, SQLITE_STATIC);
  2323     sqlite3_bind_text(session->log, 2, entity, -1, SQLITE_STATIC);
  2324     if (description)
  2325         sqlite3_bind_text(session->log, 3, description, -1, SQLITE_STATIC);
  2326     else
  2327         sqlite3_bind_null(session->log, 3);
  2328     if (comment)
  2329         sqlite3_bind_text(session->log, 4, comment, -1, SQLITE_STATIC);
  2330     else
  2331         sqlite3_bind_null(session->log, 4);
  2332     result = sqlite3_step(session->log);
  2333     sqlite3_reset(session->log);
  2334     
  2335 #endif
  2336     return PEP_STATUS_OK; // We ignore errors for this function.
  2337 }
  2338 
  2339 DYNAMIC_API PEP_STATUS log_service(
  2340         PEP_SESSION session,
  2341         const char *title,
  2342         const char *entity,
  2343         const char *description,
  2344         const char *comment
  2345     )
  2346 {
  2347     assert(session);
  2348     if (!session)
  2349         return PEP_ILLEGAL_VALUE;
  2350 
  2351     if (session->service_log)
  2352         return log_event(session, title, entity, description, comment);
  2353     else
  2354         return PEP_STATUS_OK;
  2355 }
  2356 
  2357 DYNAMIC_API PEP_STATUS trustword(
  2358             PEP_SESSION session, uint16_t value, const char *lang,
  2359             char **word, size_t *wsize
  2360         )
  2361 {
  2362     PEP_STATUS status = PEP_STATUS_OK;
  2363 
  2364     assert(session);
  2365     assert(word);
  2366     assert(wsize);
  2367 
  2368     if (!(session && word && wsize))
  2369         return PEP_ILLEGAL_VALUE;
  2370 
  2371     *word = NULL;
  2372     *wsize = 0;
  2373 
  2374     if (lang == NULL)
  2375         lang = "en";
  2376 
  2377     assert((lang[0] >= 'A' && lang[0] <= 'Z')
  2378             || (lang[0] >= 'a' && lang[0] <= 'z'));
  2379     assert((lang[1] >= 'A' && lang[1] <= 'Z')
  2380             || (lang[1] >= 'a' && lang[1] <= 'z'));
  2381     assert(lang[2] == 0);
  2382 
  2383     sqlite3_reset(session->trustword);
  2384     sqlite3_bind_text(session->trustword, 1, lang, -1, SQLITE_STATIC);
  2385     sqlite3_bind_int(session->trustword, 2, value);
  2386 
  2387     const int result = sqlite3_step(session->trustword);
  2388     if (result == SQLITE_ROW) {
  2389         *word = strdup((const char *) sqlite3_column_text(session->trustword,
  2390                     1));
  2391         if (*word)
  2392             *wsize = sqlite3_column_bytes(session->trustword, 1);
  2393         else
  2394             status = PEP_OUT_OF_MEMORY;
  2395     } else
  2396         status = PEP_TRUSTWORD_NOT_FOUND;
  2397 
  2398     sqlite3_reset(session->trustword);
  2399     return status;
  2400 }
  2401 
  2402 DYNAMIC_API PEP_STATUS trustwords(
  2403         PEP_SESSION session, const char *fingerprint, const char *lang,
  2404         char **words, size_t *wsize, int max_words
  2405     )
  2406 {
  2407     const char *source = fingerprint;
  2408 
  2409     assert(session);
  2410     assert(fingerprint);
  2411     assert(words);
  2412     assert(wsize);
  2413     assert(max_words >= 0);
  2414 
  2415     if (!(session && fingerprint && words && wsize && max_words >= 0))
  2416         return PEP_ILLEGAL_VALUE;
  2417 
  2418     *words = NULL;
  2419     *wsize = 0;
  2420 
  2421     char *buffer = calloc(1, MAX_TRUSTWORDS_SPACE);
  2422     assert(buffer);
  2423     if (buffer == NULL)
  2424         return PEP_OUT_OF_MEMORY;
  2425     char *dest = buffer;
  2426 
  2427     const size_t fsize = strlen(fingerprint);
  2428 
  2429     if (!lang || !lang[0])
  2430         lang = "en";
  2431 
  2432     assert((lang[0] >= 'A' && lang[0] <= 'Z')
  2433             || (lang[0] >= 'a' && lang[0] <= 'z'));
  2434     assert((lang[1] >= 'A' && lang[1] <= 'Z')
  2435             || (lang[1] >= 'a' && lang[1] <= 'z'));
  2436     assert(lang[2] == 0);
  2437 
  2438     int n_words = 0;
  2439     while (source < fingerprint + fsize) {
  2440         PEP_STATUS _status;
  2441         uint16_t value;
  2442         char *word = NULL;
  2443         size_t _wsize = 0;
  2444         int j;
  2445 
  2446         for (value=0, j=0; j < 4 && source < fingerprint + fsize; ) {
  2447             if (*source >= 'a' && *source <= 'f')
  2448                 value += (*source - 'a' + 10) << (3 - j++) * 4;
  2449             else if (*source >= 'A' && *source <= 'F')
  2450                 value += (*source - 'A' + 10) << (3 - j++) * 4;
  2451             else if (*source >= '0' && *source <= '9')
  2452                 value += (*source - '0') << (3 - j++) * 4;
  2453             
  2454             source++;
  2455         }
  2456 
  2457         _status = trustword(session, value, lang, &word, &_wsize);
  2458         if (_status == PEP_OUT_OF_MEMORY) {
  2459             free(buffer);
  2460             return PEP_OUT_OF_MEMORY;
  2461         }
  2462         if (word == NULL) {
  2463             free(buffer);
  2464             return PEP_TRUSTWORD_NOT_FOUND;
  2465         }
  2466 
  2467         if (dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1) {
  2468             strncpy(dest, word, _wsize);
  2469             free(word);
  2470             dest += _wsize;
  2471         }
  2472         else {
  2473             free(word);
  2474             break; // buffer full
  2475         }
  2476 
  2477         if (source < fingerprint + fsize
  2478                 && dest + _wsize < buffer + MAX_TRUSTWORDS_SPACE - 1)
  2479             *dest++ = ' ';
  2480 
  2481         ++n_words;
  2482         if (max_words && n_words >= max_words)
  2483             break;
  2484     }
  2485 
  2486     *words = buffer;
  2487     *wsize = dest - buffer;
  2488     return PEP_STATUS_OK;
  2489 }
  2490 
  2491 pEp_identity *new_identity(
  2492         const char *address, const char *fpr, const char *user_id,
  2493         const char *username
  2494     )
  2495 {
  2496     pEp_identity *result = calloc(1, sizeof(pEp_identity));
  2497     assert(result);
  2498     if (result) {
  2499         if (address) {
  2500             result->address = strdup(address);
  2501             assert(result->address);
  2502             if (result->address == NULL) {
  2503                 free(result);
  2504                 return NULL;
  2505             }
  2506         }
  2507         if (fpr) {
  2508             result->fpr = strdup(fpr);
  2509             assert(result->fpr);
  2510             if (result->fpr == NULL) {
  2511                 free_identity(result);
  2512                 return NULL;
  2513             }
  2514         }
  2515         if (user_id) {
  2516             result->user_id = strdup(user_id);
  2517             assert(result->user_id);
  2518             if (result->user_id == NULL) {
  2519                 free_identity(result);
  2520                 return NULL;
  2521             }
  2522         }
  2523         if (username) {
  2524             result->username = strdup(username);
  2525             assert(result->username);
  2526             if (result->username == NULL) {
  2527                 free_identity(result);
  2528                 return NULL;
  2529             }
  2530         }
  2531     }
  2532     return result;
  2533 }
  2534 
  2535 pEp_identity *identity_dup(const pEp_identity *src)
  2536 {
  2537     assert(src);
  2538 
  2539     pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id,
  2540             src->username);
  2541     assert(dup);
  2542     if (dup == NULL)
  2543         return NULL;
  2544     
  2545     dup->comm_type = src->comm_type;
  2546     dup->lang[0] = src->lang[0];
  2547     dup->lang[1] = src->lang[1];
  2548     dup->lang[2] = 0;
  2549     dup->flags = src->flags;
  2550     dup->me = src->me;
  2551     dup->major_ver = src->major_ver;
  2552     dup->minor_ver = src->minor_ver;
  2553     
  2554     return dup;
  2555 }
  2556 
  2557 void free_identity(pEp_identity *identity)
  2558 {
  2559     if (identity) {
  2560         free(identity->address);
  2561         free(identity->fpr);
  2562         free(identity->user_id);
  2563         free(identity->username);
  2564         free(identity);
  2565     }
  2566 }
  2567 
  2568 DYNAMIC_API PEP_STATUS get_default_own_userid(
  2569         PEP_SESSION session, 
  2570         char** userid
  2571     )
  2572 {
  2573     assert(session);
  2574     assert(userid);
  2575     
  2576     if (!session || !userid)
  2577         return PEP_ILLEGAL_VALUE;
  2578         
  2579     PEP_STATUS status = PEP_STATUS_OK;
  2580     char* retval = NULL;
  2581     
  2582     sqlite3_reset(session->get_default_own_userid);
  2583 
  2584     const int result = sqlite3_step(session->get_default_own_userid);
  2585     const char* id;
  2586     
  2587     switch (result) {
  2588         case SQLITE_ROW:
  2589             id = (const char *) sqlite3_column_text(session->get_default_own_userid, 0);
  2590             if (!id) {
  2591                 // Shouldn't happen.
  2592                 status = PEP_UNKNOWN_ERROR;
  2593             }
  2594             else {
  2595                 retval = strdup(id);
  2596                 if (!retval)
  2597                     status = PEP_OUT_OF_MEMORY;
  2598             }
  2599             break;
  2600         default:
  2601             // Technically true, given how we find it, but FIXME we need a more descriptive error
  2602             status = PEP_CANNOT_FIND_IDENTITY;
  2603     }
  2604 
  2605     *userid = retval;
  2606 
  2607     sqlite3_reset(session->get_default_own_userid);
  2608     
  2609     return status;
  2610 }
  2611 
  2612 DYNAMIC_API PEP_STATUS get_userid_alias_default(
  2613         PEP_SESSION session, 
  2614         const char* alias_id,
  2615         char** default_id) {
  2616             
  2617     assert(session);
  2618     assert(alias_id);
  2619     assert(alias_id[0]);
  2620     assert(default_id);
  2621 
  2622     if (!(session && alias_id && alias_id[0] && default_id))
  2623         return PEP_ILLEGAL_VALUE;
  2624 
  2625     PEP_STATUS status = PEP_STATUS_OK;
  2626     char* retval = NULL;
  2627 
  2628     sqlite3_reset(session->get_userid_alias_default);
  2629     sqlite3_bind_text(session->get_userid_alias_default, 1, alias_id, -1, SQLITE_STATIC);
  2630 
  2631     const char* tempid;
  2632     
  2633     const int result = sqlite3_step(session->get_userid_alias_default);
  2634     switch (result) {
  2635     case SQLITE_ROW:
  2636         tempid = (const char *) sqlite3_column_text(session->get_userid_alias_default, 0);
  2637         if (tempid) {
  2638             retval = strdup(tempid);
  2639             assert(retval);
  2640             if (retval == NULL)
  2641                 return PEP_OUT_OF_MEMORY;
  2642         }
  2643     
  2644         *default_id = retval;
  2645         break;
  2646     default:
  2647         status = PEP_CANNOT_FIND_ALIAS;
  2648         *default_id = NULL;
  2649     }
  2650 
  2651     sqlite3_reset(session->get_userid_alias_default);
  2652     return status;            
  2653 }
  2654 
  2655 DYNAMIC_API PEP_STATUS set_userid_alias (
  2656         PEP_SESSION session, 
  2657         const char* default_id,
  2658         const char* alias_id) {
  2659             
  2660     int result;
  2661 
  2662     assert(session);
  2663     assert(default_id);
  2664     assert(alias_id);
  2665 
  2666     if (!(session && default_id && alias_id && 
  2667           default_id[0] != '\0' && alias_id[0] != '\0'))
  2668         return PEP_ILLEGAL_VALUE;
  2669     
  2670     sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
  2671 
  2672     sqlite3_reset(session->add_userid_alias);
  2673     sqlite3_bind_text(session->add_userid_alias, 1, default_id, -1,
  2674             SQLITE_STATIC);
  2675     sqlite3_bind_text(session->add_userid_alias, 2, alias_id, -1,
  2676             SQLITE_STATIC);
  2677         
  2678     result = sqlite3_step(session->add_userid_alias);
  2679 
  2680     sqlite3_reset(session->add_userid_alias);
  2681     if (result != SQLITE_DONE) {
  2682         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);        
  2683         return PEP_CANNOT_SET_ALIAS;
  2684     }
  2685     sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
  2686         
  2687 
  2688     return PEP_STATUS_OK;
  2689 }
  2690 
  2691 DYNAMIC_API PEP_STATUS get_identity(
  2692         PEP_SESSION session,
  2693         const char *address,
  2694         const char *user_id,
  2695         pEp_identity **identity
  2696     )
  2697 {
  2698     PEP_STATUS status = PEP_STATUS_OK;
  2699     pEp_identity *_identity = NULL;
  2700 
  2701     assert(session);
  2702     assert(address);
  2703     assert(address[0]);
  2704     assert(identity);
  2705 
  2706     if (!(session && address && address[0] && identity))
  2707         return PEP_ILLEGAL_VALUE;
  2708 
  2709     *identity = NULL;
  2710 
  2711     sqlite3_reset(session->get_identity);
  2712     sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
  2713     sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
  2714 
  2715     const int result = sqlite3_step(session->get_identity);
  2716     switch (result) {
  2717     case SQLITE_ROW:
  2718         _identity = new_identity(
  2719                 address,
  2720                 (const char *) sqlite3_column_text(session->get_identity, 0),
  2721                 user_id,
  2722                 (const char *) sqlite3_column_text(session->get_identity, 1)
  2723                 );
  2724         assert(_identity);
  2725         if (_identity == NULL) {
  2726             sqlite3_reset(session->get_identity);
  2727             return PEP_OUT_OF_MEMORY;
  2728         }
  2729 
  2730         _identity->comm_type = (PEP_comm_type)
  2731             sqlite3_column_int(session->get_identity, 2);
  2732         const char* const _lang = (const char *)
  2733             sqlite3_column_text(session->get_identity, 3);
  2734         if (_lang && _lang[0]) {
  2735             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
  2736             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
  2737             assert(_lang[2] == 0);
  2738             _identity->lang[0] = _lang[0];
  2739             _identity->lang[1] = _lang[1];
  2740             _identity->lang[2] = 0;
  2741         }
  2742         _identity->flags = (unsigned int)
  2743             sqlite3_column_int(session->get_identity, 4);
  2744         _identity->me = (unsigned int)
  2745             sqlite3_column_int(session->get_identity, 5);
  2746         _identity->major_ver =
  2747             sqlite3_column_int(session->get_identity, 6);
  2748         _identity->minor_ver =
  2749             sqlite3_column_int(session->get_identity, 7);
  2750     
  2751         *identity = _identity;
  2752         break;
  2753     default:
  2754         sqlite3_reset(session->get_identity);
  2755         status = PEP_CANNOT_FIND_IDENTITY;
  2756         *identity = NULL;
  2757     }
  2758 
  2759     sqlite3_reset(session->get_identity);
  2760     return status;
  2761 }
  2762 
  2763 PEP_STATUS get_identities_by_userid(
  2764         PEP_SESSION session,
  2765         const char *user_id,
  2766         identity_list **identities
  2767     )
  2768 {
  2769     if (!session || !identities || EMPTYSTR(user_id))
  2770         return PEP_ILLEGAL_VALUE;
  2771 
  2772     PEP_STATUS status = PEP_STATUS_OK;
  2773     
  2774     pEp_identity* ident = NULL;
  2775 
  2776     *identities = new_identity_list(NULL);
  2777 
  2778     sqlite3_reset(session->get_identities_by_userid);
  2779     sqlite3_bind_text(session->get_identities_by_userid, 1, user_id, -1, SQLITE_STATIC);
  2780 
  2781     int result = -1;
  2782     while ((result = sqlite3_step(session->get_identities_by_userid)) == SQLITE_ROW) {
  2783             // "select address, fpr, username, comm_type, lang,"
  2784             // "   identity.flags | pgp_keypair.flags,"
  2785             // "   is_own"
  2786             // "   from identity"
  2787             // "   join person on id = identity.user_id"
  2788             // "   join pgp_keypair on fpr = identity.main_key_id"
  2789             // "   join trust on id = trust.user_id"
  2790             // "       and pgp_keypair_fpr = identity.main_key_id"    
  2791             // "   where identity.user_id = ?1" 
  2792             // "   order by is_own desc, "
  2793             // "   timestamp desc; ";
  2794 
  2795         ident = new_identity(
  2796                     (const char *) sqlite3_column_text(session->get_identities_by_userid, 0),
  2797                     (const char *) sqlite3_column_text(session->get_identities_by_userid, 1),                
  2798                     user_id,
  2799                     (const char *) sqlite3_column_text(session->get_identities_by_userid, 2)
  2800                 );
  2801                 
  2802         assert(ident);
  2803         if (ident == NULL) {
  2804             sqlite3_reset(session->get_identities_by_userid);
  2805             return PEP_OUT_OF_MEMORY;
  2806         }
  2807 
  2808         ident->comm_type = (PEP_comm_type)
  2809             sqlite3_column_int(session->get_identities_by_userid, 3);
  2810         const char* const _lang = (const char *)
  2811             sqlite3_column_text(session->get_identities_by_userid, 4);
  2812         if (_lang && _lang[0]) {
  2813             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
  2814             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
  2815             assert(_lang[2] == 0);
  2816             ident->lang[0] = _lang[0];
  2817             ident->lang[1] = _lang[1];
  2818             ident->lang[2] = 0;
  2819         }
  2820         ident->flags = (unsigned int)
  2821             sqlite3_column_int(session->get_identities_by_userid, 5);
  2822         ident->me = (unsigned int)
  2823             sqlite3_column_int(session->get_identities_by_userid, 6);
  2824         ident->major_ver =
  2825             sqlite3_column_int(session->get_identities_by_userid, 7);
  2826         ident->minor_ver =
  2827             sqlite3_column_int(session->get_identities_by_userid, 8);
  2828     
  2829         identity_list_add(*identities, ident);
  2830         ident = NULL;
  2831     }
  2832 
  2833     if ((*identities)->ident == NULL) {
  2834         free_identity_list(*identities);
  2835         *identities = NULL;
  2836         status = PEP_CANNOT_FIND_IDENTITY;
  2837     }
  2838             
  2839     sqlite3_reset(session->get_identities_by_userid);
  2840 
  2841     return status;
  2842 }
  2843 
  2844 PEP_STATUS get_identities_by_main_key_id(
  2845         PEP_SESSION session,
  2846         const char *fpr,
  2847         identity_list **identities
  2848     )
  2849 {
  2850     if (!session || !identities || EMPTYSTR(fpr))
  2851         return PEP_ILLEGAL_VALUE;
  2852 
  2853     PEP_STATUS status = PEP_STATUS_OK;
  2854     
  2855     pEp_identity* ident = NULL;
  2856 
  2857     *identities = new_identity_list(NULL);
  2858 
  2859     sqlite3_reset(session->get_identities_by_main_key_id);
  2860     sqlite3_bind_text(session->get_identities_by_main_key_id, 1, fpr, -1, SQLITE_STATIC);
  2861 
  2862     int result = -1;
  2863     
  2864     while ((result = sqlite3_step(session->get_identities_by_main_key_id)) == SQLITE_ROW) {
  2865         ident = new_identity(
  2866                     (const char *) sqlite3_column_text(session->get_identities_by_main_key_id, 0),
  2867                     fpr,
  2868                     (const char *) sqlite3_column_text(session->get_identities_by_main_key_id, 1),                
  2869                     (const char *) sqlite3_column_text(session->get_identities_by_main_key_id, 2)
  2870                 );
  2871                 
  2872         assert(ident);
  2873         if (ident == NULL) {
  2874             sqlite3_reset(session->get_identities_by_main_key_id);
  2875             return PEP_OUT_OF_MEMORY;
  2876         }
  2877 
  2878         ident->comm_type = (PEP_comm_type)
  2879             sqlite3_column_int(session->get_identities_by_main_key_id, 3);
  2880         const char* const _lang = (const char *)
  2881             sqlite3_column_text(session->get_identities_by_main_key_id, 4);
  2882         if (_lang && _lang[0]) {
  2883             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
  2884             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
  2885             assert(_lang[2] == 0);
  2886             ident->lang[0] = _lang[0];
  2887             ident->lang[1] = _lang[1];
  2888             ident->lang[2] = 0;
  2889         }
  2890         ident->flags = (unsigned int)
  2891             sqlite3_column_int(session->get_identities_by_main_key_id, 5);
  2892         ident->me = (unsigned int)
  2893             sqlite3_column_int(session->get_identities_by_main_key_id, 6);
  2894         ident->major_ver =
  2895             sqlite3_column_int(session->get_identities_by_main_key_id, 7);
  2896         ident->minor_ver =
  2897             sqlite3_column_int(session->get_identities_by_main_key_id, 8);
  2898     
  2899         identity_list_add(*identities, ident);
  2900         ident = NULL;
  2901     }
  2902 
  2903     if ((*identities)->ident == NULL) {
  2904         free_identity_list(*identities);
  2905         *identities = NULL;
  2906         status = PEP_CANNOT_FIND_IDENTITY;
  2907     }
  2908             
  2909     sqlite3_reset(session->get_identities_by_main_key_id);
  2910 
  2911     return status;
  2912 }
  2913 
  2914 PEP_STATUS get_identity_without_trust_check(
  2915         PEP_SESSION session,
  2916         const char *address,
  2917         const char *user_id,
  2918         pEp_identity **identity
  2919     )
  2920 {
  2921     PEP_STATUS status = PEP_STATUS_OK;
  2922     pEp_identity *_identity = NULL;
  2923 
  2924     assert(session);
  2925     assert(address);
  2926     assert(address[0]);
  2927     assert(identity);
  2928 
  2929     if (!(session && address && address[0] && identity))
  2930         return PEP_ILLEGAL_VALUE;
  2931 
  2932     *identity = NULL;
  2933 
  2934     sqlite3_reset(session->get_identity_without_trust_check);
  2935     sqlite3_bind_text(session->get_identity_without_trust_check, 1, address, -1, SQLITE_STATIC);
  2936     sqlite3_bind_text(session->get_identity_without_trust_check, 2, user_id, -1, SQLITE_STATIC);
  2937 
  2938     const int result = sqlite3_step(session->get_identity_without_trust_check);
  2939     switch (result) {
  2940     case SQLITE_ROW:
  2941         _identity = new_identity(
  2942                 address,
  2943                 (const char *) sqlite3_column_text(session->get_identity_without_trust_check, 0),
  2944                 user_id,
  2945                 (const char *) sqlite3_column_text(session->get_identity_without_trust_check, 1)
  2946                 );
  2947         assert(_identity);
  2948         if (_identity == NULL) {
  2949             sqlite3_reset(session->get_identity_without_trust_check);
  2950             return PEP_OUT_OF_MEMORY;
  2951         }
  2952 
  2953         _identity->comm_type = PEP_ct_unknown;
  2954         const char* const _lang = (const char *)
  2955             sqlite3_column_text(session->get_identity_without_trust_check, 2);
  2956         if (_lang && _lang[0]) {
  2957             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
  2958             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
  2959             assert(_lang[2] == 0);
  2960             _identity->lang[0] = _lang[0];
  2961             _identity->lang[1] = _lang[1];
  2962             _identity->lang[2] = 0;
  2963         }
  2964         _identity->flags = (unsigned int)
  2965             sqlite3_column_int(session->get_identity_without_trust_check, 3);
  2966         _identity->me = (unsigned int)
  2967             sqlite3_column_int(session->get_identity_without_trust_check, 4);
  2968         _identity->major_ver =
  2969             sqlite3_column_int(session->get_identity_without_trust_check, 5);
  2970         _identity->minor_ver =
  2971             sqlite3_column_int(session->get_identity_without_trust_check, 6);
  2972     
  2973         *identity = _identity;
  2974         break;
  2975     default:
  2976         status = PEP_CANNOT_FIND_IDENTITY;
  2977         *identity = NULL;
  2978     }
  2979 
  2980     sqlite3_reset(session->get_identity_without_trust_check);
  2981     return status;
  2982 }
  2983 
  2984 
  2985 PEP_STATUS get_identities_by_address(
  2986         PEP_SESSION session,
  2987         const char *address,
  2988         identity_list** id_list
  2989     )
  2990 {
  2991     assert(session);
  2992     assert(address);
  2993     assert(address[0]);
  2994     assert(id_list);
  2995 
  2996     if (!(session && address && address[0] && id_list))
  2997         return PEP_ILLEGAL_VALUE;
  2998 
  2999     *id_list = NULL;
  3000     identity_list* ident_list = NULL;
  3001 
  3002     sqlite3_reset(session->get_identities_by_address);
  3003     sqlite3_bind_text(session->get_identities_by_address, 1, address, -1, SQLITE_STATIC);
  3004     int result;
  3005 
  3006     while ((result = sqlite3_step(session->get_identities_by_address)) == SQLITE_ROW) {
  3007         //"select user_id, main_key_id, username, comm_type, lang,"
  3008         //"   identity.flags, is_own"
  3009         pEp_identity *ident = new_identity(
  3010                 address,
  3011                 (const char *) sqlite3_column_text(session->get_identities_by_address, 1),
  3012                 (const char *) sqlite3_column_text(session->get_identities_by_address, 0),
  3013                 (const char *) sqlite3_column_text(session->get_identities_by_address, 2)
  3014                 );
  3015         assert(ident);
  3016         if (ident == NULL) {
  3017             sqlite3_reset(session->get_identities_by_address);
  3018             return PEP_OUT_OF_MEMORY;
  3019         }
  3020 
  3021         ident->comm_type = PEP_ct_unknown;
  3022         
  3023         const char* const _lang = (const char *)
  3024             sqlite3_column_text(session->get_identities_by_address, 3);
  3025         if (_lang && _lang[0]) {
  3026             assert(_lang[0] >= 'a' && _lang[0] <= 'z');
  3027             assert(_lang[1] >= 'a' && _lang[1] <= 'z');
  3028             assert(_lang[2] == 0);
  3029             ident->lang[0] = _lang[0];
  3030             ident->lang[1] = _lang[1];
  3031             ident->lang[2] = 0;
  3032         }
  3033         ident->flags = (unsigned int)
  3034             sqlite3_column_int(session->get_identities_by_address, 4);
  3035         ident->me = (unsigned int)
  3036             sqlite3_column_int(session->get_identities_by_address, 5);
  3037         ident->major_ver =
  3038             sqlite3_column_int(session->get_identities_by_address, 6);
  3039         ident->minor_ver =
  3040             sqlite3_column_int(session->get_identities_by_address, 7);
  3041     
  3042         if (ident_list)
  3043             identity_list_add(ident_list, ident);
  3044         else
  3045             ident_list = new_identity_list(ident);
  3046     }
  3047 
  3048     sqlite3_reset(session->get_identities_by_address);
  3049     
  3050     *id_list = ident_list;
  3051     
  3052     if (!ident_list)
  3053         return PEP_CANNOT_FIND_IDENTITY;
  3054     
  3055     return PEP_STATUS_OK;
  3056 }
  3057 
  3058 PEP_STATUS exists_identity_entry(PEP_SESSION session, pEp_identity* identity,
  3059                                  bool* exists) {
  3060     assert(session);
  3061     assert(identity);
  3062     assert(!EMPTYSTR(identity->user_id));        
  3063     assert(!EMPTYSTR(identity->address));
  3064     if (!session || !exists || EMPTYSTR(identity->user_id) || EMPTYSTR(identity->address))
  3065         return PEP_ILLEGAL_VALUE;
  3066     
  3067     *exists = false;
  3068     
  3069     PEP_STATUS status = PEP_STATUS_OK;
  3070     
  3071     sqlite3_reset(session->exists_identity_entry);
  3072     sqlite3_bind_text(session->exists_identity_entry, 1, identity->address, -1,
  3073                       SQLITE_STATIC);
  3074     sqlite3_bind_text(session->exists_identity_entry, 2, identity->user_id, -1,
  3075                       SQLITE_STATIC);
  3076                   
  3077     int result = sqlite3_step(session->exists_identity_entry);
  3078 
  3079     switch (result) {
  3080         case SQLITE_ROW: {
  3081             // yeah yeah, I know, we could be lazy here, but it looks bad.
  3082             *exists = (sqlite3_column_int(session->exists_identity_entry, 0) != 0);
  3083             break;
  3084         }
  3085         default: 
  3086             status = PEP_UNKNOWN_DB_ERROR;
  3087     }
  3088 
  3089     sqlite3_reset(session->exists_identity_entry);
  3090     return status;
  3091 }
  3092 
  3093 PEP_STATUS exists_trust_entry(PEP_SESSION session, pEp_identity* identity,
  3094                               bool* exists) {
  3095     assert(session);
  3096     assert(identity);
  3097     assert(!EMPTYSTR(identity->user_id));        
  3098     assert(!EMPTYSTR(identity->fpr));
  3099     if (!session || !exists || EMPTYSTR(identity->user_id) || EMPTYSTR(identity->fpr))
  3100         return PEP_ILLEGAL_VALUE;
  3101     
  3102     *exists = false;
  3103     
  3104     PEP_STATUS status = PEP_STATUS_OK;
  3105     
  3106     sqlite3_reset(session->exists_trust_entry);
  3107     sqlite3_bind_text(session->exists_trust_entry, 1, identity->user_id, -1,
  3108                       SQLITE_STATIC);
  3109     sqlite3_bind_text(session->exists_trust_entry, 2, identity->fpr, -1,
  3110                       SQLITE_STATIC);
  3111                   
  3112     int result = sqlite3_step(session->exists_trust_entry);
  3113     switch (result) {
  3114         case SQLITE_ROW: {
  3115             // yeah yeah, I know, we could be lazy here, but it looks bad.
  3116             *exists = (sqlite3_column_int(session->exists_trust_entry, 0) != 0);
  3117             break;
  3118         }
  3119         default:
  3120             status = PEP_UNKNOWN_DB_ERROR;
  3121     }
  3122     
  3123     sqlite3_reset(session->exists_trust_entry);
  3124     return status;
  3125 }
  3126 
  3127 PEP_STATUS set_pgp_keypair(PEP_SESSION session, const char* fpr) {
  3128     if (!session || EMPTYSTR(fpr))
  3129         return PEP_ILLEGAL_VALUE;
  3130         
  3131     int result;
  3132     
  3133     sqlite3_reset(session->set_pgp_keypair);
  3134     sqlite3_bind_text(session->set_pgp_keypair, 1, fpr, -1,
  3135             SQLITE_STATIC);
  3136     result = sqlite3_step(session->set_pgp_keypair);
  3137     sqlite3_reset(session->set_pgp_keypair);
  3138     if (result != SQLITE_DONE) {
  3139         return PEP_CANNOT_SET_PGP_KEYPAIR;
  3140     }
  3141     
  3142     return PEP_STATUS_OK;
  3143 }
  3144 
  3145 PEP_STATUS clear_trust_info(PEP_SESSION session,
  3146                             const char* user_id,
  3147                             const char* fpr) {
  3148     if (!session || EMPTYSTR(fpr) || EMPTYSTR(user_id))
  3149         return PEP_ILLEGAL_VALUE;
  3150         
  3151     int result;
  3152     
  3153     sqlite3_reset(session->clear_trust_info);
  3154     sqlite3_bind_text(session->clear_trust_info, 1, user_id, -1,
  3155             SQLITE_STATIC);    
  3156     sqlite3_bind_text(session->clear_trust_info, 2, fpr, -1,
  3157             SQLITE_STATIC);
  3158     result = sqlite3_step(session->clear_trust_info);
  3159     sqlite3_reset(session->clear_trust_info);
  3160     if (result != SQLITE_DONE) {
  3161         return PEP_UNKNOWN_ERROR;
  3162     }
  3163     
  3164     return PEP_STATUS_OK;
  3165 }
  3166 
  3167 static PEP_STATUS _set_or_update_trust(PEP_SESSION session,
  3168                                        pEp_identity* identity,
  3169                                        sqlite3_stmt* set_or_update) {
  3170 
  3171     assert(session);
  3172     assert(identity);
  3173     assert(identity->user_id);
  3174     assert(identity->fpr);
  3175     
  3176     if (!session || !identity || EMPTYSTR(identity->user_id) || EMPTYSTR(identity->fpr))
  3177         return PEP_ILLEGAL_VALUE;
  3178         
  3179     PEP_STATUS status = set_pgp_keypair(session, identity->fpr);
  3180     if (status != PEP_STATUS_OK)
  3181         return status;
  3182         
  3183     int result;
  3184                 
  3185     sqlite3_reset(set_or_update);
  3186     sqlite3_bind_text(set_or_update, 1, identity->user_id, -1,
  3187             SQLITE_STATIC);
  3188     sqlite3_bind_text(set_or_update, 2, identity->fpr, -1,
  3189             SQLITE_STATIC);
  3190     sqlite3_bind_int(set_or_update, 3, identity->comm_type);
  3191     result = sqlite3_step(set_or_update);
  3192     assert(result == SQLITE_DONE);
  3193     sqlite3_reset(set_or_update);
  3194     if (result != SQLITE_DONE)
  3195         return PEP_CANNOT_SET_TRUST;
  3196 
  3197     return PEP_STATUS_OK;
  3198 }
  3199 
  3200 static PEP_STATUS _set_or_update_identity_entry(PEP_SESSION session,
  3201                                                 pEp_identity* identity,
  3202                                                 sqlite3_stmt* set_or_update) {
  3203     assert(session);
  3204     assert(identity);
  3205     assert(set_or_update);
  3206                       
  3207     if (!session || !identity || !identity->user_id || !identity->address)
  3208         return PEP_ILLEGAL_VALUE;
  3209                                               
  3210     sqlite3_reset(set_or_update);
  3211     sqlite3_bind_text(set_or_update, 1, identity->address, -1,
  3212             SQLITE_STATIC);
  3213     sqlite3_bind_text(set_or_update, 2, identity->fpr, -1,
  3214             SQLITE_STATIC);
  3215     sqlite3_bind_text(set_or_update, 3, identity->user_id, -1,
  3216             SQLITE_STATIC);
  3217     sqlite3_bind_int(set_or_update, 4, identity->flags);
  3218     sqlite3_bind_int(set_or_update, 5, identity->me);
  3219     sqlite3_bind_int(set_or_update, 6, identity->major_ver);
  3220     sqlite3_bind_int(set_or_update, 7, identity->minor_ver);
  3221         
  3222     int result = sqlite3_step(set_or_update);
  3223     sqlite3_reset(set_or_update);
  3224     if (result != SQLITE_DONE)
  3225         return PEP_CANNOT_SET_IDENTITY;
  3226     
  3227     return PEP_STATUS_OK;
  3228 }
  3229 
  3230 static PEP_STATUS _set_or_update_person(PEP_SESSION session, 
  3231                                         pEp_identity* identity,
  3232                                         sqlite3_stmt* set_or_update) {
  3233     assert(session);
  3234     assert(identity);
  3235     assert(set_or_update);
  3236                         
  3237     if (!session || !identity || !identity->user_id || !identity->username)
  3238         return PEP_ILLEGAL_VALUE;
  3239         
  3240     sqlite3_reset(set_or_update);
  3241     sqlite3_bind_text(set_or_update, 1, identity->user_id, -1,
  3242             SQLITE_STATIC);
  3243     sqlite3_bind_text(set_or_update, 2, identity->username, -1,
  3244             SQLITE_STATIC);
  3245     if (identity->lang[0])
  3246         sqlite3_bind_text(set_or_update, 3, identity->lang, 2,
  3247                 SQLITE_STATIC);
  3248     else
  3249         sqlite3_bind_null(set_or_update, 3);
  3250     sqlite3_bind_text(set_or_update, 4, identity->fpr, -1,
  3251                       SQLITE_STATIC);
  3252     int result = sqlite3_step(set_or_update);
  3253     sqlite3_reset(set_or_update);
  3254     
  3255     if (result != SQLITE_DONE)
  3256         return PEP_CANNOT_SET_PERSON;
  3257     
  3258     return PEP_STATUS_OK;                                         
  3259 }
  3260 
  3261 PEP_STATUS set_or_update_with_identity(PEP_SESSION session,
  3262                                        pEp_identity* identity,
  3263                                        PEP_STATUS (* set_function)(PEP_SESSION, pEp_identity*, sqlite3_stmt*),
  3264                                        PEP_STATUS (* exists_function)(PEP_SESSION, pEp_identity*, bool*),                                       
  3265                                        sqlite3_stmt* update_query,
  3266                                        sqlite3_stmt* set_query,
  3267                                        bool guard_transaction) {
  3268 
  3269     if (guard_transaction) {
  3270         sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
  3271     }
  3272     bool exists = false;
  3273     PEP_STATUS status = exists_function(session, identity, &exists);
  3274     
  3275     if (status == PEP_STATUS_OK) {
  3276         if (exists) {
  3277             status = set_function(session, identity, update_query);
  3278         }
  3279         else {
  3280             status = set_function(session, identity, set_query);                                              
  3281         }                    
  3282     }   
  3283     if (guard_transaction) {        
  3284         if (status != PEP_STATUS_OK)
  3285             sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3286         else 
  3287             sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
  3288     }                      
  3289     return status;
  3290 }
  3291 
  3292 PEP_STATUS _set_trust_internal(PEP_SESSION session, pEp_identity* identity,
  3293                                bool guard_transaction) {
  3294     return set_or_update_with_identity(session, identity,
  3295                                        _set_or_update_trust,
  3296                                         exists_trust_entry,
  3297                                         session->update_trust,
  3298                                         session->set_trust,
  3299                                         guard_transaction);
  3300 }
  3301 
  3302 // This is the TOP-LEVEL function. If you're calling from set_identity,
  3303 // you can't use this one.
  3304 PEP_STATUS set_trust(PEP_SESSION session, pEp_identity* identity) {
  3305     PEP_STATUS status = PEP_STATUS_OK;
  3306     
  3307     status = _set_trust_internal(session, identity, true);
  3308     if (status == PEP_STATUS_OK) {
  3309         if ((identity->comm_type | PEP_ct_confirmed) == PEP_ct_pEp)
  3310             status = set_as_pEp_user(session, identity);
  3311     }
  3312     return status;
  3313 }
  3314 
  3315 PEP_STATUS set_person(PEP_SESSION session, pEp_identity* identity,
  3316                       bool guard_transaction) {
  3317     return set_or_update_with_identity(session, identity,
  3318                                        _set_or_update_person,
  3319                                        exists_person,
  3320                                        session->update_person,
  3321                                        session->set_person,
  3322                                        guard_transaction);
  3323 }
  3324 
  3325 PEP_STATUS set_identity_entry(PEP_SESSION session, pEp_identity* identity,
  3326                               bool guard_transaction) {
  3327     return set_or_update_with_identity(session, identity,
  3328                                        _set_or_update_identity_entry,
  3329                                        exists_identity_entry,
  3330                                        session->update_identity_entry,
  3331                                        session->set_identity_entry,
  3332                                        guard_transaction);
  3333 }
  3334 
  3335 // This will NOT call set_as_pEp_user, nor set_pEp_version; you have to do that separately.
  3336 DYNAMIC_API PEP_STATUS set_identity(
  3337         PEP_SESSION session, const pEp_identity *identity
  3338     )
  3339 {
  3340     int result;
  3341 
  3342     assert(session);
  3343     assert(identity);
  3344     assert(identity->address);
  3345     assert(identity->user_id);
  3346     assert(identity->username);
  3347 
  3348     if (!(session && identity && identity->address &&
  3349                 identity->user_id && identity->username))
  3350         return PEP_ILLEGAL_VALUE;
  3351 
  3352     PEP_STATUS status = PEP_STATUS_OK;
  3353     
  3354     bool has_fpr = (!EMPTYSTR(identity->fpr));
  3355     
  3356     sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
  3357 
  3358     if (identity->lang[0]) {
  3359         assert(identity->lang[0] >= 'a' && identity->lang[0] <= 'z');
  3360         assert(identity->lang[1] >= 'a' && identity->lang[1] <= 'z');
  3361         assert(identity->lang[2] == 0);
  3362     }
  3363 
  3364     if (has_fpr) {
  3365         sqlite3_reset(session->set_pgp_keypair);
  3366         sqlite3_bind_text(session->set_pgp_keypair, 1, identity->fpr, -1,
  3367                 SQLITE_STATIC);
  3368         result = sqlite3_step(session->set_pgp_keypair);
  3369         sqlite3_reset(session->set_pgp_keypair);
  3370         if (result != SQLITE_DONE) {
  3371             sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3372             return PEP_CANNOT_SET_PGP_KEYPAIR;
  3373         }
  3374     }
  3375 
  3376     // We do this because there are checks in set_person for
  3377     // aliases, which modify the identity object on return.
  3378     pEp_identity* ident_copy = identity_dup(identity); 
  3379     if (!ident_copy)
  3380         return PEP_OUT_OF_MEMORY;
  3381 
  3382     status = set_person(session, ident_copy, false);
  3383     if (status != PEP_STATUS_OK) {
  3384         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3385         goto pEp_free;
  3386     }
  3387 
  3388     status = set_identity_entry(session, ident_copy, false);
  3389     if (status != PEP_STATUS_OK) {
  3390         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3391         goto pEp_free;
  3392     }
  3393 
  3394     if (has_fpr) {
  3395         status = _set_trust_internal(session, ident_copy, false);
  3396         if (status != PEP_STATUS_OK) {
  3397             sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3398             goto pEp_free;
  3399         }
  3400     }
  3401     
  3402     status = set_pEp_version(session, ident_copy, ident_copy->major_ver, ident_copy->minor_ver);
  3403     if (status != PEP_STATUS_OK) {
  3404         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  3405         goto pEp_free;            
  3406     }
  3407     
  3408     result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
  3409     if (result == SQLITE_OK)
  3410         status = PEP_STATUS_OK;
  3411     else
  3412         status = PEP_COMMIT_FAILED;
  3413 
  3414 pEp_free:
  3415     free_identity(ident_copy);
  3416     return status;
  3417 }
  3418 
  3419 PEP_STATUS update_pEp_user_trust_vals(PEP_SESSION session,
  3420                                       pEp_identity* user) {
  3421     if (!user->user_id)
  3422         return PEP_ILLEGAL_VALUE;
  3423     
  3424     sqlite3_reset(session->update_trust_to_pEp);
  3425     sqlite3_bind_text(session->update_trust_to_pEp, 1, user->user_id, -1,
  3426             SQLITE_STATIC);
  3427     int result = sqlite3_step(session->update_trust_to_pEp);
  3428     sqlite3_reset(session->update_trust_to_pEp);
  3429     if (result != SQLITE_DONE)
  3430         return PEP_CANNOT_SET_TRUST;
  3431 
  3432     PEP_STATUS status = upgrade_pEp_version_by_user_id(session, user, 2, 0);
  3433     
  3434     return status;
  3435 }
  3436 
  3437 
  3438 // This ONLY sets the user flag. Must be called outside of a transaction.
  3439 DYNAMIC_API PEP_STATUS set_as_pEp_user(PEP_SESSION session, pEp_identity* user) {
  3440 
  3441     assert(session);
  3442     assert(user);
  3443     assert(!EMPTYSTR(user->user_id));
  3444         
  3445     if (!session || !user || EMPTYSTR(user->user_id))
  3446         return PEP_ILLEGAL_VALUE;
  3447             
  3448     PEP_STATUS status = PEP_STATUS_OK;
  3449     
  3450     bool person_exists = false;
  3451     
  3452     status = exists_person(session, user, &person_exists);
  3453     
  3454     if (status != PEP_STATUS_OK)
  3455         return status;
  3456         
  3457     if (!person_exists)
  3458         status = set_person(session, user, true);
  3459         
  3460     // Ok, let's set it.
  3461     sqlite3_reset(session->set_as_pEp_user);
  3462     sqlite3_bind_text(session->set_as_pEp_user, 1, user->user_id, -1,
  3463             SQLITE_STATIC);
  3464     int result = sqlite3_step(session->set_as_pEp_user);
  3465     sqlite3_reset(session->set_as_pEp_user);
  3466     
  3467     if (result != SQLITE_DONE)
  3468         return PEP_CANNOT_SET_PERSON;
  3469 
  3470     status = update_pEp_user_trust_vals(session, user);
  3471         
  3472     return status;
  3473 }
  3474 
  3475 // This ONLY sets the version flag. Must be called outside of a transaction.
  3476 PEP_STATUS set_pEp_version(PEP_SESSION session, pEp_identity* ident, unsigned int new_ver_major, unsigned int new_ver_minor) {
  3477     assert(session);
  3478     assert(!EMPTYSTR(ident->user_id));
  3479     assert(!EMPTYSTR(ident->address));
  3480     
  3481     sqlite3_reset(session->set_pEp_version);
  3482     sqlite3_bind_double(session->set_pEp_version, 1, new_ver_major);
  3483     sqlite3_bind_double(session->set_pEp_version, 2, new_ver_minor);    
  3484     sqlite3_bind_text(session->set_pEp_version, 3, ident->address, -1,
  3485             SQLITE_STATIC);
  3486     sqlite3_bind_text(session->set_pEp_version, 4, ident->user_id, -1,
  3487             SQLITE_STATIC);
  3488     
  3489     int result = sqlite3_step(session->set_pEp_version);
  3490     sqlite3_reset(session->set_pEp_version);
  3491         
  3492     if (result != SQLITE_DONE)
  3493         return PEP_CANNOT_SET_PEP_VERSION;
  3494     
  3495     return PEP_STATUS_OK;
  3496 }
  3497 
  3498 // Generally ONLY called by set_as_pEp_user, and ONLY from < 2.0 to 2.0.
  3499 PEP_STATUS upgrade_pEp_version_by_user_id(PEP_SESSION session, 
  3500         pEp_identity* ident, 
  3501         unsigned int new_ver_major,
  3502         unsigned int new_ver_minor
  3503     ) 
  3504 {
  3505     assert(session);
  3506     assert(!EMPTYSTR(ident->user_id));
  3507     
  3508     sqlite3_reset(session->upgrade_pEp_version_by_user_id);
  3509     sqlite3_bind_int(session->upgrade_pEp_version_by_user_id, 1, new_ver_major);
  3510     sqlite3_bind_int(session->upgrade_pEp_version_by_user_id, 2, new_ver_minor);    
  3511     sqlite3_bind_text(session->upgrade_pEp_version_by_user_id, 3, ident->user_id, -1,
  3512             SQLITE_STATIC);
  3513     
  3514     int result = sqlite3_step(session->upgrade_pEp_version_by_user_id);
  3515     sqlite3_reset(session->upgrade_pEp_version_by_user_id);
  3516         
  3517     if (result != SQLITE_DONE)
  3518         return PEP_CANNOT_SET_PEP_VERSION;
  3519     
  3520     return PEP_STATUS_OK;    
  3521 }
  3522 
  3523 PEP_STATUS exists_person(PEP_SESSION session, pEp_identity* identity,
  3524                          bool* exists) {            
  3525     
  3526     // const char* user_id,
  3527     //                      char** default_id, bool* exists) {
  3528     assert(session);
  3529     assert(exists);
  3530     assert(identity);
  3531     assert(!EMPTYSTR(identity->user_id));
  3532         
  3533     if (!session || !exists || !identity || EMPTYSTR(identity->user_id))
  3534         return PEP_ILLEGAL_VALUE;
  3535     
  3536     *exists = false;
  3537 
  3538     const char* user_id = identity->user_id;
  3539     char* alias_default = NULL;
  3540     
  3541     PEP_STATUS status = get_userid_alias_default(session, user_id, &alias_default);
  3542     
  3543     if (status == PEP_CANNOT_FIND_ALIAS || EMPTYSTR(alias_default)) {
  3544         sqlite3_reset(session->exists_person);
  3545         sqlite3_bind_text(session->exists_person, 1, user_id, -1,
  3546                 SQLITE_STATIC);
  3547         int result = sqlite3_step(session->exists_person);
  3548         switch (result) {
  3549             case SQLITE_ROW: {
  3550                 // yeah yeah, I know, we could be lazy here, but it looks bad.
  3551                 *exists = (sqlite3_column_int(session->exists_person, 0) != 0);
  3552                 status = PEP_STATUS_OK;
  3553                 break;
  3554             }
  3555             default:
  3556                 sqlite3_reset(session->exists_person);
  3557                 return PEP_UNKNOWN_DB_ERROR;
  3558         }
  3559         sqlite3_reset(session->exists_person);
  3560     }
  3561     else if (status == PEP_STATUS_OK) {
  3562         *exists = true; // thank you, delete on cascade!
  3563         // FIXME: Should we correct the userid default here? I think we should.
  3564         free(identity->user_id);
  3565         identity->user_id = alias_default; // ownership transfer
  3566     }
  3567     else
  3568         free(alias_default);
  3569             
  3570     return status;
  3571 }
  3572 
  3573 PEP_STATUS delete_person(PEP_SESSION session, const char* user_id) {
  3574     assert(session);
  3575     assert(!EMPTYSTR(user_id));        
  3576     if (!session || EMPTYSTR(user_id))
  3577         return PEP_ILLEGAL_VALUE;
  3578         
  3579     PEP_STATUS status = PEP_STATUS_OK;
  3580     
  3581     sqlite3_reset(session->delete_person);
  3582     sqlite3_bind_text(session->delete_person, 1, user_id, -1,
  3583                       SQLITE_STATIC);
  3584                       
  3585     int result = sqlite3_step(session->delete_person);
  3586     
  3587     if (result != SQLITE_DONE)
  3588         status = PEP_UNKNOWN_ERROR;
  3589         
  3590     sqlite3_reset(session->delete_person);
  3591     return status;
  3592 }
  3593 
  3594 DYNAMIC_API PEP_STATUS is_pEp_user(PEP_SESSION session, pEp_identity *identity, bool* is_pEp)
  3595 {
  3596     assert(session);
  3597     assert(is_pEp);
  3598     assert(identity);
  3599     assert(!EMPTYSTR(identity->user_id));
  3600 
  3601     if (!session || !is_pEp || !identity || EMPTYSTR(identity->user_id))
  3602         return PEP_ILLEGAL_VALUE;
  3603     
  3604     *is_pEp = false;
  3605             
  3606     const char* user_id = identity->user_id;
  3607             
  3608     char* alias_default = NULL;
  3609     
  3610     PEP_STATUS status = get_userid_alias_default(session, user_id, &alias_default);
  3611     
  3612     if (status == PEP_CANNOT_FIND_ALIAS || EMPTYSTR(alias_default)) {
  3613         free(alias_default);
  3614         alias_default = strdup(user_id);
  3615     }
  3616     
  3617     sqlite3_reset(session->is_pEp_user);
  3618     sqlite3_bind_text(session->is_pEp_user, 1, user_id, -1,
  3619             SQLITE_STATIC);
  3620     int result = sqlite3_step(session->is_pEp_user);
  3621     switch (result) {
  3622         case SQLITE_ROW: {
  3623             // yeah yeah, I know, we could be lazy here, but it looks bad.
  3624             *is_pEp = (sqlite3_column_int(session->is_pEp_user, 0) != 0);
  3625             break;
  3626         }
  3627         default:
  3628             sqlite3_reset(session->is_pEp_user);
  3629             free(alias_default);
  3630             return PEP_CANNOT_FIND_PERSON;
  3631     }
  3632 
  3633     sqlite3_reset(session->is_pEp_user);
  3634     
  3635     free(alias_default);
  3636     return PEP_STATUS_OK;
  3637 }
  3638 
  3639 PEP_STATUS is_own_address(PEP_SESSION session, const char* address, bool* is_own_addr)
  3640 {
  3641     assert(session);
  3642     assert(is_own_addr);
  3643     assert(!EMPTYSTR(address));
  3644 
  3645     if (!session || !is_own_addr || EMPTYSTR(address))
  3646         return PEP_ILLEGAL_VALUE;
  3647     
  3648     *is_own_addr = false;
  3649                 
  3650     if (!session || EMPTYSTR(address))
  3651         return PEP_ILLEGAL_VALUE;
  3652         
  3653     sqlite3_reset(session->is_own_address);
  3654     sqlite3_bind_text(session->is_own_address, 1, address, -1,
  3655             SQLITE_STATIC);
  3656     int result = sqlite3_step(session->is_own_address);
  3657     switch (result) {
  3658         case SQLITE_ROW: {
  3659             // yeah yeah, I know, we could be lazy here, but it looks bad.
  3660             *is_own_addr = (sqlite3_column_int(session->is_own_address, 0) != 0);
  3661             break;
  3662         }
  3663         default:
  3664             sqlite3_reset(session->is_own_address);
  3665             return PEP_RECORD_NOT_FOUND;
  3666     }
  3667 
  3668     sqlite3_reset(session->is_own_address);
  3669     
  3670     return PEP_STATUS_OK;
  3671 }
  3672 
  3673 PEP_STATUS bind_own_ident_with_contact_ident(PEP_SESSION session,
  3674                                              pEp_identity* own_ident, 
  3675                                              pEp_identity* contact_ident) {
  3676     if (!own_ident || !contact_ident || 
  3677         !own_ident->address || !own_ident->user_id || !contact_ident->user_id)
  3678         return PEP_ILLEGAL_VALUE;
  3679         
  3680     sqlite3_reset(session->add_into_social_graph);
  3681     sqlite3_bind_text(session->add_into_social_graph, 1, own_ident->user_id, -1,
  3682             SQLITE_STATIC);
  3683     sqlite3_bind_text(session->add_into_social_graph, 2, own_ident->address, -1,
  3684             SQLITE_STATIC);
  3685     sqlite3_bind_text(session->add_into_social_graph, 3, contact_ident->user_id, -1,
  3686             SQLITE_STATIC);
  3687         
  3688     int result = sqlite3_step(session->add_into_social_graph);
  3689     sqlite3_reset(session->add_into_social_graph);
  3690     
  3691     if (result != SQLITE_DONE)
  3692         return PEP_CANNOT_SET_PERSON;
  3693 
  3694     return PEP_STATUS_OK;
  3695 }
  3696 
  3697 // FIXME: should be more like is there a communications relationship,
  3698 // since this could be either way
  3699 PEP_STATUS has_partner_contacted_address(PEP_SESSION session, const char* partner_id,
  3700                                          const char* own_address, bool* was_contacted) {            
  3701     
  3702     assert(session);
  3703     assert(!EMPTYSTR(partner_id));
  3704     assert(!EMPTYSTR(own_address));    
  3705     assert(was_contacted);
  3706     
  3707     if (!session || !was_contacted || EMPTYSTR(partner_id) || EMPTYSTR(own_address))
  3708         return PEP_ILLEGAL_VALUE;
  3709     
  3710     *was_contacted = false;
  3711 
  3712     PEP_STATUS status = PEP_STATUS_OK;
  3713     
  3714     sqlite3_reset(session->has_id_contacted_address);
  3715     sqlite3_bind_text(session->has_id_contacted_address, 1, partner_id, -1,
  3716             SQLITE_STATIC);
  3717     int result = sqlite3_step(session->has_id_contacted_address);
  3718     switch (result) {
  3719         case SQLITE_ROW: {
  3720             // yeah yeah, I know, we could be lazy here, but it looks bad.
  3721             *was_contacted = (sqlite3_column_int(session->has_id_contacted_address, 0) != 0);
  3722             status = PEP_STATUS_OK;
  3723             break;
  3724         }
  3725         default:
  3726             status = PEP_UNKNOWN_DB_ERROR;
  3727     }
  3728     sqlite3_reset(session->has_id_contacted_address);
  3729             
  3730     return status;
  3731 }
  3732 
  3733 // FIXME: problematic - can be multiple and this now matters
  3734 PEP_STATUS get_own_ident_for_contact_id(PEP_SESSION session,
  3735                                           const pEp_identity* contact,
  3736                                           pEp_identity** own_ident) {
  3737                                               
  3738     if (!contact || !contact->user_id || !own_ident)
  3739         return PEP_ILLEGAL_VALUE;
  3740         
  3741     char* own_user_id = NULL;
  3742     *own_ident = NULL;
  3743     PEP_STATUS status = get_default_own_userid(session, &own_user_id);
  3744     
  3745     if (status != PEP_STATUS_OK)
  3746         return status;
  3747 
  3748     sqlite3_reset(session->get_own_address_binding_from_contact);
  3749     sqlite3_bind_text(session->get_own_address_binding_from_contact, 1, own_user_id, -1,
  3750             SQLITE_STATIC);
  3751     sqlite3_bind_text(session->get_own_address_binding_from_contact, 2, contact->user_id, -1,
  3752             SQLITE_STATIC);
  3753 
  3754     int result = sqlite3_step(session->get_own_address_binding_from_contact);
  3755     
  3756     const char* own_address = NULL;
  3757     
  3758     switch (result) {
  3759         case SQLITE_ROW:
  3760             own_address = (const char *)
  3761                 sqlite3_column_text(session->get_own_address_binding_from_contact, 0);
  3762             if (own_address) {
  3763                 status = get_identity(session, own_address, own_user_id, own_ident);
  3764                 if (status == PEP_STATUS_OK) {
  3765                     if (!own_ident)
  3766                         status = PEP_CANNOT_FIND_IDENTITY;
  3767                 }
  3768             }
  3769             break;
  3770         default:
  3771             status = PEP_CANNOT_FIND_IDENTITY;
  3772     }
  3773     
  3774     free(own_user_id);
  3775     return status;
  3776 }
  3777 
  3778 PEP_STATUS remove_fpr_as_default(PEP_SESSION session, 
  3779                                  const char* fpr) 
  3780 {
  3781     assert(fpr);
  3782     
  3783     if (!session || !fpr)
  3784         return PEP_ILLEGAL_VALUE;
  3785             
  3786     sqlite3_reset(session->remove_fpr_as_default);
  3787     sqlite3_bind_text(session->remove_fpr_as_default, 1, fpr, -1,
  3788                       SQLITE_STATIC);
  3789 
  3790     int result = sqlite3_step(session->remove_fpr_as_default);
  3791     sqlite3_reset(session->remove_fpr_as_default);
  3792     
  3793     if (result != SQLITE_DONE)
  3794         return PEP_CANNOT_SET_IDENTITY; // misleading - could also be person
  3795 
  3796     return PEP_STATUS_OK;
  3797 }
  3798 
  3799 
  3800 PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
  3801                                  const char* old_fpr, 
  3802                                  const char* new_fpr) 
  3803 {
  3804     assert(old_fpr);
  3805     assert(new_fpr);
  3806     
  3807     if (!old_fpr || !new_fpr)
  3808         return PEP_ILLEGAL_VALUE;
  3809             
  3810     sqlite3_reset(session->replace_identities_fpr);
  3811     sqlite3_bind_text(session->replace_identities_fpr, 1, new_fpr, -1,
  3812                       SQLITE_STATIC);
  3813     sqlite3_bind_text(session->replace_identities_fpr, 2, old_fpr, -1,
  3814                       SQLITE_STATIC);
  3815 
  3816     int result = sqlite3_step(session->replace_identities_fpr);
  3817     sqlite3_reset(session->replace_identities_fpr);
  3818     
  3819     if (result != SQLITE_DONE)
  3820         return PEP_CANNOT_SET_IDENTITY;
  3821 
  3822     return PEP_STATUS_OK;
  3823 }
  3824 
  3825 PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
  3826                                 const char* fpr, 
  3827                                 PEP_comm_type comm_type)
  3828 {
  3829     if (!fpr)
  3830         return PEP_ILLEGAL_VALUE;
  3831         
  3832     sqlite3_reset(session->update_trust_for_fpr);
  3833     sqlite3_bind_int(session->update_trust_for_fpr, 1, comm_type);
  3834     sqlite3_bind_text(session->update_trust_for_fpr, 2, fpr, -1,
  3835             SQLITE_STATIC);
  3836     int result = sqlite3_step(session->update_trust_for_fpr);
  3837     sqlite3_reset(session->update_trust_for_fpr);
  3838     if (result != SQLITE_DONE) {
  3839         return PEP_CANNOT_SET_TRUST;
  3840     }
  3841     
  3842     return PEP_STATUS_OK;
  3843 }
  3844 
  3845 DYNAMIC_API PEP_STATUS set_identity_flags(
  3846         PEP_SESSION session,
  3847         pEp_identity *identity,
  3848         unsigned int flags
  3849     )
  3850 {
  3851     int result;
  3852 
  3853     assert(session);
  3854     assert(identity);
  3855     assert(identity->address);
  3856     assert(identity->user_id);
  3857 
  3858     if (!(session && identity && identity->address && identity->user_id))
  3859         return PEP_ILLEGAL_VALUE;
  3860 
  3861     sqlite3_reset(session->set_identity_flags);
  3862     sqlite3_bind_int(session->set_identity_flags, 1, flags);
  3863     sqlite3_bind_text(session->set_identity_flags, 2, identity->address, -1,
  3864             SQLITE_STATIC);
  3865     sqlite3_bind_text(session->set_identity_flags, 3, identity->user_id, -1,
  3866         SQLITE_STATIC);
  3867         
  3868     result = sqlite3_step(session->set_identity_flags);
  3869 
  3870     sqlite3_reset(session->set_identity_flags);
  3871     if (result != SQLITE_DONE)
  3872         return PEP_CANNOT_SET_IDENTITY;
  3873 
  3874     identity->flags |= flags;
  3875     return PEP_STATUS_OK;
  3876 }
  3877 
  3878 DYNAMIC_API PEP_STATUS unset_identity_flags(
  3879         PEP_SESSION session,
  3880         pEp_identity *identity,
  3881         unsigned int flags
  3882     )
  3883 {
  3884     int result;
  3885 
  3886     assert(session);
  3887     assert(identity);
  3888     assert(identity->address);
  3889     assert(identity->user_id);
  3890 
  3891     if (!(session && identity && identity->address && identity->user_id))
  3892         return PEP_ILLEGAL_VALUE;
  3893 
  3894     sqlite3_reset(session->unset_identity_flags);
  3895     sqlite3_bind_int(session->unset_identity_flags, 1, flags);
  3896     sqlite3_bind_text(session->unset_identity_flags, 2, identity->address, -1,
  3897             SQLITE_STATIC);
  3898     sqlite3_bind_text(session->unset_identity_flags, 3, identity->user_id, -1,
  3899             SQLITE_STATIC);
  3900     result = sqlite3_step(session->unset_identity_flags);
  3901     sqlite3_reset(session->unset_identity_flags);
  3902     if (result != SQLITE_DONE)
  3903         return PEP_CANNOT_SET_IDENTITY;
  3904 
  3905     identity->flags &= ~flags;
  3906 
  3907     return PEP_STATUS_OK;
  3908 }
  3909 
  3910 PEP_STATUS get_trust_by_userid(PEP_SESSION session, const char* user_id,
  3911                                            labeled_int_list_t** trust_list)
  3912 {
  3913     int result;
  3914 
  3915     if (!(session && user_id && user_id[0]))
  3916         return PEP_ILLEGAL_VALUE;
  3917 
  3918     *trust_list = NULL;
  3919     labeled_int_list_t* t_list = NULL;
  3920 
  3921     sqlite3_reset(session->get_trust_by_userid);
  3922     sqlite3_bind_text(session->get_trust_by_userid, 1, user_id, -1, SQLITE_STATIC);
  3923 
  3924     while ((result = sqlite3_step(session->get_trust_by_userid)) == SQLITE_ROW) {
  3925         if (!t_list)
  3926             t_list = new_labeled_int_list(sqlite3_column_int(session->get_trust_by_userid, 1),
  3927                                          (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
  3928         else
  3929             labeled_int_list_add(t_list, sqlite3_column_int(session->get_trust_by_userid, 1),
  3930                                 (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
  3931     }
  3932 
  3933     sqlite3_reset(session->get_trust_by_userid);
  3934 
  3935     *trust_list = t_list;
  3936         
  3937     return PEP_STATUS_OK;
  3938 }
  3939 
  3940 PEP_comm_type reconcile_trust(PEP_comm_type t_old, PEP_comm_type t_new) {
  3941     switch (t_new) {
  3942         case PEP_ct_mistrusted:
  3943         case PEP_ct_key_revoked:
  3944         case PEP_ct_compromised:
  3945         case PEP_ct_key_b0rken:
  3946             return t_new;
  3947         default:
  3948             break;
  3949     }
  3950     switch (t_old) {
  3951         case PEP_ct_mistrusted:
  3952         case PEP_ct_key_revoked:
  3953         case PEP_ct_compromised:
  3954         case PEP_ct_key_b0rken:
  3955             return t_old;
  3956         default:
  3957             break;
  3958     }
  3959     if (t_old < PEP_ct_strong_but_unconfirmed && t_new >= PEP_ct_strong_but_unconfirmed)
  3960         return t_new;
  3961     
  3962     bool confirmed = (t_old & PEP_ct_confirmed) || (t_new & PEP_ct_confirmed);
  3963     PEP_comm_type result = _MAX(t_old, t_new);
  3964     if (confirmed)
  3965         result |= PEP_ct_confirmed;
  3966     return result;
  3967 }
  3968 
  3969 PEP_STATUS reconcile_pEp_status(PEP_SESSION session, const char* old_uid, 
  3970                                 const char* new_uid) {
  3971     PEP_STATUS status = PEP_STATUS_OK;
  3972     // We'll make this easy - if the old one has a pEp status, we set no matter
  3973     // what.
  3974     pEp_identity* ident = new_identity(NULL, NULL, old_uid, NULL);
  3975     bool is_pEp_peep = false;
  3976     status = is_pEp_user(session, ident, &is_pEp_peep);
  3977     if (is_pEp_peep) {
  3978         free(ident->user_id);
  3979         ident->user_id = strdup(new_uid);
  3980         if (!ident->user_id) {
  3981             status = PEP_OUT_OF_MEMORY;
  3982             goto pEp_free;
  3983         }
  3984         status = set_as_pEp_user(session, ident);
  3985     }
  3986 pEp_free:
  3987     free_identity(ident);
  3988     return status;
  3989 }
  3990 
  3991 const char* reconcile_usernames(const char* old_name, const char* new_name, 
  3992                                 const char* address) {
  3993     if (EMPTYSTR(old_name)) {
  3994         if (EMPTYSTR(new_name))
  3995             return address;
  3996         else
  3997             return new_name;
  3998     }
  3999     if (EMPTYSTR(new_name))
  4000         return old_name;        
  4001     if (strcmp(new_name, address) == 0)
  4002         return old_name;
  4003     return new_name;        
  4004 }
  4005 
  4006 PEP_STATUS reconcile_default_keys(PEP_SESSION session, pEp_identity* old_ident,
  4007                                   pEp_identity* new_ident) {
  4008     PEP_STATUS status = PEP_STATUS_OK;
  4009                                       
  4010     const char* old_fpr = old_ident->fpr;
  4011     const char* new_fpr = new_ident->fpr;
  4012     if (!old_fpr)
  4013         return status;
  4014 
  4015     PEP_comm_type old_ct = old_ident->comm_type;    
  4016     PEP_comm_type new_ct = new_ident->comm_type;
  4017     
  4018     if (!new_fpr) {
  4019         new_ident->fpr = strdup(old_fpr);
  4020         if (!new_ident->fpr)
  4021             status = PEP_OUT_OF_MEMORY;
  4022         else    
  4023             new_ident->comm_type = old_ct;
  4024         return status;
  4025     }        
  4026     
  4027     if (strcmp(old_fpr, new_fpr) == 0) {
  4028         new_ident->comm_type = reconcile_trust(old_ct, new_ct);
  4029         return status;
  4030     }
  4031     
  4032     bool old_confirmed = old_ct & PEP_ct_confirmed;
  4033     bool new_confirmed = new_ct & PEP_ct_confirmed;
  4034     
  4035     if (new_confirmed)
  4036         return status;
  4037     else if (old_confirmed) {
  4038         free(new_ident->fpr);
  4039         new_ident->fpr = strdup(old_fpr);
  4040         if (!new_ident->fpr)
  4041             status = PEP_OUT_OF_MEMORY;
  4042         else    
  4043             new_ident->comm_type = old_ct;
  4044         return status;
  4045     }
  4046     
  4047     if (old_ct > new_ct) {
  4048         free(new_ident->fpr);
  4049         new_ident->fpr = strdup(old_fpr);
  4050         if (!new_ident->fpr)
  4051             status = PEP_OUT_OF_MEMORY;
  4052         else    
  4053             new_ident->comm_type = old_ct;
  4054     }
  4055     return status;
  4056 }
  4057 
  4058 void reconcile_language(pEp_identity* old_ident,
  4059                         pEp_identity* new_ident) {
  4060     if (new_ident->lang[0] == 0) {
  4061         if (old_ident->lang[0] != 0) {
  4062             new_ident->lang[0] = old_ident->lang[0];
  4063             new_ident->lang[1] = old_ident->lang[1];
  4064             new_ident->lang[2] = old_ident->lang[2];
  4065         }
  4066     }
  4067 }
  4068 
  4069 // ONLY CALL THIS IF BOTH IDs ARE IN THE PERSON DB, FOOL! </Mr_T>
  4070 PEP_STATUS merge_records(PEP_SESSION session, const char* old_uid,
  4071                          const char* new_uid) {
  4072     PEP_STATUS status = PEP_STATUS_OK;
  4073     
  4074     pEp_identity* new_ident = NULL;
  4075     identity_list* old_identities = NULL;
  4076     labeled_int_list_t* trust_list = NULL;
  4077     stringlist_t* touched_keys = new_stringlist(NULL);
  4078     char* main_user_fpr = NULL;
  4079 
  4080     status = reconcile_pEp_status(session, old_uid, new_uid);
  4081     if (status != PEP_STATUS_OK)
  4082         goto pEp_free;
  4083                         
  4084     bool new_is_pEp = false;
  4085     new_ident = new_identity(NULL, NULL, new_uid, NULL);
  4086     status = is_pEp_user(session, new_ident, &new_is_pEp);
  4087     if (status != PEP_STATUS_OK)
  4088         goto pEp_free;
  4089     free(new_ident);
  4090     new_ident = NULL;
  4091         
  4092     status = get_identities_by_userid(session, old_uid, &old_identities);
  4093     if (status == PEP_STATUS_OK && old_identities) {
  4094         identity_list* curr_old = old_identities;
  4095         for (; curr_old && curr_old->ident; curr_old = curr_old->next) {
  4096             pEp_identity* old_ident = curr_old->ident;
  4097             const char* address = old_ident->address;
  4098             status = get_identity(session, address, new_uid, &new_ident);
  4099             if (status == PEP_CANNOT_FIND_IDENTITY) {
  4100                 // No new identity matching the old one, so we just set one w. new user_id
  4101                 free(old_ident->user_id);
  4102                 old_ident->user_id = strdup(new_uid);
  4103                 if (!old_ident->user_id) {
  4104                     status = PEP_OUT_OF_MEMORY;
  4105                     goto pEp_free;
  4106                 }
  4107                 if (new_is_pEp) {
  4108                     PEP_comm_type confirmed_bit = old_ident->comm_type & PEP_ct_confirmed;
  4109                     if ((old_ident->comm_type | PEP_ct_confirmed) == PEP_ct_OpenPGP)
  4110                         old_ident->comm_type = PEP_ct_pEp_unconfirmed | confirmed_bit;
  4111                 }
  4112                 
  4113                 status = set_identity(session, old_ident);
  4114                 if (status != PEP_STATUS_OK)
  4115                     goto pEp_free;
  4116             }
  4117             else if (status != PEP_STATUS_OK)
  4118                 goto pEp_free;
  4119             else {
  4120                 // Ok, so we have two idents which might be in conflict. Have to merge them.
  4121                 const char* username = reconcile_usernames(old_ident->username,
  4122                                                            new_ident->username,
  4123                                                            address);
  4124                                                            
  4125                 if (!new_ident->username || strcmp(username, new_ident->username) != 0) {
  4126                     free(new_ident->username);
  4127                     new_ident->username = strdup(username);
  4128                     if (!new_ident->username) {
  4129                         status = PEP_OUT_OF_MEMORY;
  4130                         goto pEp_free;
  4131                     }
  4132                 }
  4133         
  4134                 // Reconcile default keys if they differ, trust if they don't
  4135                 status = reconcile_default_keys(session, old_ident, new_ident);
  4136                 if (status != PEP_STATUS_OK)
  4137                     goto pEp_free;
  4138                     
  4139                 // reconcile languages
  4140                 reconcile_language(old_ident, new_ident);
  4141 
  4142                 // reconcile flags - FIXME - is this right?
  4143                 new_ident->flags |= old_ident->flags;
  4144                 
  4145                 // NOTE: In principle, this is only called from update_identity,
  4146                 // which would never have me flags set. So I am ignoring them here.
  4147                 // if this function is ever USED for that, though, you'll have
  4148                 // to go through making sure that the user ids are appropriately
  4149                 // aliased, etc. So be careful.
  4150                 
  4151                 // Set the reconciled record
  4152                     
  4153                 status = set_identity(session, new_ident);
  4154                 if (status != PEP_STATUS_OK)
  4155                     goto pEp_free;
  4156 
  4157                 if (new_ident->fpr)
  4158                     stringlist_add(touched_keys, new_ident->fpr);
  4159                         
  4160                 free_identity(new_ident);
  4161                 new_ident = NULL;    
  4162             }
  4163         }
  4164     }
  4165     // otherwise, no need to reconcile identity records. But maybe trust...    
  4166     new_ident = new_identity(NULL, NULL, new_uid, NULL);
  4167     if (!new_ident) {
  4168         status = PEP_OUT_OF_MEMORY;
  4169         goto pEp_free;
  4170     }
  4171     status = get_trust_by_userid(session, old_uid, &trust_list);
  4172 
  4173     labeled_int_list_t* trust_curr = trust_list;
  4174     for (; trust_curr && trust_curr->label; trust_curr = trust_curr->next) {
  4175         const char* curr_fpr = trust_curr->label;
  4176         new_ident->fpr = strdup(curr_fpr); 
  4177         status = get_trust(session, new_ident);
  4178         switch (status) {
  4179             case PEP_STATUS_OK:
  4180                 new_ident->comm_type = reconcile_trust(trust_curr->value,
  4181                                                        new_ident->comm_type);
  4182                 break;
  4183             case PEP_CANNOT_FIND_IDENTITY:
  4184                 new_ident->comm_type = trust_curr->value;
  4185                 break;
  4186             default:
  4187                 goto pEp_free;
  4188         }
  4189         new_ident->comm_type = reconcile_trust(trust_curr->value,
  4190                                                new_ident->comm_type);
  4191         if (new_is_pEp) {
  4192             PEP_comm_type confirmed_bit = new_ident->comm_type & PEP_ct_confirmed;
  4193             if ((new_ident->comm_type | PEP_ct_confirmed) == PEP_ct_OpenPGP)
  4194                 new_ident->comm_type = PEP_ct_pEp_unconfirmed | confirmed_bit;
  4195         }
  4196 
  4197         status = set_trust(session, new_ident);
  4198         if (status != PEP_STATUS_OK) {
  4199             goto pEp_free;
  4200         }                  
  4201                               
  4202         free(new_ident->fpr);
  4203         new_ident->fpr = NULL;
  4204         new_ident->comm_type = 0;
  4205     }
  4206 
  4207     // reconcile the default keys if the new id doesn't have one?
  4208     status = get_main_user_fpr(session, new_uid, &main_user_fpr);
  4209     if (status == PEP_KEY_NOT_FOUND || (status == PEP_STATUS_OK && !main_user_fpr)) {
  4210         status = get_main_user_fpr(session, old_uid, &main_user_fpr);
  4211         if (status == PEP_STATUS_OK && main_user_fpr)
  4212             status = replace_main_user_fpr(session, new_uid, main_user_fpr);
  4213         if (status != PEP_STATUS_OK)
  4214             goto pEp_free;
  4215     }
  4216     
  4217     // delete the old user
  4218     status = delete_person(session, old_uid);
  4219     
  4220 pEp_free:
  4221     free_identity(new_ident);
  4222     free_identity_list(old_identities);
  4223     free_labeled_int_list(trust_list);
  4224     free_stringlist(touched_keys);
  4225     free(main_user_fpr);
  4226     return status;
  4227 }
  4228 
  4229 PEP_STATUS replace_userid(PEP_SESSION session, const char* old_uid,
  4230                           const char* new_uid) {
  4231     assert(session);
  4232     assert(old_uid);
  4233     assert(new_uid);
  4234     
  4235     if (!session || !old_uid || !new_uid)
  4236         return PEP_ILLEGAL_VALUE;
  4237 
  4238     pEp_identity* temp_ident = new_identity(NULL, NULL, new_uid, NULL);
  4239     bool new_exists = false;
  4240     PEP_STATUS status = exists_person(session, temp_ident, &new_exists);
  4241     free_identity(temp_ident);
  4242     if (status != PEP_STATUS_OK) // DB error
  4243         return status;
  4244         
  4245     if (new_exists)
  4246         return merge_records(session, old_uid, new_uid);
  4247 
  4248     int result;
  4249 
  4250     sqlite3_reset(session->replace_userid);
  4251     sqlite3_bind_text(session->replace_userid, 1, new_uid, -1,
  4252             SQLITE_STATIC);
  4253     sqlite3_bind_text(session->replace_userid, 2, old_uid, -1,
  4254             SQLITE_STATIC);
  4255     result = sqlite3_step(session->replace_userid);
  4256 #ifndef NDEBUG
  4257     if (result) {
  4258         const char *errmsg = sqlite3_errmsg(session->db);
  4259         log_event(session, "SQLite3 error", "replace_userid", errmsg, NULL);
  4260     }
  4261 #endif // !NDEBUG
  4262     sqlite3_reset(session->replace_userid);
  4263     if (result != SQLITE_DONE)
  4264         return PEP_CANNOT_SET_PERSON; // May need clearer retval
  4265 
  4266     return PEP_STATUS_OK;
  4267 }
  4268 
  4269 PEP_STATUS remove_key(PEP_SESSION session, const char* fpr) {
  4270     assert(session);
  4271     assert(fpr);
  4272     
  4273     if (!session || EMPTYSTR(fpr))
  4274         return PEP_ILLEGAL_VALUE;
  4275 
  4276     int result;
  4277 
  4278     sqlite3_reset(session->delete_key);
  4279     sqlite3_bind_text(session->delete_key, 1, fpr, -1,
  4280             SQLITE_STATIC);
  4281     result = sqlite3_step(session->delete_key);
  4282     sqlite3_reset(session->delete_key);
  4283     if (result != SQLITE_DONE)
  4284         return PEP_CANNOT_SET_PGP_KEYPAIR;
  4285 
  4286     return PEP_STATUS_OK;
  4287 }
  4288 
  4289 
  4290 PEP_STATUS refresh_userid_default_key(PEP_SESSION session, const char* user_id) {
  4291     assert(session);
  4292     assert(user_id);
  4293     
  4294     if (!session || !user_id)
  4295         return PEP_ILLEGAL_VALUE;
  4296 
  4297     int result;
  4298 
  4299     sqlite3_reset(session->refresh_userid_default_key);
  4300     sqlite3_bind_text(session->refresh_userid_default_key, 1, user_id, -1,
  4301             SQLITE_STATIC);
  4302     result = sqlite3_step(session->refresh_userid_default_key);
  4303     sqlite3_reset(session->refresh_userid_default_key);
  4304     if (result != SQLITE_DONE)
  4305         return PEP_CANNOT_SET_PERSON;
  4306 
  4307     return PEP_STATUS_OK;    
  4308 }
  4309 
  4310 PEP_STATUS replace_main_user_fpr(PEP_SESSION session, const char* user_id,
  4311                                  const char* new_fpr) {
  4312     assert(session);
  4313     assert(user_id);
  4314     assert(new_fpr);
  4315     
  4316     if (!session || !user_id || !new_fpr)
  4317         return PEP_ILLEGAL_VALUE;
  4318 
  4319     int result;
  4320 
  4321     sqlite3_reset(session->replace_main_user_fpr);
  4322     sqlite3_bind_text(session->replace_main_user_fpr, 1, new_fpr, -1,
  4323             SQLITE_STATIC);
  4324     sqlite3_bind_text(session->replace_main_user_fpr, 2, user_id, -1,
  4325             SQLITE_STATIC);
  4326     result = sqlite3_step(session->replace_main_user_fpr);
  4327     sqlite3_reset(session->replace_main_user_fpr);
  4328     if (result != SQLITE_DONE)
  4329         return PEP_CANNOT_SET_PERSON;
  4330 
  4331     return PEP_STATUS_OK;
  4332 }
  4333 
  4334 PEP_STATUS get_main_user_fpr(PEP_SESSION session, 
  4335                              const char* user_id,
  4336                              char** main_fpr)
  4337 {
  4338     PEP_STATUS status = PEP_STATUS_OK;
  4339     int result;
  4340     
  4341     assert(session);
  4342     assert(user_id);
  4343     assert(main_fpr);
  4344     
  4345     if (!(session && user_id && user_id[0] && main_fpr))
  4346         return PEP_ILLEGAL_VALUE;
  4347         
  4348     *main_fpr = NULL;
  4349     
  4350     sqlite3_reset(session->get_main_user_fpr);
  4351     sqlite3_bind_text(session->get_main_user_fpr, 1, user_id, -1,
  4352                       SQLITE_STATIC);
  4353     result = sqlite3_step(session->get_main_user_fpr);
  4354     switch (result) {
  4355     case SQLITE_ROW: {
  4356         const char* _fpr = 
  4357             (const char *) sqlite3_column_text(session->get_main_user_fpr, 0);
  4358         if (_fpr) {
  4359             *main_fpr = strdup(_fpr);
  4360             if (!(*main_fpr))
  4361                 status = PEP_OUT_OF_MEMORY;
  4362         }
  4363         else {
  4364             status = PEP_KEY_NOT_FOUND;
  4365         }
  4366         break;
  4367     }
  4368     default:
  4369         status = PEP_CANNOT_FIND_PERSON;
  4370     }
  4371 
  4372     sqlite3_reset(session->get_main_user_fpr);
  4373     return status;
  4374 }
  4375 
  4376 // Deprecated
  4377 DYNAMIC_API PEP_STATUS mark_as_compromized(
  4378         PEP_SESSION session,
  4379         const char *fpr
  4380     )
  4381 {
  4382     return mark_as_compromised(session, fpr);
  4383 }
  4384 
  4385 DYNAMIC_API PEP_STATUS mark_as_compromised(
  4386         PEP_SESSION session,
  4387         const char *fpr
  4388     )
  4389 {
  4390     int result;
  4391 
  4392     assert(session);
  4393     assert(fpr && fpr[0]);
  4394 
  4395     if (!(session && fpr && fpr[0]))
  4396         return PEP_ILLEGAL_VALUE;
  4397 
  4398     sqlite3_reset(session->mark_compromised);
  4399     sqlite3_bind_text(session->mark_compromised, 1, fpr, -1,
  4400             SQLITE_STATIC);
  4401     result = sqlite3_step(session->mark_compromised);
  4402     sqlite3_reset(session->mark_compromised);
  4403 
  4404     if (result != SQLITE_DONE)
  4405         return PEP_CANNOT_SET_TRUST;
  4406 
  4407     return PEP_STATUS_OK;
  4408 }
  4409 
  4410 void pEp_free(void *p)
  4411 {
  4412     free(p);
  4413 }
  4414 
  4415 
  4416 DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
  4417 {
  4418     PEP_STATUS status = PEP_STATUS_OK;
  4419     int result;
  4420 
  4421     // We need to be able to test that we break correctly without shutting
  4422     // asserts off everywhere.
  4423     // assert(session);
  4424     // assert(identity);
  4425     // assert(identity->user_id);
  4426     // assert(identity->user_id[0]);
  4427     // assert(identity->fpr);
  4428     // assert(identity->fpr[0]);
  4429 
  4430     if (!(session && identity && identity->user_id && identity->user_id[0] &&
  4431                 identity->fpr && identity->fpr[0]))
  4432         return PEP_ILLEGAL_VALUE;
  4433 
  4434     identity->comm_type = PEP_ct_unknown;
  4435     sqlite3_reset(session->get_trust);
  4436 
  4437     sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
  4438             SQLITE_STATIC);
  4439     sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
  4440 
  4441     result = sqlite3_step(session->get_trust);
  4442     switch (result) {
  4443     case SQLITE_ROW: {
  4444         int comm_type = (PEP_comm_type) sqlite3_column_int(session->get_trust,
  4445                 0);
  4446         identity->comm_type = comm_type;
  4447         break;
  4448     }
  4449  
  4450     default:
  4451         status = PEP_CANNOT_FIND_IDENTITY;
  4452     }
  4453 
  4454     sqlite3_reset(session->get_trust);
  4455     return status;
  4456 }
  4457 
  4458 
  4459 DYNAMIC_API PEP_STATUS least_trust(
  4460         PEP_SESSION session,
  4461         const char *fpr,
  4462         PEP_comm_type *comm_type
  4463     )
  4464 {
  4465     PEP_STATUS status = PEP_STATUS_OK;
  4466     int result;
  4467 
  4468     assert(session);
  4469     assert(fpr);
  4470     assert(comm_type);
  4471 
  4472     if (!(session && fpr && comm_type))
  4473         return PEP_ILLEGAL_VALUE;
  4474 
  4475     *comm_type = PEP_ct_unknown;
  4476 
  4477     sqlite3_reset(session->least_trust);
  4478     sqlite3_bind_text(session->least_trust, 1, fpr, -1, SQLITE_STATIC);
  4479 
  4480     result = sqlite3_step(session->least_trust);
  4481     switch (result) {
  4482         case SQLITE_ROW: {
  4483             int _comm_type = sqlite3_column_int(session->least_trust, 0);
  4484             *comm_type = (PEP_comm_type) _comm_type;
  4485             break;
  4486         }
  4487         default:
  4488             // never reached because of sql min()
  4489             status = PEP_CANNOT_FIND_IDENTITY;
  4490     }
  4491 
  4492     sqlite3_reset(session->least_trust);
  4493     return status;
  4494 }
  4495 
  4496 DYNAMIC_API PEP_STATUS decrypt_and_verify(
  4497     PEP_SESSION session, const char *ctext, size_t csize,
  4498     const char *dsigtext, size_t dsigsize,
  4499     char **ptext, size_t *psize, stringlist_t **keylist,
  4500     char** filename_ptr
  4501     )
  4502 {
  4503     assert(session);
  4504     assert(ctext);
  4505     assert(csize);
  4506     assert(ptext);
  4507     assert(psize);
  4508     assert(keylist);
  4509 
  4510     if (!(session && ctext && csize && ptext && psize && keylist))
  4511         return PEP_ILLEGAL_VALUE;
  4512 
  4513     PEP_STATUS status = session->cryptotech[PEP_crypt_OpenPGP].decrypt_and_verify(
  4514             session, ctext, csize, dsigtext, dsigsize, ptext, psize, keylist,
  4515             filename_ptr);
  4516 
  4517     if (status == PEP_DECRYPT_NO_KEY)
  4518         signal_Sync_event(session, Sync_PR_keysync, CannotDecrypt, NULL);
  4519 
  4520     return status;
  4521 }
  4522 
  4523 DYNAMIC_API PEP_STATUS encrypt_and_sign(
  4524     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  4525     size_t psize, char **ctext, size_t *csize
  4526     )
  4527 {
  4528     assert(session);
  4529     assert(keylist);
  4530     assert(ptext);
  4531     assert(psize);
  4532     assert(ctext);
  4533     assert(csize);
  4534 
  4535     if (!(session && keylist && ptext && psize && ctext && csize))
  4536         return PEP_ILLEGAL_VALUE;
  4537 
  4538     return session->cryptotech[PEP_crypt_OpenPGP].encrypt_and_sign(session,
  4539             keylist, ptext, psize, ctext, csize);
  4540 }
  4541 
  4542 PEP_STATUS encrypt_only(
  4543     PEP_SESSION session, const stringlist_t *keylist, const char *ptext,
  4544     size_t psize, char **ctext, size_t *csize
  4545     )
  4546 {
  4547     assert(session);
  4548     assert(keylist);
  4549     assert(ptext);
  4550     assert(psize);
  4551     assert(ctext);
  4552     assert(csize);
  4553 
  4554     if (!(session && keylist && ptext && psize && ctext && csize))
  4555         return PEP_ILLEGAL_VALUE;
  4556 
  4557     return session->cryptotech[PEP_crypt_OpenPGP].encrypt_only(session,
  4558             keylist, ptext, psize, ctext, csize);
  4559 }
  4560 
  4561 PEP_STATUS sign_only(PEP_SESSION session, 
  4562                      const char *data, 
  4563                      size_t data_size, 
  4564                      const char *fpr, 
  4565                      char **sign, 
  4566                      size_t *sign_size) {
  4567     assert(session);
  4568     assert(fpr);
  4569     assert(data);
  4570     assert(data_size);
  4571     assert(sign);
  4572     assert(sign_size);
  4573 
  4574     if (!(session && fpr && data && data_size && sign && sign_size))
  4575         return PEP_ILLEGAL_VALUE;
  4576 
  4577     return session->cryptotech[PEP_crypt_OpenPGP].sign_only(session,
  4578                                 fpr, data, data_size, sign, sign_size);
  4579                          
  4580 }
  4581 
  4582 
  4583 
  4584 DYNAMIC_API PEP_STATUS verify_text(
  4585     PEP_SESSION session, const char *text, size_t size,
  4586     const char *signature, size_t sig_size, stringlist_t **keylist
  4587     )
  4588 {
  4589     assert(session);
  4590     assert(text);
  4591     assert(size);
  4592     assert(signature);
  4593     assert(sig_size);
  4594     assert(keylist);
  4595 
  4596     if (!(session && text && size && signature && sig_size && keylist))
  4597         return PEP_ILLEGAL_VALUE;
  4598 
  4599     return session->cryptotech[PEP_crypt_OpenPGP].verify_text(session, text,
  4600             size, signature, sig_size, keylist);
  4601 }
  4602 
  4603 DYNAMIC_API PEP_STATUS delete_keypair(PEP_SESSION session, const char *fpr)
  4604 {
  4605     assert(session);
  4606     assert(fpr);
  4607 
  4608     if (!(session && fpr))
  4609         return PEP_ILLEGAL_VALUE;
  4610 
  4611     return session->cryptotech[PEP_crypt_OpenPGP].delete_keypair(session, fpr);
  4612 }
  4613 
  4614 DYNAMIC_API PEP_STATUS export_key(
  4615         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  4616     )
  4617 {
  4618     assert(session);
  4619     assert(fpr);
  4620     assert(key_data);
  4621     assert(size);
  4622 
  4623     if (!(session && fpr && key_data && size))
  4624         return PEP_ILLEGAL_VALUE;
  4625 
  4626     return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
  4627             key_data, size, false);
  4628 }
  4629 
  4630 DYNAMIC_API PEP_STATUS export_secret_key(
  4631         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  4632     )
  4633 {
  4634     assert(session);
  4635     assert(fpr);
  4636     assert(key_data);
  4637     assert(size);
  4638 
  4639     if (!(session && fpr && key_data && size))
  4640         return PEP_ILLEGAL_VALUE;
  4641 
  4642     // don't accept key IDs but full fingerprints only
  4643     if (strlen(fpr) < 16)
  4644         return PEP_ILLEGAL_VALUE;
  4645 
  4646     return session->cryptotech[PEP_crypt_OpenPGP].export_key(session, fpr,
  4647             key_data, size, true);
  4648 }
  4649 
  4650 // Deprecated
  4651 DYNAMIC_API PEP_STATUS export_secrect_key(
  4652         PEP_SESSION session, const char *fpr, char **key_data, size_t *size
  4653     )
  4654 {
  4655     return export_secret_key(session, fpr, key_data, size);
  4656 }
  4657 
  4658 DYNAMIC_API PEP_STATUS find_keys(
  4659         PEP_SESSION session, const char *pattern, stringlist_t **keylist
  4660     )
  4661 {
  4662     assert(session);
  4663     assert(pattern);
  4664     assert(keylist);
  4665 
  4666     if (!(session && pattern && keylist))
  4667         return PEP_ILLEGAL_VALUE;
  4668 
  4669     return session->cryptotech[PEP_crypt_OpenPGP].find_keys(session, pattern,
  4670             keylist);
  4671 }
  4672 
  4673 
  4674 DYNAMIC_API PEP_STATUS generate_keypair(
  4675         PEP_SESSION session, pEp_identity *identity
  4676     )
  4677 {
  4678     assert(session);
  4679     assert(identity);
  4680     assert(identity->address);
  4681     assert(identity->fpr == NULL || identity->fpr[0] == 0);
  4682     assert(identity->username);
  4683 
  4684     if (!(session && identity && identity->address &&
  4685             (identity->fpr == NULL || identity->fpr[0] == 0) &&
  4686             identity->username))
  4687         return PEP_ILLEGAL_VALUE;
  4688 
  4689     char* saved_username = NULL;
  4690     char* at = NULL;
  4691     size_t uname_len = strlen(identity->username);
  4692     
  4693     if (uname_len > 0)
  4694         at = strstr(identity->username, "@"); 
  4695     
  4696     if (at) {
  4697         saved_username = identity->username;
  4698         identity->username = calloc(uname_len + 3, 1);
  4699         if (!identity->username) {
  4700             identity->username = saved_username;
  4701             return PEP_OUT_OF_MEMORY;
  4702         }
  4703         identity->username[0] = '"';
  4704         strlcpy((identity->username) + 1, saved_username, uname_len + 1);
  4705         identity->username[uname_len + 1] = '"';        
  4706     }
  4707 
  4708     PEP_STATUS status =
  4709         session->cryptotech[PEP_crypt_OpenPGP].generate_keypair(session,
  4710                 identity);
  4711                 
  4712     if (saved_username) {
  4713         free(identity->username);
  4714         identity->username = saved_username;
  4715     }            
  4716     if (status != PEP_STATUS_OK)
  4717         return status;
  4718 
  4719     if (identity->fpr)
  4720         status = set_pgp_keypair(session, identity->fpr);
  4721 
  4722     signal_Sync_event(session, Sync_PR_keysync, KeyGen, NULL);
  4723 
  4724     // add to known keypair DB, as this might not end up being a default
  4725     return status;
  4726 }
  4727 
  4728 DYNAMIC_API PEP_STATUS get_key_rating(
  4729         PEP_SESSION session,
  4730         const char *fpr,
  4731         PEP_comm_type *comm_type
  4732     )
  4733 {
  4734     assert(session);
  4735     assert(fpr);
  4736     assert(comm_type);
  4737 
  4738     if (!(session && fpr && comm_type))
  4739         return PEP_ILLEGAL_VALUE;
  4740 
  4741     return session->cryptotech[PEP_crypt_OpenPGP].get_key_rating(session, fpr,
  4742             comm_type);
  4743 }
  4744 
  4745 DYNAMIC_API PEP_STATUS import_key(
  4746         PEP_SESSION session,
  4747         const char *key_data,
  4748         size_t size,
  4749         identity_list **private_keys
  4750     )
  4751 {
  4752     assert(session);
  4753     assert(key_data);
  4754 
  4755     if (!(session && key_data))
  4756         return PEP_ILLEGAL_VALUE;
  4757 
  4758     return session->cryptotech[PEP_crypt_OpenPGP].import_key(session, key_data,
  4759             size, private_keys);
  4760 }
  4761 
  4762 DYNAMIC_API PEP_STATUS recv_key(PEP_SESSION session, const char *pattern)
  4763 {
  4764     assert(session);
  4765     assert(pattern);
  4766 
  4767     if (!(session && pattern))
  4768         return PEP_ILLEGAL_VALUE;
  4769 
  4770     return session->cryptotech[PEP_crypt_OpenPGP].recv_key(session, pattern);
  4771 }
  4772 
  4773 DYNAMIC_API PEP_STATUS send_key(PEP_SESSION session, const char *pattern)
  4774 {
  4775     assert(session);
  4776     assert(pattern);
  4777 
  4778     if (!(session && pattern))
  4779         return PEP_ILLEGAL_VALUE;
  4780 
  4781     return session->cryptotech[PEP_crypt_OpenPGP].send_key(session, pattern);
  4782 }
  4783 
  4784 DYNAMIC_API PEP_STATUS renew_key(
  4785         PEP_SESSION session,
  4786         const char *fpr,
  4787         const timestamp *ts
  4788     )
  4789 {
  4790     assert(session);
  4791     assert(fpr);
  4792 
  4793     if (!(session && fpr))
  4794         return PEP_ILLEGAL_VALUE;
  4795 
  4796     return session->cryptotech[PEP_crypt_OpenPGP].renew_key(session, fpr, ts);
  4797 }
  4798 
  4799 DYNAMIC_API PEP_STATUS revoke_key(
  4800         PEP_SESSION session,
  4801         const char *fpr,
  4802         const char *reason
  4803     )
  4804 {
  4805     assert(session);
  4806     assert(fpr);
  4807 
  4808     if (!(session && fpr))
  4809         return PEP_ILLEGAL_VALUE;
  4810 
  4811     // Check to see first if it is revoked
  4812     bool revoked = false;
  4813     PEP_STATUS status = key_revoked(session, fpr, &revoked);
  4814     if (status != PEP_STATUS_OK)
  4815         return status;
  4816         
  4817     if (revoked)
  4818         return PEP_STATUS_OK;
  4819 
  4820     return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
  4821             reason);
  4822 }
  4823 
  4824 DYNAMIC_API PEP_STATUS key_expired(
  4825         PEP_SESSION session,
  4826         const char *fpr,
  4827         const time_t when,
  4828         bool *expired
  4829     )
  4830 {
  4831     assert(session);
  4832     assert(fpr);
  4833     assert(expired);
  4834 
  4835     if (!(session && fpr && expired))
  4836         return PEP_ILLEGAL_VALUE;
  4837 
  4838     return session->cryptotech[PEP_crypt_OpenPGP].key_expired(session, fpr,
  4839             when, expired);
  4840 }
  4841 
  4842 DYNAMIC_API PEP_STATUS key_revoked(
  4843        PEP_SESSION session,
  4844        const char *fpr,
  4845        bool *revoked
  4846    )
  4847 {
  4848     assert(session);
  4849     assert(fpr);
  4850     assert(revoked);
  4851     
  4852     if (!(session && fpr && revoked))
  4853         return PEP_ILLEGAL_VALUE;
  4854     
  4855     return session->cryptotech[PEP_crypt_OpenPGP].key_revoked(session, fpr,
  4856             revoked);
  4857 }
  4858 
  4859 DYNAMIC_API PEP_STATUS config_cipher_suite(PEP_SESSION session,
  4860         PEP_CIPHER_SUITE suite)
  4861 {
  4862     assert(session);
  4863     if (!session)
  4864         return PEP_ILLEGAL_VALUE;
  4865 
  4866     return session->cryptotech[PEP_crypt_OpenPGP].config_cipher_suite(session, suite);
  4867 }
  4868 
  4869 static void _clean_log_value(char *text)
  4870 {
  4871     if (text) {
  4872         for (char *c = text; *c; c++) {
  4873             if (*c < 32 && *c != '\n')
  4874                 *c = 32;
  4875             else if (*c == '"')
  4876                 *c = '\'';
  4877         }
  4878     }
  4879 }
  4880 
  4881 static char *_concat_string(char *str1, const char *str2, char delim)
  4882 {
  4883     str2 = str2 ? str2 : "";
  4884     size_t len1 = str1 ? strlen(str1) : 0;
  4885     size_t len2 = strlen(str2);
  4886     size_t len = len1 + len2 + 3;
  4887     char * result = realloc(str1, len + 1);
  4888 
  4889     if (result) {
  4890         result[len1] = '"';
  4891         strcpy(result + len1 + 1, str2);
  4892         result[len - 2] = '"';
  4893         result[len - 1] = delim;
  4894         result[len] = 0;
  4895     }
  4896     else {
  4897         free(str1);
  4898     }
  4899 
  4900     return result;
  4901 }
  4902 
  4903 DYNAMIC_API PEP_STATUS get_crashdump_log(
  4904         PEP_SESSION session,
  4905         int maxlines,
  4906         char **logdata
  4907     )
  4908 {
  4909     PEP_STATUS status = PEP_STATUS_OK;
  4910     char *_logdata= NULL;
  4911 
  4912     assert(session);
  4913     assert(maxlines >= 0 && maxlines <= CRASHDUMP_MAX_LINES);
  4914     assert(logdata);
  4915 
  4916     if (!(session && logdata && maxlines >= 0 && maxlines <=
  4917             CRASHDUMP_MAX_LINES))
  4918         return PEP_ILLEGAL_VALUE;
  4919 
  4920     *logdata = NULL;
  4921 
  4922     int limit = maxlines ? maxlines : CRASHDUMP_DEFAULT_LINES;
  4923     const char *timestamp = NULL;
  4924     const char *title = NULL;
  4925     const char *entity = NULL;
  4926     const char *desc = NULL;
  4927     const char *comment = NULL;
  4928 
  4929     sqlite3_reset(session->crashdump);
  4930     sqlite3_bind_int(session->crashdump, 1, limit);
  4931 
  4932     int result;
  4933 
  4934     do {
  4935         result = sqlite3_step(session->crashdump);
  4936         switch (result) {
  4937         case SQLITE_ROW:
  4938             timestamp = (const char *) sqlite3_column_text(session->crashdump,
  4939                     0);
  4940             title   = (const char *) sqlite3_column_text(session->crashdump,
  4941                     1);
  4942             entity  = (const char *) sqlite3_column_text(session->crashdump,
  4943                     2);
  4944             desc    = (const char *) sqlite3_column_text(session->crashdump,
  4945                     3);
  4946             comment = (const char *) sqlite3_column_text(session->crashdump,
  4947                     4);
  4948 
  4949             _logdata = _concat_string(_logdata, timestamp, ',');
  4950             if (_logdata == NULL)
  4951                 goto enomem;
  4952 
  4953             _logdata = _concat_string(_logdata, title, ',');
  4954             if (_logdata == NULL)
  4955                 goto enomem;
  4956 
  4957             _logdata = _concat_string(_logdata, entity, ',');
  4958             if (_logdata == NULL)
  4959                 goto enomem;
  4960 
  4961             _logdata = _concat_string(_logdata, desc, ',');
  4962             if (_logdata == NULL)
  4963                 goto enomem;
  4964 
  4965             _logdata = _concat_string(_logdata, comment, '\n');
  4966             if (_logdata == NULL)
  4967                 goto enomem;
  4968 
  4969             _clean_log_value(_logdata);
  4970             break;
  4971 
  4972         case SQLITE_DONE:
  4973             break;
  4974 
  4975         default:
  4976             status = PEP_UNKNOWN_ERROR;
  4977             result = SQLITE_DONE;
  4978         }
  4979     } while (result != SQLITE_DONE);
  4980 
  4981     sqlite3_reset(session->crashdump);
  4982     if (status == PEP_STATUS_OK) {
  4983         if (_logdata) {
  4984             *logdata = _logdata;
  4985         }
  4986         else {
  4987             *logdata = strdup("");
  4988             if (!*logdata)
  4989                 goto enomem;
  4990         }
  4991     }
  4992 
  4993     goto the_end;
  4994 
  4995 enomem:
  4996     status = PEP_OUT_OF_MEMORY;
  4997 
  4998 the_end:
  4999     return status;
  5000 }
  5001 
  5002 DYNAMIC_API PEP_STATUS get_languagelist(
  5003         PEP_SESSION session,
  5004         char **languages
  5005     )
  5006 {
  5007     PEP_STATUS status = PEP_STATUS_OK;
  5008     char *_languages= NULL;
  5009 
  5010     assert(session);
  5011     assert(languages);
  5012 
  5013     if (!(session && languages))
  5014         return PEP_ILLEGAL_VALUE;
  5015 
  5016     *languages = NULL;
  5017 
  5018     const char *lang = NULL;
  5019     const char *name = NULL;
  5020     const char *phrase = NULL;
  5021 
  5022     sqlite3_reset(session->languagelist);
  5023 
  5024     int result;
  5025 
  5026     do {
  5027         result = sqlite3_step(session->languagelist);
  5028         switch (result) {
  5029         case SQLITE_ROW:
  5030             lang = (const char *) sqlite3_column_text(session->languagelist,
  5031                     0);
  5032             name = (const char *) sqlite3_column_text(session->languagelist,
  5033                     1);
  5034             phrase = (const char *) sqlite3_column_text(session->languagelist,
  5035                     2);
  5036 
  5037             _languages = _concat_string(_languages, lang, ',');
  5038             if (_languages == NULL)
  5039                 goto enomem;
  5040 
  5041             _languages = _concat_string(_languages, name, ',');
  5042             if (_languages == NULL)
  5043                 goto enomem;
  5044 
  5045             _languages = _concat_string(_languages, phrase, '\n');
  5046             if (_languages == NULL)
  5047                 goto enomem;
  5048 
  5049             break;
  5050 
  5051         case SQLITE_DONE:
  5052             break;
  5053 
  5054         default:
  5055             status = PEP_UNKNOWN_DB_ERROR;
  5056             result = SQLITE_DONE;
  5057         }
  5058     } while (result != SQLITE_DONE);
  5059 
  5060     sqlite3_reset(session->languagelist);
  5061     if (status == PEP_STATUS_OK)
  5062         *languages = _languages;
  5063 
  5064     goto the_end;
  5065 
  5066 enomem:
  5067     status = PEP_OUT_OF_MEMORY;
  5068 
  5069 the_end:
  5070     return status;
  5071 }
  5072 
  5073 DYNAMIC_API PEP_STATUS get_phrase(
  5074         PEP_SESSION session,
  5075         const char *lang,
  5076         int phrase_id,
  5077         char **phrase
  5078     )
  5079 {
  5080     PEP_STATUS status = PEP_STATUS_OK;
  5081 
  5082     assert(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase);
  5083     if (!(session && lang && lang[0] && lang[1] && lang[2] == 0 && phrase))
  5084         return PEP_ILLEGAL_VALUE;
  5085 
  5086     *phrase = NULL;
  5087 
  5088     sqlite3_reset(session->i18n_token);
  5089     sqlite3_bind_text(session->i18n_token, 1, lang, -1, SQLITE_STATIC);
  5090     sqlite3_bind_int(session->i18n_token, 2, phrase_id);
  5091 
  5092     const char *_phrase = NULL;
  5093     int result;
  5094 
  5095     result = sqlite3_step(session->i18n_token);
  5096     switch (result) {
  5097     case SQLITE_ROW:
  5098         _phrase = (const char *) sqlite3_column_text(session->i18n_token, 0);
  5099         break;
  5100 
  5101     case SQLITE_DONE:
  5102         status = PEP_PHRASE_NOT_FOUND;
  5103         break;
  5104 
  5105     default:
  5106         status = PEP_UNKNOWN_DB_ERROR;
  5107     }
  5108 
  5109     if (status == PEP_STATUS_OK) {
  5110         *phrase = strdup(_phrase);
  5111         if (*phrase == NULL)
  5112             goto enomem;
  5113     }
  5114 
  5115     sqlite3_reset(session->i18n_token);
  5116     goto the_end;
  5117 
  5118 enomem:
  5119     status = PEP_OUT_OF_MEMORY;
  5120 
  5121 the_end:
  5122     return status;
  5123 }
  5124 
  5125 static PEP_STATUS _get_sequence_value(PEP_SESSION session, const char *name,
  5126         int32_t *value)
  5127 {
  5128     assert(session && name && value);
  5129     if (!(session && name && value))
  5130         return PEP_ILLEGAL_VALUE;
  5131 
  5132     PEP_STATUS status = PEP_STATUS_OK;
  5133 
  5134     sqlite3_reset(session->sequence_value2);
  5135     sqlite3_bind_text(session->sequence_value2, 1, name, -1,
  5136             SQLITE_STATIC);
  5137     int result = sqlite3_step(session->sequence_value2);
  5138     switch (result) {
  5139         case SQLITE_ROW: {
  5140             int32_t _value = (int32_t)
  5141                     sqlite3_column_int(session->sequence_value2, 0);
  5142             *value = _value;
  5143             break;
  5144         }
  5145         case SQLITE_DONE:
  5146             status = PEP_RECORD_NOT_FOUND;
  5147             break;
  5148         default:
  5149             status = PEP_UNKNOWN_DB_ERROR;
  5150     }
  5151     sqlite3_reset(session->sequence_value2);
  5152 
  5153     return status;
  5154 }
  5155 
  5156 static PEP_STATUS _increment_sequence_value(PEP_SESSION session,
  5157         const char *name)
  5158 {
  5159     assert(session && name);
  5160     if (!(session && name))
  5161         return PEP_ILLEGAL_VALUE;
  5162 
  5163     sqlite3_reset(session->sequence_value1);
  5164     sqlite3_bind_text(session->sequence_value1, 1, name, -1, SQLITE_STATIC);
  5165     int result = sqlite3_step(session->sequence_value1);
  5166     assert(result == SQLITE_DONE);
  5167     sqlite3_reset(session->sequence_value1);
  5168     if (result == SQLITE_DONE)
  5169         return PEP_STATUS_OK;
  5170     else
  5171         return PEP_CANNOT_INCREASE_SEQUENCE;
  5172 }
  5173 
  5174 DYNAMIC_API PEP_STATUS sequence_value(
  5175         PEP_SESSION session,
  5176         const char *name,
  5177         int32_t *value
  5178     )
  5179 {
  5180     PEP_STATUS status = PEP_STATUS_OK;
  5181 
  5182     assert(session);
  5183     assert(name && value);
  5184 
  5185     if (!(session && name && name[0] && value))
  5186         return PEP_ILLEGAL_VALUE;
  5187 
  5188     *value = 0;
  5189     sqlite3_exec(session->db, "BEGIN TRANSACTION ;", NULL, NULL, NULL);
  5190     status = _increment_sequence_value(session, name);
  5191     if (status == PEP_STATUS_OK)
  5192         status = _get_sequence_value(session, name, value);
  5193 
  5194     if (status == PEP_STATUS_OK) {
  5195         int result = sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
  5196         if (result == SQLITE_OK){
  5197             assert(*value < INT32_MAX);
  5198             if (*value == INT32_MAX){
  5199                 return PEP_CANNOT_INCREASE_SEQUENCE;
  5200             }
  5201             return status;
  5202         } else {
  5203             return PEP_COMMIT_FAILED;
  5204         }
  5205     } else {
  5206         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
  5207         return status;
  5208     }
  5209 
  5210     return status;
  5211 }
  5212 
  5213 PEP_STATUS is_own_key(PEP_SESSION session, const char* fpr, bool* own_key) {
  5214     
  5215     assert(session);
  5216     assert(!EMPTYSTR(fpr));
  5217 
  5218     if (!session || EMPTYSTR(fpr))
  5219         return PEP_ILLEGAL_VALUE;
  5220     
  5221     *own_key = false;
  5222     sqlite3_reset(session->own_key_is_listed);
  5223     
  5224     sqlite3_bind_text(session->own_key_is_listed, 1, fpr, -1,
  5225             SQLITE_STATIC);
  5226     int result = sqlite3_step(session->own_key_is_listed);
  5227     switch (result) {
  5228         case SQLITE_ROW: {
  5229             *own_key = (sqlite3_column_int(session->own_key_is_listed, 0) != 0);
  5230             break;
  5231         }
  5232         default:
  5233             sqlite3_reset(session->own_key_is_listed);
  5234             return PEP_UNKNOWN_DB_ERROR;
  5235     }
  5236 
  5237     sqlite3_reset(session->own_key_is_listed);
  5238     return PEP_STATUS_OK;
  5239 
  5240 }
  5241 
  5242 DYNAMIC_API PEP_STATUS set_revoked(
  5243        PEP_SESSION session,
  5244        const char *revoked_fpr,
  5245        const char *replacement_fpr,
  5246        const uint64_t revocation_date
  5247     )
  5248 {
  5249     PEP_STATUS status = PEP_STATUS_OK;
  5250     
  5251     assert(session &&
  5252            revoked_fpr && revoked_fpr[0] &&
  5253            replacement_fpr && replacement_fpr[0]
  5254           );
  5255     
  5256     if (!(session &&
  5257           revoked_fpr && revoked_fpr[0] &&
  5258           replacement_fpr && replacement_fpr[0]
  5259          ))
  5260         return PEP_ILLEGAL_VALUE;
  5261     
  5262     sqlite3_reset(session->set_revoked);
  5263     sqlite3_bind_text(session->set_revoked, 1, revoked_fpr, -1, SQLITE_STATIC);
  5264     sqlite3_bind_text(session->set_revoked, 2, replacement_fpr, -1,
  5265             SQLITE_STATIC);
  5266     sqlite3_bind_int64(session->set_revoked, 3, revocation_date);
  5267 
  5268     int result;
  5269     
  5270     result = sqlite3_step(session->set_revoked);
  5271     switch (result) {
  5272         case SQLITE_DONE:
  5273             status = PEP_STATUS_OK;
  5274             break;
  5275             
  5276         default:
  5277             status = PEP_UNKNOWN_DB_ERROR;
  5278     }
  5279     
  5280     sqlite3_reset(session->set_revoked);
  5281     return status;
  5282 }
  5283 
  5284 DYNAMIC_API PEP_STATUS get_revoked(
  5285         PEP_SESSION session,
  5286         const char *fpr,
  5287         char **revoked_fpr,
  5288         uint64_t *revocation_date
  5289     )
  5290 {
  5291     PEP_STATUS status = PEP_STATUS_OK;
  5292 
  5293     assert(session &&
  5294            revoked_fpr &&
  5295            fpr && fpr[0]
  5296           );
  5297     
  5298     if (!(session &&
  5299            revoked_fpr &&
  5300            fpr && fpr[0]
  5301           ))
  5302         return PEP_ILLEGAL_VALUE;
  5303 
  5304     *revoked_fpr = NULL;
  5305     *revocation_date = 0;
  5306 
  5307     sqlite3_reset(session->get_revoked);
  5308     sqlite3_bind_text(session->get_revoked, 1, fpr, -1, SQLITE_STATIC);
  5309 
  5310     int result;
  5311     
  5312     result = sqlite3_step(session->get_revoked);
  5313     switch (result) {
  5314         case SQLITE_ROW: {
  5315             *revoked_fpr = strdup((const char *)
  5316                     sqlite3_column_text(session->get_revoked, 0));
  5317             if(*revoked_fpr)
  5318                 *revocation_date = sqlite3_column_int64(session->get_revoked,
  5319                         1);
  5320             else
  5321                 status = PEP_OUT_OF_MEMORY;
  5322 
  5323             break;
  5324         }
  5325         default:
  5326             status = PEP_CANNOT_FIND_IDENTITY;
  5327     }
  5328 
  5329     sqlite3_reset(session->get_revoked);
  5330 
  5331     return status;
  5332 }
  5333 
  5334 DYNAMIC_API PEP_STATUS get_replacement_fpr(
  5335         PEP_SESSION session,
  5336         const char *fpr,
  5337         char **revoked_fpr,
  5338         uint64_t *revocation_date
  5339     )
  5340 {
  5341     PEP_STATUS status = PEP_STATUS_OK;
  5342 
  5343     assert(session && revoked_fpr && !EMPTYSTR(fpr) && revocation_date);
  5344     
  5345     if (!session || !revoked_fpr || EMPTYSTR(fpr) || !revocation_date)
  5346         return PEP_ILLEGAL_VALUE;
  5347 
  5348     *revoked_fpr = NULL;
  5349     *revocation_date = 0;
  5350 
  5351     sqlite3_reset(session->get_replacement_fpr);
  5352     sqlite3_bind_text(session->get_replacement_fpr, 1, fpr, -1, SQLITE_STATIC);
  5353 
  5354     int result;
  5355     
  5356     result = sqlite3_step(session->get_replacement_fpr);
  5357     switch (result) {
  5358         case SQLITE_ROW: {
  5359             *revoked_fpr = strdup((const char *)
  5360                     sqlite3_column_text(session->get_replacement_fpr, 0));
  5361             if(*revoked_fpr)
  5362                 *revocation_date = sqlite3_column_int64(session->get_replacement_fpr,
  5363                         1);
  5364             else
  5365                 status = PEP_OUT_OF_MEMORY;
  5366 
  5367             break;
  5368         }
  5369         default:
  5370             status = PEP_CANNOT_FIND_IDENTITY;
  5371     }
  5372 
  5373     sqlite3_reset(session->get_replacement_fpr);
  5374 
  5375     return status;
  5376 }
  5377 
  5378 PEP_STATUS get_last_contacted(
  5379         PEP_SESSION session,
  5380         identity_list** id_list
  5381     )
  5382 {
  5383     assert(session);
  5384     assert(id_list);
  5385 
  5386     if (!(session && id_list))
  5387         return PEP_ILLEGAL_VALUE;
  5388 
  5389     *id_list = NULL;
  5390     identity_list* ident_list = NULL;
  5391 
  5392     sqlite3_reset(session->get_last_contacted);
  5393     int result;
  5394 
  5395     while ((result = sqlite3_step(session->get_last_contacted)) == SQLITE_ROW) {
  5396         pEp_identity *ident = new_identity(
  5397                 (const char *) sqlite3_column_text(session->get_last_contacted, 1),
  5398                 NULL,
  5399                 (const char *) sqlite3_column_text(session->get_last_contacted, 0),
  5400                 NULL);
  5401                 
  5402         assert(ident);
  5403         if (ident == NULL) {
  5404             sqlite3_reset(session->get_last_contacted);
  5405             return PEP_OUT_OF_MEMORY;
  5406         }
  5407     
  5408         if (ident_list)
  5409             identity_list_add(ident_list, ident);
  5410         else
  5411             ident_list = new_identity_list(ident);
  5412     }
  5413 
  5414     sqlite3_reset(session->get_last_contacted);
  5415     
  5416     *id_list = ident_list;
  5417     
  5418     if (!ident_list)
  5419         return PEP_CANNOT_FIND_IDENTITY;
  5420     
  5421     return PEP_STATUS_OK;    
  5422 }
  5423 
  5424 
  5425 PEP_STATUS key_created(
  5426         PEP_SESSION session,
  5427         const char *fpr,
  5428         time_t *created
  5429     )
  5430 {
  5431     assert(session && fpr && created);
  5432     if (!(session && fpr && created))
  5433         return PEP_ILLEGAL_VALUE;
  5434 
  5435     return session->cryptotech[PEP_crypt_OpenPGP].key_created(session, fpr,
  5436             created);
  5437 }
  5438 
  5439 PEP_STATUS find_private_keys(PEP_SESSION session, const char* pattern,
  5440                              stringlist_t **keylist) {
  5441     assert(session && keylist);
  5442     if (!(session && keylist))
  5443         return PEP_ILLEGAL_VALUE;
  5444     
  5445     return session->cryptotech[PEP_crypt_OpenPGP].find_private_keys(session, pattern,
  5446                                                                     keylist);
  5447 }
  5448 
  5449 
  5450 DYNAMIC_API const char* get_engine_version() {
  5451     return PEP_ENGINE_VERSION;
  5452 }
  5453 
  5454 DYNAMIC_API const char* get_protocol_version() {
  5455     return PEP_VERSION;
  5456 }
  5457 
  5458 DYNAMIC_API PEP_STATUS reset_pEptest_hack(PEP_SESSION session)
  5459 {
  5460     assert(session);
  5461 
  5462     if (!session)
  5463         return PEP_ILLEGAL_VALUE;
  5464 
  5465     int int_result = sqlite3_exec(
  5466         session->db,
  5467         "delete from identity where address like '%@pEptest.ch' ;",
  5468         NULL,
  5469         NULL,
  5470         NULL
  5471     );
  5472     assert(int_result == SQLITE_OK);
  5473 
  5474     if (int_result != SQLITE_OK)
  5475         return PEP_UNKNOWN_DB_ERROR;
  5476 
  5477     return PEP_STATUS_OK;
  5478 }
  5479 
  5480 #ifdef DEBUG_ERRORSTACK
  5481 PEP_STATUS session_add_error(PEP_SESSION session, const char* file, unsigned line, PEP_STATUS status)
  5482 {
  5483     char logline[48];
  5484     if(status>0)
  5485     {
  5486         snprintf(logline,47, "%.24s:%u status=%u (0x%x)", file, line, status, status);
  5487     }else{
  5488         snprintf(logline,47, "%.24s:%u status=%i.", file, line, status);
  5489     }
  5490     stringlist_add(session->errorstack, logline); // logline is copied! :-)
  5491     return status;
  5492 }
  5493 
  5494 DYNAMIC_API const stringlist_t* get_errorstack(PEP_SESSION session)
  5495 {
  5496     return session->errorstack;
  5497 }
  5498 
  5499 DYNAMIC_API void clear_errorstack(PEP_SESSION session)
  5500 {
  5501     const int old_len = stringlist_length(session->errorstack);
  5502     char buf[48];
  5503     free_stringlist(session->errorstack);
  5504     snprintf(buf, 47, "(%i elements cleared)", old_len);
  5505     session->errorstack = new_stringlist(buf);
  5506 }
  5507 
  5508 #else
  5509 
  5510 static stringlist_t* dummy_errorstack = NULL;
  5511 
  5512 DYNAMIC_API const stringlist_t* get_errorstack(PEP_SESSION session)
  5513 {
  5514     if(dummy_errorstack == NULL)
  5515     {
  5516         dummy_errorstack = new_stringlist("( Please recompile pEpEngine with -DDEBUG_ERRORSTACK )");
  5517     }
  5518 
  5519     return dummy_errorstack;
  5520 }
  5521 
  5522 DYNAMIC_API void clear_errorstack(PEP_SESSION session)
  5523 {
  5524     // nothing to do here
  5525 }
  5526 
  5527 #endif
  5528 
  5529 DYNAMIC_API void _service_error_log(PEP_SESSION session, const char *entity,
  5530         PEP_STATUS status, const char *where)
  5531 {
  5532     char buffer[128];
  5533     static const size_t size = 127;
  5534     memset(buffer, 0, size+1);
  5535 #ifdef PEP_STATUS_TO_STRING
  5536     snprintf(buffer, size, "%s %.4x", pEp_status_to_string(status), status);
  5537 #else
  5538     snprintf(buffer, size, "error %.4x", status);
  5539 #endif
  5540     log_service(session, "### service error log ###", entity, buffer, where);
  5541 }
  5542 
  5543 DYNAMIC_API void set_debug_color(PEP_SESSION session, int ansi_color)
  5544 {
  5545 #ifndef NDEBUG
  5546     session->debug_color = ansi_color;
  5547 #endif
  5548 }
  5549 
  5550 PEP_STATUS set_all_userids_to_own(PEP_SESSION session, identity_list* id_list) {
  5551     static char* ownid = NULL;
  5552     PEP_STATUS status = PEP_STATUS_OK;
  5553     if (!ownid) {
  5554         status = get_default_own_userid(session, &ownid);
  5555     }    
  5556     if (status == PEP_STATUS_OK) {
  5557         if (ownid) {
  5558             status = set_all_userids_in_list(id_list, ownid);
  5559         }
  5560     }
  5561     return status;    
  5562 }