sync/cond_act_sync.yml2
author Volker Birk <vb@pep.foundation>
Fri, 17 Aug 2018 15:23:08 +0200
branchsync
changeset 2856 451ac4260963
parent 2845 d474c5ebfec8
child 2871 4593a7b8b6a8
permissions -rw-r--r--
...
vb@2831
     1
// This file is under GNU General Public License 3.0
vb@2831
     2
// see LICENSE.txt
vb@2831
     3
vb@2831
     4
// generate conditions and actions
vb@2831
     5
vb@2845
     6
// Copyleft (c) 2017, 2018, p≡p foundation
vb@2831
     7
vb@2831
     8
// Written by Volker Birk
vb@2831
     9
vb@2831
    10
vb@2844
    11
include ./sql_func.yml2
vb@2831
    12
vb@2831
    13
// condition: PEP_STATUS «@name»(PEP_SESSION session, bool *result)
vb@2831
    14
vb@2831
    15
condition deviceGrouped {
vb@2831
    16
    call "exec_sql_int" with "sql"
vb@2831
    17
        > "select count(*) from identity where user_id = '"PEP_OWN_USERID"' and (flags & 4) = 4;"
vb@2831
    18
    |> *result = _result > 0;
vb@2831
    19
}
vb@2831
    20
vb@2831
    21
condition partnerIsGrouped
vb@2831
    22
|> *result = session->sync_state.keysync.is_group;
vb@2831
    23
vb@2831
    24
condition challengeAccepted
vb@2831
    25
||
vb@2831
    26
    TID_t *t1 = &session->sync_state.keysync.challenge;
vb@2831
    27
    TID_t *t2 = &session->own_sync_state.challenge;
vb@2831
    28
vb@2831
    29
    *result = t1->size == t2->size && memcmp(t1->buf, t2->buf, t1->size) == 0;
vb@2831
    30
||
vb@2831
    31
vb@2831
    32
condition keyElectionWon
vb@2831
    33
||
vb@2838
    34
    pEp_identity *from = session->sync_state.common.from;
vb@2831
    35
vb@2831
    36
    assert(from && from->fpr && from->fpr[0] && from->address && from->address[0]);
vb@2831
    37
    if (!(from && from->fpr && from->fpr[0] && from->address && from->address[0]))
vb@2831
    38
        return PEP_ILLEGAL_VALUE;
vb@2831
    39
vb@2831
    40
    pEp_identity *me = NULL;
vb@2831
    41
    PEP_STATUS status = get_identity(session, from->address, PEP_OWN_USERID, &me);
vb@2831
    42
    assert(status == PEP_STATUS_OK);
vb@2831
    43
    if (status)
vb@2831
    44
        return status;
vb@2831
    45
vb@2831
    46
    assert(me->fpr && me->fpr[0]);
vb@2831
    47
    if (!(me->fpr && me->fpr[0])) {
vb@2831
    48
        free_identity(me);
vb@2831
    49
        return PEP_ILLEGAL_VALUE;
vb@2831
    50
    }
vb@2831
    51
vb@2831
    52
    size_t len = MIN(strlen(from->fpr), strlen(me->fpr));
vb@2831
    53
    *result = strncasecmp(from->fpr, me->fpr, len) > 0;
vb@2831
    54
    free_identity(me);
vb@2831
    55
||
vb@2831
    56
vb@2831
    57
// action: PEP_STATUS «@name»(PEP_SESSION session)
vb@2831
    58
vb@2831
    59
action closeHandshakeDialog
vb@2831
    60
||
vb@2831
    61
    assert(session->notifyHandshake);
vb@2831
    62
    if (!session->notifyHandshake)
vb@2831
    63
        return PEP_SYNC_NO_NOTIFY_CALLBACK;
vb@2831
    64
vb@2831
    65
    PEP_STATUS status = session->notifyHandshake(
vb@2831
    66
            session->sync_management, NULL, NULL, SYNC_NOTIFY_OVERTAKEN);
vb@2831
    67
    if (status)
vb@2831
    68
        return status;
vb@2831
    69
||
vb@2831
    70
vb@2831
    71
function "new_UUID" {
vb@2831
    72
    param "dst";
vb@2831
    73
    ||
vb@2831
    74
        pEpUUID c;
vb@2831
    75
        uuid_generate_random(c);
vb@2831
    76
vb@2831
    77
        OCTET_STRING_fromBuf(«$dst», (char *) c, 16);
vb@2831
    78
    ||
vb@2831
    79
}
vb@2831
    80
