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