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