fixes keysync
authorVolker Birk <vb@pep.foundation>
Mon, 29 Aug 2016 17:39:35 +0200
branchkeysync
changeset 10910571ce4b97a2
parent 1090 c15ef7d00170
child 1092 222705a14f1a
fixes
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
src/sync.c
src/sync_impl.c
src/sync_impl.h
src/sync_send_actions.c
sync/gen_actions_skeleton.ysl2
     1.1 --- a/src/pEpEngine.c	Mon Aug 29 15:02:16 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Aug 29 17:39:35 2016 +0200
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "sync_fsm.h"
     1.5  
     1.6  static int init_count = -1;
     1.7 +char sync_uuid[37];
     1.8  
     1.9  static int user_version(void *_version, int count, char **text, char **name)
    1.10  {
    1.11 @@ -132,6 +133,8 @@
    1.12  #define _DDL_USER_VERSION "3"
    1.13  
    1.14      if (in_first) {
    1.15 +        memset(sync_uuid, 0, 37);
    1.16 +
    1.17          int_result = sqlite3_exec(
    1.18              _session->db,
    1.19                  "create table if not exists version_info (\n"
    1.20 @@ -385,7 +388,7 @@
    1.21                                "(select coalesce((select value + 1 from sequences "
    1.22                                "where name = ?1), 1 )), ?2) ; ";
    1.23          sql_sequence_value2 = "select value, own from sequences where name = ?1 ;";
    1.24 -        sql_sequence_value3 = "update sequences set value = ?2 where name = ?1 ;";
    1.25 +        sql_sequence_value3 = "update sequences set value = ?2, own = ?3 where name = ?1 ;";
    1.26          
    1.27          sql_set_revoked =     "insert or replace into revoked_keys ("
    1.28                                "    revoked_fpr, replacement_fpr, revocation_date) "
    1.29 @@ -1727,6 +1730,9 @@
    1.30                  status = PEP_OWN_SEQUENCE;
    1.31              break;
    1.32          }
    1.33 +        case SQLITE_DONE:
    1.34 +            status = PEP_RECORD_NOT_FOUND;
    1.35 +            break;
    1.36          default:
    1.37              status = PEP_UNKNOWN_ERROR;
    1.38      }
    1.39 @@ -1755,7 +1761,7 @@
    1.40  }
    1.41  
    1.42  static PEP_STATUS _set_sequence_value(PEP_SESSION session,
    1.43 -        const char *name, int32_t value)
    1.44 +        const char *name, int32_t value, int own)
    1.45  {
    1.46      assert(session && name && value > 0);
    1.47      if (!(session && name && value > 0))
    1.48 @@ -1764,6 +1770,7 @@
    1.49      sqlite3_reset(session->sequence_value3);
    1.50      sqlite3_bind_text(session->sequence_value3, 1, name, -1, SQLITE_STATIC);
    1.51      sqlite3_bind_int(session->sequence_value3, 2, value);
    1.52 +    sqlite3_bind_int(session->sequence_value3, 3, own);
    1.53      int result = sqlite3_step(session->sequence_value3);
    1.54      assert(result == SQLITE_DONE);
    1.55      sqlite3_reset(session->sequence_value3);
    1.56 @@ -1794,18 +1801,22 @@
    1.57          uuid_unparse_upper(uuid, name);
    1.58          own = 1;
    1.59      }
    1.60 +    else {
    1.61 +        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
    1.62 +            own = 1;
    1.63 +    }
    1.64  
    1.65      if (*value) {
    1.66          int32_t old_value = 0;
    1.67          status = _get_sequence_value(session, name, &old_value);
    1.68 -        if (status != PEP_STATUS_OK)
    1.69 +        if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
    1.70              return status;
    1.71  
    1.72          if (old_value >= *value) {
    1.73              return PEP_SEQUENCE_VIOLATED;
    1.74          }
    1.75          else {
    1.76 -            status = _set_sequence_value(session, name, *value);
    1.77 +            status = _set_sequence_value(session, name, *value, own);
    1.78              return status;
    1.79          }
    1.80      }
     2.1 --- a/src/pEpEngine.h	Mon Aug 29 15:02:16 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Mon Aug 29 17:39:35 2016 +0200
     2.3 @@ -89,6 +89,7 @@
     2.4      PEP_COMMIT_FAILED                               = 0xff01,
     2.5      PEP_MESSAGE_CONSUMED                            = 0xff02,
     2.6  
     2.7 +    PEP_RECORD_NOT_FOUND                            = -6,
     2.8      PEP_CANNOT_CREATE_TEMP_FILE                     = -5,
     2.9      PEP_ILLEGAL_VALUE                               = -4,
    2.10      PEP_BUFFER_TOO_SMALL                            = -3,
     3.1 --- a/src/pEp_internal.h	Mon Aug 29 15:02:16 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Mon Aug 29 17:39:35 2016 +0200
     3.3 @@ -77,6 +77,8 @@
     3.4  
     3.5  #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
     3.6  
     3.7 +extern char sync_uuid[37];
     3.8 +
     3.9  typedef struct _pEpSession {
    3.10      const char *version;
    3.11  #ifdef USE_GPG
    3.12 @@ -137,7 +139,6 @@
    3.13  
    3.14      // state machines
    3.15      DeviceState_state sync_state;
    3.16 -    char sync_uuid[37];
    3.17  
    3.18      // runtime config
    3.19  
     4.1 --- a/src/sync.c	Mon Aug 29 15:02:16 2016 +0200
     4.2 +++ b/src/sync.c	Mon Aug 29 17:39:35 2016 +0200
     4.3 @@ -21,6 +21,10 @@
     4.4          retrieve_next_sync_msg_t retrieve_next_sync_msg
     4.5      )
     4.6  {
     4.7 +    unsigned char uuid[16];
     4.8 +    uuid_generate_random(uuid);
     4.9 +    uuid_unparse_upper(uuid, sync_uuid);
    4.10 +
    4.11      session->sync_obj = obj;
    4.12      session->messageToSend = messageToSend;
    4.13      session->showHandshake = showHandshake;
     5.1 --- a/src/sync_impl.c	Mon Aug 29 15:02:16 2016 +0200
     5.2 +++ b/src/sync_impl.c	Mon Aug 29 17:39:35 2016 +0200
     5.3 @@ -70,14 +70,22 @@
     5.4          if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp") == 0
     5.5                  && bl->size) {
     5.6              DeviceGroup_Protocol_t *msg = NULL;
     5.7 -            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol,
     5.8 -                    (void **) &msg, bl->value, bl->size);
     5.9 +            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **)
    5.10 +                    &msg, bl->value, bl->size);
    5.11              if (msg) {
    5.12                  found = true;
    5.13  
    5.14                  int32_t value = (int32_t) msg->header.sequence;
    5.15 -                PEP_STATUS status = sequence_value(session, (char *)
    5.16 -                        msg->header.me.user_id, &value);
    5.17 +                char *user_id = strndup((char *) msg->header.me.user_id->buf,
    5.18 +                        msg->header.me.user_id->size);
    5.19 +                assert(user_id);
    5.20 +                if (!user_id) {
    5.21 +                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    5.22 +                    return PEP_OUT_OF_MEMORY;
    5.23 +                }
    5.24 +
    5.25 +                PEP_STATUS status = sequence_value(session, (char *) user_id,
    5.26 +                        &value);
    5.27  
    5.28                  if (status == PEP_STATUS_OK) {
    5.29                      status = session->inject_sync_msg(msg, session->sync_obj);
    5.30 @@ -122,7 +130,7 @@
    5.31  
    5.32  PEP_STATUS unicast_msg(
    5.33          PEP_SESSION session,
    5.34 -        Identity partner,
    5.35 +        const Identity partner,
    5.36          DeviceState_state state,
    5.37          DeviceGroup_Protocol_t *msg
    5.38      )
    5.39 @@ -152,7 +160,7 @@
    5.40      
    5.41      int32_t seq = 0;
    5.42  
    5.43 -    status = sequence_value(session, session->sync_uuid, &seq);
    5.44 +    status = sequence_value(session, sync_uuid, &seq);
    5.45      if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
    5.46          goto error;
    5.47  
    5.48 @@ -163,7 +171,7 @@
    5.49          goto enomem;
    5.50  
    5.51      free(_me->user_id);
    5.52 -    _me->user_id = strndup(session->sync_uuid, 37);
    5.53 +    _me->user_id = strndup(sync_uuid, 36);
    5.54      assert(_me->user_id);
    5.55      if (!_me->user_id)
    5.56          goto enomem;
    5.57 @@ -200,8 +208,6 @@
    5.58      payload = NULL;
    5.59      free_identity(me);
    5.60      me = NULL;
    5.61 -    free_identity(partner);
    5.62 -    partner = NULL;
    5.63  
    5.64      message *_encrypted = NULL;
    5.65      status = encrypt_message(session, _message, NULL, &_encrypted, PEP_enc_PEP, 0);
    5.66 @@ -220,7 +226,6 @@
    5.67      free(payload);
    5.68      free_message(_message);
    5.69      free_identity(me);
    5.70 -    free_identity(partner);
    5.71      return status;
    5.72  }
    5.73  
    5.74 @@ -242,9 +247,7 @@
    5.75          return status;
    5.76  
    5.77      for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
    5.78 -        pEp_identity *me = identity_dup(_i->ident);
    5.79 -        if (!me)
    5.80 -            goto enomem;
    5.81 +        pEp_identity *me = _i->ident;
    5.82  
    5.83          // FIXME: no deep copy for multicast supported yet
    5.84          DeviceGroup_Protocol_t *_msg = malloc(sizeof(DeviceGroup_Protocol_t));
     6.1 --- a/src/sync_impl.h	Mon Aug 29 15:02:16 2016 +0200
     6.2 +++ b/src/sync_impl.h	Mon Aug 29 17:39:35 2016 +0200
     6.3 @@ -19,7 +19,7 @@
     6.4  
     6.5  PEP_STATUS unicast_msg(
     6.6          PEP_SESSION session,
     6.7 -        Identity partner,
     6.8 +        const Identity partner,
     6.9          DeviceState_state state,
    6.10          DeviceGroup_Protocol_t *msg
    6.11      );
     7.1 --- a/src/sync_send_actions.c	Mon Aug 29 15:02:16 2016 +0200
     7.2 +++ b/src/sync_send_actions.c	Mon Aug 29 17:39:35 2016 +0200
     7.3 @@ -46,12 +46,13 @@
     7.4          goto error;
     7.5  
     7.6      free_DeviceGroup_Protocol_msg(msg);
     7.7 +    free_identity(partner);
     7.8      return PEP_STATUS_OK;
     7.9  
    7.10  enomem:
    7.11      status = PEP_OUT_OF_MEMORY;
    7.12  error:
    7.13 -    free(partner);
    7.14 +    free_identity(partner);
    7.15      free_DeviceGroup_Protocol_msg(msg);
    7.16      return status;
    7.17  }
    7.18 @@ -89,12 +90,13 @@
    7.19          goto error;
    7.20  
    7.21      free_DeviceGroup_Protocol_msg(msg);
    7.22 +    free_identity(partner);
    7.23      return PEP_STATUS_OK;
    7.24  
    7.25  enomem:
    7.26      status = PEP_OUT_OF_MEMORY;
    7.27  error:
    7.28 -    free(partner);
    7.29 +    free_identity(partner);
    7.30      free_DeviceGroup_Protocol_msg(msg);
    7.31      return status;
    7.32  }
    7.33 @@ -140,12 +142,13 @@
    7.34  
    7.35      free_identity_list(kl);
    7.36      free_DeviceGroup_Protocol_msg(msg);
    7.37 +    free_identity(partner);
    7.38      return PEP_STATUS_OK;
    7.39  
    7.40  enomem:
    7.41      status = PEP_OUT_OF_MEMORY;
    7.42  error:
    7.43 -    free(partner);
    7.44 +    free_identity(partner);
    7.45      free_DeviceGroup_Protocol_msg(msg);
    7.46      free_identity_list(kl);
    7.47      return status;
     8.1 --- a/sync/gen_actions_skeleton.ysl2	Mon Aug 29 15:02:16 2016 +0200
     8.2 +++ b/sync/gen_actions_skeleton.ysl2	Mon Aug 29 17:39:35 2016 +0200
     8.3 @@ -193,12 +193,13 @@
     8.4  
     8.5              `` if "$name='GroupKeys'" |> free_identity_list(kl);
     8.6              free_DeviceGroup_Protocol_msg(msg);
     8.7 +            free_identity(partner);
     8.8              return PEP_STATUS_OK;
     8.9  
    8.10          enomem:
    8.11              status = PEP_OUT_OF_MEMORY;
    8.12          error:
    8.13 -            free(partner);
    8.14 +            free_identity(partner);
    8.15              free_DeviceGroup_Protocol_msg(msg);
    8.16              `` if "$name='GroupKeys'" |> free_identity_list(kl);
    8.17              return status;