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