LCOV - code coverage report
Current view: top level - src - passphrase.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 48 74 64.9 %
Date: 2018-11-15 08:49:49 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /* passphrase.c - Passphrase callback.
       2             :    Copyright (C) 2000 Werner Koch (dd9jn)
       3             :    Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH
       4             : 
       5             :    This file is part of GPGME.
       6             : 
       7             :    GPGME is free software; you can redistribute it and/or modify it
       8             :    under the terms of the GNU Lesser General Public License as
       9             :    published by the Free Software Foundation; either version 2.1 of
      10             :    the License, or (at your option) any later version.
      11             : 
      12             :    GPGME is distributed in the hope that it will be useful, but
      13             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :    Lesser General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU Lesser General Public
      18             :    License along with this program; if not, write to the Free Software
      19             :    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
      20             :    02111-1307, USA.  */
      21             : 
      22             : #if HAVE_CONFIG_H
      23             : #include <config.h>
      24             : #endif
      25             : #include <stdio.h>
      26             : #include <stdlib.h>
      27             : #include <string.h>
      28             : #include <assert.h>
      29             : #include <errno.h>
      30             : 
      31             : #include "gpgme.h"
      32             : #include "context.h"
      33             : #include "ops.h"
      34             : #include "util.h"
      35             : #include "debug.h"
      36             : 
      37             : 
      38             : typedef struct
      39             : {
      40             :   int no_passphrase;
      41             :   char *uid_hint;
      42             :   char *passphrase_info;
      43             :   int bad_passphrase;
      44             :   char *maxlen;
      45             : } *op_data_t;
      46             : 
      47             : 
      48             : static void
      49         153 : release_op_data (void *hook)
      50             : {
      51         153 :   op_data_t opd = (op_data_t) hook;
      52             : 
      53         153 :   if (opd->passphrase_info)
      54          40 :     free (opd->passphrase_info);
      55         153 :   if (opd->uid_hint)
      56           7 :     free (opd->uid_hint);
      57         153 :   free (opd->maxlen);
      58         153 : }
      59             : 
      60             : 
      61             : gpgme_error_t
      62        1704 : _gpgme_passphrase_status_handler (void *priv, gpgme_status_code_t code,
      63             :                                   char *args)
      64             : {
      65        1704 :   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
      66             :   gpgme_error_t err;
      67             :   void *hook;
      68             :   op_data_t opd;
      69             : 
      70        1704 :   err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, &hook,
      71             :                                sizeof (*opd), release_op_data);
      72        1704 :   opd = hook;
      73        1704 :   if (err)
      74           0 :     return err;
      75             : 
      76        1704 :   switch (code)
      77             :     {
      78             :     case GPGME_STATUS_INQUIRE_MAXLEN:
      79          55 :       free (opd->maxlen);
      80          55 :       if (!(opd->maxlen = strdup (args)))
      81           0 :         return gpg_error_from_syserror ();
      82          55 :       break;
      83             :     case GPGME_STATUS_USERID_HINT:
      84           7 :       if (opd->uid_hint)
      85           0 :         free (opd->uid_hint);
      86           7 :       if (!(opd->uid_hint = strdup (args)))
      87           0 :         return gpg_error_from_syserror ();
      88           7 :       break;
      89             : 
      90             :     case GPGME_STATUS_BAD_PASSPHRASE:
      91           0 :       opd->bad_passphrase++;
      92           0 :       opd->no_passphrase = 0;
      93           0 :       break;
      94             : 
      95             :     case GPGME_STATUS_GOOD_PASSPHRASE:
      96           0 :       opd->bad_passphrase = 0;
      97           0 :       opd->no_passphrase = 0;
      98           0 :       break;
      99             : 
     100             :     case GPGME_STATUS_NEED_PASSPHRASE:
     101             :     case GPGME_STATUS_NEED_PASSPHRASE_SYM:
     102             :     case GPGME_STATUS_NEED_PASSPHRASE_PIN:
     103          45 :       if (opd->passphrase_info)
     104           3 :         free (opd->passphrase_info);
     105          45 :       opd->passphrase_info = strdup (args);
     106          45 :       if (!opd->passphrase_info)
     107           0 :         return gpg_error_from_syserror ();
     108          45 :       break;
     109             : 
     110             :     case GPGME_STATUS_MISSING_PASSPHRASE:
     111           0 :       opd->no_passphrase = 1;
     112           0 :       break;
     113             : 
     114             :     case GPGME_STATUS_EOF:
     115         149 :       if (opd->no_passphrase || opd->bad_passphrase)
     116           0 :         return gpg_error (GPG_ERR_BAD_PASSPHRASE);
     117         149 :       break;
     118             : 
     119             :     case GPGME_STATUS_ERROR:
     120             :       /* We abuse this status handler to forward ERROR status codes to
     121             :          the caller.  */
     122           0 :       if (ctx->status_cb && !ctx->full_status)
     123             :         {
     124           0 :           err = ctx->status_cb (ctx->status_cb_value, "ERROR", args);
     125           0 :           if (err)
     126           0 :             return err;
     127             :         }
     128           0 :       break;
     129             : 
     130             :     case GPGME_STATUS_FAILURE:
     131             :       /* We abuse this status handler to forward FAILURE status codes
     132             :          to the caller.  */
     133           4 :       if (ctx->status_cb && !ctx->full_status)
     134             :         {
     135           0 :           err = ctx->status_cb (ctx->status_cb_value, "FAILURE", args);
     136           0 :           if (err)
     137           0 :             return err;
     138             :         }
     139           4 :       break;
     140             : 
     141             : 
     142             :     default:
     143             :       /* Ignore all other codes.  */
     144        1444 :       break;
     145             :     }
     146        1704 :   return 0;
     147             : }
     148             : 
     149             : 
     150             : gpgme_error_t
     151          69 : _gpgme_passphrase_command_handler (void *priv, gpgme_status_code_t code,
     152             :                                    const char *key, int fd, int *processed)
     153             : {
     154          69 :   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
     155             :   gpgme_error_t err;
     156             :   void *hook;
     157             :   op_data_t opd;
     158             : 
     159          69 :   assert (ctx->passphrase_cb);
     160             : 
     161          69 :   err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, &hook,
     162             :                                sizeof (*opd), release_op_data);
     163          69 :   opd = hook;
     164          69 :   if (err)
     165           0 :     return err;
     166             : 
     167          69 :   if (code == GPGME_STATUS_GET_HIDDEN
     168          32 :       && (!strcmp (key, "passphrase.enter")
     169           0 :           || !strcmp (key, "passphrase.pin.ask")))
     170             :     {
     171          32 :       if (processed)
     172          32 :         *processed = 1;
     173             : 
     174             :       /* Fake a status line to to convey the MAXLEN info.  */
     175          32 :       if (ctx->status_cb && opd->maxlen)
     176           0 :         err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN",
     177           0 :                               opd->maxlen);
     178             : 
     179          32 :       if (!err)
     180          64 :         err = ctx->passphrase_cb (ctx->passphrase_cb_value,
     181          32 :                                   opd->uid_hint, opd->passphrase_info,
     182             :                                   opd->bad_passphrase, fd);
     183             : 
     184             :       /* Reset bad passphrase flag, in case it is correct now.  */
     185          32 :       opd->bad_passphrase = 0;
     186             : 
     187          32 :       return err;
     188             :     }
     189             : 
     190          37 :   return 0;
     191             : }

Generated by: LCOV version 1.13