merge ENGINE-129
authorKrista Grothoff <krista@pep-project.org>
Thu, 27 Oct 2016 21:57:24 +0200
branchENGINE-129
changeset 1344bdde3e670d9d
parent 1343 59f014327d4a
parent 1340 276625d4f083
child 1345 3c27d945fe92
child 1346 3653950e729d
merge
     1.1 --- a/src/sync_impl.c	Thu Oct 27 21:56:46 2016 +0200
     1.2 +++ b/src/sync_impl.c	Thu Oct 27 21:57:24 2016 +0200
     1.3 @@ -42,6 +42,7 @@
     1.4      if (!(session && sync_msg))
     1.5          return PEP_ILLEGAL_VALUE;
     1.6  
     1.7 +    bool msgIsFromGroup = false;
     1.8      if(sync_msg->is_a_message){
     1.9          DeviceGroup_Protocol_t *msg = sync_msg->u.message;
    1.10          assert(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
    1.11 @@ -50,6 +51,8 @@
    1.12              goto error;
    1.13          }
    1.14  
    1.15 +        msgIsFromGroup = msg->header.devicegroup;
    1.16 +
    1.17          switch (msg->payload.present) {
    1.18              case DeviceGroup_Protocol__payload_PR_beacon:
    1.19                  event = Beacon;
    1.20 @@ -164,6 +167,9 @@
    1.21  
    1.22              // finaly add partner to DB
    1.23              status = set_identity(session, tmpident);
    1.24 +            assert(status == PEP_STATUS_OK);
    1.25 +            if(status == PEP_STATUS_OK && msgIsFromGroup)
    1.26 +                status = set_identity_flags(session, tmpident, PEP_idf_devicegroup);
    1.27              free_identity(tmpident);
    1.28              assert(status == PEP_STATUS_OK);
    1.29              if (status != PEP_STATUS_OK) {
    1.30 @@ -257,12 +263,10 @@
    1.31      if (!(session && src))
    1.32          return PEP_ILLEGAL_VALUE;
    1.33  
    1.34 -    bool found = false;
    1.35 -    bool expired = false;
    1.36 -    bool discarded = false;
    1.37 +    bool consume = false;
    1.38 +    bool discard = false;
    1.39      bool force_keep_msg = false;
    1.40 -    
    1.41 -    bloblist_t *last = NULL;
    1.42 +
    1.43      for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
    1.44          if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
    1.45                  && bl->size) {
    1.46 @@ -286,7 +290,7 @@
    1.47                      time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_TIME;
    1.48                      time_t now = time(NULL);
    1.49                      if(expiry != 0 && now != 0 && expiry < now){
    1.50 -                        expired = true;
    1.51 +                        consume = true;
    1.52                          goto free_all;
    1.53                      }
    1.54                  }
    1.55 @@ -303,9 +307,17 @@
    1.56                                  strncmp(session->sync_uuid,
    1.57                                          (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
    1.58                                          msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
    1.59 -                                discarded = true;
    1.60 +                                discard = true;
    1.61                                  goto free_all;
    1.62                              }
    1.63 +                            
    1.64 +                            // Ignore and consume handshakes with devices
    1.65 +                            // already using trusted own key to encrypt
    1.66 +                            if (rating >= PEP_rating_trusted){
    1.67 +                                consume = true;
    1.68 +                                goto free_all;
    1.69 +                            }
    1.70 +
    1.71                              break;
    1.72                          // accepting GroupKeys needs encryption and trust of peer device
    1.73                          case DeviceGroup_Protocol__payload_PR_groupKeys:
    1.74 @@ -315,7 +327,7 @@
    1.75                                  (strncmp(session->sync_uuid,
    1.76                                          (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    1.77                                          msg->payload.choice.groupKeys.partner.user_id->size) != 0)){
    1.78 -                                discarded = true;
    1.79 +                                discard = true;
    1.80                                  goto free_all;
    1.81                              }
    1.82  
    1.83 @@ -333,7 +345,7 @@
    1.84                              status = get_trust(session, _from);
    1.85                              if (_from->comm_type < PEP_ct_strong_encryption) {
    1.86                                  free_identity(_from);
    1.87 -                                discarded = true;
    1.88 +                                discard = true;
    1.89                                  goto free_all;
    1.90                              }
    1.91                              free_identity(_from);
    1.92 @@ -347,7 +359,7 @@
    1.93                              force_keep_msg = true;
    1.94                              
    1.95                              if (!keylist || rating < PEP_rating_reliable){
    1.96 -                                discarded = true;
    1.97 +                                discard = true;
    1.98                                  goto free_all;
    1.99                              }
   1.100                              // GroupUpdate and UpdateRequests come from group.
   1.101 @@ -364,7 +376,7 @@
   1.102                              status = get_trust(session, _from);
   1.103                              if (_from->comm_type < PEP_ct_pEp) {
   1.104                                  free_identity(_from);
   1.105 -                                discarded = true;
   1.106 +                                discard = true;
   1.107                                  goto free_all;
   1.108                              }
   1.109                              free_identity(_from);
   1.110 @@ -374,7 +386,7 @@
   1.111                      }
   1.112  
   1.113  
   1.114 -                    found = true;
   1.115 +                    consume = true;
   1.116                      sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
   1.117                      if(sync_msg == NULL){
   1.118                          status = PEP_OUT_OF_MEMORY;
   1.119 @@ -389,12 +401,12 @@
   1.120                          }
   1.121                          goto free_all;
   1.122                      }
   1.123 -                    // don't message now that it is in the queue
   1.124 +                    // don't free message now that it is in the queue
   1.125                      goto free_userid;
   1.126                  }
   1.127                  else if (status == PEP_OWN_SEQUENCE) {
   1.128                      status = PEP_STATUS_OK;
   1.129 -                    discarded = true;
   1.130 +                    discard = true;
   1.131                      goto free_all;
   1.132                  }
   1.133  
   1.134 @@ -405,25 +417,7 @@
   1.135  
   1.136                  if (status != PEP_STATUS_OK)
   1.137                      return status;
   1.138 -
   1.139              }
   1.140 -
   1.141 -            if (!session->keep_sync_msg) {
   1.142 -                bloblist_t *blob = bl;
   1.143 -                if (last)
   1.144 -                    last->next = bl->next;
   1.145 -                else
   1.146 -                    src->attachments = bl->next;
   1.147 -
   1.148 -                blob->next = NULL;
   1.149 -                free_bloblist(blob);
   1.150 -            }
   1.151 -            else {
   1.152 -                last = bl;
   1.153 -            }
   1.154 -        }
   1.155 -        else {
   1.156 -            last = bl;
   1.157          }
   1.158      }
   1.159  
   1.160 @@ -431,7 +425,7 @@
   1.161          return PEP_MESSAGE_DISCARDED;
   1.162      }
   1.163  
   1.164 -    if ((expired || found) && !session->keep_sync_msg) {
   1.165 +    if (consume && !session->keep_sync_msg) {
   1.166          for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
   1.167                  spl = spl->next) {
   1.168              if (spl->value->key &&
   1.169 @@ -444,9 +438,31 @@
   1.170          return PEP_MESSAGE_DISCARDED;
   1.171      }
   1.172  
   1.173 -    if(discarded)
   1.174 +    if(discard)
   1.175          return PEP_MESSAGE_DISCARDED;
   1.176  
   1.177 +    if (!session->keep_sync_msg) {
   1.178 +        bloblist_t *last = NULL;
   1.179 +        for (bloblist_t *bl = src->attachments; bl && bl->value; ) {
   1.180 +            if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
   1.181 +                bloblist_t *b = bl;
   1.182 +                bl = bl->next;
   1.183 +                if (!last)
   1.184 +                    src->attachments = bl;
   1.185 +                else
   1.186 +                    last->next = bl;
   1.187 +                free(b->mime_type);
   1.188 +                free(b->filename);
   1.189 +                free(b->value);
   1.190 +                free(b);
   1.191 +            }
   1.192 +            else {
   1.193 +                last = bl;
   1.194 +                bl = bl->next;
   1.195 +            }
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199      return PEP_STATUS_OK;
   1.200  }
   1.201