1.1 --- a/src/sync_api.c Thu Jan 30 11:23:11 2020 +0100
1.2 +++ b/src/sync_api.c Thu Jan 30 12:57:23 2020 +0100
1.3 @@ -260,35 +260,48 @@
1.4 DYNAMIC_API PEP_STATUS enable_identity_for_sync(PEP_SESSION session,
1.5 pEp_identity *ident)
1.6 {
1.7 - assert(session && ident);
1.8 - if (!(session && ident))
1.9 + assert(session && ident && ident->user_id && ident->user_id[0] && ident->address && ident->address[0]);
1.10 + if (!(session && ident && ident->user_id && ident->user_id[0] && ident->address && ident->address[0]))
1.11 return PEP_ILLEGAL_VALUE;
1.12
1.13 - // Find out if flags are already set. If there's no identity, this won't
1.14 - // create one.
1.15 - PEP_STATUS status = _myself(session, ident, false, true, false);
1.16 - if (status != PEP_STATUS_OK && status != PEP_KEY_NOT_FOUND && status != PEP_GET_KEY_FAILED)
1.17 + // safeguard: in case the delivered identity is not valid fetch flags from the database
1.18 + // while doing this check if this is an own identity and return an error if not
1.19 +
1.20 + pEp_identity *stored_ident = NULL;
1.21 + PEP_STATUS status = get_identity(session, ident->address, ident->user_id, &stored_ident);
1.22 + if (status)
1.23 return status;
1.24 + assert(stored_ident);
1.25 + if (!stored_ident->me) {
1.26 + free_identity(stored_ident);
1.27 + return PEP_ILLEGAL_VALUE;
1.28 + }
1.29 + ident->flags = stored_ident->flags;
1.30 + free_identity(stored_ident);
1.31
1.32 + // if we're grouped and this identity is enabled already we can stop here
1.33 if ((ident->flags & PEP_idf_devicegroup) && !(ident->flags & PEP_idf_not_for_sync))
1.34 return PEP_STATUS_OK;
1.35
1.36 - // Ok, this is kind of annoying - if the flags aren't set, we won't create
1.37 - // a keygen event anyway, so we're going to have to call myself and issue
1.38 - // an unconditional keygen event.
1.39 - status = myself(session, ident);
1.40 - if (status != PEP_STATUS_OK)
1.41 - return status;
1.42 + // if the identity is marked not for sync unset this to enable
1.43 + if (ident->flags & PEP_idf_not_for_sync) {
1.44 + status = unset_identity_flags(session, ident, PEP_idf_not_for_sync);
1.45 + if (status)
1.46 + return status;
1.47 + }
1.48
1.49 - status = unset_identity_flags(session, ident, PEP_idf_not_for_sync);
1.50 - if (status != PEP_STATUS_OK) // explicit. sorry, but lazy makes mistakes in C
1.51 + // if we're grouped then add the identity to the group
1.52 + bool grouped = false;
1.53 + status = deviceGrouped(session, &grouped);
1.54 + if (status)
1.55 return status;
1.56 -
1.57 - status = set_identity_flags(session, ident, PEP_idf_devicegroup);
1.58 - if (status != PEP_STATUS_OK)
1.59 - return status;
1.60 -
1.61 - signal_Sync_event(session, Sync_PR_keysync, KeyGen, NULL);
1.62 + if (grouped) {
1.63 + status = set_identity_flags(session, ident, PEP_idf_devicegroup);
1.64 + if (status)
1.65 + return status;
1.66 + // signal we have a new identity in the group
1.67 + signal_Sync_event(session, Sync_PR_keysync, KeyGen, NULL);
1.68 + }
1.69
1.70 return PEP_STATUS_OK;
1.71 }
2.1 --- a/src/sync_api.h Thu Jan 30 11:23:11 2020 +0100
2.2 +++ b/src/sync_api.h Thu Jan 30 12:57:23 2020 +0100
2.3 @@ -222,7 +222,7 @@
2.4 // ident identity to enable
2.5 //
2.6 // caveat:
2.7 -// do not call this function without sync running
2.8 +// ident must be an own identity
2.9
2.10 DYNAMIC_API PEP_STATUS enable_identity_for_sync(PEP_SESSION session,
2.11 pEp_identity *ident);