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