src/blacklist.c
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 2899 63b619aef131
parent 2478 0460dfb7f3dc
child 3739 523fb9710660
permissions -rw-r--r--
somewhat bigger
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
markus@525
     4
#include "pEp_internal.h"
fdik@494
     5
#include "blacklist.h"
fdik@494
     6
fdik@494
     7
DYNAMIC_API PEP_STATUS blacklist_add(PEP_SESSION session, const char *fpr)
fdik@494
     8
{
fdik@494
     9
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    10
fdik@494
    11
    assert(session && fpr && fpr[0]);
fdik@494
    12
fdik@494
    13
    if (!(session && fpr && fpr[0]))
fdik@494
    14
        return PEP_ILLEGAL_VALUE;
fdik@494
    15
krista@2478
    16
    sqlite3_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
krista@2478
    17
fdik@494
    18
    sqlite3_reset(session->blacklist_add);
fdik@494
    19
	sqlite3_bind_text(session->blacklist_add, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    20
fdik@494
    21
    int result;
fdik@494
    22
fdik@494
    23
    result = sqlite3_step(session->blacklist_add);
fdik@494
    24
    switch (result) {
fdik@494
    25
    case SQLITE_DONE:
fdik@494
    26
        status = PEP_STATUS_OK;
krista@2478
    27
        sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
fdik@494
    28
        break;
fdik@494
    29
fdik@494
    30
    default:
krista@2478
    31
        sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
fdik@494
    32
        status = PEP_UNKNOWN_ERROR;
fdik@494
    33
    }
fdik@494
    34
fdik@494
    35
    sqlite3_reset(session->blacklist_add);
fdik@494
    36
    return status;
fdik@494
    37
}
fdik@494
    38
fdik@494
    39
DYNAMIC_API PEP_STATUS blacklist_delete(PEP_SESSION session, const char *fpr)
fdik@494
    40
{
fdik@494
    41
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    42
fdik@494
    43
    assert(session && fpr && fpr[0]);
fdik@494
    44
fdik@494
    45
    if (!(session && fpr && fpr[0]))
fdik@494
    46
        return PEP_ILLEGAL_VALUE;
fdik@494
    47
fdik@494
    48
    sqlite3_reset(session->blacklist_delete);
fdik@494
    49
	sqlite3_bind_text(session->blacklist_delete, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    50
fdik@494
    51
    int result;
fdik@494
    52
fdik@494
    53
    result = sqlite3_step(session->blacklist_delete);
fdik@494
    54
    switch (result) {
fdik@494
    55
    case SQLITE_DONE:
fdik@494
    56
        status = PEP_STATUS_OK;
fdik@494
    57
        break;
fdik@494
    58
fdik@494
    59
    default:
fdik@494
    60
        status = PEP_UNKNOWN_ERROR;
fdik@494
    61
    }
fdik@494
    62
fdik@494
    63
    sqlite3_reset(session->blacklist_delete);
fdik@494
    64
    return status;
fdik@494
    65
}
fdik@494
    66
fdik@494
    67
DYNAMIC_API PEP_STATUS blacklist_is_listed(
fdik@494
    68
        PEP_SESSION session,
fdik@494
    69
        const char *fpr,
fdik@494
    70
        bool *listed
fdik@494
    71
    )
fdik@494
    72
{
fdik@494
    73
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    74
    int count;
fdik@494
    75
fdik@494
    76
    assert(session && fpr && fpr[0] && listed);
fdik@494
    77
fdik@494
    78
    if (!(session && fpr && fpr[0] && listed))
fdik@494
    79
        return PEP_ILLEGAL_VALUE;
fdik@494
    80
fdik@494
    81
    *listed = false;
fdik@494
    82
fdik@494
    83
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
    84
    sqlite3_bind_text(session->blacklist_is_listed, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    85
fdik@494
    86
    int result;
fdik@494
    87
fdik@494
    88
    result = sqlite3_step(session->blacklist_is_listed);
fdik@494
    89
    switch (result) {
fdik@494
    90
    case SQLITE_ROW:
fdik@494
    91
        count = sqlite3_column_int(session->blacklist_is_listed, 0);
fdik@494
    92
        *listed = count > 0;
fdik@494
    93
        status = PEP_STATUS_OK;
fdik@494
    94
        break;
fdik@494
    95
fdik@494
    96
    default:
fdik@494
    97
        status = PEP_UNKNOWN_ERROR;
fdik@494
    98
    }
fdik@494
    99
fdik@494
   100
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
   101
    return status;
fdik@494
   102
}
fdik@494
   103
fdik@494
   104
DYNAMIC_API PEP_STATUS blacklist_retrieve(
fdik@494
   105
        PEP_SESSION session,
fdik@494
   106
        stringlist_t **blacklist
fdik@494
   107
    )
fdik@494
   108
{
fdik@494
   109
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
   110
fdik@494
   111
    assert(session);
fdik@494
   112
    assert(blacklist);
fdik@494
   113
fdik@494
   114
    if (!(session && blacklist))
fdik@494
   115
        return PEP_ILLEGAL_VALUE;
fdik@494
   116
fdik@494
   117
    *blacklist = NULL;
fdik@494
   118
    stringlist_t *_blacklist = new_stringlist(NULL);
fdik@494
   119
    if (_blacklist == NULL)
fdik@494
   120
        goto enomem;
fdik@494
   121
fdik@494
   122
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   123
fdik@494
   124
    int result;
fdik@494
   125
fdik@494
   126
    stringlist_t *_bl = _blacklist;
fdik@494
   127
    do {
fdik@494
   128
        result = sqlite3_step(session->blacklist_retrieve);
fdik@494
   129
        switch (result) {
fdik@494
   130
        case SQLITE_ROW:
roker@879
   131
        {
roker@879
   132
            const char *fpr = (const char *) sqlite3_column_text(session->blacklist_retrieve, 0);
fdik@494
   133
fdik@494
   134
            _bl = stringlist_add(_bl, fpr);
fdik@494
   135
            if (_bl == NULL)
fdik@494
   136
                goto enomem;
fdik@494
   137
fdik@494
   138
            break;
roker@879
   139
        }
fdik@494
   140
        case SQLITE_DONE:
fdik@494
   141
            break;
fdik@494
   142
fdik@494
   143
        default:
fdik@494
   144
            status = PEP_UNKNOWN_ERROR;
fdik@494
   145
            result = SQLITE_DONE;
fdik@494
   146
        }
fdik@494
   147
    } while (result != SQLITE_DONE);
fdik@494
   148
fdik@494
   149
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   150
    if (status == PEP_STATUS_OK)
fdik@494
   151
        *blacklist = _blacklist;
fdik@494
   152
    else
fdik@494
   153
        free_stringlist(_blacklist);
fdik@494
   154
fdik@494
   155
    goto the_end;
fdik@494
   156
fdik@494
   157
enomem:
fdik@494
   158
    free_stringlist(_blacklist);
fdik@494
   159
    status = PEP_OUT_OF_MEMORY;
fdik@494
   160
fdik@494
   161
the_end:
fdik@494
   162
    return status;
fdik@494
   163
}