disable_identity_for_sync() sync
authorVolker Birk <vb@pep-project.org>
Thu, 30 Jan 2020 13:21:30 +0100
branchsync
changeset 43755d1ec3eaeee2
parent 4374 9b1574786c91
child 4376 2e81846e20b1
disable_identity_for_sync()
src/sync_api.c
src/sync_api.h
     1.1 --- a/src/sync_api.c	Thu Jan 30 12:57:23 2020 +0100
     1.2 +++ b/src/sync_api.c	Thu Jan 30 13:21:30 2020 +0100
     1.3 @@ -279,11 +279,17 @@
     1.4      ident->flags = stored_ident->flags;
     1.5      free_identity(stored_ident);
     1.6  
     1.7 +    // this is an invalid state; detect while debugging
     1.8 +
     1.9 +    assert(!((ident->flags & PEP_idf_devicegroup) && (ident->flags & PEP_idf_not_for_sync)));
    1.10 +
    1.11      // if we're grouped and this identity is enabled already we can stop here
    1.12 +
    1.13      if ((ident->flags & PEP_idf_devicegroup) && !(ident->flags & PEP_idf_not_for_sync))
    1.14          return PEP_STATUS_OK;
    1.15  
    1.16      // if the identity is marked not for sync unset this to enable
    1.17 +
    1.18      if (ident->flags & PEP_idf_not_for_sync) {
    1.19          status = unset_identity_flags(session, ident, PEP_idf_not_for_sync);
    1.20          if (status)
    1.21 @@ -291,6 +297,7 @@
    1.22      }
    1.23  
    1.24      // if we're grouped then add the identity to the group
    1.25 +
    1.26      bool grouped = false;
    1.27      status = deviceGrouped(session, &grouped);
    1.28      if (status)
    1.29 @@ -313,28 +320,38 @@
    1.30      if (!(session && ident))
    1.31          return PEP_ILLEGAL_VALUE;
    1.32  
    1.33 -    // create the identity in the database if it is not yet there
    1.34 -    PEP_STATUS status = _myself(session, ident, false, true, false);
    1.35 -    if (status != PEP_STATUS_OK && status != PEP_KEY_NOT_FOUND && status != PEP_GET_KEY_FAILED)
    1.36 +    // safeguard: in case the delivered identity is not valid fetch flags from the database
    1.37 +    //            while doing this check if this is an own identity and return an error if not
    1.38 +
    1.39 +    pEp_identity *stored_ident = NULL;
    1.40 +    PEP_STATUS status = get_identity(session, ident->address, ident->user_id, &stored_ident);
    1.41 +    if (status)
    1.42          return status;
    1.43 -        
    1.44 -    bool explicitly_enabled = ident->flags & PEP_idf_devicegroup;
    1.45 -        
    1.46 -    // if identity is already disabled for sync do nothing
    1.47 -    if ((!explicitly_enabled) && (ident->flags & PEP_idf_not_for_sync))
    1.48 +    assert(stored_ident);
    1.49 +    if (!stored_ident->me) {
    1.50 +        free_identity(stored_ident);
    1.51 +        return PEP_ILLEGAL_VALUE;
    1.52 +    }
    1.53 +    ident->flags = stored_ident->flags;
    1.54 +    free_identity(stored_ident);
    1.55 +
    1.56 +    // this is an invalid state; detect while debugging
    1.57 +
    1.58 +    assert(!((ident->flags & PEP_idf_devicegroup) && (ident->flags & PEP_idf_not_for_sync)));
    1.59 +
    1.60 +    // if this identity is disabled already we can end here
    1.61 +
    1.62 +    if (ident->flags & PEP_idf_not_for_sync)
    1.63          return PEP_STATUS_OK;
    1.64  
    1.65 -    bool key_genned = false;
    1.66 -    
    1.67 -    // status != PEP_STATUS_OK means there was no key and, probably, no record.    
    1.68 -    if (!explicitly_enabled && ((status != PEP_STATUS_OK) || EMPTYSTR(ident->fpr))) {
    1.69 -        // safe to gen here, no event will happen
    1.70 -        status = myself(session, ident);
    1.71 -        if (status != PEP_STATUS_OK)
    1.72 -            return status;
    1.73 -            
    1.74 -        key_genned = true;
    1.75 -    }        
    1.76 +    // if the identity is not part of a device group just disable it to keep this
    1.77 +
    1.78 +    if (!(ident->flags & PEP_idf_devicegroup)) {
    1.79 +        status = set_identity_flags(session, ident, PEP_idf_not_for_sync);
    1.80 +        return status;
    1.81 +    }
    1.82 +
    1.83 +    // we are grouped and this identity is part of a device group => key reset in all cases
    1.84  
    1.85      status = unset_identity_flags(session, ident, PEP_idf_devicegroup);
    1.86      if (status)
    1.87 @@ -343,12 +360,7 @@
    1.88      status = set_identity_flags(session, ident, PEP_idf_not_for_sync);
    1.89      if (status)
    1.90          return status;
    1.91 -        
    1.92 -    ident->flags = (ident->flags | PEP_idf_not_for_sync) & ~PEP_idf_devicegroup;   
    1.93  
    1.94 -    // If the key is new, it hasn't been sync'd. If it's not, it has. If it has, reset it.
    1.95 -    if (!key_genned)
    1.96 -        status = key_reset_identity(session, ident, NULL);
    1.97 -        
    1.98 +    status = key_reset_identity(session, ident, NULL);
    1.99      return status;
   1.100  }
     2.1 --- a/src/sync_api.h	Thu Jan 30 12:57:23 2020 +0100
     2.2 +++ b/src/sync_api.h	Thu Jan 30 13:21:30 2020 +0100
     2.3 @@ -208,6 +208,7 @@
     2.4  
     2.5  PEP_STATUS disable_sync(PEP_SESSION session);
     2.6  
     2.7 +
     2.8  // leave_device_group() - Issue a group key reset request and 
     2.9  // leave the device group, shutting down sync 
    2.10  //
    2.11 @@ -216,13 +217,11 @@
    2.12  
    2.13  DYNAMIC_API PEP_STATUS leave_device_group(PEP_SESSION session);
    2.14  
    2.15 +
    2.16  // enable_identity_for_sync() - enable sync for this identity
    2.17  //  parameters:
    2.18  //      session                 pEp session
    2.19 -//      ident                   identity to enable
    2.20 -//
    2.21 -//  caveat:
    2.22 -//      ident must be an own identity
    2.23 +//      ident                   own identity to enable
    2.24  
    2.25  DYNAMIC_API PEP_STATUS enable_identity_for_sync(PEP_SESSION session,
    2.26          pEp_identity *ident);
    2.27 @@ -231,10 +230,7 @@
    2.28  // disable_identity_for_sync() - disable sync for this identity
    2.29  //  parameters:
    2.30  //      session                 pEp session
    2.31 -//      ident                   identity to disable
    2.32 -//
    2.33 -//  caveat:
    2.34 -//      do not call this function without sync running
    2.35 +//      ident                   own identity to disable
    2.36  
    2.37  DYNAMIC_API PEP_STATUS disable_identity_for_sync(PEP_SESSION session,
    2.38          pEp_identity *ident);