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