ENGINE-293: Intermittent commit. Probably not compiling yet, but PEP_OWN_USERID is out from src where it needs to be.

doc_update_sequoia
Krista Bennett 5 years ago
parent 580c0959db
commit 42a4895cc9

@ -104,13 +104,18 @@ DYNAMIC_API PEP_STATUS update_identity(
if (_no_user_id)
{
status = get_identity(session, identity->address, PEP_OWN_USERID,
&stored_identity);
if (status == PEP_STATUS_OK) {
free_identity(stored_identity);
return _myself(session, identity, false, true);
char* own_id = NULL;
status = get_own_userid(session, &own_id);
if (status == PEP_STATUS_OK && own_id) {
status = get_identity(session, identity->address, own_id,
&stored_identity);
if (status == PEP_STATUS_OK) {
free_identity(stored_identity);
identity->user_id = own_id;
return _myself(session, identity, false, true);
}
}
free(identity->user_id);
identity->user_id = calloc(1, strlen(identity->address) + 6);
@ -120,6 +125,7 @@ DYNAMIC_API PEP_STATUS update_identity(
}
snprintf(identity->user_id, strlen(identity->address) + 6,
"TOFU_%s", identity->address);
}
}
status = get_identity(session,
@ -429,12 +435,15 @@ PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen,
assert(identity);
assert(!EMPTYSTR(identity->address));
char* own_id = NULL;
status = get_own_userid(session, &own_id);
assert(EMPTYSTR(identity->user_id) ||
strcmp(identity->user_id, PEP_OWN_USERID) == 0);
(own_id && strcmp(identity->user_id, own_id) == 0));
if (!(session && identity && !EMPTYSTR(identity->address) &&
(EMPTYSTR(identity->user_id) ||
strcmp(identity->user_id, PEP_OWN_USERID) == 0)))
(own_id && strcmp(identity->user_id, own_id) == 0)))
return ADD_TO_LOG(PEP_ILLEGAL_VALUE);
identity->comm_type = PEP_ct_pEp;
@ -445,7 +454,7 @@ PEP_STATUS _myself(PEP_SESSION session, pEp_identity * identity, bool do_keygen,
if (EMPTYSTR(identity->user_id))
{
free(identity->user_id);
identity->user_id = strdup(PEP_OWN_USERID);
identity->user_id = (own_id ? own_id : strdup(PEP_OWN_USERID));
assert(identity->user_id);
if (identity->user_id == NULL)
return PEP_OUT_OF_MEMORY;

@ -50,7 +50,8 @@ static const char *sql_trustword =
static const char *sql_get_identity =
"select fpr, username, comm_type, lang,"
" identity.flags | pgp_keypair.flags"
" identity.flags | pgp_keypair.flags,"
" is_own"
" from identity"
" join person on id = identity.user_id"
" join pgp_keypair on fpr = identity.main_key_id"
@ -77,15 +78,13 @@ static const char *sql_set_person =
" where id = ?1), upper(replace(?4,' ','')))),"
" (select device_group from person where id = ?1)) ;";
// FIXME: PEP_OWN_USERID
static const char *sql_set_device_group =
"update person set device_group = ?1 "
"where id = '" PEP_OWN_USERID "';";
"where id = ?2;";
// FIXME: PEP_OWN_USERID
static const char *sql_get_device_group =
"select device_group from person "
"where id = '" PEP_OWN_USERID "';";
"where id = ?1;";
static const char *sql_set_pgp_keypair =
"insert or replace into pgp_keypair (fpr) "
@ -94,7 +93,7 @@ static const char *sql_set_pgp_keypair =
static const char *sql_set_identity =
"insert or replace into identity ("
" address, main_key_id, "
" user_id, flags"
" user_id, flags, is_own"
") values ("
" ?1,"
" upper(replace(?2,' ','')),"
@ -107,7 +106,8 @@ static const char *sql_set_identity =
// " 0)"
// " ) | (?4 & 255)"
/* set_identity ignores previous flags, and doesn't filter machine flags */
" ?4"
" ?4,"
" ?5"
");";
static const char *sql_set_identity_flags =
@ -197,12 +197,12 @@ static const char *sql_own_identities_retrieve =
static const char *sql_own_keys_retrieve =
"select fpr from trust"
" join identity on id = identity.user_id"
" where identity.is_own = 1"
" where identity.is_own = 1";
// FIXME: PEP_OWN_USERID
static const char *sql_set_own_key =
"insert or replace into own_keys (address, user_id, fpr)"
" values (?1, '" PEP_OWN_USERID "', upper(replace(?2,' ','')));";
static const char* sql_get_own_userid =
"select user_id from person"
" join identity on id = identity.user_id"
" where identity.is_own = 1";
// Sequence
static const char *sql_sequence_value1 =
@ -355,7 +355,7 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
sqlite3_busy_timeout(_session->system_db, 1000);
// increment this when patching DDL
#define _DDL_USER_VERSION "5"
#define _DDL_USER_VERSION "6"
if (in_first) {
@ -445,18 +445,6 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
" on delete cascade,\n"
" revocation_date integer\n"
");\n"
"create table if not exists own_keys (\n"
" address text,\n"
" user_id text,\n"
" fpr text not null\n"
" references pgp_keypair (fpr)\n"
" on delete cascade,\n"
" foreign key (address, user_id)\n"
" references identity\n"
" on delete cascade\n"
// " check (user_id = '" PEP_OWN_USERID "')\n"
" primary key (address, fpr)\n"
");\n"
,
NULL,
NULL,
@ -562,7 +550,18 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
NULL,
NULL,
NULL
);
);
assert(int_result = SQLITE_OK);
int_result = sqlite3_exec(
_session->db,
"update identity\n"
" set is_own = 1\n"
" where (user_id = '" PEP_OWN_USERID "');\n",
NULL,
NULL,
NULL
);
assert(int_result = SQLITE_OK);
}
}
else {
@ -601,6 +600,10 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
(int)strlen(sql_get_identity), &_session->get_identity, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_get_own_userid,
(int)strlen(sql_get_own_userid), &_session->get_own_userid, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_replace_identities_fpr,
(int)strlen(sql_replace_identities_fpr),
&_session->replace_identities_fpr, NULL);
@ -708,10 +711,10 @@ DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
&_session->own_keys_retrieve, NULL);
assert(int_result == SQLITE_OK);
int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
(int)strlen(sql_set_own_key),
&_session->set_own_key, NULL);
assert(int_result == SQLITE_OK);
// int_result = sqlite3_prepare_v2(_session->db, sql_set_own_key,
// (int)strlen(sql_set_own_key),
// &_session->set_own_key, NULL);
// assert(int_result == SQLITE_OK);
// Sequence
@ -842,6 +845,8 @@ DYNAMIC_API void release(PEP_SESSION session)
sqlite3_finalize(session->trustword);
if (session->get_identity)
sqlite3_finalize(session->get_identity);
if (session->get_own_userid)
sqlite3_finalize(session->get_own_userid);
if (session->replace_identities_fpr)
sqlite3_finalize(session->replace_identities_fpr);
if (session->set_person)
@ -888,8 +893,8 @@ DYNAMIC_API void release(PEP_SESSION session)
sqlite3_finalize(session->own_identities_retrieve);
if (session->own_keys_retrieve)
sqlite3_finalize(session->own_keys_retrieve);
if (session->set_own_key)
sqlite3_finalize(session->set_own_key);
// if (session->set_own_key)
// sqlite3_finalize(session->set_own_key);
if (session->sequence_value1)
sqlite3_finalize(session->sequence_value1);
if (session->sequence_value2)
@ -1208,6 +1213,49 @@ void free_identity(pEp_identity *identity)
}
}
DYNAMIC_API PEP_STATUS get_own_userid(
PEP_SESSION session,
char** userid
)
{
assert(session);
assert(userid);
if (!session || !userid)
return PEP_ILLEGAL_VALUE;
PEP_STATUS status = PEP_STATUS_OK;
char* retval = NULL;
sqlite3_reset(session->get_own_userid);
const int result = sqlite3_step(session->get_own_userid);
switch (result) {
case SQLITE_ROW:
const char* id = (const char *) sqlite3_column_text(session->get_own_userid, 0);
if (!id) {
// Shouldn't happen.
status = PEP_UNKNOWN_ERROR;
}
else {
retval = strdup(id);
if (!retval)
status = PEP_OUT_OF_MEMORY;
}
break;
default:
// Technically true, given how we find it, but FIXME we need a more descriptive error
status = PEP_CANNOT_FIND_IDENTITY;
*identity = NULL;
}
*userid = retval;
sqlite3_reset(session->get_own_userid);
return status;
}
DYNAMIC_API PEP_STATUS get_identity(
PEP_SESSION session,
const char *address,
@ -1259,6 +1307,9 @@ DYNAMIC_API PEP_STATUS get_identity(
}
_identity->flags = (unsigned int)
sqlite3_column_int(session->get_identity, 4);
_identity->me = (unsigned int)
sqlite3_column_int(session->get_identity, 5);
*identity = _identity;
break;
default:
@ -1350,6 +1401,7 @@ DYNAMIC_API PEP_STATUS set_identity(
sqlite3_bind_text(session->set_identity, 3, identity->user_id, -1,
SQLITE_STATIC);
sqlite3_bind_int(session->set_identity, 4, identity->flags);
sqlite3_bind_int(session->set_identity, 5, identity->me);
result = sqlite3_step(session->set_identity);
sqlite3_reset(session->set_identity);
if (result != SQLITE_DONE) {
@ -1358,22 +1410,6 @@ DYNAMIC_API PEP_STATUS set_identity(
}
if (has_fpr) {
if(_identity_me(identity)) {
sqlite3_reset(session->set_own_key);
sqlite3_bind_text(session->set_own_key, 1, identity->address, -1,
SQLITE_STATIC);
sqlite3_bind_text(session->set_own_key, 2, identity->fpr, -1,
SQLITE_STATIC);
result = sqlite3_step(session->set_own_key);
sqlite3_reset(session->set_own_key);
if (result != SQLITE_DONE) {
sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
return PEP_CANNOT_SET_PGP_KEYPAIR;
}
}
// status = set_trust(session, identity->user_id, identity->fpr,
// identity->comm_type)
sqlite3_reset(session->set_trust);
sqlite3_bind_text(session->set_trust, 1, identity->user_id, -1,
SQLITE_STATIC);
@ -1453,6 +1489,15 @@ DYNAMIC_API PEP_STATUS set_device_group(
if (!(session && group_name))
return PEP_ILLEGAL_VALUE;
// 1. Get own user_id
char* user_id = NULL;
PEP_STATUS status = get_own_userid(session, &user_id);
// No user_id is returned in this case, no need to free;
if (status != PEP_STATUS_OK)
return status;
// 2. Set device group
sqlite3_reset(session->set_device_group);
if(group_name){
sqlite3_bind_text(session->set_device_group, 1, group_name, -1,
@ -1460,9 +1505,15 @@ DYNAMIC_API PEP_STATUS set_device_group(
} else {
sqlite3_bind_null(session->set_device_group, 1);
}
sqlite3_bind_text(session->set_device_group, 2, user_id, -1,
SQLITE_STATIC);
result = sqlite3_step(session->set_device_group);
sqlite3_reset(session->set_device_group);
free(user_id);
if (result != SQLITE_DONE)
return PEP_CANNOT_SET_PERSON;
@ -1480,7 +1531,18 @@ DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
if (!(session && group_name))
return PEP_ILLEGAL_VALUE;
// 1. Get own user_id
char* user_id = NULL;
PEP_STATUS status = get_own_userid(session, &user_id);
// No user_id is returned in this case, no need to free;
if (status != PEP_STATUS_OK)
return status;
// 2. get device group
sqlite3_reset(session->get_device_group);
sqlite3_bind_text(session->get_device_group, 1, user_id, -1,
SQLITE_STATIC);
result = sqlite3_step(session->get_device_group);
switch (result) {
@ -1498,6 +1560,7 @@ DYNAMIC_API PEP_STATUS get_device_group(PEP_SESSION session, char **group_name)
status = PEP_RECORD_NOT_FOUND;
}
free(user_id);
sqlite3_reset(session->get_device_group);
return status;
}

@ -601,6 +601,27 @@ DYNAMIC_API PEP_STATUS set_identity(
PEP_SESSION session, const pEp_identity *identity
);
// get_own_userid() - get the user_id of the own user
//
// parameters:
// session (in) session handle
// userid (out) own user id (if it exists)
//
// return value:
// PEP_STATUS_OK = 0 userid was found
// PEP_CANNOT_FIND_IDENTITY no own_user found in the DB
// PEP_UNKNOWN_ERROR results were returned, but no ID
// found (no reason this should ever occur)
// caveat:
// userid will be NULL if not found; otherwise, returned string
// belongs to the caller.
DYNAMIC_API PEP_STATUS get_own_userid(
PEP_SESSION session,
char** userid
);
// set_device_group() - update own person's device group
//
// parameters:

@ -153,7 +153,7 @@ struct _pEpSession {
sqlite3_stmt *own_identities_retrieve;
sqlite3_stmt *own_userid_by_address;
sqlite3_stmt *own_keys_retrieve;
sqlite3_stmt *set_own_key;
// sqlite3_stmt *set_own_key;
// sequence value
sqlite3_stmt *sequence_value1;

@ -56,8 +56,14 @@ int keyElectionWon(PEP_SESSION session, Identity partner)
// key created first wins
Identity me = NULL;
PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
&me);
char* own_id = NULL;
status = get_own_userid(session, &own_id);
if (own_id) {
PEP_STATUS status = get_identity(session, partner->address, own_id,
&me);
free(own_id);
}
if (status == PEP_OUT_OF_MEMORY)
return invalid_out_of_memory;
if (status != PEP_STATUS_OK)
@ -142,9 +148,15 @@ PEP_STATUS _notifyHandshake(
if (!session->notifyHandshake)
return PEP_SYNC_NO_NOTIFY_CALLBACK;
char* own_id = NULL;
status = get_own_userid(session, &own_id);
// notifyHandshake take ownership of given identities
pEp_identity *me = NULL;
status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
if (own_id) {
status = get_identity(session, partner->address, own_id, &me);
free(own_id);
}
if (status != PEP_STATUS_OK)
goto error;
@ -235,16 +247,23 @@ PEP_STATUS _storeGroupKeys(
)
{
PEP_STATUS status = PEP_STATUS_OK;
char* own_id = NULL;
status = get_own_userid(session, &own_id);
// FIXME: Is this where and what we wanna do with this?
if (status != PEP_STATUS_OK)
return status;
for (identity_list *il = group_keys; il && il->ident; il = il->next) {
if (strcmp(il->ident->user_id, PEP_OWN_USERID)!=0) {
if (strcmp(il->ident->user_id, own_id)!=0) {
assert(0);
continue;
}
// Check that identity isn't excluded from sync.
pEp_identity *stored_identity = NULL;
status = get_identity(session, il->ident->address, PEP_OWN_USERID,
status = get_identity(session, il->ident->address, own_id,
&stored_identity);
if (status == PEP_STATUS_OK) {
if(stored_identity->flags & PEP_idf_not_for_sync){

@ -275,16 +275,25 @@ PEP_STATUS receive_sync_msg(
// partner identity must be explicitely added DB to later
// be able to communicate securely with it.
if(partner){
char* own_id = NULL;
status = get_own_userid(session, &own_id);
if (!own_id)
own_id = strdup(PEP_OWN_USERID);
// protect virtual user IDs
if((strncmp("TOFU_", partner->user_id, 6) == 0 &&
strlen(partner->user_id) == strlen(partner->address) + 6 &&
strcmp(partner->user_id + 6, partner->address)) ||
// protect own ID
(strcmp(PEP_OWN_USERID, partner->user_id) == 0)){
(strcmp(own_id, partner->user_id) == 0)){
status = PEP_SYNC_ILLEGAL_MESSAGE;
free(own_id);
goto error;
}
free(own_id);
// partner IDs are UUIDs bound to session lifespan
// and therefore partner identities are not supposed
// to mutate over time, but just not be used anymore.
@ -426,6 +435,9 @@ PEP_STATUS receive_DeviceState_msg(
bool discard = false;
bool force_keep_msg = false;
char* own_id = NULL;
PEP_STATUS own_id_status = get_own_userid(session, &own_id);
for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0
&& bl->size) {
@ -451,11 +463,18 @@ PEP_STATUS receive_DeviceState_msg(
msg->header.me.address->size);
if(null_terminated_address){
status = get_identity(session,
null_terminated_address,
PEP_OWN_USERID,
&check_me);
free(null_terminated_address);
if (own_id) {
status = get_identity(session,
null_terminated_address,
own_id,
&check_me);
free(null_terminated_address);
}
else {
status = own_id_status;
}
}
else
status = PEP_OUT_OF_MEMORY;
@ -608,10 +627,21 @@ PEP_STATUS receive_DeviceState_msg(
// GroupUpdate and UpdateRequests come from group.
// check trust relation in between signer key and
// own id to be sure.
pEp_identity *_from = new_identity(NULL,
keylist->value,
PEP_OWN_USERID,
NULL);
if (status != PEP_STATUS_OK)
goto free_all;
if (own_id) {
pEp_identity *_from = new_identity(NULL,
keylist->value,
own_id,
NULL);
}
else {
status = own_id_status;
goto free_all;
}
if (_from == NULL){
status = PEP_OUT_OF_MEMORY;
goto free_all;
@ -658,7 +688,7 @@ PEP_STATUS receive_DeviceState_msg(
ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
free_userid:
free(user_id);
free(own_id);
if (status != PEP_STATUS_OK)
return status;
}
@ -765,10 +795,15 @@ PEP_STATUS unicast_msg(
goto error;
}
char* own_id = NULL;
status = get_own_userid(session, &own_id);
if (status != PEP_STATUS_OK)
goto error;
msg->header.version.major = SYNC_VERSION_MAJOR;
msg->header.version.minor = SYNC_VERSION_MINOR;
status = get_identity(session, partner->address, PEP_OWN_USERID, &me);
status = get_identity(session, partner->address, own_id, &me);
if (status != PEP_STATUS_OK)
goto error;
@ -889,6 +924,7 @@ error:
free(payload);
free_message(_message);
free_identity(me);
free(own_id);
return status;
}

Loading…
Cancel
Save