src/blacklist.c
author fdik
Thu, 03 Mar 2016 19:27:46 +0100
changeset 494 ddf4130d00a0
child 520 e29a693d2748
child 547 ebba80433495
permissions -rw-r--r--
Added blacklist
fdik@494
     1
#include "blacklist.h"
fdik@494
     2
#include "pEp_internal.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
    goto the_end;
fdik@494
    30
fdik@494
    31
enomem:
fdik@494
    32
    status = PEP_OUT_OF_MEMORY;
fdik@494
    33
fdik@494
    34
the_end:
fdik@494
    35
    return status;
fdik@494
    36
}
fdik@494
    37
fdik@494
    38
DYNAMIC_API PEP_STATUS blacklist_delete(PEP_SESSION session, const char *fpr)
fdik@494
    39
{
fdik@494
    40
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    41
fdik@494
    42
    assert(session && fpr && fpr[0]);
fdik@494
    43
fdik@494
    44
    if (!(session && fpr && fpr[0]))
fdik@494
    45
        return PEP_ILLEGAL_VALUE;
fdik@494
    46
fdik@494
    47
    sqlite3_reset(session->blacklist_delete);
fdik@494
    48
	sqlite3_bind_text(session->blacklist_delete, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    49
fdik@494
    50
    int result;
fdik@494
    51
fdik@494
    52
    result = sqlite3_step(session->blacklist_delete);
fdik@494
    53
    switch (result) {
fdik@494
    54
    case SQLITE_DONE:
fdik@494
    55
        status = PEP_STATUS_OK;
fdik@494
    56
        break;
fdik@494
    57
fdik@494
    58
    default:
fdik@494
    59
        status = PEP_UNKNOWN_ERROR;
fdik@494
    60
    }
fdik@494
    61
fdik@494
    62
    sqlite3_reset(session->blacklist_delete);
fdik@494
    63
    goto the_end;
fdik@494
    64
fdik@494
    65
enomem:
fdik@494
    66
    status = PEP_OUT_OF_MEMORY;
fdik@494
    67
fdik@494
    68
the_end:
fdik@494
    69
    return status;
fdik@494
    70
}
fdik@494
    71
fdik@494
    72
DYNAMIC_API PEP_STATUS blacklist_is_listed(
fdik@494
    73
        PEP_SESSION session,
fdik@494
    74
        const char *fpr,
fdik@494
    75
        bool *listed
fdik@494
    76
    )
fdik@494
    77
{
fdik@494
    78
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    79
    int count;
fdik@494
    80
fdik@494
    81
    assert(session && fpr && fpr[0] && listed);
fdik@494
    82
fdik@494
    83
    if (!(session && fpr && fpr[0] && listed))
fdik@494
    84
        return PEP_ILLEGAL_VALUE;
fdik@494
    85
fdik@494
    86
    *listed = false;
fdik@494
    87
fdik@494
    88
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
    89
    sqlite3_bind_text(session->blacklist_is_listed, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    90
fdik@494
    91
    int result;
fdik@494
    92
fdik@494
    93
    result = sqlite3_step(session->blacklist_is_listed);
fdik@494
    94
    switch (result) {
fdik@494
    95
    case SQLITE_ROW:
fdik@494
    96
        count = sqlite3_column_int(session->blacklist_is_listed, 0);
fdik@494
    97
        *listed = count > 0;
fdik@494
    98
        status = PEP_STATUS_OK;
fdik@494
    99
        break;
fdik@494
   100
fdik@494
   101
    default:
fdik@494
   102
        status = PEP_UNKNOWN_ERROR;
fdik@494
   103
    }
fdik@494
   104
fdik@494
   105
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
   106
    goto the_end;
fdik@494
   107
fdik@494
   108
enomem:
fdik@494
   109
    status = PEP_OUT_OF_MEMORY;
fdik@494
   110
fdik@494
   111
the_end:
fdik@494
   112
    return status;
fdik@494
   113
}
fdik@494
   114
fdik@494
   115
DYNAMIC_API PEP_STATUS blacklist_retrieve(
fdik@494
   116
        PEP_SESSION session,
fdik@494
   117
        stringlist_t **blacklist
fdik@494
   118
    )
fdik@494
   119
{
fdik@494
   120
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
   121
fdik@494
   122
    assert(session);
fdik@494
   123
    assert(blacklist);
fdik@494
   124
fdik@494
   125
    if (!(session && blacklist))
fdik@494
   126
        return PEP_ILLEGAL_VALUE;
fdik@494
   127
fdik@494
   128
    *blacklist = NULL;
fdik@494
   129
    stringlist_t *_blacklist = new_stringlist(NULL);
fdik@494
   130
    if (_blacklist == NULL)
fdik@494
   131
        goto enomem;
fdik@494
   132
fdik@494
   133
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   134
fdik@494
   135
    int result;
fdik@494
   136
    const char *fpr = NULL;
fdik@494
   137
fdik@494
   138
    stringlist_t *_bl = _blacklist;
fdik@494
   139
    do {
fdik@494
   140
        result = sqlite3_step(session->blacklist_retrieve);
fdik@494
   141
        switch (result) {
fdik@494
   142
        case SQLITE_ROW:
fdik@494
   143
            fpr = (const char *) sqlite3_column_text(session->blacklist_retrieve, 0);
fdik@494
   144
fdik@494
   145
            _bl = stringlist_add(_bl, fpr);
fdik@494
   146
            if (_bl == NULL)
fdik@494
   147
                goto enomem;
fdik@494
   148
fdik@494
   149
            break;
fdik@494
   150
fdik@494
   151
        case SQLITE_DONE:
fdik@494
   152
            break;
fdik@494
   153
fdik@494
   154
        default:
fdik@494
   155
            status = PEP_UNKNOWN_ERROR;
fdik@494
   156
            result = SQLITE_DONE;
fdik@494
   157
        }
fdik@494
   158
    } while (result != SQLITE_DONE);
fdik@494
   159
fdik@494
   160
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   161
    if (status == PEP_STATUS_OK)
fdik@494
   162
        *blacklist = _blacklist;
fdik@494
   163
    else
fdik@494
   164
        free_stringlist(_blacklist);
fdik@494
   165
fdik@494
   166
    goto the_end;
fdik@494
   167
fdik@494
   168
enomem:
fdik@494
   169
    free_stringlist(_blacklist);
fdik@494
   170
    status = PEP_OUT_OF_MEMORY;
fdik@494
   171
fdik@494
   172
the_end:
fdik@494
   173
    return status;
fdik@494
   174
}
fdik@494
   175