src/keymanagement.c
branchENGINE-289
changeset 2301 62ed2fc84b3a
parent 2288 8aee10631677
child 2302 3fbe4956dd34
     1.1 --- a/src/keymanagement.c	Wed Nov 22 10:44:14 2017 +0100
     1.2 +++ b/src/keymanagement.c	Mon Nov 27 16:23:13 2017 +0100
     1.3 @@ -892,14 +892,15 @@
     1.4                      sqlite3_column_text(session->own_identities_retrieve, 0);
     1.5                  fpr = (const char *)
     1.6                      sqlite3_column_text(session->own_identities_retrieve, 1);
     1.7 -                user_id = PEP_OWN_USERID;
     1.8 +                user_id = (const char *)
     1.9 +                    sqlite3_column_text(session->own_identities_retrieve, 2);
    1.10                  username = (const char *)
    1.11 -                    sqlite3_column_text(session->own_identities_retrieve, 2);
    1.12 +                    sqlite3_column_text(session->own_identities_retrieve, 3);
    1.13                  comm_type = PEP_ct_pEp;
    1.14                  lang = (const char *)
    1.15 -                    sqlite3_column_text(session->own_identities_retrieve, 3);
    1.16 +                    sqlite3_column_text(session->own_identities_retrieve, 4);
    1.17                  flags = (unsigned int)
    1.18 -                    sqlite3_column_int(session->own_identities_retrieve, 4);
    1.19 +                    sqlite3_column_int(session->own_identities_retrieve, 5);
    1.20  
    1.21                  pEp_identity *ident = new_identity(address, fpr, user_id, username);
    1.22                  if (!ident)
    1.23 @@ -1043,24 +1044,70 @@
    1.24            fpr && fpr[0]
    1.25           ))
    1.26          return PEP_ILLEGAL_VALUE;
    1.27 +            
    1.28 +            
    1.29 +    // First see if we have it in own identities already, AND we retrieve
    1.30 +    // our own user_id
    1.31 +    pEp_identity* my_id = NULL;
    1.32 +    identity_list* my_identities = NULL;
    1.33 +    char* my_user_id = NULL;
    1.34 +    status = own_identities_retrieve(session, &my_identities);
    1.35      
    1.36 -    sqlite3_reset(session->set_own_key);
    1.37 -    sqlite3_bind_text(session->set_own_key, 1, address, -1, SQLITE_STATIC);
    1.38 -    sqlite3_bind_text(session->set_own_key, 2, fpr, -1, SQLITE_STATIC);
    1.39 +    if (status == PEP_STATUS_OK) {
    1.40 +        if (my_identities) {
    1.41 +            if (!(my_identities->ident && my_identities->ident->user_id))
    1.42 +                return PEP_ILLEGAL_VALUE;
    1.43  
    1.44 -    int result;
    1.45 +            my_user_id = strdup(my_identities->ident->user_id);
    1.46 +
    1.47 +            if (!my_user_id) 
    1.48 +                return PEP_OUT_OF_MEMORY;
    1.49 +            
    1.50 +            // Probably cheaper than all the strcmps if there are many,
    1.51 +            // plus this avoids the capitalisation and . problems:
    1.52 +            
    1.53 +            status = get_identity(session, my_user_id, address, &my_id);
    1.54 +            
    1.55 +            if (status == PEP_STATUS_OK && my_id) {
    1.56 +                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0)) {
    1.57 +                    // We're done. It was already here.
    1.58 +                    // FIXME: Do we check trust/revocation/?
    1.59 +                    goto pep_free;
    1.60 +                }            
    1.61 +            }
    1.62 +            
    1.63 +            // Otherwise, we see if there's a binding for this user_id/key
    1.64 +            // in the trust DB
    1.65 +            
    1.66 +            // If there's an id w/ user_id + address
    1.67 +            if (my_id) {
    1.68 +                free(my_id->fpr);
    1.69 +                my_id->fpr = my_user_id;
    1.70 +                my_user_id->comm_type = PEP_ct_pEp;
    1.71 +            }
    1.72 +            else { // Else, we need a new identity
    1.73 +                status = new_identity(session, address, fpr, my_user_id, NULL, &my_id); 
    1.74 +                if (status != PEP_STATUS_OK)
    1.75 +                    goto pep_free; 
    1.76 +                my_user_id->me = true;
    1.77 +                my_user_id->comm_type = PEP_ct_pEp;
    1.78 +            }
    1.79 +        }
    1.80 +        else {
    1.81 +            // I think the prerequisite should be that at least one own identity
    1.82 +            // already in the DB, so REALLY look at this.
    1.83 +            // status = new_identity(session, address, fpr, "PEP_OWN_USERID", NULL); 
    1.84 +            // my_user_id->me = true;
    1.85 +            // my_user_id->comm_type = PEP_ct_pEp;
    1.86 +            return PEP_CANNOT_FIND_IDENTITY;
    1.87 +        }
    1.88 +        
    1.89 +        status = set_identity(session, my_id);
    1.90 +    }  
    1.91      
    1.92 -    result = sqlite3_step(session->set_own_key);
    1.93 -    switch (result) {
    1.94 -        case SQLITE_DONE:
    1.95 -            status = PEP_STATUS_OK;
    1.96 -            break;
    1.97 -            
    1.98 -        default:
    1.99 -            status = PEP_UNKNOWN_ERROR;
   1.100 -    }
   1.101 -    
   1.102 -    sqlite3_reset(session->set_own_key);
   1.103 +pep_free:
   1.104 +    free(my_id);
   1.105 +    free(my_user_id);
   1.106      return status;
   1.107  }
   1.108