Committing to move to proper branch ENGINE-289
authorKrista Bennett <krista@pep-project.org>
Mon, 27 Nov 2017 16:23:13 +0100
branchENGINE-289
changeset 230162ed2fc84b3a
parent 2300 cfa7b4d7e59d
child 2302 3fbe4956dd34
child 2309 b4c78872e2c1
Committing to move to proper branch
src/keymanagement.c
src/pEpEngine.c
src/pEp_internal.h
     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  
     2.1 --- a/src/pEpEngine.c	Wed Nov 22 10:44:14 2017 +0100
     2.2 +++ b/src/pEpEngine.c	Mon Nov 27 16:23:13 2017 +0100
     2.3 @@ -174,23 +174,17 @@
     2.4                  
     2.5  
     2.6  // Own keys
     2.7 -// FIXME: PEP_OWN_USERID
     2.8 +// We only care if it's 0 or non-zero
     2.9  static const char *sql_own_key_is_listed = 
    2.10      "select count(*) from ("
    2.11 -    " select main_key_id from person "
    2.12 -    "   where main_key_id = upper(replace(?1,' ',''))"
    2.13 -    "    and id = '" PEP_OWN_USERID "' "
    2.14 -    " union "
    2.15 -    "  select main_key_id from identity "
    2.16 -    "   where main_key_id = upper(replace(?1,' ',''))"
    2.17 -    "    and user_id = '" PEP_OWN_USERID "' "
    2.18 -    " union "
    2.19 -    "  select fpr from own_keys "
    2.20 -    "   where fpr = upper(replace(?1,' ',''))"
    2.21 -    " );";
    2.22 +    "   select fpr from trust"
    2.23 +    "      join identity on id = identity.user_id"
    2.24 +    "      where fpr = upper(replace(?1,' ',''))"
    2.25 +    "           and identity.is_own = 1;"
    2.26 +    ");";
    2.27  
    2.28  static const char *sql_own_identities_retrieve =  
    2.29 -    "select address, fpr, username, "
    2.30 +    "select address, fpr, username, user_id, "
    2.31      "   lang, identity.flags | pgp_keypair.flags"
    2.32      "   from identity"
    2.33      "   join person on id = identity.user_id"
    2.34 @@ -200,18 +194,16 @@
    2.35      "   where identity.is_own = 1"
    2.36      "       and (identity.flags & ?1) = 0;";
    2.37  
    2.38 -// FIXME: PEP_OWN_USERID        
    2.39  static const char *sql_own_keys_retrieve =  
    2.40 -    "select fpr from own_keys"
    2.41 -    "   natural join identity"
    2.42 -    "   where (identity.flags & ?1) = 0;";
    2.43 +    "select fpr from trust"
    2.44 +    "   join identity on id = identity.user_id"
    2.45 +    "   where identity.is_own = 1"
    2.46  
    2.47  // FIXME: PEP_OWN_USERID
    2.48  static const char *sql_set_own_key = 
    2.49      "insert or replace into own_keys (address, user_id, fpr)"
    2.50      " values (?1, '" PEP_OWN_USERID "', upper(replace(?2,' ','')));";
    2.51  
    2.52 -
    2.53  // Sequence
    2.54  static const char *sql_sequence_value1 = 
    2.55      "insert or replace into sequences (name, value, own) "
     3.1 --- a/src/pEp_internal.h	Wed Nov 22 10:44:14 2017 +0100
     3.2 +++ b/src/pEp_internal.h	Mon Nov 27 16:23:13 2017 +0100
     3.3 @@ -151,6 +151,7 @@
     3.4      // Own keys
     3.5      sqlite3_stmt *own_key_is_listed;
     3.6      sqlite3_stmt *own_identities_retrieve;
     3.7 +    sqlite3_stmt *own_userid_by_address;
     3.8      sqlite3_stmt *own_keys_retrieve;
     3.9      sqlite3_stmt *set_own_key;
    3.10