ENGINE-293: Intermittent commit. Probably not compiling yet, but PEP_OWN_USERID is out from src where it needs to be. ENGINE-293
authorKrista Bennett <krista@pep-project.org>
Mon, 04 Dec 2017 18:23:14 +0100
branchENGINE-293
changeset 2303e7b3fa443f3e
parent 2302 3fbe4956dd34
child 2304 3bd45e2b8f92
ENGINE-293: Intermittent commit. Probably not compiling yet, but PEP_OWN_USERID is out from src where it needs to be.
src/keymanagement.c
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
src/sync_actions.c
src/sync_impl.c
     1.1 --- a/src/keymanagement.c	Mon Nov 27 16:46:52 2017 +0100
     1.2 +++ b/src/keymanagement.c	Mon Dec 04 18:23:14 2017 +0100
     1.3 @@ -104,13 +104,18 @@
     1.4  
     1.5      if (_no_user_id)
     1.6      {
     1.7 -        status = get_identity(session, identity->address, PEP_OWN_USERID,
     1.8 -                &stored_identity);
     1.9 -        if (status == PEP_STATUS_OK) {
    1.10 -            free_identity(stored_identity);
    1.11 -            return _myself(session, identity, false, true);
    1.12 +        char* own_id = NULL;
    1.13 +        status = get_own_userid(session, &own_id);
    1.14 +        if (status == PEP_STATUS_OK && own_id) {
    1.15 +            status = get_identity(session, identity->address, own_id,
    1.16 +                    &stored_identity);
    1.17 +            if (status == PEP_STATUS_OK) {
    1.18 +                free_identity(stored_identity);
    1.19 +                identity->user_id = own_id;
    1.20 +                return _myself(session, identity, false, true);
    1.21 +            }
    1.22          }
    1.23 -
    1.24 +        
    1.25          free(identity->user_id);
    1.26  
    1.27          identity->user_id = calloc(1, strlen(identity->address) + 6);
    1.28 @@ -120,6 +125,7 @@
    1.29          }
    1.30          snprintf(identity->user_id, strlen(identity->address) + 6,
    1.31                   "TOFU_%s", identity->address);
    1.32 +        }
    1.33      }
    1.34   
    1.35      status = get_identity(session,
    1.36 @@ -429,12 +435,15 @@
    1.37      assert(identity);
    1.38      assert(!EMPTYSTR(identity->address));
    1.39  
    1.40 +    char* own_id = NULL;
    1.41 +    status = get_own_userid(session, &own_id);
    1.42 +
    1.43      assert(EMPTYSTR(identity->user_id) ||
    1.44 -           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.45 +           (own_id && strcmp(identity->user_id, own_id) == 0));
    1.46  
    1.47      if (!(session && identity && !EMPTYSTR(identity->address) &&
    1.48              (EMPTYSTR(identity->user_id) ||
    1.49 -            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.50 +            (own_id && strcmp(identity->user_id, own_id) == 0)))
    1.51          return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.52  
    1.53      identity->comm_type = PEP_ct_pEp;
    1.54 @@ -445,7 +454,7 @@
    1.55      if (EMPTYSTR(identity->user_id))
    1.56      {
    1.57          free(identity->user_id);
    1.58 -        identity->user_id = strdup(PEP_OWN_USERID);
    1.59 +        identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
    1.60          assert(identity->user_id);
    1.61          if (identity->user_id == NULL)
    1.62              return PEP_OUT_OF_MEMORY;
     2.1 --- a/src/pEpEngine.c	Mon Nov 27 16:46:52 2017 +0100
     2.2 +++ b/src/pEpEngine.c	Mon Dec 04 18:23:14 2017 +0100
     2.3 @@ -50,7 +50,8 @@
     2.4  
     2.5  static const char *sql_get_identity =  
     2.6      "select fpr, username, comm_type, lang,"
     2.7 -    "   identity.flags | pgp_keypair.flags"
     2.8 +    "   identity.flags | pgp_keypair.flags,"
     2.9 +    "   is_own"
    2.10      "   from identity"
    2.11      "   join person on id = identity.user_id"
    2.12      "   join pgp_keypair on fpr = identity.main_key_id"
    2.13 @@ -77,15 +78,13 @@
    2.14      "      where id = ?1), upper(replace(?4,' ','')))),"
    2.15      "    (select device_group from person where id = ?1)) ;";
    2.16  
    2.17 -// FIXME: PEP_OWN_USERID
    2.18  static const char *sql_set_device_group = 
    2.19      "update person set device_group = ?1 "
    2.20 -    "where id = '" PEP_OWN_USERID "';";
    2.21 +    "where id = ?2;";
    2.22  
    2.23 -// FIXME: PEP_OWN_USERID
    2.24  static const char *sql_get_device_group = 
    2.25      "select device_group from person "
    2.26 -    "where id = '" PEP_OWN_USERID "';";
    2.27 +    "where id = ?1;";
    2.28  
    2.29  static const char *sql_set_pgp_keypair = 
    2.30      "insert or replace into pgp_keypair (fpr) "
    2.31 @@ -94,7 +93,7 @@
    2.32  static const char *sql_set_identity = 
    2.33      "insert or replace into identity ("
    2.34      " address, main_key_id, "
    2.35 -    " user_id, flags"
    2.36 +    " user_id, flags, is_own"
    2.37      ") values ("
    2.38      " ?1,"
    2.39      " upper(replace(?2,' ','')),"
    2.40 @@ -107,7 +106,8 @@
    2.41      // "    0)"
    2.42      // " ) | (?4 & 255)"
    2.43      /* set_identity ignores previous flags, and doesn't filter machine flags */
    2.44 -    " ?4"
    2.45 +    " ?4,"
    2.46 +    " ?5"
    2.47      ");";
    2.48          
    2.49  static const char *sql_set_identity_flags = 
    2.50 @@ -197,12 +197,12 @@
    2.51  static const char *sql_own_keys_retrieve =  
    2.52      "select fpr from trust"
    2.53      "   join identity on id = identity.user_id"
    2.54 -    "   where identity.is_own = 1"
    2.55 +    "   where identity.is_own = 1";
    2.56  
    2.57 -// FIXME: PEP_OWN_USERID
    2.58 -static const char *sql_set_own_key = 
    2.59 -    "insert or replace into own_keys (address, user_id, fpr)"
    2.60 -    " values (?1, '" PEP_OWN_USERID "', upper(replace(?2,' ','')));";
    2.61 +static const char* sql_get_own_userid =
    2.62 +    "select user_id from person"
    2.63 +    "   join identity on id = identity.user_id"
    2.64 +    "   where identity.is_own = 1";
    2.65  
    2.66  // Sequence
    2.67  static const char *sql_sequence_value1 = 
    2.68 @@ -355,7 +355,7 @@
    2.69      sqlite3_busy_timeout(_session->system_db, 1000);
    2.70  
    2.71  // increment this when patching DDL
    2.72 -#define _DDL_USER_VERSION "5"
    2.73 +#define _DDL_USER_VERSION "6"
    2.74  
    2.75      if (in_first) {
    2.76  
    2.77 @@ -445,18 +445,6 @@
    2.78                  "       on delete cascade,\n"
    2.79                  "   revocation_date integer\n"
    2.80                  ");\n"
    2.81 -                "create table if not exists own_keys (\n"
    2.82 -                "   address text,\n"
    2.83 -                "   user_id text,\n"
    2.84 -                "   fpr text not null\n"
    2.85 -                "       references pgp_keypair (fpr)\n"
    2.86 -                "       on delete cascade,\n"
    2.87 -                "   foreign key (address, user_id)\n"
    2.88 -                "       references identity\n"
    2.89 -                "       on delete cascade\n"
    2.90 -//                "   check (user_id = '" PEP_OWN_USERID "')\n"
    2.91 -                "   primary key (address, fpr)\n"
    2.92 -                ");\n" 
    2.93                  ,
    2.94              NULL,
    2.95              NULL,
    2.96 @@ -562,7 +550,18 @@
    2.97                      NULL,
    2.98                      NULL,
    2.99                      NULL
   2.100 -                );                
   2.101 +                );
   2.102 +                assert(int_result = SQLITE_OK);                
   2.103 +                int_result = sqlite3_exec(
   2.104 +                    _session->db,
   2.105 +                    "update identity\n"
   2.106 +                    "   set is_own = 1\n"
   2.107 +                    "   where (user_id = '" PEP_OWN_USERID "');\n",
   2.108 +                    NULL,
   2.109 +                    NULL,
   2.110 +                    NULL
   2.111 +                );
   2.112 +                assert(int_result = SQLITE_OK);                
   2.113              }
   2.114          }
   2.115          else { 
   2.116 @@ -601,6 +600,10 @@
   2.117              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
   2.118      assert(int_result == SQLITE_OK);
   2.119  
   2.120 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_own_userid,
   2.121 +            (int)strlen(sql_get_own_userid), &_session->get_own_userid, NULL);
   2.122 +    assert(int_result == SQLITE_OK);
   2.123 +
   2.124      int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
   2.125              (int)strlen(sql_replace_identities_fpr), 
   2.126              &_session->replace_identities_fpr, NULL);
   2.127 @@ -708,10 +711,10 @@
   2.128              &_session->own_keys_retrieve, NULL);
   2.129      assert(int_result == SQLITE_OK);
   2.130   
   2.131 -    int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
   2.132 -            (int)strlen(sql_set_own_key),
   2.133 -            &_session->set_own_key, NULL);
   2.134 -    assert(int_result == SQLITE_OK);
   2.135 +    // int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
   2.136 +    //         (int)strlen(sql_set_own_key),
   2.137 +    //         &_session->set_own_key, NULL);
   2.138 +    // assert(int_result == SQLITE_OK);
   2.139   
   2.140      // Sequence
   2.141  
   2.142 @@ -842,6 +845,8 @@
   2.143                  sqlite3_finalize(session->trustword);
   2.144              if (session->get_identity)
   2.145                  sqlite3_finalize(session->get_identity);
   2.146 +            if (session->get_own_userid)
   2.147 +                sqlite3_finalize(session->get_own_userid);
   2.148              if (session->replace_identities_fpr)
   2.149                  sqlite3_finalize(session->replace_identities_fpr);        
   2.150              if (session->set_person)
   2.151 @@ -888,8 +893,8 @@
   2.152                  sqlite3_finalize(session->own_identities_retrieve);
   2.153              if (session->own_keys_retrieve)
   2.154                  sqlite3_finalize(session->own_keys_retrieve);
   2.155 -            if (session->set_own_key)
   2.156 -                sqlite3_finalize(session->set_own_key);
   2.157 +            // if (session->set_own_key)
   2.158 +            //     sqlite3_finalize(session->set_own_key);
   2.159              if (session->sequence_value1)
   2.160                  sqlite3_finalize(session->sequence_value1);
   2.161              if (session->sequence_value2)
   2.162 @@ -1208,6 +1213,49 @@
   2.163      }
   2.164  }
   2.165  
   2.166 +DYNAMIC_API PEP_STATUS get_own_userid(
   2.167 +        PEP_SESSION session, 
   2.168 +        char** userid
   2.169 +    )
   2.170 +{
   2.171 +    assert(session);
   2.172 +    assert(userid);
   2.173 +    
   2.174 +    if (!session || !userid)
   2.175 +        return PEP_ILLEGAL_VALUE;
   2.176 +        
   2.177 +    PEP_STATUS status = PEP_STATUS_OK;
   2.178 +    char* retval = NULL;
   2.179 +    
   2.180 +    sqlite3_reset(session->get_own_userid);
   2.181 +
   2.182 +    const int result = sqlite3_step(session->get_own_userid);
   2.183 +    switch (result) {
   2.184 +        case SQLITE_ROW:
   2.185 +            const char* id = (const char *) sqlite3_column_text(session->get_own_userid, 0);
   2.186 +            if (!id) {
   2.187 +                // Shouldn't happen.
   2.188 +                status = PEP_UNKNOWN_ERROR;
   2.189 +            }
   2.190 +            else {
   2.191 +                retval = strdup(id);
   2.192 +                if (!retval)
   2.193 +                    status = PEP_OUT_OF_MEMORY;
   2.194 +            }
   2.195 +            break;
   2.196 +        default:
   2.197 +            // Technically true, given how we find it, but FIXME we need a more descriptive error
   2.198 +            status = PEP_CANNOT_FIND_IDENTITY;
   2.199 +            *identity = NULL;
   2.200 +    }
   2.201 +
   2.202 +    *userid = retval;
   2.203 +
   2.204 +    sqlite3_reset(session->get_own_userid);
   2.205 +    
   2.206 +    return status;
   2.207 +}
   2.208 +
   2.209  DYNAMIC_API PEP_STATUS get_identity(
   2.210          PEP_SESSION session,
   2.211          const char *address,
   2.212 @@ -1259,6 +1307,9 @@
   2.213          }
   2.214          _identity->flags = (unsigned int)
   2.215              sqlite3_column_int(session->get_identity, 4);
   2.216 +        _identity->me = (unsigned int)
   2.217 +            sqlite3_column_int(session->get_identity, 5);
   2.218 +    
   2.219          *identity = _identity;
   2.220          break;
   2.221      default:
   2.222 @@ -1350,6 +1401,7 @@
   2.223      sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
   2.224              SQLITE_STATIC);
   2.225      sqlite3_bind_int(session->set_identity, 4, identity->flags);
   2.226 +    sqlite3_bind_int(session->set_identity, 5, identity->me);
   2.227      result = sqlite3_step(session->set_identity);
   2.228      sqlite3_reset(session->set_identity);
   2.229      if (result != SQLITE_DONE) {
   2.230 @@ -1358,22 +1410,6 @@
   2.231      }
   2.232  
   2.233      if (has_fpr) {
   2.234 -        if(_identity_me(identity)) {
   2.235 -            sqlite3_reset(session->set_own_key);
   2.236 -            sqlite3_bind_text(session->set_own_key, 1, identity->address, -1,
   2.237 -                SQLITE_STATIC);
   2.238 -            sqlite3_bind_text(session->set_own_key, 2, identity->fpr, -1,
   2.239 -                SQLITE_STATIC);
   2.240 -            result = sqlite3_step(session->set_own_key);
   2.241 -            sqlite3_reset(session->set_own_key);
   2.242 -            if (result != SQLITE_DONE) {
   2.243 -                sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
   2.244 -                return PEP_CANNOT_SET_PGP_KEYPAIR;
   2.245 -            }
   2.246 -        }
   2.247 -
   2.248 -        // status = set_trust(session, identity->user_id, identity->fpr,
   2.249 -        //                    identity->comm_type)
   2.250          sqlite3_reset(session->set_trust);
   2.251          sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
   2.252                  SQLITE_STATIC);
   2.253 @@ -1453,6 +1489,15 @@
   2.254      if (!(session && group_name))
   2.255          return PEP_ILLEGAL_VALUE;
   2.256  
   2.257 +    // 1. Get own user_id
   2.258 +    char* user_id = NULL;
   2.259 +    PEP_STATUS status = get_own_userid(session, &user_id);
   2.260 +    
   2.261 +    // No user_id is returned in this case, no need to free;
   2.262 +    if (status != PEP_STATUS_OK)
   2.263 +        return status;
   2.264 +        
   2.265 +    // 2. Set device group
   2.266      sqlite3_reset(session->set_device_group);
   2.267      if(group_name){
   2.268          sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
   2.269 @@ -1460,9 +1505,15 @@
   2.270      } else {
   2.271          sqlite3_bind_null(session->set_device_group, 1);
   2.272      }
   2.273 +    
   2.274 +    sqlite3_bind_text(session->set_device_group, 2, user_id, -1,
   2.275 +            SQLITE_STATIC);
   2.276  
   2.277      result = sqlite3_step(session->set_device_group);
   2.278      sqlite3_reset(session->set_device_group);
   2.279 +    
   2.280 +    free(user_id);
   2.281 +    
   2.282      if (result != SQLITE_DONE)
   2.283          return PEP_CANNOT_SET_PERSON;
   2.284  
   2.285 @@ -1480,7 +1531,18 @@
   2.286      if (!(session && group_name))
   2.287          return PEP_ILLEGAL_VALUE;
   2.288  
   2.289 +    // 1. Get own user_id
   2.290 +    char* user_id = NULL;
   2.291 +    PEP_STATUS status = get_own_userid(session, &user_id);
   2.292 +    
   2.293 +    // No user_id is returned in this case, no need to free;
   2.294 +    if (status != PEP_STATUS_OK)
   2.295 +        return status;
   2.296 +
   2.297 +    // 2. get device group
   2.298      sqlite3_reset(session->get_device_group);
   2.299 +    sqlite3_bind_text(session->get_device_group, 1, user_id, -1,
   2.300 +            SQLITE_STATIC);
   2.301  
   2.302      result = sqlite3_step(session->get_device_group);
   2.303      switch (result) {
   2.304 @@ -1498,6 +1560,7 @@
   2.305          status = PEP_RECORD_NOT_FOUND;
   2.306      }
   2.307  
   2.308 +    free(user_id);
   2.309      sqlite3_reset(session->get_device_group);
   2.310      return status;
   2.311  }
     3.1 --- a/src/pEpEngine.h	Mon Nov 27 16:46:52 2017 +0100
     3.2 +++ b/src/pEpEngine.h	Mon Dec 04 18:23:14 2017 +0100
     3.3 @@ -601,6 +601,27 @@
     3.4          PEP_SESSION session, const pEp_identity *identity
     3.5      );
     3.6  
     3.7 +// get_own_userid() - get the user_id of the own user
     3.8 +//
     3.9 +//    parameters:
    3.10 +//        session (in)        session handle
    3.11 +//        userid  (out)       own user id (if it exists)
    3.12 +//
    3.13 +//    return value:
    3.14 +//        PEP_STATUS_OK = 0             userid was found
    3.15 +//        PEP_CANNOT_FIND_IDENTITY      no own_user found in the DB
    3.16 +//        PEP_UNKNOWN_ERROR             results were returned, but no ID
    3.17 +//                                      found (no reason this should ever occur)
    3.18 +//    caveat:
    3.19 +//        userid will be NULL if not found; otherwise, returned string
    3.20 +//        belongs to the caller.
    3.21 +
    3.22 +DYNAMIC_API PEP_STATUS get_own_userid(
    3.23 +        PEP_SESSION session, 
    3.24 +        char** userid
    3.25 +    );
    3.26 +
    3.27 +
    3.28  // set_device_group() - update own person's device group
    3.29  //
    3.30  //    parameters:
     4.1 --- a/src/pEp_internal.h	Mon Nov 27 16:46:52 2017 +0100
     4.2 +++ b/src/pEp_internal.h	Mon Dec 04 18:23:14 2017 +0100
     4.3 @@ -153,7 +153,7 @@
     4.4      sqlite3_stmt *own_identities_retrieve;
     4.5      sqlite3_stmt *own_userid_by_address;
     4.6      sqlite3_stmt *own_keys_retrieve;
     4.7 -    sqlite3_stmt *set_own_key;
     4.8 +//    sqlite3_stmt *set_own_key;
     4.9  
    4.10      // sequence value
    4.11      sqlite3_stmt *sequence_value1;
     5.1 --- a/src/sync_actions.c	Mon Nov 27 16:46:52 2017 +0100
     5.2 +++ b/src/sync_actions.c	Mon Dec 04 18:23:14 2017 +0100
     5.3 @@ -56,8 +56,14 @@
     5.4      // key created first wins
     5.5  
     5.6      Identity me = NULL;
     5.7 -    PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
     5.8 -            &me);
     5.9 +    
    5.10 +    char* own_id = NULL;
    5.11 +    status = get_own_userid(session, &own_id);
    5.12 +    if (own_id) {
    5.13 +        PEP_STATUS status = get_identity(session, partner->address, own_id,
    5.14 +                &me);
    5.15 +        free(own_id);
    5.16 +    }
    5.17      if (status == PEP_OUT_OF_MEMORY)
    5.18          return invalid_out_of_memory;
    5.19      if (status != PEP_STATUS_OK)
    5.20 @@ -142,9 +148,15 @@
    5.21      if (!session->notifyHandshake)
    5.22          return PEP_SYNC_NO_NOTIFY_CALLBACK;
    5.23  
    5.24 +    char* own_id = NULL;
    5.25 +    status = get_own_userid(session, &own_id);
    5.26 +        
    5.27      // notifyHandshake take ownership of given identities
    5.28      pEp_identity *me = NULL;
    5.29 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
    5.30 +    if (own_id) {
    5.31 +        status = get_identity(session, partner->address, own_id, &me);
    5.32 +        free(own_id);
    5.33 +    }
    5.34      if (status != PEP_STATUS_OK)
    5.35          goto error;
    5.36      
    5.37 @@ -235,16 +247,23 @@
    5.38      )
    5.39  {
    5.40      PEP_STATUS status = PEP_STATUS_OK;
    5.41 -
    5.42 +    
    5.43 +    char* own_id = NULL;
    5.44 +    status = get_own_userid(session, &own_id);
    5.45 +    
    5.46 +    // FIXME: Is this where and what we wanna do with this?
    5.47 +    if (status != PEP_STATUS_OK)
    5.48 +        return status;
    5.49 +        
    5.50      for (identity_list *il = group_keys; il && il->ident; il = il->next) {
    5.51  
    5.52 -        if (strcmp(il->ident->user_id, PEP_OWN_USERID)!=0) {
    5.53 +        if (strcmp(il->ident->user_id, own_id)!=0) {
    5.54              assert(0);
    5.55              continue;
    5.56          }
    5.57          // Check that identity isn't excluded from sync.
    5.58          pEp_identity *stored_identity = NULL;
    5.59 -        status = get_identity(session, il->ident->address, PEP_OWN_USERID,
    5.60 +        status = get_identity(session, il->ident->address, own_id,
    5.61                  &stored_identity);
    5.62          if (status == PEP_STATUS_OK) {
    5.63              if(stored_identity->flags & PEP_idf_not_for_sync){
     6.1 --- a/src/sync_impl.c	Mon Nov 27 16:46:52 2017 +0100
     6.2 +++ b/src/sync_impl.c	Mon Dec 04 18:23:14 2017 +0100
     6.3 @@ -275,16 +275,25 @@
     6.4      // partner identity must be explicitely added DB to later
     6.5      // be able to communicate securely with it.
     6.6      if(partner){
     6.7 +        
     6.8 +        char* own_id = NULL;
     6.9 +        status = get_own_userid(session, &own_id);
    6.10 +        
    6.11 +        if (!own_id)
    6.12 +            own_id = strdup(PEP_OWN_USERID);
    6.13 +            
    6.14          // protect virtual user IDs 
    6.15          if((strncmp("TOFU_", partner->user_id, 6) == 0 &&
    6.16             strlen(partner->user_id) == strlen(partner->address) + 6 &&
    6.17             strcmp(partner->user_id + 6, partner->address)) ||
    6.18          // protect own ID 
    6.19 -           (strcmp(PEP_OWN_USERID, partner->user_id) == 0)){
    6.20 +           (strcmp(own_id, partner->user_id) == 0)){
    6.21              status = PEP_SYNC_ILLEGAL_MESSAGE;
    6.22 +            free(own_id);
    6.23              goto error;
    6.24          }
    6.25  
    6.26 +        free(own_id);
    6.27          // partner IDs are UUIDs bound to session lifespan
    6.28          // and therefore partner identities are not supposed
    6.29          // to mutate over time, but just not be used anymore.
    6.30 @@ -426,6 +435,9 @@
    6.31      bool discard = false;
    6.32      bool force_keep_msg = false;
    6.33  
    6.34 +    char* own_id = NULL;
    6.35 +    PEP_STATUS own_id_status = get_own_userid(session, &own_id);
    6.36 +
    6.37      for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
    6.38          if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
    6.39                  && bl->size) {
    6.40 @@ -451,11 +463,18 @@
    6.41                              msg->header.me.address->size);
    6.42  
    6.43                  if(null_terminated_address){
    6.44 -                    status = get_identity(session, 
    6.45 -                                          null_terminated_address, 
    6.46 -                                          PEP_OWN_USERID, 
    6.47 -                                          &check_me);
    6.48 -                    free(null_terminated_address);
    6.49 +                    
    6.50 +                    if (own_id) {                        
    6.51 +                        status = get_identity(session, 
    6.52 +                                              null_terminated_address, 
    6.53 +                                              own_id, 
    6.54 +                                              &check_me);
    6.55 +                        free(null_terminated_address);
    6.56 +
    6.57 +                    }
    6.58 +                    else {
    6.59 +                        status = own_id_status;
    6.60 +                    }
    6.61                  } 
    6.62                  else
    6.63                      status = PEP_OUT_OF_MEMORY;
    6.64 @@ -608,10 +627,21 @@
    6.65                              // GroupUpdate and UpdateRequests come from group.
    6.66                              // check trust relation in between signer key and 
    6.67                              // own id to be sure.
    6.68 -                            pEp_identity *_from = new_identity(NULL, 
    6.69 -                                                               keylist->value,
    6.70 -                                                               PEP_OWN_USERID,
    6.71 -                                                               NULL);
    6.72 +                            
    6.73 +                            if (status != PEP_STATUS_OK)
    6.74 +                                goto free_all;
    6.75 +                            
    6.76 +                            if (own_id) {    
    6.77 +                                pEp_identity *_from = new_identity(NULL, 
    6.78 +                                                                   keylist->value,
    6.79 +                                                                   own_id,
    6.80 +                                                                   NULL);
    6.81 +                            }
    6.82 +                            else {
    6.83 +                                status = own_id_status;
    6.84 +                                goto free_all;
    6.85 +                            }
    6.86 +                            
    6.87                              if (_from == NULL){
    6.88                                  status = PEP_OUT_OF_MEMORY;
    6.89                                  goto free_all;
    6.90 @@ -658,7 +688,7 @@
    6.91                  ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    6.92              free_userid:
    6.93                  free(user_id);
    6.94 -
    6.95 +                free(own_id);
    6.96                  if (status != PEP_STATUS_OK)
    6.97                      return status;
    6.98              }
    6.99 @@ -765,10 +795,15 @@
   6.100          goto error;
   6.101      }
   6.102  
   6.103 +    char* own_id = NULL;
   6.104 +    status = get_own_userid(session, &own_id);
   6.105 +    if (status != PEP_STATUS_OK)
   6.106 +        goto error;
   6.107 +
   6.108      msg->header.version.major = SYNC_VERSION_MAJOR;
   6.109      msg->header.version.minor = SYNC_VERSION_MINOR;
   6.110  
   6.111 -    status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
   6.112 +    status = get_identity(session, partner->address, own_id, &me);
   6.113      if (status != PEP_STATUS_OK)
   6.114          goto error;
   6.115      
   6.116 @@ -889,6 +924,7 @@
   6.117      free(payload);
   6.118      free_message(_message);
   6.119      free_identity(me);
   6.120 +    free(own_id);
   6.121      return status;
   6.122  }
   6.123