ENGINE-463: remaining functionality and integration into replace_userid. STILL UNTESTED ENGINE-463
authorKrista Bennett <krista@pep-project.org>
Tue, 02 Oct 2018 20:08:32 +0200
branchENGINE-463
changeset 29724a68c8c4726c
parent 2971 124bcf396623
child 2977 dbb4edf4d231
ENGINE-463: remaining functionality and integration into replace_userid. STILL UNTESTED
src/pEpEngine.c
     1.1 --- a/src/pEpEngine.c	Tue Oct 02 16:37:46 2018 +0200
     1.2 +++ b/src/pEpEngine.c	Tue Oct 02 20:08:32 2018 +0200
     1.3 @@ -2941,6 +2941,35 @@
     1.4      return PEP_STATUS_OK;
     1.5  }
     1.6  
     1.7 +PEP_STATUS get_trust_by_userid(PEP_SESSION session, const char* user_id,
     1.8 +                                           labeled_int_list_t** trust_list)
     1.9 +{
    1.10 +    int result;
    1.11 +
    1.12 +    if (!(session && user_id && user_id[0]))
    1.13 +        return PEP_ILLEGAL_VALUE;
    1.14 +
    1.15 +    *trust_list = NULL;
    1.16 +    labeled_int_list_t* t_list = new_labeled_int_list(0, NULL); // empty
    1.17 +
    1.18 +    sqlite3_reset(session->get_trust_by_userid);
    1.19 +    sqlite3_bind_text(session->get_trust_by_userid, 1, user_id, -1, SQLITE_STATIC);
    1.20 +
    1.21 +    while ((result = sqlite3_step(session->get_trust_by_userid)) == SQLITE_ROW) {
    1.22 +        labeled_int_list_add(t_list, sqlite3_column_int(session->get_trust_by_userid, 1),
    1.23 +                            (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
    1.24 +    }
    1.25 +
    1.26 +    sqlite3_reset(session->get_trust_by_userid);
    1.27 +
    1.28 +    if (!t_list->label)
    1.29 +        free_labeled_int_list(t_list);
    1.30 +    else
    1.31 +        *trust_list = t_list;
    1.32 +        
    1.33 +    return PEP_STATUS_OK;
    1.34 +}
    1.35 +
    1.36  PEP_comm_type reconcile_trust(PEP_comm_type t_old, PEP_comm_type t_new) {
    1.37      switch (t_new) {
    1.38          case PEP_ct_mistrusted:
    1.39 @@ -3059,6 +3088,17 @@
    1.40      return status;
    1.41  }
    1.42  
    1.43 +void reconcile_language(pEp_identity* old_ident,
    1.44 +                        pEp_identity* new_ident) {
    1.45 +    if (new_ident->lang[0] == 0) {
    1.46 +        if (old_ident->lang[0] != 0) {
    1.47 +            new_ident->lang[0] = old_ident->lang[0];
    1.48 +            new_ident->lang[1] = old_ident->lang[1];
    1.49 +            new_ident->lang[2] = old_ident->lang[2];
    1.50 +        }
    1.51 +    }
    1.52 +}
    1.53 +
    1.54  // ONLY CALL THIS IF BOTH IDs ARE IN THE PERSON DB, FOOL! </Mr_T>
    1.55  PEP_STATUS merge_records(PEP_SESSION session, const char* old_uid,
    1.56                           const char* new_uid) {
    1.57 @@ -3072,6 +3112,7 @@
    1.58      identity_list* old_identities = NULL;
    1.59      labeled_int_list_t* trust_list = NULL;
    1.60      stringlist_t* touched_keys = new_stringlist(NULL);
    1.61 +    char* main_user_fpr = NULL;
    1.62          
    1.63      status = get_identities_by_userid(session, old_uid, &old_identities);
    1.64      if (status == PEP_STATUS_OK && old_identities) {
    1.65 @@ -3114,19 +3155,67 @@
    1.66                  if (status != PEP_STATUS_OK)
    1.67                      goto pEp_free;
    1.68                      
    1.69 +                // reconcile languages
    1.70 +                reconcile_language(old_ident, new_ident);
    1.71 +
    1.72 +                // reconcile flags - FIXME - is this right?
    1.73 +                new_ident->flags |= old_ident->flags;
    1.74 +                
    1.75 +                // NOTE: In principle, this is only called from update_identity,
    1.76 +                // which would never have me flags set. So I am ignoring them here.
    1.77 +                // if this function is ever USED for that, though, you'll have
    1.78 +                // to go through making sure that the user ids are appropriately
    1.79 +                // aliased, etc. So be careful.
    1.80 +                
    1.81                  // Set the reconciled record
    1.82 +                    
    1.83                  status = set_identity(session, new_ident);
    1.84                  if (status != PEP_STATUS_OK)
    1.85                      goto pEp_free;
    1.86 -                    
    1.87 +
    1.88 +                if (new_ident->fpr)
    1.89 +                    stringlist_add(touched_keys, new_ident->fpr);
    1.90 +                        
    1.91                  free_identity(new_ident);
    1.92                  new_ident = NULL;    
    1.93              }
    1.94          }
    1.95      }
    1.96      // otherwise, no need to reconcile identity records. But maybe trust...    
    1.97 +    new_ident = new_identity(NULL, NULL, new_uid, NULL);
    1.98 +    if (!new_ident) {
    1.99 +        status = PEP_OUT_OF_MEMORY;
   1.100 +        goto pEp_free;
   1.101 +    }
   1.102 +    status = get_trust_by_userid(session, old_uid, &trust_list);
   1.103 +
   1.104 +    labeled_int_list_t* trust_curr = trust_list;
   1.105 +    for (; trust_curr && trust_curr->label; trust_curr = trust_curr->next) {
   1.106 +        const char* curr_fpr = trust_curr->label;
   1.107 +        new_ident->fpr = strdup(curr_fpr); 
   1.108 +        status = get_trust(session, new_ident);
   1.109 +        if (status == PEP_STATUS_OK) {
   1.110 +            new_ident->comm_type = reconcile_trust(trust_curr->value,
   1.111 +                                                   new_ident->comm_type);
   1.112 +            status = set_trust(session, new_ident);
   1.113 +            if (status != PEP_STATUS_OK) {
   1.114 +                goto pEp_free;
   1.115 +            }                                        
   1.116 +        }
   1.117 +        free(new_ident->fpr);
   1.118 +        new_ident->fpr = NULL;
   1.119 +        new_ident->comm_type = 0;
   1.120 +    }
   1.121  
   1.122      // reconcile the default keys if the new id doesn't have one?
   1.123 +    status = get_main_user_fpr(session, new_uid, &main_user_fpr);
   1.124 +    if (status == PEP_KEY_NOT_FOUND || (status == PEP_STATUS_OK && !main_user_fpr)) {
   1.125 +        status = get_main_user_fpr(session, old_uid, &main_user_fpr);
   1.126 +        if (status == PEP_STATUS_OK && main_user_fpr)
   1.127 +            status = replace_main_user_fpr(session, new_uid, main_user_fpr);
   1.128 +        if (status != PEP_STATUS_OK)
   1.129 +            goto pEp_free;
   1.130 +    }
   1.131      
   1.132      // delete the old user
   1.133      status = delete_person(session, old_uid);
   1.134 @@ -3136,11 +3225,12 @@
   1.135      free_identity_list(old_identities);
   1.136      free_labeled_int_list(trust_list);
   1.137      free_stringlist(touched_keys);
   1.138 +    free(main_user_fpr);
   1.139      return status;
   1.140  }
   1.141  
   1.142  PEP_STATUS replace_userid(PEP_SESSION session, const char* old_uid,
   1.143 -                              const char* new_uid) {
   1.144 +                          const char* new_uid) {
   1.145      assert(session);
   1.146      assert(old_uid);
   1.147      assert(new_uid);
   1.148 @@ -3148,6 +3238,15 @@
   1.149      if (!session || !old_uid || !new_uid)
   1.150          return PEP_ILLEGAL_VALUE;
   1.151  
   1.152 +    pEp_identity* temp_ident = new_identity(NULL, NULL, new_uid, NULL);
   1.153 +    bool new_exists = false;
   1.154 +    PEP_STATUS status = exists_person(session, temp_ident, &new_exists);
   1.155 +    free_identity(temp_ident);
   1.156 +    if (status != PEP_STATUS_OK) // DB error
   1.157 +        return status;
   1.158 +        
   1.159 +    if (new_exists)
   1.160 +        return merge_records(session, old_uid, new_uid);
   1.161  
   1.162      int result;
   1.163  
   1.164 @@ -3315,8 +3414,8 @@
   1.165          return PEP_ILLEGAL_VALUE;
   1.166  
   1.167      identity->comm_type = PEP_ct_unknown;
   1.168 -
   1.169      sqlite3_reset(session->get_trust);
   1.170 +
   1.171      sqlite3_bind_text(session->get_trust, 1, identity->user_id, -1,
   1.172              SQLITE_STATIC);
   1.173      sqlite3_bind_text(session->get_trust, 2, identity->fpr, -1, SQLITE_STATIC);
   1.174 @@ -3338,34 +3437,6 @@
   1.175      return status;
   1.176  }
   1.177  
   1.178 -PEP_STATUS get_trust_by_userid(PEP_SESSION session, const char* user_id,
   1.179 -                                           labeled_int_list_t** trust_list)
   1.180 -{
   1.181 -    int result;
   1.182 -
   1.183 -    if (!(session && user_id && user_id[0]))
   1.184 -        return PEP_ILLEGAL_VALUE;
   1.185 -
   1.186 -    *trust_list = NULL;
   1.187 -    labeled_int_list_t* t_list = new_labeled_int_list(0, NULL); // empty
   1.188 -
   1.189 -    sqlite3_reset(session->get_trust_by_userid);
   1.190 -    sqlite3_bind_text(session->get_trust_by_userid, 1, user_id, -1, SQLITE_STATIC);
   1.191 -
   1.192 -    while ((result = sqlite3_step(session->get_trust_by_userid)) == SQLITE_ROW) {
   1.193 -        labeled_int_list_add(t_list, sqlite3_column_int(session->get_trust_by_userid, 1),
   1.194 -                            (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
   1.195 -    }
   1.196 -
   1.197 -    sqlite3_reset(session->get_trust_by_userid);
   1.198 -
   1.199 -    if (!t_list->label)
   1.200 -        free_labeled_int_list(t_list);
   1.201 -    else
   1.202 -        *trust_list = t_list;
   1.203 -        
   1.204 -    return PEP_STATUS_OK;
   1.205 -}
   1.206  
   1.207  DYNAMIC_API PEP_STATUS least_trust(
   1.208          PEP_SESSION session,