ENGINE-209: commit before travel. Updated a lot of mistakes, but need to fix a compiler error. ENGINE-209
authorKrista Bennett <krista@pep-project.org>
Fri, 19 May 2017 14:56:20 +0200
branchENGINE-209
changeset 17991e46b8759248
parent 1798 dddaa8a5d31c
child 1800 73f28d6a45ac
ENGINE-209: commit before travel. Updated a lot of mistakes, but need to fix a compiler error.
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/keymanagement.c	Thu May 18 11:43:01 2017 +0200
     1.2 +++ b/src/keymanagement.c	Fri May 19 14:56:20 2017 +0200
     1.3 @@ -152,9 +152,9 @@
     1.4                  
     1.5              if (revoked) {
     1.6                  // Do stuff
     1.7 -                status = change_key_comm_type(session, stored_identity->fpr, PEP_ct_key_revoked);
     1.8 +                status = update_trust_for_fpr(session, stored_identity->fpr, PEP_ct_key_revoked);
     1.9                  // What to do on failure? FIXME
    1.10 -                status = remove_key_as_id_default(session, stored_identity->fpr);
    1.11 +                status = replace_identities_fpr(session, stored_identity->fpr, "");
    1.12              }
    1.13              else {    
    1.14                  status = blacklist_is_listed(session, stored_identity->fpr, &dont_use_stored_fpr);
    1.15 @@ -1062,67 +1062,3 @@
    1.16  
    1.17      return session->cryptotech[PEP_crypt_OpenPGP].contains_priv_key(session, fpr, has_private);
    1.18  }
    1.19 -
    1.20 -PEP_STATUS change_key_comm_type(PEP_SESSION session, 
    1.21 -                                       const char* fpr,
    1.22 -                                       PEP_comm_type comm_type) {
    1.23 -                                           
    1.24 -    // get all user ids associated with this key
    1.25 -    stringlist_t* user_id_list = NULL;
    1.26 -    
    1.27 -    PEP_STATUS status = get_key_userids(session, fpr, &user_id_list);
    1.28 -    
    1.29 -    if (status != PEP_STATUS_OK) {
    1.30 -        free_stringlist(user_id_list);
    1.31 -        return status;
    1.32 -    }
    1.33 -        
    1.34 -    if (!user_id_list || !user_id_list->value)
    1.35 -        return PEP_KEY_NOT_FOUND;
    1.36 -        
    1.37 -    stringlist_t* curr_id = user_id_list;
    1.38 -    
    1.39 -    while (curr_id) {
    1.40 -        status = set_trust(session, curr_id->value, fpr, comm_type);
    1.41 -        if (status != PEP_STATUS_OK) {
    1.42 -            free_stringlist(user_id_list);
    1.43 -            return status;
    1.44 -        }
    1.45 -        curr_id = curr_id->next;
    1.46 -    }
    1.47 -    
    1.48 -    return PEP_STATUS_OK;
    1.49 -}
    1.50 -
    1.51 -PEP_STATUS remove_key_as_id_default(PEP_SESSION session, 
    1.52 -                                           const char* fpr)
    1.53 -{
    1.54 -    identity_list* affected_ids = NULL;
    1.55 -    
    1.56 -    PEP_STATUS status = get_identities_by_fpr(session, fpr, &affected_ids);
    1.57 -    
    1.58 -    if (status != PEP_STATUS_OK) {
    1.59 -        free_identity_list(affected_ids);
    1.60 -        return status;
    1.61 -    }
    1.62 -    if (!affected_ids)
    1.63 -        return PEP_STATUS_OK; // it's fine if none are found;
    1.64 -        
    1.65 -    identity_list* curr_identity = affected_ids;
    1.66 -    
    1.67 -    status = PEP_STATUS_OK;
    1.68 -    
    1.69 -    while (curr_identity) {
    1.70 -        free(curr_identity->ident->fpr);
    1.71 -        curr_identity->ident->fpr = (char*)calloc(1, sizeof(char)); // ""
    1.72 -        status = set_identity(session, curr_identity->ident);
    1.73 -        if (status != PEP_STATUS_OK)
    1.74 -            goto the_end;
    1.75 -        curr_identity = curr_identity->next;
    1.76 -    }
    1.77 -    
    1.78 -    
    1.79 -the_end:    
    1.80 -    free_identity_list(affected_ids);
    1.81 -    return status;
    1.82 -}
     2.1 --- a/src/keymanagement.h	Thu May 18 11:43:01 2017 +0200
     2.2 +++ b/src/keymanagement.h	Fri May 19 14:56:20 2017 +0200
     2.3 @@ -240,24 +240,6 @@
     2.4         const char *fpr
     2.5      );
     2.6  
     2.7 -// change_key_comm_type() - change comm_type for a key already in the trust table
     2.8 -//                          this impacts any user id associated with this fpr    
     2.9 -//  parameters:
    2.10 -//      session(in)         session to use
    2.11 -//      fpr(in)             fpr for which to change comm_type
    2.12 -//      comm_type(in)       comm_type to change to
    2.13 -//
    2.14 -PEP_STATUS change_key_comm_type(
    2.15 -        PEP_SESSION session,
    2.16 -        const char* fpr,
    2.17 -        PEP_comm_type comm_type
    2.18 -    );
    2.19 -    
    2.20 -PEP_STATUS remove_key_as_id_default(
    2.21 -        PEP_SESSION session, 
    2.22 -        const char* fpr
    2.23 -    );
    2.24 -
    2.25  #ifdef __cplusplus
    2.26  }
    2.27  #endif
     3.1 --- a/src/pEpEngine.c	Thu May 18 11:43:01 2017 +0200
     3.2 +++ b/src/pEpEngine.c	Fri May 19 14:56:20 2017 +0200
     3.3 @@ -29,15 +29,10 @@
     3.4      "       and pgp_keypair_fpr = identity.main_key_id"
     3.5      "   where address = ?1 and identity.user_id = ?2;";
     3.6  
     3.7 -static const char *sql_get_identities_by_fpr =  
     3.8 -    "select fpr, username, comm_type, lang,"
     3.9 -    "   identity.flags | pgp_keypair.flags"
    3.10 -    "   from identity"
    3.11 -    "   join person on id = identity.user_id"
    3.12 -    "   join pgp_keypair on fpr = identity.main_key_id"
    3.13 -    "   join trust on id = trust.user_id"
    3.14 -    "       and pgp_keypair_fpr = identity.main_key_id"
    3.15 -    "   where identity.main_key_id = ?1;";
    3.16 +static const char *sql_replace_identities_fpr =  
    3.17 +    "update identity"
    3.18 +    "   set main_key_id = ?1 "
    3.19 +    "   where main_key_id = ?2 ;";
    3.20  
    3.21  // Set person, but if already exist, only update.
    3.22  // if main_key_id already set, don't touch.
    3.23 @@ -95,14 +90,15 @@
    3.24      "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
    3.25      "values (?1, upper(replace(?2,' ','')), ?3) ;";
    3.26  
    3.27 +static const char *sql_update_trust_for_fpr =
    3.28 +    "update trust "
    3.29 +    "set comm_type = ?1 "
    3.30 +    "where pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
    3.31 +
    3.32  static const char *sql_get_trust = 
    3.33      "select comm_type from trust where user_id = ?1 "
    3.34      "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
    3.35  
    3.36 -static const char *sql_get_key_userids = 
    3.37 -    "select user_id from trust where "
    3.38 -    "pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
    3.39 -
    3.40  static const char *sql_least_trust = 
    3.41      "select min(comm_type) from trust where"
    3.42      " pgp_keypair_fpr = upper(replace(?1,' ',''))"
    3.43 @@ -517,9 +513,9 @@
    3.44              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
    3.45      assert(int_result == SQLITE_OK);
    3.46  
    3.47 -    int_result = sqlite3_prepare_v2(_session->db, sql_get_identities_by_fpr,
    3.48 -            (int)strlen(sql_get_identities_by_fpr), 
    3.49 -            &_session->get_identities_by_fpr, NULL);
    3.50 +    int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
    3.51 +            (int)strlen(sql_replace_identities_fpr), 
    3.52 +            &_session->replace_identities_fpr, NULL);
    3.53      assert(int_result == SQLITE_OK);
    3.54  
    3.55      int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
    3.56 @@ -557,14 +553,14 @@
    3.57              (int)strlen(sql_set_trust), &_session->set_trust, NULL);
    3.58      assert(int_result == SQLITE_OK);
    3.59  
    3.60 +    int_result = sqlite3_prepare_v2(_session->db, sql_update_trust_for_fpr,
    3.61 +            (int)strlen(sql_update_trust_for_fpr), &_session->update_trust_for_fpr, NULL);
    3.62 +    assert(int_result == SQLITE_OK);
    3.63 +
    3.64      int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
    3.65              (int)strlen(sql_get_trust), &_session->get_trust, NULL);
    3.66      assert(int_result == SQLITE_OK);
    3.67  
    3.68 -    int_result = sqlite3_prepare_v2(_session->db, sql_get_key_userids,
    3.69 -            (int)strlen(sql_get_key_userids), &_session->get_key_userids, NULL);
    3.70 -    assert(int_result == SQLITE_OK);
    3.71 -
    3.72      int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
    3.73              (int)strlen(sql_least_trust), &_session->least_trust, NULL);
    3.74      assert(int_result == SQLITE_OK);
    3.75 @@ -754,8 +750,8 @@
    3.76                  sqlite3_finalize(session->trustword);
    3.77              if (session->get_identity)
    3.78                  sqlite3_finalize(session->get_identity);
    3.79 -            if (session->get_identities_by_fpr)
    3.80 -                sqlite3_finalize(session->get_identities_by_fpr);        
    3.81 +            if (session->replace_identities_fpr)
    3.82 +                sqlite3_finalize(session->replace_identities_fpr);        
    3.83              if (session->set_person)
    3.84                  sqlite3_finalize(session->set_person);
    3.85              if (session->set_device_group)
    3.86 @@ -772,10 +768,10 @@
    3.87                  sqlite3_finalize(session->unset_identity_flags);
    3.88              if (session->set_trust)
    3.89                  sqlite3_finalize(session->set_trust);
    3.90 +            if (session->update_trust_for_fpr)
    3.91 +                sqlite3_finalize(session->update_trust_for_fpr);
    3.92              if (session->get_trust)
    3.93                  sqlite3_finalize(session->get_trust);
    3.94 -            if (session->get_key_userids)
    3.95 -                sqlite3_finalize(session->get_key_userids);    
    3.96              if (session->least_trust)
    3.97                  sqlite3_finalize(session->least_trust);
    3.98              if (session->mark_compromized)
    3.99 @@ -1283,108 +1279,48 @@
   3.100          return PEP_COMMIT_FAILED;
   3.101  }
   3.102  
   3.103 -PEP_STATUS get_identities_by_fpr(PEP_SESSION session, 
   3.104 -                                 const char* fpr, 
   3.105 -                                 identity_list** id_list) 
   3.106 +PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
   3.107 +                                 const char* old_fpr, 
   3.108 +                                 const char* new_fpr) 
   3.109  {
   3.110      PEP_STATUS status = PEP_STATUS_OK;
   3.111      
   3.112 -    assert(fpr);
   3.113 -    assert(id_list);
   3.114 +    assert(old_fpr);
   3.115 +    assert(new_fpr);
   3.116      
   3.117 -    if (!fpr || !id_list)
   3.118 +    if (!old_fpr || !new_fpr)
   3.119          return PEP_ILLEGAL_VALUE;
   3.120 -        
   3.121 -    *id_list = NULL;
   3.122 -
   3.123 -    stringlist_t* ids = NULL;
   3.124 -    
   3.125 -    sqlite3_reset(session->get_identities_by_fpr);
   3.126 -    sqlite3_bind_text(session->get_identities_by_fpr, 1, fpr, -1,
   3.127 +            
   3.128 +    sqlite3_reset(session->replace_identities_fpr);
   3.129 +    sqlite3_bind_text(session->replace_identities_fpr, 1, new_fpr, -1,
   3.130 +                      SQLITE_STATIC);
   3.131 +    sqlite3_bind_text(session->replace_identities_fpr, 2, old_fpr, -1,
   3.132                        SQLITE_STATIC);
   3.133  
   3.134 -    int result;
   3.135 -
   3.136 -    pEp_identity* ident;
   3.137 +    int result = sqlite3_step(session->replace_identities_fpr);
   3.138 +    sqlite3_reset(session->replace_identities_fpr);
   3.139      
   3.140 -    do {
   3.141 -        ident = NULL;
   3.142 -        
   3.143 -        result = sqlite3_step(session->get_identities_by_fpr);
   3.144 -        switch (result) {
   3.145 -        case SQLITE_ROW:            
   3.146 -            ident = new_identity(
   3.147 -                    address,
   3.148 -                    (const char *) sqlite3_column_text(session->get_identities_by_fpr, 0),
   3.149 -                    user_id,
   3.150 -                    (const char *) sqlite3_column_text(session->get_identities_by_fpr, 1)
   3.151 -                    );
   3.152 -            assert(_identity);
   3.153 -            if (ident == NULL)
   3.154 -                return PEP_OUT_OF_MEMORY;
   3.155 +    if (result != SQLITE_DONE)
   3.156 +        return PEP_CANNOT_SET_IDENTITY;
   3.157  
   3.158 -            ident->comm_type = (PEP_comm_type)
   3.159 -                sqlite3_column_int(session->get_identities_by_fpr, 2);
   3.160 -            const char* const _lang = (const char *)
   3.161 -                sqlite3_column_text(session->get_identities_by_fpr, 3);
   3.162 -            if (_lang && _lang[0]) {
   3.163 -                assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   3.164 -                assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   3.165 -                assert(_lang[2] == 0);
   3.166 -                ident->lang[0] = _lang[0];
   3.167 -                ident->lang[1] = _lang[1];
   3.168 -                ident->lang[2] = 0;
   3.169 -            }
   3.170 -            ident->flags = (unsigned int)
   3.171 -                sqlite3_column_int(session->get_identities_by_fpr, 4);
   3.172 -            
   3.173 -            if (!ids)
   3.174 -                ids = new_identity_list(ident, ids);
   3.175 -            else
   3.176 -                identity_list_add(ident, ids);
   3.177 -            
   3.178 -            break;
   3.179 -
   3.180 -        case SQLITE_DONE:
   3.181 -            break;
   3.182 -
   3.183 -        default:
   3.184 -            status = PEP_UNKNOWN_ERROR;
   3.185 -            result = SQLITE_DONE;
   3.186 -        }
   3.187 -    } while (result != SQLITE_DONE);
   3.188 -
   3.189 -    sqlite3_reset(session->get_identities_by_fpr);
   3.190 -    if (status == PEP_STATUS_OK)
   3.191 -        *id_list = ids;
   3.192 -
   3.193 -    goto the_end;
   3.194 -
   3.195 -enomem:
   3.196 -    status = PEP_OUT_OF_MEMORY;
   3.197 -
   3.198 -the_end:
   3.199 -    return status;
   3.200 -
   3.201 +    return PEP_STATUS_OK;
   3.202  }
   3.203  
   3.204  
   3.205 -PEP_STATUS set_trust(PEP_SESSION session, 
   3.206 -                            const char* user_id,
   3.207 -                            const char* fpr, 
   3.208 -                            PEP_comm_type comm_type)
   3.209 +PEP_STATUS update_trust_for_fpr(PEP_SESSION session, 
   3.210 +                                const char* fpr, 
   3.211 +                                PEP_comm_type comm_type)
   3.212  {
   3.213 -    if (!user_id || !fpr)
   3.214 +    if (!fpr)
   3.215          return PEP_ILLEGAL_VALUE;
   3.216          
   3.217 -    sqlite3_reset(session->set_trust);
   3.218 -    sqlite3_bind_text(session->set_trust, 1, user_id, -1,
   3.219 +    sqlite3_reset(session->update_trust_for_fpr);
   3.220 +    sqlite3_bind_int(session->update_trust_for_fpr, 1, comm_type, -1,
   3.221              SQLITE_STATIC);
   3.222 -    sqlite3_bind_text(session->set_trust, 2, fpr, -1,
   3.223 +    sqlite3_bind_text(session->update_trust_for_fpr, 2, fpr, -1,
   3.224              SQLITE_STATIC);
   3.225 -    sqlite3_bind_int(session->set_trust, 3, comm_type);
   3.226 -    int result = sqlite3_step(session->set_trust);
   3.227 -    sqlite3_reset(session->set_trust);
   3.228 +    int result = sqlite3_step(session->update_trust_for_fpr);
   3.229 +    sqlite3_reset(session->update_trust_for_fpr);
   3.230      if (result != SQLITE_DONE) {
   3.231          return PEP_CANNOT_SET_TRUST;
   3.232      }
   3.233 @@ -2105,73 +2041,6 @@
   3.234      return status;
   3.235  }
   3.236  
   3.237 -PEP_STATUS get_key_userids(
   3.238 -        PEP_SESSION session,
   3.239 -        const char* fpr,
   3.240 -        stringlist_t** keylist
   3.241 -    )
   3.242 -{
   3.243 -    PEP_STATUS status = PEP_STATUS_OK;
   3.244 -    assert(fpr);
   3.245 -    assert(keylist);
   3.246 -    
   3.247 -    if (!keylist || !fpr)
   3.248 -        return PEP_ILLEGAL_VALUE;
   3.249 -        
   3.250 -    *keylist = NULL;
   3.251 -
   3.252 -    stringlist_t* userid_list = NULL;
   3.253 -    
   3.254 -    sqlite3_reset(session->get_key_userids);
   3.255 -
   3.256 -    int result;
   3.257 -
   3.258 -    char* userid;
   3.259 -    
   3.260 -    do {
   3.261 -        userid = NULL;
   3.262 -        
   3.263 -        result = sqlite3_step(session->get_key_userids);
   3.264 -        switch (result) {
   3.265 -        case SQLITE_ROW:
   3.266 -            userid = (char *) sqlite3_column_text(session->get_key_userids, 0);
   3.267 -    
   3.268 -            if (!userid)
   3.269 -                return PEP_UNKNOWN_ERROR;
   3.270 -
   3.271 -            if (!userid_list) {
   3.272 -                userid_list = new_stringlist(userid);
   3.273 -                if (!userid_list)
   3.274 -                    goto enomem;
   3.275 -            }
   3.276 -            else {
   3.277 -                stringlist_add(userid_list, userid);
   3.278 -            }
   3.279 -            
   3.280 -            break;
   3.281 -
   3.282 -        case SQLITE_DONE:
   3.283 -            break;
   3.284 -
   3.285 -        default:
   3.286 -            status = PEP_UNKNOWN_ERROR;
   3.287 -            result = SQLITE_DONE;
   3.288 -        }
   3.289 -    } while (result != SQLITE_DONE);
   3.290 -
   3.291 -    sqlite3_reset(session->get_key_userids);
   3.292 -    if (status == PEP_STATUS_OK)
   3.293 -        *keylist = userid_list;
   3.294 -
   3.295 -    goto the_end;
   3.296 -
   3.297 -enomem:
   3.298 -    status = PEP_OUT_OF_MEMORY;
   3.299 -
   3.300 -the_end:
   3.301 -    return status;
   3.302 -}
   3.303 -
   3.304  DYNAMIC_API PEP_STATUS get_phrase(
   3.305          PEP_SESSION session,
   3.306          const char *lang,
     4.1 --- a/src/pEpEngine.h	Thu May 18 11:43:01 2017 +0200
     4.2 +++ b/src/pEpEngine.h	Fri May 19 14:56:20 2017 +0200
     4.3 @@ -543,9 +543,9 @@
     4.4          pEp_identity **identity
     4.5      );
     4.6  
     4.7 -PEP_STATUS get_identities_by_fpr(PEP_SESSION session, 
     4.8 -                                 const char* fpr, 
     4.9 -                                 identity_list** id_list); 
    4.10 +PEP_STATUS replace_identities_fpr(PEP_SESSION session, 
    4.11 +                                 const char* old_fpr, 
    4.12 +                                 const char* new_fpr); 
    4.13  
    4.14  
    4.15  // set_identity() - set identity information
     5.1 --- a/src/pEp_internal.h	Thu May 18 11:43:01 2017 +0200
     5.2 +++ b/src/pEp_internal.h	Fri May 19 14:56:20 2017 +0200
     5.3 @@ -100,7 +100,7 @@
     5.4      sqlite3_stmt *log;
     5.5      sqlite3_stmt *trustword;
     5.6      sqlite3_stmt *get_identity;
     5.7 -    sqlite3_stmt *get_identities_by_fpr;
     5.8 +    sqlite3_stmt *replace_identities_fpr;
     5.9      sqlite3_stmt *set_person;
    5.10      sqlite3_stmt *set_device_group;
    5.11      sqlite3_stmt *get_device_group;
    5.12 @@ -109,8 +109,8 @@
    5.13      sqlite3_stmt *set_identity_flags;
    5.14      sqlite3_stmt *unset_identity_flags;
    5.15      sqlite3_stmt *set_trust;
    5.16 +    sqlite3_stmt *update_trust_for_fpr;
    5.17      sqlite3_stmt *get_trust;
    5.18 -    sqlite3_stmt *get_key_userids;
    5.19      sqlite3_stmt *least_trust;
    5.20      sqlite3_stmt *mark_compromized;
    5.21      sqlite3_stmt *reset_trust;