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