sync/cond_act_sync.yml2
branchsync
changeset 2844 6fce34991f7d
parent 2838 7eef15a2c61e
child 2845 d474c5ebfec8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sync/cond_act_sync.yml2	Mon Aug 13 16:45:23 2018 +0200
     1.3 @@ -0,0 +1,253 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +// generate conditions and actions
     1.8 +
     1.9 +// Copyleft (c) 2017, p≡p foundation
    1.10 +
    1.11 +// Written by Volker Birk
    1.12 +
    1.13 +
    1.14 +include ./sql_func.yml2
    1.15 +
    1.16 +// condition: PEP_STATUS «@name»(PEP_SESSION session, bool *result)
    1.17 +
    1.18 +condition deviceGrouped {
    1.19 +    call "exec_sql_int" with "sql"
    1.20 +        > "select count(*) from identity where user_id = '"PEP_OWN_USERID"' and (flags & 4) = 4;"
    1.21 +    |> *result = _result > 0;
    1.22 +}
    1.23 +
    1.24 +condition partnerIsGrouped
    1.25 +|> *result = session->sync_state.keysync.is_group;
    1.26 +
    1.27 +condition challengeAccepted
    1.28 +||
    1.29 +    TID_t *t1 = &session->sync_state.keysync.challenge;
    1.30 +    TID_t *t2 = &session->own_sync_state.challenge;
    1.31 +
    1.32 +    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
    1.33 +||
    1.34 +
    1.35 +condition keyElectionWon
    1.36 +||
    1.37 +    pEp_identity *from = session->sync_state.common.from;
    1.38 +
    1.39 +    assert(from && from->fpr && from->fpr[0] && from->address && from->address[0]);
    1.40 +    if (!(from && from->fpr && from->fpr[0] && from->address && from->address[0]))
    1.41 +        return PEP_ILLEGAL_VALUE;
    1.42 +
    1.43 +    pEp_identity *me = NULL;
    1.44 +    PEP_STATUS status = get_identity(session, from->address, PEP_OWN_USERID, &me);
    1.45 +    assert(status == PEP_STATUS_OK);
    1.46 +    if (status)
    1.47 +        return status;
    1.48 +
    1.49 +    assert(me->fpr && me->fpr[0]);
    1.50 +    if (!(me->fpr && me->fpr[0])) {
    1.51 +        free_identity(me);
    1.52 +        return PEP_ILLEGAL_VALUE;
    1.53 +    }
    1.54 +
    1.55 +    size_t len = MIN(strlen(from->fpr), strlen(me->fpr));
    1.56 +    *result = strncasecmp(from->fpr, me->fpr, len) > 0;
    1.57 +    free_identity(me);
    1.58 +||
    1.59 +
    1.60 +// action: PEP_STATUS «@name»(PEP_SESSION session)
    1.61 +
    1.62 +action closeHandshakeDialog
    1.63 +||
    1.64 +    assert(session->notifyHandshake);
    1.65 +    if (!session->notifyHandshake)
    1.66 +        return PEP_SYNC_NO_NOTIFY_CALLBACK;
    1.67 +
    1.68 +    PEP_STATUS status = session->notifyHandshake(
    1.69 +            session->sync_management, NULL, NULL, SYNC_NOTIFY_OVERTAKEN);
    1.70 +    if (status)
    1.71 +        return status;
    1.72 +||
    1.73 +
    1.74 +function "new_UUID" {
    1.75 +    param "dst";
    1.76 +    ||
    1.77 +        pEpUUID c;
    1.78 +        uuid_generate_random(c);
    1.79 +
    1.80 +        OCTET_STRING_fromBuf(«$dst», (char *) c, 16);
    1.81 +    ||
    1.82 +}
    1.83 +
    1.84 +function "copy_UUID" {
    1.85 +    param "src", param "dst";
    1.86 +    ||
    1.87 +        TID_t *src = «$src»;
    1.88 +        TID_t *dst = «$dst»;
    1.89 +
    1.90 +        assert(src->size == 16);
    1.91 +        if (!(src->size == 16))
    1.92 +            return PEP_UNKNOWN_ERROR;
    1.93 +
    1.94 +        OCTET_STRING_fromBuf(dst, (char *) src->buf, src->size);
    1.95 +    ||
    1.96 +}
    1.97 +
    1.98 +action openChallenge
    1.99 +    call "new_UUID" with "dst" > &session->own_sync_state.challenge
   1.100 +
   1.101 +action storeChallenge call "copy_UUID" {
   1.102 +    with "src" > &session->sync_state.keysync.challenge
   1.103 +    with "dst" > &session->own_sync_state.challenge
   1.104 +}
   1.105 +
   1.106 +action openTransaction
   1.107 +    call "new_UUID" with "dst" > &session->own_sync_state.transaction
   1.108 +
   1.109 +action storeTransaction call "copy_UUID" {
   1.110 +    with "src" > &session->sync_state.keysync.transaction
   1.111 +    with "dst" >  &session->own_sync_state.transaction
   1.112 +}
   1.113 +
   1.114 +function "show_handshake" {
   1.115 +    param "type";
   1.116 +    ||
   1.117 +        assert(session->notifyHandshake);
   1.118 +        if (!session->notifyHandshake)
   1.119 +            return PEP_SYNC_NO_NOTIFY_CALLBACK;
   1.120 +     
   1.121 +        assert(session->sync_state.common.from);
   1.122 +        if (!session->sync_state.common.from)
   1.123 +            return PEP_ILLEGAL_VALUE;
   1.124 +
   1.125 +        pEp_identity *from = session->sync_state.common.from;
   1.126 +        pEp_identity *me = NULL;
   1.127 +        PEP_STATUS status = get_identity(session, from->address, PEP_OWN_USERID, &me);
   1.128 +        assert(status == PEP_STATUS_OK);
   1.129 +        if (status)
   1.130 +            return status;
   1.131 +
   1.132 +        assert(me->fpr && me->fpr[0]);
   1.133 +        if (!(me->fpr && me->fpr[0])) {
   1.134 +            free_identity(me);
   1.135 +            return PEP_ILLEGAL_VALUE;
   1.136 +        }
   1.137 +
   1.138 +        pEp_identity *partner = identity_dup(from);
   1.139 +        if (!partner) {
   1.140 +            free_identity(me);
   1.141 +            return PEP_OUT_OF_MEMORY;
   1.142 +        }
   1.143 +
   1.144 +        status = session->notifyHandshake(session->sync_management, me,
   1.145 +                partner, «$type»);
   1.146 +        if (status)
   1.147 +            return status;
   1.148 +    ||
   1.149 +}
   1.150 +
   1.151 +action showSoleHandshake
   1.152 +    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
   1.153 +
   1.154 +action showJoinGroupHandshake
   1.155 +    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
   1.156 +
   1.157 +action showGroupedHandshake
   1.158 +    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
   1.159 +
   1.160 +action saveGroupKeys
   1.161 +||
   1.162 +    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.identities, NULL);
   1.163 +    if (!il)
   1.164 +        return PEP_OUT_OF_MEMORY;
   1.165 +    
   1.166 +    // BUG: this should be a transaction and been rolled back completely on error
   1.167 +    for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
   1.168 +        PEP_STATUS status = set_identity(session, _il->ident);
   1.169 +        if (status) {
   1.170 +            free_identity_list(il);
   1.171 +            return status;
   1.172 +        }
   1.173 +    }
   1.174 +
   1.175 +    free_identity_list(il);
   1.176 +||
   1.177 +
   1.178 +action ownKeysAreGroupKeys {
   1.179 +    call "init_sql" with "sql" {
   1.180 +        ||
   1.181 +        "select fpr, username, comm_type, lang,"
   1.182 +                "   identity.flags | pgp_keypair.flags"
   1.183 +                "   from identity"
   1.184 +                "   join person on id = identity.user_id"
   1.185 +                "   join pgp_keypair on fpr = identity.main_key_id"
   1.186 +                "   join trust on id = trust.user_id"
   1.187 +                "       and pgp_keypair_fpr = identity.main_key_id"
   1.188 +        ||
   1.189 +        >         "   where identity.user_id = '" PEP_OWN_USERID "';"
   1.190 +    }
   1.191 +
   1.192 +    ||
   1.193 +        identity_list *il = new_identity_list(NULL);
   1.194 +        if (!il)
   1.195 +            return PEP_OUT_OF_MEMORY;
   1.196 +
   1.197 +        pEp_identity *from = session->sync_state.common.from;
   1.198 +        identity_list *_il = il;
   1.199 +
   1.200 +        int result;
   1.201 +        do {
   1.202 +            result = sqlite3_step(_sql);
   1.203 +            pEp_identity *_identity = NULL;
   1.204 +            switch (result) {
   1.205 +            case SQLITE_ROW:
   1.206 +                _identity = new_identity(
   1.207 +                        from->address,
   1.208 +                        (const char *) sqlite3_column_text(_sql, 0),
   1.209 +                        from->user_id,
   1.210 +                        (const char *) sqlite3_column_text(_sql, 1)
   1.211 +                        );
   1.212 +                assert(_identity);
   1.213 +                if (_identity == NULL)
   1.214 +                    return PEP_OUT_OF_MEMORY;
   1.215 +
   1.216 +                _identity->comm_type = (PEP_comm_type)
   1.217 +                    sqlite3_column_int(_sql, 2);
   1.218 +                const char* const _lang = (const char *)
   1.219 +                    sqlite3_column_text(_sql, 3);
   1.220 +                if (_lang && _lang[0]) {
   1.221 +                    assert(_lang[0] >= 'a' && _lang[0] <= 'z');
   1.222 +                    assert(_lang[1] >= 'a' && _lang[1] <= 'z');
   1.223 +                    assert(_lang[2] == 0);
   1.224 +                    _identity->lang[0] = _lang[0];
   1.225 +                    _identity->lang[1] = _lang[1];
   1.226 +                    _identity->lang[2] = 0;
   1.227 +                }
   1.228 +                _identity->flags = (unsigned int)
   1.229 +                    sqlite3_column_int(_sql, 4);
   1.230 +
   1.231 +                _il = identity_list_add(_il, _identity);
   1.232 +                if (!_il) {
   1.233 +                    free_identity_list(il);
   1.234 +                    free_identity(_identity);
   1.235 +                    return PEP_OUT_OF_MEMORY;
   1.236 +                }
   1.237 +                break;
   1.238 +
   1.239 +            case SQLITE_DONE:
   1.240 +                break;
   1.241 +
   1.242 +            default:
   1.243 +                free_identity_list(il);
   1.244 +                return PEP_UNKNOWN_ERROR;
   1.245 +            }
   1.246 +        } while (result != SQLITE_DONE);
   1.247 +
   1.248 +        IdentityList_t *r = IdentityList_from_identity_list(il, &session->sync_state.keysync.identities);
   1.249 +        free_identity_list(il);
   1.250 +        if (!r)
   1.251 +            return PEP_OUT_OF_MEMORY;
   1.252 +    ||
   1.253 +}
   1.254 +
   1.255 +action disable;
   1.256 +