1998-10-21 17:34:36 +00:00
|
|
|
/* delkey.c - delete keys
|
2002-06-29 13:46:34 +00:00
|
|
|
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
1998-09-11 05:47:32 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* This file is part of GnuPG.
|
1998-09-11 05:47:32 +00:00
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
1998-09-11 05:47:32 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
1998-12-23 12:41:40 +00:00
|
|
|
* GnuPG is distributed in the hope that it will be useful,
|
1998-09-11 05:47:32 +00:00
|
|
|
* 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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "options.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "errors.h"
|
|
|
|
#include "iobuf.h"
|
|
|
|
#include "keydb.h"
|
2002-06-29 13:46:34 +00:00
|
|
|
#include "memory.h"
|
1998-09-11 05:47:32 +00:00
|
|
|
#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.
|
2002-06-29 13:46:34 +00:00
|
|
|
* r_sec_avail will be set if a secret key is available and the public
|
|
|
|
* key can't be deleted for that reason.
|
1998-09-11 05:47:32 +00:00
|
|
|
*/
|
2002-06-29 13:46:34 +00:00
|
|
|
static int
|
|
|
|
do_delete_key( const char *username, int secret, int *r_sec_avail )
|
1998-09-11 05:47:32 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
KBNODE keyblock = NULL;
|
|
|
|
KBNODE node;
|
2002-06-29 13:46:34 +00:00
|
|
|
KEYDB_HANDLE hd = keydb_new (secret);
|
1998-09-11 05:47:32 +00:00
|
|
|
PKT_public_key *pk = NULL;
|
|
|
|
PKT_secret_key *sk = NULL;
|
|
|
|
u32 keyid[2];
|
|
|
|
int okay=0;
|
|
|
|
int yes;
|
2002-06-29 13:46:34 +00:00
|
|
|
KEYDB_SEARCH_DESC desc;
|
2002-08-02 10:15:39 +00:00
|
|
|
int exactmatch;
|
2002-06-29 13:46:34 +00:00
|
|
|
|
|
|
|
*r_sec_avail = 0;
|
1998-09-11 05:47:32 +00:00
|
|
|
|
|
|
|
/* search the userid */
|
2002-06-29 13:46:34 +00:00
|
|
|
classify_user_id (username, &desc);
|
2002-08-02 10:15:39 +00:00
|
|
|
exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR
|
|
|
|
|| desc.mode == KEYDB_SEARCH_MODE_FPR16
|
|
|
|
|| desc.mode == KEYDB_SEARCH_MODE_FPR20);
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = desc.mode? keydb_search (hd, &desc, 1):G10ERR_INV_USER_ID;
|
|
|
|
if (rc) {
|
|
|
|
log_error (_("key `%s' not found: %s\n"), username, g10_errstr (rc));
|
2000-07-14 17:34:53 +00:00
|
|
|
write_status_text( STATUS_DELETE_PROBLEM, "1" );
|
1998-09-11 05:47:32 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
/* read the keyblock */
|
|
|
|
rc = keydb_get_keyblock (hd, &keyblock );
|
|
|
|
if (rc) {
|
|
|
|
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
1998-09-11 05:47:32 +00:00
|
|
|
/* get the keyid from the keyblock */
|
|
|
|
node = find_kbnode( keyblock, secret? PKT_SECRET_KEY:PKT_PUBLIC_KEY );
|
|
|
|
if( !node ) {
|
|
|
|
log_error("Oops; key not found anymore!\n");
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = G10ERR_GENERAL;
|
1998-09-11 05:47:32 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( secret ) {
|
|
|
|
sk = node->pkt->pkt.secret_key;
|
|
|
|
keyid_from_sk( sk, keyid );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pk = node->pkt->pkt.public_key;
|
|
|
|
keyid_from_pk( pk, keyid );
|
|
|
|
rc = seckey_available( keyid );
|
|
|
|
if( !rc ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
*r_sec_avail = 1;
|
|
|
|
rc = -1;
|
|
|
|
goto leave;
|
1998-09-11 05:47:32 +00:00
|
|
|
}
|
2002-06-29 13:46:34 +00:00
|
|
|
else if( rc != G10ERR_NO_SECKEY ) {
|
|
|
|
log_error("%s: get secret key: %s\n", username, g10_errstr(rc) );
|
2000-07-14 17:34:53 +00:00
|
|
|
}
|
1998-09-11 05:47:32 +00:00
|
|
|
else
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( rc )
|
|
|
|
rc = 0;
|
2002-08-02 10:15:39 +00:00
|
|
|
else if (opt.batch && exactmatch)
|
|
|
|
okay++;
|
1998-09-11 05:47:32 +00:00
|
|
|
else if( opt.batch && secret )
|
2002-08-02 10:15:39 +00:00
|
|
|
{
|
1998-09-11 05:47:32 +00:00
|
|
|
log_error(_("can't do that in batchmode\n"));
|
2002-08-02 10:15:39 +00:00
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
1998-09-11 05:47:32 +00:00
|
|
|
else if( opt.batch && opt.answer_yes )
|
|
|
|
okay++;
|
|
|
|
else if( opt.batch )
|
2002-08-02 10:15:39 +00:00
|
|
|
{
|
1998-09-11 05:47:32 +00:00
|
|
|
log_error(_("can't do that in batchmode without \"--yes\"\n"));
|
2002-08-02 10:15:39 +00:00
|
|
|
log_info (_("(unless you specify the key by fingerprint)\n"));
|
|
|
|
}
|
1998-09-11 05:47:32 +00:00
|
|
|
else {
|
2002-10-18 15:41:33 +00:00
|
|
|
if( secret )
|
|
|
|
print_seckey_info( sk );
|
|
|
|
else
|
2003-10-02 10:20:12 +00:00
|
|
|
print_pubkey_info(NULL, pk );
|
2002-10-18 15:41:33 +00:00
|
|
|
tty_printf( "\n" );
|
1998-09-11 05:47:32 +00:00
|
|
|
|
1998-11-27 11:42:49 +00:00
|
|
|
yes = cpr_get_answer_is_yes( secret? "delete_key.secret.okay"
|
|
|
|
: "delete_key.okay",
|
1998-09-11 05:47:32 +00:00
|
|
|
_("Delete this key from the keyring? "));
|
|
|
|
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.
|
|
|
|
*/
|
1998-11-27 11:42:49 +00:00
|
|
|
yes = cpr_get_answer_is_yes("delete_key.secret.okay",
|
1998-09-11 05:47:32 +00:00
|
|
|
_("This is a secret key! - really delete? "));
|
|
|
|
}
|
|
|
|
if( yes )
|
|
|
|
okay++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( okay ) {
|
2002-06-29 13:46:34 +00:00
|
|
|
rc = keydb_delete_keyblock (hd);
|
|
|
|
if (rc) {
|
|
|
|
log_error (_("deleting keyblock failed: %s\n"), g10_errstr(rc) );
|
1998-09-11 05:47:32 +00:00
|
|
|
goto leave;
|
|
|
|
}
|
2002-06-29 13:46:34 +00: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. */
|
|
|
|
|
2002-10-30 03:11:57 +00:00
|
|
|
if (!secret && pk && clear_ownertrusts (pk)) {
|
2002-06-29 13:46:34 +00:00
|
|
|
if (opt.verbose)
|
|
|
|
log_info (_("ownertrust information cleared\n"));
|
|
|
|
}
|
1998-09-11 05:47:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
2002-06-29 13:46:34 +00:00
|
|
|
keydb_release (hd);
|
|
|
|
release_kbnode (keyblock);
|
1998-09-11 05:47:32 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2002-06-29 13:46:34 +00:00
|
|
|
/****************
|
|
|
|
* Delete a public or secret key from a keyring.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
delete_keys( STRLIST names, int secret, int allow_both )
|
|
|
|
{
|
|
|
|
int rc, avail;
|
|
|
|
|
|
|
|
for(;names;names=names->next) {
|
|
|
|
rc = do_delete_key (names->d, secret, &avail );
|
|
|
|
if ( rc && avail ) {
|
|
|
|
if ( allow_both ) {
|
|
|
|
rc = do_delete_key (names->d, 1, &avail );
|
|
|
|
if ( !rc )
|
|
|
|
rc = do_delete_key (names->d, 0, &avail );
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
log_error("%s: delete key failed: %s\n", names->d, g10_errstr(rc) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|