src/keymanagement.c
branchENGINE-289
changeset 2395 2b7294a57041
parent 2394 88b9027db1bf
child 2399 09faff2e1f54
     1.1 --- a/src/keymanagement.c	Tue Jan 16 14:14:01 2018 +0100
     1.2 +++ b/src/keymanagement.c	Tue Jan 16 23:31:06 2018 +0100
     1.3 @@ -548,40 +548,52 @@
     1.4           * Temporary identity information with username supplied
     1.5              * Input: address, username (no others)
     1.6           */
     1.7 -        identity_list* id_list = NULL;
     1.8 -        status = get_identities_by_address(session, identity->address, &id_list);
     1.9 +         
    1.10 +        //  * See if there is an own identity that uses this address. If so, we'll
    1.11 +        //    prefer that
    1.12 +        stored_ident = NULL;
    1.13 +        
    1.14 +        if (default_own_id) {
    1.15 +            status = get_identity(session, 
    1.16 +                                  default_own_id, 
    1.17 +                                  identity->address, 
    1.18 +                                  &stored_ident);
    1.19 +        }
    1.20 +        // If there isn't an own identity, search for a non-temp stored ident
    1.21 +        // with this address.                      
    1.22 +        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
    1.23 + 
    1.24 +            identity_list* id_list = NULL;
    1.25 +            status = get_identities_by_address(session, identity->address, &id_list);
    1.26  
    1.27 -        //  * Search for an identity with non-temporary user_id with that mapping
    1.28 -        if (id_list) {
    1.29 -            identity_list* id_curr = id_list;
    1.30 -            while (id_curr) {
    1.31 -                pEp_identity* this_id = id_curr->ident;
    1.32 -                if (this_id) {
    1.33 -                    char* this_uid = this_id->user_id;
    1.34 -                    if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
    1.35 -                        // FIXME: should we also be fixing pEp_own_userId in this
    1.36 -                        // function here?
    1.37 -                        
    1.38 -                        // if usernames match, we replace the userid.
    1.39 -                        if (identity->username && 
    1.40 -                            strcasecmp(identity->username, 
    1.41 -                                       this_id->username) == 0) {
    1.42 -                            
    1.43 -                            // Ok, we have a real ID. Copy it!
    1.44 -                            identity->user_id = strdup(this_uid);
    1.45 -                            
    1.46 -                            if (!identity->user_id)
    1.47 -                                status = PEP_OUT_OF_MEMORY;
    1.48 -                            stored_ident = this_id;
    1.49 -                            
    1.50 -                            break;                                
    1.51 -                        }                            
    1.52 -                    } 
    1.53 +            if (id_list) {
    1.54 +                identity_list* id_curr = id_list;
    1.55 +                while (id_curr) {
    1.56 +                    pEp_identity* this_id = id_curr->ident;
    1.57 +                    if (this_id) {
    1.58 +                        char* this_uid = this_id->user_id;
    1.59 +                        if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
    1.60 +                            // if usernames match, we replace the userid.
    1.61 +                            if (identity->username && 
    1.62 +                                strcasecmp(identity->username, 
    1.63 +                                           this_id->username) == 0) {
    1.64 +                                
    1.65 +                                // Ok, we have a real ID. Copy it!
    1.66 +                                identity->user_id = strdup(this_uid);
    1.67 +                                
    1.68 +                                if (!identity->user_id)
    1.69 +                                    status = PEP_OUT_OF_MEMORY;
    1.70 +                                stored_ident = this_id;
    1.71 +                                
    1.72 +                                break;                                
    1.73 +                            }                            
    1.74 +                        } 
    1.75 +                    }
    1.76 +                    id_curr = id_curr->next;
    1.77                  }
    1.78 -                id_curr = id_curr->next;
    1.79              }
    1.80          }
    1.81 -
    1.82 +        
    1.83          if (stored_ident) {
    1.84              status = prepare_updated_identity(session,
    1.85                                                identity,
    1.86 @@ -616,22 +628,35 @@
    1.87           * Temporary identity information without username suplied
    1.88              * Input: address (no others)
    1.89           */
    1.90 -        identity_list* id_list = NULL;
    1.91 -        status = get_identities_by_address(session, identity->address, &id_list);
    1.92 +         
    1.93 +        //  * Again, see if there is an own identity that uses this address. If so, we'll
    1.94 +        //    prefer that
    1.95 +        stored_ident = NULL;
    1.96 +         
    1.97 +        if (default_own_id) {
    1.98 +            status = get_identity(session, 
    1.99 +                                  default_own_id, 
   1.100 +                                  identity->address, 
   1.101 +                                  &stored_ident);
   1.102 +        }
   1.103 +        // If there isn't an own identity, search for a non-temp stored ident
   1.104 +        // with this address.                      
   1.105 +        if (status == PEP_CANNOT_FIND_IDENTITY || !stored_ident) { 
   1.106 + 
   1.107 +            identity_list* id_list = NULL;
   1.108 +            status = get_identities_by_address(session, identity->address, &id_list);
   1.109  
   1.110 -        //    * Search for identity with this address
   1.111 -        if (id_list && !(id_list->next)) { // exactly one            
   1.112 -            //    * If exactly one found
   1.113 -            //      * elect valid key for identity (see below)
   1.114 -            //      * Return this identity
   1.115 -            stored_ident = id_list->ident;
   1.116 -            
   1.117 -            if (stored_ident)
   1.118 -                status = prepare_updated_identity(session, identity,
   1.119 -                                                  stored_ident, false);
   1.120 -            else
   1.121 -                status = PEP_CANNOT_FIND_IDENTITY;
   1.122 +            //    * Search for identity with this address
   1.123 +            if (id_list && !(id_list->next)) { // exactly one            
   1.124 +                //    * If exactly one found
   1.125 +                //      * elect valid key for identity (see below)
   1.126 +                //      * Return this identity
   1.127 +                stored_ident = id_list->ident;
   1.128 +            }
   1.129          }
   1.130 +        if (stored_ident)
   1.131 +            status = prepare_updated_identity(session, identity,
   1.132 +                                              stored_ident, false);
   1.133          else // too little info
   1.134              status = PEP_CANNOT_FIND_IDENTITY; 
   1.135      }