Blame src/delete.c

Packit Service 672cf4
/* delete.c - Delete a key.
Packit Service 672cf4
   Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
Packit Service 672cf4
Packit Service 672cf4
   This file is part of GPGME.
Packit Service 672cf4
Packit Service 672cf4
   GPGME is free software; you can redistribute it and/or modify it
Packit Service 672cf4
   under the terms of the GNU Lesser General Public License as
Packit Service 672cf4
   published by the Free Software Foundation; either version 2.1 of
Packit Service 672cf4
   the License, or (at your option) any later version.
Packit Service 672cf4
Packit Service 672cf4
   GPGME is distributed in the hope that it will be useful, but
Packit Service 672cf4
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 672cf4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 672cf4
   Lesser General Public License for more details.
Packit Service 672cf4
Packit Service 672cf4
   You should have received a copy of the GNU Lesser General Public
Packit Service 672cf4
   License along with this program; if not, write to the Free Software
Packit Service 672cf4
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
Packit Service 672cf4
   02111-1307, USA.  */
Packit Service 672cf4
Packit Service 672cf4
#if HAVE_CONFIG_H
Packit Service 672cf4
#include <config.h>
Packit Service 672cf4
#endif
Packit Service 672cf4
#include <stdlib.h>
Packit Service 672cf4
#include <errno.h>
Packit Service 672cf4
Packit Service 672cf4
#include "debug.h"
Packit Service 672cf4
#include "gpgme.h"
Packit Service 672cf4
#include "context.h"
Packit Service 672cf4
#include "ops.h"
Packit Service 672cf4
Packit Service 672cf4

Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
delete_status_handler (void *priv, gpgme_status_code_t code, char *args)
Packit Service 672cf4
{
Packit Service 672cf4
  (void)priv;
Packit Service 672cf4
Packit Service 672cf4
  if (code == GPGME_STATUS_DELETE_PROBLEM)
Packit Service 672cf4
    {
Packit Service 672cf4
      enum delete_problem
Packit Service 672cf4
	{
Packit Service 672cf4
	  DELETE_No_Problem = 0,
Packit Service 672cf4
	  DELETE_No_Such_Key = 1,
Packit Service 672cf4
	  DELETE_Must_Delete_Secret_Key = 2,
Packit Service 672cf4
	  DELETE_Ambiguous_Specification = 3
Packit Service 672cf4
	};
Packit Service 672cf4
      long problem;
Packit Service 672cf4
      char *tail;
Packit Service 672cf4
Packit Service 672cf4
      gpg_err_set_errno (0);
Packit Service 672cf4
      problem = strtol (args, &tail, 0);
Packit Service 672cf4
      if (errno || (*tail && *tail != ' '))
Packit Service 672cf4
	return trace_gpg_error (GPG_ERR_INV_ENGINE);
Packit Service 672cf4
Packit Service 672cf4
      switch (problem)
Packit Service 672cf4
	{
Packit Service 672cf4
	case DELETE_No_Problem:
Packit Service 672cf4
	  break;
Packit Service 672cf4
Packit Service 672cf4
	case DELETE_No_Such_Key:
Packit Service 672cf4
	  return gpg_error (GPG_ERR_NO_PUBKEY);
Packit Service 672cf4
Packit Service 672cf4
	case DELETE_Must_Delete_Secret_Key:
Packit Service 672cf4
	  return gpg_error (GPG_ERR_CONFLICT);
Packit Service 672cf4
Packit Service 672cf4
	case DELETE_Ambiguous_Specification:
Packit Service 672cf4
	  return gpg_error (GPG_ERR_AMBIGUOUS_NAME);
Packit Service 672cf4
Packit Service 672cf4
	}
Packit Service 672cf4
Packit Service 672cf4
      return gpg_error (GPG_ERR_GENERAL);
Packit Service 672cf4
    }
Packit Service 672cf4
  else if (code == GPGME_STATUS_ERROR)
Packit Service 672cf4
    {
Packit Service 672cf4
      /* Some error stati are informational, so we don't return an
Packit Service 672cf4
         error code if we are not ready to process this status.  */
Packit Service 672cf4
      gpgme_error_t err;
Packit Service 672cf4
      char *where = strchr (args, ' ');
Packit Service 672cf4
      char *which;
Packit Service 672cf4
Packit Service 672cf4
      if (where)
Packit Service 672cf4
	{
Packit Service 672cf4
	  *where = '\0';
Packit Service 672cf4
	  which = where + 1;
Packit Service 672cf4
Packit Service 672cf4
	  where = strchr (which, ' ');
Packit Service 672cf4
	  if (where)
Packit Service 672cf4
	    *where = '\0';
Packit Service 672cf4
Packit Service 672cf4
	  where = args;
Packit Service 672cf4
	}
Packit Service 672cf4
      else
Packit Service 672cf4
	return trace_gpg_error (GPG_ERR_INV_ENGINE);
Packit Service 672cf4
Packit Service 672cf4
      err = atoi (which);
Packit Service 672cf4
Packit Service 672cf4
      if (!strcmp (where, "delete_key.secret")
Packit Service 672cf4
	  && (gpg_err_code (err) == GPG_ERR_CANCELED
Packit Service 672cf4
	      || gpg_err_code (err) == GPG_ERR_FULLY_CANCELED))
Packit Service 672cf4
	{
Packit Service 672cf4
	  /* This indicates a user cancellation on the confirmation dialog.  */
Packit Service 672cf4
	  return gpg_error (gpg_err_code (err));
Packit Service 672cf4
	}
Packit Service 672cf4
    }
Packit Service 672cf4
  return 0;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
static gpgme_error_t
Packit Service 672cf4
delete_start (gpgme_ctx_t ctx, int synchronous, const gpgme_key_t key,
Packit Service 672cf4
	      unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 672cf4
  err = _gpgme_op_reset (ctx, synchronous);
Packit Service 672cf4
  if (err)
Packit Service 672cf4
    return err;
Packit Service 672cf4
Packit Service 672cf4
  _gpgme_engine_set_status_handler (ctx->engine, delete_status_handler, ctx);
Packit Service 672cf4
Packit Service 672cf4
  return _gpgme_engine_op_delete (ctx->engine, key, flags);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Delete KEY from the keyring.  If ALLOW_SECRET is non-zero, secret
Packit Service 672cf4
   keys are also deleted.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_delete_start (gpgme_ctx_t ctx, const gpgme_key_t key,
Packit Service 672cf4
		       int allow_secret)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 0ef63b
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_delete_start", ctx,
Packit Service 672cf4
	      "key=%p (%s), allow_secret=%i", key,
Packit Service 672cf4
	      (key->subkeys && key->subkeys->fpr) ?
Packit Service 672cf4
	      key->subkeys->fpr : "invalid", allow_secret);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = delete_start (ctx, 0, key,
Packit Service 672cf4
		      allow_secret ? GPGME_DELETE_ALLOW_SECRET : 0);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Delete KEY from the keyring.  If ALLOW_SECRET is non-zero, secret
Packit Service 672cf4
   keys are also deleted.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 0ef63b
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_delete", ctx,
Packit Service 672cf4
	      "key=%p (%s), allow_secret=%i", key,
Packit Service 672cf4
	      (key->subkeys && key->subkeys->fpr) ?
Packit Service 672cf4
	      key->subkeys->fpr : "invalid", allow_secret);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = delete_start (ctx, 1, key,
Packit Service 672cf4
		      allow_secret ? GPGME_DELETE_ALLOW_SECRET : 0);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return err;
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Delete KEY from the keyring.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_delete_ext_start (gpgme_ctx_t ctx, const gpgme_key_t key,
Packit Service 672cf4
			   unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 0ef63b
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_delete_ext_start", ctx,
Packit Service 672cf4
	      "key=%p (%s), flags=0x%x", key,
Packit Service 672cf4
	      (key->subkeys && key->subkeys->fpr) ?
Packit Service 672cf4
	      key->subkeys->fpr : "invalid", flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = delete_start (ctx, 0, key, flags);
Packit Service 672cf4
  return TRACE_ERR (err);
Packit Service 672cf4
}
Packit Service 672cf4
Packit Service 672cf4
Packit Service 672cf4
/* Delete KEY from the keyring.  */
Packit Service 672cf4
gpgme_error_t
Packit Service 672cf4
gpgme_op_delete_ext (gpgme_ctx_t ctx, const gpgme_key_t key,
Packit Service 672cf4
		     unsigned int flags)
Packit Service 672cf4
{
Packit Service 672cf4
  gpgme_error_t err;
Packit Service 672cf4
Packit Service 0ef63b
  TRACE_BEG  (DEBUG_CTX, "gpgme_op_delete_ext", ctx,
Packit Service 672cf4
	      "key=%p (%s), flags=0x%x", key,
Packit Service 672cf4
	      (key->subkeys && key->subkeys->fpr) ?
Packit Service 672cf4
	      key->subkeys->fpr : "invalid", flags);
Packit Service 672cf4
Packit Service 672cf4
  if (!ctx)
Packit Service 672cf4
    return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
Packit Service 672cf4
Packit Service 672cf4
  err = delete_start (ctx, 1, key, flags);
Packit Service 672cf4
  if (!err)
Packit Service 672cf4
    err = _gpgme_wait_one (ctx);
Packit Service 672cf4
  return err;
Packit Service 672cf4
}