2003-01-09 14:29:36 +01:00
|
|
|
/* delkey.c - delete keys
|
2006-04-19 13:26:11 +02:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004,
|
|
|
|
* 2005, 2006 Free Software Foundation, Inc.
|
2003-01-09 14:29:36 +01:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-01-09 14:29:36 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-01-09 14:29:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2006-04-19 13:26:11 +02:00
|
|
|
#include "gpg.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
2007-11-19 17:03:50 +01:00
|
|
|
#include "status.h"
|
2003-01-09 14:29:36 +01:00
|
|
|
#include "iobuf.h"
|
|
|
|
#include "keydb.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "trustdb.h"
|
|
|
|
#include "filter.h"
|
|
|
|
#include "ttyio.h"
|
|
|
|
#include "status.h"
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Delete a public or secret key from a keyring.
|
|
|
|
* r_sec_avail will be set if a secret key is available and the public
|
|
|
|
* key can't be deleted for that reason.
|
|
|
|
*/
|
|
|
|
static int
|
2006-04-19 13:26:11 +02:00
|
|
|
do_delete_key( const char *username, int secret, int force, int *r_sec_avail )
|
2003-01-09 14:29:36 +01:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
KBNODE keyblock = NULL;
|
|
|
|
KBNODE node;
|
2010-04-21 18:26:17 +02:00
|
|
|
KEYDB_HANDLE hd = keydb_new ();
|
2003-01-09 14:29:36 +01:00
|
|
|
PKT_public_key *pk = NULL;
|
|
|
|
u32 keyid[2];
|
|
|
|
int okay=0;
|
|
|
|
int yes;
|
|
|
|
KEYDB_SEARCH_DESC desc;
|
|
|
|
int exactmatch;
|
|
|
|
|
|
|
|
*r_sec_avail = 0;
|
|
|
|
|
2009-12-08 17:30:33 +01:00
|
|
|
/* Search the userid */
|
2011-04-25 23:56:47 +02:00
|
|
|
rc = classify_user_id (username, &desc, 1);
|
2003-01-09 14:29:36 +01:00
|
|
|
exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR
|
|
|
|
|| desc.mode == KEYDB_SEARCH_MODE_FPR16
|
|
|
|
|| desc.mode == KEYDB_SEARCH_MODE_FPR20);
|
2009-12-08 17:30:33 +01:00
|
|
|
if (!rc)
|
|
|
|
rc = keydb_search (hd, &desc, 1);
|
2003-01-09 14:29:36 +01:00
|
|
|
if (rc) {
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error (_("key \"%s\" not found: %s\n"), username, g10_errstr (rc));
|
2003-01-09 14:29:36 +01:00
|
|
|
write_status_text( STATUS_DELETE_PROBLEM, "1" );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the keyblock */
|
|
|
|
rc = keydb_get_keyblock (hd, &keyblock );
|
|
|
|
if (rc) {
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
|
2003-01-09 14:29:36 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the keyid from the keyblock */
|
2011-04-29 12:01:52 +02:00
|
|
|
node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
|
2003-01-09 14:29:36 +01:00
|
|
|
if( !node ) {
|
|
|
|
log_error("Oops; key not found anymore!\n");
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = G10ERR_GENERAL;
|
2003-01-09 14:29:36 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2010-09-06 21:57:42 +02:00
|
|
|
pk = node->pkt->pkt.public_key;
|
|
|
|
keyid_from_pk( pk, keyid );
|
2011-02-04 12:57:53 +01:00
|
|
|
|
2011-04-29 12:01:52 +02:00
|
|
|
if (!secret && !force)
|
2006-04-19 13:26:11 +02:00
|
|
|
{
|
2010-09-06 21:57:42 +02:00
|
|
|
if (have_secret_key_with_kid (keyid))
|
|
|
|
{
|
|
|
|
*r_sec_avail = 1;
|
|
|
|
rc = -1;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rc = 0;
|
2006-04-19 13:26:11 +02:00
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
|
|
|
|
if( rc )
|
|
|
|
rc = 0;
|
|
|
|
else if (opt.batch && exactmatch)
|
|
|
|
okay++;
|
|
|
|
else if( opt.batch && secret )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("can't do this in batch mode\n"));
|
2003-01-09 14:29:36 +01:00
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
|
|
|
else if( opt.batch && opt.answer_yes )
|
|
|
|
okay++;
|
|
|
|
else if( opt.batch )
|
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error(_("can't do this in batch mode without \"--yes\"\n"));
|
2003-01-09 14:29:36 +01:00
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if( secret )
|
2010-09-06 21:57:42 +02:00
|
|
|
print_seckey_info (pk);
|
2003-01-09 14:29:36 +01:00
|
|
|
else
|
2010-09-06 21:57:42 +02:00
|
|
|
print_pubkey_info (NULL, pk );
|
2003-01-09 14:29:36 +01:00
|
|
|
tty_printf( "\n" );
|
|
|
|
|
|
|
|
yes = cpr_get_answer_is_yes( secret? "delete_key.secret.okay"
|
|
|
|
: "delete_key.okay",
|
2006-04-19 13:26:11 +02:00
|
|
|
_("Delete this key from the keyring? (y/N) "));
|
2003-01-09 14:29:36 +01:00
|
|
|
if( !cpr_enabled() && secret && yes ) {
|
|
|
|
/* I think it is not required to check a passphrase; if
|
|
|
|
* the user is so stupid as to let others access his secret keyring
|
|
|
|
* (and has no backup) - it is up him to read some very
|
|
|
|
* basic texts about security.
|
|
|
|
*/
|
|
|
|
yes = cpr_get_answer_is_yes("delete_key.secret.okay",
|
2006-04-19 13:26:11 +02:00
|
|
|
_("This is a secret key! - really delete? (y/N) "));
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
if( yes )
|
|
|
|
okay++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( okay ) {
|
2011-04-29 12:01:52 +02:00
|
|
|
if (secret)
|
|
|
|
{
|
|
|
|
log_error (_("deleting secret key not implemented\n"));
|
|
|
|
rc = gpg_error (GPG_ERR_NOT_IMPLEMENTED); /* FIXME */
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rc = keydb_delete_keyblock (hd);
|
|
|
|
if (rc) {
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error (_("deleting keyblock failed: %s\n"), g10_errstr(rc) );
|
2003-01-09 14:29:36 +01:00
|
|
|
goto leave;
|
2011-04-29 12:01:52 +02:00
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
2011-04-29 12:01:52 +02:00
|
|
|
/* Note that the ownertrust being cleared will trigger a
|
|
|
|
revalidation_mark(). This makes sense - only deleting keys
|
|
|
|
that have ownertrust set should trigger this. */
|
2003-01-09 14:29:36 +01:00
|
|
|
|
2011-04-29 12:01:52 +02:00
|
|
|
if (!secret && pk && clear_ownertrusts (pk)) {
|
|
|
|
if (opt.verbose)
|
|
|
|
log_info (_("ownertrust information cleared\n"));
|
|
|
|
}
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
keydb_release (hd);
|
|
|
|
release_kbnode (keyblock);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************
|
|
|
|
* Delete a public or secret key from a keyring.
|
|
|
|
*/
|
|
|
|
int
|
2006-10-02 13:54:35 +02:00
|
|
|
delete_keys( strlist_t names, int secret, int allow_both )
|
2003-01-09 14:29:36 +01:00
|
|
|
{
|
2006-04-19 13:26:11 +02:00
|
|
|
int rc, avail, force=(!allow_both && !secret && opt.expert);
|
|
|
|
|
|
|
|
/* Force allows us to delete a public key even if a secret key
|
|
|
|
exists. */
|
2003-01-09 14:29:36 +01:00
|
|
|
|
|
|
|
for(;names;names=names->next) {
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = do_delete_key (names->d, secret, force, &avail );
|
2011-02-04 12:57:53 +01:00
|
|
|
if ( rc && avail ) {
|
2003-01-09 14:29:36 +01:00
|
|
|
if ( allow_both ) {
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = do_delete_key (names->d, 1, 0, &avail );
|
2003-01-09 14:29:36 +01:00
|
|
|
if ( !rc )
|
2006-04-19 13:26:11 +02:00
|
|
|
rc = do_delete_key (names->d, 0, 0, &avail );
|
2003-01-09 14:29:36 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error(_(
|
|
|
|
"there is a secret key for public key \"%s\"!\n"),names->d);
|
|
|
|
log_info(_(
|
|
|
|
"use option \"--delete-secret-keys\" to delete it first.\n"));
|
|
|
|
write_status_text( STATUS_DELETE_PROBLEM, "2" );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(rc) {
|
2006-04-19 13:26:11 +02:00
|
|
|
log_error("%s: delete key failed: %s\n", names->d, g10_errstr(rc) );
|
2003-01-09 14:29:36 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|