LCOV - code coverage report
Current view: top level - src - tofupolicy.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 63 42.9 %
Date: 2016-12-01 18:45:36 Functions: 3 5 60.0 %

          Line data    Source code
       1             : /* tofupolicy.c - Tofu policy helpers.
       2             :  * Copyright (C) 2016 g10 Code GmbH
       3             :  *
       4             :  * This file is part of GPGME.
       5             :  *
       6             :  * GPGME is free software; you can redistribute it and/or modify it
       7             :  * under the terms of the GNU Lesser General Public License as
       8             :  * published by the Free Software Foundation; either version 2.1 of
       9             :  * the License, or (at your option) any later version.
      10             :  *
      11             :  * GPGME is distributed in the hope that it will be useful, but
      12             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14             :  * Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public
      17             :  * License along with this program; if not, see <https://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #if HAVE_CONFIG_H
      21             : #include <config.h>
      22             : #endif
      23             : #include <stdlib.h>
      24             : 
      25             : #include "gpgme.h"
      26             : #include "debug.h"
      27             : #include "context.h"
      28             : #include "ops.h"
      29             : 
      30             : 
      31             : typedef struct
      32             : {
      33             :   /* The error code from a FAILURE status line or 0.  */
      34             :   gpg_error_t failure_code;
      35             : 
      36             :   /* The error code from an ERROR status line or 0.  */
      37             :   gpg_error_t error_code;
      38             : 
      39             : } *op_data_t;
      40             : 
      41             : 
      42             : 
      43             : /* Parse an error status line.  Return the error location and the
      44             :    error code.  The function may modify ARGS. */
      45             : static char *
      46           0 : parse_error (char *args, gpg_error_t *r_err)
      47             : {
      48           0 :   char *where = strchr (args, ' ');
      49             :   char *which;
      50             : 
      51           0 :   if (where)
      52             :     {
      53           0 :       *where = '\0';
      54           0 :       which = where + 1;
      55             : 
      56           0 :       where = strchr (which, ' ');
      57           0 :       if (where)
      58           0 :         *where = '\0';
      59             : 
      60           0 :       where = args;
      61             :     }
      62             :   else
      63             :     {
      64           0 :       *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
      65           0 :       return NULL;
      66             :     }
      67             : 
      68           0 :   *r_err = atoi (which);
      69             : 
      70           0 :   return where;
      71             : }
      72             : 
      73             : 
      74             : static gpgme_error_t
      75           2 : tofu_policy_status_handler (void *priv, gpgme_status_code_t code, char *args)
      76             : {
      77           2 :   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
      78             :   gpgme_error_t err;
      79             :   void *hook;
      80             :   op_data_t opd;
      81             :   char *loc;
      82             : 
      83           2 :   err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook, -1, NULL);
      84           2 :   opd = hook;
      85           2 :   if (err)
      86           0 :     return err;
      87             : 
      88           2 :   switch (code)
      89             :     {
      90             :     case GPGME_STATUS_ERROR:
      91           0 :       loc = parse_error (args, &err);
      92           0 :       if (!loc)
      93           0 :         return err;
      94           0 :       if (!opd->error_code)
      95           0 :         opd->error_code = err;
      96           0 :       break;
      97             : 
      98             :     case GPGME_STATUS_FAILURE:
      99           0 :       opd->failure_code = _gpgme_parse_failure (args);
     100           0 :       break;
     101             : 
     102             :     case GPGME_STATUS_EOF:
     103           2 :       if (opd->error_code)
     104           0 :         err = opd->error_code;
     105           2 :       else if (opd->failure_code)
     106           0 :         err = opd->failure_code;
     107           2 :       break;
     108             : 
     109             :     default:
     110           0 :       break;
     111             :     }
     112             : 
     113           2 :   return err;
     114             : }
     115             : 
     116             : 
     117             : /* Set the TOFU policy for KEY to POLICY.  */
     118             : static gpgme_error_t
     119           2 : tofu_policy_start (gpgme_ctx_t ctx, int synchronous,
     120             :                    gpgme_key_t key, gpgme_tofu_policy_t policy)
     121             : {
     122             :   gpgme_error_t err;
     123             :   void *hook;
     124             :   op_data_t opd;
     125             : 
     126           2 :   if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
     127           0 :     return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
     128             : 
     129           2 :   if (!key)
     130           0 :     return gpg_error (GPG_ERR_INV_VALUE);
     131             : 
     132           2 :   err = _gpgme_op_reset (ctx, synchronous);
     133           2 :   if (err)
     134           0 :     return err;
     135             : 
     136           2 :   err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook,
     137             :                                sizeof (*opd), NULL);
     138           2 :   opd = hook;
     139           2 :   if (err)
     140           0 :     return err;
     141             : 
     142           2 :   _gpgme_engine_set_status_handler (ctx->engine, tofu_policy_status_handler,
     143             :                                     ctx);
     144             : 
     145           2 :   return _gpgme_engine_op_tofu_policy (ctx->engine, key, policy);
     146             : }
     147             : 
     148             : 
     149             : 
     150             : /* Set the TOFU policy of KEY to POLCIY.  This is the asynchronous
     151             :  * variant.  */
     152             : gpgme_error_t
     153           0 : gpgme_op_tofu_policy_start (gpgme_ctx_t ctx,
     154             :                             gpgme_key_t key, gpgme_tofu_policy_t policy)
     155             : {
     156             :   gpg_error_t err;
     157           0 :   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy_start", ctx,
     158             :               "key=%p, policy=%u", key, (unsigned int)policy);
     159             : 
     160           0 :   if (!ctx)
     161           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     162             : 
     163           0 :   err = tofu_policy_start (ctx, 0, key, policy);
     164           0 :   return TRACE_ERR (err);
     165             : }
     166             : 
     167             : 
     168             : /* This is the synchronous variant of gpgme_op_tofu_policy_start.  */
     169             : gpgme_error_t
     170           2 : gpgme_op_tofu_policy (gpgme_ctx_t ctx,
     171             :                       gpgme_key_t key, gpgme_tofu_policy_t policy)
     172             : {
     173             :   gpgme_error_t err;
     174           2 :   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy", ctx,
     175             :               "key=%p, policy=%u", key, (unsigned int)policy);
     176             : 
     177           2 :   if (!ctx)
     178           0 :     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
     179             : 
     180           2 :   err = tofu_policy_start (ctx, 1, key, policy);
     181           2 :   if (!err)
     182           2 :     err = _gpgme_wait_one (ctx);
     183           2 :   return TRACE_ERR (err);
     184             : }

Generated by: LCOV version 1.11