1 /* delkey.c - delete keys
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
45 * Delete a public or secret key from a keyring.
46 * r_sec_avail will be set if a secret key is available and the public
47 * key can't be deleted for that reason.
50 do_delete_key( const char *username
, int secret
, int *r_sec_avail
)
53 KBNODE keyblock
= NULL
;
55 KEYDB_HANDLE hd
= keydb_new (secret
);
56 PKT_public_key
*pk
= NULL
;
57 PKT_secret_key
*sk
= NULL
;
61 KEYDB_SEARCH_DESC desc
;
66 /* search the userid */
67 classify_user_id (username
, &desc
);
68 exactmatch
= (desc
.mode
== KEYDB_SEARCH_MODE_FPR
69 || desc
.mode
== KEYDB_SEARCH_MODE_FPR16
70 || desc
.mode
== KEYDB_SEARCH_MODE_FPR20
);
71 rc
= desc
.mode
? keydb_search (hd
, &desc
, 1):GPG_ERR_INV_USER_ID
;
73 log_error (_("key `%s' not found: %s\n"), username
, gpg_strerror (rc
));
74 write_status_text( STATUS_DELETE_PROBLEM
, "1" );
78 /* read the keyblock */
79 rc
= keydb_get_keyblock (hd
, &keyblock
);
81 log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc
) );
85 /* get the keyid from the keyblock */
86 node
= find_kbnode( keyblock
, secret
? PKT_SECRET_KEY
:PKT_PUBLIC_KEY
);
88 log_error("Oops; key not found anymore!\n");
94 sk
= node
->pkt
->pkt
.secret_key
;
95 keyid_from_sk( sk
, keyid
);
98 pk
= node
->pkt
->pkt
.public_key
;
99 keyid_from_pk( pk
, keyid
);
100 rc
= seckey_available( keyid
);
106 else if( rc
!= GPG_ERR_NO_SECKEY
) {
107 log_error("%s: get secret key: %s\n", username
, gpg_strerror (rc
) );
115 else if (opt
.batch
&& exactmatch
)
117 else if( opt
.batch
&& secret
)
119 log_error(_("can't do that in batchmode\n"));
120 log_info (_("(unless you specify the key by fingerprint)\n"));
122 else if( opt
.batch
&& opt
.answer_yes
)
126 log_error(_("can't do that in batchmode without \"--yes\"\n"));
127 log_info (_("(unless you specify the key by fingerprint)\n"));
131 print_seckey_info( sk
);
133 print_pubkey_info (NULL
, pk
);
136 yes
= cpr_get_answer_is_yes( secret
? "delete_key.secret.okay"
138 _("Delete this key from the keyring? "));
139 if( !cpr_enabled() && secret
&& yes
) {
140 /* I think it is not required to check a passphrase; if
141 * the user is so stupid as to let others access his secret keyring
142 * (and has no backup) - it is up him to read some very
143 * basic texts about security.
145 yes
= cpr_get_answer_is_yes("delete_key.secret.okay",
146 _("This is a secret key! - really delete? "));
154 rc
= keydb_delete_keyblock (hd
);
156 log_error (_("deleting keyblock failed: %s\n"), gpg_strerror (rc
) );
160 /* Note that the ownertrust being cleared will trigger a
161 revalidation_mark(). This makes sense - only deleting keys
162 that have ownertrust set should trigger this. */
164 if (!secret
&& pk
&& clear_ownertrusts (pk
)) {
166 log_info (_("ownertrust information cleared\n"));
172 release_kbnode (keyblock
);
177 * Delete a public or secret key from a keyring.
180 delete_keys( STRLIST names
, int secret
, int allow_both
)
184 for(;names
;names
=names
->next
) {
185 rc
= do_delete_key (names
->d
, secret
, &avail
);
188 rc
= do_delete_key (names
->d
, 1, &avail
);
190 rc
= do_delete_key (names
->d
, 0, &avail
);
194 "there is a secret key for public key \"%s\"!\n"),names
->d
);
196 "use option \"--delete-secret-keys\" to delete it first.\n"));
197 write_status_text( STATUS_DELETE_PROBLEM
, "2" );
203 log_error("%s: delete key failed: %s\n", names
->d
, gpg_strerror (rc
) );