ENGINE-527: part 1, myself now doesn't write when called from decrypt_message (directly or, hoepfully, indirectly)
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 19 Mar 2019 18:19:59 +0100
changeset 3346278702033186
parent 3342 3e11f68497d4
child 3347 1572cabb0cc4
child 3349 4320b0f44436
ENGINE-527: part 1, myself now doesn't write when called from decrypt_message (directly or, hoepfully, indirectly)
src/keymanagement.c
src/keymanagement.h
src/message_api.c
src/pEpEngine.c
src/pEp_internal.h
     1.1 --- a/src/keymanagement.c	Mon Mar 18 15:37:56 2019 +0100
     1.2 +++ b/src/keymanagement.c	Tue Mar 19 18:19:59 2019 +0100
     1.3 @@ -663,7 +663,7 @@
     1.4              //      here, none of them fit.
     1.5              //    * call set_identity() to store
     1.6              // FIXME: Do we set if we had to copy in the address?
     1.7 -                adjust_pep_trust_status(session, identity);
     1.8 +            adjust_pep_trust_status(session, identity);
     1.9              status = set_identity(session, identity);
    1.10              //  * Return: created identity
    1.11          }        
    1.12 @@ -958,7 +958,11 @@
    1.13      return status;
    1.14  }
    1.15  
    1.16 -PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags)
    1.17 +PEP_STATUS _myself(PEP_SESSION session, 
    1.18 +                   pEp_identity * identity, 
    1.19 +                   bool do_keygen, 
    1.20 +                   bool ignore_flags,
    1.21 +                   bool read_only)
    1.22  {
    1.23  
    1.24      PEP_STATUS status;
    1.25 @@ -980,18 +984,24 @@
    1.26      status = get_default_own_userid(session, &default_own_id);
    1.27  
    1.28      // Deal with non-default user_ids.
    1.29 +    // FIXME: if non-default and read-only, reject totally?
    1.30      if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
    1.31 -        
    1.32 -        status = set_userid_alias(session, default_own_id, identity->user_id);
    1.33 -        // Do we want this to be fatal? For now, we'll do it...
    1.34 -        if (status != PEP_STATUS_OK)
    1.35 -            goto pep_free;
    1.36 -            
    1.37 -        free(identity->user_id);
    1.38 -        identity->user_id = strdup(default_own_id);
    1.39 -        if (identity->user_id == NULL) {
    1.40 -            status = PEP_OUT_OF_MEMORY;
    1.41 -            goto pep_free;
    1.42 +        if (read_only) {
    1.43 +            free(identity->user_id);
    1.44 +            identity->user_id = strdup(default_own_id);
    1.45 +        }
    1.46 +        else {
    1.47 +            status = set_userid_alias(session, default_own_id, identity->user_id);
    1.48 +            // Do we want this to be fatal? For now, we'll do it...
    1.49 +            if (status != PEP_STATUS_OK)
    1.50 +                goto pep_free;
    1.51 +                
    1.52 +            free(identity->user_id);
    1.53 +            identity->user_id = strdup(default_own_id);
    1.54 +            if (identity->user_id == NULL) {
    1.55 +                status = PEP_OUT_OF_MEMORY;
    1.56 +                goto pep_free;
    1.57 +            }
    1.58          }
    1.59      }
    1.60  
    1.61 @@ -1024,7 +1034,7 @@
    1.62      // Set usernames - priority is input username > stored name > address
    1.63      // If there's an input username, we always patch the username with that
    1.64      // input.
    1.65 -    if (EMPTYSTR(identity->username)) {
    1.66 +    if (EMPTYSTR(identity->username) || read_only) {
    1.67          bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
    1.68          char* uname = (stored_uname ? stored_identity->username : identity->address);
    1.69          free(identity->username);
    1.70 @@ -1077,7 +1087,7 @@
    1.71      
    1.72      // Nothing left to do but generate a key
    1.73      if (!valid_key_found) {
    1.74 -        if (!do_keygen)
    1.75 +        if (!do_keygen || read_only)
    1.76              status = PEP_GET_KEY_FAILED;
    1.77          else {
    1.78  // /            DEBUG_LOG("Generating key pair", "debug", identity->address);
    1.79 @@ -1115,12 +1125,14 @@
    1.80      
    1.81      // We want to set an identity in the DB even if a key isn't found, but we have to preserve the status if
    1.82      // it's NOT ok
    1.83 -    PEP_STATUS set_id_status = set_identity(session, identity);
    1.84 -    if (set_id_status == PEP_STATUS_OK)
    1.85 -        set_id_status = set_as_pep_user(session, identity);
    1.86 +    if (!read_only) {
    1.87 +        PEP_STATUS set_id_status = set_identity(session, identity);
    1.88 +        if (set_id_status == PEP_STATUS_OK)
    1.89 +            set_id_status = set_as_pep_user(session, identity);
    1.90  
    1.91 -    status = (status == PEP_STATUS_OK ? set_id_status : status);
    1.92 -
    1.93 +        status = (status == PEP_STATUS_OK ? set_id_status : status);
    1.94 +    }
    1.95 +    
    1.96  pep_free:    
    1.97      free(default_own_id);
    1.98      free(revoked_fpr);                     
    1.99 @@ -1156,7 +1168,7 @@
   1.100  
   1.101  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
   1.102  {
   1.103 -    return _myself(session, identity, true, false);
   1.104 +    return _myself(session, identity, true, false, false);
   1.105  }
   1.106  
   1.107  DYNAMIC_API PEP_STATUS register_examine_function(
   1.108 @@ -1804,7 +1816,7 @@
   1.109              EMPTYSTR(me->user_id) || EMPTYSTR(me->username))
   1.110          return PEP_ILLEGAL_VALUE;
   1.111  
   1.112 -    status = _myself(session, me, false, true);
   1.113 +    status = _myself(session, me, false, true, false);
   1.114      // we do not need a valid key but dislike other errors
   1.115      if (status != PEP_STATUS_OK && status != PEP_GET_KEY_FAILED && status != PEP_KEY_UNSUITABLE)
   1.116          return status;
     2.1 --- a/src/keymanagement.h	Mon Mar 18 15:37:56 2019 +0100
     2.2 +++ b/src/keymanagement.h	Tue Mar 19 18:19:59 2019 +0100
     2.3 @@ -114,7 +114,11 @@
     2.4  
     2.5  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity);
     2.6  
     2.7 -PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
     2.8 +PEP_STATUS _myself(PEP_SESSION session, 
     2.9 +                   pEp_identity * identity, 
    2.10 +                   bool do_keygen, 
    2.11 +                   bool ignore_flags,
    2.12 +                   bool read_only);
    2.13  
    2.14  // retrieve_next_identity() - callback being called by do_keymanagement()
    2.15  //
    2.16 @@ -387,7 +391,7 @@
    2.17         const char *fpr
    2.18      );
    2.19  
    2.20 -PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
    2.21 +//PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
    2.22  
    2.23  PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr);
    2.24  PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr);
     3.1 --- a/src/message_api.c	Mon Mar 18 15:37:56 2019 +0100
     3.2 +++ b/src/message_api.c	Tue Mar 19 18:19:59 2019 +0100
     3.3 @@ -987,7 +987,7 @@
     3.4                  }                        
     3.5              }
     3.6              else
     3.7 -                status = myself(session, curr_identity);
     3.8 +                status = _myself(session, curr_identity, false, false, true);
     3.9          if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    3.10              return status;
    3.11          }
    3.12 @@ -1322,7 +1322,8 @@
    3.13      if (!is_me(session, ident))
    3.14          status = update_identity(session, ident);
    3.15      else
    3.16 -        status = myself(session, ident);
    3.17 +        // ???
    3.18 +        status = _myself(session, ident, false, false, true);
    3.19  
    3.20      if (status == PEP_STATUS_OK) {
    3.21          if (ident->comm_type == PEP_ct_compromised)
    3.22 @@ -2970,6 +2971,7 @@
    3.23      return status;
    3.24  }
    3.25  
    3.26 +// FIXME: myself ??????
    3.27  static PEP_STATUS update_sender_to_pep_trust(
    3.28          PEP_SESSION session, 
    3.29          pEp_identity* sender, 
    3.30 @@ -3238,6 +3240,7 @@
    3.31      stringlist_t *_keylist = NULL;
    3.32      char* signer_fpr = NULL;
    3.33      bool is_pep_msg = is_a_pEpmessage(src);
    3.34 +    bool myself_read_only = (src->dir == PEP_dir_incoming);
    3.35  
    3.36      // Grab input flags
    3.37      bool reencrypt = (((*flags & PEP_decrypt_flag_untrusted_server) > 0) && *keylist && !EMPTYSTR((*keylist)->value));
    3.38 @@ -3301,7 +3304,7 @@
    3.39          if (!is_me(session, src->from))
    3.40              status = update_identity(session, src->from);
    3.41          else
    3.42 -            status = myself(session, src->from);
    3.43 +            status = _myself(session, src->from, false, false, myself_read_only);
    3.44          
    3.45          // We absolutely should NOT be bailing here unless it's a serious error
    3.46          if (status == PEP_OUT_OF_MEMORY)
    3.47 @@ -3517,7 +3520,7 @@
    3.48                                                  if (!is_me(session, src->from))
    3.49                                                      update_identity(session, (src->from));
    3.50                                                  else
    3.51 -                                                    myself(session, src->from);
    3.52 +                                                    _myself(session, src->from, false, false, myself_read_only);
    3.53                                              }
    3.54                                              break;        
    3.55                                          }
    3.56 @@ -3882,7 +3885,7 @@
    3.57          return PEP_ILLEGAL_VALUE;
    3.58  
    3.59      if (ident->me)
    3.60 -        status = _myself(session, ident, false, true);
    3.61 +        status = _myself(session, ident, false, true, true);
    3.62      else
    3.63          status = update_identity(session, ident);
    3.64  
    3.65 @@ -4280,12 +4283,13 @@
    3.66      if (status != PEP_STATUS_OK)
    3.67          goto pep_error;
    3.68  
    3.69 +    tmp_msg->dir = PEP_dir_incoming;
    3.70      // MIME decode message delivers only addresses. We need more.
    3.71      if (tmp_msg->from) {
    3.72          if (!is_me(session, tmp_msg->from))
    3.73              status = update_identity(session, (tmp_msg->from));
    3.74          else
    3.75 -            status = myself(session, tmp_msg->from);
    3.76 +            status = _myself(session, tmp_msg->from, false, false, true);
    3.77  
    3.78          if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    3.79              goto pep_error;
    3.80 @@ -4603,7 +4607,7 @@
    3.81      if (!is_me(session, msg->from))
    3.82          status = update_identity(session, msg->from);
    3.83      else
    3.84 -        status = myself(session, msg->from);
    3.85 +        status = _myself(session, msg->from, false, false, true);
    3.86  
    3.87      switch (status) {
    3.88          case PEP_KEY_NOT_FOUND:
     4.1 --- a/src/pEpEngine.c	Mon Mar 18 15:37:56 2019 +0100
     4.2 +++ b/src/pEpEngine.c	Tue Mar 19 18:19:59 2019 +0100
     4.3 @@ -367,6 +367,17 @@
     4.4      "      where pgp_keypair_fpr = upper(replace(?1,' ',''))"
     4.5      "           and identity.is_own = 1"
     4.6      ");";
     4.7 +    
     4.8 +static const char *sql_is_own_address =
     4.9 +    "select count(*) from ("
    4.10 +    "   select address from identity"
    4.11 +    "       where (case when (address = ?1) then (1)"
    4.12 +    "                   when (lower(address) = lower(?1)) then (1)"
    4.13 +    "                   when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    4.14 +    "                   else 0"
    4.15 +    "           end) = 1 "
    4.16 +    "           and identity.is_own = 1"
    4.17 +    ");";
    4.18  
    4.19  static const char *sql_own_identities_retrieve =  
    4.20      "select address, fpr, username, identity.user_id, "
    4.21 @@ -1268,6 +1279,11 @@
    4.22              (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
    4.23              NULL);
    4.24      assert(int_result == SQLITE_OK);
    4.25 +
    4.26 +    int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
    4.27 +            (int)strlen(sql_is_own_address), &_session->is_own_address,
    4.28 +            NULL);
    4.29 +    assert(int_result == SQLITE_OK);
    4.30      
    4.31      int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
    4.32              (int)strlen(sql_own_identities_retrieve),
    4.33 @@ -1489,6 +1505,8 @@
    4.34                  sqlite3_finalize(session->blacklist_retrieve);
    4.35              if (session->own_key_is_listed)
    4.36                  sqlite3_finalize(session->own_key_is_listed);
    4.37 +            if (session->is_own_address)
    4.38 +                sqlite3_finalize(session->is_own_address);
    4.39              if (session->own_identities_retrieve)
    4.40                  sqlite3_finalize(session->own_identities_retrieve);
    4.41              if (session->own_keys_retrieve)
    4.42 @@ -2722,6 +2740,43 @@
    4.43      return PEP_STATUS_OK;
    4.44  }
    4.45  
    4.46 +DYNAMIC_API PEP_STATUS is_own_address(PEP_SESSION session, pEp_identity *identity, bool* is_own_addr)
    4.47 +{
    4.48 +    assert(session);
    4.49 +    assert(is_own_addr);
    4.50 +    assert(identity);
    4.51 +    assert(!EMPTYSTR(identity->user_id));
    4.52 +
    4.53 +    if (!session || !is_own_addr || !identity || EMPTYSTR(identity->address))
    4.54 +        return PEP_ILLEGAL_VALUE;
    4.55 +    
    4.56 +    *is_own_addr = false;
    4.57 +            
    4.58 +    const char* address = identity->address;
    4.59 +    
    4.60 +    if (!session || EMPTYSTR(address))
    4.61 +        return PEP_ILLEGAL_VALUE;
    4.62 +        
    4.63 +    sqlite3_reset(session->is_own_address);
    4.64 +    sqlite3_bind_text(session->is_own_address, 1, address, -1,
    4.65 +            SQLITE_STATIC);
    4.66 +    int result = sqlite3_step(session->is_own_address);
    4.67 +    switch (result) {
    4.68 +        case SQLITE_ROW: {
    4.69 +            // yeah yeah, I know, we could be lazy here, but it looks bad.
    4.70 +            *is_own_addr = (sqlite3_column_int(session->is_own_address, 0) != 0);
    4.71 +            break;
    4.72 +        }
    4.73 +        default:
    4.74 +            sqlite3_reset(session->is_own_address);
    4.75 +            return PEP_RECORD_NOT_FOUND;
    4.76 +    }
    4.77 +
    4.78 +    sqlite3_reset(session->is_own_address);
    4.79 +    
    4.80 +    return PEP_STATUS_OK;
    4.81 +}
    4.82 +
    4.83  
    4.84  PEP_STATUS remove_fpr_as_default(PEP_SESSION session, 
    4.85                                   const char* fpr) 
     5.1 --- a/src/pEp_internal.h	Mon Mar 18 15:37:56 2019 +0100
     5.2 +++ b/src/pEp_internal.h	Tue Mar 19 18:19:59 2019 +0100
     5.3 @@ -174,6 +174,7 @@
     5.4      
     5.5      // Own keys
     5.6      sqlite3_stmt *own_key_is_listed;
     5.7 +    sqlite3_stmt *is_own_address;
     5.8      sqlite3_stmt *own_identities_retrieve;
     5.9      sqlite3_stmt *own_keys_retrieve;
    5.10      sqlite3_stmt *get_user_default_key;