src/blacklist.c
author Volker Birk <vb@pep-project.org>
Fri, 17 May 2019 17:59:06 +0200
branchsync
changeset 3720 9ed76a79d784
parent 2478 0460dfb7f3dc
child 3739 523fb9710660
permissions -rw-r--r--
...
     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_exec(session->db, "BEGIN ;", NULL, NULL, NULL);
    17 
    18     sqlite3_reset(session->blacklist_add);
    19 	sqlite3_bind_text(session->blacklist_add, 1, fpr, -1, SQLITE_STATIC);
    20 
    21     int result;
    22 
    23     result = sqlite3_step(session->blacklist_add);
    24     switch (result) {
    25     case SQLITE_DONE:
    26         status = PEP_STATUS_OK;
    27         sqlite3_exec(session->db, "COMMIT ;", NULL, NULL, NULL);
    28         break;
    29 
    30     default:
    31         sqlite3_exec(session->db, "ROLLBACK ;", NULL, NULL, NULL);
    32         status = PEP_UNKNOWN_ERROR;
    33     }
    34 
    35     sqlite3_reset(session->blacklist_add);
    36     return status;
    37 }
    38 
    39 DYNAMIC_API PEP_STATUS blacklist_delete(PEP_SESSION session, const char *fpr)
    40 {
    41     PEP_STATUS status = PEP_STATUS_OK;
    42 
    43     assert(session && fpr && fpr[0]);
    44 
    45     if (!(session && fpr && fpr[0]))
    46         return PEP_ILLEGAL_VALUE;
    47 
    48     sqlite3_reset(session->blacklist_delete);
    49 	sqlite3_bind_text(session->blacklist_delete, 1, fpr, -1, SQLITE_STATIC);
    50 
    51     int result;
    52 
    53     result = sqlite3_step(session->blacklist_delete);
    54     switch (result) {
    55     case SQLITE_DONE:
    56         status = PEP_STATUS_OK;
    57         break;
    58 
    59     default:
    60         status = PEP_UNKNOWN_ERROR;
    61     }
    62 
    63     sqlite3_reset(session->blacklist_delete);
    64     return status;
    65 }
    66 
    67 DYNAMIC_API PEP_STATUS blacklist_is_listed(
    68         PEP_SESSION session,
    69         const char *fpr,
    70         bool *listed
    71     )
    72 {
    73     PEP_STATUS status = PEP_STATUS_OK;
    74     int count;
    75 
    76     assert(session && fpr && fpr[0] && listed);
    77 
    78     if (!(session && fpr && fpr[0] && listed))
    79         return PEP_ILLEGAL_VALUE;
    80 
    81     *listed = false;
    82 
    83     sqlite3_reset(session->blacklist_is_listed);
    84     sqlite3_bind_text(session->blacklist_is_listed, 1, fpr, -1, SQLITE_STATIC);
    85 
    86     int result;
    87 
    88     result = sqlite3_step(session->blacklist_is_listed);
    89     switch (result) {
    90     case SQLITE_ROW:
    91         count = sqlite3_column_int(session->blacklist_is_listed, 0);
    92         *listed = count > 0;
    93         status = PEP_STATUS_OK;
    94         break;
    95 
    96     default:
    97         status = PEP_UNKNOWN_ERROR;
    98     }
    99 
   100     sqlite3_reset(session->blacklist_is_listed);
   101     return status;
   102 }
   103 
   104 DYNAMIC_API PEP_STATUS blacklist_retrieve(
   105         PEP_SESSION session,
   106         stringlist_t **blacklist
   107     )
   108 {
   109     PEP_STATUS status = PEP_STATUS_OK;
   110 
   111     assert(session);
   112     assert(blacklist);
   113 
   114     if (!(session && blacklist))
   115         return PEP_ILLEGAL_VALUE;
   116 
   117     *blacklist = NULL;
   118     stringlist_t *_blacklist = new_stringlist(NULL);
   119     if (_blacklist == NULL)
   120         goto enomem;
   121 
   122     sqlite3_reset(session->blacklist_retrieve);
   123 
   124     int result;
   125 
   126     stringlist_t *_bl = _blacklist;
   127     do {
   128         result = sqlite3_step(session->blacklist_retrieve);
   129         switch (result) {
   130         case SQLITE_ROW:
   131         {
   132             const char *fpr = (const char *) sqlite3_column_text(session->blacklist_retrieve, 0);
   133 
   134             _bl = stringlist_add(_bl, fpr);
   135             if (_bl == NULL)
   136                 goto enomem;
   137 
   138             break;
   139         }
   140         case SQLITE_DONE:
   141             break;
   142 
   143         default:
   144             status = PEP_UNKNOWN_ERROR;
   145             result = SQLITE_DONE;
   146         }
   147     } while (result != SQLITE_DONE);
   148 
   149     sqlite3_reset(session->blacklist_retrieve);
   150     if (status == PEP_STATUS_OK)
   151         *blacklist = _blacklist;
   152     else
   153         free_stringlist(_blacklist);
   154 
   155     goto the_end;
   156 
   157 enomem:
   158     free_stringlist(_blacklist);
   159     status = PEP_OUT_OF_MEMORY;
   160 
   161 the_end:
   162     return status;
   163 }