src/keymanagement.c
branchENGINE-289
changeset 2317 b80dd91c8869
parent 2311 688b925c3e73
child 2319 93c84d8281d2
     1.1 --- a/src/keymanagement.c	Wed Dec 13 11:42:23 2017 +0100
     1.2 +++ b/src/keymanagement.c	Fri Dec 15 12:49:03 2017 +0100
     1.3 @@ -224,8 +224,48 @@
     1.4      if (identity->user_id) {            
     1.5          // (we're gonna update the trust/fpr anyway, so we user the no-fpr variant)
     1.6          //      * do get_identity() to retrieve stored identity information
     1.7 -        status = get_identity_without_fpr(session, &stored_ident);
     1.8 +        status = get_identity_without_fpr(session, identity->address, identity->user_id, &stored_ident);
     1.9 +
    1.10 +        // Before we start - if there was no stored identity, we should check to make sure we don't
    1.11 +        // have a stored identity with a temporary user_id that differs from the input user_id. This
    1.12 +        // happens in multithreaded environments sometimes.
    1.13 +        if (!stored_ident) {
    1.14 +            identity_list* id_list = NULL;
    1.15 +            status = get_identities_by_address(session, identity->address, &id_list);
    1.16 +
    1.17 +            if (id_list) {
    1.18 +                identity_list* id_curr = id_list;
    1.19 +                while (id_curr) {
    1.20 +                    pEp_identity* this_id = id_curr->ident;
    1.21 +                    if (this_id) {
    1.22 +                        char* this_uid = this_id->user_id;
    1.23 +                        if (this_uid && (strstr(this_uid, "TOFU_") == this_uid)) {
    1.24 +                            // FIXME: should we also be fixing pEp_own_userId in this
    1.25 +                            // function here?
    1.26 +                            
    1.27 +                            // Ok, we have a temp ID. We have to replace this
    1.28 +                            // with the real ID.
    1.29 +                            status = replace_userid(this_uid, identity->user_id);
    1.30 +                            if (status != PEP_STATUS_OK) {
    1.31 +                                free_identity_list(id_list);
    1.32 +                                return status;
    1.33 +                            }
    1.34 +                                
    1.35 +                            free(this_uid);
    1.36 +                            
    1.37 +                            // Reflect the change we just made to the DB
    1.38 +                            this_id->user_id = strdup(identity->user_id);
    1.39 +                            stored_ident = this_id;
    1.40 +                            // FIXME: free list.
    1.41 +                            break;
    1.42 +                        } 
    1.43 +                    }
    1.44 +                    id_curr = id_curr->next;
    1.45 +                }
    1.46 +            }
    1.47 +        } 
    1.48          
    1.49 +        // Ok, now we start the real algorithm:
    1.50          if (identity->username) {
    1.51              /*
    1.52               * Retrieving information of an identity with username supplied
    1.53 @@ -236,13 +276,30 @@
    1.54                  //      * patch it with username
    1.55                  //          (note: this will happen when 
    1.56                  //           setting automatically below...)
    1.57 -                //      * elect valid key for identity (see below)
    1.58 -                //    * if valid key exists
    1.59 +                //      * elect valid key for identity
    1.60 +                //    * if valid key existS
    1.61 +                //        * set return value's fpr
    1.62 +                status = get_valid_pubkey(session, stored_ident);
    1.63 +                if (status == PEP_STATUS_OK && stored_ident->fpr) {
    1.64                  //        * set identity comm_type from trust db (user_id, FPR)
    1.65 -                //        * set return value's fpr
    1.66 +                    status = get_trust(session, stored_ident);
    1.67 +                    if (status != PEP_STATUS_OK)
    1.68 +                        return status; // FIXME - free mem
    1.69 +                    if (identity->fpr && 
    1.70 +                             strcasecmp(stored_ident->fpr, identity->fpr) != 0) {
    1.71 +                        free(identity->fpr);
    1.72 +                        strdup(identity->fpr, stored_ident->fpr);
    1.73 +                        identity->comm_type = stored_ident->comm_type;
    1.74 +                    }
    1.75 +                }
    1.76 +                else {
    1.77 +                    return status; // Couldn't find a key.
    1.78 +                }
    1.79                  //    * call set_identity() to store
    1.80                  status = set_identity(identity);
    1.81 +            }
    1.82              //  * else (identity unavailable)
    1.83 +            else {
    1.84              //      * create identity with user_id, address, username
    1.85              //    * search for a temporary identity for address and username
    1.86              //    * if temporary identity available
    1.87 @@ -251,6 +308,7 @@
    1.88              //    * call set_identity() to store
    1.89              //  * Return: modified or created identity
    1.90               // 
    1.91 +            }
    1.92          }
    1.93          else {
    1.94              /*