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