src/keymanagement.c
branchENGINE-293
changeset 2302 3fbe4956dd34
parent 2219 99b05a2f117e
parent 2301 62ed2fc84b3a
child 2303 e7b3fa443f3e
     1.1 --- a/src/keymanagement.c	Fri Oct 27 20:02:41 2017 +0200
     1.2 +++ b/src/keymanagement.c	Mon Nov 27 16:46:52 2017 +0100
     1.3 @@ -93,14 +93,12 @@
     1.4      if (!(session && identity && !EMPTYSTR(identity->address)))
     1.5          return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
     1.6  
     1.7 -    /* Ok, if I'm trying to indicate this is me in the identity struct, then
     1.8 -       call _myself */
     1.9 +    // dunno if we should keep this function shortcut.
    1.10      if (_identity_me(identity)) {
    1.11 +        identity->me = true;
    1.12          return _myself(session, identity, false, true);
    1.13      }
    1.14  
    1.15 -    /* Otherwise, unless get_identity indicates that this is me, it's
    1.16 -       someone else as far as this syscall is concerned. */
    1.17      int _no_user_id = EMPTYSTR(identity->user_id);
    1.18      int _did_elect_new_key = 0;
    1.19  
    1.20 @@ -284,7 +282,7 @@
    1.21  
    1.22          if (EMPTYSTR(temp_id->username)) { // mitigate
    1.23              free(temp_id->username);
    1.24 -            temp_id->username = strdup("anonymous");
    1.25 +            temp_id->username = strdup("Anonymous");
    1.26              assert(temp_id->username);
    1.27              if (temp_id->username == NULL){
    1.28                  status = PEP_OUT_OF_MEMORY;
    1.29 @@ -317,11 +315,12 @@
    1.30      free(identity->user_id);
    1.31      identity->user_id = strdup(temp_id->user_id);
    1.32      free(identity->username);
    1.33 -    identity->username = strdup(temp_id->username ? temp_id->username : "anonymous");
    1.34 +    identity->username = strdup(temp_id->username ? temp_id->username : "Anonymous");
    1.35      identity->comm_type = temp_id->comm_type;
    1.36      identity->lang[0] = temp_id->lang[0];
    1.37      identity->lang[1] = temp_id->lang[1];
    1.38      identity->lang[2] = 0;
    1.39 +    identity->me = temp_id->me;
    1.40      identity->flags = temp_id->flags;
    1.41  
    1.42  exit_free :
    1.43 @@ -430,23 +429,19 @@
    1.44      assert(identity);
    1.45      assert(!EMPTYSTR(identity->address));
    1.46  
    1.47 -    // assert(EMPTYSTR(identity->user_id) ||
    1.48 -    //        strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.49 +    assert(EMPTYSTR(identity->user_id) ||
    1.50 +           strcmp(identity->user_id, PEP_OWN_USERID) == 0);
    1.51  
    1.52 -    // if (!(session && identity && !EMPTYSTR(identity->address) &&
    1.53 -    //         (EMPTYSTR(identity->user_id) ||
    1.54 -    //         strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.55 -    //     return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.56 -
    1.57 -    if (!(session && identity && !EMPTYSTR(identity->address)))
    1.58 +    if (!(session && identity && !EMPTYSTR(identity->address) &&
    1.59 +            (EMPTYSTR(identity->user_id) ||
    1.60 +            strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
    1.61          return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
    1.62  
    1.63      identity->comm_type = PEP_ct_pEp;
    1.64 +    identity->me = true;
    1.65      if(ignore_flags)
    1.66          identity->flags = 0;
    1.67      
    1.68 -    // FIXME: do we replace DB user_id and username if they WERE defaults?
    1.69 -
    1.70      if (EMPTYSTR(identity->user_id))
    1.71      {
    1.72          free(identity->user_id);
    1.73 @@ -564,8 +559,6 @@
    1.74          }
    1.75      }
    1.76     
    1.77 -    bool new_key_generated = false;
    1.78 -
    1.79      if (EMPTYSTR(identity->fpr) || revoked)
    1.80      {
    1.81          if(!do_keygen){
    1.82 @@ -590,7 +583,6 @@
    1.83              return ADD_TO_LOG(status);
    1.84          }
    1.85  
    1.86 -        new_key_generated = true;
    1.87          
    1.88          if(revoked)
    1.89          {
    1.90 @@ -630,15 +622,6 @@
    1.91          return status;
    1.92      }
    1.93  
    1.94 -    if(new_key_generated)
    1.95 -    {
    1.96 -        // if a state machine for keysync is in place, inject notify
    1.97 -        status = inject_DeviceState_event(session, KeyGen, NULL, NULL);
    1.98 -        if (status == PEP_OUT_OF_MEMORY){
    1.99 -            return PEP_OUT_OF_MEMORY;
   1.100 -        }
   1.101 -    }
   1.102 -
   1.103      return ADD_TO_LOG(PEP_STATUS_OK);
   1.104  }
   1.105  
   1.106 @@ -909,14 +892,15 @@
   1.107                      sqlite3_column_text(session->own_identities_retrieve, 0);
   1.108                  fpr = (const char *)
   1.109                      sqlite3_column_text(session->own_identities_retrieve, 1);
   1.110 -                user_id = PEP_OWN_USERID;
   1.111 +                user_id = (const char *)
   1.112 +                    sqlite3_column_text(session->own_identities_retrieve, 2);
   1.113                  username = (const char *)
   1.114 -                    sqlite3_column_text(session->own_identities_retrieve, 2);
   1.115 +                    sqlite3_column_text(session->own_identities_retrieve, 3);
   1.116                  comm_type = PEP_ct_pEp;
   1.117                  lang = (const char *)
   1.118 -                    sqlite3_column_text(session->own_identities_retrieve, 3);
   1.119 +                    sqlite3_column_text(session->own_identities_retrieve, 4);
   1.120                  flags = (unsigned int)
   1.121 -                    sqlite3_column_int(session->own_identities_retrieve, 4);
   1.122 +                    sqlite3_column_int(session->own_identities_retrieve, 5);
   1.123  
   1.124                  pEp_identity *ident = new_identity(address, fpr, user_id, username);
   1.125                  if (!ident)
   1.126 @@ -927,6 +911,7 @@
   1.127                      ident->lang[1] = lang[1];
   1.128                      ident->lang[2] = 0;
   1.129                  }
   1.130 +                ident->me = true;
   1.131                  ident->flags = flags;
   1.132  
   1.133                  _bl = identity_list_add(_bl, ident);
   1.134 @@ -1059,24 +1044,70 @@
   1.135            fpr && fpr[0]
   1.136           ))
   1.137          return PEP_ILLEGAL_VALUE;
   1.138 +            
   1.139 +            
   1.140 +    // First see if we have it in own identities already, AND we retrieve
   1.141 +    // our own user_id
   1.142 +    pEp_identity* my_id = NULL;
   1.143 +    identity_list* my_identities = NULL;
   1.144 +    char* my_user_id = NULL;
   1.145 +    status = own_identities_retrieve(session, &my_identities);
   1.146      
   1.147 -    sqlite3_reset(session->set_own_key);
   1.148 -    sqlite3_bind_text(session->set_own_key, 1, address, -1, SQLITE_STATIC);
   1.149 -    sqlite3_bind_text(session->set_own_key, 2, fpr, -1, SQLITE_STATIC);
   1.150 +    if (status == PEP_STATUS_OK) {
   1.151 +        if (my_identities) {
   1.152 +            if (!(my_identities->ident && my_identities->ident->user_id))
   1.153 +                return PEP_ILLEGAL_VALUE;
   1.154  
   1.155 -    int result;
   1.156 +            my_user_id = strdup(my_identities->ident->user_id);
   1.157 +
   1.158 +            if (!my_user_id) 
   1.159 +                return PEP_OUT_OF_MEMORY;
   1.160 +            
   1.161 +            // Probably cheaper than all the strcmps if there are many,
   1.162 +            // plus this avoids the capitalisation and . problems:
   1.163 +            
   1.164 +            status = get_identity(session, my_user_id, address, &my_id);
   1.165 +            
   1.166 +            if (status == PEP_STATUS_OK && my_id) {
   1.167 +                if (my_id->fpr && strcasecmp(my_id->fpr, fpr) == 0)) {
   1.168 +                    // We're done. It was already here.
   1.169 +                    // FIXME: Do we check trust/revocation/?
   1.170 +                    goto pep_free;
   1.171 +                }            
   1.172 +            }
   1.173 +            
   1.174 +            // Otherwise, we see if there's a binding for this user_id/key
   1.175 +            // in the trust DB
   1.176 +            
   1.177 +            // If there's an id w/ user_id + address
   1.178 +            if (my_id) {
   1.179 +                free(my_id->fpr);
   1.180 +                my_id->fpr = my_user_id;
   1.181 +                my_user_id->comm_type = PEP_ct_pEp;
   1.182 +            }
   1.183 +            else { // Else, we need a new identity
   1.184 +                status = new_identity(session, address, fpr, my_user_id, NULL, &my_id); 
   1.185 +                if (status != PEP_STATUS_OK)
   1.186 +                    goto pep_free; 
   1.187 +                my_user_id->me = true;
   1.188 +                my_user_id->comm_type = PEP_ct_pEp;
   1.189 +            }
   1.190 +        }
   1.191 +        else {
   1.192 +            // I think the prerequisite should be that at least one own identity
   1.193 +            // already in the DB, so REALLY look at this.
   1.194 +            // status = new_identity(session, address, fpr, "PEP_OWN_USERID", NULL); 
   1.195 +            // my_user_id->me = true;
   1.196 +            // my_user_id->comm_type = PEP_ct_pEp;
   1.197 +            return PEP_CANNOT_FIND_IDENTITY;
   1.198 +        }
   1.199 +        
   1.200 +        status = set_identity(session, my_id);
   1.201 +    }  
   1.202      
   1.203 -    result = sqlite3_step(session->set_own_key);
   1.204 -    switch (result) {
   1.205 -        case SQLITE_DONE:
   1.206 -            status = PEP_STATUS_OK;
   1.207 -            break;
   1.208 -            
   1.209 -        default:
   1.210 -            status = PEP_UNKNOWN_ERROR;
   1.211 -    }
   1.212 -    
   1.213 -    sqlite3_reset(session->set_own_key);
   1.214 +pep_free:
   1.215 +    free(my_id);
   1.216 +    free(my_user_id);
   1.217      return status;
   1.218  }
   1.219