Little bits of cleanup fun
authorKrista Bennett <krista@pep-project.org>
Mon, 22 Oct 2018 10:34:59 +0200
changeset 31037a51a2b6c7ba
parent 3095 4df9a15497c2
child 3104 2b14c4d40d95
Little bits of cleanup fun
src/keymanagement.c
     1.1 --- a/src/keymanagement.c	Mon Oct 22 08:18:06 2018 +0200
     1.2 +++ b/src/keymanagement.c	Mon Oct 22 10:34:59 2018 +0200
     1.3 @@ -557,58 +557,61 @@
     1.4  
     1.5              if (id_list) {
     1.6                  identity_list* id_curr = id_list;
     1.7 -                bool input_is_TOFU = strstr(identity->user_id, "TOFU_") == identity->user_id;
     1.8 +                bool input_is_TOFU = (strstr(identity->user_id, "TOFU_") == identity->user_id);
     1.9                  while (id_curr) {
    1.10                      pEp_identity* this_id = id_curr->ident;
    1.11                      if (this_id) {
    1.12                          char* this_uid = this_id->user_id;
    1.13 -                        bool curr_is_TOFU = strstr(this_uid, "TOFU_") == this_uid;
    1.14 -                        if (this_uid) {
    1.15 -                            if (curr_is_TOFU && !input_is_TOFU) {
    1.16 -                                // FIXME: should we also be fixing pEp_own_userId in this
    1.17 -                                // function here?
    1.18 +                        bool curr_is_TOFU = false;
    1.19 +                        // this_uid should never be NULL, as this is half of the ident
    1.20 +                        // DB primary key
    1.21 +                        assert(!EMPTYSTR(this_uid));
    1.22 +
    1.23 +                        curr_is_TOFU = (strstr(this_uid, "TOFU_") == this_uid);
    1.24 +                        if (curr_is_TOFU && !input_is_TOFU) {
    1.25 +                            // FIXME: should we also be fixing pEp_own_userId in this
    1.26 +                            // function here?
    1.27 +                            
    1.28 +                            // if usernames match, we replace the userid. Or if the temp username
    1.29 +                            // is anonymous.
    1.30 +                            // FIXME: do we need to create an address match function which
    1.31 +                            // matches the whole dot-and-case rigamarole from 
    1.32 +                            if (EMPTYSTR(this_id->username) ||
    1.33 +                                strcasecmp(this_id->username, this_id->address) == 0 ||
    1.34 +                                (identity->username && 
    1.35 +                                 strcasecmp(identity->username, 
    1.36 +                                            this_id->username) == 0)) {
    1.37                                  
    1.38 -                                // if usernames match, we replace the userid. Or if the temp username
    1.39 -                                // is anonymous.
    1.40 -                                // FIXME: do we need to create an address match function which
    1.41 -                                // matches the whole dot-and-case rigamarole from 
    1.42 -                                if (EMPTYSTR(this_id->username) ||
    1.43 -                                    strcasecmp(this_id->username, this_id->address) == 0 ||
    1.44 -                                    (identity->username && 
    1.45 -                                     strcasecmp(identity->username, 
    1.46 -                                                this_id->username) == 0)) {
    1.47 +                                // Ok, we have a temp ID. We have to replace this
    1.48 +                                // with the real ID.
    1.49 +                                status = replace_userid(session, 
    1.50 +                                                        this_uid, 
    1.51 +                                                        identity->user_id);
    1.52 +                                if (status != PEP_STATUS_OK) {
    1.53 +                                    free_identity_list(id_list);
    1.54 +                                    free(default_own_id);
    1.55 +                                    return status;
    1.56 +                                }
    1.57                                      
    1.58 -                                    // Ok, we have a temp ID. We have to replace this
    1.59 -                                    // with the real ID.
    1.60 -                                    status = replace_userid(session, 
    1.61 -                                                            this_uid, 
    1.62 -                                                            identity->user_id);
    1.63 -                                    if (status != PEP_STATUS_OK) {
    1.64 -                                        free_identity_list(id_list);
    1.65 -                                        free(default_own_id);
    1.66 -                                        return status;
    1.67 -                                    }
    1.68 -                                        
    1.69 -                                    free(this_uid);
    1.70 -                                    this_uid = NULL;
    1.71 -                                    
    1.72 -                                    // Reflect the change we just made to the DB
    1.73 -                                    this_id->user_id = strdup(identity->user_id);
    1.74 -                                    stored_ident = this_id;
    1.75 -                                    // FIXME: free list.
    1.76 -                                    break;                                
    1.77 -                                }
    1.78 -                            }
    1.79 -                            else if (input_is_TOFU && !curr_is_TOFU) {
    1.80 -                                // Replace ruthlessly - this is NOT supposed to happen.
    1.81 -                                // BAD APP BEHAVIOUR.
    1.82 -                                free(identity->user_id);
    1.83 -                                identity->user_id = strdup(this_id->user_id);
    1.84 +                                free(this_uid);
    1.85 +                                this_uid = NULL;
    1.86 +                                
    1.87 +                                // Reflect the change we just made to the DB
    1.88 +                                this_id->user_id = strdup(identity->user_id);
    1.89                                  stored_ident = this_id;
    1.90                                  // FIXME: free list.
    1.91                                  break;                                
    1.92 -                            }                            
    1.93 -                        } 
    1.94 +                            }
    1.95 +                        }
    1.96 +                        else if (input_is_TOFU && !curr_is_TOFU) {
    1.97 +                            // Replace ruthlessly - this is NOT supposed to happen.
    1.98 +                            // BAD APP BEHAVIOUR.
    1.99 +                            free(identity->user_id);
   1.100 +                            identity->user_id = strdup(this_id->user_id);
   1.101 +                            stored_ident = this_id;
   1.102 +                            // FIXME: free list.
   1.103 +                            break;                                
   1.104 +                        }                            
   1.105                      }
   1.106                      id_curr = id_curr->next;
   1.107                  }
   1.108 @@ -644,19 +647,15 @@
   1.109              //    * create identity with user_id, address, username
   1.110              //      (this is the input id without the fpr + comm type!)
   1.111  
   1.112 -            if (status == PEP_STATUS_OK) {
   1.113 -                elect_pubkey(session, identity, false);
   1.114 -            }
   1.115 +            elect_pubkey(session, identity, false);
   1.116                          
   1.117              //    * We've already checked and retrieved
   1.118              //      any applicable temporary identities above. If we're 
   1.119              //      here, none of them fit.
   1.120              //    * call set_identity() to store
   1.121 -            if (status == PEP_STATUS_OK) {
   1.122 -                // FIXME: Do we set if we had to copy in the address?
   1.123 +            // FIXME: Do we set if we had to copy in the address?
   1.124                  adjust_pep_trust_status(session, identity);
   1.125 -                status = set_identity(session, identity);
   1.126 -            }
   1.127 +            status = set_identity(session, identity);
   1.128              //  * Return: created identity
   1.129          }        
   1.130      }
   1.131 @@ -689,7 +688,10 @@
   1.132                      pEp_identity* this_id = id_curr->ident;
   1.133                      if (this_id) {
   1.134                          char* this_uid = this_id->user_id;
   1.135 -                        if (this_uid && (strstr(this_uid, "TOFU_") != this_uid)) {
   1.136 +                        assert(!EMPTYSTR(this_uid));
   1.137 +                        // Should never be NULL - DB primary key
   1.138 +                        
   1.139 +                        if (strstr(this_uid, "TOFU_") != this_uid) {
   1.140                              // if usernames match, we replace the userid.
   1.141                              if (identity->username && 
   1.142                                  strcasecmp(identity->username,