ENGINE-527: sync merge sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 19 Mar 2019 18:38:28 +0100
branchsync
changeset 33471572cabb0cc4
parent 3345 dc801621cafa
parent 3346 278702033186
child 3348 b9c735a0bc94
ENGINE-527: sync merge
src/key_reset.c
src/keymanagement.c
src/keymanagement.h
src/message_api.c
src/pEpEngine.c
src/pEp_internal.h
     1.1 --- a/src/key_reset.c	Tue Mar 19 10:02:55 2019 +0100
     1.2 +++ b/src/key_reset.c	Tue Mar 19 18:38:28 2019 +0100
     1.3 @@ -394,7 +394,7 @@
     1.4                  //
     1.5                  // if (!EMPTYSTR(ident->fpr))
     1.6                  //     fpr_copy = strdup(ident->fpr);
     1.7 -                status = _myself(session, ident, false, true);
     1.8 +                status = _myself(session, ident, false, true, true);
     1.9                  if (status == PEP_STATUS_OK && ident->fpr)
    1.10                      fpr_copy = strdup(ident->fpr);
    1.11                  else {
     2.1 --- a/src/keymanagement.c	Tue Mar 19 10:02:55 2019 +0100
     2.2 +++ b/src/keymanagement.c	Tue Mar 19 18:38:28 2019 +0100
     2.3 @@ -994,7 +994,11 @@
     2.4      return status;
     2.5  }
     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  
    2.15      PEP_STATUS status;
    2.16 @@ -1016,18 +1020,24 @@
    2.17      status = get_default_own_userid(session, &default_own_id);
    2.18  
    2.19      // Deal with non-default user_ids.
    2.20 +    // FIXME: if non-default and read-only, reject totally?
    2.21      if (default_own_id && strcmp(default_own_id, identity->user_id) != 0) {
    2.22 -        
    2.23 -        status = set_userid_alias(session, default_own_id, identity->user_id);
    2.24 -        // Do we want this to be fatal? For now, we'll do it...
    2.25 -        if (status != PEP_STATUS_OK)
    2.26 -            goto pEp_free;
    2.27 -            
    2.28 -        free(identity->user_id);
    2.29 -        identity->user_id = strdup(default_own_id);
    2.30 -        if (identity->user_id == NULL) {
    2.31 -            status = PEP_OUT_OF_MEMORY;
    2.32 -            goto pEp_free;
    2.33 +        if (read_only) {
    2.34 +            free(identity->user_id);
    2.35 +            identity->user_id = strdup(default_own_id);
    2.36 +        }
    2.37 +        else {
    2.38 +            status = set_userid_alias(session, default_own_id, identity->user_id);
    2.39 +            // Do we want this to be fatal? For now, we'll do it...
    2.40 +            if (status != PEP_STATUS_OK)
    2.41 +                goto pEp_free;
    2.42 +                
    2.43 +            free(identity->user_id);
    2.44 +            identity->user_id = strdup(default_own_id);
    2.45 +            if (identity->user_id == NULL) {
    2.46 +                status = PEP_OUT_OF_MEMORY;
    2.47 +                goto pEp_free;
    2.48 +            }
    2.49          }
    2.50      }
    2.51  
    2.52 @@ -1060,7 +1070,7 @@
    2.53      // Set usernames - priority is input username > stored name > address
    2.54      // If there's an input username, we always patch the username with that
    2.55      // input.
    2.56 -    if (EMPTYSTR(identity->username)) {
    2.57 +    if (EMPTYSTR(identity->username) || read_only) {
    2.58          bool stored_uname = (stored_identity && !EMPTYSTR(stored_identity->username));
    2.59          char* uname = (stored_uname ? stored_identity->username : identity->address);
    2.60          free(identity->username);
    2.61 @@ -1113,7 +1123,7 @@
    2.62      
    2.63      // Nothing left to do but generate a key
    2.64      if (!valid_key_found) {
    2.65 -        if (!do_keygen)
    2.66 +        if (!do_keygen || read_only)
    2.67              status = PEP_GET_KEY_FAILED;
    2.68          else {
    2.69  // /            DEBUG_LOG("Generating key pair", "debug", identity->address);
    2.70 @@ -1151,12 +1161,14 @@
    2.71      
    2.72      // We want to set an identity in the DB even if a key isn't found, but we have to preserve the status if
    2.73      // it's NOT ok
    2.74 -    PEP_STATUS set_id_status = set_identity(session, identity);
    2.75 -    if (set_id_status == PEP_STATUS_OK)
    2.76 -        set_id_status = set_as_pEp_user(session, identity);
    2.77 +    if (!read_only) {
    2.78 +        PEP_STATUS set_id_status = set_identity(session, identity);
    2.79 +        if (set_id_status == PEP_STATUS_OK)
    2.80 +            set_id_status = set_as_pEp_user(session, identity);
    2.81  
    2.82 -    status = (status == PEP_STATUS_OK ? set_id_status : status);
    2.83 -
    2.84 +        status = (status == PEP_STATUS_OK ? set_id_status : status);
    2.85 +    }
    2.86 +    
    2.87  pEp_free:    
    2.88      free(default_own_id);
    2.89      free(revoked_fpr);                     
    2.90 @@ -1166,7 +1178,7 @@
    2.91  
    2.92  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
    2.93  {
    2.94 -    return _myself(session, identity, true, false);
    2.95 +    return _myself(session, identity, true, false, false);
    2.96  }
    2.97  
    2.98  DYNAMIC_API PEP_STATUS register_examine_function(
    2.99 @@ -1769,7 +1781,7 @@
   2.100              EMPTYSTR(me->user_id) || EMPTYSTR(me->username))
   2.101          return PEP_ILLEGAL_VALUE;
   2.102  
   2.103 -    status = _myself(session, me, false, true);
   2.104 +    status = _myself(session, me, false, true, false);
   2.105      // we do not need a valid key but dislike other errors
   2.106      if (status != PEP_STATUS_OK && status != PEP_GET_KEY_FAILED && status != PEP_KEY_UNSUITABLE)
   2.107          return status;
     3.1 --- a/src/keymanagement.h	Tue Mar 19 10:02:55 2019 +0100
     3.2 +++ b/src/keymanagement.h	Tue Mar 19 18:38:28 2019 +0100
     3.3 @@ -114,7 +114,11 @@
     3.4  
     3.5  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity);
     3.6  
     3.7 -PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
     3.8 +PEP_STATUS _myself(PEP_SESSION session, 
     3.9 +                   pEp_identity * identity, 
    3.10 +                   bool do_keygen, 
    3.11 +                   bool ignore_flags,
    3.12 +                   bool read_only);
    3.13  
    3.14  // retrieve_next_identity() - callback being called by do_keymanagement()
    3.15  //
    3.16 @@ -375,7 +379,7 @@
    3.17                                   stringlist_t** keys);
    3.18  
    3.19  
    3.20 -PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
    3.21 +//PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen, bool ignore_flags);
    3.22  
    3.23  PEP_STATUS add_mistrusted_key(PEP_SESSION session, const char* fpr);
    3.24  PEP_STATUS delete_mistrusted_key(PEP_SESSION session, const char* fpr);
     4.1 --- a/src/message_api.c	Tue Mar 19 10:02:55 2019 +0100
     4.2 +++ b/src/message_api.c	Tue Mar 19 18:38:28 2019 +0100
     4.3 @@ -995,7 +995,7 @@
     4.4                  }                        
     4.5              }
     4.6              else
     4.7 -                status = myself(session, curr_identity);
     4.8 +                status = _myself(session, curr_identity, false, false, true);
     4.9          if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    4.10              return status;
    4.11          }
    4.12 @@ -1312,7 +1312,8 @@
    4.13      if (!is_me(session, ident))
    4.14          status = update_identity(session, ident);
    4.15      else
    4.16 -        status = myself(session, ident);
    4.17 +        // ???
    4.18 +        status = _myself(session, ident, false, false, true);
    4.19  
    4.20      if (status == PEP_STATUS_OK) {
    4.21          if (ident->comm_type == PEP_ct_compromised)
    4.22 @@ -2978,6 +2979,7 @@
    4.23      return status;
    4.24  }
    4.25  
    4.26 +// FIXME: myself ??????
    4.27  static PEP_STATUS update_sender_to_pEp_trust(
    4.28          PEP_SESSION session, 
    4.29          pEp_identity* sender, 
    4.30 @@ -3308,6 +3310,7 @@
    4.31      stringlist_t *_keylist = NULL;
    4.32      char* signer_fpr = NULL;
    4.33      bool is_pEp_msg = is_a_pEpmessage(src);
    4.34 +    bool myself_read_only = (src->dir == PEP_dir_incoming);
    4.35  
    4.36      // Grab input flags
    4.37      bool reencrypt = (((*flags & PEP_decrypt_flag_untrusted_server) > 0) && *keylist && !EMPTYSTR((*keylist)->value));
    4.38 @@ -3371,7 +3374,7 @@
    4.39          if (!is_me(session, src->from))
    4.40              status = update_identity(session, src->from);
    4.41          else
    4.42 -            status = myself(session, src->from);
    4.43 +            status = _myself(session, src->from, false, false, myself_read_only);
    4.44          
    4.45          // We absolutely should NOT be bailing here unless it's a serious error
    4.46          if (status == PEP_OUT_OF_MEMORY)
    4.47 @@ -3605,7 +3608,7 @@
    4.48                                                  if (!is_me(session, src->from))
    4.49                                                      update_identity(session, (src->from));
    4.50                                                  else
    4.51 -                                                    myself(session, src->from);
    4.52 +                                                    _myself(session, src->from, false, false, myself_read_only);
    4.53                                              }
    4.54                                              break;        
    4.55                                          }
    4.56 @@ -4078,7 +4081,7 @@
    4.57      *rating = PEP_rating_undefined;
    4.58  
    4.59      if (ident->me)
    4.60 -        status = _myself(session, ident, false, true);
    4.61 +        status = _myself(session, ident, false, true, true);
    4.62      else
    4.63          status = update_identity(session, ident);
    4.64  
    4.65 @@ -4476,12 +4479,13 @@
    4.66      if (status != PEP_STATUS_OK)
    4.67          goto pEp_error;
    4.68  
    4.69 +    tmp_msg->dir = PEP_dir_incoming;
    4.70      // MIME decode message delivers only addresses. We need more.
    4.71      if (tmp_msg->from) {
    4.72          if (!is_me(session, tmp_msg->from))
    4.73              status = update_identity(session, (tmp_msg->from));
    4.74          else
    4.75 -            status = myself(session, tmp_msg->from);
    4.76 +            status = _myself(session, tmp_msg->from, false, false, true);
    4.77  
    4.78          if (status == PEP_ILLEGAL_VALUE || status == PEP_OUT_OF_MEMORY)
    4.79              goto pEp_error;
    4.80 @@ -4799,7 +4803,7 @@
    4.81      if (!is_me(session, msg->from))
    4.82          status = update_identity(session, msg->from);
    4.83      else
    4.84 -        status = myself(session, msg->from);
    4.85 +        status = _myself(session, msg->from, false, false, true);
    4.86  
    4.87      switch (status) {
    4.88          case PEP_KEY_NOT_FOUND:
     5.1 --- a/src/pEpEngine.c	Tue Mar 19 10:02:55 2019 +0100
     5.2 +++ b/src/pEpEngine.c	Tue Mar 19 18:38:28 2019 +0100
     5.3 @@ -389,6 +389,17 @@
     5.4      "      where pgp_keypair_fpr = upper(replace(?1,' ',''))"
     5.5      "           and identity.is_own = 1"
     5.6      ");";
     5.7 +    
     5.8 +static const char *sql_is_own_address =
     5.9 +    "select count(*) from ("
    5.10 +    "   select address from identity"
    5.11 +    "       where (case when (address = ?1) then (1)"
    5.12 +    "                   when (lower(address) = lower(?1)) then (1)"
    5.13 +    "                   when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    5.14 +    "                   else 0"
    5.15 +    "           end) = 1 "
    5.16 +    "           and identity.is_own = 1"
    5.17 +    ");";
    5.18  
    5.19  static const char *sql_own_identities_retrieve =  
    5.20      "select address, fpr, identity.user_id, username,"
    5.21 @@ -1446,6 +1457,11 @@
    5.22              (int)strlen(sql_own_key_is_listed), &_session->own_key_is_listed,
    5.23              NULL);
    5.24      assert(int_result == SQLITE_OK);
    5.25 +
    5.26 +    int_result = sqlite3_prepare_v2(_session->db, sql_is_own_address,
    5.27 +            (int)strlen(sql_is_own_address), &_session->is_own_address,
    5.28 +            NULL);
    5.29 +    assert(int_result == SQLITE_OK);
    5.30      
    5.31      int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
    5.32              (int)strlen(sql_own_identities_retrieve),
    5.33 @@ -1680,6 +1696,8 @@
    5.34                  sqlite3_finalize(session->blacklist_retrieve);
    5.35              if (session->own_key_is_listed)
    5.36                  sqlite3_finalize(session->own_key_is_listed);
    5.37 +            if (session->is_own_address)
    5.38 +                sqlite3_finalize(session->is_own_address);
    5.39              if (session->own_identities_retrieve)
    5.40                  sqlite3_finalize(session->own_identities_retrieve);
    5.41              if (session->own_keys_retrieve)
    5.42 @@ -2981,6 +2999,43 @@
    5.43      return PEP_STATUS_OK;
    5.44  }
    5.45  
    5.46 +DYNAMIC_API PEP_STATUS is_own_address(PEP_SESSION session, pEp_identity *identity, bool* is_own_addr)
    5.47 +{
    5.48 +    assert(session);
    5.49 +    assert(is_own_addr);
    5.50 +    assert(identity);
    5.51 +    assert(!EMPTYSTR(identity->user_id));
    5.52 +
    5.53 +    if (!session || !is_own_addr || !identity || EMPTYSTR(identity->address))
    5.54 +        return PEP_ILLEGAL_VALUE;
    5.55 +    
    5.56 +    *is_own_addr = false;
    5.57 +            
    5.58 +    const char* address = identity->address;
    5.59 +    
    5.60 +    if (!session || EMPTYSTR(address))
    5.61 +        return PEP_ILLEGAL_VALUE;
    5.62 +        
    5.63 +    sqlite3_reset(session->is_own_address);
    5.64 +    sqlite3_bind_text(session->is_own_address, 1, address, -1,
    5.65 +            SQLITE_STATIC);
    5.66 +    int result = sqlite3_step(session->is_own_address);
    5.67 +    switch (result) {
    5.68 +        case SQLITE_ROW: {
    5.69 +            // yeah yeah, I know, we could be lazy here, but it looks bad.
    5.70 +            *is_own_addr = (sqlite3_column_int(session->is_own_address, 0) != 0);
    5.71 +            break;
    5.72 +        }
    5.73 +        default:
    5.74 +            sqlite3_reset(session->is_own_address);
    5.75 +            return PEP_RECORD_NOT_FOUND;
    5.76 +    }
    5.77 +
    5.78 +    sqlite3_reset(session->is_own_address);
    5.79 +    
    5.80 +    return PEP_STATUS_OK;
    5.81 +}
    5.82 +
    5.83  PEP_STATUS bind_own_ident_with_contact_ident(PEP_SESSION session,
    5.84                                               pEp_identity* own_ident, 
    5.85                                               pEp_identity* contact_ident) {
     6.1 --- a/src/pEp_internal.h	Tue Mar 19 10:02:55 2019 +0100
     6.2 +++ b/src/pEp_internal.h	Tue Mar 19 18:38:28 2019 +0100
     6.3 @@ -209,6 +209,7 @@
     6.4      
     6.5      // Keys
     6.6      sqlite3_stmt *own_key_is_listed;
     6.7 +    sqlite3_stmt *is_own_address;
     6.8      sqlite3_stmt *own_identities_retrieve;
     6.9      sqlite3_stmt *own_keys_retrieve;
    6.10      sqlite3_stmt *get_user_default_key;