ENGINE-293: initialise_own_identities (cheap myself() only keygen/retrieval) + additional internal fprless get_identity function added so that temporary identities w/o fpr can be retrieved ENGINE-293
authorKrista Bennett <krista@pep-project.org>
Sat, 09 Dec 2017 13:04:40 +0100
branchENGINE-293
changeset 2308b7ef1c9005ae
parent 2307 c33ec7f88dd9
child 2310 f0aeda5e1ce9
child 2716 b74d2ea743a5
ENGINE-293: initialise_own_identities (cheap myself() only keygen/retrieval) + additional internal fprless get_identity function added so that temporary identities w/o fpr can be retrieved
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
test/case_and_dot_address_test.cc
     1.1 --- a/src/keymanagement.c	Thu Dec 07 09:31:47 2017 +0100
     1.2 +++ b/src/keymanagement.c	Sat Dec 09 13:04:40 2017 +0100
     1.3 @@ -437,6 +437,7 @@
     1.4      char* own_id = NULL;
     1.5      status = get_own_userid(session, &own_id);
     1.6  
     1.7 +
     1.8      assert(EMPTYSTR(identity->user_id) ||
     1.9             (own_id && strcmp(identity->user_id, own_id) == 0) ||
    1.10             !own_id);
    1.11 @@ -447,6 +448,11 @@
    1.12               !own_id)))
    1.13          return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.14  
    1.15 +    if (!own_id) {
    1.16 +        // check to see if we have ANY identity for this address... could have
    1.17 +        // happened due to race condition
    1.18 +    }
    1.19 +    
    1.20      identity->comm_type = PEP_ct_pEp;
    1.21      identity->me = true;
    1.22      if(ignore_flags)
    1.23 @@ -635,6 +641,45 @@
    1.24      return ADD_TO_LOG(PEP_STATUS_OK);
    1.25  }
    1.26  
    1.27 +DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
    1.28 +                                                 identity_list* my_idents) {
    1.29 +    PEP_STATUS status = PEP_STATUS_OK;
    1.30 +    if (!session)
    1.31 +        return PEP_ILLEGAL_VALUE;
    1.32 +        
    1.33 +    char* stored_own_userid = NULL;
    1.34 +    get_own_userid(session, &stored_own_userid);
    1.35 +    
    1.36 +    identity_list* ident_curr = my_idents;
    1.37 +    while (ident_curr) {
    1.38 +        pEp_identity* ident = ident_curr->ident;
    1.39 +        if (!ident)
    1.40 +            return PEP_ILLEGAL_VALUE;
    1.41 +            
    1.42 +        if (stored_own_userid) {
    1.43 +            if (!ident->user_id) 
    1.44 +                ident->user_id = strdup(stored_own_userid);
    1.45 +            else if (strcmp(stored_own_userid, ident->user_id) != 0)
    1.46 +                return PEP_ILLEGAL_VALUE;
    1.47 +        }
    1.48 +        else if (!ident->user_id) {
    1.49 +            stored_own_userid = PEP_OWN_USERID;
    1.50 +            ident->user_id = strdup(PEP_OWN_USERID);
    1.51 +        }
    1.52 +        
    1.53 +        ident->me = true; // Just in case.
    1.54 +        
    1.55 +        // Ok, do it...
    1.56 +        status = set_identity(session, ident);
    1.57 +        if (status != PEP_STATUS_OK)
    1.58 +            return status;
    1.59 +        
    1.60 +        ident_curr = ident_curr->next;
    1.61 +    }
    1.62 +    
    1.63 +    return status;
    1.64 +}
    1.65 +
    1.66  DYNAMIC_API PEP_STATUS myself(PEP_SESSION session, pEp_identity * identity)
    1.67  {
    1.68      return ADD_TO_LOG(_myself(session, identity, true, false));
     2.1 --- a/src/keymanagement.h	Thu Dec 07 09:31:47 2017 +0100
     2.2 +++ b/src/keymanagement.h	Sat Dec 09 13:04:40 2017 +0100
     2.3 @@ -34,6 +34,8 @@
     2.4          PEP_SESSION session, pEp_identity * identity
     2.5      );
     2.6  
     2.7 +DYNAMIC_API PEP_STATUS initialise_own_identities(PEP_SESSION session,
     2.8 +                                                 identity_list* my_idents);
     2.9  
    2.10  // myself() - ensures that an own identity is complete
    2.11  //
     3.1 --- a/src/pEpEngine.c	Thu Dec 07 09:31:47 2017 +0100
     3.2 +++ b/src/pEpEngine.c	Sat Dec 09 13:04:40 2017 +0100
     3.3 @@ -47,7 +47,6 @@
     3.4      "select id, word from wordlist where lang = lower(?1) "
     3.5      "and id = ?2 ;";
     3.6  
     3.7 -
     3.8  static const char *sql_get_identity =  
     3.9      "select fpr, username, comm_type, lang,"
    3.10      "   identity.flags | pgp_keypair.flags,"
    3.11 @@ -64,6 +63,18 @@
    3.12      "          end) = 1"
    3.13      "   and identity.user_id = ?2;";
    3.14  
    3.15 +static const char *sql_get_identity_without_fpr =  
    3.16 +    "select main_key_id, username, comm_type, lang,"
    3.17 +    "   identity.flags, is_own"
    3.18 +    "   from identity"
    3.19 +    "   join person on id = identity.user_id"
    3.20 +    "   where (case when (address = ?1) then (1)"
    3.21 +    "               when (lower(address) = lower(?1)) then (1)"
    3.22 +    "               when (replace(lower(address),'.','') = replace(lower(?1),'.','')) then (1)"
    3.23 +    "               else 0"
    3.24 +    "          end) = 1"
    3.25 +    "   and identity.user_id = ?2;";
    3.26 +
    3.27  static const char *sql_replace_identities_fpr =  
    3.28      "update identity"
    3.29      "   set main_key_id = ?1 "
    3.30 @@ -692,6 +703,11 @@
    3.31              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
    3.32      assert(int_result == SQLITE_OK);
    3.33  
    3.34 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_identity_without_fpr,
    3.35 +            (int)strlen(sql_get_identity_without_fpr), 
    3.36 +            &_session->get_identity_without_fpr, NULL);
    3.37 +    assert(int_result == SQLITE_OK);
    3.38 +
    3.39      int_result = sqlite3_prepare_v2(_session->db, sql_get_own_userid,
    3.40              (int)strlen(sql_get_own_userid), &_session->get_own_userid, NULL);
    3.41      assert(int_result == SQLITE_OK);
    3.42 @@ -937,6 +953,8 @@
    3.43                  sqlite3_finalize(session->trustword);
    3.44              if (session->get_identity)
    3.45                  sqlite3_finalize(session->get_identity);
    3.46 +            if (session->get_identity_without_fpr)
    3.47 +                sqlite3_finalize(session->get_identity_without_fpr);    
    3.48              if (session->get_own_userid)
    3.49                  sqlite3_finalize(session->get_own_userid);
    3.50              if (session->replace_identities_fpr)
    3.51 @@ -1350,11 +1368,13 @@
    3.52      return status;
    3.53  }
    3.54  
    3.55 -DYNAMIC_API PEP_STATUS get_identity(
    3.56 +
    3.57 +static PEP_STATUS _get_identity_internal(
    3.58          PEP_SESSION session,
    3.59          const char *address,
    3.60          const char *user_id,
    3.61 -        pEp_identity **identity
    3.62 +        pEp_identity **identity,
    3.63 +        sqlite3_stmt* get_id_stmt
    3.64      )
    3.65  {
    3.66      PEP_STATUS status = PEP_STATUS_OK;
    3.67 @@ -1370,27 +1390,27 @@
    3.68  
    3.69      *identity = NULL;
    3.70  
    3.71 -    sqlite3_reset(session->get_identity);
    3.72 -    sqlite3_bind_text(session->get_identity, 1, address, -1, SQLITE_STATIC);
    3.73 -    sqlite3_bind_text(session->get_identity, 2, user_id, -1, SQLITE_STATIC);
    3.74 -
    3.75 -    const int result = sqlite3_step(session->get_identity);
    3.76 +    sqlite3_reset(get_id_stmt);
    3.77 +    sqlite3_bind_text(get_id_stmt, 1, address, -1, SQLITE_STATIC);
    3.78 +    sqlite3_bind_text(get_id_stmt, 2, user_id, -1, SQLITE_STATIC);
    3.79 +
    3.80 +    const int result = sqlite3_step(get_id_stmt);
    3.81      switch (result) {
    3.82      case SQLITE_ROW:
    3.83          _identity = new_identity(
    3.84                  address,
    3.85 -                (const char *) sqlite3_column_text(session->get_identity, 0),
    3.86 +                (const char *) sqlite3_column_text(get_id_stmt, 0),
    3.87                  user_id,
    3.88 -                (const char *) sqlite3_column_text(session->get_identity, 1)
    3.89 +                (const char *) sqlite3_column_text(get_id_stmt, 1)
    3.90                  );
    3.91          assert(_identity);
    3.92          if (_identity == NULL)
    3.93              return PEP_OUT_OF_MEMORY;
    3.94  
    3.95          _identity->comm_type = (PEP_comm_type)
    3.96 -            sqlite3_column_int(session->get_identity, 2);
    3.97 +            sqlite3_column_int(get_id_stmt, 2);
    3.98          const char* const _lang = (const char *)
    3.99 -            sqlite3_column_text(session->get_identity, 3);
   3.100 +            sqlite3_column_text(get_id_stmt, 3);
   3.101          if (_lang && _lang[0]) {
   3.102              assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   3.103              assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   3.104 @@ -1400,9 +1420,9 @@
   3.105              _identity->lang[2] = 0;
   3.106          }
   3.107          _identity->flags = (unsigned int)
   3.108 -            sqlite3_column_int(session->get_identity, 4);
   3.109 +            sqlite3_column_int(get_id_stmt, 4);
   3.110          _identity->me = (unsigned int)
   3.111 -            sqlite3_column_int(session->get_identity, 5);
   3.112 +            sqlite3_column_int(get_id_stmt, 5);
   3.113      
   3.114          *identity = _identity;
   3.115          break;
   3.116 @@ -1411,10 +1431,33 @@
   3.117          *identity = NULL;
   3.118      }
   3.119  
   3.120 -    sqlite3_reset(session->get_identity);
   3.121 +    sqlite3_reset(get_id_stmt);
   3.122      return status;
   3.123  }
   3.124  
   3.125 +DYNAMIC_API PEP_STATUS get_identity(
   3.126 +        PEP_SESSION session,
   3.127 +        const char *address,
   3.128 +        const char *user_id,
   3.129 +        pEp_identity **identity
   3.130 +    )
   3.131 +{
   3.132 +    return _get_identity_internal(session, address, user_id, identity,
   3.133 +                                  session->get_identity);
   3.134 +}
   3.135 +
   3.136 +PEP_STATUS get_identity_without_fpr(
   3.137 +        PEP_SESSION session,
   3.138 +        const char *address,
   3.139 +        const char *user_id,
   3.140 +        pEp_identity **identity
   3.141 +    )
   3.142 +{
   3.143 +    return _get_identity_internal(session, address, user_id, identity,
   3.144 +                                  session->get_identity_without_fpr);
   3.145 +}
   3.146 +
   3.147 +
   3.148  DYNAMIC_API PEP_STATUS set_identity(
   3.149          PEP_SESSION session, const pEp_identity *identity
   3.150      )
     4.1 --- a/src/pEpEngine.h	Thu Dec 07 09:31:47 2017 +0100
     4.2 +++ b/src/pEpEngine.h	Sat Dec 09 13:04:40 2017 +0100
     4.3 @@ -1149,6 +1149,16 @@
     4.4  
     4.5  DYNAMIC_API PEP_STATUS reset_peptest_hack(PEP_SESSION session);
     4.6  
     4.7 +// This is used internally when there is a temporary identity to be retrieved
     4.8 +// that may not yet have an FPR attached. See get_identity() for functionality,
     4.9 +// params and caveats.
    4.10 +PEP_STATUS get_identity_without_fpr(
    4.11 +        PEP_SESSION session,
    4.12 +        const char *address,
    4.13 +        const char *user_id,
    4.14 +        pEp_identity **identity
    4.15 +    );
    4.16 +    
    4.17  #ifdef __cplusplus
    4.18  }
    4.19  #endif
     5.1 --- a/src/pEp_internal.h	Thu Dec 07 09:31:47 2017 +0100
     5.2 +++ b/src/pEp_internal.h	Sat Dec 09 13:04:40 2017 +0100
     5.3 @@ -124,6 +124,7 @@
     5.4      sqlite3_stmt *log;
     5.5      sqlite3_stmt *trustword;
     5.6      sqlite3_stmt *get_identity;
     5.7 +    sqlite3_stmt *get_identity_without_fpr;    
     5.8      sqlite3_stmt *replace_identities_fpr;
     5.9      sqlite3_stmt *set_person;
    5.10      sqlite3_stmt *set_device_group;
     6.1 --- a/test/case_and_dot_address_test.cc	Thu Dec 07 09:31:47 2017 +0100
     6.2 +++ b/test/case_and_dot_address_test.cc	Sat Dec 09 13:04:40 2017 +0100
     6.3 @@ -39,36 +39,42 @@
     6.4      assert(statuspub == PEP_STATUS_OK);
     6.5      assert(statuspriv == PEP_STATUS_OK);
     6.6  
     6.7 -    pEp_identity * alice_id = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
     6.8 -    status = update_identity(session, alice_id);
     6.9 -    assert(alice_id->fpr);
    6.10 -    assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.11 +    pEp_identity * alice_id = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
    6.12 +    identity_list* own_id = new_identity_list(alice_id);
    6.13 +    status = initialise_own_identities(session, own_id);
    6.14 +    status = trust_personal_key(session, alice_id);
    6.15 +    pEp_identity * new_alice_id = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
    6.16 +    status = update_identity(session, new_alice_id);
    6.17 +    assert(new_alice_id->fpr);
    6.18 +    assert(strcmp(new_alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.19 +    free_identity(new_alice_id);
    6.20      free_identity(alice_id);
    6.21      alice_id = NULL;
    6.22 +    new_alice_id = NULL;
    6.23  
    6.24      alice_id = new_identity(alice_email_case, NULL, PEP_OWN_USERID, "Alice Test");
    6.25 -    status = update_identity(session, alice_id);
    6.26 +    status = myself(session, alice_id);
    6.27      assert(alice_id->fpr);
    6.28      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.29      free_identity(alice_id);
    6.30      alice_id = NULL;
    6.31  
    6.32      alice_id = new_identity(alice_email_dot, NULL, PEP_OWN_USERID, "Alice Test");
    6.33 -    status = update_identity(session, alice_id);
    6.34 +    status = myself(session, alice_id);
    6.35      assert(alice_id->fpr);
    6.36      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.37      free_identity(alice_id);
    6.38      alice_id = NULL;
    6.39  
    6.40      alice_id = new_identity(alice_email_dotless, NULL, PEP_OWN_USERID, "Alice Test");
    6.41 -    status = update_identity(session, alice_id);
    6.42 +    status = myself(session, alice_id);
    6.43      assert(alice_id->fpr);
    6.44      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.45      free_identity(alice_id);
    6.46      alice_id = NULL;
    6.47  
    6.48      alice_id = new_identity(alice_email_case_and_dot, NULL, PEP_OWN_USERID, "Alice Test");
    6.49 -    status = update_identity(session, alice_id);
    6.50 +    status = myself(session, alice_id);
    6.51      assert(alice_id->fpr);
    6.52      assert(strcmp(alice_id->fpr, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97") == 0);
    6.53      free_identity(alice_id);