ENGINE-250: fixed some things, am screaming at others. Have overcome the 'insert or update' issue at least, probably. ENGINE-250
authorKrista Bennett <krista@pep-project.org>
Tue, 06 Feb 2018 18:48:45 +0100
branchENGINE-250
changeset 24780460dfb7f3dc
parent 2477 45e8ed722fec
child 2479 aba55b53cd31
ENGINE-250: fixed some things, am screaming at others. Have overcome the 'insert or update' issue at least, probably.
src/blacklist.c
src/keymanagement.c
src/message_api.c
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
test/Makefile
     1.1 --- a/src/blacklist.c	Mon Feb 05 10:58:03 2018 +0100
     1.2 +++ b/src/blacklist.c	Tue Feb 06 18:48:45 2018 +0100
     1.3 @@ -13,6 +13,8 @@
     1.4      if (!(session && fpr && fpr[0]))
     1.5          return PEP_ILLEGAL_VALUE;
     1.6  
     1.7 +    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
     1.8 +
     1.9      sqlite3_reset(session->blacklist_add);
    1.10  	sqlite3_bind_text(session->blacklist_add, 1, fpr, -1, SQLITE_STATIC);
    1.11  
    1.12 @@ -22,9 +24,11 @@
    1.13      switch (result) {
    1.14      case SQLITE_DONE:
    1.15          status = PEP_STATUS_OK;
    1.16 +        sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
    1.17          break;
    1.18  
    1.19      default:
    1.20 +        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
    1.21          status = PEP_UNKNOWN_ERROR;
    1.22      }
    1.23  
    1.24 @@ -157,4 +161,3 @@
    1.25  the_end:
    1.26      return status;
    1.27  }
    1.28 -
     2.1 --- a/src/keymanagement.c	Mon Feb 05 10:58:03 2018 +0100
     2.2 +++ b/src/keymanagement.c	Tue Feb 06 18:48:45 2018 +0100
     2.3 @@ -1070,7 +1070,12 @@
     2.4          // set mistrust for this user_id/keypair (even if there's not an
     2.5          // identity set yet, this is important, as we need to record the mistrust
     2.6          // action)
     2.7 -        status = set_trust(session, ident->user_id, ident->fpr, PEP_ct_mistrusted);
     2.8 +        
     2.9 +        // We set this temporarily but will grab it back from the cache afterwards
    2.10 +        ident->comm_type = PEP_ct_mistrusted;
    2.11 +        status = set_trust(session, ident);
    2.12 +        ident->comm_type = session->cached_mistrusted->comm_type;
    2.13 +        
    2.14          if (status == PEP_STATUS_OK)
    2.15              // cascade that mistrust for anyone using this key
    2.16              status = mark_as_compromized(session, ident->fpr);
    2.17 @@ -1140,7 +1145,9 @@
    2.18      if (input_copy->comm_type != PEP_ct_mistrusted)
    2.19          new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
    2.20  
    2.21 -    status = set_trust(session, ident->user_id, ident->fpr, new_trust);
    2.22 +    // We'll return the status from the input_copy cache afterward
    2.23 +    input_copy->comm_type = new_trust;
    2.24 +    status = set_trust(session, input_copy);
    2.25      
    2.26      if (status != PEP_STATUS_OK)
    2.27          goto pep_free;
    2.28 @@ -1157,8 +1164,6 @@
    2.29  
    2.30      if (status != PEP_STATUS_OK)
    2.31          goto pep_free;
    2.32 -    
    2.33 -    input_copy->comm_type = new_trust;
    2.34          
    2.35      tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
    2.36  
    2.37 @@ -1263,7 +1268,7 @@
    2.38  
    2.39                      trusted_default = true;
    2.40                                      
    2.41 -                    status = set_trust(session, tmp_id->user_id, cached_fpr, tmp_id->comm_type);
    2.42 +                    status = set_trust(session, tmp_id);
    2.43                      input_default_ct = tmp_id->comm_type;                    
    2.44                  }
    2.45                  else {
     3.1 --- a/src/message_api.c	Mon Feb 05 10:58:03 2018 +0100
     3.2 +++ b/src/message_api.c	Tue Feb 06 18:48:45 2018 +0100
     3.3 @@ -2478,12 +2478,12 @@
     3.4      // This file's code is difficult enough to parse. But change at will.
     3.5      switch (sender->comm_type) {
     3.6          case PEP_ct_OpenPGP_unconfirmed:
     3.7 -            status = set_trust(session, sender->user_id, sender->fpr, PEP_ct_pEp_unconfirmed);
     3.8 -            break;
     3.9          case PEP_ct_OpenPGP:
    3.10 -            status = set_trust(session, sender->user_id, sender->fpr, PEP_ct_pEp);
    3.11 +            sender->comm_type = PEP_ct_pEp_unconfirmed | (sender->comm_type & PEP_ct_confirmed);
    3.12 +            status = set_trust(session, sender);
    3.13              break;
    3.14          default:
    3.15 +            status = PEP_CANNOT_SET_TRUST;
    3.16              break;
    3.17      }
    3.18      
     4.1 --- a/src/pEpEngine.c	Mon Feb 05 10:58:03 2018 +0100
     4.2 +++ b/src/pEpEngine.c	Tue Feb 06 18:48:45 2018 +0100
     4.3 @@ -98,12 +98,15 @@
     4.4  // Set person, but if already exist, only update.
     4.5  // if main_key_id already set, don't touch.
     4.6  static const char *sql_set_person = 
     4.7 -     "insert or ignore into person (id, username, lang, main_key_id, device_group)"
     4.8 +     "insert or replace into person (id, username, lang, main_key_id, device_group)"
     4.9       "  values (?1, ?2, ?3,"
    4.10 -     "    (select coalesce( "
    4.11 -     "          (select main_key_id from person where id = ?1), " 
    4.12 -     "           upper(replace(?4,' ','')))),"     
    4.13 -     "    (select device_group from person where id = ?1)) ;"
    4.14 +//     "    (select coalesce( "
    4.15 +//     "          (select main_key_id from person where id = ?1), " 
    4.16 +//     "           upper(replace(?4,' ','')))),"
    4.17 +    "   ?4,"
    4.18 +     "    (select device_group from person where id = ?1)) ;";
    4.19 +
    4.20 +static const char *sql_update_person = 
    4.21      "update person "
    4.22      "   set username = ?2, "
    4.23      "       lang = ?3, "
    4.24 @@ -165,8 +168,8 @@
    4.25      "insert or ignore into pgp_keypair (fpr) "
    4.26      "values (upper(replace(?1,' ',''))) ;";
    4.27  
    4.28 -static const char *sql_set_identity = 
    4.29 -    "insert or ignore into identity ("
    4.30 +static const char *sql_set_identity_entry = 
    4.31 +    "insert or replace into identity ("
    4.32      "       address, main_key_id, "
    4.33      "       user_id, flags, is_own"
    4.34      "   ) values ("
    4.35 @@ -175,7 +178,9 @@
    4.36      "       ?3,"
    4.37      "       ?4,"
    4.38      "       ?5"
    4.39 -    "   );"
    4.40 +    "   );";
    4.41 +    
    4.42 +static const char* sql_update_identity_entry =    
    4.43      "update identity "
    4.44      "   set main_key_id = upper(replace(?2,' ','')), "
    4.45      "       flags = ?4, " 
    4.46 @@ -190,8 +195,6 @@
    4.47      // "    0)"
    4.48      // " ) | (?4 & 255)"
    4.49      /* set_identity ignores previous flags, and doesn't filter machine flags */
    4.50 -
    4.51 -
    4.52          
    4.53  static const char *sql_set_identity_flags = 
    4.54      "update identity set flags = "
    4.55 @@ -206,8 +209,10 @@
    4.56      "where address = ?2 and user_id = ?3 ;";
    4.57  
    4.58  static const char *sql_set_trust =
    4.59 -    "insert or ignore into trust (user_id, pgp_keypair_fpr, comm_type) "
    4.60 -    "values (?1, upper(replace(?2,' ','')), ?3) ;"
    4.61 +    "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
    4.62 +    "values (?1, upper(replace(?2,' ','')), ?3) ;";
    4.63 +
    4.64 +static const char *sql_update_trust =
    4.65      "update trust set comm_type = ?3 " 
    4.66      "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
    4.67      
    4.68 @@ -329,7 +334,7 @@
    4.69  
    4.70  // Revocation tracking
    4.71  static const char *sql_add_mistrusted_key =
    4.72 -    "insert or ignore into mistrusted_keys (fpr) "
    4.73 +    "insert or replace into mistrusted_keys (fpr) "
    4.74      "   values (upper(replace(?1,' ',''))) ;";
    4.75          
    4.76  static const char *sql_delete_mistrusted_key = 
    4.77 @@ -339,7 +344,7 @@
    4.78      "select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
    4.79  
    4.80  static const char *sql_add_userid_alias =
    4.81 -    "insert or ignore into alternate_user_id (default_id, alternate_id) "
    4.82 +    "insert or replace into alternate_user_id (default_id, alternate_id) "
    4.83      "values (?1, ?2) ;";
    4.84      
    4.85  static int user_version(void *_version, int count, char **text, char **name)
    4.86 @@ -611,8 +616,8 @@
    4.87                  ");\n"
    4.88                  // user id aliases
    4.89                  "create table if not exists alternate_user_id (\n"
    4.90 -                "    default_id text references person (id),\n"
    4.91 -//                "       on delete cascade on update cascade,\n"
    4.92 +                "    default_id text references person (id)\n"
    4.93 +                "       on delete cascade on update cascade,\n"
    4.94                  "    alternate_id text primary key\n"
    4.95                  ");\n"
    4.96                  // mistrusted keys
    4.97 @@ -835,8 +840,8 @@
    4.98                      "\n"
    4.99                      "PRAGMA foreign_keys=on;\n"
   4.100                      "create table if not exists alternate_user_id (\n"
   4.101 -                    "    default_id text references person (id),\n"
   4.102 -//                    "       on delete cascade on update cascade,\n"
   4.103 +                    "    default_id text references person (id)\n"
   4.104 +                    "       on delete cascade on update cascade,\n"
   4.105                      "    alternate_id text primary key\n"
   4.106                      ");\n"
   4.107                      ,
   4.108 @@ -977,6 +982,10 @@
   4.109              (int)strlen(sql_set_person), &_session->set_person, NULL);
   4.110      assert(int_result == SQLITE_OK);
   4.111  
   4.112 +    int_result = sqlite3_prepare_v2(_session->db, sql_update_person,
   4.113 +            (int)strlen(sql_update_person), &_session->update_person, NULL);
   4.114 +    assert(int_result == SQLITE_OK);
   4.115 +
   4.116      int_result = sqlite3_prepare_v2(_session->db, sql_set_as_pep_user,
   4.117              (int)strlen(sql_set_as_pep_user), &_session->set_as_pep_user, NULL);
   4.118      assert(int_result == SQLITE_OK);
   4.119 @@ -1002,8 +1011,12 @@
   4.120              NULL);
   4.121      assert(int_result == SQLITE_OK);
   4.122  
   4.123 -    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity,
   4.124 -            (int)strlen(sql_set_identity), &_session->set_identity, NULL);
   4.125 +    int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_entry,
   4.126 +            (int)strlen(sql_set_identity_entry), &_session->set_identity_entry, NULL);
   4.127 +    assert(int_result == SQLITE_OK);
   4.128 +
   4.129 +    int_result = sqlite3_prepare_v2(_session->db, sql_update_identity_entry,
   4.130 +            (int)strlen(sql_update_identity_entry), &_session->update_identity_entry, NULL);
   4.131      assert(int_result == SQLITE_OK);
   4.132  
   4.133      int_result = sqlite3_prepare_v2(_session->db, sql_set_identity_flags,
   4.134 @@ -1020,6 +1033,10 @@
   4.135              (int)strlen(sql_set_trust), &_session->set_trust, NULL);
   4.136      assert(int_result == SQLITE_OK);
   4.137  
   4.138 +    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust,
   4.139 +            (int)strlen(sql_update_trust), &_session->update_trust, NULL);
   4.140 +    assert(int_result == SQLITE_OK);
   4.141 +
   4.142      int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
   4.143              (int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
   4.144      assert(int_result == SQLITE_OK);
   4.145 @@ -1159,7 +1176,7 @@
   4.146          // private keys have an 'unknown' trust designation in PGP).
   4.147  
   4.148          // We don't really worry about the status here.
   4.149 -        status = import_trusted_own_keys(_session);        
   4.150 +//        status = import_trusted_own_keys(_session);        
   4.151      }
   4.152  
   4.153      // sync_session set to own session by default
   4.154 @@ -1239,14 +1256,18 @@
   4.155                  sqlite3_finalize(session->get_device_group);
   4.156              if (session->set_pgp_keypair)
   4.157                  sqlite3_finalize(session->set_pgp_keypair);
   4.158 -            if (session->set_identity)
   4.159 -                sqlite3_finalize(session->set_identity);
   4.160 +            if (session->set_identity_entry)
   4.161 +                sqlite3_finalize(session->set_identity_entry);
   4.162 +            if (session->update_identity_entry)
   4.163 +                sqlite3_finalize(session->update_identity_entry);    
   4.164              if (session->set_identity_flags)
   4.165                  sqlite3_finalize(session->set_identity_flags);
   4.166              if (session->unset_identity_flags)
   4.167                  sqlite3_finalize(session->unset_identity_flags);
   4.168              if (session->set_trust)
   4.169                  sqlite3_finalize(session->set_trust);
   4.170 +            if (session->update_trust)
   4.171 +                sqlite3_finalize(session->update_trust);                
   4.172              if (session->update_trust_for_fpr)
   4.173                  sqlite3_finalize(session->update_trust_for_fpr);
   4.174              if (session->get_trust)
   4.175 @@ -1712,6 +1733,8 @@
   4.176      if (!(session && default_id && alias_id && 
   4.177            default_id[0] != '\0' && alias_id[0] != '\0'))
   4.178          return PEP_ILLEGAL_VALUE;
   4.179 +    
   4.180 +    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   4.181  
   4.182      sqlite3_reset(session->add_userid_alias);
   4.183      sqlite3_bind_text(session->add_userid_alias, 1, default_id, -1,
   4.184 @@ -1722,9 +1745,13 @@
   4.185      result = sqlite3_step(session->add_userid_alias);
   4.186  
   4.187      sqlite3_reset(session->add_userid_alias);
   4.188 -    if (result != SQLITE_DONE)
   4.189 +    if (result != SQLITE_DONE) {
   4.190 +        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);        
   4.191          return PEP_CANNOT_SET_ALIAS;
   4.192 -    
   4.193 +    }
   4.194 +    sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
   4.195 +        
   4.196 +
   4.197      return PEP_STATUS_OK;
   4.198  }
   4.199  
   4.200 @@ -1946,6 +1973,155 @@
   4.201      return PEP_STATUS_OK;
   4.202  }
   4.203  
   4.204 +static PEP_STATUS _set_or_update_trust(PEP_SESSION session,
   4.205 +                                       const pEp_identity* identity,
   4.206 +                                       sqlite3_stmt* set_or_update) {
   4.207 +
   4.208 +    assert(session);
   4.209 +    assert(identity);
   4.210 +    assert(identity->user_id);
   4.211 +    assert(identity->fpr);
   4.212 +    
   4.213 +    if (!session || !identity || EMPTYSTR(identity->user_id) || EMPTYSTR(identity->fpr))
   4.214 +        return PEP_ILLEGAL_VALUE;
   4.215 +        
   4.216 +    int result;
   4.217 +                
   4.218 +    sqlite3_reset(set_or_update);
   4.219 +    sqlite3_bind_text(set_or_update, 1, identity->user_id, -1,
   4.220 +            SQLITE_STATIC);
   4.221 +    sqlite3_bind_text(set_or_update, 2, identity->fpr, -1,
   4.222 +            SQLITE_STATIC);
   4.223 +    sqlite3_bind_int(set_or_update, 3, identity->comm_type);
   4.224 +    result = sqlite3_step(set_or_update);
   4.225 +    assert(result == SQLITE_DONE);
   4.226 +    sqlite3_reset(set_or_update);
   4.227 +    if (result != SQLITE_DONE)
   4.228 +        return PEP_CANNOT_SET_TRUST;
   4.229 +
   4.230 +    return PEP_STATUS_OK;
   4.231 +}
   4.232 +
   4.233 +static PEP_STATUS _set_or_update_identity_entry(PEP_SESSION session,
   4.234 +                                                const pEp_identity* identity,
   4.235 +                                                sqlite3_stmt* set_or_update) {
   4.236 +    assert(session);
   4.237 +    assert(identity);
   4.238 +    assert(set_or_update);
   4.239 +                      
   4.240 +    if (!session || !identity || !identity->user_id || !identity->address)
   4.241 +        return PEP_ILLEGAL_VALUE;
   4.242 +                                              
   4.243 +    sqlite3_reset(set_or_update);
   4.244 +    sqlite3_bind_text(set_or_update, 1, identity->address, -1,
   4.245 +            SQLITE_STATIC);
   4.246 +    sqlite3_bind_text(set_or_update, 2, identity->fpr, -1,
   4.247 +            SQLITE_STATIC);
   4.248 +    sqlite3_bind_text(set_or_update, 3, identity->user_id, -1,
   4.249 +            SQLITE_STATIC);
   4.250 +    sqlite3_bind_int(set_or_update, 4, identity->flags);
   4.251 +    sqlite3_bind_int(set_or_update, 5, identity->me);
   4.252 +    int result = sqlite3_step(set_or_update);
   4.253 +    sqlite3_reset(set_or_update);
   4.254 +    if (result != SQLITE_DONE)
   4.255 +        return PEP_CANNOT_SET_IDENTITY;
   4.256 +    
   4.257 +    return PEP_STATUS_OK;
   4.258 +}
   4.259 +
   4.260 +static PEP_STATUS _set_or_update_person(PEP_SESSION session, 
   4.261 +                                        const pEp_identity* identity,
   4.262 +                                        sqlite3_stmt* set_or_update) {
   4.263 +    assert(session);
   4.264 +    assert(identity);
   4.265 +    assert(set_or_update);
   4.266 +                        
   4.267 +    if (!session || !identity || !identity->user_id || !identity->username)
   4.268 +        return PEP_ILLEGAL_VALUE;
   4.269 +        
   4.270 +    sqlite3_reset(set_or_update);
   4.271 +    sqlite3_bind_text(set_or_update, 1, identity->user_id, -1,
   4.272 +            SQLITE_STATIC);
   4.273 +    sqlite3_bind_text(set_or_update, 2, identity->username, -1,
   4.274 +            SQLITE_STATIC);
   4.275 +    if (identity->lang[0])
   4.276 +        sqlite3_bind_text(set_or_update, 3, identity->lang, 2,
   4.277 +                SQLITE_STATIC);
   4.278 +    else
   4.279 +        sqlite3_bind_null(set_or_update, 3);
   4.280 +    sqlite3_bind_text(set_or_update, 4, identity->fpr, -1,
   4.281 +                      SQLITE_STATIC);
   4.282 +    int result = sqlite3_step(set_or_update);
   4.283 +    sqlite3_reset(set_or_update);
   4.284 +    
   4.285 +    if (result != SQLITE_DONE)
   4.286 +        return PEP_CANNOT_SET_PERSON;
   4.287 +    
   4.288 +    return PEP_STATUS_OK;                                         
   4.289 +}
   4.290 +
   4.291 +PEP_STATUS set_or_update_with_identity(PEP_SESSION session,
   4.292 +                                       const pEp_identity* identity,
   4.293 +                                       PEP_STATUS (* set_function)(PEP_SESSION, const pEp_identity*, sqlite3_stmt*),
   4.294 +                                       sqlite3_stmt* set_query,
   4.295 +                                       sqlite3_stmt* update_query,
   4.296 +                                       bool guard_transaction) {
   4.297 +
   4.298 +    if (guard_transaction) {
   4.299 +        sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
   4.300 +    }                      
   4.301 +    PEP_STATUS status = set_function(session, identity, update_query);
   4.302 +    if (status != PEP_STATUS_OK) {
   4.303 +        if (guard_transaction)
   4.304 +            sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   4.305 +        return status;    
   4.306 +    }   
   4.307 +    else {
   4.308 +        if (sqlite3_changes(session->db) == 0) {
   4.309 +            status = set_function(session, identity, set_query);            
   4.310 +            if (status != PEP_STATUS_OK) {
   4.311 +                if (guard_transaction)
   4.312 +                     sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   4.313 +                return status;    
   4.314 +            }                                  
   4.315 +        }
   4.316 +    }
   4.317 +    if (guard_transaction) {
   4.318 +        sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
   4.319 +    }                      
   4.320 +    return status;
   4.321 +}
   4.322 +
   4.323 +PEP_STATUS _set_trust_internal(PEP_SESSION session, const pEp_identity* identity,
   4.324 +                               bool guard_transaction) {
   4.325 +    return set_or_update_with_identity(session, identity,
   4.326 +                                       _set_or_update_trust,
   4.327 +                                       session->update_trust,
   4.328 +                                       session->set_trust,
   4.329 +                                       guard_transaction);
   4.330 +}
   4.331 +PEP_STATUS set_trust(PEP_SESSION session, const pEp_identity* identity) {
   4.332 +    return _set_trust_internal(session, identity, true);
   4.333 +}
   4.334 +
   4.335 +PEP_STATUS set_person(PEP_SESSION session, const pEp_identity* identity,
   4.336 +                      bool guard_transaction) {
   4.337 +    return set_or_update_with_identity(session, identity,
   4.338 +                                       _set_or_update_person,
   4.339 +                                       session->update_person,
   4.340 +                                       session->set_person,
   4.341 +                                       guard_transaction);
   4.342 +}
   4.343 +
   4.344 +PEP_STATUS set_identity_entry(PEP_SESSION session, const pEp_identity* identity,
   4.345 +                              bool guard_transaction) {
   4.346 +    return set_or_update_with_identity(session, identity,
   4.347 +                                       _set_or_update_identity_entry,
   4.348 +                                       session->update_identity_entry,
   4.349 +                                       session->set_identity_entry,
   4.350 +                                       guard_transaction);
   4.351 +}
   4.352 +
   4.353  DYNAMIC_API PEP_STATUS set_identity(
   4.354          PEP_SESSION session, const pEp_identity *identity
   4.355      )
   4.356 @@ -1999,53 +2175,23 @@
   4.357          }
   4.358      }
   4.359  
   4.360 -    sqlite3_reset(session->set_person);
   4.361 -    sqlite3_bind_text(session->set_person, 1, identity->user_id, -1,
   4.362 -            SQLITE_STATIC);
   4.363 -    sqlite3_bind_text(session->set_person, 2, identity->username, -1,
   4.364 -            SQLITE_STATIC);
   4.365 -    if (identity->lang[0])
   4.366 -        sqlite3_bind_text(session->set_person, 3, identity->lang, 2,
   4.367 -                SQLITE_STATIC);
   4.368 -    else
   4.369 -        sqlite3_bind_null(session->set_person, 3);
   4.370 -    sqlite3_bind_text(session->set_person, 4, identity->fpr, -1,
   4.371 -                      SQLITE_STATIC);
   4.372 -    result = sqlite3_step(session->set_person);
   4.373 -    sqlite3_reset(session->set_person);
   4.374 -    if (result != SQLITE_DONE) {
   4.375 +    status = set_person(session, identity, false);
   4.376 +    if (status != PEP_STATUS_OK) {
   4.377          sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   4.378 -        return PEP_CANNOT_SET_PERSON;
   4.379 +        return status;
   4.380      }
   4.381  
   4.382 -    sqlite3_reset(session->set_identity);
   4.383 -    sqlite3_bind_text(session->set_identity, 1, identity->address, -1,
   4.384 -            SQLITE_STATIC);
   4.385 -    sqlite3_bind_text(session->set_identity, 2, identity->fpr, -1,
   4.386 -            SQLITE_STATIC);
   4.387 -    sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   4.388 -            SQLITE_STATIC);
   4.389 -    sqlite3_bind_int(session->set_identity, 4, identity->flags);
   4.390 -    sqlite3_bind_int(session->set_identity, 5, identity->me);
   4.391 -    result = sqlite3_step(session->set_identity);
   4.392 -    sqlite3_reset(session->set_identity);
   4.393 -    if (result != SQLITE_DONE) {
   4.394 +    status = set_identity_entry(session, identity, false);
   4.395 +    if (status != PEP_STATUS_OK) {
   4.396          sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   4.397 -        return PEP_CANNOT_SET_IDENTITY;
   4.398 +        return status;
   4.399      }
   4.400  
   4.401      if (has_fpr) {
   4.402 -        sqlite3_reset(session->set_trust);
   4.403 -        sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   4.404 -                SQLITE_STATIC);
   4.405 -        sqlite3_bind_text(session->set_trust, 2, identity->fpr, -1,
   4.406 -                SQLITE_STATIC);
   4.407 -        sqlite3_bind_int(session->set_trust, 3, identity->comm_type);
   4.408 -        result = sqlite3_step(session->set_trust);
   4.409 -        sqlite3_reset(session->set_trust);
   4.410 -        if (result != SQLITE_DONE) {
   4.411 +        status = _set_trust_internal(session, identity, false);
   4.412 +        if (status != PEP_STATUS_OK) {
   4.413              sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   4.414 -            return PEP_CANNOT_SET_TRUST;
   4.415 +            return status;
   4.416          }
   4.417      }
   4.418      
   4.419 @@ -2554,35 +2700,6 @@
   4.420      free(p);
   4.421  }
   4.422  
   4.423 -PEP_STATUS set_trust(PEP_SESSION session, 
   4.424 -                     const char* user_id,
   4.425 -                     const char* fpr, 
   4.426 -                     PEP_comm_type comm_type) 
   4.427 -{
   4.428 -    assert(session);
   4.429 -    assert(user_id);
   4.430 -    assert(fpr);
   4.431 -    
   4.432 -    if (!session || !user_id || user_id[0] == '\0' || !fpr || fpr[0] == '\0')
   4.433 -        return PEP_ILLEGAL_VALUE;
   4.434 -        
   4.435 -    int result;
   4.436 -                
   4.437 -    sqlite3_reset(session->set_trust);
   4.438 -    sqlite3_bind_text(session->set_trust, 1, user_id, -1,
   4.439 -            SQLITE_STATIC);
   4.440 -    sqlite3_bind_text(session->set_trust, 2, fpr, -1,
   4.441 -            SQLITE_STATIC);
   4.442 -    sqlite3_bind_int(session->set_trust, 3, comm_type);
   4.443 -    result = sqlite3_step(session->set_trust);
   4.444 -    assert(result == SQLITE_DONE);
   4.445 -    sqlite3_reset(session->set_trust);
   4.446 -    if (result != SQLITE_DONE)
   4.447 -        return PEP_CANNOT_SET_TRUST;
   4.448 -
   4.449 -    return PEP_STATUS_OK;
   4.450 -}
   4.451 -
   4.452  
   4.453  DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
   4.454  {
     5.1 --- a/src/pEpEngine.h	Mon Feb 05 10:58:03 2018 +0100
     5.2 +++ b/src/pEpEngine.h	Tue Feb 06 18:48:45 2018 +0100
     5.3 @@ -934,9 +934,7 @@
     5.4  
     5.5  
     5.6  PEP_STATUS set_trust(PEP_SESSION session, 
     5.7 -                            const char* user_id,
     5.8 -                            const char* fpr, 
     5.9 -                            PEP_comm_type comm_type);
    5.10 +                     const pEp_identity* identity);
    5.11                              
    5.12  PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
    5.13                                  const char* fpr, 
     6.1 --- a/src/pEp_internal.h	Mon Feb 05 10:58:03 2018 +0100
     6.2 +++ b/src/pEp_internal.h	Tue Feb 06 18:48:45 2018 +0100
     6.3 @@ -131,16 +131,19 @@
     6.4      sqlite3_stmt *refresh_userid_default_key;
     6.5      sqlite3_stmt *remove_fpr_as_default;
     6.6      sqlite3_stmt *set_person;
     6.7 +    sqlite3_stmt *update_person;    
     6.8      sqlite3_stmt *set_as_pep_user;
     6.9      sqlite3_stmt *is_pep_user;
    6.10      sqlite3_stmt *exists_person;
    6.11      sqlite3_stmt *set_device_group;
    6.12      sqlite3_stmt *get_device_group;
    6.13      sqlite3_stmt *set_pgp_keypair;
    6.14 -    sqlite3_stmt *set_identity;
    6.15 +    sqlite3_stmt *set_identity_entry;
    6.16 +    sqlite3_stmt *update_identity_entry;    
    6.17      sqlite3_stmt *set_identity_flags;
    6.18      sqlite3_stmt *unset_identity_flags;
    6.19      sqlite3_stmt *set_trust;
    6.20 +    sqlite3_stmt *update_trust;    
    6.21      sqlite3_stmt *update_trust_for_fpr;
    6.22      sqlite3_stmt *get_trust;
    6.23      sqlite3_stmt *least_trust;
     7.1 --- a/test/Makefile	Mon Feb 05 10:58:03 2018 +0100
     7.2 +++ b/test/Makefile	Tue Feb 06 18:48:45 2018 +0100
     7.3 @@ -55,7 +55,7 @@
     7.4  
     7.5  UNIT_TESTS_SOURCE=$(wildcard *_test.cc)
     7.6  UNIT_TESTS=$(subst .cc,,$(UNIT_TESTS_SOURCE))
     7.7 -UNIT_TESTS_RUN=$(subst .cc,_gdb,$(UNIT_TESTS_SOURCE))
     7.8 +UNIT_TESTS_RUN=$(subst .cc,_run,$(UNIT_TESTS_SOURCE))
     7.9  
    7.10  CFLAGS:=$(filter-out -DNDEBUG,$(CFLAGS))
    7.11  ifneq ($(MAKECMDGOALS),clean)