vb@2831
    81
function "copy_UUID" {
vb@2831
    82
    param "src", param "dst";
vb@2831
    83
    ||
vb@2831
    84
        TID_t *src = «$src»;
vb@2831
    85
        TID_t *dst = «$dst»;
vb@2831
    86
vb@2831
    87
        assert(src->size == 16);
vb@2831
    88
        if (!(src->size == 16))
vb@2831
    89
            return PEP_UNKNOWN_ERROR;
vb@2831
    90
vb@2831
    91
        OCTET_STRING_fromBuf(dst, (char *) src->buf, src->size);
vb@2831
    92
    ||
vb@2831
    93
}
vb@2831
    94
vb@2856
    95
action clearState
vb@2856
    96
    |> free_Sync_state(session);
vb@2856
    97
vb@2831
    98
action openChallenge
vb@2831
    99
    call "new_UUID" with "dst" > &session->own_sync_state.challenge
vb@2831
   100
vb@2831
   101
action storeChallenge call "copy_UUID" {
vb@2831
   102
    with "src" > &session->sync_state.keysync.challenge
vb@2831
   103
    with "dst" > &session->own_sync_state.challenge
vb@2831
   104
}
vb@2831
   105
vb@2831
   106
action openTransaction
vb@2831
   107
    call "new_UUID" with "dst" > &session->own_sync_state.transaction
vb@2831
   108
vb@2831
   109
action storeTransaction call "copy_UUID" {
vb@2831
   110
    with "src" > &session->sync_state.keysync.transaction
vb@2831
   111
    with "dst" >  &session->own_sync_state.transaction
vb@2831
   112
}
vb@2831
   113
vb@2831
   114
function "show_handshake" {
vb@2831
   115
    param "type";
vb@2831
   116
    ||
vb@2831
   117
        assert(session->notifyHandshake);
vb@2831
   118
        if (!session->notifyHandshake)
vb@2831
   119
            return PEP_SYNC_NO_NOTIFY_CALLBACK;
vb@2831
   120
     
vb@2838
   121
        assert(session->sync_state.common.from);
vb@2838
   122
        if (!session->sync_state.common.from)
vb@2831
   123
            return PEP_ILLEGAL_VALUE;
vb@2831
   124
vb@2838
   125
        pEp_identity *from = session->sync_state.common.from;
vb@2831
   126
        pEp_identity *me = NULL;
vb@2831
   127
        PEP_STATUS status = get_identity(session, from->address, PEP_OWN_USERID, &me);
vb@2831
   128
        assert(status == PEP_STATUS_OK);
vb@2831
   129
        if (status)
vb@2831
   130
            return status;
vb@2831
   131
vb@2831
   132
        assert(me->fpr && me->fpr[0]);
vb@2831
   133
        if (!(me->fpr && me->fpr[0])) {
vb@2831
   134
            free_identity(me);
vb@2831
   135
            return PEP_ILLEGAL_VALUE;
vb@2831
   136
        }
vb@2831
   137
vb@2831
   138
        pEp_identity *partner = identity_dup(from);
vb@2831
   139
        if (!partner) {
vb@2831
   140
            free_identity(me);
vb@2831
   141
            return PEP_OUT_OF_MEMORY;
vb@2831
   142
        }
vb@2831
   143
vb@2831
   144
        status = session->notifyHandshake(session->sync_management, me,
vb@2831
   145
                partner, «$type»);
vb@2831
   146
        if (status)
vb@2831
   147
            return status;
vb@2831
   148
    ||
vb@2831
   149
}
vb@2831
   150
vb@2831
   151
action showSoleHandshake
vb@2831
   152
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_FORM_GROUP
vb@2831
   153
vb@2831
   154
action showJoinGroupHandshake
vb@2831
   155
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OUR_DEVICE
vb@2831
   156
vb@2831
   157
action showGroupedHandshake
vb@2831
   158
    call "show_handshake" with "type" > SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE
vb@2831
   159
vb@2831
   160
action saveGroupKeys
vb@2831
   161
||
vb@2831
   162
    identity_list *il = IdentityList_to_identity_list(&session->sync_state.keysync.identities, NULL);
vb@2831
   163
    if (!il)
vb@2831
   164
        return PEP_OUT_OF_MEMORY;
vb@2831
   165
    
vb@2831
   166
    // BUG: this should be a transaction and been rolled back completely on error
vb@2831
   167
    for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
vb@2831
   168
        PEP_STATUS status = set_identity(session, _il->ident);
