moving functionality into pEpEngine
authorvb
Fri, 11 Jul 2014 17:43:11 +0200
changeset 826cc9f0228f4
parent 7 d79baf355fcb
child 9 41e66a54f03e
moving functionality into pEpEngine
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEpEngine.h
src/platform_windows.cpp
test/pEpEngineTest.cc
     1.1 --- a/src/keymanagement.c	Fri Jul 11 12:07:26 2014 +0200
     1.2 +++ b/src/keymanagement.c	Fri Jul 11 17:43:11 2014 +0200
     1.3 @@ -17,13 +17,16 @@
     1.4  #define MIN(A, B) ((B) > (A) ? (A) : (B))
     1.5  #endif
     1.6  
     1.7 +#ifndef EMPTY
     1.8 +#define EMPTY(STR) ((STR == NULL) || (STR)[0] == 0)
     1.9 +#endif
    1.10 +
    1.11  DYNAMIC_API PEP_STATUS update_identity(
    1.12          PEP_SESSION session, pEp_identity * identity
    1.13      )
    1.14  {
    1.15      pEp_identity *stored_identity;
    1.16      PEP_STATUS status;
    1.17 -    bool bDirty;
    1.18  
    1.19      assert(session);
    1.20      assert(identity);
    1.21 @@ -35,25 +38,105 @@
    1.22          return PEP_OUT_OF_MEMORY;
    1.23  
    1.24      if (stored_identity) {
    1.25 -        if (identity->username == NULL || identity->username[0] == 0) {
    1.26 +        if (EMPTY(identity->fpr)) {
    1.27 +            identity->comm_type = PEP_ct_unknown;
    1.28 +
    1.29 +            stringlist_t *keylist;
    1.30 +
    1.31 +            status = find_keys(session, stored_identity->fpr, &keylist);
    1.32 +            assert(status != PEP_OUT_OF_MEMORY);
    1.33 +            if (status == PEP_OUT_OF_MEMORY)
    1.34 +                return PEP_OUT_OF_MEMORY;
    1.35 +
    1.36 +            if (keylist && keylist->value) {
    1.37 +                identity->comm_type = stored_identity->comm_type;
    1.38 +            }
    1.39 +
    1.40 +            free_stringlist(keylist);
    1.41 +
    1.42 +            identity->fpr = strdup(stored_identity->fpr);
    1.43 +            assert(identity->fpr);
    1.44 +            if (identity->fpr == NULL)
    1.45 +                return PEP_OUT_OF_MEMORY;
    1.46 +            identity->fpr_size = stored_identity->address_size;
    1.47 +        }
    1.48 +        else /* !EMPTY(identity->fpr) */ {
    1.49 +            stringlist_t *keylist;
    1.50 +
    1.51 +            status = find_keys(session, identity->fpr, &keylist);
    1.52 +            assert(status != PEP_OUT_OF_MEMORY);
    1.53 +            if (status == PEP_OUT_OF_MEMORY)
    1.54 +                return PEP_OUT_OF_MEMORY;
    1.55 +
    1.56 +            if (keylist && keylist->value) {
    1.57 +                if (identity->comm_type == PEP_ct_unknown) {
    1.58 +                    if (strcmp(identity->fpr, stored_identity->fpr) == 0) {
    1.59 +                        identity->comm_type = stored_identity->comm_type;
    1.60 +                    }
    1.61 +                    else {
    1.62 +                        status = get_trust(session, identity);
    1.63 +                        assert(status != PEP_OUT_OF_MEMORY);
    1.64 +                        if (status == PEP_OUT_OF_MEMORY)
    1.65 +                            return PEP_OUT_OF_MEMORY;
    1.66 +                    }
    1.67 +                }
    1.68 +            }
    1.69 +            else
    1.70 +                identity->comm_type = PEP_ct_unknown;
    1.71 +
    1.72 +            free_stringlist(keylist);
    1.73 +        }
    1.74 +
    1.75 +        if (EMPTY(identity->username)) {
    1.76              free(identity->username);
    1.77              identity->username = strdup(stored_identity->username);
    1.78 +            assert(identity->username);
    1.79 +            if (identity->username == NULL)
    1.80 +                return PEP_OUT_OF_MEMORY;
    1.81 +            identity->username_size = stored_identity->username_size;
    1.82          }
    1.83 -        if (identity->user_id == NULL || identity->user_id[0] == 0) {
    1.84 +
    1.85 +        if (EMPTY(identity->user_id)) {
    1.86              free(identity->user_id);
    1.87              identity->user_id = strdup(stored_identity->user_id);
    1.88 +            assert(identity->user_id);
    1.89 +            if (identity->user_id == NULL)
    1.90 +                return PEP_OUT_OF_MEMORY;
    1.91 +            identity->user_id_size = stored_identity->user_id_size;
    1.92          }
    1.93 -        if (identity->fpr != NULL && identity->fpr[0] != 0) {
    1.94 -            if (strcmp(identity->fpr, stored_identity->fpr) != 0)
    1.95 -                identity->comm_type = PEP_ct_unknown;
    1.96 +
    1.97 +        if (identity->lang[0] == 0) {
    1.98 +            identity->lang[0] = stored_identity->lang[0];
    1.99 +            identity->lang[1] = stored_identity->lang[1];
   1.100 +            identity->lang[2] = 0;
   1.101          }
   1.102      }
   1.103 -    else
   1.104 -        identity->comm_type = PEP_ct_unknown;
   1.105 +    else /* stored_identity == NULL */ {
   1.106 +        if (identity->fpr && identity->user_id) {
   1.107 +            if (identity->comm_type == PEP_ct_unknown) {
   1.108 +                status = get_trust(session, identity);
   1.109 +                assert(status != PEP_OUT_OF_MEMORY);
   1.110 +                if (status == PEP_OUT_OF_MEMORY)
   1.111 +                    return PEP_OUT_OF_MEMORY;
   1.112 +            }
   1.113 +            if (identity->comm_type != PEP_ct_unknown && EMPTY(identity->username)) {
   1.114 +                free(identity->username);
   1.115 +                identity->username = strdup("anonymous");
   1.116 +                identity->username_size = 10;
   1.117 +            }
   1.118 +        }
   1.119 +        else
   1.120 +            identity->comm_type = PEP_ct_unknown;
   1.121 +    }
   1.122  
   1.123 -    status = set_identity(session, identity);
   1.124 +    status = PEP_STATUS_OK;
   1.125  
   1.126 -    return PEP_STATUS_OK;
   1.127 +    if (identity->comm_type != PEP_ct_unknown) {
   1.128 +        status = set_identity(session, identity);
   1.129 +        assert(status == PEP_STATUS_OK);
   1.130 +    }
   1.131 +
   1.132 +    return status;
   1.133  }
   1.134  
   1.135  DYNAMIC_API PEP_STATUS outgoing_comm_type(
   1.136 @@ -62,7 +145,6 @@
   1.137          PEP_comm_type *comm_type
   1.138      )
   1.139  {
   1.140 -    int i;
   1.141      const stringlist_t *l;
   1.142  
   1.143      assert(session);
   1.144 @@ -167,6 +249,7 @@
   1.145      free_stringlist(keylist);
   1.146      if (identity->fpr == NULL)
   1.147          return PEP_OUT_OF_MEMORY;
   1.148 +    identity->fpr_size = strlen(identity->fpr);
   1.149  
   1.150      status = set_identity(session, identity);
   1.151      assert(status == PEP_STATUS_OK);
     2.1 --- a/src/keymanagement.h	Fri Jul 11 12:07:26 2014 +0200
     2.2 +++ b/src/keymanagement.h	Fri Jul 11 17:43:11 2014 +0200
     2.3 @@ -13,7 +13,7 @@
     2.4  //      caller must insert the identity into the asynchronous management
     2.5  //      implementation, so retrieve_next_identity() will return this identity
     2.6  //      later
     2.7 -//      at least identity->address must be a valid C string as input
     2.8 +//      at least identity->address must be a valid UTF-8 string as input
     2.9  
    2.10  DYNAMIC_API PEP_STATUS update_identity(
    2.11          PEP_SESSION session, pEp_identity * identity
     3.1 --- a/src/pEpEngine.c	Fri Jul 11 12:07:26 2014 +0200
     3.2 +++ b/src/pEpEngine.c	Fri Jul 11 17:43:11 2014 +0200
     3.3 @@ -130,6 +130,7 @@
     3.4  	sqlite3_stmt *set_pgp_keypair;
     3.5  	sqlite3_stmt *set_identity;
     3.6  	sqlite3_stmt *set_trust;
     3.7 +    sqlite3_stmt *get_trust;
     3.8  
     3.9  	gpgme_check_version_t gpgme_check;
    3.10  	gpgme_set_locale_t gpgme_set_locale;
    3.11 @@ -214,6 +215,8 @@
    3.12  	const char *sql_set_pgp_keypair;
    3.13  	const char *sql_set_identity;
    3.14  	const char *sql_set_trust;
    3.15 +    const char *sql_get_trust;
    3.16 +
    3.17      bool bResult;
    3.18  
    3.19  	assert(sqlite3_threadsafe());
    3.20 @@ -571,6 +574,8 @@
    3.21      sql_set_trust = "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type)"
    3.22                          "values (?1, ?2, ?3) ;";
    3.23  	
    3.24 +    sql_get_trust = "select user_id, comm_type from trust where user_id = ?1 and pgp_keypair_fpr = ?2 ;";
    3.25 +
    3.26      int_result = sqlite3_prepare_v2(_session->db, sql_set_person,
    3.27              strlen(sql_set_person), &_session->set_person, NULL);
    3.28      assert(int_result == SQLITE_OK);
    3.29 @@ -583,6 +588,9 @@
    3.30      int_result = sqlite3_prepare_v2(_session->db, sql_set_trust,
    3.31              strlen(sql_set_trust), &_session->set_trust, NULL);
    3.32  	assert(int_result == SQLITE_OK);
    3.33 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_trust,
    3.34 +            strlen(sql_get_trust), &_session->get_trust, NULL);
    3.35 +    assert(int_result == SQLITE_OK);
    3.36  
    3.37  	sqlite3_reset(_session->log);
    3.38      sqlite3_bind_text(_session->log, 1, "init", -1, SQLITE_STATIC);
    3.39 @@ -1354,6 +1362,7 @@
    3.40  
    3.41  	assert(session);
    3.42  	assert(address);
    3.43 +    assert(address[0]);
    3.44  
    3.45      sqlite3_reset(_session->get_identity);
    3.46      sqlite3_bind_text(_session->get_identity, 1, address, -1, SQLITE_STATIC);
    3.47 @@ -1853,6 +1862,9 @@
    3.48  	pEpSession *_session = (pEpSession *) session;
    3.49  	gpgme_error_t gpgme_error;
    3.50  
    3.51 +    assert(session);
    3.52 +    assert(pattern);
    3.53 +
    3.54      gpgme_error = _session->gpgme_op_export(_session->ctx, pattern,
    3.55              GPGME_EXPORT_MODE_EXTERN, NULL);
    3.56      assert(gpgme_error != GPG_ERR_INV_VALUE);
    3.57 @@ -1867,3 +1879,46 @@
    3.58      free(p);
    3.59  }
    3.60  
    3.61 +DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity)
    3.62 +{
    3.63 +    pEpSession *_session = (pEpSession *) session;
    3.64 +    PEP_STATUS status = PEP_STATUS_OK;
    3.65 +    int result;
    3.66 +
    3.67 +    assert(session);
    3.68 +    assert(identity);
    3.69 +    assert(identity->user_id);
    3.70 +    assert(identity->user_id[0]);
    3.71 +    assert(identity->fpr);
    3.72 +    assert(identity->fpr[0]);
    3.73 +
    3.74 +    identity->comm_type = PEP_ct_unknown;
    3.75 +
    3.76 +    sqlite3_reset(_session->get_trust);
    3.77 +    sqlite3_bind_text(_session->get_trust, 1, identity->user_id, -1, SQLITE_STATIC);
    3.78 +    sqlite3_bind_text(_session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
    3.79 +
    3.80 +    result = sqlite3_step(_session->get_trust);
    3.81 +    switch (result) {
    3.82 +    case SQLITE_ROW: {
    3.83 +        const char * user_id = (const char *) sqlite3_column_text(_session->get_trust, 1);
    3.84 +        int comm_type = (PEP_comm_type) sqlite3_column_int(_session->get_trust, 2);
    3.85 +
    3.86 +        if (strcmp(user_id, identity->user_id) != 0) {
    3.87 +            free(identity->user_id);
    3.88 +            identity->user_id = strdup(user_id);
    3.89 +            assert(identity->user_id);
    3.90 +            if (identity->user_id == NULL)
    3.91 +                return PEP_OUT_OF_MEMORY;
    3.92 +        }
    3.93 +        identity->comm_type = comm_type;
    3.94 +        break;
    3.95 +    }
    3.96 + 
    3.97 +    default:
    3.98 +        status = PEP_CANNOT_FIND_IDENTITY;
    3.99 +    }
   3.100 +
   3.101 +    sqlite3_reset(_session->get_trust);
   3.102 +    return status;
   3.103 +}
   3.104 \ No newline at end of file
     4.1 --- a/src/pEpEngine.h	Fri Jul 11 12:07:26 2014 +0200
     4.2 +++ b/src/pEpEngine.h	Fri Jul 11 17:43:11 2014 +0200
     4.3 @@ -578,6 +578,21 @@
     4.4  
     4.5  DYNAMIC_API void pEp_free(void *p);
     4.6  
     4.7 +
     4.8 +// get_trust() - get the trust level a key has for a person
     4.9 +//
    4.10 +//  parameters:
    4.11 +//      session (in)            session handle
    4.12 +//      identity (inout)        user_id and fpr to check as UTF-8 strings (in)
    4.13 +//                              user_id and comm_type as result (out)
    4.14 +//
    4.15 +//  this function modifies the given identity struct; the struct remains in the
    4.16 +//  ownership of the caller
    4.17 +//  if the trust level cannot be determined identity->comm_type is set to PEP_ct_unknown
    4.18 +
    4.19 +DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity);
    4.20 +
    4.21 +
    4.22  #ifdef __cplusplus
    4.23  }
    4.24  #endif
     5.1 --- a/src/platform_windows.cpp	Fri Jul 11 12:07:26 2014 +0200
     5.2 +++ b/src/platform_windows.cpp	Fri Jul 11 17:43:11 2014 +0200
     5.3 @@ -111,7 +111,7 @@
     5.4              tPath, PATH_BUF_SIZE);
     5.5  	assert(length);
     5.6      if (length == 0)
     5.7 -        throw bad_alloc();
     5.8 +        throw bad_alloc(); // BUG: there are other errors possible beside out of memory
     5.9  
    5.10  	CreateDirectory(tPath, NULL);
    5.11  	DWORD error = GetLastError();
     6.1 --- a/test/pEpEngineTest.cc	Fri Jul 11 12:07:26 2014 +0200
     6.2 +++ b/test/pEpEngineTest.cc	Fri Jul 11 17:43:11 2014 +0200
     6.3 @@ -8,10 +8,6 @@
     6.4  #include "../src/pEpEngine.h"
     6.5  #include "../src/keymanagement.h"
     6.6  
     6.7 -#ifdef _WIN32
     6.8 -#define strdup _strdup
     6.9 -#endif
    6.10 -
    6.11  using namespace std;
    6.12  
    6.13  int main(int argc, char* argv[])
    6.14 @@ -165,10 +161,10 @@
    6.15  	pEp_identity *identity;
    6.16  
    6.17      identity = new_identity(
    6.18 -            strdup("leon.schumacher@digitalekho.com"),
    6.19 -            strdup("8BD08954C74D830EEFFB5DEB2682A17F7C87F73D"),
    6.20 -            strdup("23"),
    6.21 -            strdup("Leon Schumacher")
    6.22 +            "leon.schumacher@digitalekho.com",
    6.23 +            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
    6.24 +            "23",
    6.25 +            "Leon Schumacher"
    6.26          );
    6.27  	identity->comm_type = PEP_ct_pEp;
    6.28  
    6.29 @@ -179,8 +175,15 @@
    6.30  	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
    6.31  	assert(identity);
    6.32  	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
    6.33 +
    6.34 +    PEP_STATUS get_trust_result = get_trust(session, identity);
    6.35 +    assert(get_trust_result == PEP_STATUS_OK);
    6.36 +    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
    6.37 +
    6.38      free_identity(identity);
    6.39  
    6.40 +    // testing key management
    6.41 +
    6.42      stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
    6.43      PEP_comm_type comm_type;
    6.44      cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
    6.45 @@ -200,10 +203,10 @@
    6.46  
    6.47      cout << "\ngenerating key for testuser\n";
    6.48      identity = new_identity(
    6.49 -            strdup("testuser@pibit.ch"),
    6.50 +            "testuser@pibit.ch",
    6.51              NULL,
    6.52 -            strdup("423"),
    6.53 -            strdup("Alfred E. Neuman")
    6.54 +            "423",
    6.55 +            "Alfred E. Neuman"
    6.56          );
    6.57      assert(identity);
    6.58      PEP_STATUS generate_status = generate_keypair(session, identity);