src/blacklist.c
author Krista Bennett <krista@pep-project.org>
Wed, 15 Mar 2017 18:02:46 +0100
changeset 1674 5639f969866a
parent 1513 e7f7e42385b5
child 2478 0460dfb7f3dc
permissions -rw-r--r--
Added pgp_encrypt_only stubs for NetPGP
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     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
fdik@494
    16
    sqlite3_reset(session->blacklist_add);
fdik@494
    17
	sqlite3_bind_text(session->blacklist_add, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    18
fdik@494
    19
    int result;
fdik@494
    20
fdik@494
    21
    result = sqlite3_step(session->blacklist_add);
fdik@494
    22
    switch (result) {
fdik@494
    23
    case SQLITE_DONE:
fdik@494
    24
        status = PEP_STATUS_OK;
fdik@494
    25
        break;
fdik@494
    26
fdik@494
    27
    default:
fdik@494
    28
        status = PEP_UNKNOWN_ERROR;
fdik@494
    29
    }
fdik@494
    30
fdik@494
    31
    sqlite3_reset(session->blacklist_add);
fdik@494
    32
    return status;
fdik@494
    33
}
fdik@494
    34
fdik@494
    35
DYNAMIC_API PEP_STATUS blacklist_delete(PEP_SESSION session, const char *fpr)
fdik@494
    36
{
fdik@494
    37
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    38
fdik@494
    39
    assert(session && fpr && fpr[0]);
fdik@494
    40
fdik@494
    41
    if (!(session && fpr && fpr[0]))
fdik@494
    42
        return PEP_ILLEGAL_VALUE;
fdik@494
    43
fdik@494
    44
    sqlite3_reset(session->blacklist_delete);
fdik@494
    45
	sqlite3_bind_text(session->blacklist_delete, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    46
fdik@494
    47
    int result;
fdik@494
    48
fdik@494
    49
    result = sqlite3_step(session->blacklist_delete);
fdik@494
    50
    switch (result) {
fdik@494
    51
    case SQLITE_DONE:
fdik@494
    52
        status = PEP_STATUS_OK;
fdik@494
    53
        break;
fdik@494
    54
fdik@494
    55
    default:
fdik@494
    56
        status = PEP_UNKNOWN_ERROR;
fdik@494
    57
    }
fdik@494
    58
fdik@494
    59
    sqlite3_reset(session->blacklist_delete);
fdik@494
    60
    return status;
fdik@494
    61
}
fdik@494
    62
fdik@494
    63
DYNAMIC_API PEP_STATUS blacklist_is_listed(
fdik@494
    64
        PEP_SESSION session,
fdik@494
    65
        const char *fpr,
fdik@494
    66
        bool *listed
fdik@494
    67
    )
fdik@494
    68
{
fdik@494
    69
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
    70
    int count;
fdik@494
    71
fdik@494
    72
    assert(session && fpr && fpr[0] && listed);
fdik@494
    73
fdik@494
    74
    if (!(session && fpr && fpr[0] && listed))
fdik@494
    75
        return PEP_ILLEGAL_VALUE;
fdik@494
    76
fdik@494
    77
    *listed = false;
fdik@494
    78
fdik@494
    79
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
    80
    sqlite3_bind_text(session->blacklist_is_listed, 1, fpr, -1, SQLITE_STATIC);
fdik@494
    81
fdik@494
    82
    int result;
fdik@494
    83
fdik@494
    84
    result = sqlite3_step(session->blacklist_is_listed);
fdik@494
    85
    switch (result) {
fdik@494
    86
    case SQLITE_ROW:
fdik@494
    87
        count = sqlite3_column_int(session->blacklist_is_listed, 0);
fdik@494
    88
        *listed = count > 0;
fdik@494
    89
        status = PEP_STATUS_OK;
fdik@494
    90
        break;
fdik@494
    91
fdik@494
    92
    default:
fdik@494
    93
        status = PEP_UNKNOWN_ERROR;
fdik@494
    94
    }
fdik@494
    95
fdik@494
    96
    sqlite3_reset(session->blacklist_is_listed);
fdik@494
    97
    return status;
fdik@494
    98
}
fdik@494
    99
fdik@494
   100
DYNAMIC_API PEP_STATUS blacklist_retrieve(
fdik@494
   101
        PEP_SESSION session,
fdik@494
   102
        stringlist_t **blacklist
fdik@494
   103
    )
fdik@494
   104
{
fdik@494
   105
    PEP_STATUS status = PEP_STATUS_OK;
fdik@494
   106
fdik@494
   107
    assert(session);
fdik@494
   108
    assert(blacklist);
fdik@494
   109
fdik@494
   110
    if (!(session && blacklist))
fdik@494
   111
        return PEP_ILLEGAL_VALUE;
fdik@494
   112
fdik@494
   113
    *blacklist = NULL;
fdik@494
   114
    stringlist_t *_blacklist = new_stringlist(NULL);
fdik@494
   115
    if (_blacklist == NULL)
fdik@494
   116
        goto enomem;
fdik@494
   117
fdik@494
   118
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   119
fdik@494
   120
    int result;
fdik@494
   121
fdik@494
   122
    stringlist_t *_bl = _blacklist;
fdik@494
   123
    do {
fdik@494
   124
        result = sqlite3_step(session->blacklist_retrieve);
fdik@494
   125
        switch (result) {
fdik@494
   126
        case SQLITE_ROW:
roker@876
   127
        {
roker@876
   128
            const char *fpr = (const char *) sqlite3_column_text(session->blacklist_retrieve, 0);
fdik@494
   129
fdik@494
   130
            _bl = stringlist_add(_bl, fpr);
fdik@494
   131
            if (_bl == NULL)
fdik@494
   132
                goto enomem;
fdik@494
   133
fdik@494
   134
            break;
roker@876
   135
        }
fdik@494
   136
        case SQLITE_DONE:
fdik@494
   137
            break;
fdik@494
   138
fdik@494
   139
        default:
fdik@494
   140
            status = PEP_UNKNOWN_ERROR;
fdik@494
   141
            result = SQLITE_DONE;
fdik@494
   142
        }
fdik@494
   143
    } while (result != SQLITE_DONE);
fdik@494
   144
fdik@494
   145
    sqlite3_reset(session->blacklist_retrieve);
fdik@494
   146
    if (status == PEP_STATUS_OK)
fdik@494
   147
        *blacklist = _blacklist;
fdik@494
   148
    else
fdik@494
   149
        free_stringlist(_blacklist);
fdik@494
   150
fdik@494
   151
    goto the_end;
fdik@494
   152
fdik@494
   153
enomem:
fdik@494
   154
    free_stringlist(_blacklist);
fdik@494
   155
    status = PEP_OUT_OF_MEMORY;
fdik@494
   156
fdik@494
   157
the_end:
fdik@494
   158
    return status;
fdik@494
   159
}
fdik@494
   160