vb@2831
   169
        if (status) {
vb@2831
   170
            free_identity_list(il);
vb@2831
   171
            return status;
vb@2831
   172
        }
vb@2831
   173
    }
vb@2831
   174
vb@2831
   175
    free_identity_list(il);
vb@2831
   176
||
vb@2831
   177
vb@2831
   178
action ownKeysAreGroupKeys {
vb@2831
   179
    call "init_sql" with "sql" {
vb@2831
   180
        ||
vb@2831
   181
        "select fpr, username, comm_type, lang,"
vb@2831
   182
                "   identity.flags | pgp_keypair.flags"
vb@2831
   183
                "   from identity"
vb@2831
   184
                "   join person on id = identity.user_id"
vb@2831
   185
                "   join pgp_keypair on fpr = identity.main_key_id"
vb@2831
   186
                "   join trust on id = trust.user_id"
vb@2831
   187
                "       and pgp_keypair_fpr = identity.main_key_id"
vb@2831
   188
        ||
vb@2831
   189
        >         "   where identity.user_id = '" PEP_OWN_USERID "';"
vb@2831
   190
    }
vb@2831
   191
vb@2831
   192
    ||
vb@2831
   193
        identity_list *il = new_identity_list(NULL);
vb@2831
   194
        if (!il)
vb@2831
   195
            return PEP_OUT_OF_MEMORY;
vb@2831
   196
vb@2838
   197
        pEp_identity *from = session->sync_state.common.from;
vb@2831
   198
        identity_list *_il = il;
vb@2831
   199
vb@2831
   200
        int result;
vb@2831
   201
        do {
vb@2831
   202
            result = sqlite3_step(_sql);
vb@2831
   203
            pEp_identity *_identity = NULL;
vb@2831
   204
            switch (result) {
vb@2831
   205
            case SQLITE_ROW:
vb@2831
   206
                _identity = new_identity(
vb@2831
   207
                        from->address,
vb@2831
   208
                        (const char *) sqlite3_column_text(_sql, 0),
vb@2831
   209
                        from->user_id,
vb@2831
   210
                        (const char *) sqlite3_column_text(_sql, 1)
vb@2831
   211
                        );
vb@2831
   212
                assert(_identity);
vb@2831
   213
                if (_identity == NULL)
vb@2831
   214
                    return PEP_OUT_OF_MEMORY;
vb@2831
   215
vb@2831
   216
                _identity->comm_type = (PEP_comm_type)
vb@2831
   217
                    sqlite3_column_int(_sql, 2);
vb@2831
   218
                const char* const _lang = (const char *)
vb@2831
   219
                    sqlite3_column_text(_sql, 3);
vb@2831
   220
                if (_lang && _lang[0]) {
vb@2831
   221
                    assert(_lang[0] >= 'a' && _lang[0] <= 'z');
vb@2831
   222
                    assert(_lang[1] >= 'a' && _lang[1] <= 'z');
vb@2831
   223
                    assert(_lang[2] == 0);
vb@2831
   224
                    _identity->lang[0] = _lang[0];
vb@2831
   225
                    _identity->lang[1] = _lang[1];
vb@2831
   226
                    _identity->lang[2] = 0;
vb@2831
   227
                }
vb@2831
   228
                _identity->flags = (unsigned int)
vb@2831
   229
                    sqlite3_column_int(_sql, 4);
vb@2831
   230
vb@2831
   231
                _il = identity_list_add(_il, _identity);
vb@2831
   232
                if (!_il) {
vb@2831
   233
                    free_identity_list(il);
vb@2831
   234
                    free_identity(_identity);
vb@2831
   235
                    return PEP_OUT_OF_MEMORY;
vb@2831
   236
                }
vb@2831
   237
                break;
vb@2831
   238
vb@2831
   239
            case SQLITE_DONE:
vb@2831
   240
                break;
vb@2831
   241
vb@2831
   242
            default:
vb@2831
   243
                free_identity_list(il);
vb@2831
   244
                return PEP_UNKNOWN_ERROR;
vb@2831
   245
            }
vb@2831
   246
        } while (result != SQLITE_DONE);
vb@2831
   247
vb@2831
   248
        IdentityList_t *r = IdentityList_from_identity_list(il, &session->sync_state.keysync.identities);
vb@2831
   249
        free_identity_list(il);
vb@2831
   250
        if (!r)
vb@2831
   251
            return PEP_OUT_OF_MEMORY;
vb@2831
   252
    ||
vb@2831
   253
}
vb@2831
   254
vb@2831
   255
action disable;
vb@2831
   256