ENGINE-137 added function to get keys already trusted for some user, useful when questioning stored identity's fpr
authorEdouard Tisserant <edouard@pep-project.org>
Wed, 21 Dec 2016 02:02:11 +0100
changeset 1507cee84fad7f33
parent 1506 fc7d96185247
child 1508 ab010a3b7894
ENGINE-137 added function to get keys already trusted for some user, useful when questioning stored identity's fpr
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/pEpEngine.c	Tue Dec 20 22:51:41 2016 +0100
     1.2 +++ b/src/pEpEngine.c	Wed Dec 21 02:02:11 2016 +0100
     1.3 @@ -36,6 +36,7 @@
     1.4      static const char *sql_unset_identity_flags;
     1.5      static const char *sql_set_trust;
     1.6      static const char *sql_get_trust;
     1.7 +    static const char *sql_greater_trust_keys;
     1.8      static const char *sql_least_trust;
     1.9      static const char *sql_mark_as_compromized;
    1.10      static const char *sql_crashdump;
    1.11 @@ -384,6 +385,10 @@
    1.12          sql_get_trust = "select comm_type from trust where user_id = ?1 "
    1.13                          "and pgp_keypair_fpr = upper(replace(?2,' ','')) ;";
    1.14  
    1.15 +        sql_greater_trust_keys = "select pgp_keypair_fpr from trust"
    1.16 +                                 "    where user_id = ?1"
    1.17 +                                 "      and comm_type > ?2;";
    1.18 +
    1.19          sql_least_trust = "select min(comm_type) from trust where pgp_keypair_fpr = upper(replace(?1,' ','')) ;";
    1.20  
    1.21          sql_mark_as_compromized = "update trust not indexed set comm_type = 15"
    1.22 @@ -506,6 +511,11 @@
    1.23              (int)strlen(sql_get_trust), &_session->get_trust, NULL);
    1.24      assert(int_result == SQLITE_OK);
    1.25  
    1.26 +    int_result = sqlite3_prepare_v2(_session->db, sql_greater_trust_keys,
    1.27 +            (int)strlen(sql_greater_trust_keys), &_session->greater_trust_keys,
    1.28 +            NULL);
    1.29 +    assert(int_result == SQLITE_OK);
    1.30 +    
    1.31      int_result = sqlite3_prepare_v2(_session->db, sql_least_trust,
    1.32              (int)strlen(sql_least_trust), &_session->least_trust, NULL);
    1.33      assert(int_result == SQLITE_OK);
    1.34 @@ -676,6 +686,8 @@
    1.35                  sqlite3_finalize(session->set_trust);
    1.36              if (session->get_trust)
    1.37                  sqlite3_finalize(session->get_trust);
    1.38 +            if (session->greater_trust_keys)
    1.39 +                sqlite3_finalize(session->greater_trust_keys);
    1.40              if (session->least_trust)
    1.41                  sqlite3_finalize(session->least_trust);
    1.42              if (session->mark_compromized)
    1.43 @@ -1373,6 +1385,71 @@
    1.44      return status;
    1.45  }
    1.46  
    1.47 +DYNAMIC_API PEP_STATUS greater_trust_keys(
    1.48 +        PEP_SESSION session,
    1.49 +        const char *user_id,
    1.50 +        PEP_comm_type min_comm_type,
    1.51 +        stringlist_t **keylist
    1.52 +    )
    1.53 +{
    1.54 +    PEP_STATUS status = PEP_STATUS_OK;
    1.55 +
    1.56 +    assert(session);
    1.57 +    assert(keylist);
    1.58 +
    1.59 +    if (!(session && keylist))
    1.60 +        return PEP_ILLEGAL_VALUE;
    1.61 +
    1.62 +    *keylist = NULL;
    1.63 +    stringlist_t *_keylist = new_stringlist(NULL);
    1.64 +    if (_keylist == NULL)
    1.65 +        goto enomem;
    1.66 +
    1.67 +    sqlite3_reset(session->greater_trust_keys);
    1.68 +
    1.69 +    sqlite3_bind_text(session->greater_trust_keys, 1, user_id, -1, SQLITE_STATIC);
    1.70 +    sqlite3_bind_int(session->greater_trust_keys, 2, min_comm_type);
    1.71 +    int result;
    1.72 +
    1.73 +    stringlist_t *_bl = _keylist;
    1.74 +    do {
    1.75 +        result = sqlite3_step(session->greater_trust_keys);
    1.76 +        switch (result) {
    1.77 +        case SQLITE_ROW:
    1.78 +        {
    1.79 +            const char *fpr = (const char *) sqlite3_column_text(session->greater_trust_keys, 0);
    1.80 +
    1.81 +            _bl = stringlist_add(_bl, fpr);
    1.82 +            if (_bl == NULL)
    1.83 +                goto enomem;
    1.84 +
    1.85 +            break;
    1.86 +        }
    1.87 +        case SQLITE_DONE:
    1.88 +            break;
    1.89 +
    1.90 +        default:
    1.91 +            status = PEP_UNKNOWN_ERROR;
    1.92 +            result = SQLITE_DONE;
    1.93 +        }
    1.94 +    } while (result != SQLITE_DONE);
    1.95 +
    1.96 +    sqlite3_reset(session->greater_trust_keys);
    1.97 +    if (status == PEP_STATUS_OK)
    1.98 +        *keylist = _keylist;
    1.99 +    else
   1.100 +        free_stringlist(_keylist);
   1.101 +
   1.102 +    goto the_end;
   1.103 +
   1.104 +enomem:
   1.105 +    free_stringlist(_keylist);
   1.106 +    status = PEP_OUT_OF_MEMORY;
   1.107 +
   1.108 +the_end:
   1.109 +    return status;
   1.110 +}
   1.111 +
   1.112  DYNAMIC_API PEP_STATUS least_trust(
   1.113          PEP_SESSION session,
   1.114          const char *fpr,
     2.1 --- a/src/pEpEngine.h	Tue Dec 20 22:51:41 2016 +0100
     2.2 +++ b/src/pEpEngine.h	Wed Dec 21 02:02:11 2016 +0100
     2.3 @@ -805,6 +805,26 @@
     2.4  DYNAMIC_API PEP_STATUS get_trust(PEP_SESSION session, pEp_identity *identity);
     2.5  
     2.6  
     2.7 +// greater_trust_keys() - user_id's keys with trust greater than given trust 
     2.8 +//
     2.9 +//  parameters:
    2.10 +//      session (in)            session handle
    2.11 +//      user_id (in)            UTF-8 string or NULL 
    2.12 +//      min_comm_type (in       key id, user id or address to search for as
    2.13 +//                              UTF-8 string
    2.14 +//      keylist (out)           list of fingerprints found or NULL on error
    2.15 +//
    2.16 +//  caveat:
    2.17 +//        the ownerships of keylist isgoing to the caller
    2.18 +//        the caller must use free_stringlist() to free it
    2.19 +
    2.20 +DYNAMIC_API PEP_STATUS greater_trust_keys(
    2.21 +        PEP_SESSION session,
    2.22 +        const char *user_id,
    2.23 +        PEP_comm_type min_comm_type,
    2.24 +        stringlist_t **keylist
    2.25 +    );
    2.26 +
    2.27  // least_trust() - get the least known trust level for a key in the database
    2.28  //
    2.29  //  parameters:
     3.1 --- a/src/pEp_internal.h	Tue Dec 20 22:51:41 2016 +0100
     3.2 +++ b/src/pEp_internal.h	Wed Dec 21 02:02:11 2016 +0100
     3.3 @@ -103,6 +103,7 @@
     3.4      sqlite3_stmt *unset_identity_flags;
     3.5      sqlite3_stmt *set_trust;
     3.6      sqlite3_stmt *get_trust;
     3.7 +    sqlite3_stmt *greater_trust_keys;
     3.8      sqlite3_stmt *least_trust;
     3.9      sqlite3_stmt *mark_compromized;
    3.10      sqlite3_stmt *reset_trust;