... keysync
authorVolker Birk <vb@pep.foundation>
Mon, 01 Aug 2016 09:14:47 +0200
branchkeysync
changeset 955060272dfaaeb
parent 954 a72e5c669833
child 958 cef602982909
...
src/keymanagement.c
src/keymanagement.h
src/pEpEngine.c
src/pEp_internal.h
src/sync_send_actions.c
sync/gen_actions_skeleton.ysl2
     1.1 --- a/src/keymanagement.c	Sun Jul 31 21:25:22 2016 +0200
     1.2 +++ b/src/keymanagement.c	Mon Aug 01 09:14:47 2016 +0200
     1.3 @@ -659,10 +659,10 @@
     1.4  }
     1.5  
     1.6  DYNAMIC_API PEP_STATUS own_key_is_listed(
     1.7 -                                           PEP_SESSION session,
     1.8 -                                           const char *fpr,
     1.9 -                                           bool *listed
    1.10 -                                           )
    1.11 +        PEP_SESSION session,
    1.12 +        const char *fpr,
    1.13 +        bool *listed
    1.14 +    )
    1.15  {
    1.16      PEP_STATUS status = PEP_STATUS_OK;
    1.17      int count;
    1.18 @@ -695,37 +695,63 @@
    1.19      return status;
    1.20  }
    1.21  
    1.22 -DYNAMIC_API PEP_STATUS own_key_retrieve(
    1.23 -                                          PEP_SESSION session,
    1.24 -                                          stringlist_t **own_key
    1.25 -                                          )
    1.26 +DYNAMIC_API PEP_STATUS own_identities_retrieve(
    1.27 +        PEP_SESSION session,
    1.28 +        identity_list **own_identities
    1.29 +      )
    1.30  {
    1.31      PEP_STATUS status = PEP_STATUS_OK;
    1.32      
    1.33 -    assert(session);
    1.34 -    assert(own_key);
    1.35 -    
    1.36 -    if (!(session && own_key))
    1.37 +    assert(session && own_identities);
    1.38 +    if (!(session && own_identities))
    1.39          return PEP_ILLEGAL_VALUE;
    1.40      
    1.41 -    *own_key = NULL;
    1.42 -    stringlist_t *_own_key = new_stringlist(NULL);
    1.43 -    if (_own_key == NULL)
    1.44 +    *own_identities = NULL;
    1.45 +    identity_list *_own_identities = new_identity_list(NULL);
    1.46 +    if (_own_identities == NULL)
    1.47          goto enomem;
    1.48      
    1.49 -    sqlite3_reset(session->own_key_retrieve);
    1.50 +    sqlite3_reset(session->own_identities_retrieve);
    1.51      
    1.52      int result;
    1.53 +    // address, fpr, username, user_id, comm_type, lang, flags
    1.54 +    const char *address = NULL;
    1.55      const char *fpr = NULL;
    1.56 +    const char *username = NULL;
    1.57 +    const char *user_id = NULL;
    1.58 +    PEP_comm_type comm_type = PEP_ct_unknown;
    1.59 +    const char *lang = NULL;
    1.60 +    unsigned int flags = 0;
    1.61      
    1.62 -    stringlist_t *_bl = _own_key;
    1.63 +    identity_list *_bl = _own_identities;
    1.64      do {
    1.65 -        result = sqlite3_step(session->own_key_retrieve);
    1.66 +        result = sqlite3_step(session->own_identities_retrieve);
    1.67          switch (result) {
    1.68              case SQLITE_ROW:
    1.69 -                fpr = (const char *) sqlite3_column_text(session->own_key_retrieve, 0);
    1.70 -                
    1.71 -                _bl = stringlist_add(_bl, fpr);
    1.72 +                address = (const char *)
    1.73 +                    sqlite3_column_text(session->own_identities_retrieve, 0);
    1.74 +                fpr = (const char *)
    1.75 +                    sqlite3_column_text(session->own_identities_retrieve, 1);
    1.76 +                username = (const char *)
    1.77 +                    sqlite3_column_text(session->own_identities_retrieve, 2);
    1.78 +                user_id = (const char *)
    1.79 +                    sqlite3_column_text(session->own_identities_retrieve, 3);
    1.80 +                comm_type = (PEP_comm_type)
    1.81 +                    sqlite3_column_int(session->own_key_is_listed, 4);
    1.82 +                lang = (const char *)
    1.83 +                    sqlite3_column_text(session->own_identities_retrieve, 5);
    1.84 +                flags = (unsigned int)
    1.85 +                    sqlite3_column_int(session->own_key_is_listed, 6);
    1.86 +
    1.87 +                pEp_identity *ident = new_identity(address, fpr, username, user_id);
    1.88 +                ident->comm_type = comm_type;
    1.89 +                if (lang && lang[0]) {
    1.90 +                    ident->lang[0] = lang[0];
    1.91 +                    ident->lang[1] = lang[1];
    1.92 +                }
    1.93 +                ident->flags = flags;
    1.94 +
    1.95 +                _bl = identity_list_add(_bl, ident);
    1.96                  if (_bl == NULL)
    1.97                      goto enomem;
    1.98                  
    1.99 @@ -740,18 +766,19 @@
   1.100          }
   1.101      } while (result != SQLITE_DONE);
   1.102      
   1.103 -    sqlite3_reset(session->own_key_retrieve);
   1.104 +    sqlite3_reset(session->own_identities_retrieve);
   1.105      if (status == PEP_STATUS_OK)
   1.106 -        *own_key = _own_key;
   1.107 +        *own_identities = _own_identities;
   1.108      else
   1.109 -        free_stringlist(_own_key);
   1.110 +        free_identity_list(_own_identities);
   1.111      
   1.112      goto the_end;
   1.113      
   1.114  enomem:
   1.115 -    free_stringlist(_own_key);
   1.116 +    free_identity_list(_own_identities);
   1.117      status = PEP_OUT_OF_MEMORY;
   1.118      
   1.119  the_end:
   1.120      return status;
   1.121  }
   1.122 +
     2.1 --- a/src/keymanagement.h	Sun Jul 31 21:25:22 2016 +0200
     2.2 +++ b/src/keymanagement.h	Mon Aug 01 09:14:47 2016 +0200
     2.3 @@ -170,18 +170,18 @@
     2.4      );
     2.5  
     2.6  
     2.7 -// own_key_retrieve() - retrieve all own keys fingerprints
     2.8 +// own_identities_retrieve() - retrieve all own identities
     2.9  //
    2.10  //  parameters:
    2.11 -//      session (in)        session to use
    2.12 -//      own_key (out)     copy of own_key
    2.13 +//      session (in)            session to use
    2.14 +//      own_identities (out)    list of own identities
    2.15  //
    2.16  //  caveat:
    2.17  //      the ownership of the copy of own_key goes to the caller
    2.18  
    2.19 -DYNAMIC_API PEP_STATUS own_key_retrieve(
    2.20 +DYNAMIC_API PEP_STATUS own_identities_retrieve(
    2.21          PEP_SESSION session,
    2.22 -        stringlist_t **own_key
    2.23 +        identity_list **own_identities
    2.24      );
    2.25  
    2.26  #ifdef __cplusplus
     3.1 --- a/src/pEpEngine.c	Sun Jul 31 21:25:22 2016 +0200
     3.2 +++ b/src/pEpEngine.c	Mon Aug 01 09:14:47 2016 +0200
     3.3 @@ -47,7 +47,7 @@
     3.4  
     3.5      // Own keys
     3.6      static const char *sql_own_key_is_listed;
     3.7 -    static const char *sql_own_key_retrieve;
     3.8 +    static const char *sql_own_identities_retrieve;
     3.9  
    3.10      // Sequence
    3.11      static const char *sql_sequence_value1;
    3.12 @@ -360,13 +360,14 @@
    3.13                                  "   where main_key_id = upper(replace(?1,' ',''))"
    3.14                                  "    and user_id = '" PEP_OWN_USERID "' );";
    3.15  
    3.16 -        sql_own_key_retrieve = "select main_key_id from person "
    3.17 -                               "  where main_key_id is not null"
    3.18 -                               "   and id = '" PEP_OWN_USERID "' "
    3.19 -                               "union "
    3.20 -                               " select main_key_id from identity "
    3.21 -                               "  where main_key_id is not null"
    3.22 -                               "   and user_id = '" PEP_OWN_USERID "' ;";
    3.23 +        sql_own_identities_retrieve =  "select address, fpr, username, '" PEP_OWN_USERID "',"
    3.24 +                            "   comm_type, lang, identity.flags | pgp_keypair.flags"
    3.25 +                            "   from identity"
    3.26 +                            "   join person on id = identity.user_id"
    3.27 +                            "   join pgp_keypair on fpr = identity.main_key_id"
    3.28 +                            "   join trust on id = trust.user_id"
    3.29 +                            "       and pgp_keypair_fpr = identity.main_key_id"
    3.30 +                            "   where identity.user_id = '" PEP_OWN_USERID "';";
    3.31          
    3.32          sql_sequence_value1 = "insert or replace into sequences (name, value) "
    3.33                                "values (?1, "
    3.34 @@ -471,9 +472,9 @@
    3.35              NULL);
    3.36      assert(int_result == SQLITE_OK);
    3.37      
    3.38 -    int_result = sqlite3_prepare_v2(_session->db, sql_own_key_retrieve,
    3.39 -            (int)strlen(sql_own_key_retrieve), &_session->own_key_retrieve,
    3.40 -            NULL);
    3.41 +    int_result = sqlite3_prepare_v2(_session->db, sql_own_identities_retrieve,
    3.42 +            (int)strlen(sql_own_identities_retrieve),
    3.43 +            &_session->own_identities_retrieve, NULL);
    3.44      assert(int_result == SQLITE_OK);
    3.45   
    3.46      // Sequence
     4.1 --- a/src/pEp_internal.h	Sun Jul 31 21:25:22 2016 +0200
     4.2 +++ b/src/pEp_internal.h	Mon Aug 01 09:14:47 2016 +0200
     4.3 @@ -115,7 +115,7 @@
     4.4      
     4.5      // Own keys
     4.6      sqlite3_stmt *own_key_is_listed;
     4.7 -    sqlite3_stmt *own_key_retrieve;
     4.8 +    sqlite3_stmt *own_identities_retrieve;
     4.9  
    4.10      // sequence value
    4.11      sqlite3_stmt *sequence_value1;
     5.1 --- a/src/sync_send_actions.c	Sun Jul 31 21:25:22 2016 +0200
     5.2 +++ b/src/sync_send_actions.c	Mon Aug 01 09:14:47 2016 +0200
     5.3 @@ -113,6 +113,7 @@
     5.4      free(payload);
     5.5      free_message(_message);
     5.6      free_identity(me);
     5.7 +    free_identity(partner);
     5.8      return status;
     5.9  }
    5.10  
    5.11 @@ -218,6 +219,7 @@
    5.12      free(payload);
    5.13      free_message(_message);
    5.14      free_identity(me);
    5.15 +    free_identity(partner);
    5.16      return status;
    5.17  }
    5.18  
    5.19 @@ -244,6 +246,7 @@
    5.20      char *payload = NULL;
    5.21      message *_message = NULL;
    5.22      pEp_identity *me = NULL;
    5.23 +    identity_list *kl = NULL;
    5.24  
    5.25      assert(session);
    5.26      assert(!partner);
    5.27 @@ -289,6 +292,12 @@
    5.28      if (Identity_from_Struct(me, &msg->header.me) == NULL)
    5.29          goto enomem;
    5.30  
    5.31 +    status = own_identities_retrieve(session, &kl);
    5.32 +    if (status != PEP_STATUS_OK)
    5.33 +        goto error;
    5.34 +    if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
    5.35 +        goto enomem;
    5.36 +
    5.37      if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
    5.38          status = PEP_CONTRAINTS_VIOLATED;
    5.39          goto error;
    5.40 @@ -314,6 +323,7 @@
    5.41      free_message(_message);
    5.42      ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
    5.43      free_identity(partner);
    5.44 +    free_identity_list(kl);
    5.45      return status;
    5.46  
    5.47  enomem:
    5.48 @@ -323,6 +333,8 @@
    5.49      free(payload);
    5.50      free_message(_message);
    5.51      free_identity(me);
    5.52 +    free_identity(partner);
    5.53 +    free_identity_list(kl);
    5.54      return status;
    5.55  }
    5.56  
     6.1 --- a/sync/gen_actions_skeleton.ysl2	Sun Jul 31 21:25:22 2016 +0200
     6.2 +++ b/sync/gen_actions_skeleton.ysl2	Mon Aug 01 09:14:47 2016 +0200
     6.3 @@ -157,6 +157,7 @@
     6.4              char *payload = NULL;
     6.5              message *_message = NULL;
     6.6              pEp_identity *me = NULL;
     6.7 +            `` if "$name='GroupKeys'" |> identity_list *kl = NULL;
     6.8  
     6.9              `` call "paramcheck" with "partner", "parm/partner";
    6.10  
    6.11 @@ -199,13 +200,12 @@
    6.12              if (Identity_from_Struct(me, &msg->header.me) == NULL)
    6.13                  goto enomem;
    6.14          ||
    6.15 -        if "$name='OwnKeys'" {
    6.16 +        if "$name='GroupKeys'" {
    6.17              |
    6.18 -            |> identity_list *kl;
    6.19 -            |> status = own_key_retrieve(session, &kl);
    6.20 +            |> status = own_identities_retrieve(session, &kl);
    6.21              |> if (status != PEP_STATUS_OK)
    6.22              |>> goto error;
    6.23 -            |> if (KeyList_from_stringlist(kl, &msg->keylist) == NULL)
    6.24 +            |> if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
    6.25              |>> goto enomem;
    6.26          }
    6.27          ||
    6.28 @@ -235,7 +235,7 @@
    6.29              free_message(_message);
    6.30              ASN_STRUCT_FREE(asn_DEF_«$name», msg);
    6.31              free_identity(partner);
    6.32 -            `` if "$name='OwnKeys'" |> free_identity_list(kl);
    6.33 +            `` if "$name='GroupKeys'" |> free_identity_list(kl);
    6.34              return status;
    6.35  
    6.36          enomem:
    6.37 @@ -245,7 +245,8 @@
    6.38              free(payload);
    6.39              free_message(_message);
    6.40              free_identity(me);
    6.41 -            `` if "$name='OwnKeys'" |> free_identity_list(kl);
    6.42 +            free_identity(partner);
    6.43 +            `` if "$name='GroupKeys'" |> free_identity_list(kl);
    6.44              return status;
    6.45          }
    6.46